[go: nahoru, domu]

Deflake WorkerWrapperTest#testInterruption_isMarkedOnRunningWorker

Removed one of
testInterruptionWithoutCancellation_isMarkedOnRunningWorker /
testInterruptionWithCancellation_isMarkedOnRunningWorker,

because cancellation concept of Worker that was added back ag/4222723
was removed before first release of workmanager

Test: passes
Change-Id: I8e30f2179931a4c58dc8bb7a5f4bade3aac01219
diff --git a/work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java b/work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java
index fc6fc8f..a9209c7 100644
--- a/work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java
+++ b/work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java
@@ -1102,50 +1102,18 @@
                 containsInAnyOrder(runtimeExtras.triggeredContentUris.toArray()));
     }
 
-    @Test
-    @SmallTest
-    public void testInterruptionWithoutCancellation_isMarkedOnRunningWorker() {
-        OneTimeWorkRequest work =
-                new OneTimeWorkRequest.Builder(InterruptionAwareWorker.class).build();
-        insertWork(work);
-
-        ListenableWorker worker = mConfiguration.getWorkerFactory().createWorkerWithDefaultFallback(
-                mContext.getApplicationContext(),
-                InterruptionAwareWorker.class.getName(),
-                new WorkerParameters(
-                        work.getId(),
-                        Data.EMPTY,
-                        work.getTags(),
-                        new WorkerParameters.RuntimeExtras(),
-                        1,
-                        0,
-                        mSynchronousExecutor,
-                        mWorkTaskExecutor,
-                        mConfiguration.getWorkerFactory(),
-                        mMockProgressUpdater,
-                        mMockForegroundUpdater));
-        assertThat(worker, is(notNullValue()));
-        assertThat(worker.isStopped(), is(false));
-
-        WorkerWrapper workerWrapper =
-                createBuilder(work.getStringId()).withWorker(worker).build();
-        mExecutorService.submit(workerWrapper);
-        workerWrapper.interrupt(0);
-        assertThat(worker.isStopped(), is(true));
-        assertThat(mWorkSpecDao.getState(work.getStringId()), is(ENQUEUED));
-    }
-
     // getStopReason() requires API level 31, but only because JobScheduler provides them
     // since API level 31, but in this isolated test we don't care.
     @SuppressLint("NewApi")
     @Test
     @SmallTest
-    public void testInterruptionWithCancellation_isMarkedOnRunningWorker() {
+    public void testInterruption_isMarkedOnRunningWorker() throws InterruptedException {
         OneTimeWorkRequest work =
                 new OneTimeWorkRequest.Builder(InterruptionAwareWorker.class).build();
         insertWork(work);
 
-        ListenableWorker worker = mConfiguration.getWorkerFactory().createWorkerWithDefaultFallback(
+        InterruptionAwareWorker worker = (InterruptionAwareWorker)
+                mConfiguration.getWorkerFactory().createWorkerWithDefaultFallback(
                 mContext.getApplicationContext(),
                 InterruptionAwareWorker.class.getName(),
                 new WorkerParameters(
@@ -1166,6 +1134,7 @@
         WorkerWrapper workerWrapper =
                 createBuilder(work.getStringId()).withWorker(worker).build();
         mExecutorService.submit(workerWrapper);
+        worker.doWorkLatch.await();
         workerWrapper.interrupt(STOP_REASON_CONSTRAINT_CHARGING);
         assertThat(worker.isStopped(), is(true));
         assertThat(worker.getStopReason(), is(STOP_REASON_CONSTRAINT_CHARGING));
diff --git a/work/work-runtime/src/androidTest/java/androidx/work/worker/InterruptionAwareWorker.java b/work/work-runtime/src/androidTest/java/androidx/work/worker/InterruptionAwareWorker.java
index cca8f3d..b1a16c6 100644
--- a/work/work-runtime/src/androidTest/java/androidx/work/worker/InterruptionAwareWorker.java
+++ b/work/work-runtime/src/androidTest/java/androidx/work/worker/InterruptionAwareWorker.java
@@ -22,7 +22,10 @@
 import androidx.work.Worker;
 import androidx.work.WorkerParameters;
 
+import java.util.concurrent.CountDownLatch;
+
 public class InterruptionAwareWorker extends Worker {
+    public CountDownLatch doWorkLatch = new CountDownLatch(1);
 
     public InterruptionAwareWorker(@NonNull Context context,
             @NonNull WorkerParameters workerParams) {
@@ -31,6 +34,7 @@
 
     @Override
     public @NonNull Result doWork() {
+        doWorkLatch.countDown();
         try {
             do {
                 Thread.sleep(1000L);