rouse-context/app
monkopedia-coder ce959cd33a
Fix leaked Main dispatcher flake in AuditHistoryViewModelRoomTest (#376) (#471)
AuditHistoryViewModelRoomTest pointed Dispatchers.Main at the shared
Dispatchers.IO pool and tore down with a fire-and-forget
testScope.coroutineContext[Job]?.cancel() (no join) before resetMain(),
and never cancelled the VM's viewModelScope at all. A viewModelScope
coroutine (the stateIn upstream / Room flow collector) could therefore
still be dispatching on Main when resetMain() ran, and could outlive it.

Unit tests run in a single JVM (no forkEvery), so the leaked Main override
surfaces as an IllegalStateException from kotlinx.coroutines.test's internal
TestMainDispatcher in a random later test's setMain. That is the moving-target
flake in #376 (CI run 27476494784 attempt 1: failures in
AuditHistoryViewModelRoomTest.kt:83 at resetMain and, downstream,
OnboardingViewModelTest).

Fix:
- Add MainDispatcherRule: pairs setMain/resetMain in a TestRule so the reset
  always runs in a finally around the whole test.
- Route Main to a dedicated single-thread executor the test owns (not shared
  IO), preserving the real cross-thread scheduler Room's invalidation tracker
  needs while making the dispatcher drainable.
- Own the VM in a ViewModelStore; in tearDown cancel viewModelScope via
  clear(), then shutdown()+awaitTermination() the executor so Main is fully
  quiescent (worker thread dead) before the rule's resetMain() fires. Join the
  warmup collector with cancelAndJoin on the runBlocking scope.

Co-authored-by: Monkopedia <monkopedia@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 16:28:09 -04:00
..
screenshots Clean up orphan screenshots and add missing test coverage (#434) 2026-04-27 17:51:31 -04:00
src Fix leaked Main dispatcher flake in AuditHistoryViewModelRoomTest (#376) (#471) 2026-06-13 16:28:09 -04:00
build.gradle.kts Add google/foss product flavors (#461) (#466) 2026-06-13 08:55:11 -04:00
google-services.json Add Firebase: google-services.json, Google Services plugin, debug applicationIdSuffix 2026-04-05 15:38:40 -04:00
proguard-rules.pro Add ProGuard rules for Ktor JMX references 2026-04-11 15:07:22 -04:00