Skip to content

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

feat: enhance DownloadForegroundService timeout handling and update smoke test setup for data sync timeout duration

David Nashashb78a59742890
diff --git a/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt b/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt
index efb78296..88e5fa2f 100644
--- a/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt
+++ b/app/src/androidTest/java/com/streamvault/app/service/DownloadForegroundServiceQuotaInstrumentationTest.kt
@@ -56,7 +56,10 @@ class DownloadForegroundServiceQuotaInstrumentationTest {
await("download service acquires shared quota lease") {
quotaOwner.snapshot().activeOwners.contains(DataSyncServiceOwner.DOWNLOAD)
}
- await("Android timeout callback releases shared quota lease") {
+ await(
+ description = "Android timeout callback releases shared quota lease",
+ timeoutMs = TIMEOUT_RELEASE_WAIT_MS
+ ) {
!quotaOwner.snapshot().activeOwners.contains(DataSyncServiceOwner.DOWNLOAD)
}
}
@@ -64,8 +67,12 @@ class DownloadForegroundServiceQuotaInstrumentationTest {
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
+ private fun await(
+ description: String,
+ timeoutMs: Long = TIMEOUT_WAIT_MS,
+ predicate: () -> Boolean
+ ) {
+ val deadline = SystemClock.elapsedRealtime() + timeoutMs
do {
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
if (predicate()) return
@@ -79,5 +86,6 @@ class DownloadForegroundServiceQuotaInstrumentationTest {
const val PROBE_DOWNLOAD_ID = "wp0-reduced-timeout-probe"
const val POLL_INTERVAL_MS = 250L
const val TIMEOUT_WAIT_MS = 20_000L
+ const val TIMEOUT_RELEASE_WAIT_MS = 45_000L
}
}
diff --git a/app/src/main/java/com/streamvault/app/service/DownloadForegroundService.kt b/app/src/main/java/com/streamvault/app/service/DownloadForegroundService.kt
index 9d151164..0d847029 100644
--- a/app/src/main/java/com/streamvault/app/service/DownloadForegroundService.kt
+++ b/app/src/main/java/com/streamvault/app/service/DownloadForegroundService.kt
@@ -138,13 +138,17 @@ class DownloadForegroundService : Service() {
override fun onTimeout(startId: Int, fgsType: Int) {
val downloadId = currentDownloadId
Log.e(TAG, "Foreground-service time allowance exhausted; pausing download $downloadId")
+
+ // Android only gives the service a short grace period after this callback. Release the
+ // shared lease before doing database work so another dataSync service cannot be blocked
+ // by a slow pause/reconciliation operation.
+ releaseDataSyncQuotaLease("android_timeout")
serviceScope.launch {
try {
if (!downloadId.isNullOrBlank()) {
entryPoint().downloadManager().pauseDownloadForForegroundServiceTimeout(downloadId)
}
} finally {
- releaseDataSyncQuotaLease("android_timeout")
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf(startId)
}
diff --git a/tools/run-platform-smoke.sh b/tools/run-platform-smoke.sh
index bf4dc83a..ae6db3a0 100755
--- a/tools/run-platform-smoke.sh
+++ b/tools/run-platform-smoke.sh
@@ -43,6 +43,12 @@ export PLATFORM_SMOKE_ACTIVE_SUITE
if [ "$api_level" = "35" ] || [ "$api_level" = "36" ]; then
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
+ fgs_timeout_duration=$(adb shell device_config get activity_manager data_sync_fgs_timeout_duration | tr -d '\r')
+ if [ "$fgs_timeout_duration" != "5000" ]; then
+ printf 'API %s smoke setup did not apply data_sync_fgs_timeout_duration (got %s)\n' \
+ "$api_level" "$fgs_timeout_duration" >&2
+ exit 1
+ fi
PLATFORM_SMOKE_ACTIVE_SUITE="com.streamvault.app.service.DownloadForegroundServiceInstrumentationTest"
export PLATFORM_SMOKE_ACTIVE_SUITE