Skip to content

Commit bef927c

Browse files
committed
Refactor awaitProgressEvents into awaitCondition
1 parent 0133e0a commit bef927c

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package com.google.firebase.appdistribution.impl;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18-
import static com.google.firebase.appdistribution.impl.TestUtils.awaitProgressEvents;
1918
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTask;
19+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitCondition;
2020
import static org.junit.Assert.assertThrows;
2121
import static org.mockito.ArgumentMatchers.any;
2222
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -180,8 +180,7 @@ public void updateApk_whenCannotReadInputStream_setsDownloadFailure() throws Exc
180180

181181
@Test
182182
public void updateApk_whenSuccessfullyUpdated_notificationsSetCorrectly()
183-
throws FirebaseAppDistributionException, ExecutionException, InterruptedException,
184-
IOException {
183+
throws FirebaseAppDistributionException, InterruptedException, IOException {
185184
doReturn(new ByteArrayInputStream(TEST_FILE.getBytes()))
186185
.when(mockHttpsUrlConnection)
187186
.getInputStream();
@@ -208,7 +207,7 @@ public void updateApk_whenSuccessfullyUpdated_notificationsSetCorrectly()
208207
List<UpdateProgress> events = new ArrayList<>();
209208
updateTask.addOnProgressListener(FirebaseExecutors.directExecutor(), events::add);
210209
countDownLatch.countDown();
211-
awaitProgressEvents(events, 3);
210+
awaitCondition(() -> events.size() == 3);
212211

213212
assertThat(events.get(0).getUpdateStatus()).isEqualTo(UpdateStatus.PENDING);
214213
assertThat(events.get(1).getUpdateStatus()).isEqualTo(UpdateStatus.DOWNLOADING);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
import static com.google.firebase.appdistribution.impl.FeedbackActivity.RELEASE_NAME_KEY;
3434
import static com.google.firebase.appdistribution.impl.FeedbackActivity.SCREENSHOT_URI_KEY;
3535
import static com.google.firebase.appdistribution.impl.TestUtils.awaitAsyncOperations;
36-
import static com.google.firebase.appdistribution.impl.TestUtils.awaitProgressEvents;
3736
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTask;
3837
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTaskFailure;
3938
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTermination;
4039
import static com.google.firebase.appdistribution.impl.TestUtils.mockForegroundActivity;
40+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitCondition;
4141
import static java.util.stream.Collectors.toList;
4242
import static org.junit.Assert.assertEquals;
4343
import static org.junit.Assert.assertFalse;
@@ -386,7 +386,7 @@ public void updateIfNewReleaseAvailable_whenActivityBackgrounded_updateDialogNot
386386
List<UpdateProgress> progressEvents = new ArrayList<>();
387387
task.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
388388
countDownLatch.countDown();
389-
awaitProgressEvents(progressEvents, 1);
389+
awaitCondition(() -> progressEvents.size() == 1);
390390

391391
assertEquals(UpdateStatus.NEW_RELEASE_NOT_AVAILABLE, progressEvents.get(0).getUpdateStatus());
392392
assertNull(ShadowAlertDialog.getLatestAlertDialog());

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,25 @@
2828
import com.google.android.gms.tasks.Tasks;
2929
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
3030
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
31-
import com.google.firebase.appdistribution.UpdateProgress;
3231
import com.google.firebase.appdistribution.impl.FirebaseAppDistributionLifecycleNotifier.ActivityConsumer;
3332
import com.google.firebase.appdistribution.impl.FirebaseAppDistributionLifecycleNotifier.ActivityFunction;
34-
import com.google.firebase.concurrent.TestOnlyExecutors;
3533
import java.io.IOException;
3634
import java.io.InputStream;
37-
import java.util.Collection;
3835
import java.util.concurrent.ExecutionException;
3936
import java.util.concurrent.ExecutorService;
4037
import java.util.concurrent.Executors;
4138
import java.util.concurrent.TimeUnit;
39+
import java.util.function.BooleanSupplier;
4240
import org.json.JSONException;
4341
import org.json.JSONObject;
4442
import org.mockito.stubbing.Answer;
4543

4644
final class TestUtils {
45+
46+
private static final int AWAIT_TERMINATION_TIMEOUT_MS = 100;
47+
private static final int AWAIT_CONDITION_TIMEOUT_MS = 500;
48+
private static final int SLEEP_MS = 50;
49+
4750
private TestUtils() {}
4851

4952
static void awaitTaskFailure(Task task, Status status, String messageSubstring) {
@@ -86,7 +89,7 @@ static <T> T awaitTask(Task<T> task)
8689
}
8790

8891
static void awaitTermination(ExecutorService executorService) throws InterruptedException {
89-
executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
92+
executorService.awaitTermination(AWAIT_TERMINATION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
9093
}
9194

9295
static void awaitAsyncOperations(ExecutorService executorService) throws InterruptedException {
@@ -98,22 +101,19 @@ static void awaitAsyncOperations(ExecutorService executorService) throws Interru
98101
shadowOf(getMainLooper()).idle();
99102
}
100103

101-
/** Await a specified number of progress events being added to the given collection. */
102-
static void awaitProgressEvents(Collection<UpdateProgress> progressEvents, int count)
103-
throws InterruptedException {
104-
ExecutorService executor = TestOnlyExecutors.blocking();
105-
executor.execute(
106-
() -> {
107-
while (progressEvents.size() < count) {
108-
try {
109-
Thread.sleep(50);
110-
} catch (InterruptedException e) {
111-
throw new RuntimeException("Interrupted while waiting for progress events", e);
112-
}
113-
}
114-
});
115-
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
116-
assertThat(progressEvents).hasSize(count);
104+
static void awaitCondition(BooleanSupplier condition) throws InterruptedException {
105+
long start = System.currentTimeMillis();
106+
while (elapsedTime(start) < AWAIT_CONDITION_TIMEOUT_MS) {
107+
if (condition.getAsBoolean()) {
108+
return;
109+
}
110+
Thread.sleep(SLEEP_MS);
111+
}
112+
throw new AssertionError("Timed out waiting for condition");
113+
}
114+
115+
private static long elapsedTime(long start) {
116+
return System.currentTimeMillis() - start;
117117
}
118118

119119
/**

0 commit comments

Comments
 (0)