From 262578528b8ac46b8acc33e774edbeb77ff4301a Mon Sep 17 00:00:00 2001 From: M57 Date: Sun, 21 Jun 2026 16:26:07 +0300 Subject: [PATCH] fix: stabilize flaky testAutoReconnect with polling assertion --- .../hermescontrol/data/ws/HermesWsClientTest.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/com/m57/hermescontrol/data/ws/HermesWsClientTest.kt b/app/src/test/java/com/m57/hermescontrol/data/ws/HermesWsClientTest.kt index 45740e74..9bcf1acd 100644 --- a/app/src/test/java/com/m57/hermescontrol/data/ws/HermesWsClientTest.kt +++ b/app/src/test/java/com/m57/hermescontrol/data/ws/HermesWsClientTest.kt @@ -323,10 +323,20 @@ class HermesWsClientTest { serverSocket1?.close(1001, "Server shutting down") assertTrue("Client didn't detect disconnect", clientDisconnectedLatch.await(5, TimeUnit.SECONDS)) - assertEquals(ConnectionStatus.RECONNECTING, HermesWsClient.connectionStatus.value) + + // Poll for status to become RECONNECTING (async race mitigation — + // onClosed fires onDisconnected before updating _connectionStatus) + var status: ConnectionStatus + val deadline = System.currentTimeMillis() + 2000 + do { + status = HermesWsClient.connectionStatus.value + if (status == ConnectionStatus.RECONNECTING) break + Thread.sleep(50) + } while (System.currentTimeMillis() < deadline) + assertEquals(ConnectionStatus.RECONNECTING, status) // The client should now attempt to reconnect after initial backoff (1000ms) // Wait for the second connection to hit the server - assertTrue("Failed to reconnect", connect2Latch.await(5, TimeUnit.SECONDS)) + assertTrue("Failed to reconnect", connect2Latch.await(6, TimeUnit.SECONDS)) } }