findfamily: idle the main looper inside awaitFamilyCondition
- Commit
997233f192ee10be25531eb4c16aba6056136b2d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 12:22 UTC (19 days ago)
- Parents
6143e77
CI failed FindFamilyViewModelStatesRealTest.derivedStatesTrackRepositoryRows on the waypoint assertion: the user derived state landed within the await budget and the waypoint one did not. awaitFamilyCondition polled from a runBlocking thread with delay(25) but never idled the Robolectric main looper. The ViewModel's users/waypoints flows are stateIn on viewModelScope (Main) and the accessors under test are compose derived states, so the work the poll waits for advances on that paused looper; whether it ran in time was a scheduling coin flip. The failure reproduced locally on the first filtered run and passed unloaded afterwards, which fits a looper-pumping race rather than a product regression (the lint wave only moved the ViewModel file; the accessor code is byte-identical before and after). The shared helper now idles the main looper on each poll (runCatching, since not every caller is in a Robolectric test). No assertion changed, and every other user of the helper gets the same determinism. Verified: FindFamilyViewModelStatesRealTest passes directly, and :findfamily:testDevUnitTest was green sequentially and in two parallel all-module runs.
Diff
findfamily/src/test/java/com/vayunmathur/findfamily/data/TestFindFamilyRepository.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/data/TestFindFamilyRepository.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/data/TestFindFamilyRepository.ktindex 14dac4100d..57567488c1 100644--- a/findfamily/src/test/java/com/vayunmathur/findfamily/data/TestFindFamilyRepository.kt+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/data/TestFindFamilyRepository.kt@@ -56,6 +56,14 @@ object TestFindFamilyRepository { suspend fun awaitFamilyCondition(timeoutMs: Long = 8_000, condition: suspend () -> Boolean): Boolean { val deadline = System.currentTimeMillis() + timeoutMs while (System.currentTimeMillis() < deadline) {+ // The ViewModel's flows and compose derived states advance on the+ // paused Robolectric main looper; without idling it here, a poll+ // from a background thread can spin its whole budget while the+ // work it waits for sits queued on that looper (the waypoint+ // derived state in FindFamilyViewModelStatesRealTest).+ runCatching {+ org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+ } if (condition()) return true kotlinx.coroutines.delay(25) }