m_publisher.kill();
@Override public void kill() throws Exception { connection.kill(); }
@Test(timeout = 60 * 1000) public void testClientConnectionFailure() throws Exception { MQTT mqtt = createMQTTConnection("reconnect", false); mqtt.setKeepAlive((short) 1); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Wait.waitFor(() -> connection.isConnected()); final String TOPIC = "TopicA"; final byte[] qos = connection.subscribe(new Topic[]{new Topic(TOPIC, QoS.EXACTLY_ONCE)}); assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]); connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false); // kill transport connection.kill(); // FIXME Wait for the previous connection to timeout. This is not required in ActiveMQ. Needs investigating. Thread.sleep(10000); final BlockingConnection newConnection = mqtt.blockingConnection(); newConnection.connect(); Wait.waitFor(() -> newConnection.isConnected()); assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]); Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS); assertNotNull(msg); assertEquals(TOPIC, new String(msg.getPayload())); msg.ack(); newConnection.disconnect(); }
@Test(timeout = 60 * 1000) public void testClientConnectionFailureSendsWillMessage() throws Exception { getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false); MQTT mqtt = createMQTTConnection("1", false); mqtt.setKeepAlive((short) 1); mqtt.setWillMessage("test message"); mqtt.setWillTopic("will"); mqtt.setWillQos(QoS.AT_LEAST_ONCE); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Wait.waitFor(() -> connection.isConnected()); MQTT mqtt2 = createMQTTConnection("2", false); BlockingConnection connection2 = mqtt2.blockingConnection(); connection2.connect(); connection2.subscribe(new Topic[]{new Topic("will", QoS.AT_LEAST_ONCE)}); // kill transport connection.kill(); // FIXME Wait for the previous connection to timeout. This is not required in ActiveMQ. Needs investigating. Thread.sleep(10000); Message m = connection2.receive(1000, TimeUnit.MILLISECONDS); assertEquals("test message", new String(m.getPayload())); }
@Test(timeout = 60 * 1000) public void testLinkRouteAmqpReceiveMQTT() throws Exception { MQTT mqtt = createMQTTConnection(); mqtt.setClientId("TestClient"); BlockingConnection blockingConnection = mqtt.blockingConnection(); blockingConnection.connect(); Topic t = new Topic("test", QoS.AT_LEAST_ONCE); blockingConnection.subscribe(new Topic[]{t}); AmqpClient client = new AmqpClient(new URI(AMQP_URI), null, null); AmqpConnection connection = client.connect(); try { AmqpSession session = connection.createSession(); AmqpSender sender = session.createSender("test", true); AmqpMessage message = new AmqpMessage(); message.setText("Test-Message"); sender.send(message); sender.close(); } finally { connection.close(); } try { blockingConnection.subscribe(new Topic[]{t}); assertNotNull(blockingConnection.receive(5, TimeUnit.SECONDS)); } finally { blockingConnection.kill(); } }
@Test(timeout = 60 * 1000) public void testWillMessageIsRetained() throws Exception { getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false); MQTT mqtt = createMQTTConnection("1", false); mqtt.setKeepAlive((short) 1); mqtt.setWillMessage("test message"); mqtt.setWillTopic("will"); mqtt.setWillQos(QoS.AT_LEAST_ONCE); mqtt.setWillRetain(true); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Wait.waitFor(() -> connection.isConnected()); // kill transport connection.kill(); Thread.sleep(10000); MQTT mqtt2 = createMQTTConnection("2", false); BlockingConnection connection2 = mqtt2.blockingConnection(); connection2.connect(); connection2.subscribe(new Topic[]{new Topic("will", QoS.AT_LEAST_ONCE)}); Message m = connection2.receive(1000, TimeUnit.MILLISECONDS); assertNotNull(m); m.ack(); assertEquals("test message", new String(m.getPayload())); }