Skip to content

Commit 0133e0a

Browse files
committed
Use a latch for the other usage of awaitProgressEvents()
1 parent 75108e0 commit 0133e0a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionServiceImplTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.google.firebase.installations.InstallationTokenResult;
8787
import java.util.ArrayList;
8888
import java.util.List;
89+
import java.util.concurrent.CountDownLatch;
8990
import java.util.concurrent.ExecutionException;
9091
import java.util.concurrent.ExecutorService;
9192
import java.util.concurrent.Executors;
@@ -364,11 +365,29 @@ public void updateIfNewReleaseAvailable_whenNoReleaseAvailable_updateDialogNotSh
364365
@Test
365366
public void updateIfNewReleaseAvailable_whenActivityBackgrounded_updateDialogNotShown()
366367
throws InterruptedException {
367-
when(mockNewReleaseFetcher.checkForNewRelease()).thenReturn(Tasks.forResult(null));
368-
368+
// Block thread making the request on a latch, which gives us time to add listeners to the
369+
// returned UpdateTask in time to get all the progress updates
370+
CountDownLatch countDownLatch = new CountDownLatch(1);
371+
when(mockNewReleaseFetcher.checkForNewRelease())
372+
.thenAnswer(
373+
invocation -> {
374+
try {
375+
countDownLatch.await();
376+
} catch (InterruptedException e) {
377+
throw new AssertionError("Interrupted while waiting in mock");
378+
}
379+
return Tasks.forResult(null);
380+
});
381+
382+
// Start the update
369383
UpdateTask task = firebaseAppDistribution.updateIfNewReleaseAvailable();
370384

371-
List<UpdateProgress> progressEvents = awaitProgressEvents(task, 1);
385+
// Listen for progress events
386+
List<UpdateProgress> progressEvents = new ArrayList<>();
387+
task.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
388+
countDownLatch.countDown();
389+
awaitProgressEvents(progressEvents, 1);
390+
372391
assertEquals(UpdateStatus.NEW_RELEASE_NOT_AVAILABLE, progressEvents.get(0).getUpdateStatus());
373392
assertNull(ShadowAlertDialog.getLatestAlertDialog());
374393
}

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/TestUtils.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@
2929
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
3030
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
3131
import com.google.firebase.appdistribution.UpdateProgress;
32-
import com.google.firebase.appdistribution.UpdateTask;
3332
import com.google.firebase.appdistribution.impl.FirebaseAppDistributionLifecycleNotifier.ActivityConsumer;
3433
import com.google.firebase.appdistribution.impl.FirebaseAppDistributionLifecycleNotifier.ActivityFunction;
35-
import com.google.firebase.concurrent.FirebaseExecutors;
3634
import com.google.firebase.concurrent.TestOnlyExecutors;
3735
import java.io.IOException;
3836
import java.io.InputStream;
39-
import java.util.ArrayList;
4037
import java.util.Collection;
41-
import java.util.List;
4238
import java.util.concurrent.ExecutionException;
4339
import java.util.concurrent.ExecutorService;
4440
import java.util.concurrent.Executors;
@@ -102,15 +98,6 @@ static void awaitAsyncOperations(ExecutorService executorService) throws Interru
10298
shadowOf(getMainLooper()).idle();
10399
}
104100

105-
/** Await a specified number of progress events on an {@link UpdateTask}. */
106-
static List<UpdateProgress> awaitProgressEvents(UpdateTask updateTask, int count)
107-
throws InterruptedException {
108-
List<UpdateProgress> progressEvents = new ArrayList<>();
109-
updateTask.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
110-
awaitProgressEvents(progressEvents, count);
111-
return progressEvents;
112-
}
113-
114101
/** Await a specified number of progress events being added to the given collection. */
115102
static void awaitProgressEvents(Collection<UpdateProgress> progressEvents, int count)
116103
throws InterruptedException {

0 commit comments

Comments
 (0)