Fixed bad qos publish, issue #56

This commit is contained in:
andrea
2015-06-28 19:53:22 +02:00
parent b8af814940
commit ac2eb6dce0
5 changed files with 89 additions and 27 deletions
@@ -17,6 +17,7 @@ package org.eclipse.moquette.server;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
import org.fusesource.mqtt.client.QoS;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -361,4 +362,47 @@ public class ServerIntegrationPahoTest {
assertEquals("Hello MQTT 2", message.toString());
}
/**
* subscriber A connect and subscribe on "a/b" QoS 1
* subscriber B connect and subscribe on "a/+" BUT with QoS 2
* publisher connects and send a message "hello" on "a/b"
* subscriber A must receive a notification with QoS1
* subscriber B must receive a notification with QoS2
*/
@Test
public void checkSubscribersGetCorrectQosNotifications() throws Exception {
LOG.info("*** checkSubscribersGetCorrectQosNotifications ***");
String tmpDir = System.getProperty("java.io.tmpdir");
MqttClientPersistence dsSubscriberA = new MqttDefaultFilePersistence(tmpDir + File.separator + "subscriberA");
MqttClient subscriberA = new MqttClient("tcp://localhost:1883", "SubscriberA", dsSubscriberA);
TestCallback cbSubscriberA = new TestCallback();
subscriberA.setCallback(cbSubscriberA);
subscriberA.connect();
subscriberA.subscribe("a/b", 1);
MqttClientPersistence dsSubscriberB = new MqttDefaultFilePersistence(tmpDir + File.separator + "subscriberB");
MqttClient subscriberB = new MqttClient("tcp://localhost:1883", "SubscriberB", dsSubscriberB);
TestCallback cbSubscriberB = new TestCallback();
subscriberB.setCallback(cbSubscriberB);
subscriberB.connect();
subscriberB.subscribe("a/+", 2);
m_client.connect();
m_client.publish("a/b", "Hello world MQTT!!".getBytes(), 2, false);
MqttMessage messageOnA = cbSubscriberA.getMessage(true);
assertEquals("Hello world MQTT!!", new String(messageOnA.getPayload()));
assertEquals(1, messageOnA.getQos());
subscriberA.disconnect();
MqttMessage messageOnB = cbSubscriberB.getMessage(true);
assertEquals("Hello world MQTT!!", new String(messageOnB.getPayload()));
assertEquals(2, messageOnB.getQos());
subscriberB.disconnect();
}
}
@@ -695,4 +695,39 @@ public class ProtocolProcessorTest {
});
assertTrue(messages.isEmpty());
}
List<PublishEvent> publishedForwarded = new ArrayList<>();
@Test
public void testForwardPublishWithCorrectQos() {
ByteBuffer payload = ByteBuffer.wrap("Hello world MQTT!!".getBytes());
PublishEvent forwardPublish = new PublishEvent("a/b", QOSType.EXACTLY_ONCE, payload, true, "Publisher", 1);
IMessagesStore memoryMessageStore = new MemoryStorageService();
Subscription subQos1 = new Subscription("Sub A", "a/b", QOSType.LEAST_ONE, false);
Subscription subQos2 = new Subscription("Sub B", "a/+", QOSType.EXACTLY_ONCE, false);
SubscriptionsStore subscriptions = new SubscriptionsStore();
subscriptions.add(subQos1);
subscriptions.add(subQos2);
ProtocolProcessor processor = new ProtocolProcessor() {
@Override
protected void sendPublish(String clientId, String topic, AbstractMessage.QOSType qos, ByteBuffer message,
boolean retained, Integer messageID) {
publishedForwarded.add(new PublishEvent(topic, qos, message, retained, clientId, messageID));
}
};
processor.init(subscriptions, memoryMessageStore, null, null, true, null);
//Exercise
processor.forward2Subscribers(forwardPublish);
//Verify
assertEquals(2, publishedForwarded.size());
assertEquals(subQos1.getClientId(), publishedForwarded.get(0).getClientID());
assertEquals(subQos1.getRequestedQos(), publishedForwarded.get(0).getQos());
assertEquals(subQos2.getClientId(), publishedForwarded.get(1).getClientID());
assertEquals(subQos2.getRequestedQos(), publishedForwarded.get(1).getQos());
}
}