Skip to content

Commit e1e63d3

Browse files
authored
De-flake some tests by waiting for UpdateTasks to complete (#4529)
1 parent 8363396 commit e1e63d3

File tree

4 files changed

+25
-39
lines changed

4 files changed

+25
-39
lines changed

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
package com.google.firebase.appdistribution.impl;
1515

1616
import static com.google.common.truth.Truth.assertThat;
17-
import static com.google.firebase.appdistribution.impl.TestUtils.assertTaskFailure;
1817
import static com.google.firebase.appdistribution.impl.TestUtils.awaitAsyncOperations;
1918
import static com.google.firebase.appdistribution.impl.TestUtils.awaitCondition;
19+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTaskFailure;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.mockito.ArgumentMatchers.anyString;
2222
import static org.mockito.Mockito.times;
@@ -111,52 +111,41 @@ public void setup() throws IOException, FirebaseAppDistributionException {
111111
}
112112

113113
@Test
114-
public void updateAppTask_whenOpenConnectionFails_setsNetworkFailure()
115-
throws IOException, InterruptedException {
114+
public void updateAppTask_whenOpenConnectionFails_setsNetworkFailure() throws IOException {
116115
IOException caughtException = new IOException("error");
117116
when(mockHttpsUrlConnectionFactory.openConnection(TEST_URL)).thenThrow(caughtException);
118117

119118
UpdateTask updateTask = aabUpdater.updateAab(TEST_RELEASE_NEWER_AAB_INTERNAL);
120-
awaitAsyncOperations(blockingExecutor);
121-
awaitAsyncOperations(lightweightExecutor);
122119

123-
assertTaskFailure(
120+
awaitTaskFailure(
124121
updateTask, Status.NETWORK_FAILURE, "Failed to open connection", caughtException);
125122
}
126123

127124
@Test
128-
public void updateAppTask_isNotRedirectResponse_setsDownloadFailure()
129-
throws IOException, InterruptedException {
125+
public void updateAppTask_isNotRedirectResponse_setsDownloadFailure() throws IOException {
130126
when(mockHttpsUrlConnection.getResponseCode()).thenReturn(200);
131127

132128
UpdateTask updateTask = aabUpdater.updateAab(TEST_RELEASE_NEWER_AAB_INTERNAL);
133-
awaitAsyncOperations(blockingExecutor);
134-
awaitAsyncOperations(lightweightExecutor);
135129

136-
assertTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "Expected redirect");
130+
awaitTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "Expected redirect");
137131
}
138132

139133
@Test
140-
public void updateAppTask_missingLocationHeader_setsDownloadFailure()
141-
throws InterruptedException {
134+
public void updateAppTask_missingLocationHeader_setsDownloadFailure() {
142135
when(mockHttpsUrlConnection.getHeaderField("Location")).thenReturn(null);
143136

144137
UpdateTask updateTask = aabUpdater.updateAab(TEST_RELEASE_NEWER_AAB_INTERNAL);
145-
awaitAsyncOperations(blockingExecutor);
146-
awaitAsyncOperations(lightweightExecutor);
147138

148-
assertTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "No Location header");
139+
awaitTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "No Location header");
149140
}
150141

151142
@Test
152-
public void updateAppTask_emptyLocationHeader_setsDownloadFailure() throws InterruptedException {
143+
public void updateAppTask_emptyLocationHeader_setsDownloadFailure() {
153144
when(mockHttpsUrlConnection.getHeaderField("Location")).thenReturn("");
154145

155146
UpdateTask updateTask = aabUpdater.updateAab(TEST_RELEASE_NEWER_AAB_INTERNAL);
156-
awaitAsyncOperations(blockingExecutor);
157-
awaitAsyncOperations(lightweightExecutor);
158147

159-
assertTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "Empty Location header");
148+
awaitTaskFailure(updateTask, Status.DOWNLOAD_FAILURE, "Empty Location header");
160149
}
161150

162151
@Test
@@ -195,7 +184,7 @@ public void updateAppTask_onAppResume_setsUpdateCancelled() throws InterruptedEx
195184
aabUpdater.onActivityStarted(activity);
196185

197186
FirebaseAppDistributionException exception =
198-
assertTaskFailure(updateTask, Status.INSTALLATION_CANCELED, ErrorMessages.UPDATE_CANCELED);
187+
awaitTaskFailure(updateTask, Status.INSTALLATION_CANCELED, ErrorMessages.UPDATE_CANCELED);
199188
assertEquals(
200189
ReleaseUtils.convertToAppDistributionRelease(TEST_RELEASE_NEWER_AAB_INTERNAL),
201190
exception.getRelease());

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
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.assertTaskFailure;
18+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTaskFailure;
1919
import static org.mockito.ArgumentMatchers.any;
2020
import static org.mockito.Mockito.doThrow;
2121
import static org.mockito.Mockito.mock;
@@ -99,7 +99,7 @@ public void consumeForegroundActivity_consumerThrows_fails() {
9999
// Simulate an activity resuming
100100
lifecycleNotifier.onActivityResumed(activity);
101101

102-
assertTaskFailure(task, Status.UNKNOWN, "Unknown", consumerException);
102+
awaitTaskFailure(task, Status.UNKNOWN, "Unknown", consumerException);
103103
}
104104

105105
@Test
@@ -144,7 +144,7 @@ public void applyToForegroundActivity_functionThrows_fails()
144144
// Simulate an activity resuming
145145
lifecycleNotifier.onActivityResumed(activity);
146146

147-
assertTaskFailure(task, Status.UNKNOWN, "Unknown", functionException);
147+
awaitTaskFailure(task, Status.UNKNOWN, "Unknown", functionException);
148148
}
149149

