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.
Retry and backoff policyIf you require that WorkManager retry your work, you can return Result. retry() from your worker. Your work is then rescheduled according to a backoff delay and backoff policy. Backoff delay specifies the minimum amount of time to wait before retrying your work after the first attempt.
This method is deprecated. Call getInstance instead.
This second interval (the flexInterval) it's positioned at the end of the repetition interval itself. Let's look at an example. Imagine you want to build a periodic Work request with a 30 minutes period. You can specify a flexInterval, smaller than this period, say a 15 minute flexInterval.
Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.
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.
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
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With