Skip to content

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

test: add reduced data sync timeout gate

David Nashash73de416d947c
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 45333165..18a79119 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -214,7 +214,11 @@ jobs:
chmod +x gradlew
./gradlew --console=plain :app:connectedDebugAndroidTest -PcompatApi=${{ matrix.api-level }} -Pandroid.testInstrumentationRunnerArguments.class=com.streamvault.app.compat.PlatformCompatibilityMatrixTest --no-daemon
if [ "${{ matrix.api-level }}" = "35" ] || [ "${{ matrix.api-level }}" = "36" ]; then
+ trap 'adb shell device_config delete activity_manager data_sync_fgs_timeout_duration >/dev/null 2>&1 || true; adb shell am compat disable FGS_INTRODUCE_TIME_LIMITS com.streamvault.app.debug >/dev/null 2>&1 || true' EXIT
+ adb shell am compat enable FGS_INTRODUCE_TIME_LIMITS com.streamvault.app.debug
+ adb shell device_config put activity_manager data_sync_fgs_timeout_duration 5000
./gradlew --console=plain :app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.streamvault.app.service.DownloadForegroundServiceInstrumentationTest --no-daemon
+ ./gradlew --console=plain :app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.streamvault.app.service.DownloadForegroundServiceQuotaInstrumentationTest --no-daemon
./gradlew --console=plain :data:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.streamvault.data.manager.recording.PlatformReleaseSafetyInstrumentationTest --no-daemon
fi
if [ "${{ matrix.api-level }}" = "25" ] || [ "${{ matrix.api-level }}" = "36" ]; then
diff --git a/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt b/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt
new file mode 100644
index 00000000..8f920ea3
--- /dev/null
+++ b/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt
@@ -0,0 +1,92 @@
+package com.streamvault.app.service
+
+import android.content.Context
+import android.content.Intent
+import android.os.SystemClock
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import com.streamvault.data.platform.DataSyncQuotaOwner
+import com.streamvault.data.platform.DataSyncServiceOwner
+import dagger.hilt.EntryPoint
+import dagger.hilt.InstallIn
+import dagger.hilt.android.EntryPointAccessors
+import dagger.hilt.components.SingletonComponent
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+/**
+ * Runs only with the API 35/36 reduced-timeout harness from release CI.
+ *
+ * The missing download id deliberately leaves the service observing a live Room flow, so the
+ * Android timeout callback—not normal download completion—must release the shared lease.
+ */
+@RunWith(AndroidJUnit4::class)
+class DownloadForegroundServiceQuotaInstrumentationTest {
+
+ @EntryPoint
+ @InstallIn(SingletonComponent::class)
+ interface QuotaEntryPoint {
+ fun dataSyncQuotaOwner(): DataSyncQuotaOwner
+ }
+
+ private lateinit var context: Context
+ private lateinit var quotaOwner: DataSyncQuotaOwner
+
+ @Before
+ fun setUp() {
+ context = ApplicationProvider.getApplicationContext()
+ context.getSharedPreferences(QUOTA_PREFERENCES, Context.MODE_PRIVATE)
+ .edit()
+ .clear()
+ .commit()
+ quotaOwner = EntryPointAccessors.fromApplication(
+ context,
+ QuotaEntryPoint::class.java
+ ).dataSyncQuotaOwner()
+ }
+
+ @After
+ fun tearDown() {
+ context.stopService(probeIntent())
+ context.getSharedPreferences(QUOTA_PREFERENCES, Context.MODE_PRIVATE)
+ .edit()
+ .clear()
+ .commit()
+ }
+
+ @Test
+ fun reducedDataSyncTimeoutReleasesDownloadLease() {
+ val intent = probeIntent()
+ androidx.core.content.ContextCompat.startForegroundService(context, intent)
+
+ await("download service acquires shared quota lease") {
+ quotaOwner.snapshot().activeOwners.contains(DataSyncServiceOwner.DOWNLOAD)
+ }
+ await("Android timeout callback releases shared quota lease") {
+ !quotaOwner.snapshot().activeOwners.contains(DataSyncServiceOwner.DOWNLOAD)
+ }
+ }
+
+ private fun probeIntent(): Intent = Intent(context, DownloadForegroundService::class.java)
+ .putExtra("download_id", PROBE_DOWNLOAD_ID)
+
+ private fun await(description: String, predicate: () -> Boolean) {
+ val deadline = SystemClock.elapsedRealtime() + TIMEOUT_WAIT_MS
+ do {
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync()
+ if (predicate()) return
+ SystemClock.sleep(POLL_INTERVAL_MS)
+ } while (SystemClock.elapsedRealtime() < deadline)
+ if (!predicate()) throw AssertionError(description)
+ }
+
+ private companion object {
+ const val QUOTA_PREFERENCES = "foreground_service_quota"
+ const val PROBE_DOWNLOAD_ID = "wp0-reduced-timeout-probe"
+ const val POLL_INTERVAL_MS = 250L
+ const val TIMEOUT_WAIT_MS = 20_000L
+ }
+}