From 1fcd0290d5d81c40fd3d22728ad79a5e750e6f22 Mon Sep 17 00:00:00 2001 From: "selva.andre" Date: Fri, 10 Aug 2012 19:36:47 +0200 Subject: [PATCH] Added integration tests to check cleanSession flag functioning --- .../server/ServerIntegrationTest.java | 138 +++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/parser/src/test/java/org/dna/mqtt/moquette/server/ServerIntegrationTest.java b/parser/src/test/java/org/dna/mqtt/moquette/server/ServerIntegrationTest.java index d5359d4e..9df3e9c6 100644 --- a/parser/src/test/java/org/dna/mqtt/moquette/server/ServerIntegrationTest.java +++ b/parser/src/test/java/org/dna/mqtt/moquette/server/ServerIntegrationTest.java @@ -4,9 +4,9 @@ import java.io.IOException; import org.dna.mqtt.moquette.client.Client; import org.dna.mqtt.moquette.client.IPublishCallback; import org.junit.After; -import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; +import org.junit.Test; /** * @@ -51,8 +51,144 @@ public class ServerIntegrationTest { client.publish("/topic", "Test my payload".getBytes()); client.close(); + client.shutdown(); assertTrue(received); } + + @Test + public void testCleanSession_maintainClientSubscriptions() { + Client client = new Client("localhost", Server.PORT/*, "CLID_123"*/); + client.connect(false); //without session cleanup + + client.subscribe("/topic", new IPublishCallback() { + + public void published(String topic, byte[] message) { + received = true; + } + }); + client.close(); + + //Exercise that the client maintain the subscriptions + client.connect(false); + client.publish("/topic", "Test my payload".getBytes()); + client.close(); + + //Verify + assertTrue(received); + + //TearDown + client.shutdown(); + } + + @Test + public void testCleanSession_maintainClientSubscriptions_againstClientDestruction() { + Client client = new Client("localhost", Server.PORT, "CLID_123"); + client.connect(false); //without session cleanup + + client.subscribe("/topic", new IPublishCallback() { + + public void published(String topic, byte[] message) { + received = true; + } + }); + client.close(); + client.shutdown(); + + //Exercise that the client maintain the subscriptions + client = new Client("localhost", Server.PORT, "CLID_123"); + client.register("/topic", new IPublishCallback() { + + public void published(String topic, byte[] message) { + received = true; + } + }); + client.connect(false); + client.publish("/topic", "Test my payload".getBytes()); + client.close(); + + //Verify + assertTrue(received); + + //TearDown + client.shutdown(); + } + + + /** + * Check that after a client has connected with clean session false, subscribed + * to some topic and exited, if it reconnect with clean session true, the server + * correctly cleanup every previous subscription + */ + @Test + public void testCleanSession_correctlyClientSubscriptions() { + Client client = new Client("localhost", Server.PORT, "CLID_123"); + client.connect(false); //without session cleanup + + client.subscribe("/topic", new IPublishCallback() { + + public void published(String topic, byte[] message) { + received = true; + } + }); + client.close(); + client.shutdown(); + + //Exercise that the client maintain the subscriptions + client = new Client("localhost", Server.PORT, "CLID_123"); + client.register("/topic", new IPublishCallback() { + + public void published(String topic, byte[] message) { + received = true; + } + }); + client.connect(true); + client.publish("/topic", "Test my payload".getBytes()); + client.close(); + + //Verify + assertFalse(received); + + //TearDown + client.shutdown(); + } + + +// @Test +// public void testCleanSession_maintainClientSubscriptions_withServerRestart() throws IOException { +// Client client = new Client("localhost", Server.PORT, "CLID_123"); +// client.connect(false); //without session cleanup +// +// client.subscribe("/topic", new IPublishCallback() { +// +// public void published(String topic, byte[] message) { +// received = true; +// } +// }); +// client.close(); +// client.shutdown(); +// server.stopServer(); +// +// server.startServer(); +// +// //Exercise that the client maintain the subscriptions +// client = new Client("localhost", Server.PORT, "CLID_123"); +// client.register("/topic", new IPublishCallback() { +// +// public void published(String topic, byte[] message) { +// received = true; +// } +// }); +// client.connect(false); +// client.publish("/topic", "Test my payload".getBytes()); +// client.close(); +// +// //Verify +// assertTrue(received); +// +// //TearDown +// client.shutdown(); +// } + }