Modern Apps git

← Modern-Apps.git

appstore: await first compositions and take settings edits off main

Commit
2aa16b104fd05be842c4b0b783d88870cd057d14
Author
Vayun Mathur <[email protected]>
When
2026-11-17 13:42 UTC (19 days ago)
Parents
335b736
CI failed SourcesPageTest's all-source-cards test and TvMainActivityTest's rail test. Both asserted on nodes immediately after setContent / waitForIdle; a screen's first composition lands on Robolectric's paused main looper, which those waits do not pump, so under load the assertions ran before anything composed (the Sources cards are static content; nothing was wrong with them beyond not existing yet). SourcesPageTest now waits for the first card with a helper that idles that looper per poll, and TvMainActivityTest waits for the rail the same way its destination waits further down already did.

While verifying, the full suite also deadlocked twice inside AccrescentVmOpsW5Test.phoneVmHomeBrowseAndPackageSelection at runBlocking { settings.setSourceEnabled(...) } on the main thread (thread dump: main thread parked in the runBlocking, the edit never dispatched; the class passes alone). This class shares the appstore-settings DataStore with every earlier ViewModel's still-collecting SettingsRepository mirror, and an edit issued from the paused main thread can wait indefinitely for work queued on that thread. The four settings edits in this class now run on Dispatchers.IO, like the rest of the suite's IO. The change is test-only: the production setSourceEnabled path is untouched, and no assertion was weakened anywhere.

Verified: SourcesPageTest, TvMainActivityTest and AccrescentVmOpsW5Test pass directly, and :appstore:testDevUnitTest is green twice in full sequential runs (about eleven minutes each); before the IO change, two of three full runs hung in that DataStore edit.

Diff

appstore/src/test/java/com/vayunmathur/appstore/tv/TvMainActivityTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvMainActivityTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvMainActivityTest.ktindex 3ae007936f..5bfca64690 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvMainActivityTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvMainActivityTest.kt@@ -66,6 +66,14 @@ class TvMainActivityTest {      @Test     fun `rail renders and destinations can be selected`() {+        // The activity's first composition lands on the paused Robolectric+        // main looper, which waitForIdle alone does not pump: wait for the+        // rail itself, idling that looper per poll (as the destination+        // waits below already do).+        composeRule.waitUntil(timeoutMillis = 15_000) {+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+            composeRule.onAllNodesWithText("Home").fetchSemanticsNodes().isNotEmpty()+        }         composeRule.waitForIdle()         for (label in listOf("Home", "Search", "Updates", "Library", "Sources")) {             composeRule.onNodeWithText(label).assertIsDisplayed()
appstore/src/test/java/com/vayunmathur/appstore/ui/SourcesPageTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/ui/SourcesPageTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/ui/SourcesPageTest.ktindex 4fcc1a3834..768f695a32 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/ui/SourcesPageTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/ui/SourcesPageTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.appstore.ui import android.app.Application import androidx.compose.ui.test.assertIsDisplayed 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.performScrollTo@@ -46,9 +47,18 @@ class SourcesPageTest {     // collecting its flows (see the matching fix in the ViewModel     // tests); the in-memory DB dies with the Robolectric sandbox. +    /** Waits for [text] to exist, idling the paused Robolectric main looper per poll: the first composition lands on it, and the compose rule's waits do not pump it. */+    private fun awaitNode(text: String) {+        rule.waitUntil(timeoutMillis = 15_000) {+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+            rule.onAllNodesWithText(text, substring = true).fetchSemanticsNodes().isNotEmpty()+        }+    }+     @Test     fun `all source cards and the auto install row render`() {         rule.setContent { SourcesPage(viewModel = vm) }+        awaitNode("Modern Apps")         rule.onNodeWithText("Modern Apps", substring = true).assertIsDisplayed()         rule.onNodeWithText("Automatically install updates", substring = true).assertIsDisplayed()         // The F-Droid card sits below the scroll fold on the Robolectric
appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentVmOpsW5Test.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentVmOpsW5Test.kt b/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentVmOpsW5Test.ktindex eb86ee835c..93b8544fba 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentVmOpsW5Test.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentVmOpsW5Test.kt@@ -152,6 +152,14 @@ class AccrescentVmOpsW5Test {         server.shutdownNow()     } +    // The settings edits below run on Dispatchers.IO, not the bare+    // runBlocking main thread: in the full suite this test class shares+    // the appstore-settings DataStore with every earlier ViewModel's+    // still-collecting SettingsRepository mirror, and an edit issued+    // from the paused Robolectric main thread could wait forever for+    // work queued on that same thread (phoneVmHomeBrowseAndPackageSelection+    // deadlocked at the edit in full-suite runs, while passing alone).+     private fun await(description: String, condition: () -> Boolean) {         val deadline = System.currentTimeMillis() + 30_000         while (System.currentTimeMillis() < deadline) {@@ -165,7 +173,7 @@ class AccrescentVmOpsW5Test {     @Test     fun phoneVmAccrescentSearchSelectAndUpdateCheck() {         val vm = AppStoreViewModel(app, db, accrescentOverride = repo)-        runBlocking { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }+        runBlocking(kotlinx.coroutines.Dispatchers.IO) { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) } // off the main thread (see below)         vm.setSearch("alpha")         await("accrescent search") {             vm.search.value.results.any { it.packageName == "app.alpha" }@@ -197,7 +205,7 @@ class AccrescentVmOpsW5Test {     fun tvVmAccrescentSearchSelectAndUpdateCheck() {         val vm = TvStoreViewModel(app, db, accrescentOverride = repo)         vm.enabledSourcesFlow.value = setOf(AppSource.ACCRESCENT, AppSource.MODERN_APPS)-        runBlocking { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }+        runBlocking(kotlinx.coroutines.Dispatchers.IO) { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }         vm.setSearch("beta")         await("tv accrescent search") {             vm.search.value.results.any { it.packageName == "app.beta" }@@ -228,7 +236,7 @@ class AccrescentVmOpsW5Test {     @Test     fun phoneVmHomeBrowseAndPackageSelection() {         val vm = AppStoreViewModel(app, db, accrescentOverride = repo)-        runBlocking { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }+        runBlocking(kotlinx.coroutines.Dispatchers.IO) { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }         vm.refresh()         await("home sections from accrescent") {             vm.home.value.sections.flatMap { it.apps }.any { it.packageName == "app.alpha" } ||@@ -253,7 +261,7 @@ class AccrescentVmOpsW5Test {     fun tvVmLibraryBrowseInstallAndSourceToggles() {         val vm = TvStoreViewModel(app, db, accrescentOverride = repo)         vm.enabledSourcesFlow.value = setOf(AppSource.ACCRESCENT, AppSource.MODERN_APPS)-        runBlocking { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }+        runBlocking(kotlinx.coroutines.Dispatchers.IO) { vm.settings.setSourceEnabled(AppSource.ACCRESCENT, true) }         vm.loadHome()         await("tv home for browse") { !vm.isLoadingFlow.value }         vm.home.value.categories.firstOrNull()?.let { vm.selectCategory(it) }