Skip to content

Commit cdd1044

Browse files
authored
Fix broken firebase-appdistribution test (#3920)
1 parent 905eeb8 commit cdd1044

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/AabUpdater.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ UpdateTaskImpl updateAab(@NonNull AppDistributionReleaseInternal newRelease) {
9696
// On a background thread, fetch the redirect URL and open it in the Play app
9797
runAsyncInTask(executor, () -> fetchDownloadRedirectUrl(newRelease.getDownloadUrl()))
9898
.onSuccessTask(
99+
executor,
99100
redirectUrl ->
100101
lifecycleNotifier.consumeForegroundActivity(
101102
activity -> openRedirectUrlInPlay(redirectUrl, activity)))
102-
.addOnFailureListener(this::setUpdateTaskCompletionError);
103+
.addOnFailureListener(executor, this::setUpdateTaskCompletionError);
103104

104105
return cachedUpdateTask;
105106
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.firebase.appdistribution.UpdateTask;
3333
import java.io.ByteArrayInputStream;
3434
import java.io.IOException;
35-
import java.util.ArrayList;
3635
import java.util.List;
3736
import java.util.concurrent.ExecutorService;
3837
import java.util.concurrent.Executors;
@@ -143,11 +142,11 @@ public void updateAppTask_emptyLocationHeader_setsDownloadFailure() throws Inter
143142

144143
@Test
145144
public void updateAppTask_whenAabReleaseAvailable_redirectsToPlay() throws Exception {
146-
List<UpdateProgress> progressEvents = new ArrayList<>();
147-
145+
TestOnProgressListener listener = new TestOnProgressListener(1);
148146
UpdateTask updateTask = aabUpdater.updateAab(TEST_RELEASE_NEWER_AAB_INTERNAL);
149-
updateTask.addOnProgressListener(testExecutor, progressEvents::add);
150-
awaitAsyncOperations(testExecutor);
147+
updateTask.addOnProgressListener(testExecutor, listener);
148+
149+
List<UpdateProgress> progressEvents = listener.await();
151150

152151
// Task is not completed in this case, because app is expected to terminate during update
153152
assertThat(shadowActivity.getNextStartedActivity().getData())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appdistribution.impl;
16+
17+
import androidx.annotation.NonNull;
18+
import com.google.firebase.appdistribution.OnProgressListener;
19+
import com.google.firebase.appdistribution.UpdateProgress;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.concurrent.CountDownLatch;
23+
import java.util.concurrent.TimeUnit;
24+
25+
/**
26+
* Helper listener that awaits a specific number of progress events on a {@code UpdateTask}.
27+
*
28+
* <p>This works around a limitation of the Tasks API where await() cannot be called on the main
29+
* thread. This listener works around it by running itself on a different thread, thus allowing the
30+
* main thread to be woken up when the Tasks complete.
31+
*
32+
* <p>Note: Calling {@link #await()} from a Robolectric test does block the main thread, since those
33+
* tests are executed on the main thread.
34+
*/
35+
class TestOnProgressListener implements OnProgressListener {
36+
private static final long TIMEOUT_MS = 5000;
37+
private final int expectedProgressCount;
38+
private final CountDownLatch latch;
39+
private final List<UpdateProgress> progressUpdates = new ArrayList<>();
40+
41+
TestOnProgressListener(int expectedProgressCount) {
42+
this.expectedProgressCount = expectedProgressCount;
43+
this.latch = new CountDownLatch(expectedProgressCount);
44+
}
45+
46+
@Override
47+
public void onProgressUpdate(@NonNull UpdateProgress updateProgress) {
48+
progressUpdates.add(updateProgress);
49+
latch.countDown();
50+
}
51+
52+
/** Blocks until the {@link #onProgressUpdate} is called the expected number of times. */
53+
List<UpdateProgress> await() throws InterruptedException {
54+
if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
55+
throw new InterruptedException(
56+
String.format(
57+
"Timed out waiting for progress events (expected = %d, actual = %d)",
58+
expectedProgressCount, progressUpdates.size()));
59+
}
60+
return progressUpdates;
61+
}
62+
}

0 commit comments

Comments
 (0)