Reputation: 659
When I use:
WorkManagerTestInitHelper.initializeTestWorkManager(context);
WorkManager workManager = WorkManager.getInstance();
to get the WorkManager instance, the WorkManager fail to achieve retry. I will present the simplest test:
public class WorkManagerTest {
Context context;
@Before
public void setUp() throws Exception {
context = InstrumentationRegistry.getTargetContext();
}
@Test
public void testWorker1(){
Constraints constraints = new Constraints.Builder()
.build();
OneTimeWorkRequest.Builder updateorderworkerBuilder =
new OneTimeWorkRequest.Builder(TestWorker1.class)
.setConstraints(constraints)
//MIN_BACKOFF_MILLIS == 10000
.setBackoffCriteria(BackoffPolicy.LINEAR,10000, TimeUnit.MILLISECONDS);
OneTimeWorkRequest workRequest = updateorderworkerBuilder.build();
WorkManager workManager = WorkManager.getInstance();
workManager.enqueue(workRequest);
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testWorker2(){
Constraints constraints = new Constraints.Builder()
.build();
OneTimeWorkRequest.Builder updateorderworkerBuilder =
new OneTimeWorkRequest.Builder(TestWorker2.class)
.setConstraints(constraints)
//MIN_BACKOFF_MILLIS == 10000
.setBackoffCriteria(BackoffPolicy.LINEAR,10000, TimeUnit.MILLISECONDS);
OneTimeWorkRequest workRequest = updateorderworkerBuilder.build();
WorkManagerTestInitHelper.initializeTestWorkManager(context);
WorkManager workManager = WorkManager.getInstance();
workManager.enqueue(workRequest);
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class TestWorker1 extends Worker {
/**
* Override this method to do your actual background processing.
*
* @return The result of the work, corresponding to a {@link Result} value. If a
* different value is returned, the result shall be defaulted to
* {@link Result#FAILURE}.
*/
@NonNull
@Override
public Result doWork() {
Log.d("TEST_WORKER", "inside doWork()1");
return Result.RETRY;
}
}
public static class TestWorker2 extends Worker {
/**
* Override this method to do your actual background processing.
*
* @return The result of the work, corresponding to a {@link Result} value. If a
* different value is returned, the result shall be defaulted to
* {@link Result#FAILURE}.
*/
@NonNull
@Override
public Result doWork() {
Log.d("TEST_WORKER", "inside doWork()2");
return Result.RETRY;
}
}
}
In the Logcat I see:
07-22 23:34:25.184 5346-5388/com.billst.app.debug D/TEST_WORKER: inside doWork()1
07-22 23:34:30.178 5346-5391/com.billst.app.debug D/TEST_WORKER: inside doWork()1
07-22 23:34:40.238 5346-5388/com.billst.app.debug D/TEST_WORKER: inside doWork()1
07-22 23:35:00.300 5346-5391/com.billst.app.debug D/TEST_WORKER: inside doWork()1
07-22 23:35:15.130 5346-5372/com.billst.app.debug D/TEST_WORKER: inside doWork()2
I take the same results even when I execute the 2 tests independently. We see that the use of WorkManagerTestInitHelper prevents the WorkManager from completing the retry of the work.
Do I use WorkManagerTestInitHelper correctly?
Upvotes: 3
Views: 1383
Reputation: 21134
You cannot simulate retries in test mode. This is intentional because you should be testing your Worker
and not testing WorkManager
. You cannot use WorkManagerTestInitHelper
for this. If you want to test that your Worker
returns a Result.retry()
then you could also use the new TestWorkerBuilder<>
and the TestListenableWorkerBuilder
APIs.
Here is an example. https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/work/workmanager-testing/src/androidTest/java/androidx/work/testing/TestWorkerBuilderTest.kt
Upvotes: 1