Skip to content

Commit 65ca135

Browse files
committed
Add more test cases as discussed with @themiswang
1 parent 92e693b commit 65ca135

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/CrashlyticsWorkerTest.java

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.android.gms.tasks.Task;
2323
import com.google.android.gms.tasks.Tasks;
2424
import com.google.firebase.concurrent.TestOnlyExecutors;
25+
import java.io.IOException;
2526
import java.util.ArrayList;
2627
import java.util.HashSet;
2728
import java.util.List;
@@ -148,6 +149,25 @@ public void submitCallableThatThrows() {
148149
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("I threw in the callable");
149150
}
150151

152+
@Test
153+
public void submitCallableThatThrowsThenReturns() throws Exception {
154+
Task<Void> throwingTask =
155+
crashlyticsWorker.submit(
156+
() -> {
157+
throw new IOException();
158+
});
159+
160+
assertThrows(ExecutionException.class, () -> Tasks.await(throwingTask));
161+
162+
String hiro =
163+
"When you are wrestling for possession of a sword, the man with the handle always wins.";
164+
Task<String> task = crashlyticsWorker.submitTask(() -> Tasks.forResult(hiro));
165+
166+
String result = Tasks.await(task);
167+
168+
assertThat(result).isEqualTo(hiro);
169+
}
170+
151171
@Test
152172
public void submitRunnable() throws Exception {
153173
Task<Void> task = crashlyticsWorker.submit(() -> {});
@@ -172,6 +192,24 @@ public void submitRunnableThatThrows() {
172192
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("I threw in the runnable");
173193
}
174194

195+
@Test
196+
public void submitRunnableThatThrowsThenReturns() throws Exception {
197+
Task<Void> thowingTask =
198+
crashlyticsWorker.submit(
199+
(Runnable)
200+
() -> {
201+
throw new IllegalArgumentException();
202+
});
203+
204+
assertThrows(ExecutionException.class, () -> Tasks.await(thowingTask));
205+
206+
Task<Void> task = crashlyticsWorker.submit(() -> {});
207+
208+
Void result = Tasks.await(task);
209+
210+
assertThat(result).isNull();
211+
}
212+
175213
@Test
176214
public void submitTaskThatReturns() throws Exception {
177215
String skippy = "Think of the problem as an enemy, and defeat them in detail.";
@@ -251,6 +289,23 @@ public void submitTaskThatCancelsThenAwaitsThenReturns() throws Exception {
251289
assertThat(result).isEqualTo("Valkyrie");
252290
}
253291

292+
@Test
293+
public void submitTaskThatCancelsThenAwaitsThenCallable() throws Exception {
294+
Task<?> cancelled = crashlyticsWorker.submitTask(Tasks::forCanceled);
295+
296+
// Await on the cancelled task to force the exception to propagate.
297+
assertThrows(CancellationException.class, () -> Tasks.await(cancelled));
298+
299+
// Submit a simple callable.
300+
Task<Boolean> task = crashlyticsWorker.submit(() -> true);
301+
302+
boolean result = Tasks.await(task);
303+
304+
assertThat(cancelled.isCanceled()).isTrue();
305+
assertThat(task.isCanceled()).isFalse();
306+
assertThat(result).isTrue();
307+
}
308+
254309
@Test
255310
public void submitTaskThatCancelsThenAwaitsThenRunnable() throws Exception {
256311
Task<?> cancelled = crashlyticsWorker.submitTask(Tasks::forCanceled);
@@ -294,7 +349,7 @@ public void submitTaskFromAnotherWorkerThatThrows() throws Exception {
294349
// Submit another task to local worker to verify the chain did not break.
295350
Task<Integer> localTask = crashlyticsWorker.submitTask(() -> Tasks.forResult(0x5f375a86));
296351

297-
Integer localResult = Tasks.await(localTask);
352+
int localResult = Tasks.await(localTask);
298353

299354
assertThat(otherTask.isSuccessful()).isFalse();
300355
assertThat(localTask.isSuccessful()).isTrue();
@@ -313,7 +368,7 @@ public void submitTaskFromAnotherWorkerThatCancels() throws Exception {
313368
// Submit another task to local worker to verify the chain did not break.
314369
Task<Long> localTask = crashlyticsWorker.submitTask(() -> Tasks.forResult(0x5fe6eb50c7b537a9L));
315370

316-
Long localResult = Tasks.await(localTask);
371+
long localResult = Tasks.await(localTask);
317372

318373
assertThat(otherCancelled.isCanceled()).isTrue();
319374
assertThat(localTask.isCanceled()).isFalse();
@@ -353,14 +408,15 @@ public void submitTaskFromAnotherWorkerDoesNotUseLocalThreads() throws Exception
353408

354409
@Test
355410
public void submitTaskWhenThreadPoolFull() {
356-
// Fill the backing executor thread pool.
411+
// Fill the underlying executor thread pool.
357412
for (int i = 0; i < 10; i++) {
358-
crashlyticsWorker.getExecutor().execute(() -> sleep(1_000));
413+
crashlyticsWorker.getExecutor().execute(() -> sleep(40));
359414
}
360415

361416
Task<Integer> task = crashlyticsWorker.submitTask(() -> Tasks.forResult(42));
362417

363-
assertThrows(TimeoutException.class, () -> Tasks.await(task, 300, TimeUnit.MILLISECONDS));
418+
// The underlying thread pool is full with tasks that will take longer than this timeout.
419+
assertThrows(TimeoutException.class, () -> Tasks.await(task, 30, TimeUnit.MILLISECONDS));
364420
}
365421

366422
private static void sleep(long millis) {

0 commit comments

Comments
 (0)