|
20 | 20 | import static org.junit.Assert.assertThrows;
|
21 | 21 |
|
22 | 22 | import com.google.android.gms.tasks.Task;
|
| 23 | +import com.google.android.gms.tasks.TaskCompletionSource; |
23 | 24 | import com.google.android.gms.tasks.Tasks;
|
24 | 25 | import com.google.firebase.concurrent.TestOnlyExecutors;
|
25 | 26 | import java.io.IOException;
|
@@ -421,6 +422,258 @@ public void submitTaskWhenThreadPoolFull() {
|
421 | 422 | assertThrows(TimeoutException.class, () -> Tasks.await(task, 30, TimeUnit.MILLISECONDS));
|
422 | 423 | }
|
423 | 424 |
|
| 425 | + @Test |
| 426 | + public void submitTaskThatReturnsWithContinuation() throws Exception { |
| 427 | + Task<String> result = |
| 428 | + crashlyticsWorker.submitTask( |
| 429 | + () -> Tasks.forResult(1337), |
| 430 | + task -> Tasks.forResult(Integer.toString(task.getResult()))); |
| 431 | + |
| 432 | + assertThat(Tasks.await(result)).isEqualTo("1337"); |
| 433 | + } |
| 434 | + |
| 435 | + @Test |
| 436 | + public void submitTaskThatThrowsWithContinuation() throws Exception { |
| 437 | + Task<String> result = |
| 438 | + crashlyticsWorker.submitTask( |
| 439 | + () -> Tasks.forException(new IndexOutOfBoundsException("Sometimes we look too far.")), |
| 440 | + task -> { |
| 441 | + if (task.getException() != null) { |
| 442 | + return Tasks.forResult("Task threw."); |
| 443 | + } |
| 444 | + return Tasks.forResult("I dunno how I got here?"); |
| 445 | + }); |
| 446 | + |
| 447 | + assertThat(Tasks.await(result)).isEqualTo("Task threw."); |
| 448 | + } |
| 449 | + |
| 450 | + @Test |
| 451 | + public void submitTaskWithContinuationThatThrows() throws Exception { |
| 452 | + Task<String> result = |
| 453 | + crashlyticsWorker.submitTask( |
| 454 | + () -> Tasks.forResult(7), task -> Tasks.forException(new IOException())); |
| 455 | + |
| 456 | + ExecutionException thrown = assertThrows(ExecutionException.class, () -> Tasks.await(result)); |
| 457 | + |
| 458 | + assertThat(thrown).hasCauseThat().isInstanceOf(IOException.class); |
| 459 | + |
| 460 | + // Verify the worker still executes tasks after the continuation threw. |
| 461 | + assertThat(Tasks.await(crashlyticsWorker.submit(() -> 42))).isEqualTo(42); |
| 462 | + } |
| 463 | + |
| 464 | + @Test |
| 465 | + public void submitTaskThatCancelsWithContinuation() throws Exception { |
| 466 | + Task<String> result = |
| 467 | + crashlyticsWorker.submitTask( |
| 468 | + Tasks::forCanceled, |
| 469 | + task -> Tasks.forResult(task.isCanceled() ? "Task cancelled." : "What?")); |
| 470 | + |
| 471 | + assertThat(Tasks.await(result)).isEqualTo("Task cancelled."); |
| 472 | + } |
| 473 | + |
| 474 | + @Test |
| 475 | + public void submitTaskWithContinuationThatCancels() throws Exception { |
| 476 | + Task<String> result = |
| 477 | + crashlyticsWorker.submitTask(() -> Tasks.forResult(7), task -> Tasks.forCanceled()); |
| 478 | + |
| 479 | + assertThrows(CancellationException.class, () -> Tasks.await(result)); |
| 480 | + |
| 481 | + // Verify the worker still executes tasks after the continuation was cancelled. |
| 482 | + assertThat(Tasks.await(crashlyticsWorker.submit(() -> "jk"))).isEqualTo("jk"); |
| 483 | + } |
| 484 | + |
| 485 | + @Test |
| 486 | + public void submitTaskWithContinuationExecutesInOrder() throws Exception { |
| 487 | + // The integers added to the list represent the order they should be executed in. |
| 488 | + List<Integer> list = new ArrayList<>(); |
| 489 | + |
| 490 | + // Start the chain which adds 1, then kicks off tasks to add 6 & 7 later, but adds 2 before |
| 491 | + // executing the newly added tasks in the continuation. |
| 492 | + crashlyticsWorker.submitTask( |
| 493 | + () -> { |
| 494 | + list.add(1); |
| 495 | + |
| 496 | + // Sleep to give time for the tasks 3, 4, 5 to be submitted. |
| 497 | + sleep(300); |
| 498 | + |
| 499 | + // We added the 1 and will add 2 in the continuation. And 3, 4, 5 have been submitted. |
| 500 | + crashlyticsWorker.submit(() -> list.add(6)); |
| 501 | + crashlyticsWorker.submit(() -> list.add(7)); |
| 502 | + |
| 503 | + return Tasks.forResult(1); |
| 504 | + }, |
| 505 | + task -> { |
| 506 | + // When the task 1 completes the next number to add is 2. Because all the other tasks are |
| 507 | + // just submitted, not executed yet. |
| 508 | + list.add(2); |
| 509 | + return Tasks.forResult("a"); |
| 510 | + }); |
| 511 | + |
| 512 | + // Submit tasks to add 3, 4, 5 since we just added 1 and know a continuation will add the 2. |
| 513 | + crashlyticsWorker.submit(() -> list.add(3)); |
| 514 | + crashlyticsWorker.submit(() -> list.add(4)); |
| 515 | + crashlyticsWorker.submit(() -> list.add(5)); |
| 516 | + |
| 517 | + crashlyticsWorker.await(); |
| 518 | + |
| 519 | + // Verify the list is complete and in order. |
| 520 | + assertThat(list).isInOrder(); |
| 521 | + assertThat(list).hasSize(7); |
| 522 | + } |
| 523 | + |
| 524 | + @Test |
| 525 | + public void raceReturnsFirstResult() throws Exception { |
| 526 | + // Create 2 tasks on different workers to race. |
| 527 | + Task<String> task1 = |
| 528 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 529 | + .submit( |
| 530 | + () -> { |
| 531 | + sleep(200); |
| 532 | + return "first"; |
| 533 | + }); |
| 534 | + Task<String> task2 = |
| 535 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 536 | + .submit( |
| 537 | + () -> { |
| 538 | + sleep(400); |
| 539 | + return "slow"; |
| 540 | + }); |
| 541 | + |
| 542 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 543 | + String result = Tasks.await(task); |
| 544 | + |
| 545 | + assertThat(result).isEqualTo("first"); |
| 546 | + } |
| 547 | + |
| 548 | + @Test |
| 549 | + public void raceReturnsFirstException() { |
| 550 | + // Create 2 tasks on different workers to race. |
| 551 | + Task<String> task1 = |
| 552 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 553 | + .submitTask( |
| 554 | + () -> { |
| 555 | + sleep(200); |
| 556 | + return Tasks.forException(new ArithmeticException()); |
| 557 | + }); |
| 558 | + Task<String> task2 = |
| 559 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 560 | + .submitTask( |
| 561 | + () -> { |
| 562 | + sleep(400); |
| 563 | + return Tasks.forException(new IllegalStateException()); |
| 564 | + }); |
| 565 | + |
| 566 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 567 | + ExecutionException thrown = assertThrows(ExecutionException.class, () -> Tasks.await(task)); |
| 568 | + |
| 569 | + // The first task throws an ArithmeticException. |
| 570 | + assertThat(thrown).hasCauseThat().isInstanceOf(ArithmeticException.class); |
| 571 | + } |
| 572 | + |
| 573 | + @Test |
| 574 | + public void raceFirstCancelsReturnsSecondResult() throws Exception { |
| 575 | + // Create 2 tasks on different workers to race. |
| 576 | + Task<String> task1 = |
| 577 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 578 | + .submitTask( |
| 579 | + () -> { |
| 580 | + sleep(200); |
| 581 | + return Tasks.forCanceled(); |
| 582 | + }); |
| 583 | + Task<String> task2 = |
| 584 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 585 | + .submitTask( |
| 586 | + () -> { |
| 587 | + sleep(400); |
| 588 | + return Tasks.forResult("I am slow but didn't cancel."); |
| 589 | + }); |
| 590 | + |
| 591 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 592 | + String result = Tasks.await(task); |
| 593 | + |
| 594 | + assertThat(result).isEqualTo("I am slow but didn't cancel."); |
| 595 | + } |
| 596 | + |
| 597 | + @Test |
| 598 | + public void raceBothCancel() { |
| 599 | + // Create 2 tasks on different workers to race. |
| 600 | + Task<String> task1 = |
| 601 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 602 | + .submitTask( |
| 603 | + () -> { |
| 604 | + sleep(200); |
| 605 | + return Tasks.forCanceled(); |
| 606 | + }); |
| 607 | + Task<String> task2 = |
| 608 | + new CrashlyticsWorker(TestOnlyExecutors.background()) |
| 609 | + .submitTask( |
| 610 | + () -> { |
| 611 | + sleep(400); |
| 612 | + return Tasks.forCanceled(); |
| 613 | + }); |
| 614 | + |
| 615 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 616 | + |
| 617 | + // Both cancelled, so cancel the race result. |
| 618 | + assertThrows(CancellationException.class, () -> Tasks.await(task)); |
| 619 | + } |
| 620 | + |
| 621 | + @Test |
| 622 | + public void raceTasksOnSameWorker() throws Exception { |
| 623 | + // Create 2 tasks on this worker to race. |
| 624 | + Task<String> task1 = |
| 625 | + crashlyticsWorker.submit( |
| 626 | + () -> { |
| 627 | + sleep(200); |
| 628 | + return "first"; |
| 629 | + }); |
| 630 | + Task<String> task2 = |
| 631 | + crashlyticsWorker.submit( |
| 632 | + () -> { |
| 633 | + sleep(300); |
| 634 | + return "second"; |
| 635 | + }); |
| 636 | + |
| 637 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 638 | + String result = Tasks.await(task); |
| 639 | + |
| 640 | + // The first task is submitted to this worker first, so will always be first. |
| 641 | + assertThat(result).isEqualTo("first"); |
| 642 | + } |
| 643 | + |
| 644 | + @Test |
| 645 | + public void raceTaskOneOnSameWorkerAnotherNeverCompletes() throws Exception { |
| 646 | + // Create a task on this worker, and another that never completes, to race. |
| 647 | + Task<String> task1 = crashlyticsWorker.submit(() -> "first"); |
| 648 | + Task<String> task2 = new TaskCompletionSource<String>().getTask(); |
| 649 | + |
| 650 | + Task<String> task = crashlyticsWorker.race(task1, task2); |
| 651 | + String result = Tasks.await(task); |
| 652 | + |
| 653 | + assertThat(result).isEqualTo("first"); |
| 654 | + } |
| 655 | + |
| 656 | + @Test |
| 657 | + public void raceTaskOneOnSameWorkerAnotherOtherThatCompletesFirst() throws Exception { |
| 658 | + // Add a decoy task to the worker to take up some time. |
| 659 | + crashlyticsWorker.submitTask( |
| 660 | + () -> { |
| 661 | + sleep(200); |
| 662 | + return Tasks.forResult(null); |
| 663 | + }); |
| 664 | + |
| 665 | + // Create a task on this worker, and another, to race. |
| 666 | + Task<String> task1 = crashlyticsWorker.submit(() -> "same worker"); |
| 667 | + TaskCompletionSource<String> task2 = new TaskCompletionSource<>(); |
| 668 | + task2.trySetResult("other"); |
| 669 | + |
| 670 | + Task<String> task = crashlyticsWorker.race(task1, task2.getTask()); |
| 671 | + String result = Tasks.await(task); |
| 672 | + |
| 673 | + // The other tasks completes first because the first task is queued up later on the worker. |
| 674 | + assertThat(result).isEqualTo("other"); |
| 675 | + } |
| 676 | + |
424 | 677 | private static void sleep(long millis) {
|
425 | 678 | try {
|
426 | 679 | Thread.sleep(millis);
|
|
0 commit comments