share: await notifier outcomes instead of fixed delays
- Commit
06010a7d4056a958d5f827c97efd9b8a35e4f895- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 12:21 UTC (19 days ago)
- Parents
bf2e9a6
CI failed both ShareReceiveNotifier tests that drive a transfer to a terminal state (notifier-posts-request-then-done-and-retires, and GapPush4's notifier-done-with-real-image-preview). Both set the terminal state and then slept a fixed 400-800ms before asserting the notification had posted and retire() had been called. The notifier collects the transfer flows on Dispatchers.Default, so the sleep was a bet on CI scheduling, and it lost under load: the retire had simply not happened yet when the assertion read it. There is no product bug - given time, both outcomes land, as they do unloaded. The tests now poll the actual conditions (a posted notification exists; the session id appears in the retired list) with a real-time deadline, and the retired lists are synchronized lists since they are written on the notifier's dispatcher and read from the test thread. The failed-transfer test got the same treatment for consistency. No assertion weakened. Verified: ShareReceiveNotifierTest and GapPush4Test pass directly, and :share:testDevUnitTest was green in two parallel all-module runs.
Diff
share/src/test/java/com/vayunmathur/share/platform/GapPush4Test.kt
diff --git a/share/src/test/java/com/vayunmathur/share/platform/GapPush4Test.kt b/share/src/test/java/com/vayunmathur/share/platform/GapPush4Test.ktindex 3b1ef939f4..5f21a755cb 100644--- a/share/src/test/java/com/vayunmathur/share/platform/GapPush4Test.kt+++ b/share/src/test/java/com/vayunmathur/share/platform/GapPush4Test.kt@@ -101,16 +101,22 @@ class GapPush4Test { fake.pendingFiles.value = listOf(PendingFile(pngFile.name, pngFile.length(), "image/png")) fake.expectedTotalBytes.value = pngFile.length() val incoming = MutableStateFlow<List<com.vayunmathur.share.platform.IncomingTransfer>>(listOf(fake))- val retired = mutableListOf<Long>()+ val retired = java.util.Collections.synchronizedList(mutableListOf<Long>()) val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) val notifier = ShareReceiveNotifier(app, incoming, retire = { retired += it }) try { notifier.start(scope)- delay(300) fake.receivedFiles.value = listOf(ReceivedFile(pngFile.name, pngFile.length(), "image/png", Uri.fromFile(pngFile))) fake.state.value = ShareState.Completed- delay(800)- assertTrue(retired.contains(31337L))+ // Poll for the retire instead of a fixed delay: the notifier+ // collects on Dispatchers.Default, and 800ms is a bet on CI load.+ val deadline = System.currentTimeMillis() + 15_000+ var retiredDone = retired.contains(31337L)+ while (!retiredDone && System.currentTimeMillis() < deadline) {+ delay(25)+ retiredDone = retired.contains(31337L)+ }+ assertTrue(retiredDone) } finally { notifier.stop(); scope.cancel() } }
share/src/test/java/com/vayunmathur/share/platform/receive/ShareReceiveNotifierTest.kt
diff --git a/share/src/test/java/com/vayunmathur/share/platform/receive/ShareReceiveNotifierTest.kt b/share/src/test/java/com/vayunmathur/share/platform/receive/ShareReceiveNotifierTest.ktindex 57c4ff257d..ef1e5bcf4d 100644--- a/share/src/test/java/com/vayunmathur/share/platform/receive/ShareReceiveNotifierTest.kt+++ b/share/src/test/java/com/vayunmathur/share/platform/receive/ShareReceiveNotifierTest.kt@@ -40,27 +40,39 @@ class ShareReceiveNotifierTest { override val error = MutableStateFlow<String?>(null) } + /** Polls [cond] in real time: the notifier collects on Dispatchers.Default, so a fixed delay is a bet on CI load, not a wait for the outcome. */+ private suspend fun awaitShareCondition(cond: () -> Boolean): Boolean {+ val deadline = System.currentTimeMillis() + 15_000+ while (System.currentTimeMillis() < deadline) {+ if (cond()) return true+ delay(25)+ }+ return cond()+ }+ @Test fun `notifier posts request then done and retires`() = runBlocking { val fake = FakeTransfer(12345L) fake.pendingFiles.value = listOf(PendingFile("a.txt", 100, "text/plain")) fake.expectedTotalBytes.value = 100 val incoming = MutableStateFlow<List<IncomingTransfer>>(listOf(fake))- val retired = mutableListOf<Long>()+ val retired = java.util.Collections.synchronizedList(mutableListOf<Long>()) val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) val notifier = ShareReceiveNotifier(ctx, incoming, retire = { retired += it }) try { notifier.start(scope)- delay(200) fake.state.value = ShareState.AwaitingAccept- delay(400) val nm = ctx.getSystemService<NotificationManager>()!!- val active = Shadows.shadowOf(nm).allNotifications- assertTrue("expected a posted notification, got ${active.size}", active.isNotEmpty())+ assertTrue(+ "expected a posted notification",+ awaitShareCondition { Shadows.shadowOf(nm).allNotifications.isNotEmpty() },+ ) fake.state.value = ShareState.Completed fake.receivedFiles.value = listOf(ReceivedFile("a.txt", 100, "text/plain", Uri.parse("content://x/a")))- delay(500)- assertTrue("retire should be called for terminal state, retired=$retired", retired.contains(12345L))+ assertTrue(+ "retire should be called for terminal state, retired=$retired",+ awaitShareCondition { retired.contains(12345L) },+ ) } finally { notifier.stop(); scope.cancel() }@@ -78,15 +90,13 @@ class ShareReceiveNotifierTest { val fake = FakeTransfer(777L) fake.error.value = "handshake broke" val incoming = MutableStateFlow<List<IncomingTransfer>>(listOf(fake))- val retired = mutableListOf<Long>()+ val retired = java.util.Collections.synchronizedList(mutableListOf<Long>()) val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) val notifier = ShareReceiveNotifier(ctx, incoming, retire = { retired += it }) try { notifier.start(scope)- delay(200) fake.state.value = ShareState.Failed- delay(500)- assertTrue(retired.contains(777L))+ assertTrue(awaitShareCondition { retired.contains(777L) }) } finally { notifier.stop(); scope.cancel() } } }