Fixed multiple publish notification for overlapping subscriptions (fix issue #52)

This commit is contained in:
andrea
2015-06-23 23:23:38 +02:00
parent 336c452ef3
commit 7dece38b57
5 changed files with 124 additions and 6 deletions
@@ -67,6 +67,7 @@ public class SubscriptionsStore {
String s = "";
for (int i=0; i < deep; i++) {
s += "\t";
// s += "--";
}
return s;
}
@@ -225,10 +226,20 @@ public class SubscriptionsStore {
return Collections.emptyList();
}
Queue<Token> tokenQueue = new LinkedBlockingDeque<Token>(tokens);
List<Subscription> matchingSubs = new ArrayList<Subscription>();
Queue<Token> tokenQueue = new LinkedBlockingDeque<>(tokens);
List<Subscription> matchingSubs = new ArrayList<>();
subscriptions.matches(tokenQueue, matchingSubs);
return matchingSubs;
//remove the overlapping subscriptions, selecting ones with greatest qos
Map<String, Subscription> subsForClient = new HashMap<>();
for (Subscription sub : matchingSubs) {
Subscription existingSub = subsForClient.get(sub.getClientId());
//update the selected subscriptions if not present or if has a greater qos
if (existingSub == null || existingSub.getRequestedQos().ordinal() < sub.getRequestedQos().ordinal()) {
subsForClient.put(sub.getClientId(), sub);
}
}
return /*matchingSubs*/new ArrayList<>(subsForClient.values());
}
public boolean contains(Subscription sub) {
@@ -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);
}
}
@@ -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("/", "/"));