Skip to content

Commit 38ff115

Browse files
authored
appdistribution: fix flaky test due to race condtion in testing logic: checkForNewRelease_whenCalledMultipleTimes_onlyFetchesReleasesOnce (#7241)
1 parent 6ba2d78 commit 38ff115

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.appdistribution.impl;
1616

17+
import androidx.annotation.VisibleForTesting;
1718
import com.google.android.gms.tasks.Task;
1819
import com.google.android.gms.tasks.TaskCompletionSource;
1920
import com.google.firebase.annotations.concurrent.Lightweight;
@@ -66,6 +67,20 @@ Task<T> getOrCreateTask(TaskProducer<T> producer) {
6667
return taskCompletionSource.getTask();
6768
}
6869

70+
/**
71+
* Returns a task that completes when this object's internal sequential executor has finished
72+
* processing all enqueued operations.
73+
* <p>
74+
* This method should never be called in production code; however, it is useful in unit tests to
75+
* ensure deterministic behavior.
76+
*/
77+
@VisibleForTesting
78+
Task<Void> newTaskForSequentialExecutorFlush() {
79+
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
80+
sequentialExecutor.execute(() -> taskCompletionSource.setResult(null));
81+
return taskCompletionSource.getTask();
82+
}
83+
6984
private static <T> boolean isOngoing(Task<T> task) {
7085
return task != null && !task.isComplete();
7186
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void checkForNewRelease_whenCalledMultipleTimes_onlyFetchesReleasesOnce()
112112

113113
// Don't set the result until after calling twice, to make sure that the task from the first
114114
// call is still ongoing.
115+
awaitTask(newReleaseFetcher.cachedCheckForNewRelease.newTaskForSequentialExecutorFlush());
115116
task.setResult(null);
116117
awaitTask(checkForNewReleaseTask1);
117118
awaitTask(checkForNewReleaseTask2);

0 commit comments

Comments
 (0)