Fixed bug due to not persisted subscriptions into the store, on restart there were ghosts subscriptions that produced errors

This commit is contained in:
selva.andre
2014-09-19 22:00:51 +02:00
parent cc45b87ebe
commit 0940ac84a0
11 changed files with 124 additions and 42 deletions
@@ -162,7 +162,7 @@ public class ProtocolProcessorTest {
m_mockAuthenticator = new MockAuthenticator(users);
subscriptions = new SubscriptionsStore();
subscriptions.init(Collections.<Subscription>emptyList());
subscriptions.init(new MemoryStorageService());
m_processor = new ProtocolProcessor();
m_processor.init(subscriptions, m_storageService, m_sessionStore, m_mockAuthenticator);
}
@@ -298,7 +298,7 @@ public class ProtocolProcessorTest {
};
//simulate a connect that register a clientID to an IoSession
subs.init(Collections.<Subscription>emptyList());
subs.init(new MemoryStorageService());
m_processor.init(subs, m_storageService, m_sessionStore, null);
ConnectMessage connectMessage = new ConnectMessage();
connectMessage.setProcotolVersion((byte)3);
@@ -342,7 +342,7 @@ public class ProtocolProcessorTest {
};
//simulate a connect that register a clientID to an IoSession
subs.init(Collections.<Subscription>emptyList());
subs.init(new MemoryStorageService());
m_processor.init(subs, m_storageService, m_sessionStore, null);
MockReceiverChannel firstReceiverSession = new MockReceiverChannel();
@@ -372,7 +372,7 @@ public class ProtocolProcessorTest {
m_processor.processPublish(m_session, msg);
//Verify
Thread.sleep(100); //ugly but we dependend on the asynch that pull data from back disruptor
Thread.sleep(100); //ugly but we depend on the asynch that pull data from back disruptor
PublishMessage pub2FirstSubscriber = (PublishMessage) firstReceiverSession.getMessage();
assertNotNull(pub2FirstSubscriber);
String firstMessageContent = DebugUtils.payload2Str(pub2FirstSubscriber.getPayload());
@@ -458,7 +458,7 @@ public class ProtocolProcessorTest {
}
}
};
subs.init(Collections.<Subscription>emptyList());
subs.init(new MemoryStorageService());
//simulate a connect that register a clientID to an IoSession
m_processor.init(subs, m_storageService, m_sessionStore, null);
@@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.dna.mqtt.moquette.messaging.spi.impl.MemoryStorageService;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.junit.After;
import static org.junit.Assert.*;
@@ -42,7 +43,7 @@ public class SubscriptionsStoreTest {
@Before
public void setUp() throws IOException {
store = new SubscriptionsStore();
store.init(Collections.<Subscription>emptyList());
store.init(new MemoryStorageService());
}
@Test
@@ -232,7 +233,7 @@ public class SubscriptionsStoreTest {
private void assertMatch(String subscription, String topic) {
store = new SubscriptionsStore();
store.init(Collections.<Subscription>emptyList());
store.init(new MemoryStorageService());
Subscription sub = new Subscription("FAKE_CLI_ID_1", subscription, AbstractMessage.QOSType.MOST_ONE, false);
store.add(sub);
assertFalse(store.matches(topic).isEmpty());
@@ -240,7 +241,7 @@ public class SubscriptionsStoreTest {
private void assertNotMatch(String subscription, String topic) {
store = new SubscriptionsStore();
store.init(Collections.<Subscription>emptyList());
store.init(new MemoryStorageService());
Subscription sub = new Subscription("FAKE_CLI_ID_1", subscription, AbstractMessage.QOSType.MOST_ONE, false);
store.add(sub);
assertTrue(store.matches(topic).isEmpty());
@@ -325,7 +326,7 @@ public class SubscriptionsStoreTest {
@Test
public void removeSubscription_withDifferentClients_subscribedSameTopic() {
SubscriptionsStore aStore = new SubscriptionsStore();
aStore.init(Collections.<Subscription>emptyList());
aStore.init(new MemoryStorageService());
//subscribe a not active clientID1 to /topic
Subscription slashSub = new Subscription("FAKE_CLI_ID_1", "/topic", AbstractMessage.QOSType.MOST_ONE, false);
aStore.add(slashSub);
@@ -64,8 +64,10 @@ public class ServerLowlevelMessagesIntegrationTests {
@After
public void tearDown() throws Exception {
m_client.close();
LOG.debug("After raw client close");
Thread.sleep(300); //to let the close event pass before server stop event
m_server.stopServer();
LOG.debug("After asked server to stop");
File dbFile = new File(Server.STORAGE_FILE_PATH);
if (dbFile.exists()) {
dbFile.delete();
@@ -111,11 +113,14 @@ public class ServerLowlevelMessagesIntegrationTests {
//Execute
m_client.sendMessage(connectMessage);
long connectTime = System.currentTimeMillis();
//but after the 2 KEEP ALIVE timeut 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
Message msg = willSubscriber.receive(3100, TimeUnit.MILLISECONDS);
Message msg = willSubscriber.receive(3300, TimeUnit.MILLISECONDS);
long willMessageReceiveTime = System.currentTimeMillis();
if (msg == null) {
LOG.warn("testament message is null");
}
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
@@ -49,7 +49,9 @@ public class ServerRestartIntegrationTest {
startServer();
m_mqtt = new MQTT();
m_mqtt.setHost("localhost", 1883);
m_mqtt.setCleanSession(false);
}
@After
@@ -72,10 +74,8 @@ public class ServerRestartIntegrationTest {
@Test
public void checkRestartCleanSubscriptionTree() throws Exception {
//subscribe to /topic
m_mqtt.setHost("localhost", 1883);
m_mqtt.setCleanSession(false);
m_mqtt.setClientId("Subscriber");
//subscribe to /topic
m_subscriber = m_mqtt.blockingConnection();
m_subscriber.connect();
Topic[] topics = new Topic[]{new Topic("/topic", QoS.AT_LEAST_ONCE)};
@@ -98,7 +98,7 @@ public class ServerRestartIntegrationTest {
topics = new Topic[]{new Topic("/topic", QoS.EXACTLY_ONCE)};
m_subscriber.subscribe(topics);
//shoud 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();
mqtt.setHost("localhost", 1883);
mqtt.setClientId("Publisher");
@@ -113,4 +113,41 @@ public class ServerRestartIntegrationTest {
//no more messages on the same topic will be received
assertNull(m_subscriber.receive(1, TimeUnit.SECONDS));
}
@Test
public void checkDontPublishInactiveClientsAfterServerRestart() throws Exception {
m_mqtt.setClientId("SubPub");
BlockingConnection conn = subscribeAndPublish("/topic");
conn.disconnect();
//shutdown the server
m_server.stopServer();
//restart the server
m_server.startServer();
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
mqtt.setClientId("Publisher");
m_publisher = mqtt.blockingConnection();
m_publisher.connect();
m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(), QoS.AT_MOST_ONCE, false);
}
/**
* Connect subscribe to topic and publish on the same topic
* */
private BlockingConnection subscribeAndPublish(String topic) throws Exception {
BlockingConnection conn = m_mqtt.blockingConnection();
conn.connect();
Topic[] topics = new Topic[]{new Topic(topic, QoS.AT_MOST_ONCE)};
conn.subscribe(topics);
conn.publish(topic, "Hello world MQTT!!".getBytes(), QoS.AT_MOST_ONCE, false);
//read the message
Message msg = conn.receive();
msg.ack();
assertEquals("Hello world MQTT!!", new String(msg.getPayload()));
return conn;
}
}