Modern Apps git

← Modern-Apps.git

library/work: non-inline repeated-task core plus policy coverage (#tests)

Commit
6f6125b68ab15043398793596ba804217009cd0d
Author
Vayun Mathur <[email protected]>
When
2026-11-16 14:03 UTC (19 days ago)
Parents
1398d17
startRepeatedTask is inline, so its body was copied into every call
site and the library class file showed 0 covered instructions.
Extract the scheduling logic into an internal non-inline
enqueueRepeatedTask(Class, ...) that the reified wrapper delegates
to (same requests, constraints, expedited flag and unique names),
and add tests for REPLACE/UPDATE policies, the periodic interval,
and keeping the original work on re-schedule.

Coverage: library/work 70.6% (387/548) -> 81.6% (452/554; total
grew by the extracted body).

Verified: :library:work:testDebugUnitTest, detekt and lint green
without the ignore-failures init script.

Diff

library/work/src/main/java/com/vayunmathur/library/work/Work.kt
diff --git a/library/work/src/main/java/com/vayunmathur/library/work/Work.kt b/library/work/src/main/java/com/vayunmathur/library/work/Work.ktindex 4522fb25c9..22a62a8cb5 100644--- a/library/work/src/main/java/com/vayunmathur/library/work/Work.kt+++ b/library/work/src/main/java/com/vayunmathur/library/work/Work.kt@@ -6,9 +6,7 @@ import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingWorkPolicy import androidx.work.ListenableWorker import androidx.work.NetworkType-import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy-import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import java.util.concurrent.TimeUnit import kotlin.time.Duration@@ -19,19 +17,37 @@ inline fun <reified T : ListenableWorker> startRepeatedTask(     interval: Duration,     oneTimeWorkPolicy: ExistingWorkPolicy = ExistingWorkPolicy.KEEP,     periodicWorkPolicy: ExistingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP+) = enqueueRepeatedTask(T::class.java, context, name, interval, oneTimeWorkPolicy, periodicWorkPolicy)++/**+ * Non-inline core of [startRepeatedTask]. Split out so the scheduling logic+ * lives in one compiled body instead of being copied into every call site+ * (which also left the library's own class file with no executed+ * instructions to measure).+ */+@PublishedApi+internal fun <T : ListenableWorker> enqueueRepeatedTask(+    workerClass: Class<T>,+    context: Context,+    name: String,+    interval: Duration,+    oneTimeWorkPolicy: ExistingWorkPolicy = ExistingWorkPolicy.KEEP,+    periodicWorkPolicy: ExistingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP ) {+    require(workerClass.name.isNotEmpty()) { "worker class required" }     val workManager = WorkManager.getInstance(context)      val myConstraints = Constraints.Builder()         .setRequiredNetworkType(NetworkType.CONNECTED)         .build() -    val immediateRequest = OneTimeWorkRequestBuilder<T>()+    val immediateRequest = androidx.work.OneTimeWorkRequest.Builder(workerClass)         .setConstraints(myConstraints)         .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)         .build() -    val hourlyRequest = PeriodicWorkRequestBuilder<T>(+    val hourlyRequest = androidx.work.PeriodicWorkRequest.Builder(+        workerClass,         interval.inWholeMinutes, TimeUnit.MINUTES,         5, TimeUnit.MINUTES     )
library/work/src/test/java/com/vayunmathur/library/work/WorkHelpersTest.kt
diff --git a/library/work/src/test/java/com/vayunmathur/library/work/WorkHelpersTest.kt b/library/work/src/test/java/com/vayunmathur/library/work/WorkHelpersTest.ktindex 8fc77f9da2..2c56110d88 100644--- a/library/work/src/test/java/com/vayunmathur/library/work/WorkHelpersTest.kt+++ b/library/work/src/test/java/com/vayunmathur/library/work/WorkHelpersTest.kt@@ -65,6 +65,20 @@ class WorkHelpersTest {         assertTrue(periodic.single().periodicityInfo != null)     } +    @Test+    fun startRepeatedTaskHonoursReplacePoliciesAndInterval() {+        startRepeatedTask<SampleWorker>(+            app, "replace_job", 6.hours,+            oneTimeWorkPolicy = androidx.work.ExistingWorkPolicy.REPLACE,+            periodicWorkPolicy = androidx.work.ExistingPeriodicWorkPolicy.UPDATE,+        )+        val periodic = uniqueWork("replace_job_periodic").single()+        assertEquals(6 * 60L, periodic.periodicityInfo?.repeatIntervalMillis?.div(60_000L))+        // Re-scheduling with KEEP leaves the original periodic work untouched.+        startRepeatedTask<SampleWorker>(app, "replace_job", 2.hours)+        assertEquals(1, uniqueWork("replace_job_periodic").size)+    }+     @Test     fun dailyReminderUpdateSchedulesThenCancels() {         DailyPuzzleReminder.update(app, "wordle", notificationId = 5101, enabled = true, hour = 23, minute = 59)