Minor, rewritten test to user Paho instead of fusesource mqtt client

This commit is contained in:
andsel
2016-03-06 19:50:47 +01:00
parent efbfab7914
commit 6cadaebb0c
3 changed files with 71 additions and 79 deletions
@@ -81,18 +81,15 @@ public class MessageCollector implements MqttCallback {
@Override @Override
public void connectionLost(Throwable cause) { public void connectionLost(Throwable cause) {
System.out.println("connectionLost");
m_connectionLost = true; m_connectionLost = true;
} }
@Override @Override
public void messageArrived(String topic, MqttMessage message) throws Exception { public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Message received");
m_messages.offer(new ReceivedMessage(message, topic)); m_messages.offer(new ReceivedMessage(message, topic));
} }
@Override @Override
public void deliveryComplete(IMqttDeliveryToken token) { public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryComplete"); }
}
} }
@@ -22,7 +22,10 @@ import io.moquette.parser.proto.messages.ConnAckMessage;
import io.moquette.server.config.IConfig; import io.moquette.server.config.IConfig;
import io.moquette.server.config.MemoryConfig; import io.moquette.server.config.MemoryConfig;
import io.moquette.testclient.Client; import io.moquette.testclient.Client;
import org.fusesource.mqtt.client.*; import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -42,10 +45,11 @@ import static org.junit.Assert.*;
*/ */
public class ServerLowlevelMessagesIntegrationTests { public class ServerLowlevelMessagesIntegrationTests {
private static final Logger LOG = LoggerFactory.getLogger(ServerLowlevelMessagesIntegrationTests.class); private static final Logger LOG = LoggerFactory.getLogger(ServerLowlevelMessagesIntegrationTests.class);
static MqttClientPersistence s_dataStore;
Server m_server; Server m_server;
Client m_client; Client m_client;
MQTT m_subscriberDef; IMqttClient m_willSubscriber;
MessageCollector m_messageCollector;
IConfig m_config; IConfig m_config;
protected void startServer() throws IOException { protected void startServer() throws IOException {
@@ -59,9 +63,9 @@ public class ServerLowlevelMessagesIntegrationTests {
public void setUp() throws Exception { public void setUp() throws Exception {
startServer(); startServer();
m_client = new Client("localhost"); m_client = new Client("localhost");
m_subscriberDef = new MQTT(); m_willSubscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore);
m_subscriberDef.setHost("localhost", 1883); m_messageCollector = new MessageCollector();
m_subscriberDef.setClientId("Subscriber"); m_willSubscriber.setCallback(m_messageCollector);
} }
@After @After
@@ -95,10 +99,8 @@ public class ServerLowlevelMessagesIntegrationTests {
String willTestamentTopic = "/will/test"; String willTestamentTopic = "/will/test";
String willTestamentMsg = "Bye bye"; String willTestamentMsg = "Bye bye";
BlockingConnection willSubscriber = m_subscriberDef.blockingConnection(); m_willSubscriber.connect();
willSubscriber.connect(); m_willSubscriber.subscribe(willTestamentTopic, 0);
Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)};
willSubscriber.subscribe(topics);
int keepAlive = 2; //secs int keepAlive = 2; //secs
ConnectMessage connectMessage = new ConnectMessage(); ConnectMessage connectMessage = new ConnectMessage();
@@ -116,18 +118,17 @@ public class ServerLowlevelMessagesIntegrationTests {
//but after the 2 KEEP ALIVE timeout expires it gets fired, //but after the 2 KEEP ALIVE timeout expires it gets fired,
//NB it's 1,5 * KEEP_ALIVE so 3 secs and some millis to propagate the message //NB it's 1,5 * KEEP_ALIVE so 3 secs and some millis to propagate the message
Message msg = willSubscriber.receive(3300, TimeUnit.MILLISECONDS); MqttMessage msg = m_messageCollector.getMessage(3300);
long willMessageReceiveTime = System.currentTimeMillis(); long willMessageReceiveTime = System.currentTimeMillis();
if (msg == null) { if (msg == null) {
LOG.warn("testament message is null"); LOG.warn("testament message is null");
} }
assertNotNull("the will message should be fired after keep alive!", msg); assertNotNull("the will message should be fired after keep alive!", msg);
msg.ack();
//the will message hasn't to be received before the elapsing of Keep Alive timeout //the will message hasn't to be received before the elapsing of Keep Alive timeout
assertTrue(willMessageReceiveTime - connectTime > 3000); assertTrue(willMessageReceiveTime - connectTime > 3000);
assertEquals(willTestamentMsg, new String(msg.getPayload())); assertEquals(willTestamentMsg, new String(msg.getPayload()));
willSubscriber.disconnect(); m_willSubscriber.disconnect();
} }
AbstractMessage receivedMsg; AbstractMessage receivedMsg;
@@ -17,13 +17,16 @@ package io.moquette.server;
import io.moquette.server.config.IConfig; import io.moquette.server.config.IConfig;
import io.moquette.server.config.MemoryConfig; import io.moquette.server.config.MemoryConfig;
import org.fusesource.mqtt.client.*; import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@@ -33,12 +36,16 @@ import static org.junit.Assert.assertNull;
*/ */
public class ServerRestartIntegrationTest { public class ServerRestartIntegrationTest {
static MqttClientPersistence s_dataStore;
static MqttClientPersistence s_pubDataStore;
static MqttConnectOptions CLEAN_SESSION_OPT = new MqttConnectOptions();
Server m_server; Server m_server;
MQTT m_mqtt; IMqttClient m_subscriber;
BlockingConnection m_subscriber; IMqttClient m_publisher;
BlockingConnection m_publisher;
IConfig m_config; IConfig m_config;
MessageCollector m_messageCollector;
protected void startServer() throws IOException { protected void startServer() throws IOException {
m_server = new Server(); m_server = new Server();
final Properties configProps = IntegrationUtils.prepareTestPropeties(); final Properties configProps = IntegrationUtils.prepareTestPropeties();
@@ -46,23 +53,34 @@ public class ServerRestartIntegrationTest {
m_server.startServer(m_config); m_server.startServer(m_config);
} }
@BeforeClass
public static void beforeTests() {
String tmpDir = System.getProperty("java.io.tmpdir");
s_dataStore = new MqttDefaultFilePersistence(tmpDir);
s_pubDataStore = new MqttDefaultFilePersistence(tmpDir + File.separator + "publisher");
CLEAN_SESSION_OPT.setCleanSession(false);
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
startServer(); startServer();
String dbPath = IntegrationUtils.localMapDBPath();
IntegrationUtils.cleanPersistenceFile(dbPath);
m_mqtt = new MQTT(); m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore);
m_messageCollector = new MessageCollector();
m_subscriber.setCallback(m_messageCollector);
m_mqtt.setHost("localhost", 1883); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore);
m_mqtt.setCleanSession(false);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (m_subscriber != null) { if (m_subscriber != null && m_subscriber.isConnected()) {
m_subscriber.disconnect(); m_subscriber.disconnect();
} }
if (m_publisher != null) { if (m_publisher != null && m_publisher.isConnected()) {
m_publisher.disconnect(); m_publisher.disconnect();
} }
@@ -73,12 +91,9 @@ public class ServerRestartIntegrationTest {
@Test @Test
public void checkRestartCleanSubscriptionTree() throws Exception { public void checkRestartCleanSubscriptionTree() throws Exception {
m_mqtt.setClientId("Subscriber");
//subscribe to /topic //subscribe to /topic
m_subscriber = m_mqtt.blockingConnection(); m_subscriber.connect(CLEAN_SESSION_OPT);
m_subscriber.connect(); m_subscriber.subscribe("/topic", 1);
Topic[] topics = new Topic[]{new Topic("/topic", QoS.AT_LEAST_ONCE)};
m_subscriber.subscribe(topics);
m_subscriber.disconnect(); m_subscriber.disconnect();
//shutdown the server //shutdown the server
@@ -89,32 +104,24 @@ public class ServerRestartIntegrationTest {
m_server.startServer(IntegrationUtils.prepareTestPropeties()); m_server.startServer(IntegrationUtils.prepareTestPropeties());
//reconnect the Subscriber subscribing to the same /topic but different QoS //reconnect the Subscriber subscribing to the same /topic but different QoS
m_subscriber = m_mqtt.blockingConnection(); m_subscriber.connect(CLEAN_SESSION_OPT);
m_subscriber.connect(); m_subscriber.subscribe("/topic", 2);
topics = new Topic[]{new Topic("/topic", QoS.EXACTLY_ONCE)};
m_subscriber.subscribe(topics);
//should be just one registration so a publisher receive one notification //should be just one registration so a publisher receive one notification
MQTT mqtt = new MQTT(); m_publisher.connect(CLEAN_SESSION_OPT);
mqtt.setHost("localhost", 1883); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), 1, false);
mqtt.setClientId("Publisher");
m_publisher = mqtt.blockingConnection();
m_publisher.connect();
m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), QoS.AT_LEAST_ONCE, false);
//read the messages //read the messages
Message msg = m_subscriber.receive(); MqttMessage msg = m_messageCollector.getMessage(true);
msg.ack();
assertEquals("Hello world MQTT!!", new String(msg.getPayload())); assertEquals("Hello world MQTT!!", new String(msg.getPayload()));
//no more messages on the same topic will be received //no more messages on the same topic will be received
assertNull(m_subscriber.receive(1, TimeUnit.SECONDS)); assertNull(m_messageCollector.getMessage(1));
} }
@Test @Test
public void checkDontPublishInactiveClientsAfterServerRestart() throws Exception { public void checkDontPublishInactiveClientsAfterServerRestart() throws Exception {
m_mqtt.setClientId("SubPub"); IMqttClient conn = subscribeAndPublish("/topic");
BlockingConnection conn = subscribeAndPublish("/topic");
conn.disconnect(); conn.disconnect();
//shutdown the server //shutdown the server
@@ -123,25 +130,18 @@ public class ServerRestartIntegrationTest {
//restart the server //restart the server
m_server.startServer(IntegrationUtils.prepareTestPropeties()); m_server.startServer(IntegrationUtils.prepareTestPropeties());
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
mqtt.setClientId("Publisher");
m_publisher = mqtt.blockingConnection();
m_publisher.connect(); m_publisher.connect();
m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), QoS.AT_MOST_ONCE, false); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), 0, false);
} }
@Test @Test
public void testClientDoesntRemainSubscribedAfterASubscriptionAndServerRestart() throws Exception { public void testClientDoesntRemainSubscribedAfterASubscriptionAndServerRestart() throws Exception {
m_mqtt.setClientId("Subscriber");
//subscribe to /topic //subscribe to /topic
m_subscriber = m_mqtt.blockingConnection();
m_subscriber.connect(); m_subscriber.connect();
Topic[] topics = new Topic[]{new Topic("/topic", QoS.AT_MOST_ONCE)};
//subscribe /topic //subscribe /topic
m_subscriber.subscribe(topics); m_subscriber.subscribe("/topic", 0);
//unsubscribe from /topic //unsubscribe from /topic
m_subscriber.unsubscribe(new String[]{"/topic"}); m_subscriber.unsubscribe("/topic");
m_subscriber.disconnect(); m_subscriber.disconnect();
//shutdown the server //shutdown the server
@@ -150,38 +150,32 @@ public class ServerRestartIntegrationTest {
//restart the server //restart the server
m_server.startServer(IntegrationUtils.prepareTestPropeties()); m_server.startServer(IntegrationUtils.prepareTestPropeties());
//subscriber reconnects //subscriber reconnects
MQTT mqttSub = new MQTT(); m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore);
mqttSub.setHost("localhost", 1883); m_subscriber.setCallback(m_messageCollector);
mqttSub.setClientId("Subscriber");
m_subscriber = mqttSub.blockingConnection();
m_subscriber.connect(); m_subscriber.connect();
//publisher publishes on /topic //publisher publishes on /topic
MQTT mqtt = new MQTT(); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore);
mqtt.setHost("localhost", 1883);
mqtt.setClientId("Publisher");
m_publisher = mqtt.blockingConnection();
m_publisher.connect(); m_publisher.connect();
m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), QoS.AT_LEAST_ONCE, false); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), 1, false);
//Expected //Expected
//the subscriber doesn't get notified (it's fully unsubscribed) //the subscriber doesn't get notified (it's fully unsubscribed)
assertNull(m_subscriber.receive(1, TimeUnit.SECONDS)); assertNull(m_messageCollector.getMessage(1));
} }
/** /**
* Connect subscribe to topic and publish on the same topic * Connect subscribe to topic and publish on the same topic
* */ * */
private BlockingConnection subscribeAndPublish(String topic) throws Exception { private IMqttClient subscribeAndPublish(String topic) throws Exception {
BlockingConnection conn = m_mqtt.blockingConnection(); IMqttClient client = new MqttClient("tcp://localhost:1883", "SubPub");
conn.connect(); MessageCollector collector = new MessageCollector();
Topic[] topics = new Topic[]{new Topic(topic, QoS.AT_MOST_ONCE)}; client.setCallback(collector);
conn.subscribe(topics); client.connect();
conn.publish(topic, "Hello world MQTT!!".getBytes(), QoS.AT_MOST_ONCE, false); client.subscribe(topic, 1);
//read the message client.publish(topic, "Hello world MQTT!!".getBytes(), 0, false);
Message msg = conn.receive(); MqttMessage msg = collector.getMessage(true);
msg.ack();
assertEquals("Hello world MQTT!!", new String(msg.getPayload())); assertEquals("Hello world MQTT!!", new String(msg.getPayload()));
return conn; return client;
} }
} }