2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2015-08-08 19:36:14 +02:00
|
|
|
* Copyright (c) 2012-2015 The original author or authors
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server;
|
2013-09-19 20:14:07 +02:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.server.config.IConfig;
|
|
|
|
|
import io.moquette.server.config.MemoryConfig;
|
2013-09-19 20:14:07 +02:00
|
|
|
import org.eclipse.paho.client.mqttv3.*;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
2015-03-15 16:45:11 +01:00
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
2013-09-19 20:14:07 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2015-03-11 10:07:27 -04:00
|
|
|
import java.util.Properties;
|
2013-09-19 20:14:07 +02:00
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
public class ServerIntegrationPahoTest {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationPahoTest.class);
|
|
|
|
|
|
|
|
|
|
static MqttClientPersistence s_dataStore;
|
2013-11-25 18:53:50 +01:00
|
|
|
static MqttClientPersistence s_pubDataStore;
|
|
|
|
|
|
2013-09-19 20:14:07 +02:00
|
|
|
Server m_server;
|
|
|
|
|
IMqttClient m_client;
|
|
|
|
|
TestCallback m_callback;
|
2015-08-22 15:35:11 +02:00
|
|
|
IConfig m_config;
|
2013-09-19 20:14:07 +02:00
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void beforeTests() {
|
|
|
|
|
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
s_dataStore = new MqttDefaultFilePersistence(tmpDir);
|
2013-11-25 18:53:50 +01:00
|
|
|
s_pubDataStore = new MqttDefaultFilePersistence(tmpDir + File.separator + "publisher");
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void startServer() throws IOException {
|
|
|
|
|
m_server = new Server();
|
2015-08-22 15:35:11 +02:00
|
|
|
final Properties configProps = IntegrationUtils.prepareTestPropeties();
|
|
|
|
|
m_config = new MemoryConfig(configProps);
|
|
|
|
|
m_server.startServer(m_config);
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
2015-04-07 07:51:44 +02:00
|
|
|
String dbPath = IntegrationUtils.localMapDBPath();
|
2015-11-15 13:17:46 +01:00
|
|
|
IntegrationUtils.cleanPersistenceFile(dbPath);
|
2015-12-06 11:43:29 +01:00
|
|
|
|
2013-09-19 20:14:07 +02:00
|
|
|
startServer();
|
|
|
|
|
|
|
|
|
|
m_client = new MqttClient("tcp://localhost:1883", "TestClient", s_dataStore);
|
|
|
|
|
m_callback = new TestCallback();
|
|
|
|
|
m_client.setCallback(m_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
|
if (m_client.isConnected()) {
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 07:51:44 +02:00
|
|
|
stopServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopServer() {
|
2013-09-19 20:14:07 +02:00
|
|
|
m_server.stopServer();
|
2015-11-08 18:25:55 +01:00
|
|
|
IntegrationUtils.cleanPersistenceFile(m_config);
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSubscribe() throws Exception {
|
|
|
|
|
LOG.info("*** testSubscribe ***");
|
|
|
|
|
m_client.connect();
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
|
|
|
|
|
MqttMessage message = new MqttMessage("Hello world!!".getBytes());
|
|
|
|
|
message.setQos(0);
|
|
|
|
|
message.setRetained(false);
|
|
|
|
|
m_client.publish("/topic", message);
|
|
|
|
|
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
}
|
2014-07-26 18:22:32 +02:00
|
|
|
|
2013-09-19 20:14:07 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCleanSession_maintainClientSubscriptions() throws Exception {
|
|
|
|
|
LOG.info("*** testCleanSession_maintainClientSubscriptions ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//reconnect and publish
|
|
|
|
|
MqttClient anotherClient = new MqttClient("tcp://localhost:1883", "TestClient", s_dataStore);
|
|
|
|
|
m_callback = new TestCallback();
|
|
|
|
|
anotherClient.setCallback(m_callback);
|
|
|
|
|
anotherClient.connect(options);
|
|
|
|
|
anotherClient.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCleanSession_maintainClientSubscriptions_againstClientDestruction() throws Exception {
|
|
|
|
|
LOG.info("*** testCleanSession_maintainClientSubscriptions_againstClientDestruction ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//reconnect and publish
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that after a client has connected with clean session false, subscribed
|
2015-12-06 11:43:29 +01:00
|
|
|
* to some topic and exited, if it reconnects with clean session true, the m_server
|
2013-09-19 20:14:07 +02:00
|
|
|
* correctly cleanup every previous subscription
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testCleanSession_correctlyClientSubscriptions() throws Exception {
|
|
|
|
|
LOG.info("*** testCleanSession_correctlyClientSubscriptions ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//reconnect and publish
|
2015-12-06 11:43:29 +01:00
|
|
|
m_client.connect(options);
|
2013-09-19 20:14:07 +02:00
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
2015-11-08 18:25:55 +01:00
|
|
|
assertEquals("Test my payload", new String(m_callback.getMessage(false).getPayload()));
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCleanSession_maintainClientSubscriptions_withServerRestart() throws Exception {
|
|
|
|
|
LOG.info("*** testCleanSession_maintainClientSubscriptions_withServerRestart ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
m_server.stopServer();
|
|
|
|
|
|
2015-04-07 07:51:44 +02:00
|
|
|
m_server.startServer(IntegrationUtils.prepareTestPropeties());
|
2013-09-19 20:14:07 +02:00
|
|
|
|
|
|
|
|
//reconnect and publish
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testRetain_maintainMessage_againstClientDestruction() throws Exception {
|
|
|
|
|
LOG.info("*** testRetain_maintainMessage_againstClientDestruction ***");
|
|
|
|
|
m_client.connect();
|
2014-07-31 00:08:41 +02:00
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 1, true);
|
2013-09-19 20:14:07 +02:00
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//reconnect and publish
|
|
|
|
|
m_client.connect();
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testUnsubscribe_do_not_notify_anymore_same_session() throws Exception {
|
|
|
|
|
LOG.info("*** testUnsubscribe_do_not_notify_anymore_same_session ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
//m_client.disconnect();
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
|
|
|
|
|
m_client.unsubscribe("/topic");
|
|
|
|
|
m_callback.reinit();
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
2013-12-26 10:59:09 +01:00
|
|
|
assertNull(m_callback.getMessage(false));
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testUnsubscribe_do_not_notify_anymore_new_session() throws Exception {
|
|
|
|
|
LOG.info("*** testUnsubscribe_do_not_notify_anymore_new_session ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
//m_client.disconnect();
|
|
|
|
|
assertEquals("/topic", m_callback.getTopic());
|
|
|
|
|
|
|
|
|
|
m_client.unsubscribe("/topic");
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
m_callback.reinit();
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.publish("/topic", "Test my payload".getBytes(), 0, false);
|
|
|
|
|
|
2013-12-26 10:59:09 +01:00
|
|
|
assertNull(m_callback.getMessage(false));
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPublishWithQoS1() throws Exception {
|
|
|
|
|
LOG.info("*** testPublishWithQoS1 ***");
|
|
|
|
|
m_client.connect();
|
|
|
|
|
m_client.subscribe("/topic", 1);
|
|
|
|
|
m_client.publish("/topic", "Hello MQTT".getBytes(), 1, false);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//reconnect and publish
|
2013-12-26 10:59:09 +01:00
|
|
|
MqttMessage message = m_callback.getMessage(true);
|
|
|
|
|
assertEquals("Hello MQTT", message.toString());
|
|
|
|
|
assertEquals(1, message.getQos());
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPublishWithQoS1_notCleanSession() throws Exception {
|
|
|
|
|
LOG.info("*** testPublishWithQoS1_notCleanSession ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 1);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//publish a QoS 1 message another client publish a message on the topic
|
|
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT".getBytes(), 1);
|
|
|
|
|
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
|
2013-12-26 10:59:09 +01:00
|
|
|
assertEquals("Hello MQTT", m_callback.getMessage(true).toString());
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-25 18:53:50 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkReceivePublishedMessage_after_a_reconnect_with_notCleanSession() throws Exception {
|
|
|
|
|
LOG.info("*** checkReceivePublishedMessage_after_a_reconnect_with_notCleanSession ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 1);
|
|
|
|
|
m_client.disconnect();
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-25 18:53:50 +01:00
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 1);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-25 18:53:50 +01:00
|
|
|
//publish a QoS 1 message another client publish a message on the topic
|
2015-11-08 18:25:55 +01:00
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT".getBytes(), 1);
|
|
|
|
|
|
2013-11-25 18:53:50 +01:00
|
|
|
//Verify that after a reconnection the client receive the message
|
2013-12-26 10:59:09 +01:00
|
|
|
MqttMessage message = m_callback.getMessage(true);
|
|
|
|
|
assertNotNull(message);
|
|
|
|
|
assertEquals("Hello MQTT", message.toString());
|
2013-11-25 18:53:50 +01:00
|
|
|
}
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-09-19 20:14:07 +02:00
|
|
|
private void publishFromAnotherClient(String topic, byte[] payload, int qos) throws Exception {
|
2013-11-25 18:53:50 +01:00
|
|
|
IMqttClient anotherClient = new MqttClient("tcp://localhost:1883", "TestClientPUB", s_pubDataStore);
|
2013-09-19 20:14:07 +02:00
|
|
|
anotherClient.connect();
|
|
|
|
|
anotherClient.publish(topic, payload, qos, false);
|
|
|
|
|
anotherClient.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPublishWithQoS2() throws Exception {
|
|
|
|
|
LOG.info("*** testPublishWithQoS2 ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 2);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
|
|
|
|
//publish a QoS 1 message another client publish a message on the topic
|
|
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT".getBytes(), 2);
|
2013-11-27 11:21:04 +01:00
|
|
|
m_callback.reinit();
|
2013-09-19 20:14:07 +02:00
|
|
|
m_client.connect(options);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-12-26 10:59:09 +01:00
|
|
|
MqttMessage message = m_callback.getMessage(true);
|
|
|
|
|
assertEquals("Hello MQTT", message.toString());
|
|
|
|
|
assertEquals(2, message.getQos());
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPublishReceiveWithQoS2() throws Exception {
|
|
|
|
|
LOG.info("*** testPublishReceiveWithQoS2 ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 2);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
//publish a QoS 2 message another client publish a message on the topic
|
2013-09-19 20:14:07 +02:00
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT".getBytes(), 2);
|
2013-11-27 11:21:04 +01:00
|
|
|
m_callback.reinit();
|
2013-09-19 20:14:07 +02:00
|
|
|
m_client.connect(options);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-14 16:43:03 +01:00
|
|
|
assertNotNull(m_callback);
|
2013-12-26 10:59:09 +01:00
|
|
|
MqttMessage message = m_callback.getMessage(true);
|
|
|
|
|
assertNotNull(message);
|
|
|
|
|
assertEquals("Hello MQTT", message.toString());
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
@Test
|
|
|
|
|
public void avoidMultipleNotificationsAfterMultipleReconnection_cleanSessionFalseQoS1() throws Exception {
|
|
|
|
|
LOG.info("*** avoidMultipleNotificationsAfterMultipleReconnection_cleanSessionFalseQoS1, issue #16 ***");
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setCleanSession(false);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 1);
|
|
|
|
|
m_client.disconnect();
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT 1".getBytes(), 1);
|
|
|
|
|
m_callback.reinit();
|
|
|
|
|
m_client.connect(options);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
assertNotNull(m_callback);
|
2013-12-26 10:59:09 +01:00
|
|
|
MqttMessage message = m_callback.getMessage(true);
|
|
|
|
|
assertNotNull(message);
|
|
|
|
|
assertEquals("Hello MQTT 1", message.toString());
|
2013-11-27 18:45:56 +01:00
|
|
|
m_client.disconnect();
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
//publish other message
|
|
|
|
|
publishFromAnotherClient("/topic", "Hello MQTT 2".getBytes(), 1);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
//reconnect the second time
|
|
|
|
|
m_callback.reinit();
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
assertNotNull(m_callback);
|
2013-12-26 10:59:09 +01:00
|
|
|
message = m_callback.getMessage(true);
|
|
|
|
|
assertNotNull(message);
|
|
|
|
|
assertEquals("Hello MQTT 2", message.toString());
|
2013-11-27 18:45:56 +01:00
|
|
|
}
|
2013-09-19 20:14:07 +02:00
|
|
|
|
2015-06-28 19:53:22 +02:00
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-19 18:01:54 +01:00
|
|
|
@Test
|
|
|
|
|
public void testSubcriptionDoesntStayActiveAfterARestart() throws Exception {
|
|
|
|
|
LOG.info("*** testSubcriptionDoesntStayActiveAfterARestart ***");
|
|
|
|
|
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
//clientForSubscribe1 connect and subscribe to /topic QoS2
|
|
|
|
|
MqttClientPersistence dsSubscriberA = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForSubscribe1");
|
|
|
|
|
|
|
|
|
|
MqttClient clientForSubscribe1 = new MqttClient("tcp://localhost:1883", "clientForSubscribe1", dsSubscriberA);
|
|
|
|
|
TestCallback cbSubscriber1 = new TestCallback();
|
|
|
|
|
clientForSubscribe1.setCallback(cbSubscriber1);
|
|
|
|
|
clientForSubscribe1.connect();
|
|
|
|
|
clientForSubscribe1.subscribe("topic", 0);
|
|
|
|
|
|
|
|
|
|
//server stop
|
|
|
|
|
m_server.stopServer();
|
|
|
|
|
System.out.println("\n\n SEVER REBOOTING \n\n");
|
|
|
|
|
//server start
|
|
|
|
|
startServer();
|
|
|
|
|
|
|
|
|
|
//clientForSubscribe2 connect and subscribe to /topic QoS2
|
|
|
|
|
MqttClientPersistence dsSubscriberB = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForSubscribe2");
|
|
|
|
|
MqttClient clientForSubscribe2 = new MqttClient("tcp://localhost:1883", "clientForSubscribe2", dsSubscriberB);
|
|
|
|
|
TestCallback cbSubscriber2 = new TestCallback();
|
|
|
|
|
clientForSubscribe2.setCallback(cbSubscriber2);
|
|
|
|
|
clientForSubscribe2.connect();
|
|
|
|
|
clientForSubscribe2.subscribe("topic", 0);
|
|
|
|
|
|
|
|
|
|
//clientForPublish publish on /topic with QoS2 a message
|
|
|
|
|
MqttClientPersistence dsSubscriberPUB = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForPublish");
|
|
|
|
|
MqttClient clientForPublish = new MqttClient("tcp://localhost:1883", "clientForPublish", dsSubscriberPUB);
|
|
|
|
|
clientForPublish.connect();
|
|
|
|
|
clientForPublish.publish("topic", "Hello".getBytes(), 2, true);
|
|
|
|
|
|
|
|
|
|
//verify clientForSubscribe1 doesn't receive a notification but clientForSubscribe2 yes
|
|
|
|
|
System.out.println("Before waiting to receive 1 sec from " + clientForSubscribe1.getClientId());
|
|
|
|
|
assertFalse(clientForSubscribe1.isConnected());
|
|
|
|
|
assertTrue(clientForSubscribe2.isConnected());
|
|
|
|
|
System.out.println("Waiting to receive 1 sec from " + clientForSubscribe2.getClientId());
|
|
|
|
|
MqttMessage messageOnB = cbSubscriber2.getMessage(true);
|
|
|
|
|
assertEquals("Hello", new String(messageOnB.getPayload()));
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 13:19:16 +01:00
|
|
|
@Test
|
|
|
|
|
public void testForceClientDisconnection_issue116() throws Exception {
|
|
|
|
|
LOG.info("*** testForceClientDisconnection_issue118 ***");
|
|
|
|
|
TestCallback cbSubscriber1 = new TestCallback();
|
|
|
|
|
MqttClient clientXA = createClient("subscriber", "X", cbSubscriber1);
|
|
|
|
|
clientXA.subscribe("topic", 0);
|
|
|
|
|
|
|
|
|
|
MqttClient clientXB = createClient("publisher", "X");
|
|
|
|
|
clientXB.publish("topic", "Hello".getBytes(), 2, true);
|
|
|
|
|
|
|
|
|
|
TestCallback cbSubscriber2 = new TestCallback();
|
|
|
|
|
MqttClient clientYA = createClient("subscriber", "Y", cbSubscriber2);
|
|
|
|
|
clientYA.subscribe("topic", 0);
|
|
|
|
|
|
|
|
|
|
MqttClient clientYB = createClient("publisher", "Y");
|
|
|
|
|
clientYB.publish("topic", "Hello 2".getBytes(), 2, true);
|
|
|
|
|
|
|
|
|
|
//Verify that the second subscriber client get notified and not the first.
|
|
|
|
|
assertTrue(cbSubscriber1.connectionLost());
|
|
|
|
|
assertEquals("Hello 2", new String(cbSubscriber2.getMessage(true).getPayload()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected MqttClient createClient(String clientName, String storeSuffix) throws MqttException {
|
|
|
|
|
return createClient(clientName, storeSuffix, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected MqttClient createClient(String clientName, String storeSuffix, TestCallback cb) throws MqttException {
|
|
|
|
|
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
//clientX connect and subscribe to /topic QoS2
|
|
|
|
|
MqttClientPersistence dsClient = new MqttDefaultFilePersistence(tmpDir + File.separator + storeSuffix + clientName);
|
|
|
|
|
MqttClient client = new MqttClient("tcp://localhost:1883", clientName, dsClient);
|
|
|
|
|
if (cb != null) {
|
|
|
|
|
client.setCallback(cb);
|
|
|
|
|
}
|
|
|
|
|
client.connect();
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-19 20:14:07 +02:00
|
|
|
}
|