150150
@Test
@@ -190,7 +190,7 @@ public void applyToForegroundActivityTask_continuationThrows_fails() throws Exce
190190
// Simulate an activity resuming
191191
lifecycleNotifier.onActivityResumed(activity);
192192

193-
assertTaskFailure(task, Status.AUTHENTICATION_CANCELED, "exception in continuation task");
193+
awaitTaskFailure(task, Status.AUTHENTICATION_CANCELED, "exception in continuation task");
194194
}
195195

196196
@Test
@@ -204,7 +204,7 @@ public void applyToForegroundActivityTask_continuationThrowsUnknownException_wra
204204
// Simulate an activity resuming
205205
lifecycleNotifier.onActivityResumed(activity);
206206

207-
assertTaskFailure(task, Status.UNKNOWN, "Unknown", continuationException);
207+
awaitTaskFailure(task, Status.UNKNOWN, "Unknown", continuationException);
208208
}
209209

210210
@Test

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,21 @@ final class TestUtils {
4949

5050
private TestUtils() {}
5151

52-
static void awaitTaskFailure(Task task, Status status, String messageSubstring) {
52+
static FirebaseAppDistributionException awaitTaskFailure(
53+
Task task, Status status, String messageSubstring) {
5354
assertThrows(FirebaseAppDistributionException.class, () -> awaitTask(task));
54-
assertTaskFailure(task, status, messageSubstring);
55+
return assertTaskFailure(task, status, messageSubstring);
5556
}
5657

57-
static void awaitTaskFailure(Task task, Status status, String messageSubstring, Throwable cause) {
58+
static FirebaseAppDistributionException awaitTaskFailure(
59+
Task task, Status status, String messageSubstring, Throwable cause) {
5860
assertThrows(FirebaseAppDistributionException.class, () -> awaitTask(task));
59-
assertTaskFailure(task, status, messageSubstring, cause);
61+
FirebaseAppDistributionException exception = assertTaskFailure(task, status, messageSubstring);
62+
assertThat(exception).hasCauseThat().isEqualTo(cause);
63+
return exception;
6064
}
6165

62-
static FirebaseAppDistributionException assertTaskFailure(
66+
private static FirebaseAppDistributionException assertTaskFailure(
6367
Task task, Status status, String messageSubstring) {
6468
assertThat(task.isComplete()).isTrue();
6569
assertThat(task.isSuccessful()).isFalse();
@@ -70,12 +74,6 @@ static FirebaseAppDistributionException assertTaskFailure(
7074
return e;
7175
}
7276

73-
static void assertTaskFailure(
74-
Task task, Status status, String messageSubstring, Throwable cause) {
75-
assertTaskFailure(task, status, messageSubstring);
76-
assertThat(task.getException()).hasCauseThat().isEqualTo(cause);
77-
}
78-
7977
static <T> T awaitTask(Task<T> task)
8078
throws FirebaseAppDistributionException, ExecutionException, InterruptedException {
8179
TestOnCompleteListener<T> onCompleteListener = new TestOnCompleteListener<>();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.AUTHENTICATION_CANCELED;
1919
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.UNKNOWN;
20-
import static com.google.firebase.appdistribution.impl.TestUtils.assertTaskFailure;
2120
import static com.google.firebase.appdistribution.impl.TestUtils.awaitAsyncOperations;
2221
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTask;
2322
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTaskFailure;
@@ -272,6 +271,6 @@ public void signInTester_whenAppReenteredDuringSignIn_taskFails() throws Interru
272271
// Simulate re-entering app before completing sign in
273272
testerSignInManager.onActivityResumed(activity);
274273

275-
assertTaskFailure(signInTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
274+
awaitTaskFailure(signInTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
276275
}
277276
}

0 commit comments

Comments
 (0)