Skip to content

Commit 0500133

Browse files
author
Ian Craggs
committed
Add ping connection lost check test for issue #576
1 parent 4d1ea78 commit 0500133

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

  • org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/connectionLoss

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/connectionLoss/ConnectionLossTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2009, 2014 IBM Corp.
2+
* Copyright (c) 2009, 2019 IBM Corp.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License v1.0
@@ -429,8 +429,11 @@ public void run() {
429429
client.close();
430430
}
431431

432+
private boolean connectionLostCalled = false;
433+
432434
public void connectionLost(Throwable cause) {
433435
log.info((new Date())+" - Connection Lost");
436+
connectionLostCalled = true;
434437
}
435438

436439
public void messageArrived(String topic, MqttMessage message) throws Exception {
@@ -440,4 +443,49 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
440443
public void deliveryComplete(IMqttDeliveryToken token) {
441444
// log.info("Delivery Complete: " + token.getMessageId());
442445
}
446+
447+
/**
448+
* Tests whether paho can detect a connection loss with the server with no publishing - using ping only
449+
* @throws Exception
450+
*/
451+
@Test
452+
public void testConnectionLossUsingPing()
453+
throws Exception
454+
{
455+
String methodName = Utility.getMethodName();
456+
LoggingUtilities.banner(log, cclass, methodName);
457+
final int keepAlive = 15;
458+
459+
MqttConnectOptions options = new MqttConnectOptions();
460+
options.setCleanSession(true);
461+
options.setUserName(username);
462+
options.setPassword(password);
463+
options.setKeepAliveInterval(keepAlive);
464+
465+
connectionLostCalled = false;
466+
MqttClient client = new MqttClient("tcp://localhost:" + proxy.getLocalPort(), clientId, DATA_STORE);
467+
client.setCallback(this);
468+
proxy.enableProxy();
469+
client.connect(options);
470+
471+
log.info((new Date())+" - Connected.");
472+
Thread.sleep(1000);
473+
proxy.disableProxy();
474+
475+
int count = 0;
476+
while (client.isConnected() && ++count < 2*keepAlive) {
477+
try {
478+
Thread.sleep(1000);
479+
}
480+
catch (Exception e) {
481+
// ignore
482+
}
483+
}
484+
485+
Assert.assertTrue("Connection lost was not called", connectionLostCalled);
486+
Assert.assertFalse("Disconnected", client.isConnected());
487+
if (client.isConnected()) client.disconnect(0);
488+
client.close();
489+
}
490+
443491
}

0 commit comments

Comments
 (0)