22
22
import com .google .android .gms .tasks .Task ;
23
23
import com .google .android .gms .tasks .Tasks ;
24
24
import com .google .firebase .concurrent .TestOnlyExecutors ;
25
+ import java .io .IOException ;
25
26
import java .util .ArrayList ;
26
27
import java .util .HashSet ;
27
28
import java .util .List ;
@@ -148,6 +149,25 @@ public void submitCallableThatThrows() {
148
149
assertThat (thrown ).hasCauseThat ().hasMessageThat ().isEqualTo ("I threw in the callable" );
149
150
}
150
151
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
+
151
171
@ Test
152
172
public void submitRunnable () throws Exception {
153
173
Task <Void > task = crashlyticsWorker .submit (() -> {});
@@ -172,6 +192,24 @@ public void submitRunnableThatThrows() {
172
192
assertThat (thrown ).hasCauseThat ().hasMessageThat ().isEqualTo ("I threw in the runnable" );
173
193
}
174
194
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
+
175
213
@ Test
176
214
public void submitTaskThatReturns () throws Exception {
177
215
String skippy = "Think of the problem as an enemy, and defeat them in detail." ;
@@ -251,6 +289,23 @@ public void submitTaskThatCancelsThenAwaitsThenReturns() throws Exception {
251
289
assertThat (result ).isEqualTo ("Valkyrie" );
252
290
}
253
291
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
+
254
309
@ Test
255
310
public void submitTaskThatCancelsThenAwaitsThenRunnable () throws Exception {
256
311
Task <?> cancelled = crashlyticsWorker .submitTask (Tasks ::forCanceled );
@@ -294,7 +349,7 @@ public void submitTaskFromAnotherWorkerThatThrows() throws Exception {
294
349
// Submit another task to local worker to verify the chain did not break.
295
350
Task <Integer > localTask = crashlyticsWorker .submitTask (() -> Tasks .forResult (0x5f375a86 ));
296
351
297
- Integer localResult = Tasks .await (localTask );
352
+ int localResult = Tasks .await (localTask );
298
353
299
354
assertThat (otherTask .isSuccessful ()).isFalse ();
300
355
assertThat (localTask .isSuccessful ()).isTrue ();
@@ -313,7 +368,7 @@ public void submitTaskFromAnotherWorkerThatCancels() throws Exception {
313
368
// Submit another task to local worker to verify the chain did not break.
314
369
Task <Long > localTask = crashlyticsWorker .submitTask (() -> Tasks .forResult (0x5fe6eb50c7b537a9L ));
315
370
316
- Long localResult = Tasks .await (localTask );
371
+ long localResult = Tasks .await (localTask );
317
372
318
373
assertThat (otherCancelled .isCanceled ()).isTrue ();
319
374
assertThat (localTask .isCanceled ()).isFalse ();
@@ -353,14 +408,15 @@ public void submitTaskFromAnotherWorkerDoesNotUseLocalThreads() throws Exception
353
408
354
409
@ Test
355
410
public void submitTaskWhenThreadPoolFull () {
356
- // Fill the backing executor thread pool.
411
+ // Fill the underlying executor thread pool.
357
412
for (int i = 0 ; i < 10 ; i ++) {
358
- crashlyticsWorker .getExecutor ().execute (() -> sleep (1_000 ));
413
+ crashlyticsWorker .getExecutor ().execute (() -> sleep (40 ));
359
414
}
360
415
361
416
Task <Integer > task = crashlyticsWorker .submitTask (() -> Tasks .forResult (42 ));
362
417
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 ));
364
420
}
365
421
366
422
private static void sleep (long millis ) {
0 commit comments