Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

test(live): make sports refresh tests deterministic

David Nashash21103e40eeb6
diff --git a/feature/live/src/main/java/com/streamvault/feature/live/sports/SportsHubRepository.kt b/feature/live/src/main/java/com/streamvault/feature/live/sports/SportsHubRepository.kt
index 884c7582..4e2231da 100644
--- a/feature/live/src/main/java/com/streamvault/feature/live/sports/SportsHubRepository.kt
+++ b/feature/live/src/main/java/com/streamvault/feature/live/sports/SportsHubRepository.kt
@@ -37,6 +37,7 @@ import java.time.ZoneId
import kotlinx.coroutines.async
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.currentCoroutineContext
@@ -82,6 +83,14 @@ internal fun shouldRefreshSportsOnClock(
dateChanged: Boolean,
): Boolean = range == SportsTimeRange.ON_NOW || hasLiveEvents || dateChanged
+/** Keep background work on the same scheduler as Main in repository tests. */
+internal class SportsHubDispatchers(
+ val default: CoroutineDispatcher,
+ val io: CoroutineDispatcher,
+) {
+ @Inject constructor() : this(Dispatchers.Default, Dispatchers.IO)
+}
+
@Singleton
@OptIn(ExperimentalCoroutinesApi::class)
internal class SportsHubRepository @Inject constructor(
@@ -104,6 +113,7 @@ internal class SportsHubRepository @Inject constructor(
private val sportsMetadataSourceSettings: SportsMetadataSourceSettings,
private val sportsProviderEventResolver: SportsProviderEventResolver,
applicationScope: CoroutineScope,
+ private val dispatchers: SportsHubDispatchers,
) {
private val scope = CoroutineScope(applicationScope.coroutineContext + Dispatchers.Main.immediate)
private val _uiState = MutableStateFlow(SportsHubUiState())
@@ -210,7 +220,7 @@ internal class SportsHubRepository @Inject constructor(
initialSnapshot = false
}
}
- }.distinctUntilChanged().flowOn(Dispatchers.Default)
+ }.distinctUntilChanged().flowOn(dispatchers.default)
}) { it.toList() }
}
}.distinctUntilChanged().retryWhen { _, _ ->
@@ -389,7 +399,7 @@ internal class SportsHubRepository @Inject constructor(
val cachedEvents = cacheKey?.let(::cachedEventsFor)
val (baseEvents, reusableProviderEvents) = cachedEvents?.let { cached ->
cached.events to cached.providerEvents
- } ?: withContext(Dispatchers.Default) {
+ } ?: withContext(dispatchers.default) {
val (requestFromMillis, requestUntilMillis) = providerEventRequestWindow(range, now, window)
availableChannelEntries = providerChannels.flatMap { provider ->
provider.channels.map { SportsChannelIndexEntry(it, provider.combinedProfileId) }
@@ -493,7 +503,7 @@ internal class SportsHubRepository @Inject constructor(
selectedEventProviderInstanceId,
)
val (requestFromMillis, requestUntilMillis) = providerEventRequestWindow(range, now, window)
- val scoreLoad = withContext(Dispatchers.Default) {
+ val scoreLoad = withContext(dispatchers.default) {
withTimeoutOrNull(ENRICHMENT_TIMEOUT_MS) { sportsProviderScoreSourceRouter.load(
providers = scoreProviders,
reusableEventLoad = reusableProviderEvents,
@@ -518,7 +528,7 @@ internal class SportsHubRepository @Inject constructor(
)
}
}
- val resolvedEvents = withContext(Dispatchers.Default) {
+ val resolvedEvents = withContext(dispatchers.default) {
val scoreResolutionStartedAt = SystemClock.elapsedRealtime()
val resolved = sportsProviderEventResolver.resolve(
epgEvents = baseEvents,
@@ -544,7 +554,7 @@ internal class SportsHubRepository @Inject constructor(
),
)
publishEvents(resolvedEvents, range, now, window)
- val metadata = withContext(Dispatchers.Default) {
+ val metadata = withContext(dispatchers.default) {
withTimeoutOrNull(ENRICHMENT_TIMEOUT_MS) { sportsProviderMetadataRouter.load(
configs = configuredProviders,
preference = inputs.metadataSourcePreference,
@@ -661,7 +671,7 @@ internal class SportsHubRepository @Inject constructor(
val providerSource = sourcedLeague?.source as? SportsSourceRef.Provider
val league = sourcedLeague?.value ?: event.league?.let(::ProviderLeague)
val providers = sportsProviderStandingsRouter.eligibleProviders(configs, preference, providerSource)
- return withContext(Dispatchers.Default) {
+ return withContext(dispatchers.default) {
sportsProviderStandingsRouter.load(
providers = providers,
eventLeague = league,
@@ -670,7 +680,7 @@ internal class SportsHubRepository @Inject constructor(
}
}
- suspend fun loadExtraDetails(event: SportsEvent): LoadedSportsEventExtraDetails? = withContext(Dispatchers.IO) {
+ suspend fun loadExtraDetails(event: SportsEvent): LoadedSportsEventExtraDetails? = withContext(dispatchers.io) {
sportsProviderExtraDetailsRouter.load(event)
}
@@ -803,7 +813,7 @@ internal class SportsHubRepository @Inject constructor(
suspend fun searchChannels(event: SportsEvent, query: String): List<SportsBroadcast> {
val entries = availableChannelEntries
if (entries.isEmpty()) return emptyList()
- return withContext(Dispatchers.Default) {
+ return withContext(dispatchers.default) {
val index = getFallbackChannelIndex(entries)
if (availableChannelEntries != entries) return@withContext emptyList()
SportsChannelFallbackMatcher.searchChannels(event, index, query).map { match ->
@@ -904,7 +914,7 @@ internal class SportsHubRepository @Inject constructor(
val classificationStartedAt = SystemClock.elapsedRealtime()
val cacheStatsBefore = classificationCache.stats()
- val candidates = withContext(Dispatchers.Default) {
+ val candidates = withContext(dispatchers.default) {
val coroutineContext = currentCoroutineContext()
buildList {
channels.forEach { channel ->
diff --git a/feature/live/src/test/java/com/streamvault/feature/live/sports/SportsHubViewModelTest.kt b/feature/live/src/test/java/com/streamvault/feature/live/sports/SportsHubViewModelTest.kt
index 51ee7720..1cd071d0 100644
--- a/feature/live/src/test/java/com/streamvault/feature/live/sports/SportsHubViewModelTest.kt
+++ b/feature/live/src/test/java/com/streamvault/feature/live/sports/SportsHubViewModelTest.kt
@@ -32,8 +32,10 @@ import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
-import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.TestCoroutineScheduler
+import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.setMain
@@ -97,6 +99,7 @@ class SportsHubViewModelTest {
private val providerEventResolver = SportsProviderEventResolver(InMemorySportsProviderEventLinkCache())
private val store = ViewModelStore()
private val scheduler = TestCoroutineScheduler()
+ private val dispatcher = StandardTestDispatcher(scheduler)
private lateinit var repositoryScope: CoroutineScope
private var modelCount = 0
private val subscriptions = AtomicInteger()
@@ -112,7 +115,7 @@ class SportsHubViewModelTest {
@Before
fun setUp() = runBlocking<Unit> {
- Dispatchers.setMain(UnconfinedTestDispatcher(scheduler))
+ Dispatchers.setMain(dispatcher)
repositoryScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
val provider = LegacyProvider(7L, "Test", ProviderType.XTREAM_CODES, "https://example.test")
whenever(providers.getActiveProvider()).thenReturn(flowOf(provider))
@@ -146,11 +149,12 @@ class SportsHubViewModelTest {
fun tearDown() {
store.clear()
repositoryScope.cancel()
+ scheduler.runCurrent()
Dispatchers.resetMain()
}
@Test
- fun `new ViewModel immediately retains results while repository refresh is blocked`() = runBlocking<Unit> {
+ fun `new ViewModel immediately retains results while repository refresh is blocked`() = sportsTest {
val repository = createRepository()
val first = createModel(repository)
await { !first.uiState.value.isLoading }
@@ -173,7 +177,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `expired retained event is removed before another screen observes`() = runBlocking {
+ fun `expired retained event is removed before another screen observes`() = sportsTest {
val now = System.currentTimeMillis()
whenever(epg.getResolvedProgramsForChannels(any(), any(), any(), any())).thenReturn(
mapOf("1" to listOf(Program(
@@ -186,7 +190,7 @@ class SportsHubViewModelTest {
await { !first.uiState.value.isLoading }
assertThat(first.uiState.value.events).hasSize(1)
store.clear()
- delay(1_100L)
+ withContext(Dispatchers.IO) { delay(1_100L) }
val second = SportsHubViewModel(repository)
@@ -194,7 +198,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `no EPG reloads while the repository has no screen observer`() = runBlocking {
+ fun `no EPG reloads while the repository has no screen observer`() = sportsTest {
val repository = createRepository()
val model = createModel(repository)
await { !model.uiState.value.isLoading }
@@ -207,7 +211,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `hidden category invalidates retained results before a new ViewModel observes`() = runBlocking {
+ fun `hidden category invalidates retained results before a new ViewModel observes`() = sportsTest {
val repository = createRepository()
val first = createModel(repository)
await { !first.uiState.value.isLoading }
@@ -222,7 +226,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `Today queries from current time while retaining an ongoing programme`() = runBlocking {
+ fun `Today queries from current time while retaining an ongoing programme`() = sportsTest {
val model = createModel()
await { !model.uiState.value.isLoading }
val before = System.currentTimeMillis()
@@ -236,7 +240,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `minute refresh reads current programmes without reloading channel catalog`() = runBlocking {
+ fun `minute refresh reads current programmes without reloading channel catalog`() = sportsTest {
val model = createModel()
await { !model.uiState.value.isLoading }
val firstReads = programmeReads.get()
@@ -249,7 +253,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `range changes reuse channels and hidden categories invalidate cached events`() = runBlocking {
+ fun `range changes reuse channels and hidden categories invalidate cached events`() = sportsTest {
val model = createModel()
await { !model.uiState.value.isLoading }
model.selectTimeRange(SportsTimeRange.TODAY)
@@ -265,7 +269,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `manual search and remembered choices work with deferred fallback indexing`() = runBlocking {
+ fun `manual search and remembered choices work with deferred fallback indexing`() = sportsTest {
val model = createModel()
await { !model.uiState.value.isLoading }
val event = model.uiState.value.events.single()
@@ -277,7 +281,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `changing provider replaces channel subscription and playback choices`() = runBlocking {
+ fun `changing provider replaces channel subscription and playback choices`() = sportsTest {
whenever(channels.getChannels(8L)).thenReturn(flowOf(listOf(channel.copy(providerId = 8L))))
whenever(preferences.getHiddenCategoryIds(8L, ContentType.LIVE)).thenReturn(flowOf(emptySet()))
val model = createModel()
@@ -291,7 +295,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `retry recovers a failed initial channel subscription`() = runBlocking {
+ fun `retry recovers a failed initial channel subscription`() = sportsTest {
val attempts = AtomicInteger()
whenever(channels.getChannels(7L)).thenReturn(flow {
if (attempts.incrementAndGet() == 1) error("Catalog temporarily unavailable")
@@ -308,7 +312,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `catalog changes replace cached playback choices`() = runBlocking {
+ fun `catalog changes replace cached playback choices`() = sportsTest {
val model = createModel()
await { !model.uiState.value.isLoading }
channelRows.value = listOf(channel.copy(name = "Renamed Sports", streamUrl = "https://example.test/new"))
@@ -321,7 +325,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `guide is published while provider discovery is pending`() = runBlocking<Unit> {
+ fun `guide is published while provider discovery is pending`() = sportsTest {
val gate = CompletableDeferred<Unit>()
val started = CompletableDeferred<Unit>()
val factory = enrichmentFactory(events = {
@@ -341,7 +345,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `API events appear while a long guide scan remains pending across clock ticks`() = runBlocking<Unit> {
+ fun `API events appear while a long guide scan remains pending across clock ticks`() = sportsTest {
val guideGate = CompletableDeferred<Map<String, List<Program>>>()
val guideReads = AtomicInteger()
whenever(epg.getResolvedProgramsForChannels(any(), any(), any(), any())).doSuspendableAnswer {
@@ -367,7 +371,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `saving provider configuration reloads the retained sports tab`() = runBlocking {
+ fun `saving provider configuration reloads the retained sports tab`() = sportsTest {
val calls = AtomicInteger()
val factory = enrichmentFactory(events = { calls.incrementAndGet(); emptyList() })
val registry = SportsProviderRegistry(listOf(factory), SportsProviderReleasePolicy(development = true))
@@ -383,7 +387,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `events remain usable while scores and metadata are pending`() = runBlocking<Unit> {
+ fun `events remain usable while scores and metadata are pending`() = sportsTest {
val scoresStarted = CompletableDeferred<Unit>()
val scoresGate = CompletableDeferred<Unit>()
val metadataStarted = CompletableDeferred<Unit>()
@@ -411,7 +415,7 @@ class SportsHubViewModelTest {
}
@Test
- fun `clock refresh never publishes unscored replacements for scored fixtures`() = runBlocking<Unit> {
+ fun `clock refresh never publishes unscored replacements for scored fixtures`() = sportsTest {
val eventStart = java.time.Instant.now().minusSeconds(60)
val score = ProviderScore("match", listOf(
ProviderCompetitorScore(ProviderCompetitor("Arsenal", "arsenal"), "2"),
@@ -447,6 +451,12 @@ class SportsHubViewModelTest {
assertThat(observed.all { state -> state.events.isNotEmpty() && state.events.all { it.provenance.score != null } }).isTrue()
}
+ private fun sportsTest(block: suspend TestScope.() -> Unit) = runTest(scheduler) {
+ repositoryScope.cancel()
+ repositoryScope = backgroundScope
+ block()
+ }
+
private fun enrichmentFactory(
events: suspend () -> List<ProviderEvent>,
scores: suspend () -> Unit = {},
@@ -495,17 +505,25 @@ class SportsHubViewModelTest {
metadataSourceSettings,
providerEventResolver,
repositoryScope,
+ SportsHubDispatchers(dispatcher, dispatcher),
)
private fun createModel(repository: SportsHubRepository = createRepository()) = SportsHubViewModel(repository)
.also { model ->
store.put("sports-${modelCount++}", model)
model.viewModelScope.launch { model.uiState.collect {} }
+ scheduler.runCurrent()
}
- private suspend fun await(condition: () -> Boolean) = withContext(Dispatchers.Default) {
+ private suspend fun await(condition: () -> Boolean) {
withTimeout(10_000L) {
- while (!condition()) delay(10L)
+ // Drain queued work before checking presentation state. isLoading can become
+ // false while enrichment is still in flight; it is not a load-completion signal.
+ scheduler.runCurrent()
+ while (!condition()) {
+ delay(10L)
+ scheduler.runCurrent()
+ }
}
}
}