Skip to content

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

Refactor StalkerPortalStateStore tests for concurrency and pacing logic

David Nashash054a12e6e788
diff --git a/app/src/main/java/com/streamvault/app/di/NetworkModule.kt b/app/src/main/java/com/streamvault/app/di/NetworkModule.kt
index 2acb86ea..d42c994e 100644
--- a/app/src/main/java/com/streamvault/app/di/NetworkModule.kt
+++ b/app/src/main/java/com/streamvault/app/di/NetworkModule.kt
@@ -15,6 +15,7 @@ import com.streamvault.data.remote.http.buildAppUserAgent
import com.streamvault.data.remote.http.buildOpenSubtitlesUserAgent
import com.streamvault.data.remote.stalker.OkHttpStalkerApiService
import com.streamvault.data.remote.stalker.StalkerApiService
+import com.streamvault.data.remote.stalker.StalkerRequestCoordinator
import com.streamvault.data.remote.xtream.XtreamApiService
import com.streamvault.data.remote.xtream.OkHttpXtreamApiService
import com.streamvault.data.remote.xtream.XtreamUrlFactory
@@ -129,8 +130,12 @@ object NetworkModule {
@Provides
@Singleton
- fun provideStalkerApiService(okHttpClient: OkHttpClient, xtreamJson: Json): StalkerApiService =
- OkHttpStalkerApiService(okHttpClient, xtreamJson)
+ fun provideStalkerApiService(
+ okHttpClient: OkHttpClient,
+ xtreamJson: Json,
+ requestCoordinator: StalkerRequestCoordinator
+ ): StalkerApiService =
+ OkHttpStalkerApiService(okHttpClient, xtreamJson, requestCoordinator = requestCoordinator)
@Provides
@Singleton
diff --git a/app/src/test/java/com/streamvault/app/di/NetworkModuleStalkerTest.kt b/app/src/test/java/com/streamvault/app/di/NetworkModuleStalkerTest.kt
new file mode 100644
index 00000000..b07c6135
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/di/NetworkModuleStalkerTest.kt
@@ -0,0 +1,168 @@
+package com.streamvault.app.di
+
+import android.app.Application
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.data.remote.stalker.StalkerApiError
+import com.streamvault.data.remote.stalker.StalkerNetworkPriority
+import com.streamvault.data.remote.stalker.StalkerProvider
+import com.streamvault.data.remote.stalker.StalkerRequestCoordinator
+import com.streamvault.data.remote.stalker.StalkerRequestDescriptor
+import com.streamvault.data.remote.stalker.StalkerSession
+import com.streamvault.data.remote.stalker.StalkerDeviceProfile
+import com.streamvault.domain.model.Result
+import com.streamvault.domain.model.StalkerRequestPriority
+import com.streamvault.domain.model.StalkerAuthMode
+import com.streamvault.domain.model.StalkerMagPreset
+import com.streamvault.domain.model.StalkerPortalFingerprint
+import com.streamvault.domain.model.StalkerBootstrapRecipe
+import com.streamvault.domain.model.StalkerEndpointPreference
+import com.streamvault.domain.model.StalkerCookieMode
+import com.streamvault.domain.model.StalkerPlaybackBackendHint
+import com.streamvault.domain.model.StalkerTransportGrant
+import com.streamvault.domain.model.StalkerTransportMode
+import com.streamvault.domain.model.StalkerTransportOrigin
+import java.util.concurrent.atomic.AtomicInteger
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.runBlocking
+import kotlinx.serialization.json.Json
+import okhttp3.MediaType.Companion.toMediaType
+import okhttp3.OkHttpClient
+import okhttp3.Protocol
+import okhttp3.Response
+import okhttp3.ResponseBody.Companion.toResponseBody
+import okhttp3.mockwebserver.Dispatcher
+import okhttp3.mockwebserver.MockResponse
+import okhttp3.mockwebserver.MockWebServer
+import okhttp3.mockwebserver.RecordedRequest
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.annotation.Config
+
+@RunWith(RobolectricTestRunner::class)
+@Config(application = Application::class, manifest = Config.NONE, sdk = [35])
+class NetworkModuleStalkerTest {
+ @Test
+ fun appFactoryUsesSharedAdaptivePoolForMovieAndSeriesHttpRequests() = runBlocking {
+ org.junit.Assume.assumeTrue(com.streamvault.data.BuildConfig.STALKER_ADAPTIVE_INDEXING)
+ org.junit.Assume.assumeTrue(com.streamvault.data.BuildConfig.STALKER_INDEX_PAGE_CONCURRENCY == 8)
+ val active = AtomicInteger()
+ val maximum = AtomicInteger()
+ MockWebServer().use { server ->
+ server.dispatcher = object : Dispatcher() {
+ override fun dispatch(request: RecordedRequest): MockResponse {
+ val count = active.incrementAndGet()
+ maximum.updateAndGet { maxOf(it, count) }
+ try { Thread.sleep(150L) } finally { active.decrementAndGet() }
+ return MockResponse().setBody("""{"js":{"total_items":2240,"max_page_items":14,"data":[{"id":"1","name":"Item","category_id":"42"}]}}""")
+ }
+ }
+ server.start()
+ val coordinator = StalkerRequestCoordinator(null)
+ val client = NetworkModule.provideOkHttpClient(org.robolectric.RuntimeEnvironment.getApplication())
+ val service = NetworkModule.provideStalkerApiService(client, Json { ignoreUnknownKeys = true }, coordinator)
+ val profile = StalkerDeviceProfile(
+ portalUrl = server.url("/c/").toString(), macAddress = "00:1A:79:12:34:56",
+ deviceProfile = "MAG250", timezone = "UTC", locale = "en", providerId = 993L,
+ authMode = StalkerAuthMode.AUTO, magPreset = StalkerMagPreset.GENERIC_SAFE,
+ portalFingerprint = StalkerPortalFingerprint.BASIC_MAC,
+ bootstrapRecipe = StalkerBootstrapRecipe.GENERIC_SAFE,
+ endpointPreference = StalkerEndpointPreference.AUTO, cookieMode = StalkerCookieMode.NONE,
+ playbackBackendHint = StalkerPlaybackBackendHint.AUTO,
+ username = "", password = "", serialNumber = "fixture", deviceId = "fixture",
+ deviceId2 = "fixture", signature = "fixture", userAgent = "fixture",
+ playerUserAgent = "fixture", xUserAgent = "Model: MAG250; Link: WiFi",
+ transportGrant = StalkerTransportGrant(StalkerTransportMode.USER_ACCEPTED_HTTP,
+ StalkerTransportOrigin("http", server.hostName, server.port), consentedAt = 1L)
+ )
+ val session = StalkerSession(loadUrl = server.url("/server/load.php").toString(),
+ portalReferer = server.url("/c/").toString(), token = "fixture")
+ val started = System.nanoTime()
+ (1..160).map { page -> async {
+ val movies = page % 2 == 0
+ coordinator.execute(993L, StalkerRequestPriority.BACKGROUND_INDEX,
+ StalkerRequestDescriptor(if (movies) "MOVIE" else "SERIES", "INDEX_PAGE", page = page)) {
+ val result = if (movies) service.getVodStreamsPage(session, profile, "42", page, null, "added")
+ else service.getSeriesPage(session, profile, "42", page, "added")
+ assertThat(result).isInstanceOf(Result.Success::class.java)
+ }
+ } }.awaitAll()
+ val elapsedMillis = (System.nanoTime() - started) / 1_000_000L
+ assertThat(server.requestCount).isEqualTo(160)
+ assertThat(maximum.get()).isAtLeast(7)
+ assertThat(maximum.get()).isAtMost(8)
+ println("Stalker app transport fixture: requests=160 responseDelayMillis=150 elapsedMillis=$elapsedMillis maxConcurrent=${maximum.get()}")
+ }
+ }
+
+ @Test
+ fun metadataCooldownAlsoBlocksAppTransportBeforeHttp() = runTest {
+ val coordinator = StalkerRequestCoordinator()
+ coordinator.recordFailure(991L, StalkerApiError.RateLimited(retryAfterMillis = 120_000L))
+ val requests = AtomicInteger()
+ val provider = provider(991L, coordinator, requests, status = 200)
+
+ val result = provider.authenticate()
+
+ assertThat(result).isInstanceOf(Result.Error::class.java)
+ assertThat(requests.get()).isEqualTo(0)
+ }
+
+ @Test
+ fun appTransportRateLimitClosesTheSharedMetadataCircuit() = runTest {
+ val coordinator = StalkerRequestCoordinator()
+ val requests = AtomicInteger()
+ val provider = provider(992L, coordinator, requests, status = 429)
+
+ assertThat(provider.authenticate()).isInstanceOf(Result.Error::class.java)
+ val rejected = runCatching {
+ coordinator.acquireNetworkPermit(992L, StalkerNetworkPriority.INTERACTIVE)
+ }.exceptionOrNull()
+
+ assertThat(rejected).isInstanceOf(StalkerApiError.RateLimited::class.java)
+ assertThat(requests.get()).isEqualTo(1)
+ }
+
+ @Test
+ fun serverRetryAfterAlsoClosesTheSharedCircuit() = runTest {
+ val coordinator = StalkerRequestCoordinator()
+ val requests = AtomicInteger()
+ val provider = provider(994L, coordinator, requests, status = 503)
+ assertThat(provider.authenticate()).isInstanceOf(Result.Error::class.java)
+ val rejected = runCatching {
+ coordinator.acquireNetworkPermit(994L, StalkerNetworkPriority.INTERACTIVE)
+ }.exceptionOrNull()
+ assertThat(rejected).isInstanceOf(StalkerApiError.RateLimited::class.java)
+ assertThat(requests.get()).isEqualTo(1)
+ }
+
+ private fun provider(
+ providerId: Long,
+ coordinator: StalkerRequestCoordinator,
+ requests: AtomicInteger,
+ status: Int
+ ): StalkerProvider {
+ val client = OkHttpClient.Builder().addInterceptor { chain ->
+ requests.incrementAndGet()
+ Response.Builder()
+ .request(chain.request())
+ .protocol(Protocol.HTTP_1_1)
+ .code(status)
+ .message(if (status == 429) "Too Many Requests" else "OK")
+ .header("Retry-After", "120")
+ .body("{}".toResponseBody("application/json".toMediaType()))
+ .build()
+ }.build()
+ return StalkerProvider(
+ providerId = providerId,
+ api = NetworkModule.provideStalkerApiService(client, Json { ignoreUnknownKeys = true }, coordinator),
+ portalUrl = "https://portal-$providerId.invalid/c/",
+ macAddress = "00:1A:79:12:34:56",
+ deviceProfile = "MAG250",
+ timezone = "UTC",
+ locale = "en"
+ )
+ }
+}
diff --git a/data/build.gradle.kts b/data/build.gradle.kts
index 315b9ba5..2fc45e82 100644
--- a/data/build.gradle.kts
+++ b/data/build.gradle.kts
@@ -19,8 +19,8 @@ android {
val requestedStalkerPageConcurrency = providers.gradleProperty("stalkerIndexPageConcurrency")
.orNull
?.toIntOrNull()
- ?.coerceIn(1, 2)
- ?: 1
+ ?.coerceIn(1, 8)
+ ?: 8
buildConfigField("int", "STALKER_INDEX_PAGE_CONCURRENCY", requestedStalkerPageConcurrency.toString())
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiService.kt b/data/src/main/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiService.kt
index 8d6164bb..4187a724 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiService.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiService.kt
@@ -916,6 +916,14 @@ class OkHttpStalkerApiService @Inject constructor(
profile: StalkerDeviceProfile,
categoryId: String?,
page: Int
+ ): Result<StalkerPagedItems> = getSeriesPage(session, profile, categoryId, page, sortBy = null)
+
+ override suspend fun getSeriesPage(
+ session: StalkerSession,
+ profile: StalkerDeviceProfile,
+ categoryId: String?,
+ page: Int,
+ sortBy: String?
): Result<StalkerPagedItems> = runApiCall("Failed to load series") {
fetchPagedItemPage(
session = session,
@@ -926,6 +934,7 @@ class OkHttpStalkerApiService @Inject constructor(
put("action", "get_ordered_list")
put("JsHttpRequest", "1-xml")
categoryId?.takeIf { it.isNotBlank() }?.let { put("category", it) }
+ sortBy?.takeIf { it.isNotBlank() }?.let { put("sortby", it) }
}
)
}
@@ -2853,7 +2862,7 @@ class OkHttpStalkerApiService @Inject constructor(
else -> StalkerNetworkPriority.FOREGROUND
}
val permit = requestCoordinator.acquireNetworkPermit(profile.providerId, requestPriority)
- var nonRateLimitedResponseObserved = false
+ var successfulResponseObserved = false
return try {
val baseClient = stalkerHttpClientFor(profile, request.url.toString())
val remainingMillis = profile.discoveryRuntime.remainingMillis()
@@ -2865,8 +2874,8 @@ class OkHttpStalkerApiService @Inject constructor(
.build()
}
withCancellableStalkerCall(client.newCall(request)) { response ->
- if (response.code != 429) {
- nonRateLimitedResponseObserved = true
+ if (response.isSuccessful) {
+ successfulResponseObserved = true
requestCoordinator.recordNetworkSuccess(permit)
}
block(response)
@@ -2875,7 +2884,7 @@ class OkHttpStalkerApiService @Inject constructor(
throw error
} catch (error: Throwable) {
requestCoordinator.recordIndexResponse(permit, error)
- if (!nonRateLimitedResponseObserved) {
+ if (!successfulResponseObserved) {
requestCoordinator.recordNetworkFailure(permit, error)
}
if (error is StalkerApiError.TransportConsentRequired) throw error
@@ -3132,7 +3141,8 @@ class OkHttpStalkerApiService @Inject constructor(
)
in 500..599 -> StalkerApiError.Server(
message = "Portal request failed with HTTP $code.",
- httpStatus = code
+ httpStatus = code,
+ retryAfterMillis = retryAfterMillis
)
else -> StalkerApiError.BlockedOrConfiguration(
message = "Portal request failed with HTTP $code."
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicy.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicy.kt
index d9f8fb89..9db9cc13 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicy.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicy.kt
@@ -1,53 +1,40 @@
package com.streamvault.data.remote.stalker
-/** Pure, provider-owned pacing state. Admission, not parallelism, controls the wire rate. */
-internal class StalkerAdaptiveIndexPolicy(
- intervalMillis: Long = INITIAL_INTERVAL_MILLIS,
- minimumIntervalMillis: Long = MIN_INTERVAL_MILLIS
-) {
- var minimumIntervalMillis = minimumIntervalMillis.coerceIn(MIN_INTERVAL_MILLIS, MAX_INTERVAL_MILLIS)
- private set
- var intervalMillis = intervalMillis.coerceIn(this.minimumIntervalMillis, MAX_INTERVAL_MILLIS)
+/** Provider-owned adaptive window shared by movie and series page requests. */
+internal class StalkerAdaptiveIndexPolicy(concurrency: Int = INITIAL_CONCURRENCY) {
+ var concurrency = concurrency.coerceIn(MIN_CONCURRENCY, MAX_CONCURRENCY)
private set
private var healthyResponses = 0
- private var healthySince: Long? = null
- fun success(now: Long, durationMillis: Long, playbackActive: Boolean): Boolean {
+ fun success(playbackActive: Boolean): Boolean {
if (playbackActive) {
resetHealth()
return false
}
- if (durationMillis >= SLOW_RESPONSE_MILLIS) return stress()
- if (healthySince == null) healthySince = now
- healthyResponses++
- if (healthyResponses < HEALTHY_RESPONSES || now - requireNotNull(healthySince) < HEALTHY_WINDOW_MILLIS) return false
- val previous = intervalMillis
- intervalMillis = (intervalMillis * 3 / 4).coerceAtLeast(minimumIntervalMillis)
+ if (++healthyResponses < HEALTHY_RESPONSES) return false
resetHealth()
- return intervalMillis != previous
+ val previous = concurrency
+ concurrency = (concurrency + 1).coerceAtMost(MAX_CONCURRENCY)
+ return concurrency != previous
}
- /** Remember the failed rate as a ceiling, including across process restarts. */
+ /** In-flight requests finish; new admissions observe the smaller window immediately. */
fun stress(): Boolean {
- val previous = intervalMillis
- minimumIntervalMillis = (intervalMillis * 2).coerceAtMost(MAX_INTERVAL_MILLIS)
- intervalMillis = minimumIntervalMillis
resetHealth()
- return intervalMillis != previous
+ val previous = concurrency
+ concurrency = (concurrency / 2).coerceAtLeast(MIN_CONCURRENCY)
+ return concurrency != previous
}
fun resetHealth() {
healthyResponses = 0
- healthySince = null
}
companion object {
- const val INITIAL_INTERVAL_MILLIS = 5_000L
- const val MIN_INTERVAL_MILLIS = 500L
- const val MAX_INTERVAL_MILLIS = 30_000L
+ const val INITIAL_CONCURRENCY = 3
+ const val MIN_CONCURRENCY = 1
+ const val MAX_CONCURRENCY = 8
const val PLAYBACK_INTERVAL_MILLIS = 20_000L
- const val HEALTHY_RESPONSES = 12
- const val HEALTHY_WINDOW_MILLIS = 15_000L
- const val SLOW_RESPONSE_MILLIS = 2_000L
+ const val HEALTHY_RESPONSES = 20
}
}
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiError.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiError.kt
index 946c083d..2b30d8cc 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiError.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiError.kt
@@ -73,7 +73,8 @@ sealed class StalkerApiError(
class Server(
message: String,
override val httpStatus: Int? = null,
- override val portalReason: String? = null
+ override val portalReason: String? = null,
+ override val retryAfterMillis: Long? = null
) : StalkerApiError(message, httpStatus = httpStatus, portalReason = portalReason)
class Transport(message: String, cause: Throwable? = null) : StalkerApiError(message, cause)
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiService.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiService.kt
index 82e3523e..7496cd3d 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiService.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerApiService.kt
@@ -426,6 +426,14 @@ interface StalkerApiService {
page: Int
): Result<StalkerPagedItems>
+ suspend fun getSeriesPage(
+ session: StalkerSession,
+ profile: StalkerDeviceProfile,
+ categoryId: String?,
+ page: Int,
+ sortBy: String?
+ ): Result<StalkerPagedItems> = getSeriesPage(session, profile, categoryId, page)
+
suspend fun getSeriesDetails(
session: StalkerSession,
profile: StalkerDeviceProfile,
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerPortalStateStore.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerPortalStateStore.kt
index fabdd4f5..fc329c9a 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerPortalStateStore.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerPortalStateStore.kt
@@ -39,25 +39,22 @@ class StalkerPortalStateStore @Inject constructor(
suspend fun get(providerId: Long): StalkerPortalStateEntity? = dao.get(providerId)
internal fun indexPacing(state: StalkerPortalStateEntity): StalkerAdaptiveIndexPolicy = runCatching {
- val pacing = json.parseToJsonElement(state.learningJson).jsonObject.getValue("indexPacing").jsonObject
+ val pacing = json.parseToJsonElement(state.learningJson).jsonObject.getValue("indexConcurrency").jsonObject
require(pacing.getValue("configurationGeneration").jsonPrimitive.longOrNull == state.configurationGeneration)
- StalkerAdaptiveIndexPolicy(
- pacing.getValue("intervalMillis").jsonPrimitive.content.toLong(),
- pacing.getValue("minimumIntervalMillis").jsonPrimitive.content.toLong()
- )
+ // Relearn upward from at most three after a restart; a stressed serial profile stays serial.
+ StalkerAdaptiveIndexPolicy(pacing.getValue("limit").jsonPrimitive.content.toInt()
+ .coerceIn(StalkerAdaptiveIndexPolicy.MIN_CONCURRENCY, StalkerAdaptiveIndexPolicy.INITIAL_CONCURRENCY))
}.getOrElse { StalkerAdaptiveIndexPolicy() }
internal suspend fun recordIndexPacing(
providerId: Long,
configurationGeneration: Long,
- intervalMillis: Long,
- minimumIntervalMillis: Long
+ concurrency: Int
) = update(providerId, configurationGeneration) { state ->
val learning = mutableLearning(state.learningJson)
- learning["indexPacing"] = JsonObject(mapOf(
+ learning["indexConcurrency"] = JsonObject(mapOf(
"configurationGeneration" to JsonPrimitive(configurationGeneration),
- "intervalMillis" to JsonPrimitive(intervalMillis),
- "minimumIntervalMillis" to JsonPrimitive(minimumIntervalMillis)
+ "limit" to JsonPrimitive(concurrency)
))
state.copy(configurationGeneration = configurationGeneration, learningJson = JsonObject(learning).toString())
}
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerProvider.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerProvider.kt
index 5ccafca3..3eebd104 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerProvider.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerProvider.kt
@@ -270,8 +270,10 @@ internal companion object {
private var sessionCache: StalkerSession? = null
private var accountProfileCache: StalkerProviderProfile? = null
private var authFailureCache: CachedAuthFailure? = null
- private val categoryCache = mutableMapOf<ContentType, List<CategorySeed>>()
- private val categoryLookups = mutableMapOf<ContentType, CategoryLookup>()
+ private val categoryCache = ConcurrentHashMap<ContentType, List<CategorySeed>>()
+ private val categoryLoadMutexes = KeyedMutexRegistry<ContentType>()
+ private val loadedCategoryTypes = ConcurrentHashMap.newKeySet<ContentType>()
+ private val categoryLookups = ConcurrentHashMap<ContentType, CategoryLookup>()
private var resolvedCategorySnapshot: StalkerResolvedCategoryHandoff.Snapshot? = null
private val remoteIdentityCache = ConcurrentHashMap<Pair<ContentType, String>, Long>()
@@ -283,6 +285,7 @@ internal companion object {
accountProfileCache = null
authFailureCache = null
categoryCache.clear()
+ loadedCategoryTypes.clear()
categoryLookups.clear()
sharedAuthCache.remove(authCacheKey())
if (providerId > 0L) {
@@ -603,7 +606,10 @@ internal companion object {
}
suspend fun getVodStreamsPage(categoryId: Long?, page: Int): Result<StalkerPagedResult<Movie>> =
- loadVodPageWithPortalPageBase(ContentType.MOVIE, categoryId, page).let { result ->
+ getVodStreamsPage(categoryId, page, sortBy = null)
+
+ suspend fun getVodStreamsPage(categoryId: Long?, page: Int, sortBy: String?): Result<StalkerPagedResult<Movie>> =
+ loadVodPageWithPortalPageBase(ContentType.MOVIE, categoryId, page, sortBy = sortBy).let { result ->
mapResolvedPage(ContentType.MOVIE, result) { item ->
toMovie(item, requestedCategoryId = categoryId)
}
@@ -664,6 +670,14 @@ internal companion object {
): Result<StalkerPagedResult<StalkerVodCatalogItem>> =
getClassifiedVodPage(ContentType.MOVIE, categoryId, page, seriesCategoryId)
+ suspend fun getSplitVodPage(
+ categoryId: Long,
+ seriesCategoryId: Long,
+ page: Int,
+ sortBy: String?
+ ): Result<StalkerPagedResult<StalkerVodCatalogItem>> =
+ getClassifiedVodPage(ContentType.MOVIE, categoryId, page, seriesCategoryId, sortBy = sortBy)
+
suspend fun projectVodCategoryToSeries(categoryId: Long): Long? {
val rawId = resolveRawCategoryId(ContentType.MOVIE, categoryId) ?: return null
val name = categoryCache[ContentType.MOVIE]
@@ -675,9 +689,9 @@ internal companion object {
ContentType.SERIES,
listOf(rawId to name)
)?.get(rawId) ?: fallbackStableId(ContentType.SERIES, rawId)
- categoryCache[ContentType.SERIES] = categoryCache[ContentType.SERIES]
- .orEmpty()
- .filterNot { it.rawId == rawId } + CategorySeed(projectedId, rawId, name)
+ categoryCache.compute(ContentType.SERIES) { _, current ->
+ current.orEmpty().filterNot { it.rawId == rawId } + CategorySeed(projectedId, rawId, name)
+ }
putRemoteIdentity(ContentType.SERIES to rawId, projectedId)
return projectedId
}
@@ -693,9 +707,9 @@ internal companion object {
ContentType.MOVIE,
listOf(rawId to name)
)?.get(rawId) ?: fallbackStableId(ContentType.MOVIE, rawId)
- categoryCache[ContentType.MOVIE] = categoryCache[ContentType.MOVIE]
- .orEmpty()
- .filterNot { it.rawId == rawId } + CategorySeed(projectedId, rawId, name)
+ categoryCache.compute(ContentType.MOVIE) { _, current ->
+ current.orEmpty().filterNot { it.rawId == rawId } + CategorySeed(projectedId, rawId, name)
+ }
putRemoteIdentity(ContentType.MOVIE to rawId, projectedId)
return projectedId
}
@@ -806,13 +820,16 @@ internal companion object {
return mapped
}
- suspend fun getSeriesListPage(categoryId: Long?, page: Int): Result<StalkerPagedResult<Series>> {
- val primary = getNativeSeriesListPage(categoryId, page)
+ suspend fun getSeriesListPage(categoryId: Long?, page: Int): Result<StalkerPagedResult<Series>> =
+ getSeriesListPage(categoryId, page, sortBy = null)
+
+ suspend fun getSeriesListPage(categoryId: Long?, page: Int, sortBy: String?): Result<StalkerPagedResult<Series>> {
+ val primary = getNativeSeriesListPage(categoryId, page, sortBy)
// Fall back to VOD items with is_series=1 when the type=series endpoint returns
// empty (providers that serve series inside VOD). Only triggers when the primary
// path produces zero items, so portals using type=series are unaffected.
if (primary is Result.Success && primary.data.items.isEmpty()) {
- val vodPage = loadVodPageWithPortalPageBase(ContentType.SERIES, categoryId, page)
+ val vodPage = loadVodPageWithPortalPageBase(ContentType.SERIES, categoryId, page, sortBy = sortBy)
.let { result -> mapResolvedPage(ContentType.SERIES, result) { item ->
if (item.isSeries) {
toSeries(
@@ -830,8 +847,12 @@ internal companion object {
}
suspend fun getNativeSeriesListPage(categoryId: Long?, page: Int): Result<StalkerPagedResult<Series>> =
+ getNativeSeriesListPage(categoryId, page, sortBy = null)
+
+ suspend fun getNativeSeriesListPage(categoryId: Long?, page: Int, sortBy: String?): Result<StalkerPagedResult<Series>> =
mapPagedItems(ContentType.SERIES, categoryId) { session, profile, rawCategoryId ->
- api.getSeriesPage(session, profile, rawCategoryId, page)
+ if (sortBy == null) api.getSeriesPage(session, profile, rawCategoryId, page)
+ else api.getSeriesPage(session, profile, rawCategoryId, page, sortBy)
}.let { result -> mapResolvedPage(ContentType.SERIES, result) { item ->
toSeries(item, requestedCategoryId = categoryId)
} }
@@ -1590,6 +1611,7 @@ is Result.Success -> {
)
}
categoryCache[type] = categories
+ loadedCategoryTypes.add(type)
Result.success(
categories.map { seed ->
Category(
@@ -2679,20 +2701,23 @@ private fun playbackTransportChallengeFor(url: String): StalkerTransportChalleng
else -> type
}
val targetId = categoryId ?: return null
- val cached = categoryCache[normalizedType]
- if (cached != null) {
- return cached.firstOrNull { it.id == targetId }?.rawId
- }
- identityResolver?.reverse(providerId, normalizedType, targetId)?.let { return it }
- when (val categoriesResult = when (normalizedType) {
- ContentType.LIVE -> getLiveCategories()
- ContentType.VOD -> getVodCategories()
- ContentType.MOVIE -> getVodCategories()
- ContentType.SERIES -> getSeriesCategories()
- ContentType.SERIES_EPISODE -> Result.success(emptyList())
- }) {
- is Result.Success -> return categoryCache[normalizedType]?.firstOrNull { it.id == targetId }?.rawId
- else -> return null
+ return categoryLoadMutexes.withLock(normalizedType) {
+ val cached = categoryCache[normalizedType]
+ cached?.firstOrNull { it.id == targetId }?.rawId?.let { return@withLock it }
+ identityResolver?.reverse(providerId, normalizedType, targetId)?.let { return@withLock it }
+ // A cross-catalog projection can seed only part of this cache. That does not
+ // mean the portal's complete category list has already been loaded.
+ if (normalizedType in loadedCategoryTypes) return@withLock null
+ when (val categoriesResult = when (normalizedType) {
+ ContentType.LIVE -> getLiveCategories()
+ ContentType.VOD -> getUnifiedVodCategories()
+ ContentType.MOVIE -> getVodCategories()
+ ContentType.SERIES -> getSeriesCategories()
+ ContentType.SERIES_EPISODE -> Result.success(emptyList())
+ }) {
+ is Result.Success -> categoryCache[normalizedType]?.firstOrNull { it.id == targetId }?.rawId
+ else -> null
+ }
}
}
diff --git a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinator.kt b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinator.kt
index 19dab0af..2ab7f58f 100644
--- a/data/src/main/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinator.kt
+++ b/data/src/main/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinator.kt
@@ -10,6 +10,7 @@ import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
+import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
@@ -114,6 +115,8 @@ class StalkerRequestCoordinator internal constructor(
val inFlight = mutableMapOf<String, CompletableDeferred<Any?>>()
var active = 0
var activeBackground = 0
+ var activeBackgroundMetadata = 0
+ var indexNetworkInFlight = 0
var safeMetadataConcurrency = NORMAL_METADATA_CONCURRENCY
var stressCooldownUntil = 0L
var rateLimitCooldownUntil = 0L
@@ -173,17 +176,19 @@ class StalkerRequestCoordinator internal constructor(
while (!acquired) {
acquired = state.mutex.withLock {
val now = nowMillis()
- val limit = state.currentLimit(now)
val first = state.waiters.minWithOrNull(
compareBy<Waiter>({ it.priority.ordinal }, { it.ticket })
)
val backgroundAllowed = priority != StalkerRequestPriority.BACKGROUND_INDEX ||
state.activeBackground < indexExecutionConcurrency
- if (first == waiter && state.active < limit && backgroundAllowed) {
+ if (first == waiter && state.canAcquireMetadata(priority, now) && backgroundAllowed) {
state.waiters.remove(waiter)
state.active += 1
operationStarted = true
- if (priority == StalkerRequestPriority.BACKGROUND_INDEX) state.activeBackground += 1
+ if (priority == StalkerRequestPriority.BACKGROUND_INDEX) {
+ state.activeBackground += 1
+ state.activeBackgroundMetadata += 1
+ }
true
} else {
false
@@ -197,6 +202,7 @@ class StalkerRequestCoordinator internal constructor(
yieldMetadata = if (canYieldMetadata) ({
if (acquired) {
state.active -= 1
+ state.activeBackgroundMetadata -= 1
acquired = false
state.waiters += waiter
}
@@ -204,9 +210,10 @@ class StalkerRequestCoordinator internal constructor(
resumeMetadata = if (canYieldMetadata) ({
if (acquired) true else {
val first = state.waiters.minWithOrNull(compareBy<Waiter>({ it.priority.ordinal }, { it.ticket }))
- if (first == waiter && state.active < state.currentLimit(nowMillis())) {
+ if (first == waiter && state.canAcquireMetadata(priority, nowMillis())) {
state.waiters.remove(waiter)
state.active += 1
+ state.activeBackgroundMetadata += 1
acquired = true
true
} else false
@@ -253,10 +260,13 @@ class StalkerRequestCoordinator internal constructor(
deferred.completeExceptionally(error)
throw error
} finally {
- val finalSnapshot = state.mutex.withLock {
+ val finalSnapshot = withContext(NonCancellable) { state.mutex.withLock {
state.waiters.remove(waiter)
if (acquired) {
state.active = (state.active - 1).coerceAtLeast(0)
+ if (priority == StalkerRequestPriority.BACKGROUND_INDEX) {
+ state.activeBackgroundMetadata = (state.activeBackgroundMetadata - 1).coerceAtLeast(0)
+ }
}
if (operationStarted && priority == StalkerRequestPriority.BACKGROUND_INDEX) {
state.activeBackground = (state.activeBackground - 1).coerceAtLeast(0)
@@ -264,7 +274,7 @@ class StalkerRequestCoordinator internal constructor(
if (state.inFlight[requestKey] === deferred) state.inFlight.remove(requestKey)
val limit = state.currentLimit(nowMillis())
StalkerRequestSnapshot(state.active, state.waiters.size, limit, state.stressCooldownUntil)
- }
+ } }
StalkerTelemetry.requestCompleted(
providerId = providerId,
priority = priority,
@@ -296,6 +306,12 @@ class StalkerRequestCoordinator internal constructor(
return state.mutex.withLock { state.currentLimit(nowMillis()) }
}
+ internal suspend fun effectiveIndexConcurrencyLimit(providerId: Long): Int {
+ val state = states.computeIfAbsent(providerId) { ProviderState(nowMillis()) }
+ hydratePersistedState(providerId, state)
+ return state.mutex.withLock { state.indexLimit() }
+ }
+
suspend fun recordFailure(providerId: Long, error: Throwable?) {
if (providerId <= 0L) return
error?.findRateLimit()?.let { rateLimit ->
@@ -350,26 +366,28 @@ class StalkerRequestCoordinator internal constructor(
val backgroundBlocked = lowPriority &&
(state.interactiveWaiters > 0 || state.interactiveInFlight > 0)
val adaptiveIndex = adaptiveIndexingEnabled && priority == StalkerNetworkPriority.INDEX
- val indexInterval = if (StalkerTrafficCoordinator.isPlaybackActive(providerId)) {
- maxOf(state.indexPacing.intervalMillis, StalkerAdaptiveIndexPolicy.PLAYBACK_INTERVAL_MILLIS)
- } else state.indexPacing.intervalMillis
- val indexWait = if (adaptiveIndex) {
- state.lastIndexRequestAt?.let { (it + indexInterval - now).coerceAtLeast(0L) } ?: 0L
+ val playbackActive = StalkerTrafficCoordinator.isPlaybackActive(providerId)
+ val indexWait = if (adaptiveIndex && playbackActive) {
+ state.lastIndexRequestAt?.let {
+ (it + StalkerAdaptiveIndexPolicy.PLAYBACK_INTERVAL_MILLIS - now).coerceAtLeast(0L)
+ } ?: 0L
} else 0L
- val backgroundWait = if (lowPriority) {
+ val indexWindowFull = adaptiveIndex && state.indexNetworkInFlight >=
+ (if (playbackActive) 1 else state.indexLimit())
+ val backgroundWait = if (lowPriority && !adaptiveIndex) {
(state.nextBackgroundRequestAt - now).coerceAtLeast(0L)
} else {
0L
}
val canBorrow = interactive && state.networkTokens > INTERACTIVE_TOKEN_FLOOR
- val tokenWait = if (state.networkTokens >= 1.0 || canBorrow) {
+ val tokenWait = if (adaptiveIndex || state.networkTokens >= 1.0 || canBorrow) {
0L
} else {
(((1.0 - state.networkTokens) * NETWORK_TOKEN_REFILL_MILLIS).toLong())
.coerceAtLeast(1L)
}
waitMillis = maxOf(
- NETWORK_ADMISSION_POLL_MILLIS.takeIf { backgroundBlocked } ?: 0L,
+ NETWORK_ADMISSION_POLL_MILLIS.takeIf { backgroundBlocked || indexWindowFull } ?: 0L,
maxOf(backgroundWait, indexWait),
tokenWait
)
@@ -382,10 +400,13 @@ class StalkerRequestCoordinator internal constructor(
return@withLock null
}
- // Index admission has its own strict spacing (no burst capacity). It still
- // waits for foreground tokens and interactive work, but cannot drain them.
+ // Bound actual HTTP calls, including page-base probes. Foreground tokens
+ // remain available for browsing and stream resolution.
if (!adaptiveIndex) state.networkTokens -= 1.0
- if (adaptiveIndex) state.lastIndexRequestAt = now
+ if (adaptiveIndex) {
+ state.lastIndexRequestAt = now
+ state.indexNetworkInFlight += 1
+ }
if (lowPriority && !adaptiveIndex) {
val interval = if (priority == StalkerNetworkPriority.BACKGROUND || priority == StalkerNetworkPriority.INDEX) {
BACKGROUND_REQUEST_INTERVAL_MILLIS
@@ -404,9 +425,9 @@ class StalkerRequestCoordinator internal constructor(
}
} finally {
if (protectedForeground) {
- state.mutex.withLock {
+ withContext(NonCancellable) { state.mutex.withLock {
state.interactiveWaiters = (state.interactiveWaiters - 1).coerceAtLeast(0)
- }
+ } }
}
}
}
@@ -414,11 +435,14 @@ class StalkerRequestCoordinator internal constructor(
suspend fun releaseNetworkPermit(permit: StalkerNetworkPermit) {
val protectedForeground = permit.priority == StalkerNetworkPriority.INTERACTIVE ||
(adaptiveIndexingEnabled && permit.priority == StalkerNetworkPriority.FOREGROUND)
- if (permit.providerId <= 0L || !protectedForeground) return
+ if (permit.providerId <= 0L) return
val state = states[permit.providerId] ?: return
- state.mutex.withLock {
- state.interactiveInFlight = (state.interactiveInFlight - 1).coerceAtLeast(0)
- }
+ withContext(NonCancellable) { state.mutex.withLock {
+ if (protectedForeground) state.interactiveInFlight = (state.interactiveInFlight - 1).coerceAtLeast(0)
+ if (adaptiveIndexingEnabled && permit.priority == StalkerNetworkPriority.INDEX) {
+ state.indexNetworkInFlight = (state.indexNetworkInFlight - 1).coerceAtLeast(0)
+ }
+ } }
}
/** One parsed HTTP result per index permit; retries and local circuit rejections do not train health. */
@@ -429,20 +453,17 @@ class StalkerRequestCoordinator internal constructor(
val state = states[permit.providerId] ?: return
state.mutex.withLock {
val changed = when {
- error == null -> state.indexPacing.success(
- nowMillis(), (nowMillis() - permit.admittedAt).coerceAtLeast(0L),
- StalkerTrafficCoordinator.isPlaybackActive(permit.providerId)
- )
+ error == null -> state.indexPacing.success(StalkerTrafficCoordinator.isPlaybackActive(permit.providerId))
error.isProviderStressSignal() -> state.indexPacing.stress()
else -> { state.indexPacing.resetHealth(); false }
}
if (changed) {
portalStateStore?.recordIndexPacing(
permit.providerId, state.configurationGeneration,
- state.indexPacing.intervalMillis, state.indexPacing.minimumIntervalMillis
+ state.indexPacing.concurrency
)
StalkerTelemetry.capabilityChanged(
- permit.providerId, "INDEX_INTERVAL_MILLIS", state.indexPacing.intervalMillis.toString()
+ permit.providerId, "INDEX_CONCURRENCY", state.indexPacing.concurrency.toString()
)
}
}
@@ -466,6 +487,10 @@ class StalkerRequestCoordinator internal constructor(
recordRateLimit(permit.providerId, rateLimit.retryAfterMillis)
return
}
+ (error as? StalkerApiError.Server)?.retryAfterMillis?.takeIf { it > 0L }?.let { retryAfter ->
+ recordRateLimit(permit.providerId, retryAfter)
+ return
+ }
if (!permit.halfOpenProbe) return
val state = states[permit.providerId] ?: return
val retryAt = nowMillis() + HALF_OPEN_FAILURE_COOLDOWN_MILLIS
@@ -534,6 +559,18 @@ class StalkerRequestCoordinator internal constructor(
private fun ProviderState.currentLimit(now: Long): Int =
if (safeMetadataConcurrency <= 1 || stressCooldownUntil > now) 1 else NORMAL_METADATA_CONCURRENCY
+ private fun ProviderState.indexLimit(): Int = minOf(indexExecutionConcurrency, indexPacing.concurrency)
+
+ private fun ProviderState.canAcquireMetadata(priority: StalkerRequestPriority, now: Long): Boolean {
+ if (!adaptiveIndexingEnabled) return active < currentLimit(now)
+ return if (priority == StalkerRequestPriority.BACKGROUND_INDEX) {
+ activeBackgroundMetadata < indexLimit()
+ } else {
+ // Index pages cannot occupy the slots needed by browsing and playback.
+ active - activeBackgroundMetadata < currentLimit(now)
+ }
+ }
+
private fun ProviderState.refillNetworkTokens(now: Long) {
val elapsed = (now - lastNetworkRefillAt).coerceAtLeast(0L)
if (elapsed <= 0L) return
@@ -546,7 +583,9 @@ class StalkerRequestCoordinator internal constructor(
private fun Throwable.isProviderStressSignal(): Boolean = when (this) {
is StalkerApiError.RateLimited -> true
- is StalkerApiError.Server -> httpStatus == 503
+ is StalkerApiError.Authorization -> httpStatus == 403
+ is StalkerApiError.BlockedOrConfiguration -> portalReason in setOf("http_403_forbidden", "html_error_page")
+ is StalkerApiError.Server -> httpStatus in 500..599
is StalkerApiError.Transport -> cause is SocketTimeoutException ||
message.orEmpty().contains("timeout", ignoreCase = true) ||
message.orEmpty().contains("reset", ignoreCase = true)
@@ -577,7 +616,7 @@ class StalkerRequestCoordinator internal constructor(
}
private companion object {
- const val MAX_INDEX_EXECUTION_CONCURRENCY = 2
+ const val MAX_INDEX_EXECUTION_CONCURRENCY = 8
const val NORMAL_METADATA_CONCURRENCY = 2
const val ADMISSION_POLL_MILLIS = 20L
const val STRESS_COOLDOWN_MILLIS = 5L * 60L * 1000L
diff --git a/data/src/main/java/com/streamvault/data/sync/ProviderSyncPorts.kt b/data/src/main/java/com/streamvault/data/sync/ProviderSyncPorts.kt
index 21832434..e52fc411 100644
--- a/data/src/main/java/com/streamvault/data/sync/ProviderSyncPorts.kt
+++ b/data/src/main/java/com/streamvault/data/sync/ProviderSyncPorts.kt
@@ -131,7 +131,9 @@ interface ProviderSyncCommands {
force: Boolean = false,
maxCategoriesPerSection: Int? = null,
onProgress: ((String) -> Unit)? = null,
- maxPagesPerSection: Int? = null
+ maxPagesPerSection: Int? = null,
+ categoryHeadsOnly: Boolean = false,
+ scheduleContinuation: Boolean = true
): Result<Unit>
suspend fun rebuildXtreamIndex(
diff --git a/data/src/main/java/com/streamvault/data/sync/StalkerCatalogSyncExecutor.kt b/data/src/main/java/com/streamvault/data/sync/StalkerCatalogSyncExecutor.kt
index fb8c260c..054dccc8 100644
--- a/data/src/main/java/com/streamvault/data/sync/StalkerCatalogSyncExecutor.kt
+++ b/data/src/main/java/com/streamvault/data/sync/StalkerCatalogSyncExecutor.kt
@@ -61,7 +61,6 @@ private const val STALKER_BULK_LIVE_UNSUPPORTED_TTL_MILLIS = 6 * 60 * 60 * 1000L
private const val LIVE_CATEGORY_SEQUENTIAL_MODE_WARNING =
"Live category sync downgraded to sequential mode after provider stress signals."
private const val FALLBACK_STAGE_BATCH_SIZE = 500
-private const val STALKER_BOOTSTRAP_LIVE_CHANNEL_CAP = 200
/** Owns Stalker authentication, full catalog orchestration, and Live repair execution. */
internal class StalkerCatalogSyncExecutor(
@@ -178,7 +177,6 @@ internal class StalkerCatalogSyncExecutor(
hiddenLiveCategoryIds = hiddenLiveCategoryIds,
requiredHiddenLiveCategoryIds = requiredHiddenLiveCategoryIds,
onProgress = onProgress,
- maxChannels = STALKER_BOOTSTRAP_LIVE_CHANNEL_CAP.takeIf { bootstrap },
afterCatalogApply = catalogCommitCallback,
retryPendingCategoriesOnly = !force && !bootstrap
)
@@ -187,7 +185,7 @@ internal class StalkerCatalogSyncExecutor(
} else metadata.liveCount
metadata = metadata.copy(
lastLiveSync = now,
- lastLiveSuccess = if (!bootstrap && liveCatalogResult.coverageComplete) now
+ lastLiveSuccess = if (liveCatalogResult.coverageComplete) now
else metadata.lastLiveSuccess,
liveCount = committedLiveCount
)
@@ -336,16 +334,10 @@ internal class StalkerCatalogSyncExecutor(
force = force
)
}
- if (bootstrap && provider.stalkerCatalogMode == StalkerCatalogMode.BACKGROUND_INDEX) {
+ if (bootstrap) {
continuationWork += SyncContinuation(
operation = SyncContinuationOperation.STALKER_BOOTSTRAP_RESUME,
- reason = "bootstrap category shells are committed; fetch the first VOD page and finish live onboarding"
- )
- } else if (bootstrap) {
- continuationWork += SyncContinuation(
- operation = SyncContinuationOperation.FULL_CATALOG,
- reason = "bootstrap catalog is committed; durable full provider sync is queued",
- force = true
+ reason = "full live catalog and VOD category shells are committed; fetch every VOD category head"
)
} else when (provider.epgSyncMode) {
ProviderEpgSyncMode.UPFRONT -> warnings += syncProviderEpg(
diff --git a/data/src/main/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutor.kt b/data/src/main/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutor.kt
index 647aecdb..2898daae 100644
--- a/data/src/main/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutor.kt
+++ b/data/src/main/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutor.kt
@@ -23,7 +23,7 @@ import java.util.Locale
import kotlin.math.min
private const val STALKER_INCREMENTAL_WILDCARD_PAGE_SLICE_SIZE = 192
-private const val STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES = 2
+private const val STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES = 8
private const val STALKER_INCREMENTAL_MAX_SECTION_RUN_MILLIS = 240_000L
private const val TAG = "StalkerIncrementalIndexExecutor"
@@ -165,9 +165,39 @@ internal class StalkerIncrementalIndexExecutor(
BuildConfig.STALKER_INDEX_PAGE_CONCURRENCY
} else {
1
- }
+ },
+ private val adaptiveCategoryPaging: Boolean = BuildConfig.STALKER_ADAPTIVE_INDEXING
) {
- private val wildcardPageConcurrency = configuredWildcardPageConcurrency.coerceIn(1, 2)
+ private val wildcardPageConcurrency = configuredWildcardPageConcurrency.coerceIn(1, 8)
+
+ /** Warm page one for every visible category before starting the full catalog walk. */
+ suspend fun processCategoryHeads(
+ provider: Provider,
+ api: StalkerProvider,
+ contentType: ContentType,
+ onProgress: ((String) -> Unit)?
+ ) {
+ require(contentType == ContentType.MOVIE || contentType == ContentType.SERIES) {
+ "Unsupported Stalker category-head section: $contentType"
+ }
+ val allCategories = operations.allVisibleCategories(provider.id, contentType)
+ val visibleCategories = operations.visibleCategories(contentType, allCategories, api)
+ if (visibleCategories.isNotEmpty() && visibleCategories.all { category ->
+ (operations.getHydration(provider.id, contentType, category.categoryId)?.lastSuccessfulPage ?: 0) >= 1
+ }) return
+ val headCategoryCount = visibleCategories.size.coerceAtLeast(1)
+ processSummary(
+ provider = provider,
+ api = api,
+ contentType = contentType,
+ maxCategories = headCategoryCount,
+ onProgress = onProgress,
+ maxPages = headCategoryCount,
+ bypassWildcardProbe = true,
+ maxParallelFetches = if (adaptiveCategoryPaging) headCategoryCount else 2,
+ categoryHeadsOnly = true
+ )
+ }
suspend fun processSummary(
provider: Provider,
@@ -175,7 +205,10 @@ internal class StalkerIncrementalIndexExecutor(
contentType: ContentType,
maxCategories: Int,
onProgress: ((String) -> Unit)?,
- maxPages: Int? = null
+ maxPages: Int? = null,
+ bypassWildcardProbe: Boolean = false,
+ maxParallelFetches: Int? = null,
+ categoryHeadsOnly: Boolean = false
) {
require(contentType == ContentType.MOVIE || contentType == ContentType.SERIES) {
"Unsupported Stalker summary index section: $contentType"
@@ -218,7 +251,16 @@ internal class StalkerIncrementalIndexExecutor(
}
}
var categoryPlan = visibleCategories
- if (wildcardCategory != null && learnedWildcardSupport != false) {
+ val explicitCategoryPaging = adaptiveCategoryPaging && visibleCategories.any {
+ !api.isWildcardCategory(it.type, it.categoryId)
+ }
+ val wildcardCheckpoint = wildcardCategory?.let {
+ operations.getHydration(provider.id, contentType, it.categoryId)
+ }
+ val wildcardProbePending = !explicitCategoryPaging || wildcardCheckpoint?.isComplete == true ||
+ (wildcardCheckpoint?.lastSuccessfulPage ?: 0) == 0
+ if ((!bypassWildcardProbe || explicitCategoryPaging) && wildcardProbePending &&
+ wildcardCategory != null && learnedWildcardSupport != false) {
val wildcardIsSoleCategory = visibleCategories.size == 1 &&
visibleCategories.first().categoryId == wildcardCategory.categoryId
StalkerTelemetry.strategySelected(
@@ -236,32 +278,36 @@ internal class StalkerIncrementalIndexExecutor(
contentType = contentType,
wildcardCategory = wildcardCategory,
visibleCategories = allVisibleCategories,
- maxPages = maxPages?.coerceAtLeast(1) ?: STALKER_INCREMENTAL_WILDCARD_PAGE_SLICE_SIZE,
+ maxPages = if (explicitCategoryPaging) 1 else
+ maxPages?.coerceAtLeast(1) ?: STALKER_INCREMENTAL_WILDCARD_PAGE_SLICE_SIZE,
initialJob = initialJob,
- onProgress = onProgress
+ onProgress = onProgress,
+ wholeResponseProbe = explicitCategoryPaging
)
outcome.capabilitySupported?.let { supported ->
api.recordWildcardCapability(contentType, supported)
}
if (!outcome.shouldFallback) {
- if (!outcome.complete) return
- val backfill = wildcardBackfillCategories(
- provider = provider,
- api = api,
- contentType = contentType,
- visibleCategories = visibleCategories
- )
- if (backfill.isEmpty()) {
- if (!wildcardIsSoleCategory) {
- operations.pruneStaleRows(provider.id, contentType)
+ if (!outcome.complete && !explicitCategoryPaging) return
+ if (outcome.complete) {
+ val backfill = wildcardBackfillCategories(
+ provider = provider,
+ api = api,
+ contentType = contentType,
+ visibleCategories = visibleCategories
+ )
+ if (backfill.isEmpty()) {
+ if (!wildcardIsSoleCategory) {
+ operations.pruneStaleRows(provider.id, contentType)
+ }
+ return
}
- return
+ operations.log(
+ "Stalker ${contentType.name} wildcard index complete for provider ${provider.id}; " +
+ "backfilling ${backfill.size} categories without wildcard rows."
+ )
+ categoryPlan = backfill
}
- operations.log(
- "Stalker ${contentType.name} wildcard index complete for provider ${provider.id}; " +
- "backfilling ${backfill.size} categories without wildcard rows."
- )
- categoryPlan = backfill
}
} else if (wildcardCategory != null && learnedWildcardSupport == false) {
StalkerTelemetry.strategySelected(provider.id, "${contentType.name}_CATEGORY", "WILDCARD_KNOWN_UNSUPPORTED")
@@ -292,6 +338,7 @@ internal class StalkerIncrementalIndexExecutor(
categoryPlan.forEach { category ->
if (size >= maxCategories) return@forEach
val hydration = hydrationByCategory[category.categoryId]
+ if (categoryHeadsOnly && (hydration?.lastSuccessfulPage ?: 0) >= 1) return@forEach
if (!canAttempt(hydration, now)) return@forEach
add(category)
}
@@ -328,7 +375,10 @@ internal class StalkerIncrementalIndexExecutor(
var pagesCommitted = 0
var rowsCommitted = 0
val sectionStartedAt = System.currentTimeMillis()
- val parallelFetchLimit = categoryFetchConcurrencyLimit(provider, maxCategories)
+ val parallelFetchLimit = if (categoryHeadsOnly && adaptiveCategoryPaging) {
+ (maxParallelFetches ?: 8).coerceIn(1, 8)
+ } else if (adaptiveCategoryPaging) wildcardPageConcurrency else
+ categoryFetchConcurrencyLimit(provider, maxCategories, maxParallelFetches)
val runDeadlineAt = System.currentTimeMillis() + STALKER_INCREMENTAL_MAX_SECTION_RUN_MILLIS
val pendingQueue = ArrayDeque(pending)
var forceSequential = parallelFetchLimit <= 1
@@ -343,10 +393,11 @@ internal class StalkerIncrementalIndexExecutor(
}
val windowSize = minOf(
if (forceSequential) 1 else parallelFetchLimit,
- pendingQueue.size
+ if (adaptiveCategoryPaging && !categoryHeadsOnly) Int.MAX_VALUE else pendingQueue.size,
+ maxPages?.let { (it - pagesCommitted).coerceAtLeast(1) } ?: Int.MAX_VALUE
)
val window = buildList {
- repeat(windowSize) { add(pendingQueue.removeFirst()) }
+ repeat(minOf(windowSize, pendingQueue.size)) { add(pendingQueue.removeFirst()) }
}
val attempts = window.mapNotNull { category ->
val hydration = operations.getHydration(provider.id, contentType, category.categoryId)
@@ -366,20 +417,35 @@ internal class StalkerIncrementalIndexExecutor(
now = System.currentTimeMillis()
)
CategoryFetchAttempt(category, hydration, nextPage)
+ }.let { firsts ->
+ if (!adaptiveCategoryPaging || categoryHeadsOnly || firsts.isEmpty()) firsts else buildList {
+ addAll(firsts)
+ // Known totals let even a single large category fill the shared page pool.
+ for (first in firsts) {
+ val lastPage = first.hydration?.advertisedTotalPages
+ ?: first.hydration?.totalPages ?: first.nextPage
+ for (page in first.nextPage + 1..lastPage.coerceAtMost(first.nextPage + windowSize - 1)) {
+ if (size >= windowSize) break
+ add(first.copy(nextPage = page))
+ }
+ }
+ }.sortedWith(compareBy({ it.category.categoryId }, { it.nextPage }))
}
if (attempts.isEmpty()) continue
val fetchedResults = coroutineScope {
attempts.map { attempt ->
async {
+ val result = operations.fetchSummaryPage(
+ provider = provider,
+ api = api,
+ contentType = contentType,
+ categoryId = attempt.category.categoryId,
+ page = attempt.nextPage
+ )
+ if (result is Result.Error) operations.recordRequestFailure(provider.id, result.exception)
CategoryFetchResult(
attempt = attempt,
- result = operations.fetchSummaryPage(
- provider = provider,
- api = api,
- contentType = contentType,
- categoryId = attempt.category.categoryId,
- page = attempt.nextPage
- )
+ result = result
)
}
}.awaitAll()
@@ -387,13 +453,14 @@ internal class StalkerIncrementalIndexExecutor(
var windowRetryableFailures = 0
val successfulPages = mutableListOf<SuccessfulPage>()
+ val validationHydration = mutableMapOf<Long, StalkerHydrationSnapshot?>()
+ val blockedCategories = mutableSetOf<Long>()
+ val failedAttempts = mutableListOf<suspend () -> Unit>()
for (fetched in fetchedResults) {
val category = fetched.attempt.category
- val hydration = fetched.attempt.hydration
+ if (category.categoryId in blockedCategories) continue
+ val hydration = validationHydration[category.categoryId] ?: fetched.attempt.hydration
val nextPage = fetched.attempt.nextPage
- if (fetched.result is Result.Error) {
- operations.recordRequestFailure(provider.id, fetched.result.exception)
- }
when (val result = fetched.result) {
is Result.Success -> {
val pageFingerprint = StalkerIndexPolicy.pageFingerprint(result.data.items, contentType)
@@ -408,7 +475,8 @@ internal class StalkerIncrementalIndexExecutor(
skippedMalformedRows += result.data.items.size
val shouldRetryImmediately = category.categoryId !in retriedImmediately &&
((hydration?.failureCount ?: 0) == 0)
- operations.markAttemptFailed(
+ blockedCategories += category.categoryId
+ failedAttempts += suspend { operations.markAttemptFailed(
providerId = provider.id,
contentType = contentType,
categoryId = category.categoryId,
@@ -418,7 +486,7 @@ internal class StalkerIncrementalIndexExecutor(
message = anomaly,
retryable = true,
pageFingerprint = pageFingerprint
- )
+ ) }
if (shouldRetryImmediately) {
retriedImmediately += category.categoryId
pendingQueue.addLast(category)
@@ -451,6 +519,15 @@ internal class StalkerIncrementalIndexExecutor(
terminationReason = result.data.terminationReason,
pageFingerprint = pageFingerprint
)
+ if (hydration != null) validationHydration[category.categoryId] = hydration.copy(
+ lastSuccessfulPage = nextPage,
+ lastLoadedPage = nextPage,
+ itemCount = hydration.itemCount + dedupedItems.size,
+ lastPageFingerprint = pageFingerprint,
+ advertisedTotalItems = result.data.advertisedTotalItems,
+ advertisedTotalPages = result.data.advertisedTotalPages
+ )
+ if (result.data.isComplete || result.data.isTruncated) blockedCategories += category.categoryId
}
is Result.Error -> {
val failedAt = System.currentTimeMillis()
@@ -461,7 +538,8 @@ internal class StalkerIncrementalIndexExecutor(
val shouldRetryImmediately = retryable &&
category.categoryId !in retriedImmediately &&
((hydration?.failureCount ?: 0) == 0)
- operations.markAttemptFailed(
+ blockedCategories += category.categoryId
+ failedAttempts += suspend { operations.markAttemptFailed(
providerId = provider.id,
contentType = contentType,
categoryId = category.categoryId,
@@ -471,12 +549,11 @@ internal class StalkerIncrementalIndexExecutor(
message = result.message,
retryable = retryable,
pageFingerprint = hydration?.lastPageFingerprint
- )
+ ) }
if (shouldRetryImmediately) {
retriedImmediately += category.categoryId
pendingQueue.addLast(category)
}
- if (!retryable) throw IllegalStateException(result.message, result.exception)
upsertIndexJob(
providerId = provider.id,
section = contentType.name,
@@ -542,12 +619,29 @@ internal class StalkerIncrementalIndexExecutor(
terminationReason = page.terminationReason,
pageFingerprint = page.pageFingerprint
)
- if (!pageComplete && !page.isTruncated && System.currentTimeMillis() < runDeadlineAt) {
- pendingQueue.addLast(page.category)
+ }
+ val visibleRowCount = operations.currentIndexedRowCount(provider.id, contentType)
+ operations.updateSummaryMetadata(provider.id, contentType, visibleRowCount, "RUNNING", indexedAt)
+ upsertIndexJob(
+ providerId = provider.id,
+ section = contentType.name,
+ state = "RUNNING",
+ now = indexedAt,
+ indexedRows = visibleRowCount,
+ lastAttemptAt = indexedAt
+ )
+ }
+ failedAttempts.forEach { it() }
+ if (!categoryHeadsOnly) {
+ successfulPages.map { it.category }.distinctBy { it.categoryId }.forEach { category ->
+ if (category.categoryId !in blockedCategories &&
+ pendingQueue.none { it.categoryId == category.categoryId } &&
+ canAttempt(operations.getHydration(provider.id, contentType, category.categoryId), System.currentTimeMillis())) {
+ pendingQueue.addLast(category)
}
}
}
- if (!forceSequential && windowRetryableFailures >= minOf(2, attempts.size)) {
+ if (!adaptiveCategoryPaging && !forceSequential && windowRetryableFailures >= minOf(2, attempts.size)) {
forceSequential = true
operations.log(
"Stalker ${contentType.name} category fetch downgraded to sequential mode for provider ${provider.id} after retryable parallel failures."
@@ -624,7 +718,8 @@ internal class StalkerIncrementalIndexExecutor(
visibleCategories: List<CategoryEntity>,
maxPages: Int,
initialJob: StalkerIndexJobEntity?,
- onProgress: ((String) -> Unit)?
+ onProgress: ((String) -> Unit)?,
+ wholeResponseProbe: Boolean = false
): WildcardRunOutcome {
val now = System.currentTimeMillis()
val normalVisibleCategoryIds = mutableSetOf<Long>()
@@ -677,10 +772,14 @@ internal class StalkerIncrementalIndexExecutor(
val currentHydration = operations.getHydration(provider.id, contentType, wildcardCategory.categoryId)
if (!canAttempt(currentHydration, System.currentTimeMillis())) break
val nextPage = nextAttemptPage(currentHydration)
+ val remainingPages = currentHydration?.advertisedTotalPages
+ ?: currentHydration?.totalPages?.takeIf { it > 0 }
val windowSize = minOf(
wildcardPageConcurrency,
maxPages - pagesProcessed,
- STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES
+ STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES,
+ if (adaptiveCategoryPaging) remainingPages?.let { (it - nextPage + 1).coerceAtLeast(1) } ?: 1
+ else STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES
).coerceAtLeast(1)
val requestedPages = (nextPage until nextPage + windowSize).toList()
requestedPages.forEach { page ->
@@ -815,7 +914,9 @@ internal class StalkerIncrementalIndexExecutor(
}
indexedRowsEstimate += visibleItems.size
rowsCommitted += visibleItems.size
- val pageComplete = !result.data.isTruncated && (
+ val completeResponse = !wholeResponseProbe ||
+ (result.data.advertisedTotalItems?.let { it > 0 && result.data.items.size >= it } == true)
+ val pageComplete = completeResponse && !result.data.isTruncated && (
result.data.isComplete ||
(result.data.items.isEmpty() && result.data.totalPages in 1..requestedPage)
)
@@ -934,7 +1035,16 @@ internal class StalkerIncrementalIndexExecutor(
}
if (normalCategories.isEmpty()) return emptyList()
val localRows = operations.categoriesWithLocalRows(provider.id, contentType)
- return StalkerIndexPolicy.wildcardBackfillCategories(normalCategories, localRows)
+ val advertisedTotals = normalCategories.mapNotNull { category ->
+ operations.getHydration(provider.id, contentType, category.categoryId)
+ ?.advertisedTotalItems
+ ?.let { total -> category.categoryId to total }
+ }.toMap()
+ return StalkerIndexPolicy.wildcardBackfillCategories(
+ normalCategories = normalCategories,
+ localRowCounts = localRows,
+ advertisedTotals = advertisedTotals
+ )
}
private fun canAttempt(hydration: StalkerHydrationSnapshot?, now: Long): Boolean =
@@ -943,11 +1053,15 @@ internal class StalkerIncrementalIndexExecutor(
private fun nextAttemptPage(hydration: StalkerHydrationSnapshot?): Int =
StalkerIndexPolicy.nextAttemptPage(hydration)
- private fun categoryFetchConcurrencyLimit(provider: Provider, maxCategories: Int): Int =
+ private fun categoryFetchConcurrencyLimit(
+ provider: Provider,
+ maxCategories: Int,
+ requestedLimit: Int? = null
+ ): Int =
minOf(
maxCategories.coerceAtLeast(1),
- operations.runtimeProfile.maxCategoryConcurrency.coerceAtLeast(1),
- STALKER_INCREMENTAL_MAX_PARALLEL_CATEGORY_FETCHES
+ (requestedLimit ?: operations.runtimeProfile.maxCategoryConcurrency).coerceAtLeast(1),
+ 2
).coerceAtLeast(1)
private fun throughputSummary(startedAt: Long, pages: Int, rows: Int): String {
diff --git a/data/src/main/java/com/streamvault/data/sync/StalkerIndexPolicy.kt b/data/src/main/java/com/streamvault/data/sync/StalkerIndexPolicy.kt
index 05d3259b..476526de 100644
--- a/data/src/main/java/com/streamvault/data/sync/StalkerIndexPolicy.kt
+++ b/data/src/main/java/com/streamvault/data/sync/StalkerIndexPolicy.kt
@@ -210,9 +210,12 @@ internal object StalkerIndexPolicy {
*/
fun wildcardBackfillCategories(
normalCategories: List<CategoryEntity>,
- localRowCounts: Map<Long, Int>
+ localRowCounts: Map<Long, Int>,
+ advertisedTotals: Map<Long, Int> = emptyMap()
): List<CategoryEntity> = normalCategories.filter { category ->
- (localRowCounts[category.categoryId] ?: 0) == 0
+ val localRows = localRowCounts[category.categoryId] ?: 0
+ val advertisedTotal = advertisedTotals[category.categoryId]
+ if (advertisedTotal != null) localRows < advertisedTotal else localRows == 0
}
fun dedupePageItems(items: List<out Any>, contentType: ContentType): List<out Any> {
diff --git a/data/src/main/java/com/streamvault/data/sync/SyncManager.kt b/data/src/main/java/com/streamvault/data/sync/SyncManager.kt
index 5687b7c1..4b61ce86 100644
--- a/data/src/main/java/com/streamvault/data/sync/SyncManager.kt
+++ b/data/src/main/java/com/streamvault/data/sync/SyncManager.kt
@@ -1,5 +1,7 @@
package com.streamvault.data.sync
+import com.streamvault.data.BuildConfig
+
import android.content.Context
import android.util.Log
import com.streamvault.data.local.DatabaseTransactionRunner
@@ -1249,23 +1251,26 @@ class SyncManager @Inject constructor(
return Result.error("Provider $providerId is not a Stalker portal")
}
- val firstPage = processQueuedStalkerIndexJobs(
- providerId = providerId,
- force = false,
- maxCategoriesPerSection = 1,
- maxPagesPerSection = 1
- )
- if (firstPage is Result.Error) {
- Log.w(TAG, "Initial Stalker VOD page could not be indexed for provider $providerId: ${firstPage.message}")
- scheduleStalkerIndexSync(providerId, section = null, force = false)
+ if ((syncMetadataRepository.getMetadata(providerId)?.lastLiveSuccess ?: 0L) <= 0L) {
+ val live = syncSection(
+ providerId = providerId,
+ section = SyncRepairSection.LIVE
+ )
+ if (live is Result.Error) return live
+ if (live is Result.Loading) return Result.error("Stalker live bootstrap resume did not complete")
+ if ((syncMetadataRepository.getMetadata(providerId)?.lastLiveSuccess ?: 0L) <= 0L) {
+ return Result.error("Stalker live bootstrap resume did not complete")
+ }
}
- val live = syncSection(
- providerId = providerId,
- section = SyncRepairSection.LIVE
- )
- if (live is Result.Error) return live
- if (live is Result.Loading) return Result.error("Stalker live bootstrap resume did not complete")
+ withStalkerSummaryLock(providerId, ContentType.MOVIE) {
+ warmStalkerCategoryHeads(provider)
+ }
+
+ // Live is complete before the page-one VOD sweep. Start the full VOD walk only after
+ // these category heads are committed, so they are immediately usable and Live setup
+ // does not compete with the catalog crawl.
+ scheduleStalkerIndexSync(providerId, section = null, force = false)
when (provider.epgSyncMode) {
ProviderEpgSyncMode.UPFRONT -> return syncEpg(providerId = providerId, force = true, onProgress = null)
@@ -1275,6 +1280,26 @@ class SyncManager @Inject constructor(
return Result.success(Unit)
}
+ /** Caller owns the provider lane; sections share wire admission but keep their own cursors. */
+ private suspend fun warmStalkerCategoryHeads(provider: Provider, sections: List<ContentType>? = null) {
+ val headSections = sections ?: if (provider.catalogLayout == CatalogLayout.UNIFIED_VOD) {
+ listOf(ContentType.MOVIE)
+ } else listOf(ContentType.MOVIE, ContentType.SERIES)
+ coroutineScope {
+ headSections.map { contentType -> async {
+ withStalkerIndexSectionLock(provider.id, contentType) {
+ runSuspendCatching {
+ stalkerIncrementalIndexExecutor.processCategoryHeads(
+ provider, createStalkerSyncProvider(provider), contentType, null
+ )
+ }.exceptionOrNull()?.let { error ->
+ Log.w(TAG, "Initial Stalker ${contentType.name} category heads failed for provider ${provider.id}: ${sanitizeThrowableMessage(error)}")
+ }
+ }
+ } }.awaitAll()
+ }
+ }
+
private suspend fun syncProviderInternal(
providerId: Long,
force: Boolean,
@@ -1759,7 +1784,9 @@ class SyncManager @Inject constructor(
force: Boolean,
maxCategoriesPerSection: Int?,
onProgress: ((String) -> Unit)?,
- maxPagesPerSection: Int?
+ maxPagesPerSection: Int?,
+ categoryHeadsOnly: Boolean,
+ scheduleContinuation: Boolean
): com.streamvault.domain.model.Result<Unit> {
return withStalkerSummaryLock(providerId, section) lock@{
val playbackDelayMillis = if (StalkerTrafficCoordinator.shouldDeferIndexForPlayback(providerId)) 2_000L else 0L
@@ -1787,6 +1814,15 @@ class SyncManager @Inject constructor(
when { it.movies && !it.series -> ContentType.MOVIE; it.series && !it.movies -> ContentType.SERIES; else -> null }
}
val api = createStalkerSyncProvider(provider)
+ if (BuildConfig.STALKER_ADAPTIVE_INDEXING && !categoryHeadsOnly && automaticPolicy?.enabled != false) {
+ val sections = listOf(ContentType.MOVIE, ContentType.SERIES).filter { type ->
+ (type != ContentType.SERIES || provider.catalogLayout != CatalogLayout.UNIFIED_VOD) &&
+ (automaticPolicy == null || if (type == ContentType.MOVIE) automaticPolicy.movies else automaticPolicy.series)
+ }
+ // Recovery may run before the bootstrap continuation. Finish all missing heads
+ // before any category starts its remaining-page crawl.
+ warmStalkerCategoryHeads(provider, sections)
+ }
val decision = stalkerIndexContinuationCoordinator.chooseNextSection(
provider = provider,
api = api,
@@ -1810,6 +1846,10 @@ class SyncManager @Inject constructor(
}
return@lock com.streamvault.domain.model.Result.success(Unit)
}
+ if (categoryHeadsOnly && section != null && contentType != section) {
+ Log.i(TAG, "Stalker category-head warmup skipped ${contentType.name} for provider $providerId while ${section.name} was requested.")
+ return@lock com.streamvault.domain.model.Result.success(Unit)
+ }
Log.i(TAG, "Stalker catalog worker selected ${contentType.name} for provider $providerId: ${decision.reason}")
val warnings = mutableListOf<String>()
@@ -1817,14 +1857,23 @@ class SyncManager @Inject constructor(
var sawRetryableFailure = false
val categoryLimit = maxCategoriesPerSection?.coerceAtLeast(1) ?: STALKER_INDEX_CATEGORY_SLICE_SIZE
val failure = runSuspendCatching {
- stalkerIncrementalIndexExecutor.processSummary(
- provider = provider,
- api = api,
- contentType = contentType,
- maxCategories = categoryLimit,
- onProgress = onProgress,
- maxPages = maxPagesPerSection
- )
+ if (categoryHeadsOnly) {
+ stalkerIncrementalIndexExecutor.processCategoryHeads(
+ provider = provider,
+ api = api,
+ contentType = contentType,
+ onProgress = onProgress
+ )
+ } else {
+ stalkerIncrementalIndexExecutor.processSummary(
+ provider = provider,
+ api = api,
+ contentType = contentType,
+ maxCategories = categoryLimit,
+ onProgress = onProgress,
+ maxPages = maxPagesPerSection
+ )
+ }
}.exceptionOrNull()
if (failure != null) {
val failedAt = System.currentTimeMillis()
@@ -1848,8 +1897,10 @@ class SyncManager @Inject constructor(
val exception = if (sawRetryableFailure) IOException(message, cause) else IllegalStateException(message, cause)
com.streamvault.domain.model.Result.error(warnings.first(), exception)
} else {
- stalkerIndexContinuationCoordinator.scheduleNextSection(provider, api)
- stalkerIndexContinuationCoordinator.scheduleEpgIfCatalogIdle(provider)
+ if (scheduleContinuation) {
+ stalkerIndexContinuationCoordinator.scheduleNextSection(provider, api)
+ stalkerIndexContinuationCoordinator.scheduleEpgIfCatalogIdle(provider)
+ }
com.streamvault.domain.model.Result.success(Unit)
}
result.alsoApplyPendingRestore(providerId)
@@ -1866,12 +1917,16 @@ class SyncManager @Inject constructor(
): Result<com.streamvault.data.remote.stalker.StalkerPagedResult<out Any>> {
suspend fun fetch(): Result<com.streamvault.data.remote.stalker.StalkerPagedResult<out Any>> {
if (contentType == ContentType.MOVIE && provider.catalogLayout == CatalogLayout.UNIFIED_VOD) {
- return api.getUnifiedVodPage(categoryId, page)
+ return if (BuildConfig.STALKER_ADAPTIVE_INDEXING) {
+ api.getUnifiedVodPage(categoryId, page, sortBy = StalkerProvider.WILDCARD_INDEX_SORT)
+ } else api.getUnifiedVodPage(categoryId, page)
}
if (splitVod && contentType == ContentType.MOVIE) {
val seriesCategoryId = api.projectVodCategoryToSeries(categoryId)
?: return Result.error("Unable to resolve VOD-derived Series category")
- return api.getSplitVodPage(categoryId, seriesCategoryId, page)
+ return if (BuildConfig.STALKER_ADAPTIVE_INDEXING) {
+ api.getSplitVodPage(categoryId, seriesCategoryId, page, sortBy = StalkerProvider.WILDCARD_INDEX_SORT)
+ } else api.getSplitVodPage(categoryId, seriesCategoryId, page)
}
return fetchStalkerSummaryPage(api, contentType, categoryId, page)
}
@@ -1969,8 +2024,12 @@ class SyncManager @Inject constructor(
categoryId: Long,
page: Int
): Result<com.streamvault.data.remote.stalker.StalkerPagedResult<out Any>> = when (contentType) {
- ContentType.MOVIE -> api.getVodStreamsPage(categoryId, page)
- ContentType.SERIES -> api.getSeriesListPage(categoryId, page)
+ ContentType.MOVIE -> if (BuildConfig.STALKER_ADAPTIVE_INDEXING) {
+ api.getVodStreamsPage(categoryId, page, sortBy = StalkerProvider.WILDCARD_INDEX_SORT)
+ } else api.getVodStreamsPage(categoryId, page)
+ ContentType.SERIES -> if (BuildConfig.STALKER_ADAPTIVE_INDEXING) {
+ api.getSeriesListPage(categoryId, page, sortBy = StalkerProvider.WILDCARD_INDEX_SORT)
+ } else api.getSeriesListPage(categoryId, page)
else -> Result.error("Unsupported Stalker summary page section: $contentType")
}
diff --git a/data/src/test/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiServiceTest.kt b/data/src/test/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiServiceTest.kt
index 1bece290..44afada0 100644
--- a/data/src/test/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiServiceTest.kt
+++ b/data/src/test/java/com/streamvault/data/remote/stalker/OkHttpStalkerApiServiceTest.kt
@@ -1994,6 +1994,26 @@ class OkHttpStalkerApiServiceTest {
assertThat(requestedSortBy).isEqualTo("added")
}
+ @Test
+ fun getSeriesPage_ordersIndexByAddedAndKeepsInteractiveDefault() = runTest {
+ val requestedSorts = mutableListOf<String?>()
+ val service = OkHttpStalkerApiService(
+ okHttpClient = OkHttpClient.Builder().addInterceptor { chain ->
+ requestedSorts += chain.request().url.queryParameter("sortby")
+ Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1)
+ .code(200).message("OK")
+ .body("""{"js":{"total_items":1,"max_page_items":14,"data":[{"id":"1","name":"Series"}]}}"""
+ .toResponseBody("application/json".toMediaType())).build()
+ }.build(),
+ json = Json { ignoreUnknownKeys = true }
+ )
+ assertThat(service.getSeriesPage(stalkerSession(), stalkerProfile(), "42", 1, "added"))
+ .isInstanceOf(Result.Success::class.java)
+ assertThat(service.getSeriesPage(stalkerSession(), stalkerProfile(), "42", 1))
+ .isInstanceOf(Result.Success::class.java)
+ assertThat(requestedSorts).containsExactly("added", null).inOrder()
+ }
+
@Test
fun getVodStreamsPage_omits_sortby_when_not_requested() = runTest {
var requestedSortBy: String? = "unset"
diff --git a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicyTest.kt b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicyTest.kt
index 7b26d3af..6f9b1cc7 100644
--- a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicyTest.kt
+++ b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerAdaptiveIndexPolicyTest.kt
@@ -5,36 +5,37 @@ import org.junit.Test
class StalkerAdaptiveIndexPolicyTest {
@Test
- fun healthyRampRequiresBothSamplesAndTimeAndStopsAtTwoRequestsPerSecond() {
+ fun twentySuccessfulResponsesAddOneSlotUpToEight() {
val policy = StalkerAdaptiveIndexPolicy()
- repeat(100) { policy.success(0L, 100L, false) }
- assertThat(policy.intervalMillis).isEqualTo(5_000L)
- policy.success(15_000L, 100L, false)
- assertThat(policy.intervalMillis).isEqualTo(3_750L)
- repeat(1_000) { policy.success(30_000L + it * 1_000L, 100L, false) }
- assertThat(policy.intervalMillis).isEqualTo(500L)
+ assertThat(policy.concurrency).isEqualTo(3)
+ repeat(19) { policy.success(false) }
+ assertThat(policy.concurrency).isEqualTo(3)
+ policy.success(false)
+ assertThat(policy.concurrency).isEqualTo(4)
+ repeat(1_000) { policy.success(false) }
+ assertThat(policy.concurrency).isEqualTo(8)
}
@Test
- fun stressPermanentlyLowersLearnedCeilingAndBoundsRepeatedBackoff() {
- val policy = StalkerAdaptiveIndexPolicy(1_000L)
+ fun stressHalvesWindowToOneAndHealthyResponsesCanRecover() {
+ val policy = StalkerAdaptiveIndexPolicy(8)
policy.stress()
- repeat(1_000) { policy.success(it * 2_000L, 100L, false) }
- assertThat(policy.intervalMillis).isEqualTo(2_000L)
- assertThat(policy.minimumIntervalMillis).isEqualTo(2_000L)
+ assertThat(policy.concurrency).isEqualTo(4)
repeat(100) { policy.stress() }
- assertThat(policy.intervalMillis).isEqualTo(30_000L)
+ assertThat(policy.concurrency).isEqualTo(1)
+ repeat(20) { policy.success(false) }
+ assertThat(policy.concurrency).isEqualTo(2)
}
@Test
- fun playbackAndFailuresBreakHealthyStreakAndSlowResponsesBackOff() {
+ fun playbackAndFailuresBreakHealthyStreak() {
val policy = StalkerAdaptiveIndexPolicy()
- repeat(11) { policy.success(it * 2_000L, 100L, false) }
- policy.success(30_000L, 100L, true)
- policy.success(32_000L, 100L, false)
- assertThat(policy.intervalMillis).isEqualTo(5_000L)
+ repeat(19) { policy.success(false) }
+ policy.success(true)
+ policy.success(false)
+ assertThat(policy.concurrency).isEqualTo(3)
policy.resetHealth()
- policy.success(60_000L, 2_000L, false)
- assertThat(policy.intervalMillis).isEqualTo(10_000L)
+ repeat(19) { policy.success(false) }
+ assertThat(policy.concurrency).isEqualTo(3)
}
}
diff --git a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerPortalStateStoreTest.kt b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerPortalStateStoreTest.kt
index 0bf08e7c..ad9a9464 100644
--- a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerPortalStateStoreTest.kt
+++ b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerPortalStateStoreTest.kt
@@ -21,46 +21,50 @@ class StalkerPortalStateStoreTest {
fun `first run adopts learning generation created after handshake`() = runTest {
val dao = FakePortalStateDao()
val store = StalkerPortalStateStore(dao, TestCredentialCrypto())
- val coordinator = StalkerRequestCoordinator(store, true) { testScheduler.currentTime }
+ val coordinator = StalkerRequestCoordinator(store, true, { testScheduler.currentTime }, 8)
coordinator.acquireNetworkPermit(83L, StalkerNetworkPriority.FOREGROUND).also {
coordinator.releaseNetworkPermit(it)
}
dao.upsert(StalkerPortalStateEntity(providerId = 83L, configurationGeneration = 9L))
val page = coordinator.acquireNetworkPermit(83L, StalkerNetworkPriority.INDEX)
coordinator.recordIndexResponse(page, java.net.SocketTimeoutException())
+ coordinator.releaseNetworkPermit(page)
val persisted = requireNotNull(store.get(83L))
assertThat(persisted.configurationGeneration).isEqualTo(9L)
- assertThat(store.indexPacing(persisted).intervalMillis).isEqualTo(10_000L)
+ assertThat(store.indexPacing(persisted).concurrency).isEqualTo(1)
}
@Test
- fun `learned index interval and stress ceiling survive coordinator restart`() = runTest {
+ fun `stressed window survives restart and configuration changes reset learning`() = runTest {
val dao = FakePortalStateDao()
val store = StalkerPortalStateStore(dao, TestCredentialCrypto())
dao.upsert(StalkerPortalStateEntity(providerId = 82L, configurationGeneration = 7L))
- val firstCoordinator = StalkerRequestCoordinator(store, true) { testScheduler.currentTime }
+ val firstCoordinator = StalkerRequestCoordinator(store, true, { testScheduler.currentTime }, 8)
val permit = firstCoordinator.acquireNetworkPermit(82L, StalkerNetworkPriority.INDEX)
firstCoordinator.recordIndexResponse(permit, java.net.SocketTimeoutException())
- val learned = store.indexPacing(requireNotNull(store.get(82L)))
- assertThat(learned.intervalMillis).isEqualTo(10_000L)
- assertThat(learned.minimumIntervalMillis).isEqualTo(10_000L)
-
- val restarted = StalkerRequestCoordinator(store, true) { testScheduler.currentTime }
- val first = restarted.acquireNetworkPermit(82L, StalkerNetworkPriority.INDEX)
- val second = restarted.acquireNetworkPermit(82L, StalkerNetworkPriority.INDEX)
- assertThat(second.admittedAt - first.admittedAt).isEqualTo(10_000L)
- // A changed configuration cannot inherit pacing from the old generation.
+ firstCoordinator.releaseNetworkPermit(permit)
+ val restarted = StalkerRequestCoordinator(store, true, { testScheduler.currentTime }, 8)
+ assertThat(restarted.effectiveIndexConcurrencyLimit(82L)).isEqualTo(1)
val changed = requireNotNull(store.get(82L)).copy(configurationGeneration = 8L)
- assertThat(store.indexPacing(changed).intervalMillis).isEqualTo(5_000L)
+ assertThat(store.indexPacing(changed).concurrency).isEqualTo(3)
}
@Test
- fun `malformed pacing falls back safely and valid values are bounded`() = runTest {
+ fun `restart caps healthy learned window at three and ignores old timed profiles`() = runTest {
val store = StalkerPortalStateStore(FakePortalStateDao(), TestCredentialCrypto())
- assertThat(store.indexPacing(StalkerPortalStateEntity(1L, learningJson = "bad")).intervalMillis)
- .isEqualTo(5_000L)
- store.recordIndexPacing(1L, 3L, 1L, 1L)
- assertThat(store.indexPacing(requireNotNull(store.get(1L))).intervalMillis).isEqualTo(500L)
+ store.recordIndexPacing(84L, configurationGeneration = 4L, concurrency = 8)
+ assertThat(store.indexPacing(requireNotNull(store.get(84L))).concurrency).isEqualTo(3)
+ assertThat(store.indexPacing(StalkerPortalStateEntity(85L,
+ learningJson = """{"indexPacing":{"intervalMillis":30000}}""")).concurrency).isEqualTo(3)
+ }
+
+ @Test
+ fun `malformed concurrency falls back safely and learned values are bounded`() = runTest {
+ val store = StalkerPortalStateStore(FakePortalStateDao(), TestCredentialCrypto())
+ assertThat(store.indexPacing(StalkerPortalStateEntity(1L, learningJson = "bad")).concurrency)
+ .isEqualTo(3)
+ store.recordIndexPacing(1L, 3L, 0)
+ assertThat(store.indexPacing(requireNotNull(store.get(1L))).concurrency).isEqualTo(1)
}
@Test
diff --git a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerProviderTest.kt b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerProviderTest.kt
index e1a96b60..dfa6c11c 100644
--- a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerProviderTest.kt
+++ b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerProviderTest.kt
@@ -22,6 +22,8 @@ import com.streamvault.domain.model.StalkerTransportMode
import com.streamvault.domain.model.StalkerTransportOrigin
import java.time.Instant
import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
import org.junit.Before
import org.junit.Test
import java.util.Base64
@@ -40,6 +42,37 @@ class StalkerProviderTest {
resolvedCategoryHandoffKey = key
)
+ @Test
+ fun coldUnifiedCategoryLookupsLoadTheCorrectCacheOnlyOnce() = runTest {
+ val api = FakeStalkerApiService(
+ profile = StalkerProviderProfile(accountName = "Room"),
+ vodCategories = listOf(StalkerCategoryRecord("10", "Mixed"))
+ )
+ val source = categoryReuseProvider(api)
+ val category = (source.getUnifiedVodCategories() as Result.Success).data.single()
+ val before = api.vodCategoryCalls
+ val cold = categoryReuseProvider(api)
+ (1..24).map { async { cold.isWildcardCategory(ContentType.VOD, category.id) } }.awaitAll()
+ assertThat(api.vodCategoryCalls - before).isEqualTo(1)
+ cold.isWildcardCategory(ContentType.VOD, category.id)
+ assertThat(api.vodCategoryCalls - before).isEqualTo(1)
+ }
+
+ @Test
+ fun projectedCategoryDoesNotHideTheRestOfTheMovieCategoryList() = runTest {
+ val api = FakeStalkerApiService(
+ profile = StalkerProviderProfile(accountName = "Room"),
+ seriesCategories = listOf(StalkerCategoryRecord("10", "Series")),
+ vodCategories = listOf(StalkerCategoryRecord("10", "Mixed"), StalkerCategoryRecord("11", "Movies"))
+ )
+ val source = categoryReuseProvider(api)
+ val remaining = (source.getVodCategories() as Result.Success).data.single { it.name == "Movies" }
+ val target = categoryReuseProvider(api)
+ val series = (target.getSeriesCategories() as Result.Success).data.single()
+ target.projectSeriesCategoryToVod(series.id)
+ assertThat(target.projectVodCategoryToSeries(remaining.id)).isNotNull()
+ }
+
@Test
fun layoutDetectionCategoriesAreConsumedOnceWithoutRepeatingDiscovery() = runTest {
val api = FakeStalkerApiService(
diff --git a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinatorTest.kt b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinatorTest.kt
index 60276053..29411972 100644
--- a/data/src/test/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinatorTest.kt
+++ b/data/src/test/java/com/streamvault/data/remote/stalker/StalkerRequestCoordinatorTest.kt
@@ -57,9 +57,9 @@ class StalkerRequestCoordinatorTest {
val background = async {
coordinator.execute(79L, StalkerRequestPriority.BACKGROUND_INDEX,
StalkerRequestDescriptor("MOVIE", "PAGE", page = 1)) {
- coordinator.acquireNetworkPermit(79L, StalkerNetworkPriority.INDEX)
+ coordinator.acquireNetworkPermit(79L, StalkerNetworkPriority.INDEX).also { coordinator.releaseNetworkPermit(it) }
// The page-base probe's second HTTP call waits inside the same logical page.
- coordinator.acquireNetworkPermit(79L, StalkerNetworkPriority.INDEX)
+ coordinator.acquireNetworkPermit(79L, StalkerNetworkPriority.INDEX).also { coordinator.releaseNetworkPermit(it) }
}
}
runCurrent()
@@ -87,37 +87,46 @@ class StalkerRequestCoordinatorTest {
}
@Test
- fun healthyIndexWalkRampsToTwoPerSecondWithoutTokenBucketBottleneck() = runTest {
- val coordinator = StalkerRequestCoordinator(null, true) { testScheduler.currentTime }
- val times = (1..715).map {
+ fun movieAndSeriesShareThreeWireSlotsAndGrowToEightWithoutTimedSpacing() = runTest {
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
+ val held = (1..3).map { coordinator.acquireNetworkPermit(71L, StalkerNetworkPriority.INDEX) }
+ val fourth = async { coordinator.acquireNetworkPermit(71L, StalkerNetworkPriority.INDEX) }
+ runCurrent()
+ assertThat(fourth.isCompleted).isFalse()
+ coordinator.releaseNetworkPermit(held.first())
+ testScheduler.advanceTimeBy(100)
+ runCurrent()
+ coordinator.releaseNetworkPermit(fourth.await())
+ held.drop(1).forEach { coordinator.releaseNetworkPermit(it) }
+ repeat(100) {
val permit = coordinator.acquireNetworkPermit(71L, StalkerNetworkPriority.INDEX)
coordinator.recordIndexResponse(permit)
- testScheduler.currentTime
+ coordinator.releaseNetworkPermit(permit)
}
- assertThat(times[1] - times[0]).isEqualTo(5_000L)
- assertThat(times.zipWithNext { a, b -> b - a }.minOrNull()).isEqualTo(500L)
- assertThat(times.takeLast(100).zipWithNext { a, b -> b - a }.toSet()).containsExactly(500L)
- // Virtual admission benchmark, excluding transport/DB work: 715 pages / 10k rows.
- assertThat(times.last()).isLessThan(600_000L)
- println("Stalker virtual admission benchmark: pages=715 elapsedMillis=${times.last()} tailIntervalMillis=500")
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(71L)).isEqualTo(8)
+ val eight = (1..8).map { coordinator.acquireNetworkPermit(71L, StalkerNetworkPriority.INDEX) }
+ assertThat(eight.map { it.admittedAt }.distinct()).hasSize(1)
+ val ninth = async { coordinator.acquireNetworkPermit(71L, StalkerNetworkPriority.INDEX) }
+ runCurrent()
+ assertThat(ninth.isCompleted).isFalse()
+ ninth.cancel(); ninth.join()
+ eight.forEach { coordinator.releaseNetworkPermit(it) }
}
@Test
- fun stressSlowsOnlyItsProviderAndCannotRampAbovePreStressRate() = runTest {
- val coordinator = StalkerRequestCoordinator(null, true) { testScheduler.currentTime }
- val first = coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX)
- coordinator.recordIndexResponse(first, StalkerApiError.Server("busy", httpStatus = 503))
- val second = coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX)
- assertThat(second.admittedAt - first.admittedAt).isEqualTo(10_000L)
- repeat(60) {
- coordinator.recordIndexResponse(coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX))
+ fun stressHalvesOnlyItsProvidersWindowAndSuccessCanRelearn() = runTest {
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
+ val permit = coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX)
+ coordinator.recordIndexResponse(permit, StalkerApiError.Server("busy", httpStatus = 500))
+ coordinator.releaseNetworkPermit(permit)
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(72L)).isEqualTo(1)
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(73L)).isEqualTo(3)
+ repeat(20) {
+ val next = coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX)
+ coordinator.recordIndexResponse(next)
+ coordinator.releaseNetworkPermit(next)
}
- val before = testScheduler.currentTime
- coordinator.acquireNetworkPermit(72L, StalkerNetworkPriority.INDEX)
- assertThat(testScheduler.currentTime - before).isEqualTo(10_000L)
- val other = coordinator.acquireNetworkPermit(73L, StalkerNetworkPriority.INDEX)
- val otherNext = coordinator.acquireNetworkPermit(73L, StalkerNetworkPriority.INDEX)
- assertThat(otherNext.admittedAt - other.admittedAt).isEqualTo(5_000L)
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(72L)).isEqualTo(2)
}
@Test
@@ -126,6 +135,7 @@ class StalkerRequestCoordinatorTest {
StalkerTrafficCoordinator.notePlaybackStarted(74L)
try {
val first = coordinator.acquireNetworkPermit(74L, StalkerNetworkPriority.INDEX)
+ coordinator.releaseNetworkPermit(first)
val next = async { coordinator.acquireNetworkPermit(74L, StalkerNetworkPriority.INDEX) }
runCurrent()
val interactive = coordinator.acquireNetworkPermit(74L, StalkerNetworkPriority.INTERACTIVE)
@@ -135,13 +145,15 @@ class StalkerRequestCoordinatorTest {
assertThat(next.isCompleted).isFalse()
coordinator.releaseNetworkPermit(interactive)
advanceUntilIdle()
- assertThat(next.await().admittedAt - first.admittedAt).isAtLeast(20_000L)
+ val second = next.await()
+ assertThat(second.admittedAt - first.admittedAt).isAtLeast(20_000L)
+ coordinator.releaseNetworkPermit(second)
} finally { StalkerTrafficCoordinator.forgetProvider(74L) }
}
@Test
- fun foregroundTrafficYieldsNoIndexCreditAndItsStressSlowsTheIndex() = runTest {
- val coordinator = StalkerRequestCoordinator(null, true) { testScheduler.currentTime }
+ fun foregroundStressShrinksIndexAndBlocksNewPagesUntilForegroundCompletes() = runTest {
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
val foreground = coordinator.acquireNetworkPermit(77L, StalkerNetworkPriority.FOREGROUND)
val background = async { coordinator.acquireNetworkPermit(77L, StalkerNetworkPriority.INDEX) }
runCurrent()
@@ -149,15 +161,13 @@ class StalkerRequestCoordinatorTest {
coordinator.recordIndexResponse(foreground, java.net.SocketTimeoutException())
coordinator.releaseNetworkPermit(foreground)
advanceUntilIdle()
- val first = background.await()
- val second = coordinator.acquireNetworkPermit(77L, StalkerNetworkPriority.INDEX)
- assertThat(second.admittedAt - first.admittedAt).isEqualTo(10_000L)
+ coordinator.releaseNetworkPermit(background.await())
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(77L)).isEqualTo(1)
}
@Test
- fun cancelledInteractiveWaiterDoesNotStarveIndex() = runTest {
+ fun cancelledInteractiveWaiterDoesNotStarveIndexOrSpendForegroundCredits() = runTest {
val coordinator = StalkerRequestCoordinator(null, true) { testScheduler.currentTime }
- // Drain foreground credits, then exceed interactive borrowing so its next permit queues.
repeat(8) { coordinator.acquireNetworkPermit(78L, StalkerNetworkPriority.FOREGROUND).also {
coordinator.releaseNetworkPermit(it)
} }
@@ -166,10 +176,10 @@ class StalkerRequestCoordinatorTest {
} }
val interactive = async { coordinator.acquireNetworkPermit(78L, StalkerNetworkPriority.INTERACTIVE) }
runCurrent()
- interactive.cancel()
- interactive.join()
+ interactive.cancel(); interactive.join()
val index = coordinator.acquireNetworkPermit(78L, StalkerNetworkPriority.INDEX)
- assertThat(index.admittedAt).isEqualTo(3_000L)
+ assertThat(index.admittedAt).isEqualTo(0L)
+ coordinator.releaseNetworkPermit(index)
}
@Test
@@ -183,11 +193,12 @@ class StalkerRequestCoordinatorTest {
@Test
fun retryAfterBlocksAllLanesAndAllowsExactlyOneHalfOpenProbe() = runTest {
- val coordinator = StalkerRequestCoordinator(null, true) { testScheduler.currentTime }
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
val first = coordinator.acquireNetworkPermit(76L, StalkerNetworkPriority.INDEX)
val error = StalkerApiError.RateLimited(retryAfterMillis = 1_200_000L)
coordinator.recordIndexResponse(first, error)
coordinator.recordNetworkFailure(first, error)
+ coordinator.releaseNetworkPermit(first)
repeat(3) {
try {
coordinator.acquireNetworkPermit(76L, StalkerNetworkPriority.INTERACTIVE)
@@ -207,8 +218,53 @@ class StalkerRequestCoordinatorTest {
}
coordinator.recordNetworkSuccess(probe)
coordinator.recordIndexResponse(probe)
+ coordinator.releaseNetworkPermit(probe)
+ assertThat(coordinator.effectiveIndexConcurrencyLimit(76L)).isEqualTo(1)
val next = coordinator.acquireNetworkPermit(76L, StalkerNetworkPriority.INDEX)
- assertThat(next.admittedAt - probe.admittedAt).isEqualTo(10_000L)
+ assertThat(next.admittedAt).isEqualTo(probe.admittedAt)
+ coordinator.releaseNetworkPermit(next)
+ }
+
+ @Test
+ fun fullIndexWindowLeavesMetadataSlotForInteractiveWork() = runTest {
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
+ val finish = CompletableDeferred<Unit>()
+ val pages = (1..3).map { page -> async {
+ coordinator.execute(81L, StalkerRequestPriority.BACKGROUND_INDEX,
+ StalkerRequestDescriptor("MOVIE", "PAGE", page = page)) { finish.await() }
+ } }
+ runCurrent()
+ assertThat(coordinator.snapshot(81L).active).isEqualTo(3)
+ val interactive = async {
+ coordinator.execute(81L, StalkerRequestPriority.OPEN_CATEGORY,
+ StalkerRequestDescriptor("SERIES", "CATEGORY")) { "ready" }
+ }
+ runCurrent()
+ assertThat(interactive.await()).isEqualTo("ready")
+ finish.complete(Unit)
+ pages.awaitAll()
+ }
+
+ @Test
+ fun serverRetryAfterPausesEveryLaneAndFailedProbeKeepsCircuitClosed() = runTest {
+ val coordinator = StalkerRequestCoordinator(null, true, { testScheduler.currentTime }, 8)
+ val first = coordinator.acquireNetworkPermit(82L, StalkerNetworkPriority.INDEX)
+ val error = StalkerApiError.Server("busy", httpStatus = 503, retryAfterMillis = 600_000L)
+ coordinator.recordIndexResponse(first, error)
+ coordinator.recordNetworkFailure(first, error)
+ coordinator.releaseNetworkPermit(first)
+ val rejected = runCatching {
+ coordinator.acquireNetworkPermit(82L, StalkerNetworkPriority.INTERACTIVE)
+ }.exceptionOrNull() as StalkerApiError.RateLimited
+ assertThat(rejected.retryAfterMillis).isEqualTo(600_000L)
+ testScheduler.advanceTimeBy(600_000L)
+ val probe = coordinator.acquireNetworkPermit(82L, StalkerNetworkPriority.INDEX)
+ coordinator.recordNetworkFailure(probe, StalkerApiError.Server("busy", httpStatus = 503))
+ coordinator.releaseNetworkPermit(probe)
+ val stillClosed = runCatching {
+ coordinator.acquireNetworkPermit(82L, StalkerNetworkPriority.FOREGROUND)
+ }.exceptionOrNull() as StalkerApiError.RateLimited
+ assertThat(stillClosed.retryAfterMillis).isEqualTo(300_000L)
}
@Test
diff --git a/data/src/test/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutorTest.kt b/data/src/test/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutorTest.kt
index 374449c4..65b60c13 100644
--- a/data/src/test/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutorTest.kt
+++ b/data/src/test/java/com/streamvault/data/sync/StalkerIncrementalIndexExecutorTest.kt
@@ -24,9 +24,10 @@ class StalkerIncrementalIndexExecutorTest {
val wildcard = CategoryEntity(categoryId = -9L, name = "All", type = ContentType.MOVIE, providerId = 91L)
val operations = WildcardIndexOperations(wildcard)
val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
whenever(api.isWildcardCategory(ContentType.MOVIE, wildcard.categoryId)).thenReturn(true)
- StalkerIncrementalIndexExecutor(operations, configuredWildcardPageConcurrency = 2)
+ StalkerIncrementalIndexExecutor(operations, configuredWildcardPageConcurrency = 2, adaptiveCategoryPaging = false)
.processSummary(
provider = LegacyProvider(
id = 91L,
@@ -54,8 +55,9 @@ class StalkerIncrementalIndexExecutorTest {
val wildcard = CategoryEntity(categoryId = -9L, name = "All", type = ContentType.MOVIE, providerId = 91L)
val operations = WildcardIndexOperations(wildcard, failPageOneOnce = true)
val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
whenever(api.isWildcardCategory(ContentType.MOVIE, wildcard.categoryId)).thenReturn(true)
- val executor = StalkerIncrementalIndexExecutor(operations, configuredWildcardPageConcurrency = 2)
+ val executor = StalkerIncrementalIndexExecutor(operations, configuredWildcardPageConcurrency = 2, adaptiveCategoryPaging = false)
val provider = LegacyProvider(
id = 91L,
name = "Portal",
@@ -78,14 +80,123 @@ class StalkerIncrementalIndexExecutorTest {
assertThat(operations.indexedMovies.map(Movie::id)).containsExactly(1L, 2L).inOrder()
}
+ @Test
+ fun categoryHeadSweepFetchesPageOneForEveryVisibleCategoryWithoutWildcardWalk() = runTest {
+ val wildcard = CategoryEntity(categoryId = -9L, name = "All", type = ContentType.MOVIE, providerId = 91L)
+ val action = CategoryEntity(categoryId = 10L, name = "Action", type = ContentType.MOVIE, providerId = 91L)
+ val drama = CategoryEntity(categoryId = 20L, name = "Drama", type = ContentType.MOVIE, providerId = 91L)
+ val operations = WildcardIndexOperations(
+ wildcard,
+ categories = listOf(wildcard, action, drama),
+ categoryConcurrency = 1
+ )
+ val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
+ whenever(api.isWildcardCategory(ContentType.MOVIE, wildcard.categoryId)).thenReturn(true)
+
+ StalkerIncrementalIndexExecutor(operations, configuredWildcardPageConcurrency = 2, adaptiveCategoryPaging = false)
+ .processCategoryHeads(
+ provider = LegacyProvider(
+ id = 91L,
+ name = "Portal",
+ type = ProviderType.STALKER_PORTAL,
+ serverUrl = "https://portal.invalid",
+ catalogLayout = CatalogLayout.SPLIT
+ ),
+ api = api,
+ contentType = ContentType.MOVIE,
+ onProgress = null
+ )
+
+ assertThat(operations.summaryFetches).containsExactly(10L to 1, 20L to 1).inOrder()
+ assertThat(operations.maxConcurrentFetches).isEqualTo(2)
+ assertThat(operations.completedFetchPages).isEmpty()
+ assertThat(operations.categoryHydration(10L)?.lastSuccessfulPage).isEqualTo(1)
+ assertThat(operations.categoryHydration(20L)?.lastSuccessfulPage).isEqualTo(1)
+ assertThat(operations.indexedMovies).hasSize(2)
+ }
+
+ @Test
+ fun adaptiveHeadsProbeOnceThenFetchEveryCategoryHeadAndResumeAtPageTwo() = runTest {
+ val wildcard = CategoryEntity(categoryId = -9L, name = "All", type = ContentType.MOVIE, providerId = 91L)
+ val categories = (1L..24L).map { CategoryEntity(categoryId = it, name = "Category $it", type = ContentType.MOVIE, providerId = 91L) }
+ val operations = WildcardIndexOperations(wildcard, categories = listOf(wildcard) + categories,
+ singlePageProbe = true, summaryPageCount = 9)
+ val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
+ whenever(api.isWildcardCategory(ContentType.MOVIE, wildcard.categoryId)).thenReturn(true)
+ val executor = StalkerIncrementalIndexExecutor(operations, 8, adaptiveCategoryPaging = true)
+ val provider = LegacyProvider(91L, "Portal", ProviderType.STALKER_PORTAL,
+ serverUrl = "https://portal.invalid", catalogLayout = CatalogLayout.SPLIT)
+
+ executor.processCategoryHeads(provider, api, ContentType.MOVIE, null)
+
+ assertThat(operations.completedFetchPages).containsExactly(1)
+ assertThat(operations.summaryFetches).containsExactlyElementsIn(categories.map { it.categoryId to 1 })
+ assertThat(operations.summaryFetchCountAtFirstCommit).isEqualTo(8)
+ assertThat(operations.maxConcurrentFetches).isEqualTo(8)
+ assertThat(operations.metadataPublications.any { (state, rows) -> state == "RUNNING" && rows in 2..24 }).isTrue()
+ categories.forEach { assertThat(operations.categoryHydration(it.categoryId)?.lastSuccessfulPage).isEqualTo(1) }
+ operations.summaryFetches.clear()
+ executor.processCategoryHeads(provider, api, ContentType.MOVIE, null)
+ assertThat(operations.summaryFetches).isEmpty()
+ assertThat(operations.completedFetchPages).containsExactly(1)
+ }
+
+ @Test
+ fun adaptiveSingleCategoryFillsEightPageWindowAndCommitsOutOfOrderRepliesInOrder() = runTest {
+ val category = CategoryEntity(categoryId = 10L, name = "Action", type = ContentType.MOVIE, providerId = 91L)
+ val operations = WildcardIndexOperations(category, summaryPageCount = 9)
+ val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
+ val executor = StalkerIncrementalIndexExecutor(operations, 8, adaptiveCategoryPaging = true)
+ val provider = LegacyProvider(91L, "Portal", ProviderType.STALKER_PORTAL, serverUrl = "https://portal.invalid")
+ executor.processCategoryHeads(provider, api, ContentType.MOVIE, null)
+ operations.committedPages.clear()
+
+ executor.processSummary(provider, api, ContentType.MOVIE, 1, null, maxPages = 8)
+
+ assertThat(operations.maxConcurrentFetches).isEqualTo(8)
+ assertThat(operations.summaryCompletions.takeLast(8).first()).isNotEqualTo(2)
+ assertThat(operations.committedPages).containsExactlyElementsIn(2..9).inOrder()
+ assertThat(operations.categoryHydration(10L)?.itemCount).isEqualTo(9)
+ assertThat(operations.categoryHydration(10L)?.isComplete).isTrue()
+ }
+
+ @Test
+ fun adaptiveCategoryWindowNeverCheckpointsPastAFailedPage() = runTest {
+ val category = CategoryEntity(categoryId = 10L, name = "Action", type = ContentType.MOVIE, providerId = 91L)
+ val operations = WildcardIndexOperations(category, summaryPageCount = 9,
+ summaryFailurePageOnce = 4, summaryFailuresRemaining = 2)
+ val api = mock<StalkerProvider>()
+ whenever(api.isWildcardCategory(org.mockito.kotlin.any(), org.mockito.kotlin.any())).thenReturn(false)
+ val executor = StalkerIncrementalIndexExecutor(operations, 8, adaptiveCategoryPaging = true)
+ val provider = LegacyProvider(91L, "Portal", ProviderType.STALKER_PORTAL, serverUrl = "https://portal.invalid")
+ executor.processCategoryHeads(provider, api, ContentType.MOVIE, null)
+ operations.committedPages.clear()
+ executor.processSummary(provider, api, ContentType.MOVIE, 1, null, maxPages = 8)
+ assertThat(operations.committedPages).containsExactly(2, 3).inOrder()
+ operations.committedPages.clear()
+ assertThat(operations.categoryHydration(10L)?.lastSuccessfulPage).isEqualTo(3)
+ executor.processSummary(provider, api, ContentType.MOVIE, 1, null, maxPages = 8)
+ assertThat(operations.committedPages).containsExactlyElementsIn(4..9).inOrder()
+ assertThat(operations.categoryHydration(10L)?.isComplete).isTrue()
+ }
+
private class WildcardIndexOperations(
private val wildcard: CategoryEntity,
- private var failPageOneOnce: Boolean = false
+ private var failPageOneOnce: Boolean = false,
+ private val categories: List<CategoryEntity> = listOf(wildcard),
+ categoryConcurrency: Int = 2,
+ private val singlePageProbe: Boolean = false,
+ private val summaryPageCount: Int = 2,
+ private var summaryFailurePageOnce: Int? = null,
+ private var summaryFailuresRemaining: Int = 1
) : StalkerIncrementalIndexOperations {
override val runtimeProfile = CatalogSyncRuntimeProfile(
tier = DeviceSyncTier.HIGH,
stageBatchSize = 100,
- maxCategoryConcurrency = 2,
+ maxCategoryConcurrency = categoryConcurrency,
preferSegmentedLiveOnboarding = false,
deferBackgroundWorkOnLowMemory = false,
snapshot = SyncDeviceSnapshot(
@@ -104,10 +215,15 @@ class StalkerIncrementalIndexExecutorTest {
var hydration: StalkerHydrationSnapshot? = null
private set
+ private val categoryHydrations = mutableMapOf<Long, StalkerHydrationSnapshot>()
val committedPages = mutableListOf<Int>()
val failedPages = mutableListOf<Int>()
val completedFetchPages = mutableListOf<Int>()
+ val summaryFetches = mutableListOf<Pair<Long, Int>>()
+ var summaryFetchCountAtFirstCommit: Int? = null
+ val summaryCompletions = mutableListOf<Int>()
val indexedMovies = mutableListOf<Movie>()
+ val metadataPublications = mutableListOf<Pair<String, Int>>()
var activeFetches = 0
private set
var maxConcurrentFetches = 0
@@ -115,13 +231,15 @@ class StalkerIncrementalIndexExecutorTest {
private val pageOneStarted = CompletableDeferred<Unit>()
private val pageTwoStarted = CompletableDeferred<Unit>()
- override suspend fun allVisibleCategories(providerId: Long, contentType: ContentType) = listOf(wildcard)
+ override suspend fun allVisibleCategories(providerId: Long, contentType: ContentType) = categories
override suspend fun visibleCategories(
contentType: ContentType,
categories: List<CategoryEntity>,
api: StalkerProvider
- ) = categories
+ ) = categories.filterNot { category ->
+ category.categoryId == wildcard.categoryId && categories.any { it.categoryId != wildcard.categoryId }
+ }
override suspend fun getJob(providerId: Long, contentType: ContentType): StalkerIndexJobEntity? = null
@@ -129,7 +247,8 @@ class StalkerIncrementalIndexExecutorTest {
providerId: Long,
contentType: ContentType,
categoryId: Long
- ): StalkerHydrationSnapshot? = hydration
+ ): StalkerHydrationSnapshot? = categoryHydrations[categoryId]
+ ?: hydration.takeIf { categoryId == wildcard.categoryId }
override suspend fun currentIndexedRowCount(providerId: Long, contentType: ContentType): Int = indexedMovies.size
@@ -143,7 +262,7 @@ class StalkerIncrementalIndexExecutorTest {
indexedRows: Int,
finalState: String,
now: Long
- ) = Unit
+ ) { metadataPublications += finalState to indexedRows }
override suspend fun fetchSummaryPage(
provider: LegacyProvider,
@@ -151,7 +270,30 @@ class StalkerIncrementalIndexExecutorTest {
contentType: ContentType,
categoryId: Long,
page: Int
- ): Result<StalkerPagedResult<out Any>> = Result.error("Unexpected category fetch")
+ ): Result<StalkerPagedResult<out Any>> {
+ summaryFetches += categoryId to page
+ activeFetches += 1
+ maxConcurrentFetches = maxOf(maxConcurrentFetches, activeFetches)
+ return try {
+ delay(if (page == 2) 200 else 20)
+ summaryCompletions += page
+ if (summaryFailurePageOnce == page && summaryFailuresRemaining-- > 0) {
+ return Result.error("Temporary page failure", java.io.IOException("reset"))
+ }
+ Result.success(
+ StalkerPagedResult(
+ items = listOf(Movie(id = categoryId * 100 + page, name = "Category $categoryId", streamId = categoryId * 100 + page)),
+ page = page,
+ totalPages = summaryPageCount,
+ pageSize = 1,
+ advertisedTotalItems = summaryPageCount,
+ advertisedTotalPages = summaryPageCount
+ )
+ )
+ } finally {
+ activeFetches -= 1
+ }
+ }
override suspend fun fetchWildcardPage(
provider: LegacyProvider,
@@ -163,7 +305,9 @@ class StalkerIncrementalIndexExecutorTest {
activeFetches += 1
maxConcurrentFetches = maxOf(maxConcurrentFetches, activeFetches)
try {
- if (page == 1) {
+ if (singlePageProbe) {
+ delay(20)
+ } else if (page == 1) {
pageOneStarted.complete(Unit)
pageTwoStarted.await()
delay(200)
@@ -220,7 +364,7 @@ class StalkerIncrementalIndexExecutorTest {
pageFingerprint: String?
) {
committedPages += attemptedPage
- this.hydration = StalkerHydrationSnapshot(
+ val snapshot = StalkerHydrationSnapshot(
lastHydratedAt = now,
itemCount = itemCount,
lastStatus = if (truncated) "TRUNCATED" else "SUCCESS",
@@ -238,6 +382,8 @@ class StalkerIncrementalIndexExecutorTest {
retryBudgetRemaining = 3,
lastPageFingerprint = pageFingerprint
)
+ categoryHydrations[categoryId] = snapshot
+ if (categoryId == wildcard.categoryId) this.hydration = snapshot
}
override suspend fun markAttemptFailed(
@@ -253,7 +399,7 @@ class StalkerIncrementalIndexExecutorTest {
) {
failedPages += attemptedPage
val previous = hydration
- this.hydration = (previous ?: StalkerHydrationSnapshot(
+ val snapshot = (previous ?: StalkerHydrationSnapshot(
lastHydratedAt = 0L,
itemCount = 0,
lastStatus = "RUNNING",
@@ -279,9 +425,14 @@ class StalkerIncrementalIndexExecutorTest {
retryBudgetRemaining = ((previous?.retryBudgetRemaining ?: 3) - 1).coerceAtLeast(0),
lastPageFingerprint = pageFingerprint
)
+ categoryHydrations[categoryId] = snapshot
+ if (categoryId == wildcard.categoryId) this.hydration = snapshot
}
override suspend fun upsertMovieSummaryBatch(providerId: Long, movies: List<Movie>, indexedAt: Long) {
+ if (summaryFetchCountAtFirstCommit == null && summaryFetches.isNotEmpty()) {
+ summaryFetchCountAtFirstCommit = summaryFetches.size
+ }
indexedMovies += movies
}
@@ -299,6 +450,8 @@ class StalkerIncrementalIndexExecutorTest {
override suspend fun upsertJob(update: CatalogIndexJobUpdate) = Unit
+ fun categoryHydration(categoryId: Long): StalkerHydrationSnapshot? = categoryHydrations[categoryId]
+
override fun log(message: String) = Unit
}
}
diff --git a/data/src/test/java/com/streamvault/data/sync/StalkerIndexPolicyTest.kt b/data/src/test/java/com/streamvault/data/sync/StalkerIndexPolicyTest.kt
index d283e14a..cb51fde8 100644
--- a/data/src/test/java/com/streamvault/data/sync/StalkerIndexPolicyTest.kt
+++ b/data/src/test/java/com/streamvault/data/sync/StalkerIndexPolicyTest.kt
@@ -97,14 +97,15 @@ class StalkerIndexPolicyTest {
}
@Test
- fun `wildcard backfill targets categories without local rows`() {
+ fun `wildcard backfill targets missing and partially covered categories`() {
val covered = category(1L, "Action")
- val excluded = category(2L, "Adult")
+ val partiallyCovered = category(2L, "Adult")
val empty = category(3L, "Empty")
val backfill = StalkerIndexPolicy.wildcardBackfillCategories(
- normalCategories = listOf(covered, excluded, empty),
- localRowCounts = mapOf(1L to 25, 2L to 0)
+ normalCategories = listOf(covered, partiallyCovered, empty),
+ localRowCounts = mapOf(1L to 25, 2L to 14),
+ advertisedTotals = mapOf(1L to 25, 2L to 38)
)
assertThat(backfill.map { it.categoryId }).containsExactly(2L, 3L)
diff --git a/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt b/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt
index aad80818..369b9a58 100644
--- a/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt
+++ b/data/src/test/java/com/streamvault/data/sync/SyncManagerTest.kt
@@ -4109,7 +4109,7 @@ class SyncManagerTest {
}
@Test
- fun sync_stalker_bootstrap_imports_prefix_without_certifying_bulk_fidelity() = runTest {
+ fun sync_stalker_bootstrap_imports_all_live_channels_and_certifies_bulk_fidelity() = runTest {
val provider = sampleProvider(ProviderType.STALKER_PORTAL).copy(
serverUrl = "http://example.com", username = "", password = "",
stalkerMacAddress = "00:11:22:33:44:55", epgSyncMode = ProviderEpgSyncMode.SKIP
@@ -4133,12 +4133,13 @@ class SyncManagerTest {
assertThat(manager.sync(1L, bootstrap = true)).isInstanceOf(Result.Success::class.java)
val writes = argumentCaptor<List<com.streamvault.data.local.entity.ChannelImportStageEntity>>()
verify(catalogSyncDao, atLeastOnce()).insertChannelStages(writes.capture())
- assertThat(writes.allValues.flatten()).hasSize(200)
- verify(portalState, org.mockito.kotlin.never()).recordBulkLive(any(), eq(true), anyOrNull(), any(), anyOrNull())
+ assertThat(writes.allValues.flatten()).hasSize(1000)
+ verify(portalState).recordBulkLive(any(), eq(true), anyOrNull(), any(), anyOrNull())
assertThat(readinessTracker.current(1L)?.state)
.isEqualTo(com.streamvault.domain.model.StalkerReadiness.READY)
- assertThat(syncMetadataRepo.getMetadata(1L)?.lastLiveSuccess).isEqualTo(0L)
- verify(workScheduler).scheduleProviderResume(1L)
+ assertThat(syncMetadataRepo.getMetadata(1L)?.lastLiveSuccess).isGreaterThan(0L)
+ verify(workScheduler).scheduleStalkerBootstrapResume(1L)
+ verify(workScheduler, org.mockito.kotlin.never()).scheduleProviderResume(1L)
}
@Test
@@ -4177,7 +4178,7 @@ class SyncManagerTest {
}
@Test
- fun sync_stalker_bootstrap_category_fallback_stops_after_the_accepted_prefix() = runTest {
+ fun sync_stalker_bootstrap_category_fallback_imports_every_live_category() = runTest {
val provider = sampleProvider(ProviderType.STALKER_PORTAL).copy(
serverUrl = "http://example.com", username = "", password = "",
stalkerMacAddress = "00:11:22:33:44:55", epgSyncMode = ProviderEpgSyncMode.SKIP
@@ -4216,11 +4217,12 @@ class SyncManagerTest {
val writes = argumentCaptor<List<com.streamvault.data.local.entity.ChannelImportStageEntity>>()
verify(catalogSyncDao, atLeastOnce()).insertChannelStages(writes.capture())
- assertThat(writes.allValues.flatten()).hasSize(200)
+ assertThat(writes.allValues.flatten()).hasSize(251)
verify(stalkerApiService).getLiveStreams(any(), any(), eq("10"))
- verify(stalkerApiService, org.mockito.kotlin.never()).getLiveStreams(any(), any(), eq("20"))
+ verify(stalkerApiService).getLiveStreams(any(), any(), eq("20"))
verify(portalState, org.mockito.kotlin.never()).recordBulkLive(any(), eq(true), anyOrNull(), any(), anyOrNull())
- verify(workScheduler).scheduleProviderResume(1L)
+ verify(workScheduler).scheduleStalkerBootstrapResume(1L)
+ verify(workScheduler, org.mockito.kotlin.never()).scheduleProviderResume(1L)
}
@Test