diff --git a/gradle.properties b/gradle.properties index dc962a673a..8e28702445 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects org.gradle.parallel=true org.gradle.warning.mode=all -org.gradle.jvmargs=-Xmx3g -XX:+UseParallelGC +org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC android.enableJetifier=false android.useAndroidX=true diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClient.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClient.kt index 75f8d8e436..01712a8da6 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClient.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClient.kt @@ -13,6 +13,7 @@ import java.net.ServerSocket import java.net.Socket import java.net.SocketException import java.time.Duration +import java.time.Instant import java.util.Collections import java.util.concurrent.ExecutorService import java.util.concurrent.Executors @@ -142,10 +143,10 @@ class GarminSimulatorClient( /** Wait for the server to start listing to requests. */ fun awaitReady(wait: Duration): Boolean { - var waitNanos = wait.toNanos() + val waitUntil = Instant.now() + wait readyLock.withLock { - while (!serverSocket.isBound && waitNanos > 0L) { - waitNanos = readyCond.awaitNanos(waitNanos) + while (!serverSocket.isBound && Instant.now() < waitUntil) { + readyCond.await(20, TimeUnit.MILLISECONDS) } } return serverSocket.isBound diff --git a/plugins/sync/src/test/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClientTest.kt b/plugins/sync/src/test/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClientTest.kt index 997213304a..9243260d07 100644 --- a/plugins/sync/src/test/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClientTest.kt +++ b/plugins/sync/src/test/kotlin/app/aaps/plugins/sync/garmin/GarminSimulatorClientTest.kt @@ -1,7 +1,6 @@ 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 @@ -19,6 +18,14 @@ class GarminSimulatorClientTest: TestBase() { private lateinit var client: GarminSimulatorClient private val receiver: GarminReceiver = mock() + private fun 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) @@ -28,33 +35,31 @@ class GarminSimulatorClientTest: TestBase() { fun receiveMessage() { 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)) Socket(ip, port).use { socket -> assertTrue(socket.isConnected) socket.getOutputStream().write(payload) socket.getOutputStream().flush() - verify(receiver).onConnect(client) + val device = waitForOrFail { client.connectedDevices.firstOrNull() } + verify(receiver, timeout(1_000)) + .onReceiveMessage(eq(client), eq(device.id), eq("SIMAPP"), eq(payload)) } - assertEquals(1, client.connectedDevices.size) - val device: GarminDevice = client.connectedDevices.first() - verify(receiver, timeout(1_000)) - .onReceiveMessage(eq(client), eq(device.id), eq("SIMAPP"), eq(payload)) } @Test 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) }