Fix flaky GarminSimulatorClientTest.sendMessage
This commit is contained in:
parent
ec596c51df
commit
4cdf2e6969
1 changed files with 12 additions and 6 deletions
|
@ -1,11 +1,9 @@
|
||||||
package app.aaps.plugins.sync.garmin
|
package app.aaps.plugins.sync.garmin
|
||||||
|
|
||||||
import app.aaps.shared.tests.TestBase
|
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.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.mockito.kotlin.any
|
|
||||||
import org.mockito.kotlin.eq
|
import org.mockito.kotlin.eq
|
||||||
import org.mockito.kotlin.isNull
|
import org.mockito.kotlin.isNull
|
||||||
import org.mockito.kotlin.mock
|
import org.mockito.kotlin.mock
|
||||||
|
@ -20,6 +18,14 @@ class GarminSimulatorClientTest: TestBase() {
|
||||||
private lateinit var client: GarminSimulatorClient
|
private lateinit var client: GarminSimulatorClient
|
||||||
private val receiver: GarminReceiver = mock()
|
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
|
@BeforeEach
|
||||||
fun setup() {
|
fun setup() {
|
||||||
client = GarminSimulatorClient(aapsLogger, receiver, 0)
|
client = GarminSimulatorClient(aapsLogger, receiver, 0)
|
||||||
|
@ -36,8 +42,9 @@ class GarminSimulatorClientTest: TestBase() {
|
||||||
assertTrue(socket.isConnected)
|
assertTrue(socket.isConnected)
|
||||||
socket.getOutputStream().write(payload)
|
socket.getOutputStream().write(payload)
|
||||||
socket.getOutputStream().flush()
|
socket.getOutputStream().flush()
|
||||||
|
val device = waitForOrFail { client.connectedDevices.firstOrNull() }
|
||||||
verify(receiver, timeout(1_000))
|
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() {
|
fun sendMessage() {
|
||||||
val payload = "foo".toByteArray()
|
val payload = "foo".toByteArray()
|
||||||
assertTrue(client.awaitReady(Duration.ofSeconds(10)))
|
assertTrue(client.awaitReady(Duration.ofSeconds(10)))
|
||||||
|
verify(receiver, timeout(100)).onConnect(client)
|
||||||
val port = client.port
|
val port = client.port
|
||||||
val ip = Inet4Address.getByAddress(byteArrayOf(127, 0, 0, 1))
|
val ip = Inet4Address.getByAddress(byteArrayOf(127, 0, 0, 1))
|
||||||
val device: GarminDevice
|
val device: GarminDevice
|
||||||
val app: GarminApplication
|
val app: GarminApplication
|
||||||
Socket(ip, port).use { socket ->
|
Socket(ip, port).use { socket ->
|
||||||
assertTrue(socket.isConnected)
|
assertTrue(socket.isConnected)
|
||||||
verify(receiver).onConnect(client)
|
device = waitForOrFail { client.connectedDevices.firstOrNull() }
|
||||||
assertEquals(1, client.connectedDevices.size)
|
|
||||||
device = client.connectedDevices.first()
|
|
||||||
app = GarminApplication(device, "SIMAPP", "T")
|
app = GarminApplication(device, "SIMAPP", "T")
|
||||||
client.sendMessage(app, payload)
|
client.sendMessage(app, payload)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue