Fixed bug in deletion of valid conn descriptor once different clients with same ID connects (issues #116 and #124)

This commit is contained in:
andsel
2015-12-27 13:19:16 +01:00
parent 571eaa1378
commit 163858e47b
7 changed files with 94 additions and 5 deletions
@@ -443,4 +443,43 @@ public class ServerIntegrationPahoTest {
assertEquals("Hello", new String(messageOnB.getPayload()));
}
@Test
public void testForceClientDisconnection_issue116() throws Exception {
LOG.info("*** testForceClientDisconnection_issue118 ***");
TestCallback cbSubscriber1 = new TestCallback();
MqttClient clientXA = createClient("subscriber", "X", cbSubscriber1);
clientXA.subscribe("topic", 0);
MqttClient clientXB = createClient("publisher", "X");
clientXB.publish("topic", "Hello".getBytes(), 2, true);
TestCallback cbSubscriber2 = new TestCallback();
MqttClient clientYA = createClient("subscriber", "Y", cbSubscriber2);
clientYA.subscribe("topic", 0);
MqttClient clientYB = createClient("publisher", "Y");
clientYB.publish("topic", "Hello 2".getBytes(), 2, true);
//Verify that the second subscriber client get notified and not the first.
assertTrue(cbSubscriber1.connectionLost());
assertEquals("Hello 2", new String(cbSubscriber2.getMessage(true).getPayload()));
}
protected MqttClient createClient(String clientName, String storeSuffix) throws MqttException {
return createClient(clientName, storeSuffix, null);
}
protected MqttClient createClient(String clientName, String storeSuffix, TestCallback cb) throws MqttException {
String tmpDir = System.getProperty("java.io.tmpdir");
//clientX connect and subscribe to /topic QoS2
MqttClientPersistence dsClient = new MqttDefaultFilePersistence(tmpDir + File.separator + storeSuffix + clientName);
MqttClient client = new MqttClient("tcp://localhost:1883", clientName, dsClient);
if (cb != null) {
client.setCallback(cb);
}
client.connect();
return client;
}
}
@@ -30,6 +30,7 @@ class TestCallback implements MqttCallback {
private MqttMessage m_message;
private String m_topic;
private CountDownLatch m_latch = new CountDownLatch(1);
private boolean m_connectionLost = false;
public MqttMessage getMessage(boolean checkElapsed) {
try {
@@ -56,17 +57,26 @@ class TestCallback implements MqttCallback {
m_latch = new CountDownLatch(1);
m_message = null;
m_topic = null;
m_connectionLost = false;
}
public boolean connectionLost() {
return m_connectionLost;
}
@Override
public void connectionLost(Throwable cause) {
m_connectionLost = true;
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
m_message = message;
m_topic = topic;
m_latch.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
}