Fix flaky GarminSimulatorClientTest.sendMessage

This commit is contained in:
Robert Buessow 2023-12-08 16:43:42 +01:00
parent ec596c51df
commit 4cdf2e6969

View file

@ -1,11 +1,9 @@
package app.aaps.plugins.sync.garmin
import app.aaps.shared.tests.TestBase
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.isNull
import org.mockito.kotlin.mock
@ -20,6 +18,14 @@ class GarminSimulatorClientTest: TestBase() {
private lateinit var client: GarminSimulatorClient
private val receiver: GarminReceiver = mock()
private fun <T> waitForOrFail(c: ()->T?): T {
for (i in 0 until 10) {
c()?.let { return it }
Thread.sleep(1)
}
throw AssertionError("wait timed out")
}
@BeforeEach
fun setup() {
client = GarminSimulatorClient(aapsLogger, receiver, 0)
@ -36,8 +42,9 @@ class GarminSimulatorClientTest: TestBase() {
assertTrue(socket.isConnected)
socket.getOutputStream().write(payload)
socket.getOutputStream().flush()
val device = waitForOrFail { client.connectedDevices.firstOrNull() }
verify(receiver, timeout(1_000))
.onReceiveMessage(eq(client), any(), eq("SIMAPP"), eq(payload))
.onReceiveMessage(eq(client), eq(device.id), eq("SIMAPP"), eq(payload))
}
}
@ -45,15 +52,14 @@ class GarminSimulatorClientTest: TestBase() {
fun sendMessage() {
val payload = "foo".toByteArray()
assertTrue(client.awaitReady(Duration.ofSeconds(10)))
verify(receiver, timeout(100)).onConnect(client)
val port = client.port
val ip = Inet4Address.getByAddress(byteArrayOf(127, 0, 0, 1))
val device: GarminDevice
val app: GarminApplication
Socket(ip, port).use { socket ->
assertTrue(socket.isConnected)
verify(receiver).onConnect(client)
assertEquals(1, client.connectedDevices.size)
device = client.connectedDevices.first()
device = waitForOrFail { client.connectedDevices.firstOrNull() }
app = GarminApplication(device, "SIMAPP", "T")
client.sendMessage(app, payload)
}