Modern Apps git

← Modern-Apps.git

contacts: fix CI-only edit/group test races (#tests)

Commit
2a9a8aaa43a1659a05eda3016c414e27be8526bf
Author
Vayun Mathur <[email protected]>
When
2026-11-16 18:25 UTC (20 days ago)
Parents
c5ca00a
Round 2 of the CI-only failure fixes (CI at da00cba19, TZ=UTC,
4 workers, all modules in one invocation; these passed in local
dev runs).

editPageFieldsUpdateTheDraft: the reseed loop used a wall-clock
deadline, but Robolectric's substituted clock runs ahead of real
time so the loop could expire before the debounced provider sync
landed; it is now step-counted, and the field/scroll assertions
pump the paused main looper until the nodes exist.

editPageNewContactSaveWithNameSucceeds: the old assertion passed
only while the IO save was still in flight (draft non-null / stack
unpopped) and failed precisely when the save finished first,
which is what CI hit. It now awaits the completed state โ€” draft
cleared and page popped โ€” and asserts that.

contactsInGroupFiltersByMembership: the VM's debounced sync
overwrites the seeded contact list whenever it lands, and the
WhileSubscribed membership flow can still hold its empty initial
value at first(). Seeding now repeats until the derived list is
exactly the fixture set, and group queries await the expected
value instead of taking the first snapshot.

Also drops the pre-sync hasLoadedContacts==false assertion in
ContactViewModelPass2Test: the VM's own init sync is already in
flight at construction, so that value is a scheduling race.

Reproduced under TZ=UTC LC_ALL=C.UTF-8 with 4 workers in the
combined module run; verified green in three consecutive
combined runs plus a TZ=America/New_York spot check.

Diff

contacts/src/test/java/com/vayunmathur/contacts/ui/InteractionsPass2Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/ui/InteractionsPass2Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/ui/InteractionsPass2Test.ktindex f8300d80da..ae1582d358 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/ui/InteractionsPass2Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/ui/InteractionsPass2Test.kt@@ -65,6 +65,14 @@ class InteractionsPass2Test {      @BeforeTest fun setUp() { Dispatchers.setMain(Dispatchers.Unconfined) } +    /** waitUntil that also idles Robolectric's paused main looper per poll. */+    private fun pumpWait(timeoutMs: Long = 15_000, cond: () -> Boolean) {+        rule.waitUntil(timeoutMs) {+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+            cond()+        }+    }+     private fun vm(): ContactViewModel {         val vm = ContactViewModel(app)         runBlocking { vm.syncFromSystem() }@@ -134,19 +142,23 @@ class InteractionsPass2Test {         }         // the VM's debounced init sync can overwrite the fixture with the         // (empty) provider list in a busy full-suite JVM, leaving a blank-        // draft: re-seed and re-init until the fixture draft lands-        val deadline = System.nanoTime() + 15_000_000_000L-        while (vm.editDraft.value?.firstName != "Aisha" && System.nanoTime() < deadline) {+        // draft: re-seed and re-init until the fixture draft lands. Step+        // counted, not wall-clock: Robolectric's substituted clock can run+        // ahead of real time and expire the loop before the sync lands.+        var steps = 0+        while (vm.editDraft.value?.firstName != "Aisha" && steps++ < 600) {             vm.allContactsState.value = listOf(fixtureContact())             vm.clearEditDraft()             vm.initEditDraft(7L)             if (vm.editDraft.value?.firstName == "Aisha") break-            Thread.sleep(50)+            try { Thread.sleep(25) } catch (_: InterruptedException) { }         }         assertEquals("Aisha", vm.editDraft.value?.firstName)          // first name is the first text field on the form+        pumpWait { rule.onAllNodes(hasSetTextAction()).fetchSemanticsNodes().isNotEmpty() }         rule.onAllNodes(hasSetTextAction())[0].performTextReplacement("Alicia")+        pumpWait { vm.editDraft.value?.firstName == "Alicia" }         assertEquals("Alicia", vm.editDraft.value?.firstName)          // company field is further down the (scrollable, fully composed) form@@ -154,6 +166,7 @@ class InteractionsPass2Test {         assertTrue(fields.fetchSemanticsNodes().size >= 4, "edit form should expose many fields")         // scroll the form and type into the last visible text field (note/company area)         rule.onNode(hasScrollAction()).performScrollToNode(hasText("hello note", substring = true))+        pumpWait { rule.onAllNodesWithText("hello note", substring = true).fetchSemanticsNodes().isNotEmpty() }         rule.onNodeWithText("hello note", substring = true).assertExists()     } @@ -168,13 +181,18 @@ class InteractionsPass2Test {                 DynamicTheme { EditContactPage(backStack, vm, Route.EditContact(contactId = null)) }             }         }-        rule.waitForIdle()+        pumpWait { rule.onAllNodes(hasSetTextAction()).fetchSemanticsNodes().isNotEmpty() }         rule.onAllNodes(hasSetTextAction())[0].performTextReplacement("Zoe")+        pumpWait { vm.editDraft.value?.firstName == "Zoe" }         assertEquals("Zoe", vm.editDraft.value?.firstName)         rule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.save), substring = true).performClick()-        rule.waitForIdle()-        // saving a named new contact exits the page (back stack popped / draft saved)-        assertTrue(vm.editDraft.value != null || backStack.backStack.isNotEmpty())+        // A successful save clears the draft and exits via onExit (pop).+        // Assert that completed state, not the mid-save one: the old+        // assertion passed only while the IO save was still in flight and+        // failed precisely when the save finished first (CI timing).+        pumpWait(20_000) { backStack.backStack.isEmpty() }+        assertTrue(backStack.backStack.isEmpty(), "saving a named new contact should pop the page")+        assertEquals(null, vm.editDraft.value)     }      @Test
contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.ktindex a3b02097a9..d2646fa2a1 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt@@ -111,7 +111,10 @@ class ContactViewModelPass2Test {     @Test     fun syncFromSystemMarksLoadedWithEmptyProvider() {         val vm = ContactViewModel(app)-        assertEquals(false, vm.hasLoadedContacts.value)+        // No assertion on the pre-sync value: the VM's own init sync is+        // already in flight (Unconfined Main + IO), so whether it has+        // landed by this line is a scheduling race, not behaviour โ€” it+        // failed combined runs precisely when the init sync won.         runBlocking { vm.syncFromSystem() }         assertEquals(true, vm.hasLoadedContacts.value)         // loadContacts only emits a sync trigger; calling it must not throw
contacts/src/test/java/com/vayunmathur/contacts/util/GroupAndSimImportPass2Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/util/GroupAndSimImportPass2Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/util/GroupAndSimImportPass2Test.ktindex 8c7ca5c11f..9558b2ca0d 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/util/GroupAndSimImportPass2Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/util/GroupAndSimImportPass2Test.kt@@ -69,18 +69,23 @@ class GroupAndSimImportPass2Test {         ),     ) -    private fun awaitContacts(vm: ContactViewModel, expectedSize: Int) {+    private fun awaitContacts(vm: ContactViewModel, expected: List<Contact>) {         val job = kotlinx.coroutines.CoroutineScope(Dispatchers.Default).launch {             vm.contacts.collect {}         }         try {-            val deadline = System.nanoTime() + 15_000_000_000L-            while (System.nanoTime() < deadline) {+            // The VM's init/debounced sync overwrites allContactsState with+            // the provider+SIM list whenever it lands, so a single seeding+            // races it: keep re-seeding until the derived list is exactly+            // the fixture set. Step counted (Robolectric clock runs ahead).+            var steps = 0+            while (steps++ < 800) {                 Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()-                if (vm.contacts.value.size == expectedSize) return-                Thread.sleep(10)+                if (vm.contacts.value == expected) return+                vm.allContactsState.value = expected+                try { Thread.sleep(25) } catch (_: InterruptedException) { }             }-            assertEquals(expectedSize, vm.contacts.value.size)+            assertEquals(expected, vm.contacts.value)         } finally {             job.cancel()         }@@ -99,11 +104,14 @@ class GroupAndSimImportPass2Test {         val bob = contact(2, "Bob", groupIds = listOf(12))         val cara = contact(3, "Cara", groupIds = listOf(11, 12))         vm.allContactsState.value = listOf(aisha, bob, cara)-        awaitContacts(vm, 3)+        awaitContacts(vm, listOf(aisha, bob, cara)) -        assertEquals(listOf(aisha, cara), withTimeout(15_000) { vm.contactsInGroup(11).first() })-        assertEquals(listOf(bob, cara), withTimeout(15_000) { vm.contactsInGroup(12).first() })-        assertEquals(emptyList(), withTimeout(15_000) { vm.contactsInGroup(99).first() })+        // first { it == expected }, not first(): the WhileSubscribed+        // membership flow can still hold its empty initial value when the+        // collector starts under load.+        assertEquals(listOf(aisha, cara), withTimeout(15_000) { vm.contactsInGroup(11).first { it == listOf(aisha, cara) } })+        assertEquals(listOf(bob, cara), withTimeout(15_000) { vm.contactsInGroup(12).first { it == listOf(bob, cara) } })+        assertEquals(emptyList(), withTimeout(15_000) { vm.contactsInGroup(99).first { it.isEmpty() && vm.contacts.value.size == 3 } })         assertEquals(aisha, vm.findContact(1))         assertEquals(null, vm.findContact(999))     }