tests: close the two flagged flake follow-ups
- Commit
2e9c873e834d8a5eca04de6eb52206580fab4d90- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-22 15:15 UTC (14 days ago)
- Parents
26c42da
Health's condition row click tapped a section header after waitForIdle while its Room list was still in flight (the toggle is a no-op on an empty section); all row tests now await the row in the flow, its semantics node, and the terminal back-stack route. Appstore's TvHomeBranchesTest and LoadHomeTest were non-hermetic: live repositories reaching real network for catalogue sync, and ViewModel init purging the test's seeded rows whenever the looper scheduled it; they now use the repo's hermetic pattern (failing download overrides, persisted enabled set, init-settle terminal wait, reseed after init). Verified under taskset 5 CPUs: health class x3 6/6 and full suite 631 green; appstore Native companion 186 green with both classes x3.
Diff
appstore/src/test/java/com/vayunmathur/appstore/tv/TvHomeBranchesTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvHomeBranchesTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvHomeBranchesTest.ktindex 00b0fe467f..ab62a21202 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvHomeBranchesTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvHomeBranchesTest.kt@@ -32,14 +32,81 @@ class TvHomeBranchesTest { @BeforeTest fun setUp() {+ // Hermetic offline (the TvLoadHomePlayAccrescentTest pattern):+ // catalogue downloads fail deterministically, so the ViewModel's+ // first-run sync fails closed instead of importing the real+ // catalogue — and real Play clusters instead of none — on a host+ // whose proxy happens to give the test JVM live network. Without+ // this the "offline" assertions below depend on the host's+ // network, not on the code.+ com.vayunmathur.appstore.data.FDroidRepository.downloadOverride = { url, _ ->+ throw java.io.IOException("offline test double for $url")+ } val app = ApplicationProvider.getApplicationContext<Application>()+ // The ViewModel's init reloads the enabled set from settings and+ // overwrites the flow: persist a known set first so that read+ // returns it instead of a previous test's persisted disabled set+ // (the settings DataStore outlives a single test in this JVM).+ kotlinx.coroutines.runBlocking {+ val settings = com.vayunmathur.appstore.data.SettingsRepository(+ app, kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.IO),+ )+ settings.setSourceEnabled(AppSource.PLAYSTORE, true)+ settings.setSourceEnabled(AppSource.ACCRESCENT, true)+ settings.setSourceEnabled(AppSource.MODERN_APPS, true)+ settings.setSourceEnabled(AppSource.FDROID, false)+ settings.setSourceEnabled(AppSource.IZZY, false)+ settings.setSourceEnabled(AppSource.PROPRIETARY, false)+ } db = Room.inMemoryDatabaseBuilder(app, AppDatabase::class.java) .setDriver(BundledSQLiteDriver()) .allowMainThreadQueries() .build()+ // Hermetic offline: Play dispensing fails and the Accrescent API+ // is a dead channel, whatever network the test host has.+ val offlinePlay = com.vayunmathur.appstore.data.play.PlayRepository(+ app,+ object : com.vayunmathur.appstore.data.play.AnonymousAuthRepository() {+ override suspend fun ensureAuthData(+ deviceProps: java.util.Properties,+ dispenserUrls: List<String>,+ ): Result<com.aurora.gplayapi.data.models.AuthData> =+ Result.failure(java.io.IOException("offline test double"))+ },+ )+ val deadChannel = io.grpc.inprocess.InProcessChannelBuilder+ .forName("tv-home-branches-offline").directExecutor().build()+ val failingFetcher = object :+ com.vayunmathur.appstore.data.accrescent.AccrescentRepoDataFetcher(app) {+ override suspend fun fetch():+ Result<com.vayunmathur.appstore.data.accrescent.RepoData> =+ Result.failure(java.io.IOException("offline test double"))+ } // Not closed: the ViewModel scope keeps collecting DB flows; the // in-memory DB dies with the Robolectric sandbox.- vm = TvStoreViewModel(app, db)+ vm = TvStoreViewModel(+ app, db,+ playOverride = offlinePlay,+ accrescentOverride = com.vayunmathur.appstore.data.accrescent.AccrescentRepository(+ app, db,+ com.vayunmathur.appstore.data.accrescent.AccrescentApi { deadChannel },+ failingFetcher,+ ),+ )+ // Start every test after the init pass, not racing it: init+ // re-reads the enabled set, purges disabled sources' rows and+ // runs its own loadHome, and any of those landing after a test's+ // own setup overwrites the state the test just arranged (under+ // CPU contention that interleave could also stall the pumped+ // waits below for their whole step budget). isLoading starts+ // true and only the init sync/loadHome path clears it, so this+ // is a terminal signal, not a timing guess.+ await("init settled") { !vm.home.value.isLoading }+ }++ @kotlin.test.AfterTest+ fun tearDown() {+ com.vayunmathur.appstore.data.FDroidRepository.downloadOverride = null } private fun await(description: String, condition: () -> Boolean) {
appstore/src/test/java/com/vayunmathur/appstore/util/LoadHomeTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/util/LoadHomeTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/util/LoadHomeTest.ktindex e9005a46fd..cef683a31f 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/util/LoadHomeTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/util/LoadHomeTest.kt@@ -35,6 +35,25 @@ class LoadHomeTest { private lateinit var db: AppDatabase private lateinit var vm: AppStoreViewModel + private fun seedCatalogue() = runBlocking {+ db.cachedAppDao().upsertAll(+ listOf(+ UnifiedApp(+ packageName = "com.example.modern",+ source = AppSource.MODERN_APPS,+ name = "Modern",+ repoUrl = DefaultRepos.MODERN_APPS.url,+ ).toEntity().copy(lastUpdated = 300L),+ UnifiedApp(+ packageName = "com.example.fdroid",+ source = AppSource.FDROID,+ name = "Fdroid",+ repoUrl = DefaultRepos.FDROID.url,+ ).toEntity().copy(lastUpdated = 200L),+ ),+ )+ }+ @BeforeTest fun setUp() { val app = ApplicationProvider.getApplicationContext<Application>()@@ -42,29 +61,37 @@ class LoadHomeTest { .setDriver(BundledSQLiteDriver()) .allowMainThreadQueries() .build()- runBlocking {- db.cachedAppDao().upsertAll(- listOf(- UnifiedApp(- packageName = "com.example.modern",- source = AppSource.MODERN_APPS,- name = "Modern",- repoUrl = DefaultRepos.MODERN_APPS.url,- ).toEntity().copy(lastUpdated = 300L),- UnifiedApp(- packageName = "com.example.fdroid",- source = AppSource.FDROID,- name = "Fdroid",- repoUrl = DefaultRepos.FDROID.url,- ).toEntity().copy(lastUpdated = 200L),- ),- )- }+ seedCatalogue() // Not closed: the ViewModel scope keeps collecting DB flows; the // in-memory DB dies with the Robolectric sandbox. vm = AppStoreViewModel(app, db) } + /**+ * Settle-then-reseed wrapper for the test bodies.+ *+ * Init re-reads the enabled set from settings (a fresh install's+ * DEFAULT_ENABLED: MODERN_APPS + PROPRIETARY), purges every other+ * source's cached rows — including this fixture's F-Droid row — and+ * runs its own loadHome with that set. Racing the test's loadHome+ * against that pass made the outcome depend on which coroutine the+ * paused main looper happened to run first: when init landed last,+ * the F-Droid row was purged and the home state overwritten, and+ * the pumped waits below burned their whole step budget (~300s of+ * parking) on a condition that could never become true again.+ * The modern-recent section is init's terminal home signal (only+ * its loadHome can produce it here), and re-seeding afterwards+ * restores the row init legitimately purged — the+ * TvStoreLoadHomeTest arrangement.+ */+ private fun afterInitSettled(body: () -> Unit) {+ await("init home") {+ vm.home.value.sections.any { it.id == "modern-recent" }+ }+ seedCatalogue()+ body()+ }+ private fun await(description: String, condition: () -> Boolean) { try { pumpUntil(description, condition)@@ -109,8 +136,10 @@ class LoadHomeTest { } @Test- fun `loadHome fills a recent row per enabled source`() = runBlocking {- vm.loadHome(setOf(AppSource.MODERN_APPS, AppSource.FDROID))+ fun `loadHome fills a recent row per enabled source`() = afterInitSettled {+ runBlocking {+ vm.loadHome(setOf(AppSource.MODERN_APPS, AppSource.FDROID))+ } await("home rows") { vm.home.value.sections.map { it.id }.containsAll(listOf("modern-recent", "fdroid-recent")) }@@ -121,12 +150,16 @@ class LoadHomeTest { } @Test- fun `loadHome with nothing enabled clears the recent rows`() = runBlocking {- vm.loadHome(setOf(AppSource.MODERN_APPS))+ fun `loadHome with nothing enabled clears the recent rows`() = afterInitSettled {+ runBlocking {+ vm.loadHome(setOf(AppSource.MODERN_APPS))+ } await("modern row") { vm.home.value.sections.any { it.id == "modern-recent" } }- vm.loadHome(emptySet())+ runBlocking {+ vm.loadHome(emptySet())+ } await("rows cleared") { vm.recentlyUpdatedFlow.value.isEmpty() && vm.home.value.sections.none { it.id == "modern-recent" }
health/src/test/java/com/vayunmathur/health/ui/MedicalRowClickTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/ui/MedicalRowClickTest.kt b/health/src/test/java/com/vayunmathur/health/ui/MedicalRowClickTest.ktindex 2e00bdecc4..b6c39e68d9 100644--- a/health/src/test/java/com/vayunmathur/health/ui/MedicalRowClickTest.kt+++ b/health/src/test/java/com/vayunmathur/health/ui/MedicalRowClickTest.kt@@ -6,6 +6,7 @@ import androidx.compose.ui.test.hasClickAction import androidx.compose.ui.test.hasScrollAction import androidx.compose.ui.test.hasText import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollToNode@@ -25,6 +26,8 @@ import com.vayunmathur.library.util.LocalNavResultRegistry import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.NavResultRegistry import com.vayunmathur.library.util.databases+import com.vayunmathur.testsupport.awaitSemantics+import com.vayunmathur.testsupport.awaitWithMainIdle import java.time.Instant import kotlin.test.BeforeTest import kotlin.test.Test@@ -68,13 +71,37 @@ class MedicalRowClickTest { } } - private fun expandAndClick(section: String, row: String) {+ /**+ * Expand [section] and tap [row], waiting only on terminal signals.+ *+ * The section header's toggle is a no-op while its entry list is+ * still empty, and the ViewModel's lists are Room flows republished+ * through `stateIn` on the main looper: under CPU contention a plain+ * `waitForIdle()` can return while that IO delivery is still in+ * flight (the RecipeEditorFlowTest genus), so the tap landed on a+ * still-empty section and the row never appeared. Wait first for+ * the row to be in the ViewModel's terminal flow state, then for+ * the row's semantics node to exist (the expand animation runs on+ * the composition scheduler, which [awaitSemantics] pumps), and+ * finally for the navigation itself — the edit route on the back+ * stack is the click's terminal signal.+ */+ private fun expandAndClick(+ section: String,+ row: String,+ loaded: () -> Boolean,+ expected: () -> Route,+ backStack: NavBackStack<Route>,+ ) {+ awaitWithMainIdle("$row loaded in view model") { loaded() } rule.onNode(hasScrollAction()).performScrollToNode(hasText(section)) rule.onNodeWithText(section).performClick()- rule.waitForIdle()+ rule.awaitSemantics("$row row composed") {+ rule.onAllNodesWithText(row).fetchSemanticsNodes().isNotEmpty()+ } rule.onNode(hasScrollAction()).performScrollToNode(hasText(row)) rule.onNodeWithText(row).performClick()- rule.waitForIdle()+ rule.awaitSemantics("$row opens its edit form") { backStack.last() == expected() } } @Test@@ -83,7 +110,12 @@ class MedicalRowClickTest { val vm = MedicalViewModel(context, repo()) val backStack = NavBackStack<Route>(arrayOf(Route.Medical)) page(vm, backStack)- expandAndClick("Vaccinations", "RowClick Flu")+ expandAndClick(+ "Vaccinations", "RowClick Flu",+ loaded = { vm.vaccinations.value.any { it.id == "row-vac-1" } },+ expected = { Route.EditVaccination("row-vac-1") },+ backStack = backStack,+ ) assertEquals(Route.EditVaccination("row-vac-1"), backStack.last()) } @@ -93,7 +125,12 @@ class MedicalRowClickTest { val vm = MedicalViewModel(context, repo()) val backStack = NavBackStack<Route>(arrayOf(Route.Medical)) page(vm, backStack)- expandAndClick("Allergies", "RowClick Peanut")+ expandAndClick(+ "Allergies", "RowClick Peanut",+ loaded = { vm.allergies.value.any { it.id == "row-alg-1" } },+ expected = { Route.EditAllergy("row-alg-1") },+ backStack = backStack,+ ) assertEquals(Route.EditAllergy("row-alg-1"), backStack.last()) } @@ -103,7 +140,12 @@ class MedicalRowClickTest { val vm = MedicalViewModel(context, repo()) val backStack = NavBackStack<Route>(arrayOf(Route.Medical)) page(vm, backStack)- expandAndClick("Conditions", "RowClick Asthma")+ expandAndClick(+ "Conditions", "RowClick Asthma",+ loaded = { vm.conditions.value.any { it.id == "row-cond-1" } },+ expected = { Route.EditCondition("row-cond-1") },+ backStack = backStack,+ ) assertEquals(Route.EditCondition("row-cond-1"), backStack.last()) } @@ -113,7 +155,12 @@ class MedicalRowClickTest { val vm = MedicalViewModel(context, repo()) val backStack = NavBackStack<Route>(arrayOf(Route.Medical)) page(vm, backStack)- expandAndClick("Lab results", "RowClick A1c")+ expandAndClick(+ "Lab results", "RowClick A1c",+ loaded = { vm.labResults.value.any { it.id == "row-lab-1" } },+ expected = { Route.EditLabResult("row-lab-1") },+ backStack = backStack,+ ) assertEquals(Route.EditLabResult("row-lab-1"), backStack.last()) } @@ -123,7 +170,12 @@ class MedicalRowClickTest { val vm = MedicalViewModel(context, repo()) val backStack = NavBackStack<Route>(arrayOf(Route.Medical)) page(vm, backStack)- expandAndClick("Medications", "RowClick Med")+ expandAndClick(+ "Medications", "RowClick Med",+ loaded = { vm.medications.value.any { it.id == "row-med-1" } },+ expected = { Route.EditMedication("row-med-1") },+ backStack = backStack,+ ) assertEquals(Route.EditMedication("row-med-1"), backStack.last()) } @@ -137,8 +189,15 @@ class MedicalRowClickTest { assertTrue(clickables.isNotEmpty()) // The FAB toggle is the last clickable in the tree (bottom-end menu button). rule.onAllNodes(hasClickAction())[clickables.size - 1].performClick()- rule.waitForIdle()+ // The menu item composing is the terminal signal for the toggle+ // (same idle-vs-animation race as the row expansion above).+ rule.awaitSemantics("fab menu composed") {+ rule.onAllNodesWithText("Add vaccination").fetchSemanticsNodes().isNotEmpty()+ } rule.onNodeWithText("Add vaccination").performClick()+ rule.awaitSemantics("add vaccination route") {+ backStack.last() == Route.EditVaccination()+ } assertEquals(Route.EditVaccination(), backStack.last()) } }