youpipe: cancel ViewModel scope before closing test databases (#tests)
- Commit
2f728ac2031dd0a9928906f73adae920777b5623- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 18:25 UTC (20 days ago)
- Parents
b94b398
tvRecommendationsSettings, tvSearch and the playlist-delete test failed in CI with "connection pool is closed" / "Database is closed" SQLExceptions. The tests closed their in-memory Room database in tearDown while the ViewModel's WhileSubscribed flows and init IO work were still subscribed: those background queries then hit the closed database and the exception surfaced in a later test on the shared box. The ViewModel now goes through a ViewModelStore whose clear() cancels viewModelScope before db.close(), and the delete test awaits the back-stack pop (pumping the paused main looper) instead of asserting it synchronously. Reproduced under TZ=UTC with 4 workers in the combined module run; verified green in three consecutive combined runs plus a TZ=America/New_York spot check.
Diff
youpipe/src/test/java/com/vayunmathur/youpipe/tv/TvPagesTest.kt
diff --git a/youpipe/src/test/java/com/vayunmathur/youpipe/tv/TvPagesTest.kt b/youpipe/src/test/java/com/vayunmathur/youpipe/tv/TvPagesTest.ktindex 7a6b44acc7..966cbd8462 100644--- a/youpipe/src/test/java/com/vayunmathur/youpipe/tv/TvPagesTest.kt+++ b/youpipe/src/test/java/com/vayunmathur/youpipe/tv/TvPagesTest.kt@@ -55,6 +55,7 @@ class TvPagesTest { private lateinit var db: SubscriptionDatabase private lateinit var vm: YouPipeViewModel+ private val vmStore = androidx.lifecycle.ViewModelStore() private var musicPlaylistId: Long = -1 private var stack: NavBackStack<TvRoute>? = null @@ -67,12 +68,19 @@ class TvPagesTest { db = Room.inMemoryDatabaseBuilder(app, SubscriptionDatabase::class.java) .allowMainThreadQueries().build() vm = YouPipeViewModel(app, SubscriptionRepository(app, db))+ vmStore.put("vm", vm) runBlocking { seed() } } @AfterTest fun tearDown() { TvLoadedVideo.id = null+ // Cancel viewModelScope BEFORE closing the DB: the VM's+ // WhileSubscribed flows and init IO work otherwise query the+ // closed database from a background dispatcher, and Room's+ // "connection pool is closed" SQLException surfaces as an+ // uncaught exception in a later test on the shared CI box.+ vmStore.clear() db.close() }
youpipe/src/test/java/com/vayunmathur/youpipe/ui/SavedPlaylistMorePass3Test.kt
diff --git a/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SavedPlaylistMorePass3Test.kt b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SavedPlaylistMorePass3Test.ktindex bf33439309..a20a41e1fc 100644--- a/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SavedPlaylistMorePass3Test.kt+++ b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SavedPlaylistMorePass3Test.kt@@ -44,6 +44,7 @@ class SavedPlaylistMorePass3Test { private lateinit var db: SubscriptionDatabase private lateinit var vm: YouPipeViewModel+ private val vmStore = androidx.lifecycle.ViewModelStore() private lateinit var backStack: NavBackStack<Route> private var playlistId: Long = -1 @@ -55,12 +56,19 @@ class SavedPlaylistMorePass3Test { db = Room.inMemoryDatabaseBuilder(app, SubscriptionDatabase::class.java) .allowMainThreadQueries().build() vm = YouPipeViewModel(app, SubscriptionRepository(app, db))+ vmStore.put("vm", vm) backStack = NavBackStack(arrayOf<Route>(Route.Main(3))) runBlocking { playlistId = db.playlistDao().upsert(Playlist(name = "Road Trip")) } } @AfterTest- fun tearDown() = db.close()+ fun tearDown() {+ // Cancel viewModelScope before db.close(): background Room+ // queries against the closed DB ("Database is closed" /+ // connection-pool-closed) otherwise leak into later tests.+ vmStore.clear()+ db.close()+ } @Test fun savedPage_downloadsRowNavigatesAndUserPlaylistOpens() {@@ -94,9 +102,16 @@ class SavedPlaylistMorePass3Test { rule.waitUntilExactlyOneExists(hasText("Road Trip"), timeoutMillis = 10_000) rule.onNodeWithContentDescription("Delete").performClick() val remaining = runBlocking {- withTimeout(5_000) { vm.playlists.first { list -> list.none { it.id == playlistId } } }+ withTimeout(20_000) { vm.playlists.first { list -> list.none { it.id == playlistId } } } } assertTrue(remaining.none { it.id == playlistId })+ // The pop follows the delete on the main thread; under load it can+ // land after the flow emission above, so await it (pumping+ // Robolectric's paused main looper) instead of asserting at once.+ rule.waitUntil(15_000) {+ org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+ stack.backStack.lastOrNull() == Route.Main(3)+ } assertEquals(Route.Main(3), stack.backStack.last()) }