protected void connect(MQTTConnection connection, String clientId) { MqttConnectMessage connectMessage = ConnectionTestUtils.buildConnect(clientId); connect(connection, connectMessage); }
/** * subscriber connect and subscribe on "a/b" QoS 1 and "a/+" QoS 2 publisher connects and send a * message "hello" on "a/b" subscriber must receive only a single message not twice */ @Test public void checkSinglePublishOnOverlappingSubscriptions() { final MQTTConnection publisher = connectAs("Publisher"); connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, "a/b", AT_LEAST_ONCE); subscribe(connection, "a/+", EXACTLY_ONCE); // force the publisher to send publishQos1(publisher, "a/b", "Hello world MQTT!!", 60); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!"); ConnectionTestUtils.verifyNoPublishIsReceived(channel); } }
@Test public void avoidMultipleNotificationsAfterMultipleReconnection_cleanSessionFalseQoS1() { final MqttConnectMessage notCleanConnect = ConnectionTestUtils.buildConnectNotClean(FAKE_CLIENT_ID); connect(connection, notCleanConnect); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); connection.processDisconnect(null); // connect on another channel final String firstPayload = "Hello MQTT 1"; connectPublishDisconnectFromAnotherClient(firstPayload, NEWS_TOPIC); // reconnect FAKE_CLIENT on another channel EmbeddedChannel anotherChannel2 = new EmbeddedChannel(); MQTTConnection anotherConnection2 = createMQTTConnection(CONFIG, anotherChannel2); anotherConnection2.processConnect(notCleanConnect); ConnectionTestUtils.assertConnectAccepted(anotherChannel2); ConnectionTestUtils.verifyPublishIsReceived(anotherChannel2, MqttQoS.AT_LEAST_ONCE, firstPayload); anotherConnection2.processDisconnect(null); final String secondPayload = "Hello MQTT 2"; connectPublishDisconnectFromAnotherClient(secondPayload, NEWS_TOPIC); EmbeddedChannel anotherChannel3 = new EmbeddedChannel(); MQTTConnection anotherConnection3 = createMQTTConnection(CONFIG, anotherChannel3); anotherConnection3.processConnect(notCleanConnect); ConnectionTestUtils.assertConnectAccepted(anotherChannel3); ConnectionTestUtils.verifyPublishIsReceived(anotherChannel3, MqttQoS.AT_LEAST_ONCE, secondPayload); }
/** * subscriber connect and subscribe on "topic" subscriber disconnects publisher connects and * send two message "hello1" "hello2" to "topic" subscriber connects again and receive "hello1" * "hello2" */ @Test public void checkQoS2SubscriberDisconnectReceivePersistedPublishes() { connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, EXACTLY_ONCE); connection.processDisconnect(null); final MQTTConnection publisher = connectAs("Publisher"); publishQos2(publisher, NEWS_TOPIC, "Hello world MQTT!!-1"); publishQos2(publisher, NEWS_TOPIC, "Hello world MQTT!!-2"); createMQTTConnection(CONFIG); connect(this.connection, FAKE_CLIENT_ID); ConnectionTestUtils.verifyPublishIsReceived(channel, EXACTLY_ONCE, "Hello world MQTT!!-1"); ConnectionTestUtils.verifyPublishIsReceived(channel, EXACTLY_ONCE, "Hello world MQTT!!-2"); }
@Test public void testUnsubscribeWithBadFormattedTopic() { connect(this.connection, FAKE_CLIENT_ID); // Exercise sut.unsubscribe(singletonList(BAD_FORMATTED_TOPIC), connection, 1); // Verify assertFalse("Unsubscribe with bad topic MUST close drop the connection, (issue 68)", channel.isOpen()); }
@Test public void checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1() { final MQTTConnection publisher = connectAs("Publisher"); connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); // publish from another channel publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-1", 99); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-1"); connection.processDisconnect(null); publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-2", 100); publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-3", 101); createMQTTConnection(CONFIG); connect(this.connection, FAKE_CLIENT_ID); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-2"); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-3"); }
@Test public void testDontNotifyClientSubscribedToTopicAfterDisconnectedAndReconnectOnSameChannel() { connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_MOST_ONCE); // publish on /news final ByteBuf payload = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload, false, MqttMessageBuilders.publish() .payload(payload.retainedDuplicate()) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_MOST_ONCE, "Hello world!"); unsubscribeAndVerifyAck(NEWS_TOPIC); // publish on /news final ByteBuf payload2 = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload2, false, MqttMessageBuilders.publish() .payload(payload) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyNoPublishIsReceived(channel); }
@Test public void testDontNotifyClientSubscribedToTopicAfterDisconnectedAndReconnectOnNewChannel() { connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_MOST_ONCE); // publish on /news final ByteBuf payload = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload, false, MqttMessageBuilders.publish() .payload(payload.retainedDuplicate()) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_MOST_ONCE, "Hello world!"); unsubscribeAndVerifyAck(NEWS_TOPIC); connection.processDisconnect(null); // connect on another channel EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConnection = createMQTTConnection(CONFIG, anotherChannel); anotherConnection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(anotherChannel); // publish on /news final ByteBuf payload2 = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload2, false, MqttMessageBuilders.publish() .payload(payload2) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyNoPublishIsReceived(anotherChannel); }
@Test public void testConnectSubPub_cycle_getTimeout_on_second_disconnect_issue142() { connect(connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_MOST_ONCE);