games-wordmaker: bound test waits in real time and retry Main swaps
- Commit
e8faa5b808c2e520feec64b07cc94c84db61428b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 12:21 UTC (19 days ago)
- Parents
4a34302
CI hit a BUILD timeout in :games:wordmaker:testDevUnitTest, and under parallel load the suite reproduces as IllegalStateException: Dispatchers.Main is used concurrently with setting it, thrown from tearDown's resetMain while a stale ViewModel coroutine (a StateFlow resume) is still dispatching to Main. Two test-side root causes: 1. Every pollUntil/waitFor in the suite bounded its wait by System.currentTimeMillis. Under Robolectric that is the substituted scheduler clock, which only advances when the scheduler runs: a condition that never landed could spin the poll forever instead of failing (a hang, not a failure), and a healthy condition could also be cut off early. All of them (WordMakerViewModelTest, ViewModelDeepTest, GameLoaderPageDeepTest, both classes in WordMakerComposeTest) are now step-counted with a real Thread.sleep per step, so the budget is a real duration that always terminates. 2. Dispatchers.setMain/resetMain in setUp/tearDown threw when a background coroutine was using Main at that instant. They are now wrapped in the same retrying helper the nonogram suite already uses for exactly this race, in all six test classes that swap Main. No product code changed and no assertion weakened: polls still assert the same conditions, only their clocks changed. Verified: :games:wordmaker:testDevUnitTest green twice sequentially (about 50s each, normal time), after failing under a parallel all-module load run before the Main-swap fix.
Diff
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/ViewModelDeepTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/ViewModelDeepTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/ViewModelDeepTest.ktindex 559cae5947..3a0b0b0122 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/ViewModelDeepTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/ViewModelDeepTest.kt@@ -29,7 +29,7 @@ class ViewModelDeepTest { private val scope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } runCatching { androidx.work.WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) }@@ -37,7 +37,7 @@ class ViewModelDeepTest { @AfterTest fun tearDown() { scope.cancel()- Dispatchers.resetMain()+ retryingMain { Dispatchers.resetMain() } } private fun vm(): WordMakerViewModel {@@ -66,15 +66,27 @@ class ViewModelDeepTest { return vm } ++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } private fun pollUntil(deadlineMs: Long = 30000, cond: () -> Boolean) {- val deadline = System.currentTimeMillis() + deadlineMs- while (System.currentTimeMillis() < deadline && !cond()) Thread.sleep(50)+ // Step-counted, not wall-clock: Robolectric's substituted+ // currentTimeMillis only advances when its scheduler runs, so a+ // deadline poll could spin forever (the CI build timeout) if the+ // condition it waited for never landed, or expire instantly.+ // Each step sleeps real time, bounding the wait in real time.+ var steps = (deadlineMs / 50).toInt()+ while (steps-- > 0 && !cond()) Thread.sleep(50) assertTrue(cond(), "condition not met within ${deadlineMs}ms") } private fun waitFor(deadlineMs: Long = 8000, cond: () -> Boolean): Boolean {- val deadline = System.currentTimeMillis() + deadlineMs- while (System.currentTimeMillis() < deadline && !cond()) Thread.sleep(50)+ var steps = (deadlineMs / 50).toInt()+ while (steps-- > 0 && !cond()) Thread.sleep(50) return cond() }
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.ktindex 3f55adaf0d..21e01933a3 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt@@ -34,13 +34,13 @@ class WordMakerViewModelTest { private val collectorScope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } // DailyPuzzleReminder reschedules through WorkManager, which Robolectric does not init. runCatching { androidx.work.WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) } }- @AfterTest fun tearDown() { collectorScope.cancel(); Dispatchers.resetMain() }+ @AfterTest fun tearDown() { collectorScope.cancel(); retryingMain { Dispatchers.resetMain() } } /** StateIn(WhileSubscribed) flows only compute while collected, as the UI collects them. */ private fun vm(): WordMakerViewModel {@@ -65,9 +65,21 @@ class WordMakerViewModelTest { return vm } ++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } private fun pollUntil(deadlineMs: Long = 15000, cond: () -> Boolean) {- val deadline = System.currentTimeMillis() + deadlineMs- while (System.currentTimeMillis() < deadline && !cond()) Thread.sleep(50)+ // Step-counted, not wall-clock: Robolectric's substituted+ // currentTimeMillis only advances when its scheduler runs, so a+ // deadline poll could spin forever (the CI build timeout) if the+ // condition it waited for never landed, or expire instantly.+ // Each step sleeps real time, bounding the wait in real time.+ var steps = (deadlineMs / 50).toInt()+ while (steps-- > 0 && !cond()) Thread.sleep(50) assertTrue(cond()) }
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.ktindex bc167a8c95..4a079a77d4 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt@@ -44,7 +44,7 @@ class GameLoaderPageDeepTest { private val scope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } runCatching { androidx.work.WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) }@@ -52,7 +52,7 @@ class GameLoaderPageDeepTest { @AfterTest fun tearDown() { scope.cancel()- Dispatchers.resetMain()+ retryingMain { Dispatchers.resetMain() } } private fun vm(): WordMakerViewModel {@@ -81,9 +81,21 @@ class GameLoaderPageDeepTest { return vm } ++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } private fun pollUntil(deadlineMs: Long = 30000, cond: () -> Boolean) {- val deadline = System.currentTimeMillis() + deadlineMs- while (System.currentTimeMillis() < deadline && !cond()) Thread.sleep(50)+ // Step-counted, not wall-clock: Robolectric's substituted+ // currentTimeMillis only advances when its scheduler runs, so a+ // deadline poll could spin forever (the CI build timeout) if the+ // condition it waited for never landed, or expire instantly.+ // Each step sleeps real time, bounding the wait in real time.+ var steps = (deadlineMs / 50).toInt()+ while (steps-- > 0 && !cond()) Thread.sleep(50) assertTrue(cond(), "condition not met within ${deadlineMs}ms") }
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/ScreenInteractionsDeepTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/ScreenInteractionsDeepTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/ScreenInteractionsDeepTest.ktindex 2cc06352b1..9881a806f6 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/ScreenInteractionsDeepTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/ScreenInteractionsDeepTest.kt@@ -58,12 +58,12 @@ class ScreenInteractionsDeepTest { private val scope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } } @AfterTest fun tearDown() { scope.cancel()- Dispatchers.resetMain()+ retryingMain { Dispatchers.resetMain() } } private fun board() = CrosswordData.fromString("CAT\n C \n T ")!!@@ -77,6 +77,13 @@ class ScreenInteractionsDeepTest { bonusWords = bonus, gameMode = mode, ) ++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } @Test fun shuffleChangesTheWheelOrder() { rule.setContent { DynamicTheme {
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/WordMakerComposeTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/WordMakerComposeTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/WordMakerComposeTest.ktindex 2086a9b4c3..d4c38c80d1 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/WordMakerComposeTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/WordMakerComposeTest.kt@@ -69,13 +69,20 @@ class WordMakerComposeTest { private val scope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } // DailyPuzzleReminder reschedules through WorkManager, which Robolectric does not init. runCatching { androidx.work.WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) } }- @AfterTest fun tearDown() { scope.cancel(); Dispatchers.resetMain() }+ @AfterTest fun tearDown() { scope.cancel(); retryingMain { Dispatchers.resetMain() } }++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } private fun board() = CrosswordData.fromString("CAT\n C \n T ")!! private val level101 = CrosswordData.fromString("...FIVE.\n.F.I.E..\nRIFE.R.R\n.R.R.IVY\nVERY.F.E\n.....Y..")!!@@ -444,9 +451,10 @@ class WordMakerComposeTest { val app = ApplicationProvider.getApplicationContext<Application>() val vm = WordMakerViewModel(app) scope.launch { vm.crosswordData.collect {} }- // wait for the casual level to load- val deadline = System.currentTimeMillis() + 20000- while (System.currentTimeMillis() < deadline && vm.crosswordData.value == null) Thread.sleep(50)+ // wait for the casual level to load (step-counted: the+ // substituted clock can freeze, see pollUntil below)+ var steps = 400+ while (steps-- > 0 && vm.crosswordData.value == null) Thread.sleep(50) val data = vm.crosswordData.value assertTrue(data != null) val json = context.assets.open("achievements.json").bufferedReader().use { it.readText() }@@ -466,13 +474,13 @@ class WordMakerNavAndDragTest { private val context: Context get() = ApplicationProvider.getApplicationContext() private val scope = CoroutineScope(Dispatchers.Unconfined) @BeforeTest fun setUp() {- Dispatchers.setMain(Dispatchers.Unconfined)+ retryingMain { Dispatchers.setMain(Dispatchers.Unconfined) } // DailyPuzzleReminder reschedules through WorkManager, which Robolectric does not init. runCatching { androidx.work.WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) } }- @AfterTest fun tearDown() { scope.cancel(); Dispatchers.resetMain() }+ @AfterTest fun tearDown() { scope.cancel(); retryingMain { Dispatchers.resetMain() } } private fun vm(): WordMakerViewModel { val vm = WordMakerViewModel(ApplicationProvider.getApplicationContext<Application>())@@ -483,9 +491,21 @@ class WordMakerNavAndDragTest { scope.launch { vm.bonusWords.collect {} } return vm }++ /** set/resetMain throw if a background coroutine is using Main at that instant (a stale ViewModel's flow resuming); retry briefly, as the nonogram suite does. */+ private fun retryingMain(action: () -> Unit) {+ var attempts = 0+ while (attempts++ < 50 && runCatching(action).isFailure) Thread.sleep(10)+ if (attempts > 50) action()+ } private fun pollUntil(deadlineMs: Long = 20000, cond: () -> Boolean) {- val deadline = System.currentTimeMillis() + deadlineMs- while (System.currentTimeMillis() < deadline && !cond()) Thread.sleep(50)+ // Step-counted, not wall-clock: Robolectric's substituted+ // currentTimeMillis only advances when its scheduler runs, so a+ // deadline poll could spin forever (the CI build timeout) if the+ // condition it waited for never landed, or expire instantly.+ // Each step sleeps real time, bounding the wait in real time.+ var steps = (deadlineMs / 50).toInt()+ while (steps-- > 0 && !cond()) Thread.sleep(50) assertTrue(cond()) }