keyboard: settle persisted-settings observers before ServiceOps tests
- Commit
4ff69457f9a879f69505f61429fb1c943ca01c40- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 12:22 UTC (19 days ago)
- Parents
997233f
CI failed ServiceOpsTest's useComposing-requires-english-layout and updateSuggestions-respects-settings tests. Both read synchronous, settings-dependent service state immediately after setUp. KeyboardService keeps kbState.settings in sync with the persisted DataStore settings through a dozen flow collectors launched on its scope in observeSettings. Their first emissions land asynchronously (the store hydrates off an IO thread), and every earlier test in the class that persisted a setting (a non-English layout, suggestions off) could have that value applied by a late first emission in the middle of a later test that had just assigned kbState.settings directly. The two named tests are exactly the ones whose assertions depend on those fields, and both pass unloaded where the emissions land during setUp's single idle. setUp now forces store hydration and then idles the main looper until the in-memory settings have converged with a fresh persisted load, so the observers have nothing left to overwrite when a test starts. The tests themselves and all assertions are unchanged. Verified: ServiceOpsTest passes directly, and :keyboard:testDevUnitTest was green in two parallel all-module runs.
Diff
keyboard/src/test/java/com/vayunmathur/keyboard/platform/ime/ServiceOpsTest.kt
diff --git a/keyboard/src/test/java/com/vayunmathur/keyboard/platform/ime/ServiceOpsTest.kt b/keyboard/src/test/java/com/vayunmathur/keyboard/platform/ime/ServiceOpsTest.ktindex 27f1376f08..b41861349a 100644--- a/keyboard/src/test/java/com/vayunmathur/keyboard/platform/ime/ServiceOpsTest.kt+++ b/keyboard/src/test/java/com/vayunmathur/keyboard/platform/ime/ServiceOpsTest.kt@@ -89,6 +89,25 @@ class ServiceOpsTest { controller = Robolectric.buildService(KeyboardService::class.java) service = controller.create().get() ShadowLooper.idleMainLooper()+ // The service keeps kbState.settings in sync with the persisted+ // settings via flow collectors on its scope. Those first emissions+ // land asynchronously (the store hydrates off an IO thread), so a+ // test that assigns kbState.settings directly could be overwritten+ // mid-assertion by a late first emission carrying a previous+ // test's persisted values (the useComposing / updateSuggestions+ // flakes). Wait until the in-memory settings have converged with+ // a fresh persisted load before any test touches them.+ kotlinx.coroutines.runBlocking {+ service.ds.getStringAwait(com.vayunmathur.keyboard.data.KeyboardSettings.Keys.ACTIVE_LAYOUT)+ }+ val deadline = System.currentTimeMillis() + 10_000+ while (service.kbState.settings != com.vayunmathur.keyboard.data.KeyboardSettings.load(service.ds) &&+ System.currentTimeMillis() < deadline+ ) {+ ShadowLooper.idleMainLooper()+ Thread.sleep(10)+ }+ ShadowLooper.idleMainLooper() } @AfterTest