247 |
|
f = new CompletableFuture<>(); |
248 |
|
f.obtrudeValue(three); |
249 |
|
checkCompletedNormally(f, three); |
250 |
+ |
f.obtrudeValue(null); |
251 |
+ |
checkCompletedNormally(f, null); |
252 |
|
f = new CompletableFuture<>(); |
253 |
|
f.completeExceptionally(new CFException()); |
254 |
|
f.obtrudeValue(four); |
281 |
|
*/ |
282 |
|
public void testGetNumberOfDependents() { |
283 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
284 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
284 |
> |
assertEquals(0, f.getNumberOfDependents()); |
285 |
|
CompletableFuture g = f.thenRun(new Noop()); |
286 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
287 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
286 |
> |
assertEquals(1, f.getNumberOfDependents()); |
287 |
> |
assertEquals(0, g.getNumberOfDependents()); |
288 |
|
CompletableFuture h = f.thenRun(new Noop()); |
289 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
289 |
> |
assertEquals(2, f.getNumberOfDependents()); |
290 |
|
f.complete(1); |
291 |
|
checkCompletedNormally(g, null); |
292 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
293 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
292 |
> |
assertEquals(0, f.getNumberOfDependents()); |
293 |
> |
assertEquals(0, g.getNumberOfDependents()); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
452 |
|
} |
453 |
|
} |
454 |
|
|
453 |
– |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
454 |
– |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
455 |
– |
} |
456 |
– |
|
457 |
– |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
458 |
– |
int invocationCount = 0; |
459 |
– |
public Integer apply(Integer x, Throwable t) { |
460 |
– |
invocationCount++; |
461 |
– |
return (t == null) ? two : three; |
462 |
– |
} |
463 |
– |
} |
464 |
– |
|
455 |
|
/** |
456 |
|
* Permits the testing of parallel code for the 3 different |
457 |
|
* execution modes without repeating all the testing code. |
458 |
|
*/ |
459 |
|
enum ExecutionMode { |
460 |
|
DEFAULT { |
461 |
+ |
public <T> CompletableFuture<Void> thenRun |
462 |
+ |
(CompletableFuture<T> f, Runnable a) { |
463 |
+ |
return f.thenRun(a); |
464 |
+ |
} |
465 |
+ |
public <T> CompletableFuture<Void> thenAccept |
466 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a) { |
467 |
+ |
return f.thenAccept(a); |
468 |
+ |
} |
469 |
+ |
public <T,U> CompletableFuture<U> thenApply |
470 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a) { |
471 |
+ |
return f.thenApply(a); |
472 |
+ |
} |
473 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
474 |
+ |
(CompletableFuture<T> f, |
475 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
476 |
+ |
return f.thenCompose(a); |
477 |
+ |
} |
478 |
+ |
public <T,U> CompletableFuture<U> handle |
479 |
+ |
(CompletableFuture<T> f, |
480 |
+ |
BiFunction<? super T,Throwable,? extends U> a) { |
481 |
+ |
return f.handle(a); |
482 |
+ |
} |
483 |
+ |
public <T> CompletableFuture<T> whenComplete |
484 |
+ |
(CompletableFuture<T> f, |
485 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
486 |
+ |
return f.whenComplete(a); |
487 |
+ |
} |
488 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
489 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
490 |
|
return f.runAfterBoth(g, a); |
501 |
|
BiFunction<? super T,? super U,? extends V> a) { |
502 |
|
return f.thenCombine(g, a); |
503 |
|
} |
504 |
< |
public <T,U> CompletableFuture<U> applyToEither |
504 |
> |
public <T> CompletableFuture<Void> runAfterEither |
505 |
|
(CompletableFuture<T> f, |
506 |
< |
CompletionStage<? extends T> g, |
507 |
< |
Function<? super T,U> a) { |
508 |
< |
return f.applyToEither(g, a); |
506 |
> |
CompletionStage<?> g, |
507 |
> |
java.lang.Runnable a) { |
508 |
> |
return f.runAfterEither(g, a); |
509 |
|
} |
510 |
|
public <T> CompletableFuture<Void> acceptEither |
511 |
|
(CompletableFuture<T> f, |
513 |
|
Consumer<? super T> a) { |
514 |
|
return f.acceptEither(g, a); |
515 |
|
} |
516 |
< |
public <T> CompletableFuture<Void> runAfterEither |
516 |
> |
public <T,U> CompletableFuture<U> applyToEither |
517 |
|
(CompletableFuture<T> f, |
518 |
< |
CompletionStage<?> g, |
519 |
< |
java.lang.Runnable a) { |
520 |
< |
return f.runAfterEither(g, a); |
518 |
> |
CompletionStage<? extends T> g, |
519 |
> |
Function<? super T,U> a) { |
520 |
> |
return f.applyToEither(g, a); |
521 |
> |
} |
522 |
> |
}, |
523 |
> |
|
524 |
> |
DEFAULT_ASYNC { |
525 |
> |
public <T> CompletableFuture<Void> thenRun |
526 |
> |
(CompletableFuture<T> f, Runnable a) { |
527 |
> |
return f.thenRunAsync(a); |
528 |
> |
} |
529 |
> |
public <T> CompletableFuture<Void> thenAccept |
530 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
531 |
> |
return f.thenAcceptAsync(a); |
532 |
> |
} |
533 |
> |
public <T,U> CompletableFuture<U> thenApply |
534 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
535 |
> |
return f.thenApplyAsync(a); |
536 |
|
} |
537 |
|
public <T,U> CompletableFuture<U> thenCompose |
538 |
|
(CompletableFuture<T> f, |
539 |
|
Function<? super T,? extends CompletionStage<U>> a) { |
540 |
< |
return f.thenCompose(a); |
540 |
> |
return f.thenComposeAsync(a); |
541 |
> |
} |
542 |
> |
public <T,U> CompletableFuture<U> handle |
543 |
> |
(CompletableFuture<T> f, |
544 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
545 |
> |
return f.handleAsync(a); |
546 |
|
} |
547 |
|
public <T> CompletableFuture<T> whenComplete |
548 |
|
(CompletableFuture<T> f, |
549 |
|
BiConsumer<? super T,? super Throwable> a) { |
550 |
< |
return f.whenComplete(a); |
550 |
> |
return f.whenCompleteAsync(a); |
551 |
|
} |
515 |
– |
}, |
516 |
– |
|
517 |
– |
DEFAULT_ASYNC { |
552 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
553 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
554 |
|
return f.runAfterBothAsync(g, a); |
565 |
|
BiFunction<? super T,? super U,? extends V> a) { |
566 |
|
return f.thenCombineAsync(g, a); |
567 |
|
} |
568 |
< |
public <T,U> CompletableFuture<U> applyToEither |
568 |
> |
public <T> CompletableFuture<Void> runAfterEither |
569 |
|
(CompletableFuture<T> f, |
570 |
< |
CompletionStage<? extends T> g, |
571 |
< |
Function<? super T,U> a) { |
572 |
< |
return f.applyToEitherAsync(g, a); |
570 |
> |
CompletionStage<?> g, |
571 |
> |
java.lang.Runnable a) { |
572 |
> |
return f.runAfterEitherAsync(g, a); |
573 |
|
} |
574 |
|
public <T> CompletableFuture<Void> acceptEither |
575 |
|
(CompletableFuture<T> f, |
577 |
|
Consumer<? super T> a) { |
578 |
|
return f.acceptEitherAsync(g, a); |
579 |
|
} |
580 |
< |
public <T> CompletableFuture<Void> runAfterEither |
580 |
> |
public <T,U> CompletableFuture<U> applyToEither |
581 |
|
(CompletableFuture<T> f, |
582 |
< |
CompletionStage<?> g, |
583 |
< |
java.lang.Runnable a) { |
584 |
< |
return f.runAfterEitherAsync(g, a); |
582 |
> |
CompletionStage<? extends T> g, |
583 |
> |
Function<? super T,U> a) { |
584 |
> |
return f.applyToEitherAsync(g, a); |
585 |
> |
} |
586 |
> |
}, |
587 |
> |
|
588 |
> |
EXECUTOR { |
589 |
> |
public <T> CompletableFuture<Void> thenRun |
590 |
> |
(CompletableFuture<T> f, Runnable a) { |
591 |
> |
return f.thenRunAsync(a, new ThreadExecutor()); |
592 |
> |
} |
593 |
> |
public <T> CompletableFuture<Void> thenAccept |
594 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
595 |
> |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
596 |
> |
} |
597 |
> |
public <T,U> CompletableFuture<U> thenApply |
598 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
599 |
> |
return f.thenApplyAsync(a, new ThreadExecutor()); |
600 |
|
} |
601 |
|
public <T,U> CompletableFuture<U> thenCompose |
602 |
|
(CompletableFuture<T> f, |
603 |
|
Function<? super T,? extends CompletionStage<U>> a) { |
604 |
< |
return f.thenComposeAsync(a); |
604 |
> |
return f.thenComposeAsync(a, new ThreadExecutor()); |
605 |
> |
} |
606 |
> |
public <T,U> CompletableFuture<U> handle |
607 |
> |
(CompletableFuture<T> f, |
608 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
609 |
> |
return f.handleAsync(a, new ThreadExecutor()); |
610 |
|
} |
611 |
|
public <T> CompletableFuture<T> whenComplete |
612 |
|
(CompletableFuture<T> f, |
613 |
|
BiConsumer<? super T,? super Throwable> a) { |
614 |
< |
return f.whenCompleteAsync(a); |
614 |
> |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
615 |
|
} |
562 |
– |
}, |
563 |
– |
|
564 |
– |
EXECUTOR { |
616 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
617 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
618 |
|
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
629 |
|
BiFunction<? super T,? super U,? extends V> a) { |
630 |
|
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
631 |
|
} |
581 |
– |
public <T,U> CompletableFuture<U> applyToEither |
582 |
– |
(CompletableFuture<T> f, |
583 |
– |
CompletionStage<? extends T> g, |
584 |
– |
Function<? super T,U> a) { |
585 |
– |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
586 |
– |
} |
587 |
– |
public <T> CompletableFuture<Void> acceptEither |
588 |
– |
(CompletableFuture<T> f, |
589 |
– |
CompletionStage<? extends T> g, |
590 |
– |
Consumer<? super T> a) { |
591 |
– |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
592 |
– |
} |
632 |
|
public <T> CompletableFuture<Void> runAfterEither |
633 |
|
(CompletableFuture<T> f, |
634 |
|
CompletionStage<?> g, |
635 |
|
java.lang.Runnable a) { |
636 |
|
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
637 |
|
} |
638 |
< |
public <T,U> CompletableFuture<U> thenCompose |
638 |
> |
public <T> CompletableFuture<Void> acceptEither |
639 |
|
(CompletableFuture<T> f, |
640 |
< |
Function<? super T,? extends CompletionStage<U>> a) { |
641 |
< |
return f.thenComposeAsync(a, new ThreadExecutor()); |
640 |
> |
CompletionStage<? extends T> g, |
641 |
> |
Consumer<? super T> a) { |
642 |
> |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
643 |
|
} |
644 |
< |
public <T> CompletableFuture<T> whenComplete |
644 |
> |
public <T,U> CompletableFuture<U> applyToEither |
645 |
|
(CompletableFuture<T> f, |
646 |
< |
BiConsumer<? super T,? super Throwable> a) { |
647 |
< |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
646 |
> |
CompletionStage<? extends T> g, |
647 |
> |
Function<? super T,U> a) { |
648 |
> |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
649 |
|
} |
650 |
|
}; |
651 |
|
|
652 |
+ |
public abstract <T> CompletableFuture<Void> thenRun |
653 |
+ |
(CompletableFuture<T> f, Runnable a); |
654 |
+ |
public abstract <T> CompletableFuture<Void> thenAccept |
655 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a); |
656 |
+ |
public abstract <T,U> CompletableFuture<U> thenApply |
657 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a); |
658 |
+ |
public abstract <T,U> CompletableFuture<U> thenCompose |
659 |
+ |
(CompletableFuture<T> f, |
660 |
+ |
Function<? super T,? extends CompletionStage<U>> a); |
661 |
+ |
public abstract <T,U> CompletableFuture<U> handle |
662 |
+ |
(CompletableFuture<T> f, |
663 |
+ |
BiFunction<? super T,Throwable,? extends U> a); |
664 |
+ |
public abstract <T> CompletableFuture<T> whenComplete |
665 |
+ |
(CompletableFuture<T> f, |
666 |
+ |
BiConsumer<? super T,? super Throwable> a); |
667 |
|
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
668 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
669 |
|
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
674 |
|
(CompletableFuture<T> f, |
675 |
|
CompletionStage<? extends U> g, |
676 |
|
BiFunction<? super T,? super U,? extends V> a); |
621 |
– |
public abstract <T,U> CompletableFuture<U> applyToEither |
622 |
– |
(CompletableFuture<T> f, |
623 |
– |
CompletionStage<? extends T> g, |
624 |
– |
Function<? super T,U> a); |
625 |
– |
public abstract <T> CompletableFuture<Void> acceptEither |
626 |
– |
(CompletableFuture<T> f, |
627 |
– |
CompletionStage<? extends T> g, |
628 |
– |
Consumer<? super T> a); |
677 |
|
public abstract <T> CompletableFuture<Void> runAfterEither |
678 |
|
(CompletableFuture<T> f, |
679 |
|
CompletionStage<?> g, |
680 |
|
java.lang.Runnable a); |
681 |
< |
public abstract <T,U> CompletableFuture<U> thenCompose |
681 |
> |
public abstract <T> CompletableFuture<Void> acceptEither |
682 |
|
(CompletableFuture<T> f, |
683 |
< |
Function<? super T,? extends CompletionStage<U>> a); |
684 |
< |
public abstract <T> CompletableFuture<T> whenComplete |
683 |
> |
CompletionStage<? extends T> g, |
684 |
> |
Consumer<? super T> a); |
685 |
> |
public abstract <T,U> CompletableFuture<U> applyToEither |
686 |
|
(CompletableFuture<T> f, |
687 |
< |
BiConsumer<? super T,? super Throwable> a); |
687 |
> |
CompletionStage<? extends T> g, |
688 |
> |
Function<? super T,U> a); |
689 |
> |
} |
690 |
|
|
691 |
+ |
/** |
692 |
+ |
* exceptionally action is not invoked when source completes |
693 |
+ |
* normally, and source result is propagated |
694 |
+ |
*/ |
695 |
+ |
public void testExceptionally_normalCompletion() { |
696 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
697 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
698 |
+ |
{ |
699 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
700 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
701 |
+ |
if (!createIncomplete) f.complete(v1); |
702 |
+ |
final CompletableFuture<Integer> g = f.exceptionally |
703 |
+ |
((Throwable t) -> { |
704 |
+ |
// Should not be called |
705 |
+ |
a.getAndIncrement(); |
706 |
+ |
throw new AssertionError(); |
707 |
+ |
}); |
708 |
+ |
if (createIncomplete) f.complete(v1); |
709 |
+ |
|
710 |
+ |
checkCompletedNormally(g, v1); |
711 |
+ |
checkCompletedNormally(f, v1); |
712 |
+ |
assertEquals(0, a.get()); |
713 |
+ |
}} |
714 |
|
|
641 |
– |
} |
715 |
|
|
716 |
|
/** |
717 |
|
* exceptionally action completes with function value on source |
718 |
< |
* exception; otherwise with source value |
718 |
> |
* exception |
719 |
|
*/ |
720 |
< |
public void testExceptionally() { |
721 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
722 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
723 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
724 |
< |
f.completeExceptionally(new CFException()); |
725 |
< |
checkCompletedNormally(g, three); |
720 |
> |
public void testExceptionally_exceptionalCompletion() { |
721 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
722 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
723 |
> |
{ |
724 |
> |
final AtomicInteger a = new AtomicInteger(0); |
725 |
> |
final CFException ex = new CFException(); |
726 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
727 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
728 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
729 |
> |
((Throwable t) -> { |
730 |
> |
threadAssertSame(t, ex); |
731 |
> |
a.getAndIncrement(); |
732 |
> |
return v1; |
733 |
> |
}); |
734 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
735 |
|
|
736 |
< |
f = new CompletableFuture<>(); |
737 |
< |
r = new ExceptionToInteger(); |
738 |
< |
g = f.exceptionally(r); |
739 |
< |
f.complete(one); |
740 |
< |
checkCompletedNormally(g, one); |
741 |
< |
} |
736 |
> |
checkCompletedNormally(g, v1); |
737 |
> |
assertEquals(1, a.get()); |
738 |
> |
}} |
739 |
> |
|
740 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
741 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
742 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
743 |
> |
{ |
744 |
> |
final AtomicInteger a = new AtomicInteger(0); |
745 |
> |
final CFException ex1 = new CFException(); |
746 |
> |
final CFException ex2 = new CFException(); |
747 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
748 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
749 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
750 |
> |
((Throwable t) -> { |
751 |
> |
threadAssertSame(t, ex1); |
752 |
> |
a.getAndIncrement(); |
753 |
> |
throw ex2; |
754 |
> |
}); |
755 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
756 |
> |
|
757 |
> |
checkCompletedWithWrappedCFException(g, ex2); |
758 |
> |
assertEquals(1, a.get()); |
759 |
> |
}} |
760 |
|
|
761 |
|
/** |
762 |
< |
* handle action completes normally with function value on either |
763 |
< |
* normal or exceptional completion of source |
762 |
> |
* handle action completes normally with function value on normal |
763 |
> |
* completion of source |
764 |
|
*/ |
765 |
< |
public void testHandle() { |
766 |
< |
CompletableFuture<Integer> f, g; |
767 |
< |
IntegerHandler r; |
765 |
> |
public void testHandle_normalCompletion() { |
766 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
767 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
768 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
769 |
> |
{ |
770 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
771 |
> |
final AtomicInteger a = new AtomicInteger(0); |
772 |
> |
if (!createIncomplete) f.complete(v1); |
773 |
> |
final CompletableFuture<Integer> g = m.handle |
774 |
> |
(f, |
775 |
> |
(Integer x, Throwable t) -> { |
776 |
> |
threadAssertSame(x, v1); |
777 |
> |
threadAssertNull(t); |
778 |
> |
a.getAndIncrement(); |
779 |
> |
return inc(v1); |
780 |
> |
}); |
781 |
> |
if (createIncomplete) f.complete(v1); |
782 |
|
|
783 |
< |
f = new CompletableFuture<>(); |
784 |
< |
f.completeExceptionally(new CFException()); |
785 |
< |
g = f.handle(r = new IntegerHandler()); |
786 |
< |
assertEquals(1, r.invocationCount); |
673 |
< |
assertEquals(1, r.invocationCount); |
674 |
< |
checkCompletedNormally(g, three); |
783 |
> |
checkCompletedNormally(g, inc(v1)); |
784 |
> |
checkCompletedNormally(f, v1); |
785 |
> |
assertEquals(1, a.get()); |
786 |
> |
}} |
787 |
|
|
788 |
< |
f = new CompletableFuture<>(); |
789 |
< |
g = f.handle(r = new IntegerHandler()); |
790 |
< |
assertEquals(0, r.invocationCount); |
791 |
< |
f.completeExceptionally(new CFException()); |
792 |
< |
checkCompletedNormally(g, three); |
793 |
< |
assertEquals(1, r.invocationCount); |
788 |
> |
/** |
789 |
> |
* handle action completes normally with function value on |
790 |
> |
* exceptional completion of source |
791 |
> |
*/ |
792 |
> |
public void testHandle_exceptionalCompletion() { |
793 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
794 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
795 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
796 |
> |
{ |
797 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
798 |
> |
final AtomicInteger a = new AtomicInteger(0); |
799 |
> |
final CFException ex = new CFException(); |
800 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
801 |
> |
final CompletableFuture<Integer> g = m.handle |
802 |
> |
(f, |
803 |
> |
(Integer x, Throwable t) -> { |
804 |
> |
threadAssertNull(x); |
805 |
> |
threadAssertSame(t, ex); |
806 |
> |
a.getAndIncrement(); |
807 |
> |
return v1; |
808 |
> |
}); |
809 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
810 |
|
|
811 |
< |
f = new CompletableFuture<>(); |
812 |
< |
f.complete(one); |
813 |
< |
g = f.handle(r = new IntegerHandler()); |
814 |
< |
assertEquals(1, r.invocationCount); |
687 |
< |
checkCompletedNormally(g, two); |
811 |
> |
checkCompletedNormally(g, v1); |
812 |
> |
checkCompletedWithWrappedCFException(f, ex); |
813 |
> |
assertEquals(1, a.get()); |
814 |
> |
}} |
815 |
|
|
816 |
< |
f = new CompletableFuture<>(); |
817 |
< |
g = f.handle(r = new IntegerHandler()); |
818 |
< |
assertEquals(0, r.invocationCount); |
819 |
< |
f.complete(one); |
820 |
< |
assertEquals(1, r.invocationCount); |
821 |
< |
checkCompletedNormally(g, two); |
822 |
< |
} |
816 |
> |
/** |
817 |
> |
* handle action completes normally with function value on |
818 |
> |
* cancelled source |
819 |
> |
*/ |
820 |
> |
public void testHandle_sourceCancelled() { |
821 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
822 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
823 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
824 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
825 |
> |
{ |
826 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
827 |
> |
final AtomicInteger a = new AtomicInteger(0); |
828 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
829 |
> |
final CompletableFuture<Integer> g = m.handle |
830 |
> |
(f, |
831 |
> |
(Integer x, Throwable t) -> { |
832 |
> |
threadAssertNull(x); |
833 |
> |
threadAssertTrue(t instanceof CancellationException); |
834 |
> |
a.getAndIncrement(); |
835 |
> |
return v1; |
836 |
> |
}); |
837 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
838 |
> |
|
839 |
> |
checkCompletedNormally(g, v1); |
840 |
> |
checkCancelled(f); |
841 |
> |
assertEquals(1, a.get()); |
842 |
> |
}} |
843 |
> |
|
844 |
> |
/** |
845 |
> |
* handle result completes exceptionally if action does |
846 |
> |
*/ |
847 |
> |
public void testHandle_sourceFailedActionFailed() { |
848 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
849 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
850 |
> |
{ |
851 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
852 |
> |
final AtomicInteger a = new AtomicInteger(0); |
853 |
> |
final CFException ex1 = new CFException(); |
854 |
> |
final CFException ex2 = new CFException(); |
855 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
856 |
> |
final CompletableFuture<Integer> g = m.handle |
857 |
> |
(f, |
858 |
> |
(Integer x, Throwable t) -> { |
859 |
> |
threadAssertNull(x); |
860 |
> |
threadAssertSame(ex1, t); |
861 |
> |
a.getAndIncrement(); |
862 |
> |
throw ex2; |
863 |
> |
}); |
864 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
865 |
> |
|
866 |
> |
checkCompletedWithWrappedCFException(g, ex2); |
867 |
> |
checkCompletedWithWrappedCFException(f, ex1); |
868 |
> |
assertEquals(1, a.get()); |
869 |
> |
}} |
870 |
> |
|
871 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
872 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
873 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
874 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
875 |
> |
{ |
876 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
877 |
> |
final AtomicInteger a = new AtomicInteger(0); |
878 |
> |
final CFException ex = new CFException(); |
879 |
> |
if (!createIncomplete) f.complete(v1); |
880 |
> |
final CompletableFuture<Integer> g = m.handle |
881 |
> |
(f, |
882 |
> |
(Integer x, Throwable t) -> { |
883 |
> |
threadAssertSame(x, v1); |
884 |
> |
threadAssertNull(t); |
885 |
> |
a.getAndIncrement(); |
886 |
> |
throw ex; |
887 |
> |
}); |
888 |
> |
if (createIncomplete) f.complete(v1); |
889 |
> |
|
890 |
> |
checkCompletedWithWrappedCFException(g, ex); |
891 |
> |
checkCompletedNormally(f, v1); |
892 |
> |
assertEquals(1, a.get()); |
893 |
> |
}} |
894 |
|
|
895 |
|
/** |
896 |
|
* runAsync completes after running Runnable |
1397 |
|
g.complete(v2); |
1398 |
|
|
1399 |
|
checkCompletedNormally(h, null); |
1400 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1400 |
> |
assertEquals(subtract(v1, v2), r.value); |
1401 |
|
checkCompletedNormally(f, v1); |
1402 |
|
checkCompletedNormally(g, v2); |
1403 |
|
} |
1419 |
|
f.complete(v1); |
1420 |
|
|
1421 |
|
checkCompletedNormally(h, null); |
1422 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1422 |
> |
assertEquals(subtract(v1, v2), r.value); |
1423 |
|
checkCompletedNormally(f, v1); |
1424 |
|
checkCompletedNormally(g, v2); |
1425 |
|
} |
1439 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1440 |
|
|
1441 |
|
checkCompletedNormally(h, null); |
1442 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1442 |
> |
assertEquals(subtract(v1, v2), r.value); |
1443 |
|
checkCompletedNormally(f, v1); |
1444 |
|
checkCompletedNormally(g, v2); |
1445 |
|
} |
1459 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1460 |
|
|
1461 |
|
checkCompletedNormally(h, null); |
1462 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1462 |
> |
assertEquals(subtract(v1, v2), r.value); |
1463 |
|
checkCompletedNormally(f, v1); |
1464 |
|
checkCompletedNormally(g, v2); |
1465 |
|
} |
2109 |
|
// unspecified behavior |
2110 |
|
Integer v; |
2111 |
|
try { |
2112 |
< |
assertEquals(h.join(), inc(v1)); |
2112 |
> |
assertEquals(inc(v1), h.join()); |
2113 |
|
assertEquals(1, r.invocationCount); |
2114 |
|
} catch (CompletionException ok) { |
2115 |
|
checkCompletedWithWrappedCFException(h, ex); |
2137 |
|
// unspecified behavior |
2138 |
|
Integer v; |
2139 |
|
try { |
2140 |
< |
assertEquals(h.join(), inc(v1)); |
2140 |
> |
assertEquals(inc(v1), h.join()); |
2141 |
|
assertEquals(1, r.invocationCount); |
2142 |
|
} catch (CompletionException ok) { |
2143 |
|
checkCompletedWithWrappedCFException(h, ex); |
2249 |
|
// unspecified behavior |
2250 |
|
Integer v; |
2251 |
|
try { |
2252 |
< |
assertEquals(h.join(), inc(v1)); |
2252 |
> |
assertEquals(inc(v1), h.join()); |
2253 |
|
assertEquals(1, r.invocationCount); |
2254 |
|
} catch (CompletionException ok) { |
2255 |
|
checkCompletedWithWrappedCancellationException(h); |
2277 |
|
// unspecified behavior |
2278 |
|
Integer v; |
2279 |
|
try { |
2280 |
< |
assertEquals(h.join(), inc(v1)); |
2280 |
> |
assertEquals(inc(v1), h.join()); |
2281 |
|
assertEquals(1, r.invocationCount); |
2282 |
|
} catch (CompletionException ok) { |
2283 |
|
checkCompletedWithWrappedCancellationException(h); |
2305 |
|
|
2306 |
|
f.complete(v1); |
2307 |
|
checkCompletedNormally(h, null); |
2308 |
< |
assertEquals(r.value, inc(v1)); |
2308 |
> |
assertEquals(inc(v1), r.value); |
2309 |
|
g.complete(v2); |
2310 |
|
|
2311 |
|
checkCompletedNormally(f, v1); |
2326 |
|
|
2327 |
|
g.complete(v2); |
2328 |
|
checkCompletedNormally(h, null); |
2329 |
< |
assertEquals(r.value, inc(v2)); |
2329 |
> |
assertEquals(inc(v2), r.value); |
2330 |
|
f.complete(v1); |
2331 |
|
|
2332 |
|
checkCompletedNormally(f, v1); |
2419 |
|
// unspecified behavior |
2420 |
|
Integer v; |
2421 |
|
try { |
2422 |
< |
assertEquals(h.join(), null); |
2422 |
> |
assertNull(h.join()); |
2423 |
|
assertEquals(1, r.invocationCount); |
2424 |
|
assertEquals(inc(v1), r.value); |
2425 |
|
} catch (CompletionException ok) { |
2448 |
|
// unspecified behavior |
2449 |
|
Integer v; |
2450 |
|
try { |
2451 |
< |
assertEquals(h.join(), null); |
2451 |
> |
assertNull(h.join()); |
2452 |
|
assertEquals(1, r.invocationCount); |
2453 |
|
assertEquals(inc(v1), r.value); |
2454 |
|
} catch (CompletionException ok) { |
2561 |
|
// unspecified behavior |
2562 |
|
Integer v; |
2563 |
|
try { |
2564 |
< |
assertEquals(h.join(), null); |
2564 |
> |
assertNull(h.join()); |
2565 |
|
assertEquals(1, r.invocationCount); |
2566 |
|
assertEquals(inc(v1), r.value); |
2567 |
|
} catch (CompletionException ok) { |
2590 |
|
// unspecified behavior |
2591 |
|
Integer v; |
2592 |
|
try { |
2593 |
< |
assertEquals(h.join(), null); |
2593 |
> |
assertNull(h.join()); |
2594 |
|
assertEquals(1, r.invocationCount); |
2595 |
|
assertEquals(inc(v1), r.value); |
2596 |
|
} catch (CompletionException ok) { |
2732 |
|
// unspecified behavior |
2733 |
|
Integer v; |
2734 |
|
try { |
2735 |
< |
assertEquals(h.join(), null); |
2735 |
> |
assertNull(h.join()); |
2736 |
|
assertEquals(1, r.invocationCount); |
2737 |
|
} catch (CompletionException ok) { |
2738 |
|
checkCompletedWithWrappedCFException(h, ex); |
2760 |
|
// unspecified behavior |
2761 |
|
Integer v; |
2762 |
|
try { |
2763 |
< |
assertEquals(h.join(), null); |
2763 |
> |
assertNull(h.join()); |
2764 |
|
assertEquals(1, r.invocationCount); |
2765 |
|
} catch (CompletionException ok) { |
2766 |
|
checkCompletedWithWrappedCFException(h, ex); |
2872 |
|
// unspecified behavior |
2873 |
|
Integer v; |
2874 |
|
try { |
2875 |
< |
assertEquals(h.join(), null); |
2875 |
> |
assertNull(h.join()); |
2876 |
|
assertEquals(1, r.invocationCount); |
2877 |
|
} catch (CompletionException ok) { |
2878 |
|
checkCompletedWithWrappedCancellationException(h); |
2900 |
|
// unspecified behavior |
2901 |
|
Integer v; |
2902 |
|
try { |
2903 |
< |
assertEquals(h.join(), null); |
2903 |
> |
assertNull(h.join()); |
2904 |
|
assertEquals(1, r.invocationCount); |
2905 |
|
} catch (CompletionException ok) { |
2906 |
|
checkCompletedWithWrappedCancellationException(h); |
3526 |
|
* source result. |
3527 |
|
*/ |
3528 |
|
public void testWhenComplete_normalCompletion1() { |
3331 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
3529 |
|
for (ExecutionMode m : ExecutionMode.values()) |
3530 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
3531 |
< |
|
3532 |
< |
final AtomicInteger a = new AtomicInteger(); |
3530 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3531 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3532 |
> |
{ |
3533 |
> |
final AtomicInteger a = new AtomicInteger(0); |
3534 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3535 |
|
if (!createIncomplete) f.complete(v1); |
3536 |
< |
final CompletableFuture<Integer> g = |
3537 |
< |
m.whenComplete(f, |
3538 |
< |
(Integer x, Throwable t) -> { |
3539 |
< |
threadAssertSame(x, v1); |
3540 |
< |
threadAssertNull(t); |
3541 |
< |
a.getAndIncrement(); |
3542 |
< |
}); |
3536 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
3537 |
> |
(f, |
3538 |
> |
(Integer x, Throwable t) -> { |
3539 |
> |
threadAssertSame(x, v1); |
3540 |
> |
threadAssertNull(t); |
3541 |
> |
a.getAndIncrement(); |
3542 |
> |
}); |
3543 |
|
if (createIncomplete) f.complete(v1); |
3544 |
< |
checkCompletedNormally(f, v1); |
3544 |
> |
|
3545 |
|
checkCompletedNormally(g, v1); |
3546 |
< |
assertEquals(a.get(), 1); |
3547 |
< |
} |
3548 |
< |
} |
3546 |
> |
checkCompletedNormally(f, v1); |
3547 |
> |
assertEquals(1, a.get()); |
3548 |
> |
}} |
3549 |
|
|
3550 |
|
/** |
3551 |
|
* whenComplete action executes on exceptional completion, propagating |
3552 |
|
* source result. |
3553 |
|
*/ |
3554 |
|
public void testWhenComplete_exceptionalCompletion() { |
3357 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
3555 |
|
for (ExecutionMode m : ExecutionMode.values()) |
3556 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
3557 |
< |
|
3558 |
< |
final AtomicInteger a = new AtomicInteger(); |
3556 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3557 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3558 |
> |
{ |
3559 |
> |
final AtomicInteger a = new AtomicInteger(0); |
3560 |
|
final CFException ex = new CFException(); |
3561 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3562 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
3570 |
|
if (createIncomplete) f.completeExceptionally(ex); |
3571 |
|
checkCompletedWithWrappedCFException(f, ex); |
3572 |
|
checkCompletedWithWrappedCFException(g, ex); |
3573 |
< |
assertEquals(a.get(), 1); |
3574 |
< |
} |
3575 |
< |
} |
3573 |
> |
assertEquals(1, a.get()); |
3574 |
> |
}} |
3575 |
> |
|
3576 |
> |
/** |
3577 |
> |
* whenComplete action executes on cancelled source, propagating |
3578 |
> |
* CancellationException. |
3579 |
> |
*/ |
3580 |
> |
public void testWhenComplete_sourceCancelled() { |
3581 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3582 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3583 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3584 |
> |
{ |
3585 |
> |
final AtomicInteger a = new AtomicInteger(0); |
3586 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3587 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3588 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
3589 |
> |
(f, |
3590 |
> |
(Integer x, Throwable t) -> { |
3591 |
> |
threadAssertNull(x); |
3592 |
> |
threadAssertTrue(t instanceof CancellationException); |
3593 |
> |
a.getAndIncrement(); |
3594 |
> |
}); |
3595 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3596 |
> |
|
3597 |
> |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
3598 |
> |
checkCompletedWithWrappedCancellationException(g); |
3599 |
> |
checkCancelled(f); |
3600 |
> |
assertEquals(1, a.get()); |
3601 |
> |
}} |
3602 |
|
|
3603 |
|
/** |
3604 |
|
* If a whenComplete action throws an exception when triggered by |
3609 |
|
for (ExecutionMode m : ExecutionMode.values()) |
3610 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
3611 |
|
|
3612 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
3613 |
|
final CFException ex = new CFException(); |
3614 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3615 |
|
if (!createIncomplete) f.complete(v1); |
3618 |
|
(Integer x, Throwable t) -> { |
3619 |
|
threadAssertSame(x, v1); |
3620 |
|
threadAssertNull(t); |
3621 |
+ |
a.getAndIncrement(); |
3622 |
|
throw ex; |
3623 |
|
}); |
3624 |
|
if (createIncomplete) f.complete(v1); |
3625 |
|
checkCompletedNormally(f, v1); |
3626 |
|
checkCompletedWithWrappedCFException(g, ex); |
3627 |
+ |
assertEquals(1, a.get()); |
3628 |
|
} |
3629 |
|
} |
3630 |
|
|
3638 |
|
for (ExecutionMode m : ExecutionMode.values()) |
3639 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
3640 |
|
|
3641 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
3642 |
|
final CFException ex1 = new CFException(); |
3643 |
|
final CFException ex2 = new CFException(); |
3644 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3649 |
|
(Integer x, Throwable t) -> { |
3650 |
|
threadAssertSame(t, ex1); |
3651 |
|
threadAssertNull(x); |
3652 |
+ |
a.getAndIncrement(); |
3653 |
|
throw ex2; |
3654 |
|
}); |
3655 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
3656 |
|
|
3657 |
|
checkCompletedWithWrappedCFException(f, ex1); |
3658 |
|
checkCompletedWithWrappedCFException(g, ex1); |
3659 |
+ |
assertEquals(1, a.get()); |
3660 |
|
} |
3661 |
|
} |
3662 |
|
|
3433 |
– |
/** |
3434 |
– |
* handleAsync action completes normally with function value on |
3435 |
– |
* either normal or exceptional completion of source |
3436 |
– |
*/ |
3437 |
– |
public void testHandleAsync() { |
3438 |
– |
CompletableFuture<Integer> f, g; |
3439 |
– |
IntegerHandler r; |
3440 |
– |
|
3441 |
– |
f = new CompletableFuture<>(); |
3442 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3443 |
– |
assertEquals(0, r.invocationCount); |
3444 |
– |
f.completeExceptionally(new CFException()); |
3445 |
– |
checkCompletedWithWrappedCFException(f); |
3446 |
– |
checkCompletedNormally(g, three); |
3447 |
– |
assertEquals(1, r.invocationCount); |
3448 |
– |
|
3449 |
– |
f = new CompletableFuture<>(); |
3450 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3451 |
– |
assertEquals(0, r.invocationCount); |
3452 |
– |
f.completeExceptionally(new CFException()); |
3453 |
– |
checkCompletedWithWrappedCFException(f); |
3454 |
– |
checkCompletedNormally(g, three); |
3455 |
– |
assertEquals(1, r.invocationCount); |
3456 |
– |
|
3457 |
– |
f = new CompletableFuture<>(); |
3458 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3459 |
– |
assertEquals(0, r.invocationCount); |
3460 |
– |
f.complete(one); |
3461 |
– |
checkCompletedNormally(f, one); |
3462 |
– |
checkCompletedNormally(g, two); |
3463 |
– |
assertEquals(1, r.invocationCount); |
3464 |
– |
|
3465 |
– |
f = new CompletableFuture<>(); |
3466 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3467 |
– |
assertEquals(0, r.invocationCount); |
3468 |
– |
f.complete(one); |
3469 |
– |
checkCompletedNormally(f, one); |
3470 |
– |
checkCompletedNormally(g, two); |
3471 |
– |
assertEquals(1, r.invocationCount); |
3472 |
– |
} |
3473 |
– |
|
3474 |
– |
/** |
3475 |
– |
* handleAsync action with Executor completes normally with |
3476 |
– |
* function value on either normal or exceptional completion of |
3477 |
– |
* source |
3478 |
– |
*/ |
3479 |
– |
public void testHandleAsync2() { |
3480 |
– |
CompletableFuture<Integer> f, g; |
3481 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
3482 |
– |
IntegerHandler r; |
3483 |
– |
|
3484 |
– |
f = new CompletableFuture<>(); |
3485 |
– |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3486 |
– |
assertEquals(0, r.invocationCount); |
3487 |
– |
f.completeExceptionally(new CFException()); |
3488 |
– |
checkCompletedWithWrappedCFException(f); |
3489 |
– |
checkCompletedNormally(g, three); |
3490 |
– |
assertEquals(1, r.invocationCount); |
3491 |
– |
|
3492 |
– |
f = new CompletableFuture<>(); |
3493 |
– |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3494 |
– |
assertEquals(0, r.invocationCount); |
3495 |
– |
f.completeExceptionally(new CFException()); |
3496 |
– |
checkCompletedWithWrappedCFException(f); |
3497 |
– |
checkCompletedNormally(g, three); |
3498 |
– |
assertEquals(1, r.invocationCount); |
3499 |
– |
|
3500 |
– |
f = new CompletableFuture<>(); |
3501 |
– |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3502 |
– |
assertEquals(0, r.invocationCount); |
3503 |
– |
f.complete(one); |
3504 |
– |
checkCompletedNormally(f, one); |
3505 |
– |
checkCompletedNormally(g, two); |
3506 |
– |
assertEquals(1, r.invocationCount); |
3507 |
– |
|
3508 |
– |
f = new CompletableFuture<>(); |
3509 |
– |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3510 |
– |
assertEquals(0, r.invocationCount); |
3511 |
– |
f.complete(one); |
3512 |
– |
checkCompletedNormally(f, one); |
3513 |
– |
checkCompletedNormally(g, two); |
3514 |
– |
assertEquals(1, r.invocationCount); |
3515 |
– |
} |
3516 |
– |
|
3663 |
|
} |