-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add callback when mqtt connection is lost (#107)
- Loading branch information
1 parent
c021a68
commit 9661e8b
Showing
3 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
xesar-connect/src/test/kotlin/com/open200/xesar/connect/it/MqttConnectionLostTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.open200.xesar.connect.it | ||
|
||
import com.open200.xesar.connect.XesarConnect | ||
import com.open200.xesar.connect.XesarMqttClient | ||
import io.kotest.common.runBlocking | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.extensions.testcontainers.perTest | ||
import java.util.* | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withTimeout | ||
|
||
class MqttConnectionLostTest : | ||
FunSpec({ | ||
val container = MosquittoContainer.container() | ||
val config = MosquittoContainer.config(container) | ||
listener(container.perTest()) | ||
|
||
test("connect with custom mqtt client id") { | ||
runBlocking { | ||
val connected = CompletableDeferred<Unit>() | ||
val connectionLost = CompletableDeferred<Unit>() | ||
launch { | ||
val xesarMqttClient = XesarMqttClient.connectAsync(config).await() | ||
connected.complete(Unit) | ||
|
||
val xesarConnect = XesarConnect(xesarMqttClient, config) | ||
xesarConnect.onConnectionLost = { cause -> connectionLost.complete(Unit) } | ||
try { | ||
xesarConnect.delay() | ||
} catch (e: Exception) { | ||
// ignore | ||
} | ||
} | ||
launch { | ||
withTimeout(1000) { connected.await() } | ||
container.stop() | ||
withTimeout(1000) { connectionLost.await() } | ||
} | ||
} | ||
} | ||
}) |