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-11-27 18:45:56 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.server.config.IConfig;
|
|
|
|
|
import io.moquette.server.config.MemoryConfig;
|
2015-03-15 16:45:11 +01:00
|
|
|
import org.fusesource.mqtt.client.*;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
import java.io.IOException;
|
2015-08-22 15:35:11 +02:00
|
|
|
import java.util.Properties;
|
2013-12-22 17:27:43 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-03-11 10:07:27 -04:00
|
|
|
|
2014-07-26 18:22:32 +02:00
|
|
|
import static org.junit.Assert.*;
|
2015-03-11 10:07:27 -04:00
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author andrea
|
|
|
|
|
*/
|
|
|
|
|
public class ServerIntegrationFuseTest {
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationPahoTest.class);
|
|
|
|
|
|
|
|
|
|
Server m_server;
|
|
|
|
|
MQTT m_mqtt;
|
2013-12-14 10:01:14 +01:00
|
|
|
BlockingConnection m_subscriber;
|
|
|
|
|
BlockingConnection m_publisher;
|
2015-08-22 15:35:11 +02:00
|
|
|
IConfig m_config;
|
2013-11-27 18:45:56 +01: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-11-27 18:45:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
startServer();
|
|
|
|
|
|
|
|
|
|
m_mqtt = new MQTT();
|
|
|
|
|
m_mqtt.setHost("localhost", 1883);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
2013-12-14 10:01:14 +01:00
|
|
|
if (m_subscriber != null) {
|
|
|
|
|
m_subscriber.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_publisher != null) {
|
|
|
|
|
m_publisher.disconnect();
|
|
|
|
|
}
|
2013-11-27 18:45:56 +01:00
|
|
|
|
|
|
|
|
m_server.stopServer();
|
2015-11-08 18:25:55 +01:00
|
|
|
IntegrationUtils.cleanPersistenceFile(m_config);
|
2013-11-27 18:45:56 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-02 18:16:50 +01:00
|
|
|
@Test
|
2014-05-18 12:02:18 +02:00
|
|
|
public void connectWithCredentials() throws Exception {
|
2014-03-02 18:16:50 +01:00
|
|
|
LOG.info("*** connectWithCredetials ***");
|
|
|
|
|
MQTT mqtt = new MQTT();
|
|
|
|
|
mqtt.setHost("localhost", 1883);
|
|
|
|
|
mqtt.setClientId("Publisher");
|
|
|
|
|
mqtt.setUserName("testuser");
|
|
|
|
|
mqtt.setPassword("passwd");
|
|
|
|
|
|
|
|
|
|
m_publisher = mqtt.blockingConnection();
|
|
|
|
|
m_publisher.connect();
|
|
|
|
|
|
|
|
|
|
assertTrue(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 18:16:24 +02:00
|
|
|
@Test
|
2015-10-24 16:26:43 +02:00
|
|
|
public void checkWillTestamentIsPublishedOnConnectionKill_noRetain() throws Exception {
|
|
|
|
|
LOG.info("checkWillTestamentIsPublishedOnConnectionKill");
|
2014-06-15 18:16:24 +02:00
|
|
|
|
|
|
|
|
String willTestamentTopic = "/will/test";
|
|
|
|
|
String willTestamentMsg = "Bye bye";
|
|
|
|
|
|
|
|
|
|
MQTT mqtt = new MQTT();
|
|
|
|
|
mqtt.setHost("localhost", 1883);
|
|
|
|
|
mqtt.setClientId("WillTestamentPublisher");
|
|
|
|
|
mqtt.setWillRetain(false);
|
|
|
|
|
mqtt.setWillMessage(willTestamentMsg);
|
|
|
|
|
mqtt.setWillTopic(willTestamentTopic);
|
|
|
|
|
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(willTestamentTopic, QoS.AT_LEAST_ONCE)};
|
|
|
|
|
Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)};
|
|
|
|
|
m_subscriber.subscribe(topics);
|
|
|
|
|
|
|
|
|
|
//Exercise, kill the publisher connection
|
|
|
|
|
m_publisher.kill();
|
|
|
|
|
|
|
|
|
|
//Verify, that the testament is fired
|
|
|
|
|
Message msg = m_subscriber.receive(500, TimeUnit.MILLISECONDS);
|
|
|
|
|
assertNotNull("We should get notified with 'Will' message", msg);
|
|
|
|
|
msg.ack();
|
|
|
|
|
assertEquals(willTestamentMsg, new String(msg.getPayload()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 18:45:56 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1() throws Exception {
|
|
|
|
|
LOG.info("*** checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1 ***");
|
|
|
|
|
MQTT mqtt = new MQTT();
|
|
|
|
|
mqtt.setHost("localhost", 1883);
|
|
|
|
|
mqtt.setClientId("Publisher");
|
2013-12-14 10:01:14 +01:00
|
|
|
m_publisher = mqtt.blockingConnection();
|
|
|
|
|
m_publisher.connect();
|
2013-11-27 18:45:56 +01:00
|
|
|
|
|
|
|
|
m_mqtt.setHost("localhost", 1883);
|
|
|
|
|
m_mqtt.setCleanSession(false);
|
|
|
|
|
m_mqtt.setClientId("Subscriber");
|
2013-12-14 10:01:14 +01:00
|
|
|
m_subscriber = m_mqtt.blockingConnection();
|
|
|
|
|
m_subscriber.connect();
|
2013-11-27 18:45:56 +01:00
|
|
|
Topic[] topics = new Topic[]{new Topic("/topic", QoS.AT_LEAST_ONCE)};
|
2013-12-14 10:01:14 +01:00
|
|
|
m_subscriber.subscribe(topics);
|
2013-11-27 18:45:56 +01:00
|
|
|
|
|
|
|
|
//force the publisher to send
|
2013-12-14 10:01:14 +01:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT!!-1".getBytes(), QoS.AT_LEAST_ONCE, false);
|
2013-11-27 18:45:56 +01:00
|
|
|
|
|
|
|
|
//read the first message and drop the connection
|
2013-12-14 10:01:14 +01:00
|
|
|
Message msg = m_subscriber.receive();
|
2013-11-27 18:45:56 +01:00
|
|
|
msg.ack();
|
|
|
|
|
assertEquals("Hello world MQTT!!-1", new String(msg.getPayload()));
|
2013-12-14 10:01:14 +01:00
|
|
|
m_subscriber.disconnect();
|
2013-11-27 18:45:56 +01:00
|
|
|
|
2013-12-14 10:01:14 +01:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT!!-2".getBytes(), QoS.AT_LEAST_ONCE, false);
|
|
|
|
|
m_publisher.publish("/topic", "Hello world MQTT!!-3".getBytes(), QoS.AT_LEAST_ONCE, false);
|
2013-11-27 18:45:56 +01:00
|
|
|
|
|
|
|
|
//reconnect and expect to receive the hello 2 message
|
2013-12-14 10:01:14 +01:00
|
|
|
m_subscriber = m_mqtt.blockingConnection();
|
|
|
|
|
m_subscriber.connect();
|
2015-11-15 11:16:18 +01:00
|
|
|
//topics = new Topic[]{new Topic("/topic", QoS.AT_LEAST_ONCE)};
|
|
|
|
|
//m_subscriber.subscribe(topics);
|
2013-12-14 10:01:14 +01:00
|
|
|
msg = m_subscriber.receive();
|
2013-11-27 18:45:56 +01:00
|
|
|
msg.ack();
|
|
|
|
|
assertEquals("Hello world MQTT!!-2", new String(msg.getPayload()));
|
2015-01-30 00:27:44 +01:00
|
|
|
|
2013-12-14 10:01:14 +01:00
|
|
|
msg = m_subscriber.receive();
|
2013-11-27 18:45:56 +01:00
|
|
|
assertEquals("Hello world MQTT!!-3", new String(msg.getPayload()));
|
|
|
|
|
msg.ack();
|
2015-01-30 00:27:44 +01:00
|
|
|
m_subscriber.disconnect();
|
2013-11-27 18:45:56 +01:00
|
|
|
}
|
2013-12-22 17:27:43 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void checkReplayStoredPublish_forNoCleanSession_qos1() throws Exception {
|
|
|
|
|
LOG.info("*** checkReplayStoredPublish_forNoCleanSession_qos1 ***");
|
|
|
|
|
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("/topic", QoS.AT_LEAST_ONCE)};
|
|
|
|
|
m_subscriber.subscribe(topics);
|
|
|
|
|
m_subscriber.disconnect();
|
|
|
|
|
|
|
|
|
|
//force the publisher to send
|
|
|
|
|
m_publisher.publish("/topic", "Hello world MQTT!!-1".getBytes(), QoS.AT_LEAST_ONCE, false);
|
2015-04-08 23:19:23 +02:00
|
|
|
|
2013-12-22 17:27:43 +01:00
|
|
|
//reconnect and expect to receive the hello 2 message
|
|
|
|
|
m_subscriber = m_mqtt.blockingConnection();
|
|
|
|
|
m_subscriber.connect();
|
|
|
|
|
topics = new Topic[]{new Topic("/topic", QoS.AT_LEAST_ONCE)};
|
|
|
|
|
m_subscriber.subscribe(topics);
|
|
|
|
|
Message msg = m_subscriber.receive(1, TimeUnit.SECONDS);
|
|
|
|
|
assertNotNull(msg);
|
|
|
|
|
msg.ack();
|
|
|
|
|
assertEquals("Hello world MQTT!!-1", new String(msg.getPayload()));
|
|
|
|
|
// m_subscriber.disconnect();
|
|
|
|
|
}
|
2014-05-20 23:03:24 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* subscriber connect and subscribe on "topic"
|
|
|
|
|
* subscriber disconnects
|
|
|
|
|
* publisher connects and send two message "hello1" "hello2" to "topic"
|
|
|
|
|
* subscriber connects again and receive "hello1" "hello2"
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
2015-11-16 23:45:47 +01:00
|
|
|
public void checkQoS2SubscriberDisconnectReceivePersistedPublishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkQoS2SubscriberDisconnectReceivePersistedPublishes ***");
|
2014-05-20 23:03:24 +02:00
|
|
|
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("topic", QoS.EXACTLY_ONCE)};
|
|
|
|
|
m_subscriber.subscribe(topics);
|
|
|
|
|
m_subscriber.disconnect();
|
2015-04-08 23:19:23 +02:00
|
|
|
|
2014-05-20 23:03:24 +02:00
|
|
|
MQTT mqtt = new MQTT();
|
|
|
|
|
mqtt.setHost("localhost", 1883);
|
|
|
|
|
mqtt.setClientId("Publisher");
|
|
|
|
|
m_publisher = mqtt.blockingConnection();
|
|
|
|
|
m_publisher.connect();
|
|
|
|
|
m_publisher.publish("topic", "Hello1".getBytes(), QoS.EXACTLY_ONCE, true);
|
|
|
|
|
m_publisher.publish("topic", "Hello2".getBytes(), QoS.EXACTLY_ONCE, true);
|
|
|
|
|
m_publisher.disconnect();
|
2015-04-08 23:19:23 +02:00
|
|
|
|
2014-05-20 23:03:24 +02:00
|
|
|
//subscriber reconnects
|
|
|
|
|
m_mqtt = new MQTT();
|
|
|
|
|
m_mqtt.setHost("localhost", 1883);
|
|
|
|
|
m_mqtt.setClientId("Subscriber");
|
|
|
|
|
m_mqtt.setCleanSession(false);
|
|
|
|
|
m_subscriber = m_mqtt.blockingConnection();
|
|
|
|
|
m_subscriber.connect();
|
|
|
|
|
//subscriber should receive the 2 messages missed
|
|
|
|
|
Message msg = m_subscriber.receive(1, TimeUnit.SECONDS);
|
|
|
|
|
assertNotNull(msg);
|
|
|
|
|
msg.ack();
|
|
|
|
|
assertEquals("Hello1", new String(msg.getPayload()));
|
|
|
|
|
msg = m_subscriber.receive(1, TimeUnit.SECONDS);
|
|
|
|
|
assertNotNull(msg);
|
|
|
|
|
msg.ack();
|
|
|
|
|
assertEquals("Hello2", new String(msg.getPayload()));
|
|
|
|
|
}
|
2015-06-23 23:23:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
2013-11-27 18:45:56 +01:00
|
|
|
}
|