WorkManager google api: wait 15 minutes for every periodic worker execution?
Asked Answered
A

2

9

Is there a way to test a PERIODIC worker from WorkManager Google API without waiting at least 15 minutes for every execution?

I mean, it is a DEBUG app and I'm running it through Android Studio and I don't want to wait such a long time to test my features.

Augur answered 14/3, 2019 at 21:20 Comment(0)
C
3

You can't.

Periodic work has a minimum interval of 15 minutes and it cannot have an initial delay. You can find the proof in the WorkSpec.java class.

 /**
     * Sets the periodic interval for this unit of work.
     *
     * @param intervalDuration The interval in milliseconds
     */
    public void setPeriodic(long intervalDuration) {
        if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) {
            Logger.get().warning(TAG, String.format(
                    "Interval duration lesser than minimum allowed value; Changed to %s",
                    MIN_PERIODIC_INTERVAL_MILLIS));
            intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS;
        }
        setPeriodic(intervalDuration, intervalDuration);
    }

But there are other ways to deal with that.

  1. Write unit tests using work-testing library and ensure that your business logic works as expected.
  2. Use dependency injection approach and provide a OneTimeWorkRequest in debug mode, for example:
interface Scheduler {
    fun schedule()
}

class DebugScheduler {
    fun schedule() {
        WorkManager.getInstance().enqueue(
            OneTimeWorkRequest.Builder(MyWorker::class.java)
                .build()
        )
    }
}

class ProductionScheduler {
    fun schedule() {
        // your actual scheduling logic
    }
}
Convenance answered 14/3, 2019 at 22:50 Comment(0)
L
1

For testing purposes, you can use the work-testing library as shown here: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/testing

Specifically, you want to look at how to test periodic work: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/testing#periodic-work

Lightface answered 15/3, 2019 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.