Modern Apps git

← Modern-Apps.git

communicate: make W5Slice1EventProcessorTest deterministic in CI

Commit
ecf662320d6b8404325ab50b51deca0b003bf9d2
Author
Vayun Mathur <[email protected]>
When
2026-11-07 13:55 UTC (29 days ago)
Parents
a820f79
The processor collects on Dispatchers.IO in real time, but the
tests polled it with delay() inside runTest, whose virtual clock
spins the poll out in milliseconds on a slow CI box; the first
row then NPE'd at the !! lookup. Poll in real time with a 15s
deadline and assert on timeout instead. Also fix stripWa, which
duplicated the "@host" suffix and never matched group or
history-sync conversations.

Verified: targeted class 3x (--no-build-cache) and the full
:communicate:testDevUnitTest suite (1202 tests) pass.

(cherry picked from commit 09ad11b4a90a79fe19fff462bff9cf9ecc58e58a)

Diff

communicate/src/test/java/com/vayunmathur/communicate/whatsapp/W5Slice1EventProcessorTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/W5Slice1EventProcessorTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/W5Slice1EventProcessorTest.ktindex 3373b79f75..d74e8f3910 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/W5Slice1EventProcessorTest.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/W5Slice1EventProcessorTest.kt@@ -53,23 +53,30 @@ class W5Slice1EventProcessorTest {         db.close()     } +    private companion object {+        const val POLL_TIMEOUT_MS = 15_000L+    }+     private suspend fun emit(e: WhatsAppEvent) {         events.emit(e)-        // start() collects on Dispatchers.IO; poll until the row lands (bounded).-        var waited = 0-        while (waited < 200 && !settled(e)) {-            kotlinx.coroutines.delay(10)-            waited+++        // start() collects on Dispatchers.IO (real time), while runTest's delay()+        // is virtual and would spin this poll out in milliseconds on a slow box.+        // Poll in real time instead, and fail with a clear message on timeout+        // rather than a downstream NPE on a row that never landed.+        val deadline = System.currentTimeMillis() + POLL_TIMEOUT_MS+        while (System.currentTimeMillis() < deadline && !settled(e)) {+            Thread.sleep(10)         }+        assertTrue(settled(e), "event $e was not persisted within ${POLL_TIMEOUT_MS}ms")     } -    /** Poll until [cond] holds (bounded); fails the test on timeout via empty result. */+    /** Poll until [cond] holds in real time (bounded); fails the test on timeout. */     private suspend fun awaitSettled(cond: suspend () -> Boolean) {-        var waited = 0-        while (waited < 200 && !cond()) {-            kotlinx.coroutines.delay(10)-            waited+++        val deadline = System.currentTimeMillis() + POLL_TIMEOUT_MS+        while (System.currentTimeMillis() < deadline && !cond()) {+            Thread.sleep(10)         }+        assertTrue(cond(), "condition was not met within ${POLL_TIMEOUT_MS}ms")     }      /** True when the emitted event's effect is observable in the DB. */@@ -83,9 +90,12 @@ class W5Slice1EventProcessorTest {         else -> true     } -    private fun stripWa(id: String) = id.removePrefix("wa:").substringBefore(':').let {-        val at = id.removePrefix("wa:").indexOf('@')-        if (at <= 0) id.removePrefix("wa:") else it + id.removePrefix("wa:").substring(at)+    /** Mirrors the processor's chatJid: strips "wa:" and any ":device" suffix. */+    private fun stripWa(id: String): String {+        val s = id.removePrefix("wa:")+        val at = s.indexOf('@')+        if (at <= 0) return s+        return s.substring(0, at).substringBefore(':') + s.substring(at)     }      @Test