|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package io.streamnative.pulsar.handlers.mqtt.mqtt3.paho.proxy; |
| 16 | + |
| 17 | +import io.streamnative.pulsar.handlers.mqtt.base.MQTTTestBase; |
| 18 | +import io.streamnative.pulsar.handlers.mqtt.common.MQTTCommonConfiguration; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; |
| 25 | +import org.eclipse.paho.client.mqttv3.IMqttMessageListener; |
| 26 | +import org.eclipse.paho.client.mqttv3.MqttAsyncClient; |
| 27 | +import org.eclipse.paho.client.mqttv3.MqttCallback; |
| 28 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 29 | +import org.eclipse.paho.client.mqttv3.MqttMessage; |
| 30 | +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
| 31 | +import org.testng.Assert; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +@Test |
| 35 | +@Slf4j |
| 36 | +public class TestProxyConnectMultiBroker extends MQTTTestBase { |
| 37 | + |
| 38 | + @Override |
| 39 | + protected MQTTCommonConfiguration initConfig() throws Exception { |
| 40 | + MQTTCommonConfiguration mqtt = super.initConfig(); |
| 41 | + mqtt.setDefaultNumberOfNamespaceBundles(4); |
| 42 | + mqtt.setMqttProxyEnabled(true); |
| 43 | + return mqtt; |
| 44 | + } |
| 45 | + |
| 46 | + public static class Callback implements MqttCallback { |
| 47 | + |
| 48 | + @Override |
| 49 | + public void connectionLost(Throwable throwable) { |
| 50 | + log.info("Connection lost"); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { |
| 55 | + log.info("Message arrived"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test(timeOut = 1000 * 60 * 5) |
| 64 | + public void testProxyConnectMultiBroker() throws Exception { |
| 65 | + int port = getMqttProxyPortList().get(0); |
| 66 | + MqttAsyncClient client = new MqttAsyncClient("tcp://localhost:" + port, "test", new MemoryPersistence()); |
| 67 | + MqttConnectOptions connectOptions = new MqttConnectOptions(); |
| 68 | + connectOptions.setCleanSession(true); |
| 69 | + connectOptions.setKeepAliveInterval(5); |
| 70 | + log.info("connecting..."); |
| 71 | + client.connect(connectOptions).waitForCompletion(); |
| 72 | + log.info("connected"); |
| 73 | + |
| 74 | + client.subscribe("testsub1", 1).waitForCompletion(); |
| 75 | + log.info("subscribed testsub1"); |
| 76 | + // sleep the keep alive period to show that PING will happen in abscence of other messages. |
| 77 | + Thread.sleep(6000); |
| 78 | + |
| 79 | + // make more subscriptions to connect to multiple brokers. |
| 80 | + client.subscribe("testsub2", 1).waitForCompletion(); |
| 81 | + log.info("subscribed testsub2"); |
| 82 | + client.subscribe("testsub3", 1).waitForCompletion(); |
| 83 | + log.info("subscribed testsub3"); |
| 84 | + Map<String, List<String>> msgs = new HashMap<>(); |
| 85 | + String topic = "test1"; |
| 86 | + client.subscribe(topic, 1, new IMqttMessageListener() { |
| 87 | + |
| 88 | + @Override |
| 89 | + public void messageArrived(String topic, MqttMessage message) throws Exception { |
| 90 | + msgs.compute(topic, (k, v) -> { |
| 91 | + if (v == null) { |
| 92 | + v = new ArrayList<>(); |
| 93 | + } |
| 94 | + v.add(new String(message.getPayload())); |
| 95 | + return v; |
| 96 | + }); |
| 97 | + } |
| 98 | + }).waitForCompletion(); |
| 99 | + |
| 100 | + // publish QoS 1 message to prevent the need for PINGREQ. Keep alive only sends ping in abscence of other |
| 101 | + // messages. Refer to section 3.1.2.10 of the MQTT 3.1.1 specification. |
| 102 | + for (int i = 0; i < 130; i++) { |
| 103 | + log.info("Publishing message..." + System.currentTimeMillis()); |
| 104 | + client.publish(topic, "test".getBytes(), 1, false).waitForCompletion(); |
| 105 | + Thread.sleep(1000); |
| 106 | + } |
| 107 | + Assert.assertNotNull(msgs.get(topic) != null); |
| 108 | + Assert.assertEquals(msgs.get(topic).size(), 130); |
| 109 | + client.disconnect().waitForCompletion(); |
| 110 | + } |
| 111 | +} |
0 commit comments