Added memory store for will messages and implemented the correct publishing

This commit is contained in:
selva.andre
2014-06-15 18:16:24 +02:00
parent 17d2ab505c
commit fdc7f6237e
8 changed files with 145 additions and 5 deletions
@@ -93,6 +93,41 @@ public class ServerIntegrationFuseTest {
assertTrue(true);
}
@Test
public void checkWillTestmaentIsPublishedOnConnectionKill_noRetain() throws Exception {
LOG.info("checkWillTestmaentIsPublishedOnConnectionKill");
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()));
}
@Test
public void checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1() throws Exception {
LOG.info("*** checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1 ***");
@@ -17,20 +17,32 @@ package org.dna.mqtt.moquette.server;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
import org.dna.mqtt.moquette.proto.messages.ConnectMessage;
import org.dna.mqtt.moquette.testclient.Client;
import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.Message;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.Topic;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author andrea
*/
public class ServerLowlevelMessagesIntegrationTests {
private static final Logger LOG = LoggerFactory.getLogger(ServerLowlevelMessagesIntegrationTests.class);
Server m_server;
Client m_client;
MQTT m_subscriberDef;
protected void startServer() throws IOException {
m_server = new Server();
@@ -41,6 +53,9 @@ public class ServerLowlevelMessagesIntegrationTests {
public void setUp() throws Exception {
startServer();
m_client = new Client("localhost");
m_subscriberDef = new MQTT();
m_subscriberDef.setHost("localhost", 1883);
m_subscriberDef.setClientId("Subscriber");
}
@After
@@ -68,4 +83,42 @@ public class ServerLowlevelMessagesIntegrationTests {
assertTrue(m_client.isConnectionLost());
}
@Test
public void checkWillMessageIsWiredOnClientKeepAliveExpiry() throws Exception {
LOG.info("*** checkWillMessageIsWiredOnClientKeepAliveExpiry ***");
String willTestamentTopic = "/will/test";
String willTestamentMsg = "Bye bye";
BlockingConnection willSubscriber = m_subscriberDef.blockingConnection();
willSubscriber.connect();
Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)};
willSubscriber.subscribe(topics);
int keepAlive = 2; //secs
ConnectMessage connectMessage = new ConnectMessage();
connectMessage.setProcotolVersion((byte)3);
connectMessage.setClientID("FAKECLNT");
connectMessage.setKeepAlive(keepAlive);
connectMessage.setWillFlag(true);
connectMessage.setWillMessage(willTestamentMsg);
connectMessage.setWillTopic(willTestamentTopic);
connectMessage.setWillQos((byte) QOSType.MOST_ONE.ordinal());
//Execute
m_client.sendMessage(connectMessage);
long connectTime = System.currentTimeMillis();
//but after the 2 KEEP ALIVE timeut expires it gets fired,
//NB it's 1,5 * KEEP_ALIVE so 3 secs and some millis to propagate the message
Message msg = willSubscriber.receive(3100, TimeUnit.MILLISECONDS);
long willMessageReceiveTime = System.currentTimeMillis();
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
assertTrue(willMessageReceiveTime - connectTime > 3000);
assertEquals(willTestamentMsg, new String(msg.getPayload()));
willSubscriber.disconnect();
}
}