From e81c6ca74936116228e888127d67085232172424 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Thu, 15 Feb 2024 17:36:22 +0100 Subject: [PATCH 1/2] Allow multiple disconnect --- .../java/info/mqtt/android/service/MqttService.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt index 9c4a6f52..bad859be 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt @@ -344,10 +344,12 @@ class MqttService : Service(), MqttTraceHandler { * @param activityToken arbitrary identifier to be passed back to the Activity */ fun disconnect(clientHandle: String, invocationContext: String?, activityToken: String?) { - val client = getConnection(clientHandle) - client.disconnect(invocationContext, activityToken) - connections.remove(clientHandle) - + if (isConnectionAvailable(clientHandle)) { + val client = getConnection(clientHandle) + client.disconnect(invocationContext, activityToken) + connections.remove(clientHandle) + } else + Timber.w("Connection is not available $clientHandle") // the activity has finished using us, so we can stop the service // the activities are bound with BIND_AUTO_CREATE, so the service will @@ -507,6 +509,8 @@ class MqttService : Service(), MqttTraceHandler { return connections[clientHandle] ?: throw IllegalArgumentException("Invalid ClientHandle >$clientHandle<") } + private fun isConnectionAvailable(clientHandle: String) = connections.contains(clientHandle) + /** * Called by the Activity when a message has been passed back to the application * From 26155f8bf6fa8323352b601fccecd49678141db0 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Thu, 15 Feb 2024 17:58:23 +0100 Subject: [PATCH 2/2] Test multiple disconnect --- .../java/info/mqtt/android/AndroidServiceTest.kt | 6 ++++++ .../mqtt/android/AndroidServiceWithActionListenerTest.kt | 2 ++ 2 files changed, 8 insertions(+) diff --git a/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceTest.kt b/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceTest.kt index 857161dc..ea58114b 100755 --- a/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceTest.kt +++ b/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceTest.kt @@ -50,6 +50,7 @@ class AndroidServiceTest : IMqttActionListener { var connectToken: IMqttToken = mqttClient.connect(null, null) connectToken.waitForCompletion(waitForCompletionTime) var disconnectToken: IMqttToken = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) disconnectToken.waitForCompletion(waitForCompletionTime) connectToken = mqttClient.connect(null, null) connectToken.waitForCompletion(waitForCompletionTime) @@ -86,6 +87,7 @@ class AndroidServiceTest : IMqttActionListener { val connectedToken1 = connectCallback1.asyncActionToken assertFalse(connectedToken1!!.sessionPresent) var disconnectToken: IMqttToken = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) disconnectToken.waitForCompletion(waitForCompletionTime) val options2 = MqttConnectOptions() options2.isCleanSession = false @@ -102,6 +104,7 @@ class AndroidServiceTest : IMqttActionListener { val connectedToken3 = connectCallback3.asyncActionToken assertTrue(connectedToken3!!.sessionPresent) disconnectToken = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) disconnectToken.waitForCompletion(waitForCompletionTime) } } catch (exception: Exception) { @@ -454,6 +457,7 @@ class AndroidServiceTest : IMqttActionListener { // Disconnect and reconnect to make sure the subscription and all queued messages are cleared. disconnectToken = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) disconnectToken.waitForCompletion(waitForCompletionTime) mqttClient.close() @@ -478,6 +482,7 @@ class AndroidServiceTest : IMqttActionListener { fail(validateResult.message) } disconnectToken = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, this) disconnectToken.waitForCompletion(waitForCompletionTime) mqttClient.close() @@ -507,6 +512,7 @@ class AndroidServiceTest : IMqttActionListener { } finally { try { disconnectToken = mqttClient!!.disconnect(null, null) + mqttClient.disconnect(null, null) disconnectToken.waitForCompletion(waitForCompletionTime) mqttClient.close() } catch (ignored: Exception) { diff --git a/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceWithActionListenerTest.kt b/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceWithActionListenerTest.kt index 1289e2ef..9b90a6c2 100644 --- a/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceWithActionListenerTest.kt +++ b/serviceLibrary/src/androidTest/java/info/mqtt/android/AndroidServiceWithActionListenerTest.kt @@ -73,10 +73,12 @@ class AndroidServiceWithActionListenerTest { var token = mqttClient.connect(null, ActionListener()) token.waitForCompletion(waitForCompletionTime) token = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, ActionListener()) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, ActionListener()) token.waitForCompletion(waitForCompletionTime) token = mqttClient.connect(null, ActionListener()) token.waitForCompletion(waitForCompletionTime) token = mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, ActionListener()) + mqttClient.disconnect(InstrumentationRegistry.getInstrumentation().targetContext, ActionListener()) token.waitForCompletion(waitForCompletionTime) }