StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.
Kotlin iptv-player
test: add tests for bulk live stream handling in Stalker sync
David Nashash936481128c3d
diff --git a/data/src/main/java/com/streamvault/data/sync/SyncManager.kt b/data/src/main/java/com/streamvault/data/sync/SyncManager.ktindex fbd9bd1d..3853f0f5 100644--- a/data/src/main/java/com/streamvault/data/sync/SyncManager.kt+++ b/data/src/main/java/com/streamvault/data/sync/SyncManager.kt@@ -90,6 +90,8 @@ import kotlinx.coroutines.CancellationExceptionimport kotlinx.coroutines.awaitAllimport kotlinx.coroutines.coroutineScopeimport kotlinx.coroutines.delay+import kotlinx.coroutines.isActive+import kotlinx.coroutines.launchimport kotlinx.coroutines.flow.Flowimport kotlinx.coroutines.flow.StateFlowimport kotlinx.coroutines.flow.first@@ -109,6 +111,7 @@ import java.net.URIimport java.security.MessageDigestimport java.util.ArrayDequeimport java.util.concurrent.ConcurrentHashMap+import java.util.concurrent.atomic.AtomicLongimport java.util.zip.GZIPInputStreamimport javax.inject.Injectimport javax.inject.Singleton@@ -123,9 +126,20 @@ private const val STALKER_CATEGORY_RETRY_BUDGET = 3private const val STALKER_CATEGORY_RETRY_COOLDOWN_MILLIS = 5 * 60 * 1000Lprivate const val STALKER_RUNNING_JOB_STALE_MILLIS = 15 * 60 * 1000Lprivate const val STALKER_MIN_HEALTHY_EPG_PROGRAMS = 3-// Bulk live is an optimization. Bound the complete attempt so a portal that keeps-// a response alive without completing cannot block the per-category recovery path.-private const val STALKER_BULK_LIVE_REQUEST_TIMEOUT_MILLIS = 15_000L+// Bulk live is an optimization. Large portals stream tens of MB over a single+// response and can spend 8s+ generating it before the first byte, so a fixed+// wall-clock cap kills healthy transfers on slow links. Instead cancel only+// when the stream stalls (no items for the stall window) and keep a generous+// overall cap so a response that never completes cannot block the+// per-category recovery path forever.+private const val STALKER_BULK_LIVE_STALL_TIMEOUT_MILLIS = 30_000L+private const val STALKER_BULK_LIVE_OVERALL_TIMEOUT_MILLIS = 5 * 60 * 1000L+private const val STALKER_BULK_LIVE_STALL_CHECK_INTERVAL_MILLIS = 1_000L+// A negative bulk-live verdict expires far sooner than the 7-day validated+// portal state: re-probing costs one fast request, while a stale false verdict+// forces the very slow per-category path on every sync. Portals that recorded+// "unsupported" under the old fixed-timeout logic self-heal through this.+private const val STALKER_BULK_LIVE_UNSUPPORTED_TTL_MILLIS = 6 * 60 * 60 * 1000Lprivate const val LIVE_CATEGORY_SEQUENTIAL_MODE_WARNING ="Live category sync downgraded to sequential mode after provider stress signals."private const val XTREAM_RECOVERY_ABORT_WARNING_SUFFIX =@@ -5825,15 +5839,22 @@ class SyncManager @Inject constructor(try {var bulkFailure: Exception? = nullval learnedState = api.validatedPortalState()+ val bulkUnsupportedVerdictFresh = learnedState?.bulkLiveSupported == false &&+ learnedState.validatedAt > 0L &&+ System.currentTimeMillis() - learnedState.validatedAt <= STALKER_BULK_LIVE_UNSUPPORTED_TTL_MILLISval shouldTryBulk = (- learnedState?.bulkLiveSupported != false &&+ !bulkUnsupportedVerdictFresh &&learnedState?.bulkLiveCategoryFidelity != false) || preferredCategories.isNullOrEmpty()if (shouldTryBulk) {StalkerTelemetry.strategySelected(provider.id,"BULK_LIVE",- if (learnedState?.bulkLiveSupported == true) "VALIDATED_CACHE" else "CAPABILITY_PROBE"+ when (learnedState?.bulkLiveSupported) {+ true -> "VALIDATED_CACHE"+ false -> "STALE_UNSUPPORTED_REPROBE"+ else -> "CAPABILITY_PROBE"+ })} else {StalkerTelemetry.strategySelected(@@ -5847,8 +5868,9 @@ class SyncManager @Inject constructor()}val streamResult = if (shouldTryBulk) {- withTimeoutOrNull(STALKER_BULK_LIVE_REQUEST_TIMEOUT_MILLIS) {+ withStalkerBulkLiveStallTimeout { markBulkProgress ->api.streamLiveStreams { channel ->+ markBulkProgress()if (channel.categoryId != null && channel.categoryId in hiddenLiveCategoryIds) {return@streamLiveStreams}@@ -5877,7 +5899,14 @@ class SyncManager @Inject constructor(return finalizeStagedImport()}is com.streamvault.domain.model.Result.Error -> {- api.recordBulkLiveCapability(supported = false)+ // Only definitive portal verdicts (auth denial, malformed payload,+ // blocked action) prove bulk is unsupported. Transport failures and+ // 5xx/rate-limit responses are transient: keep bulk eligible so the+ // next sync retries it instead of locking in the slow per-category+ // path for the validation TTL.+ if (isDefinitiveBulkLiveFailure(streamResult.exception)) {+ api.recordBulkLiveCapability(supported = false)+ }val profileDiagnostic = stalkerCatalogAccessDiagnostic(api = api,primaryMessage = categoriesErrorMessage.orEmpty(),@@ -5896,7 +5925,9 @@ class SyncManager @Inject constructor(}is com.streamvault.domain.model.Result.Loading -> throw IllegalStateException("Unexpected loading state")null -> if (shouldTryBulk) {- api.recordBulkLiveCapability(supported = false)+ // A stalled/timed-out bulk transfer reflects link throughput, not+ // portal capability. Leave bulkLiveSupported unset so the next sync+ // retries the bulk path.bulkFailure = IOException("Bulk live request timed out; switching to category requests.")progress(provider.id, onProgress, "Bulk live request is slow; loading categories instead...")}@@ -5963,6 +5994,65 @@ class SyncManager @Inject constructor(}}+ /**+ * Runs the bulk live stream with a stall watchdog instead of a fixed wall-clock+ * cap. Large portals stream tens of MB in one response, so the attempt is+ * cancelled only when no items arrive for [STALKER_BULK_LIVE_STALL_TIMEOUT_MILLIS];+ * [STALKER_BULK_LIVE_OVERALL_TIMEOUT_MILLIS] remains as a final bound so a response+ * that never completes cannot block the per-category recovery path forever.+ * Returns null when the attempt ended by stall/overall timeout, mirroring the+ * previous withTimeoutOrNull contract.+ */+ private suspend fun <T> withStalkerBulkLiveStallTimeout(+ block: suspend (markProgress: () -> Unit) -> T+ ): T? = coroutineScope {+ val lastProgressAt = AtomicLong(System.currentTimeMillis())+ val streamDeferred = async {+ block { lastProgressAt.set(System.currentTimeMillis()) }+ }+ val watchdog = launch {+ while (isActive) {+ delay(STALKER_BULK_LIVE_STALL_CHECK_INTERVAL_MILLIS)+ val stalledFor = System.currentTimeMillis() - lastProgressAt.get()+ if (stalledFor >= STALKER_BULK_LIVE_STALL_TIMEOUT_MILLIS) {+ streamDeferred.cancel()+ break+ }+ }+ }+ try {+ withTimeoutOrNull(STALKER_BULK_LIVE_OVERALL_TIMEOUT_MILLIS) {+ try {+ streamDeferred.await()+ } catch (cancelled: CancellationException) {+ // Propagate cancellation of the outer scope (worker stopped or the+ // overall timeout fired); only the watchdog's stall-cancel maps to null.+ if (!isActive) throw cancelled+ null+ }+ }+ } finally {+ watchdog.cancel()+ streamDeferred.cancel()+ }+ }++ /**+ * Only definitive portal verdicts prove the bulk live action is unsupported.+ * Transport failures, consent challenges, rate limits and 5xx responses are+ * transient conditions and must not downgrade the learned portal state.+ */+ private fun isDefinitiveBulkLiveFailure(error: Throwable?): Boolean = when (error) {+ is StalkerApiError.Authorization,+ is StalkerApiError.ModelRejected,+ is StalkerApiError.AccountBlocked,+ is StalkerApiError.Malformed,+ is StalkerApiError.ResponseTooLarge,+ is StalkerApiError.CatalogTruncated,+ is StalkerApiError.BlockedOrConfiguration -> true+ else -> false+ }+private suspend fun loadStalkerLiveCatalog(api: StalkerProvider,provider: Provider,diff --git a/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt b/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.ktindex fd6ce147..ea69ae44 100644--- a/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt+++ b/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt@@ -2675,6 +2675,179 @@ class SyncManagerTest {verify(stalkerApiService).getLiveStreams(any(), any(), eq("10"))}+ @Test+ fun sync_stalker_does_not_persist_bulk_unsupported_after_transient_bulk_failure() = runTest {+ val providerEntity = sampleProvider(ProviderType.STALKER_PORTAL).copy(+ serverUrl = "http://example.com",+ username = "",+ password = "",+ stalkerMacAddress = "00:11:22:33:44:55",+ epgSyncMode = ProviderEpgSyncMode.SKIP+ )+ val portalStateStore = mock<com.streamvault.data.remote.stalker.StalkerPortalStateStore>()+ org.mockito.kotlin.whenever(portalStateStore.getValidated(eq(1L), any())).thenReturn(null)+ val manager = buildManager(+ providerType = ProviderType.STALKER_PORTAL,+ providerEntity = providerEntity,+ portalStateStore = portalStateStore+ )++ org.mockito.kotlin.whenever(stalkerApiService.authenticate(any())).thenReturn(+ Result.success(+ StalkerSession(+ loadUrl = "http://example.com/stalker_portal/server/load.php",+ portalReferer = "http://example.com/stalker_portal/c/",+ token = "token"+ ) to StalkerProviderProfile(accountName = "Stalker")+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getLiveCategories(any(), any())).thenReturn(+ Result.success(listOf(StalkerCategoryRecord(id = "10", name = "News")))+ )+ // A bulk transfer cut short by a slow link surfaces as a generic transport-style+ // failure; it must not downgrade the learned portal capability.+ stalkerApiService.stubStreamLiveStreamsError("Bulk live request timed out")+ org.mockito.kotlin.whenever(stalkerApiService.getLiveStreams(any(), any(), eq("10"))).thenReturn(+ Result.success(+ listOf(+ StalkerItemRecord(+ id = "100",+ name = "News",+ categoryId = "10",+ cmd = "ffmpeg http://example.com/live.ts"+ )+ )+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getVodCategories(any(), any())).thenReturn(Result.success(emptyList()))+ org.mockito.kotlin.whenever(stalkerApiService.getSeriesCategories(any(), any())).thenReturn(Result.success(emptyList()))++ val result = manager.sync(providerId = 1L, force = false)++ assertThat(result).isInstanceOf(Result.Success::class.java)+ verify(stalkerApiService).streamLiveStreams(any(), any(), any())+ verify(stalkerApiService).getLiveStreams(any(), any(), eq("10"))+ verify(portalStateStore, org.mockito.kotlin.times(0)).recordBulkLive(eq(1L), eq(false), anyOrNull(), any())+ }++ @Test+ fun sync_stalker_persists_bulk_unsupported_after_definitive_bulk_failure() = runTest {+ val providerEntity = sampleProvider(ProviderType.STALKER_PORTAL).copy(+ serverUrl = "http://example.com",+ username = "",+ password = "",+ stalkerMacAddress = "00:11:22:33:44:55",+ epgSyncMode = ProviderEpgSyncMode.SKIP+ )+ val portalStateStore = mock<com.streamvault.data.remote.stalker.StalkerPortalStateStore>()+ org.mockito.kotlin.whenever(portalStateStore.getValidated(eq(1L), any())).thenReturn(null)+ val manager = buildManager(+ providerType = ProviderType.STALKER_PORTAL,+ providerEntity = providerEntity,+ portalStateStore = portalStateStore+ )++ org.mockito.kotlin.whenever(stalkerApiService.authenticate(any())).thenReturn(+ Result.success(+ StalkerSession(+ loadUrl = "http://example.com/stalker_portal/server/load.php",+ portalReferer = "http://example.com/stalker_portal/c/",+ token = "token"+ ) to StalkerProviderProfile(accountName = "Stalker")+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getLiveCategories(any(), any())).thenReturn(+ Result.success(listOf(StalkerCategoryRecord(id = "10", name = "News")))+ )+ org.mockito.kotlin.whenever(stalkerApiService.streamLiveStreams(any(), any(), any())).thenReturn(+ Result.error(+ "Portal authorization failed with HTTP 403.",+ com.streamvault.data.remote.stalker.StalkerApiError.Authorization(+ message = "Portal authorization failed with HTTP 403.",+ httpStatus = 403+ )+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getLiveStreams(any(), any(), eq("10"))).thenReturn(+ Result.success(+ listOf(+ StalkerItemRecord(+ id = "100",+ name = "News",+ categoryId = "10",+ cmd = "ffmpeg http://example.com/live.ts"+ )+ )+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getVodCategories(any(), any())).thenReturn(Result.success(emptyList()))+ org.mockito.kotlin.whenever(stalkerApiService.getSeriesCategories(any(), any())).thenReturn(Result.success(emptyList()))++ val result = manager.sync(providerId = 1L, force = false)++ assertThat(result).isInstanceOf(Result.Success::class.java)+ verify(portalStateStore).recordBulkLive(eq(1L), eq(false), anyOrNull(), any())+ }++ @Test+ fun sync_stalker_reprobes_bulk_live_when_unsupported_verdict_is_stale() = runTest {+ val providerEntity = sampleProvider(ProviderType.STALKER_PORTAL).copy(+ serverUrl = "http://example.com",+ username = "",+ password = "",+ stalkerMacAddress = "00:11:22:33:44:55",+ epgSyncMode = ProviderEpgSyncMode.SKIP+ )+ val portalStateStore = mock<com.streamvault.data.remote.stalker.StalkerPortalStateStore>()+ // A negative verdict recorded under the old fixed-timeout logic must expire+ // long before the 7-day validated-state TTL, otherwise affected portals keep+ // using the slow per-category path for a week.+ org.mockito.kotlin.whenever(portalStateStore.getValidated(eq(1L), any())).thenReturn(+ StalkerPortalStateEntity(+ providerId = 1L,+ bulkLiveSupported = false,+ validatedAt = System.currentTimeMillis() - (7L * 60L * 60L * 1000L)+ )+ )+ val manager = buildManager(+ providerType = ProviderType.STALKER_PORTAL,+ providerEntity = providerEntity,+ portalStateStore = portalStateStore+ )++ org.mockito.kotlin.whenever(stalkerApiService.authenticate(any())).thenReturn(+ Result.success(+ StalkerSession(+ loadUrl = "http://example.com/stalker_portal/server/load.php",+ portalReferer = "http://example.com/stalker_portal/c/",+ token = "token"+ ) to StalkerProviderProfile(accountName = "Stalker")+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getLiveCategories(any(), any())).thenReturn(+ Result.success(listOf(StalkerCategoryRecord(id = "10", name = "News")))+ )+ stalkerApiService.stubStreamLiveStreams(+ listOf(+ StalkerItemRecord(+ id = "100",+ name = "News",+ categoryId = "10",+ cmd = "ffmpeg http://example.com/live.ts"+ )+ )+ )+ org.mockito.kotlin.whenever(stalkerApiService.getVodCategories(any(), any())).thenReturn(Result.success(emptyList()))+ org.mockito.kotlin.whenever(stalkerApiService.getSeriesCategories(any(), any())).thenReturn(Result.success(emptyList()))++ val result = manager.sync(providerId = 1L, force = false)++ assertThat(result).isInstanceOf(Result.Success::class.java)+ verify(stalkerApiService).streamLiveStreams(any(), any(), any())+ verify(stalkerApiService, org.mockito.kotlin.times(0)).getLiveStreams(any(), any(), eq("10"))+ verify(portalStateStore).recordBulkLive(eq(1L), eq(true), anyOrNull(), any())+ }+@Testfun sync_stalker_skips_known_unsupported_epg_and_finishes_ready_with_warning() = runTest {val providerEntity = sampleProvider(ProviderType.STALKER_PORTAL).copy(