Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager: Call doWork() immediately for test purposes

If I have a PeriodicWorkRequest I can set a time intervall - which minimum is 15 minutes. But how can I test if my doWork() method is working without waiting for 15 minutes?

Is it maybe possible to use OneTimeWorkRequest for test purposes?

Thanks in advance

like image 486
hideous Avatar asked Nov 05 '18 17:11

hideous


People also ask

What is WorkManager used for?

WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

How does a WorkManager work?

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.


2 Answers

Yes, there is nothing stopping you from using OneTimeWorkRequest.

val work = OneTimeWorkRequest.Builder(MyWorker::class.java).build()
WorkManager.getInstance().enqueue(work)

Bear in mind, that this will not allow you to test that your Worker will fire after a set number of minutes, like when using PeriodicWorkRequest. It will however let you test that your code works, without needing to wait.

like image 119
Knossos Avatar answered Oct 29 '22 05:10

Knossos


Yes, the minimum time interval for PeriodicWorkRequest is 15 minutes and it can't be changed since it is hardcoded in PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS.

However, for test purpose you can call doWork() method of your PeriodicWorkRequest whenever you want with the help of WorkManagerTestInitHelper available in androidx.work.testing.

Add the following dependency in the build.gradle file for your app or module:

//Current stable release is 2.3.4
androidTestImplementation "androidx.work:work-testing:2.3.4"

Next, you need to use setPeriodDelayMet method available with TestDriver which can be used to indicate that an interval is complete and executes PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS (15 minutes). Sample code:

@Test
public void testPeriodicWork(Context context) throws Exception {
    // Setup input data
    Data input = new Data.Builder().put(KEY_1, 1).put(KEY_2, 2).build();

    // Create periodic work request
    PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(MyWorker.class, 15,  TimeUnit.MINUTES)
                                       .setInputData(input)
                                       .build();
    // Enqueue periodic request
    WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, request);

    // Initialize testDriver
    TestDriver testDriver = WorkManagerTestInitHelper.getTestDriver();

    // Tells the testing framework the period delay is met, this will execute your code in doWork() in MyWorker class
    testDriver.setPeriodDelayMet(request.getId());

}

Read more about testing PeriodicWorkRequest at Testing Periodic Work

like image 39
Dr. DS Avatar answered Oct 29 '22 03:10

Dr. DS