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();
|
2016-05-18 00:09:40 +02:00
|
|
|
final Properties configProps = IntegrationUtils.prepareTestProperties();
|
2015-08-22 15:35:11 +02:00
|
|
|
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
|
|
|
}
|
2016-02-23 23:29:45 +01:00
|
|
|
|
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
|
|
|
}
|