Skip to content

Commit 755ebb0

Browse files
authored
fix(iot): wait for disconnect to finish before setting connection state to disconnected (#3051)
1 parent 1731b43 commit 755ebb0

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,8 @@ void reset() {
12771277
if (null != mqttClient) {
12781278
if (mqttClient.isConnected()) {
12791279
try {
1280-
mqttClient.disconnect(0);
1280+
IMqttToken token = mqttClient.disconnect(0);
1281+
token.waitForCompletion();
12811282
} catch (final MqttException e) {
12821283
throw new AmazonClientException("Client error when disconnecting.", e);
12831284
}

aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public void testConnectAlreadyConnected() throws Exception {
417417
}
418418

419419
@Test
420-
public void testConnectDisconnectConnect() throws Exception {
420+
public void testRepeatedConnectDisconnect() throws Exception {
421421
MockMqttClient mockClient = new MockMqttClient();
422422

423423
AWSIotMqttManager testClient = new AWSIotMqttManager("test-client",
@@ -429,15 +429,16 @@ public void testConnectDisconnectConnect() throws Exception {
429429

430430
KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH,
431431
KEYSTORE_NAME, KEYSTORE_PASSWORD);
432-
testClient.connect(testKeystore, csb);
433-
mockClient.mockConnectSuccess();
434-
assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState());
435-
testClient.disconnect();
436-
assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState());
437-
testClient.connect(testKeystore, csb);
438-
mockClient.mockConnectSuccess();
432+
int connectionAttempts = 3;
433+
for (int i = 0; i < connectionAttempts; i++) {
434+
testClient.connect(testKeystore, csb);
435+
mockClient.mockConnectSuccess();
436+
assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState());
437+
testClient.disconnect();
438+
assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState());
439+
}
439440

440-
assertEquals(2, mockClient.connectCalls);
441+
assertEquals(connectionAttempts, mockClient.connectCalls);
441442
}
442443

443444
@Test

aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/MockMqttClient.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.amazonaws.mobileconnectors.iot;
33

44
import org.eclipse.paho.client.mqttv3.*;
5+
import org.eclipse.paho.client.mqttv3.internal.Token;
56

67
import java.util.HashMap;
78

@@ -31,7 +32,7 @@ public class MockMqttClient extends MqttAsyncClient {
3132

3233
public HashMap<String, Integer> mockSubscriptions;
3334

34-
IMqttToken testToken = new MqttToken("unit-test");
35+
IMqttToken testToken = new TestMqttToken("unit-test");
3536
IMqttDeliveryToken testDeliveryToken = new MqttDeliveryToken();
3637

3738
MockMqttClient() throws MqttException {
@@ -146,4 +147,21 @@ public void mockDisconnect() {
146147
isConnected = false;
147148
mockCallback.connectionLost(new Exception("disconnect"));
148149
}
150+
151+
private class TestToken extends Token {
152+
153+
public TestToken(String logContext) {
154+
super(logContext);
155+
}
156+
157+
@Override
158+
public void waitForCompletion(long timeout) throws MqttException {}
159+
}
160+
161+
private class TestMqttToken extends MqttToken {
162+
163+
public TestMqttToken(String logContext) {
164+
internalTok = new TestToken(logContext);
165+
}
166+
}
149167
}

0 commit comments

Comments
 (0)