Skip to content

Commit

Permalink
Merge pull request #564 from hannesa2/TestMultipleDisconnect
Browse files Browse the repository at this point in the history
Test multiple disconnect
  • Loading branch information
hannesa2 authored Feb 16, 2024
2 parents 6460e69 + 26155f8 commit 4b0f774
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 4b0f774

Please sign in to comment.