Fixed multiple publish notification for overlapping subscriptions (fix issue #52)
This commit is contained in:
@@ -245,4 +245,41 @@ public class ServerIntegrationFuseTest {
|
||||
msg.ack();
|
||||
assertEquals("Hello2", new String(msg.getPayload()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() throws Exception {
|
||||
LOG.info("*** checkSinglePublishOnOverlappingSubscriptions ***");
|
||||
MQTT mqtt = new MQTT();
|
||||
mqtt.setHost("localhost", 1883);
|
||||
mqtt.setClientId("Publisher");
|
||||
m_publisher = mqtt.blockingConnection();
|
||||
m_publisher.connect();
|
||||
|
||||
m_mqtt.setHost("localhost", 1883);
|
||||
m_mqtt.setCleanSession(false);
|
||||
m_mqtt.setClientId("Subscriber");
|
||||
m_subscriber = m_mqtt.blockingConnection();
|
||||
m_subscriber.connect();
|
||||
Topic[] topics = new Topic[]{
|
||||
new Topic("a/+", QoS.EXACTLY_ONCE),
|
||||
new Topic("a/b", QoS.AT_LEAST_ONCE)};
|
||||
m_subscriber.subscribe(topics);
|
||||
|
||||
//force the publisher to send
|
||||
m_publisher.publish("a/b", "Hello world MQTT!!".getBytes(), QoS.AT_LEAST_ONCE, false);
|
||||
|
||||
//reconnect and expect to receive the hello 2 message
|
||||
Message msg = m_subscriber.receive(1, TimeUnit.SECONDS);
|
||||
assertNotNull(msg);
|
||||
msg.ack();
|
||||
assertEquals("Hello world MQTT!!", new String(msg.getPayload()));
|
||||
//try to listen a second publish
|
||||
msg = m_subscriber.receive(1, TimeUnit.SECONDS);
|
||||
assertNull(msg);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-3
@@ -117,7 +117,7 @@ public class SubscriptionsStoreTest {
|
||||
store.add(anySub);
|
||||
assertTrue(store.matches("finance").contains(anySub));
|
||||
|
||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_2", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
store.add(financeAnySub);
|
||||
assertTrue(store.matches("finance").containsAll(Arrays.asList(financeAnySub, anySub)));
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public class SubscriptionsStoreTest {
|
||||
@Test
|
||||
public void testMatchingDeepMulti_one_layer() {
|
||||
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_2", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
store.add(anySub);
|
||||
store.add(financeAnySub);
|
||||
|
||||
@@ -184,7 +184,7 @@ public class SubscriptionsStoreTest {
|
||||
Subscription slashPlusSub = new Subscription("FAKE_CLI_ID_1", "/finance/+/ibm", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
store.add(slashPlusSub);
|
||||
|
||||
Subscription slashPlusDeepSub = new Subscription("FAKE_CLI_ID_1", "/+/stock/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
Subscription slashPlusDeepSub = new Subscription("FAKE_CLI_ID_2", "/+/stock/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||
store.add(slashPlusDeepSub);
|
||||
|
||||
//Verify
|
||||
@@ -270,6 +270,19 @@ public class SubscriptionsStoreTest {
|
||||
assertEquals(1, store.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverlappingSubscriptions() {
|
||||
Subscription genericSub = new Subscription("FAKE_CLI_ID_1", "a/+", AbstractMessage.QOSType.EXACTLY_ONCE, false);
|
||||
store.add(genericSub);
|
||||
Subscription specificSub = new Subscription("FAKE_CLI_ID_1", "a/b", AbstractMessage.QOSType.LEAST_ONE, false);
|
||||
store.add(specificSub);
|
||||
|
||||
//Verify
|
||||
assertEquals(1, store.matches("a/b").size());
|
||||
//assertTrue(store.matches("/finance").contains(slashPlusSub));
|
||||
//assertFalse(store.matches("/finance").contains(anySub));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchTopics_simple() {
|
||||
assertTrue(SubscriptionsStore.matchTopics("/", "/"));
|
||||
|
||||
Reference in New Issue
Block a user