ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.45 by jsr166, Mon Jun 2 06:07:34 2014 UTC vs.
Revision 1.50 by jsr166, Mon Jun 2 19:32:57 2014 UTC

# Line 17 | Line 17 | import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19   import java.util.concurrent.CompletionStage;
20 + import java.util.concurrent.ForkJoinPool;
21 + import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.TimeoutException;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 247 | Line 249 | public class CompletableFutureTest exten
249          f = new CompletableFuture<>();
250          f.obtrudeValue(three);
251          checkCompletedNormally(f, three);
252 +        f.obtrudeValue(null);
253 +        checkCompletedNormally(f, null);
254          f = new CompletableFuture<>();
255          f.completeExceptionally(new CFException());
256          f.obtrudeValue(four);
# Line 279 | Line 283 | public class CompletableFutureTest exten
283       */
284      public void testGetNumberOfDependents() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286 <        assertEquals(f.getNumberOfDependents(), 0);
286 >        assertEquals(0, f.getNumberOfDependents());
287          CompletableFuture g = f.thenRun(new Noop());
288 <        assertEquals(f.getNumberOfDependents(), 1);
289 <        assertEquals(g.getNumberOfDependents(), 0);
288 >        assertEquals(1, f.getNumberOfDependents());
289 >        assertEquals(0, g.getNumberOfDependents());
290          CompletableFuture h = f.thenRun(new Noop());
291 <        assertEquals(f.getNumberOfDependents(), 2);
291 >        assertEquals(2, f.getNumberOfDependents());
292          f.complete(1);
293          checkCompletedNormally(g, null);
294 <        assertEquals(f.getNumberOfDependents(), 0);
295 <        assertEquals(g.getNumberOfDependents(), 0);
294 >        assertEquals(0, f.getNumberOfDependents());
295 >        assertEquals(0, g.getNumberOfDependents());
296      }
297  
298      /**
# Line 412 | Line 416 | public class CompletableFutureTest exten
416              throw new CFException();
417          }
418      }
419 <    static final class FailingNoop implements Runnable {
419 >    static final class FailingRunnable implements Runnable {
420          int invocationCount = 0;
421          public void run() {
422              invocationCount++;
# Line 450 | Line 454 | public class CompletableFutureTest exten
454          }
455      }
456  
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
457      /**
458       * Permits the testing of parallel code for the 3 different
459       * execution modes without repeating all the testing code.
460       */
461      enum ExecutionMode {
462          DEFAULT {
463 +            public void checkExecutionMode() {
464 +                assertNull(ForkJoinTask.getPool());
465 +            }
466 +            public <T> CompletableFuture<Void> thenRun
467 +                (CompletableFuture<T> f, Runnable a) {
468 +                return f.thenRun(a);
469 +            }
470 +            public <T> CompletableFuture<Void> thenAccept
471 +                (CompletableFuture<T> f, Consumer<? super T> a) {
472 +                return f.thenAccept(a);
473 +            }
474 +            public <T,U> CompletableFuture<U> thenApply
475 +                (CompletableFuture<T> f, Function<? super T,U> a) {
476 +                return f.thenApply(a);
477 +            }
478 +            public <T,U> CompletableFuture<U> thenCompose
479 +                (CompletableFuture<T> f,
480 +                 Function<? super T,? extends CompletionStage<U>> a) {
481 +                return f.thenCompose(a);
482 +            }
483 +            public <T,U> CompletableFuture<U> handle
484 +                (CompletableFuture<T> f,
485 +                 BiFunction<? super T,Throwable,? extends U> a) {
486 +                return f.handle(a);
487 +            }
488 +            public <T> CompletableFuture<T> whenComplete
489 +                (CompletableFuture<T> f,
490 +                 BiConsumer<? super T,? super Throwable> a) {
491 +                return f.whenComplete(a);
492 +            }
493              public <T,U> CompletableFuture<Void> runAfterBoth
494                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
495                  return f.runAfterBoth(g, a);
# Line 484 | Line 506 | public class CompletableFutureTest exten
506                   BiFunction<? super T,? super U,? extends V> a) {
507                  return f.thenCombine(g, a);
508              }
509 <            public <T,U> CompletableFuture<U> applyToEither
509 >            public <T> CompletableFuture<Void> runAfterEither
510                  (CompletableFuture<T> f,
511 <                 CompletionStage<? extends T> g,
512 <                 Function<? super T,U> a) {
513 <                return f.applyToEither(g, a);
511 >                 CompletionStage<?> g,
512 >                 java.lang.Runnable a) {
513 >                return f.runAfterEither(g, a);
514              }
515              public <T> CompletableFuture<Void> acceptEither
516                  (CompletableFuture<T> f,
# Line 496 | Line 518 | public class CompletableFutureTest exten
518                   Consumer<? super T> a) {
519                  return f.acceptEither(g, a);
520              }
521 <            public <T> CompletableFuture<Void> runAfterEither
521 >            public <T,U> CompletableFuture<U> applyToEither
522                  (CompletableFuture<T> f,
523 <                 CompletionStage<?> g,
524 <                 java.lang.Runnable a) {
525 <                return f.runAfterEither(g, a);
523 >                 CompletionStage<? extends T> g,
524 >                 Function<? super T,U> a) {
525 >                return f.applyToEither(g, a);
526 >            }
527 >        },
528 >
529 >        ASYNC {
530 >            public void checkExecutionMode() {
531 >                assertSame(ForkJoinPool.commonPool(),
532 >                           ForkJoinTask.getPool());
533 >            }
534 >            public <T> CompletableFuture<Void> thenRun
535 >                (CompletableFuture<T> f, Runnable a) {
536 >                return f.thenRunAsync(a);
537 >            }
538 >            public <T> CompletableFuture<Void> thenAccept
539 >                (CompletableFuture<T> f, Consumer<? super T> a) {
540 >                return f.thenAcceptAsync(a);
541 >            }
542 >            public <T,U> CompletableFuture<U> thenApply
543 >                (CompletableFuture<T> f, Function<? super T,U> a) {
544 >                return f.thenApplyAsync(a);
545              }
546              public <T,U> CompletableFuture<U> thenCompose
547                  (CompletableFuture<T> f,
548                   Function<? super T,? extends CompletionStage<U>> a) {
549 <                return f.thenCompose(a);
549 >                return f.thenComposeAsync(a);
550 >            }
551 >            public <T,U> CompletableFuture<U> handle
552 >                (CompletableFuture<T> f,
553 >                 BiFunction<? super T,Throwable,? extends U> a) {
554 >                return f.handleAsync(a);
555              }
556              public <T> CompletableFuture<T> whenComplete
557                  (CompletableFuture<T> f,
558                   BiConsumer<? super T,? super Throwable> a) {
559 <                return f.whenComplete(a);
559 >                return f.whenCompleteAsync(a);
560              }
515        },
516
517        DEFAULT_ASYNC {
561              public <T,U> CompletableFuture<Void> runAfterBoth
562                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
563                  return f.runAfterBothAsync(g, a);
# Line 531 | Line 574 | public class CompletableFutureTest exten
574                   BiFunction<? super T,? super U,? extends V> a) {
575                  return f.thenCombineAsync(g, a);
576              }
577 <            public <T,U> CompletableFuture<U> applyToEither
577 >            public <T> CompletableFuture<Void> runAfterEither
578                  (CompletableFuture<T> f,
579 <                 CompletionStage<? extends T> g,
580 <                 Function<? super T,U> a) {
581 <                return f.applyToEitherAsync(g, a);
579 >                 CompletionStage<?> g,
580 >                 java.lang.Runnable a) {
581 >                return f.runAfterEitherAsync(g, a);
582              }
583              public <T> CompletableFuture<Void> acceptEither
584                  (CompletableFuture<T> f,
# Line 543 | Line 586 | public class CompletableFutureTest exten
586                   Consumer<? super T> a) {
587                  return f.acceptEitherAsync(g, a);
588              }
589 <            public <T> CompletableFuture<Void> runAfterEither
589 >            public <T,U> CompletableFuture<U> applyToEither
590                  (CompletableFuture<T> f,
591 <                 CompletionStage<?> g,
592 <                 java.lang.Runnable a) {
593 <                return f.runAfterEitherAsync(g, a);
591 >                 CompletionStage<? extends T> g,
592 >                 Function<? super T,U> a) {
593 >                return f.applyToEitherAsync(g, a);
594 >            }
595 >        },
596 >
597 >        EXECUTOR {
598 >            public void checkExecutionMode() {
599 >                //TODO
600 >            }
601 >            public <T> CompletableFuture<Void> thenRun
602 >                (CompletableFuture<T> f, Runnable a) {
603 >                return f.thenRunAsync(a, new ThreadExecutor());
604 >            }
605 >            public <T> CompletableFuture<Void> thenAccept
606 >                (CompletableFuture<T> f, Consumer<? super T> a) {
607 >                return f.thenAcceptAsync(a, new ThreadExecutor());
608 >            }
609 >            public <T,U> CompletableFuture<U> thenApply
610 >                (CompletableFuture<T> f, Function<? super T,U> a) {
611 >                return f.thenApplyAsync(a, new ThreadExecutor());
612              }
613              public <T,U> CompletableFuture<U> thenCompose
614                  (CompletableFuture<T> f,
615                   Function<? super T,? extends CompletionStage<U>> a) {
616 <                return f.thenComposeAsync(a);
616 >                return f.thenComposeAsync(a, new ThreadExecutor());
617 >            }
618 >            public <T,U> CompletableFuture<U> handle
619 >                (CompletableFuture<T> f,
620 >                 BiFunction<? super T,Throwable,? extends U> a) {
621 >                return f.handleAsync(a, new ThreadExecutor());
622              }
623              public <T> CompletableFuture<T> whenComplete
624                  (CompletableFuture<T> f,
625                   BiConsumer<? super T,? super Throwable> a) {
626 <                return f.whenCompleteAsync(a);
626 >                return f.whenCompleteAsync(a, new ThreadExecutor());
627              }
562        },
563
564        EXECUTOR {
628              public <T,U> CompletableFuture<Void> runAfterBoth
629                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
630                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 578 | Line 641 | public class CompletableFutureTest exten
641                   BiFunction<? super T,? super U,? extends V> a) {
642                  return f.thenCombineAsync(g, a, new ThreadExecutor());
643              }
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            }
644              public <T> CompletableFuture<Void> runAfterEither
645                  (CompletableFuture<T> f,
646                   CompletionStage<?> g,
647                   java.lang.Runnable a) {
648                  return f.runAfterEitherAsync(g, a, new ThreadExecutor());
649              }
650 <            public <T,U> CompletableFuture<U> thenCompose
650 >            public <T> CompletableFuture<Void> acceptEither
651                  (CompletableFuture<T> f,
652 <                 Function<? super T,? extends CompletionStage<U>> a) {
653 <                return f.thenComposeAsync(a, new ThreadExecutor());
652 >                 CompletionStage<? extends T> g,
653 >                 Consumer<? super T> a) {
654 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
655              }
656 <            public <T> CompletableFuture<T> whenComplete
656 >            public <T,U> CompletableFuture<U> applyToEither
657                  (CompletableFuture<T> f,
658 <                 BiConsumer<? super T,? super Throwable> a) {
659 <                return f.whenCompleteAsync(a, new ThreadExecutor());
658 >                 CompletionStage<? extends T> g,
659 >                 Function<? super T,U> a) {
660 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
661              }
662          };
663  
664 +        public abstract void checkExecutionMode();
665 +        public abstract <T> CompletableFuture<Void> thenRun
666 +            (CompletableFuture<T> f, Runnable a);
667 +        public abstract <T> CompletableFuture<Void> thenAccept
668 +            (CompletableFuture<T> f, Consumer<? super T> a);
669 +        public abstract <T,U> CompletableFuture<U> thenApply
670 +            (CompletableFuture<T> f, Function<? super T,U> a);
671 +        public abstract <T,U> CompletableFuture<U> thenCompose
672 +            (CompletableFuture<T> f,
673 +             Function<? super T,? extends CompletionStage<U>> a);
674 +        public abstract <T,U> CompletableFuture<U> handle
675 +            (CompletableFuture<T> f,
676 +             BiFunction<? super T,Throwable,? extends U> a);
677 +        public abstract <T> CompletableFuture<T> whenComplete
678 +            (CompletableFuture<T> f,
679 +             BiConsumer<? super T,? super Throwable> a);
680          public abstract <T,U> CompletableFuture<Void> runAfterBoth
681              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
682          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 618 | Line 687 | public class CompletableFutureTest exten
687              (CompletableFuture<T> f,
688               CompletionStage<? extends U> g,
689               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);
690          public abstract <T> CompletableFuture<Void> runAfterEither
691              (CompletableFuture<T> f,
692               CompletionStage<?> g,
693               java.lang.Runnable a);
694 <        public abstract <T,U> CompletableFuture<U> thenCompose
694 >        public abstract <T> CompletableFuture<Void> acceptEither
695              (CompletableFuture<T> f,
696 <             Function<? super T,? extends CompletionStage<U>> a);
697 <        public abstract <T> CompletableFuture<T> whenComplete
696 >             CompletionStage<? extends T> g,
697 >             Consumer<? super T> a);
698 >        public abstract <T,U> CompletableFuture<U> applyToEither
699              (CompletableFuture<T> f,
700 <             BiConsumer<? super T,? super Throwable> a);
700 >             CompletionStage<? extends T> g,
701 >             Function<? super T,U> a);
702 >    }
703 >
704 >    /**
705 >     * exceptionally action is not invoked when source completes
706 >     * normally, and source result is propagated
707 >     */
708 >    public void testExceptionally_normalCompletion() {
709 >        for (boolean createIncomplete : new boolean[] { true, false })
710 >        for (Integer v1 : new Integer[] { 1, null })
711 >    {
712 >        final AtomicInteger a = new AtomicInteger(0);
713 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
714 >        if (!createIncomplete) f.complete(v1);
715 >        final CompletableFuture<Integer> g = f.exceptionally
716 >            ((Throwable t) -> {
717 >                // Should not be called
718 >                a.getAndIncrement();
719 >                throw new AssertionError();
720 >            });
721 >        if (createIncomplete) f.complete(v1);
722  
723 +        checkCompletedNormally(g, v1);
724 +        checkCompletedNormally(f, v1);
725 +        assertEquals(0, a.get());
726 +    }}
727  
641    }
728  
729      /**
730       * exceptionally action completes with function value on source
731 <     * exception; otherwise with source value
731 >     * exception
732       */
733 <    public void testExceptionally() {
734 <        CompletableFuture<Integer> f = new CompletableFuture<>();
735 <        ExceptionToInteger r = new ExceptionToInteger();
736 <        CompletableFuture<Integer> g = f.exceptionally(r);
737 <        f.completeExceptionally(new CFException());
738 <        checkCompletedNormally(g, three);
733 >    public void testExceptionally_exceptionalCompletion() {
734 >        for (boolean createIncomplete : new boolean[] { true, false })
735 >        for (Integer v1 : new Integer[] { 1, null })
736 >    {
737 >        final AtomicInteger a = new AtomicInteger(0);
738 >        final CFException ex = new CFException();
739 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
740 >        if (!createIncomplete) f.completeExceptionally(ex);
741 >        final CompletableFuture<Integer> g = f.exceptionally
742 >            ((Throwable t) -> {
743 >                threadAssertSame(t, ex);
744 >                a.getAndIncrement();
745 >                return v1;
746 >            });
747 >        if (createIncomplete) f.completeExceptionally(ex);
748  
749 <        f = new CompletableFuture<>();
750 <        r = new ExceptionToInteger();
751 <        g = f.exceptionally(r);
752 <        f.complete(one);
753 <        checkCompletedNormally(g, one);
754 <    }
749 >        checkCompletedNormally(g, v1);
750 >        assertEquals(1, a.get());
751 >    }}
752 >
753 >    public void testExceptionally_exceptionalCompletionActionFailed() {
754 >        for (boolean createIncomplete : new boolean[] { true, false })
755 >        for (Integer v1 : new Integer[] { 1, null })
756 >    {
757 >        final AtomicInteger a = new AtomicInteger(0);
758 >        final CFException ex1 = new CFException();
759 >        final CFException ex2 = new CFException();
760 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
761 >        if (!createIncomplete) f.completeExceptionally(ex1);
762 >        final CompletableFuture<Integer> g = f.exceptionally
763 >            ((Throwable t) -> {
764 >                threadAssertSame(t, ex1);
765 >                a.getAndIncrement();
766 >                throw ex2;
767 >            });
768 >        if (createIncomplete) f.completeExceptionally(ex1);
769 >
770 >        checkCompletedWithWrappedCFException(g, ex2);
771 >        assertEquals(1, a.get());
772 >    }}
773  
774      /**
775 <     * handle action completes normally with function value on either
776 <     * normal or exceptional completion of source
775 >     * handle action completes normally with function value on normal
776 >     * completion of source
777       */
778 <    public void testHandle() {
779 <        CompletableFuture<Integer> f, g;
780 <        IntegerHandler r;
778 >    public void testHandle_normalCompletion() {
779 >        for (ExecutionMode m : ExecutionMode.values())
780 >        for (boolean createIncomplete : new boolean[] { true, false })
781 >        for (Integer v1 : new Integer[] { 1, null })
782 >    {
783 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
784 >        final AtomicInteger a = new AtomicInteger(0);
785 >        if (!createIncomplete) f.complete(v1);
786 >        final CompletableFuture<Integer> g = m.handle
787 >            (f,
788 >             (Integer x, Throwable t) -> {
789 >                threadAssertSame(x, v1);
790 >                threadAssertNull(t);
791 >                a.getAndIncrement();
792 >                return inc(v1);
793 >            });
794 >        if (createIncomplete) f.complete(v1);
795  
796 <        f = new CompletableFuture<>();
797 <        f.completeExceptionally(new CFException());
798 <        g = f.handle(r = new IntegerHandler());
799 <        assertEquals(1, r.invocationCount);
673 <        assertEquals(1, r.invocationCount);
674 <        checkCompletedNormally(g, three);
796 >        checkCompletedNormally(g, inc(v1));
797 >        checkCompletedNormally(f, v1);
798 >        assertEquals(1, a.get());
799 >    }}
800  
801 <        f = new CompletableFuture<>();
802 <        g = f.handle(r = new IntegerHandler());
803 <        assertEquals(0, r.invocationCount);
804 <        f.completeExceptionally(new CFException());
805 <        checkCompletedNormally(g, three);
806 <        assertEquals(1, r.invocationCount);
801 >    /**
802 >     * handle action completes normally with function value on
803 >     * exceptional completion of source
804 >     */
805 >    public void testHandle_exceptionalCompletion() {
806 >        for (ExecutionMode m : ExecutionMode.values())
807 >        for (boolean createIncomplete : new boolean[] { true, false })
808 >        for (Integer v1 : new Integer[] { 1, null })
809 >    {
810 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
811 >        final AtomicInteger a = new AtomicInteger(0);
812 >        final CFException ex = new CFException();
813 >        if (!createIncomplete) f.completeExceptionally(ex);
814 >        final CompletableFuture<Integer> g = m.handle
815 >            (f,
816 >             (Integer x, Throwable t) -> {
817 >                threadAssertNull(x);
818 >                threadAssertSame(t, ex);
819 >                a.getAndIncrement();
820 >                return v1;
821 >            });
822 >        if (createIncomplete) f.completeExceptionally(ex);
823  
824 <        f = new CompletableFuture<>();
825 <        f.complete(one);
826 <        g = f.handle(r = new IntegerHandler());
827 <        assertEquals(1, r.invocationCount);
687 <        checkCompletedNormally(g, two);
824 >        checkCompletedNormally(g, v1);
825 >        checkCompletedWithWrappedCFException(f, ex);
826 >        assertEquals(1, a.get());
827 >    }}
828  
829 <        f = new CompletableFuture<>();
830 <        g = f.handle(r = new IntegerHandler());
831 <        assertEquals(0, r.invocationCount);
832 <        f.complete(one);
833 <        assertEquals(1, r.invocationCount);
834 <        checkCompletedNormally(g, two);
835 <    }
829 >    /**
830 >     * handle action completes normally with function value on
831 >     * cancelled source
832 >     */
833 >    public void testHandle_sourceCancelled() {
834 >        for (ExecutionMode m : ExecutionMode.values())
835 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
836 >        for (boolean createIncomplete : new boolean[] { true, false })
837 >        for (Integer v1 : new Integer[] { 1, null })
838 >    {
839 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
840 >        final AtomicInteger a = new AtomicInteger(0);
841 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
842 >        final CompletableFuture<Integer> g = m.handle
843 >            (f,
844 >             (Integer x, Throwable t) -> {
845 >                threadAssertNull(x);
846 >                threadAssertTrue(t instanceof CancellationException);
847 >                a.getAndIncrement();
848 >                return v1;
849 >            });
850 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
851 >
852 >        checkCompletedNormally(g, v1);
853 >        checkCancelled(f);
854 >        assertEquals(1, a.get());
855 >    }}
856 >
857 >    /**
858 >     * handle result completes exceptionally if action does
859 >     */
860 >    public void testHandle_sourceFailedActionFailed() {
861 >        for (ExecutionMode m : ExecutionMode.values())
862 >        for (boolean createIncomplete : new boolean[] { true, false })
863 >    {
864 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
865 >        final AtomicInteger a = new AtomicInteger(0);
866 >        final CFException ex1 = new CFException();
867 >        final CFException ex2 = new CFException();
868 >        if (!createIncomplete) f.completeExceptionally(ex1);
869 >        final CompletableFuture<Integer> g = m.handle
870 >            (f,
871 >             (Integer x, Throwable t) -> {
872 >                threadAssertNull(x);
873 >                threadAssertSame(ex1, t);
874 >                a.getAndIncrement();
875 >                throw ex2;
876 >            });
877 >        if (createIncomplete) f.completeExceptionally(ex1);
878 >
879 >        checkCompletedWithWrappedCFException(g, ex2);
880 >        checkCompletedWithWrappedCFException(f, ex1);
881 >        assertEquals(1, a.get());
882 >    }}
883 >
884 >    public void testHandle_sourceCompletedNormallyActionFailed() {
885 >        for (ExecutionMode m : ExecutionMode.values())
886 >        for (boolean createIncomplete : new boolean[] { true, false })
887 >        for (Integer v1 : new Integer[] { 1, null })
888 >    {
889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
890 >        final AtomicInteger a = new AtomicInteger(0);
891 >        final CFException ex = new CFException();
892 >        if (!createIncomplete) f.complete(v1);
893 >        final CompletableFuture<Integer> g = m.handle
894 >            (f,
895 >             (Integer x, Throwable t) -> {
896 >                threadAssertSame(x, v1);
897 >                threadAssertNull(t);
898 >                a.getAndIncrement();
899 >                throw ex;
900 >            });
901 >        if (createIncomplete) f.complete(v1);
902 >
903 >        checkCompletedWithWrappedCFException(g, ex);
904 >        checkCompletedNormally(f, v1);
905 >        assertEquals(1, a.get());
906 >    }}
907  
908      /**
909       * runAsync completes after running Runnable
# Line 722 | Line 933 | public class CompletableFutureTest exten
933       * failing runAsync completes exceptionally after running Runnable
934       */
935      public void testRunAsync3() {
936 <        FailingNoop r = new FailingNoop();
936 >        FailingRunnable r = new FailingRunnable();
937          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
938          checkCompletedWithWrappedCFException(f);
939          assertEquals(1, r.invocationCount);
# Line 763 | Line 974 | public class CompletableFutureTest exten
974      /**
975       * thenRun result completes normally after normal completion of source
976       */
977 <    public void testThenRun() {
978 <        CompletableFuture<Integer> f;
979 <        CompletableFuture<Void> g;
980 <        Noop r;
981 <
982 <        f = new CompletableFuture<>();
983 <        g = f.thenRun(r = new Noop());
984 <        f.complete(null);
985 <        checkCompletedNormally(g, null);
986 <        assertEquals(1, r.invocationCount);
977 >    public void testThenRun_normalCompletion() {
978 >        for (ExecutionMode m : ExecutionMode.values())
979 >        for (boolean createIncomplete : new boolean[] { true, false })
980 >        for (Integer v1 : new Integer[] { 1, null })
981 >    {
982 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
983 >        final Noop r = new Noop();
984 >        if (!createIncomplete) f.complete(v1);
985 >        final CompletableFuture<Void> g = m.thenRun(f, r);
986 >        if (createIncomplete) f.complete(v1);
987  
777        f = new CompletableFuture<>();
778        f.complete(null);
779        g = f.thenRun(r = new Noop());
988          checkCompletedNormally(g, null);
989 +        checkCompletedNormally(f, v1);
990          assertEquals(1, r.invocationCount);
991 <    }
991 >    }}
992  
993      /**
994       * thenRun result completes exceptionally after exceptional
995       * completion of source
996       */
997 <    public void testThenRun2() {
998 <        CompletableFuture<Integer> f;
999 <        CompletableFuture<Void> g;
1000 <        Noop r;
1001 <
1002 <        f = new CompletableFuture<>();
1003 <        g = f.thenRun(r = new Noop());
1004 <        f.completeExceptionally(new CFException());
1005 <        checkCompletedWithWrappedCFException(g);
1006 <        assertEquals(0, r.invocationCount);
997 >    public void testThenRun_exceptionalCompletion() {
998 >        for (ExecutionMode m : ExecutionMode.values())
999 >        for (boolean createIncomplete : new boolean[] { true, false })
1000 >    {
1001 >        final CFException ex = new CFException();
1002 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1003 >        final Noop r = new Noop();
1004 >        if (!createIncomplete) f.completeExceptionally(ex);
1005 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1006 >        if (createIncomplete) f.completeExceptionally(ex);
1007  
1008 <        f = new CompletableFuture<>();
1009 <        f.completeExceptionally(new CFException());
801 <        g = f.thenRun(r = new Noop());
802 <        checkCompletedWithWrappedCFException(g);
1008 >        checkCompletedWithWrappedCFException(g, ex);
1009 >        checkCompletedWithWrappedCFException(f, ex);
1010          assertEquals(0, r.invocationCount);
1011 <    }
1011 >    }}
1012  
1013      /**
1014 <     * thenRun result completes exceptionally if action does
1014 >     * thenRun result completes exceptionally if source cancelled
1015       */
1016 <    public void testThenRun3() {
1017 <        CompletableFuture<Integer> f;
1018 <        CompletableFuture<Void> g;
1019 <        FailingNoop r;
1020 <
1021 <        f = new CompletableFuture<>();
1022 <        g = f.thenRun(r = new FailingNoop());
1023 <        f.complete(null);
1024 <        checkCompletedWithWrappedCFException(g);
1016 >    public void testThenRun_sourceCancelled() {
1017 >        for (ExecutionMode m : ExecutionMode.values())
1018 >        for (boolean createIncomplete : new boolean[] { true, false })
1019 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1020 >    {
1021 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1022 >        final Noop r = new Noop();
1023 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1024 >        final CompletableFuture<Void> g = f.thenRun(r);
1025 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1026  
1027 <        f = new CompletableFuture<>();
1028 <        f.complete(null);
1029 <        g = f.thenRun(r = new FailingNoop());
1030 <        checkCompletedWithWrappedCFException(g);
823 <    }
1027 >        checkCompletedWithWrappedCancellationException(g);
1028 >        checkCancelled(f);
1029 >        assertEquals(0, r.invocationCount);
1030 >    }}
1031  
1032      /**
1033 <     * thenRun result completes exceptionally if source cancelled
1033 >     * thenRun result completes exceptionally if action does
1034       */
1035 <    public void testThenRun4() {
1036 <        CompletableFuture<Integer> f;
1037 <        CompletableFuture<Void> g;
1038 <        Noop r;
1039 <
1040 <        f = new CompletableFuture<>();
1041 <        g = f.thenRun(r = new Noop());
1042 <        assertTrue(f.cancel(true));
1043 <        checkCompletedWithWrappedCancellationException(g);
1035 >    public void testThenRun_actionFailed() {
1036 >        for (ExecutionMode m : ExecutionMode.values())
1037 >        for (boolean createIncomplete : new boolean[] { true, false })
1038 >        for (Integer v1 : new Integer[] { 1, null })
1039 >    {
1040 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1041 >        final FailingRunnable r = new FailingRunnable();
1042 >        if (!createIncomplete) f.complete(v1);
1043 >        final CompletableFuture<Void> g = f.thenRun(r);
1044 >        if (createIncomplete) f.complete(v1);
1045  
1046 <        f = new CompletableFuture<>();
1047 <        assertTrue(f.cancel(true));
1048 <        g = f.thenRun(r = new Noop());
841 <        checkCompletedWithWrappedCancellationException(g);
842 <    }
1046 >        checkCompletedWithWrappedCFException(g);
1047 >        checkCompletedNormally(f, v1);
1048 >    }}
1049  
1050      /**
1051       * thenApply result completes normally after normal completion of source
1052       */
1053 <    public void testThenApply() {
1054 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1055 <        CompletableFuture<Integer> g = f.thenApply(inc);
1056 <        f.complete(one);
1057 <        checkCompletedNormally(g, two);
1058 <    }
1053 >    public void testThenApply_normalCompletion() {
1054 >        for (ExecutionMode m : ExecutionMode.values())
1055 >        for (boolean createIncomplete : new boolean[] { true, false })
1056 >        for (Integer v1 : new Integer[] { 1, null })
1057 >    {
1058 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1059 >        final IncFunction r = new IncFunction();
1060 >        if (!createIncomplete) f.complete(v1);
1061 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1062 >        if (createIncomplete) {
1063 >            checkIncomplete(g);
1064 >            f.complete(v1);
1065 >        }
1066 >
1067 >        checkCompletedNormally(g, inc(v1));
1068 >        checkCompletedNormally(f, v1);
1069 >        assertEquals(1, r.invocationCount);
1070 >    }}
1071  
1072      /**
1073       * thenApply result completes exceptionally after exceptional
1074       * completion of source
1075       */
1076 <    public void testThenApply2() {
1077 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1078 <        CompletableFuture<Integer> g = f.thenApply(inc);
1079 <        f.completeExceptionally(new CFException());
1080 <        checkCompletedWithWrappedCFException(g);
1081 <    }
1076 >    public void testThenApply_exceptionalCompletion() {
1077 >        for (ExecutionMode m : ExecutionMode.values())
1078 >        for (boolean createIncomplete : new boolean[] { true, false })
1079 >    {
1080 >        final CFException ex = new CFException();
1081 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1082 >        final IncFunction r = new IncFunction();
1083 >        if (!createIncomplete) f.completeExceptionally(ex);
1084 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1085 >        if (createIncomplete) f.completeExceptionally(ex);
1086  
1087 <    /**
1088 <     * thenApply result completes exceptionally if action does
1089 <     */
1090 <    public void testThenApply3() {
869 <        CompletableFuture<Integer> f = new CompletableFuture<>();
870 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
871 <        f.complete(one);
872 <        checkCompletedWithWrappedCFException(g);
873 <    }
1087 >        checkCompletedWithWrappedCFException(g, ex);
1088 >        checkCompletedWithWrappedCFException(f, ex);
1089 >        assertEquals(0, r.invocationCount);
1090 >    }}
1091  
1092      /**
1093       * thenApply result completes exceptionally if source cancelled
1094       */
1095 <    public void testThenApply4() {
1096 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1097 <        CompletableFuture<Integer> g = f.thenApply(inc);
1098 <        assertTrue(f.cancel(true));
1095 >    public void testThenApply_sourceCancelled() {
1096 >        for (ExecutionMode m : ExecutionMode.values())
1097 >        for (boolean createIncomplete : new boolean[] { true, false })
1098 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1099 >    {
1100 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1101 >        final IncFunction r = new IncFunction();
1102 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1103 >        final CompletableFuture<Integer> g = f.thenApply(r);
1104 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1105 >
1106          checkCompletedWithWrappedCancellationException(g);
1107 <    }
1107 >        checkCancelled(f);
1108 >        assertEquals(0, r.invocationCount);
1109 >    }}
1110 >
1111 >    /**
1112 >     * thenApply result completes exceptionally if action does
1113 >     */
1114 >    public void testThenApply_actionFailed() {
1115 >        for (ExecutionMode m : ExecutionMode.values())
1116 >        for (boolean createIncomplete : new boolean[] { true, false })
1117 >        for (Integer v1 : new Integer[] { 1, null })
1118 >    {
1119 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1120 >        final FailingFunction r = new FailingFunction();
1121 >        if (!createIncomplete) f.complete(v1);
1122 >        final CompletableFuture<Integer> g = f.thenApply(r);
1123 >        if (createIncomplete) f.complete(v1);
1124 >
1125 >        checkCompletedWithWrappedCFException(g);
1126 >        checkCompletedNormally(f, v1);
1127 >    }}
1128  
1129      /**
1130       * thenAccept result completes normally after normal completion of source
1131       */
1132 <    public void testThenAccept() {
1133 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1134 <        IncAction r = new IncAction();
1135 <        CompletableFuture<Void> g = f.thenAccept(r);
1136 <        f.complete(one);
1132 >    public void testThenAccept_normalCompletion() {
1133 >        for (ExecutionMode m : ExecutionMode.values())
1134 >        for (boolean createIncomplete : new boolean[] { true, false })
1135 >        for (Integer v1 : new Integer[] { 1, null })
1136 >    {
1137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1138 >        final IncAction r = new IncAction();
1139 >        if (!createIncomplete) f.complete(v1);
1140 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1141 >        if (createIncomplete) f.complete(v1);
1142 >
1143          checkCompletedNormally(g, null);
1144 <        assertEquals(r.value, (Integer) 2);
1145 <    }
1144 >        checkCompletedNormally(f, v1);
1145 >        assertEquals(1, r.invocationCount);
1146 >        assertEquals(inc(v1), r.value);
1147 >    }}
1148  
1149      /**
1150       * thenAccept result completes exceptionally after exceptional
1151       * completion of source
1152       */
1153 <    public void testThenAccept2() {
1154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1155 <        IncAction r = new IncAction();
1156 <        CompletableFuture<Void> g = f.thenAccept(r);
1157 <        f.completeExceptionally(new CFException());
1158 <        checkCompletedWithWrappedCFException(g);
1159 <    }
1153 >    public void testThenAccept_exceptionalCompletion() {
1154 >        for (ExecutionMode m : ExecutionMode.values())
1155 >        for (boolean createIncomplete : new boolean[] { true, false })
1156 >    {
1157 >        final CFException ex = new CFException();
1158 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1159 >        final IncAction r = new IncAction();
1160 >        if (!createIncomplete) f.completeExceptionally(ex);
1161 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1162 >        if (createIncomplete) f.completeExceptionally(ex);
1163 >
1164 >        checkCompletedWithWrappedCFException(g, ex);
1165 >        checkCompletedWithWrappedCFException(f, ex);
1166 >        assertEquals(0, r.invocationCount);
1167 >    }}
1168  
1169      /**
1170       * thenAccept result completes exceptionally if action does
1171       */
1172 <    public void testThenAccept3() {
1173 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1174 <        FailingConsumer r = new FailingConsumer();
1175 <        CompletableFuture<Void> g = f.thenAccept(r);
1176 <        f.complete(one);
1172 >    public void testThenAccept_actionFailed() {
1173 >        for (ExecutionMode m : ExecutionMode.values())
1174 >        for (boolean createIncomplete : new boolean[] { true, false })
1175 >        for (Integer v1 : new Integer[] { 1, null })
1176 >    {
1177 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 >        final FailingConsumer r = new FailingConsumer();
1179 >        if (!createIncomplete) f.complete(v1);
1180 >        final CompletableFuture<Void> g = f.thenAccept(r);
1181 >        if (createIncomplete) f.complete(v1);
1182 >
1183          checkCompletedWithWrappedCFException(g);
1184 <        assertEquals(1, r.invocationCount);
1185 <    }
1184 >        checkCompletedNormally(f, v1);
1185 >    }}
1186  
1187      /**
1188       * thenAccept result completes exceptionally if source cancelled
1189       */
1190 <    public void testThenAccept4() {
1191 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1192 <        IncAction r = new IncAction();
1193 <        CompletableFuture<Void> g = f.thenAccept(r);
1194 <        assertTrue(f.cancel(true));
1190 >    public void testThenAccept_sourceCancelled() {
1191 >        for (ExecutionMode m : ExecutionMode.values())
1192 >        for (boolean createIncomplete : new boolean[] { true, false })
1193 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1194 >    {
1195 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1196 >        final IncAction r = new IncAction();
1197 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1198 >        final CompletableFuture<Void> g = f.thenAccept(r);
1199 >        if (createIncomplete) {
1200 >            checkIncomplete(g);
1201 >            assertTrue(f.cancel(mayInterruptIfRunning));
1202 >        }
1203 >
1204          checkCompletedWithWrappedCancellationException(g);
1205 <    }
1205 >        checkCancelled(f);
1206 >        assertEquals(0, r.invocationCount);
1207 >    }}
1208  
1209      /**
1210       * thenCombine result completes normally after normal completion
# Line 938 | Line 1215 | public class CompletableFutureTest exten
1215          for (boolean fFirst : new boolean[] { true, false })
1216          for (ExecutionMode m : ExecutionMode.values())
1217          for (Integer v1 : new Integer[] { 1, null })
1218 <        for (Integer v2 : new Integer[] { 2, null }) {
1219 <
1218 >        for (Integer v2 : new Integer[] { 2, null })
1219 >    {
1220          final CompletableFuture<Integer> f = new CompletableFuture<>();
1221          final CompletableFuture<Integer> g = new CompletableFuture<>();
1222          final SubtractFunction r = new SubtractFunction();
# Line 962 | Line 1239 | public class CompletableFutureTest exten
1239          checkCompletedNormally(f, v1);
1240          checkCompletedNormally(g, v2);
1241          assertEquals(1, r.invocationCount);
1242 <        }
966 <    }
1242 >    }}
1243  
1244      /**
1245       * thenCombine result completes exceptionally after exceptional
# Line 971 | Line 1247 | public class CompletableFutureTest exten
1247       */
1248      public void testThenCombine_exceptionalCompletion1() {
1249          for (ExecutionMode m : ExecutionMode.values())
1250 <        for (Integer v1 : new Integer[] { 1, null }) {
1251 <
1250 >        for (Integer v1 : new Integer[] { 1, null })
1251 >    {
1252          final CompletableFuture<Integer> f = new CompletableFuture<>();
1253          final CompletableFuture<Integer> g = new CompletableFuture<>();
1254          final SubtractFunction r = new SubtractFunction();
# Line 987 | Line 1263 | public class CompletableFutureTest exten
1263          checkCompletedWithWrappedCFException(f, ex);
1264          assertEquals(0, r.invocationCount);
1265          checkCompletedNormally(g, v1);
1266 <        }
991 <    }
1266 >    }}
1267  
1268      public void testThenCombine_exceptionalCompletion2() {
1269          for (ExecutionMode m : ExecutionMode.values())
1270 <        for (Integer v1 : new Integer[] { 1, null }) {
1271 <
1270 >        for (Integer v1 : new Integer[] { 1, null })
1271 >    {
1272          final CompletableFuture<Integer> f = new CompletableFuture<>();
1273          final CompletableFuture<Integer> g = new CompletableFuture<>();
1274          final SubtractFunction r = new SubtractFunction();
# Line 1008 | Line 1283 | public class CompletableFutureTest exten
1283          checkCompletedWithWrappedCFException(g, ex);
1284          assertEquals(0, r.invocationCount);
1285          checkCompletedNormally(f, v1);
1286 <        }
1012 <    }
1286 >    }}
1287  
1288      public void testThenCombine_exceptionalCompletion3() {
1289          for (ExecutionMode m : ExecutionMode.values())
1290 <        for (Integer v1 : new Integer[] { 1, null }) {
1291 <
1290 >        for (Integer v1 : new Integer[] { 1, null })
1291 >    {
1292          final CompletableFuture<Integer> f = new CompletableFuture<>();
1293          final CompletableFuture<Integer> g = new CompletableFuture<>();
1294          final SubtractFunction r = new SubtractFunction();
# Line 1028 | Line 1302 | public class CompletableFutureTest exten
1302          checkCompletedWithWrappedCFException(g, ex);
1303          assertEquals(0, r.invocationCount);
1304          checkCompletedNormally(f, v1);
1305 <        }
1032 <    }
1305 >    }}
1306  
1307      public void testThenCombine_exceptionalCompletion4() {
1308          for (ExecutionMode m : ExecutionMode.values())
1309 <        for (Integer v1 : new Integer[] { 1, null }) {
1310 <
1309 >        for (Integer v1 : new Integer[] { 1, null })
1310 >    {
1311          final CompletableFuture<Integer> f = new CompletableFuture<>();
1312          final CompletableFuture<Integer> g = new CompletableFuture<>();
1313          final SubtractFunction r = new SubtractFunction();
# Line 1048 | Line 1321 | public class CompletableFutureTest exten
1321          checkCompletedWithWrappedCFException(f, ex);
1322          assertEquals(0, r.invocationCount);
1323          checkCompletedNormally(g, v1);
1324 <        }
1052 <    }
1324 >    }}
1325  
1326      /**
1327       * thenCombine result completes exceptionally if action does
# Line 1057 | Line 1329 | public class CompletableFutureTest exten
1329      public void testThenCombine_actionFailed1() {
1330          for (ExecutionMode m : ExecutionMode.values())
1331          for (Integer v1 : new Integer[] { 1, null })
1332 <        for (Integer v2 : new Integer[] { 2, null }) {
1333 <
1332 >        for (Integer v2 : new Integer[] { 2, null })
1333 >    {
1334          final CompletableFuture<Integer> f = new CompletableFuture<>();
1335          final CompletableFuture<Integer> g = new CompletableFuture<>();
1336          final FailingBiFunction r = new FailingBiFunction();
# Line 1071 | Line 1343 | public class CompletableFutureTest exten
1343          checkCompletedWithWrappedCFException(h);
1344          checkCompletedNormally(f, v1);
1345          checkCompletedNormally(g, v2);
1346 <        }
1075 <    }
1346 >    }}
1347  
1348      public void testThenCombine_actionFailed2() {
1349          for (ExecutionMode m : ExecutionMode.values())
1350          for (Integer v1 : new Integer[] { 1, null })
1351 <        for (Integer v2 : new Integer[] { 2, null }) {
1352 <
1351 >        for (Integer v2 : new Integer[] { 2, null })
1352 >    {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354          final CompletableFuture<Integer> g = new CompletableFuture<>();
1355          final FailingBiFunction r = new FailingBiFunction();
# Line 1091 | Line 1362 | public class CompletableFutureTest exten
1362          checkCompletedWithWrappedCFException(h);
1363          checkCompletedNormally(f, v1);
1364          checkCompletedNormally(g, v2);
1365 <        }
1095 <    }
1365 >    }}
1366  
1367      /**
1368       * thenCombine result completes exceptionally if either source cancelled
# Line 1100 | Line 1370 | public class CompletableFutureTest exten
1370      public void testThenCombine_sourceCancelled1() {
1371          for (ExecutionMode m : ExecutionMode.values())
1372          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1373 <        for (Integer v1 : new Integer[] { 1, null }) {
1374 <
1373 >        for (Integer v1 : new Integer[] { 1, null })
1374 >    {
1375          final CompletableFuture<Integer> f = new CompletableFuture<>();
1376          final CompletableFuture<Integer> g = new CompletableFuture<>();
1377          final SubtractFunction r = new SubtractFunction();
# Line 1115 | Line 1385 | public class CompletableFutureTest exten
1385          checkCancelled(f);
1386          assertEquals(0, r.invocationCount);
1387          checkCompletedNormally(g, v1);
1388 <        }
1119 <    }
1388 >    }}
1389  
1390      public void testThenCombine_sourceCancelled2() {
1391          for (ExecutionMode m : ExecutionMode.values())
1392          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1393 <        for (Integer v1 : new Integer[] { 1, null }) {
1394 <
1393 >        for (Integer v1 : new Integer[] { 1, null })
1394 >    {
1395          final CompletableFuture<Integer> f = new CompletableFuture<>();
1396          final CompletableFuture<Integer> g = new CompletableFuture<>();
1397          final SubtractFunction r = new SubtractFunction();
# Line 1136 | Line 1405 | public class CompletableFutureTest exten
1405          checkCancelled(g);
1406          assertEquals(0, r.invocationCount);
1407          checkCompletedNormally(f, v1);
1408 <        }
1140 <    }
1408 >    }}
1409  
1410      public void testThenCombine_sourceCancelled3() {
1411          for (ExecutionMode m : ExecutionMode.values())
1412          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1413 <        for (Integer v1 : new Integer[] { 1, null }) {
1414 <
1413 >        for (Integer v1 : new Integer[] { 1, null })
1414 >    {
1415          final CompletableFuture<Integer> f = new CompletableFuture<>();
1416          final CompletableFuture<Integer> g = new CompletableFuture<>();
1417          final SubtractFunction r = new SubtractFunction();
# Line 1156 | Line 1424 | public class CompletableFutureTest exten
1424          checkCancelled(g);
1425          assertEquals(0, r.invocationCount);
1426          checkCompletedNormally(f, v1);
1427 <        }
1160 <    }
1427 >    }}
1428  
1429      public void testThenCombine_sourceCancelled4() {
1430          for (ExecutionMode m : ExecutionMode.values())
1431          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1432 <        for (Integer v1 : new Integer[] { 1, null }) {
1433 <
1432 >        for (Integer v1 : new Integer[] { 1, null })
1433 >    {
1434          final CompletableFuture<Integer> f = new CompletableFuture<>();
1435          final CompletableFuture<Integer> g = new CompletableFuture<>();
1436          final SubtractFunction r = new SubtractFunction();
# Line 1176 | Line 1443 | public class CompletableFutureTest exten
1443          checkCancelled(f);
1444          assertEquals(0, r.invocationCount);
1445          checkCompletedNormally(g, v1);
1446 <        }
1180 <    }
1446 >    }}
1447  
1448      /**
1449       * thenAcceptBoth result completes normally after normal
# Line 1186 | Line 1452 | public class CompletableFutureTest exten
1452      public void testThenAcceptBoth_normalCompletion1() {
1453          for (ExecutionMode m : ExecutionMode.values())
1454          for (Integer v1 : new Integer[] { 1, null })
1455 <        for (Integer v2 : new Integer[] { 2, null }) {
1456 <
1455 >        for (Integer v2 : new Integer[] { 2, null })
1456 >    {
1457          final CompletableFuture<Integer> f = new CompletableFuture<>();
1458          final CompletableFuture<Integer> g = new CompletableFuture<>();
1459          final SubtractAction r = new SubtractAction();
# Line 1199 | Line 1465 | public class CompletableFutureTest exten
1465          g.complete(v2);
1466  
1467          checkCompletedNormally(h, null);
1468 <        assertEquals(r.value, subtract(v1, v2));
1468 >        assertEquals(subtract(v1, v2), r.value);
1469          checkCompletedNormally(f, v1);
1470          checkCompletedNormally(g, v2);
1471 <        }
1206 <    }
1471 >    }}
1472  
1473      public void testThenAcceptBoth_normalCompletion2() {
1474          for (ExecutionMode m : ExecutionMode.values())
1475          for (Integer v1 : new Integer[] { 1, null })
1476 <        for (Integer v2 : new Integer[] { 2, null }) {
1477 <
1476 >        for (Integer v2 : new Integer[] { 2, null })
1477 >    {
1478          final CompletableFuture<Integer> f = new CompletableFuture<>();
1479          final CompletableFuture<Integer> g = new CompletableFuture<>();
1480          final SubtractAction r = new SubtractAction();
# Line 1221 | Line 1486 | public class CompletableFutureTest exten
1486          f.complete(v1);
1487  
1488          checkCompletedNormally(h, null);
1489 <        assertEquals(r.value, subtract(v1, v2));
1489 >        assertEquals(subtract(v1, v2), r.value);
1490          checkCompletedNormally(f, v1);
1491          checkCompletedNormally(g, v2);
1492 <        }
1228 <    }
1492 >    }}
1493  
1494      public void testThenAcceptBoth_normalCompletion3() {
1495          for (ExecutionMode m : ExecutionMode.values())
1496          for (Integer v1 : new Integer[] { 1, null })
1497 <        for (Integer v2 : new Integer[] { 2, null }) {
1498 <
1497 >        for (Integer v2 : new Integer[] { 2, null })
1498 >    {
1499          final CompletableFuture<Integer> f = new CompletableFuture<>();
1500          final CompletableFuture<Integer> g = new CompletableFuture<>();
1501          final SubtractAction r = new SubtractAction();
# Line 1241 | Line 1505 | public class CompletableFutureTest exten
1505          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1506  
1507          checkCompletedNormally(h, null);
1508 <        assertEquals(r.value, subtract(v1, v2));
1508 >        assertEquals(subtract(v1, v2), r.value);
1509          checkCompletedNormally(f, v1);
1510          checkCompletedNormally(g, v2);
1511 <        }
1248 <    }
1511 >    }}
1512  
1513      public void testThenAcceptBoth_normalCompletion4() {
1514          for (ExecutionMode m : ExecutionMode.values())
1515          for (Integer v1 : new Integer[] { 1, null })
1516 <        for (Integer v2 : new Integer[] { 2, null }) {
1517 <
1516 >        for (Integer v2 : new Integer[] { 2, null })
1517 >    {
1518          final CompletableFuture<Integer> f = new CompletableFuture<>();
1519          final CompletableFuture<Integer> g = new CompletableFuture<>();
1520          final SubtractAction r = new SubtractAction();
# Line 1261 | Line 1524 | public class CompletableFutureTest exten
1524          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1525  
1526          checkCompletedNormally(h, null);
1527 <        assertEquals(r.value, subtract(v1, v2));
1527 >        assertEquals(subtract(v1, v2), r.value);
1528          checkCompletedNormally(f, v1);
1529          checkCompletedNormally(g, v2);
1530 <        }
1268 <    }
1530 >    }}
1531  
1532      /**
1533       * thenAcceptBoth result completes exceptionally after exceptional
# Line 1273 | Line 1535 | public class CompletableFutureTest exten
1535       */
1536      public void testThenAcceptBoth_exceptionalCompletion1() {
1537          for (ExecutionMode m : ExecutionMode.values())
1538 <        for (Integer v1 : new Integer[] { 1, null }) {
1539 <
1538 >        for (Integer v1 : new Integer[] { 1, null })
1539 >    {
1540          final CompletableFuture<Integer> f = new CompletableFuture<>();
1541          final CompletableFuture<Integer> g = new CompletableFuture<>();
1542          final SubtractAction r = new SubtractAction();
# Line 1289 | Line 1551 | public class CompletableFutureTest exten
1551          checkCompletedWithWrappedCFException(f, ex);
1552          assertEquals(0, r.invocationCount);
1553          checkCompletedNormally(g, v1);
1554 <        }
1293 <    }
1554 >    }}
1555  
1556      public void testThenAcceptBoth_exceptionalCompletion2() {
1557          for (ExecutionMode m : ExecutionMode.values())
1558 <        for (Integer v1 : new Integer[] { 1, null }) {
1559 <
1558 >        for (Integer v1 : new Integer[] { 1, null })
1559 >    {
1560          final CompletableFuture<Integer> f = new CompletableFuture<>();
1561          final CompletableFuture<Integer> g = new CompletableFuture<>();
1562          final SubtractAction r = new SubtractAction();
# Line 1310 | Line 1571 | public class CompletableFutureTest exten
1571          checkCompletedWithWrappedCFException(g, ex);
1572          assertEquals(0, r.invocationCount);
1573          checkCompletedNormally(f, v1);
1574 <        }
1314 <    }
1574 >    }}
1575  
1576      public void testThenAcceptBoth_exceptionalCompletion3() {
1577          for (ExecutionMode m : ExecutionMode.values())
1578 <        for (Integer v1 : new Integer[] { 1, null }) {
1579 <
1578 >        for (Integer v1 : new Integer[] { 1, null })
1579 >    {
1580          final CompletableFuture<Integer> f = new CompletableFuture<>();
1581          final CompletableFuture<Integer> g = new CompletableFuture<>();
1582          final SubtractAction r = new SubtractAction();
# Line 1330 | Line 1590 | public class CompletableFutureTest exten
1590          checkCompletedWithWrappedCFException(g, ex);
1591          assertEquals(0, r.invocationCount);
1592          checkCompletedNormally(f, v1);
1593 <        }
1334 <    }
1593 >    }}
1594  
1595      public void testThenAcceptBoth_exceptionalCompletion4() {
1596          for (ExecutionMode m : ExecutionMode.values())
1597 <        for (Integer v1 : new Integer[] { 1, null }) {
1598 <
1597 >        for (Integer v1 : new Integer[] { 1, null })
1598 >    {
1599          final CompletableFuture<Integer> f = new CompletableFuture<>();
1600          final CompletableFuture<Integer> g = new CompletableFuture<>();
1601          final SubtractAction r = new SubtractAction();
# Line 1350 | Line 1609 | public class CompletableFutureTest exten
1609          checkCompletedWithWrappedCFException(f, ex);
1610          assertEquals(0, r.invocationCount);
1611          checkCompletedNormally(g, v1);
1612 <        }
1354 <    }
1612 >    }}
1613  
1614      /**
1615       * thenAcceptBoth result completes exceptionally if action does
# Line 1359 | Line 1617 | public class CompletableFutureTest exten
1617      public void testThenAcceptBoth_actionFailed1() {
1618          for (ExecutionMode m : ExecutionMode.values())
1619          for (Integer v1 : new Integer[] { 1, null })
1620 <        for (Integer v2 : new Integer[] { 2, null }) {
1621 <
1620 >        for (Integer v2 : new Integer[] { 2, null })
1621 >    {
1622          final CompletableFuture<Integer> f = new CompletableFuture<>();
1623          final CompletableFuture<Integer> g = new CompletableFuture<>();
1624          final FailingBiConsumer r = new FailingBiConsumer();
# Line 1373 | Line 1631 | public class CompletableFutureTest exten
1631          checkCompletedWithWrappedCFException(h);
1632          checkCompletedNormally(f, v1);
1633          checkCompletedNormally(g, v2);
1634 <        }
1377 <    }
1634 >    }}
1635  
1636      public void testThenAcceptBoth_actionFailed2() {
1637          for (ExecutionMode m : ExecutionMode.values())
1638          for (Integer v1 : new Integer[] { 1, null })
1639 <        for (Integer v2 : new Integer[] { 2, null }) {
1640 <
1639 >        for (Integer v2 : new Integer[] { 2, null })
1640 >    {
1641          final CompletableFuture<Integer> f = new CompletableFuture<>();
1642          final CompletableFuture<Integer> g = new CompletableFuture<>();
1643          final FailingBiConsumer r = new FailingBiConsumer();
# Line 1393 | Line 1650 | public class CompletableFutureTest exten
1650          checkCompletedWithWrappedCFException(h);
1651          checkCompletedNormally(f, v1);
1652          checkCompletedNormally(g, v2);
1653 <        }
1397 <    }
1653 >    }}
1654  
1655      /**
1656       * thenAcceptBoth result completes exceptionally if either source cancelled
# Line 1402 | Line 1658 | public class CompletableFutureTest exten
1658      public void testThenAcceptBoth_sourceCancelled1() {
1659          for (ExecutionMode m : ExecutionMode.values())
1660          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1661 <        for (Integer v1 : new Integer[] { 1, null }) {
1662 <
1661 >        for (Integer v1 : new Integer[] { 1, null })
1662 >    {
1663          final CompletableFuture<Integer> f = new CompletableFuture<>();
1664          final CompletableFuture<Integer> g = new CompletableFuture<>();
1665          final SubtractAction r = new SubtractAction();
# Line 1417 | Line 1673 | public class CompletableFutureTest exten
1673          checkCancelled(f);
1674          assertEquals(0, r.invocationCount);
1675          checkCompletedNormally(g, v1);
1676 <        }
1421 <    }
1676 >    }}
1677  
1678      public void testThenAcceptBoth_sourceCancelled2() {
1679          for (ExecutionMode m : ExecutionMode.values())
1680          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1681 <        for (Integer v1 : new Integer[] { 1, null }) {
1682 <
1681 >        for (Integer v1 : new Integer[] { 1, null })
1682 >    {
1683          final CompletableFuture<Integer> f = new CompletableFuture<>();
1684          final CompletableFuture<Integer> g = new CompletableFuture<>();
1685          final SubtractAction r = new SubtractAction();
# Line 1438 | Line 1693 | public class CompletableFutureTest exten
1693          checkCancelled(g);
1694          assertEquals(0, r.invocationCount);
1695          checkCompletedNormally(f, v1);
1696 <        }
1442 <    }
1696 >    }}
1697  
1698      public void testThenAcceptBoth_sourceCancelled3() {
1699          for (ExecutionMode m : ExecutionMode.values())
1700          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1701 <        for (Integer v1 : new Integer[] { 1, null }) {
1702 <
1701 >        for (Integer v1 : new Integer[] { 1, null })
1702 >    {
1703          final CompletableFuture<Integer> f = new CompletableFuture<>();
1704          final CompletableFuture<Integer> g = new CompletableFuture<>();
1705          final SubtractAction r = new SubtractAction();
# Line 1458 | Line 1712 | public class CompletableFutureTest exten
1712          checkCancelled(g);
1713          assertEquals(0, r.invocationCount);
1714          checkCompletedNormally(f, v1);
1715 <        }
1462 <    }
1715 >    }}
1716  
1717      public void testThenAcceptBoth_sourceCancelled4() {
1718          for (ExecutionMode m : ExecutionMode.values())
1719          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1720 <        for (Integer v1 : new Integer[] { 1, null }) {
1721 <
1720 >        for (Integer v1 : new Integer[] { 1, null })
1721 >    {
1722          final CompletableFuture<Integer> f = new CompletableFuture<>();
1723          final CompletableFuture<Integer> g = new CompletableFuture<>();
1724          final SubtractAction r = new SubtractAction();
# Line 1478 | Line 1731 | public class CompletableFutureTest exten
1731          checkCancelled(f);
1732          assertEquals(0, r.invocationCount);
1733          checkCompletedNormally(g, v1);
1734 <        }
1482 <    }
1734 >    }}
1735  
1736      /**
1737       * runAfterBoth result completes normally after normal
# Line 1488 | Line 1740 | public class CompletableFutureTest exten
1740      public void testRunAfterBoth_normalCompletion1() {
1741          for (ExecutionMode m : ExecutionMode.values())
1742          for (Integer v1 : new Integer[] { 1, null })
1743 <        for (Integer v2 : new Integer[] { 2, null }) {
1744 <
1743 >        for (Integer v2 : new Integer[] { 2, null })
1744 >    {
1745          final CompletableFuture<Integer> f = new CompletableFuture<>();
1746          final CompletableFuture<Integer> g = new CompletableFuture<>();
1747          final Noop r = new Noop();
# Line 1504 | Line 1756 | public class CompletableFutureTest exten
1756          assertEquals(1, r.invocationCount);
1757          checkCompletedNormally(f, v1);
1758          checkCompletedNormally(g, v2);
1759 <        }
1508 <    }
1759 >    }}
1760  
1761      public void testRunAfterBoth_normalCompletion2() {
1762          for (ExecutionMode m : ExecutionMode.values())
1763          for (Integer v1 : new Integer[] { 1, null })
1764 <        for (Integer v2 : new Integer[] { 2, null }) {
1765 <
1764 >        for (Integer v2 : new Integer[] { 2, null })
1765 >    {
1766          final CompletableFuture<Integer> f = new CompletableFuture<>();
1767          final CompletableFuture<Integer> g = new CompletableFuture<>();
1768          final Noop r = new Noop();
# Line 1526 | Line 1777 | public class CompletableFutureTest exten
1777          assertEquals(1, r.invocationCount);
1778          checkCompletedNormally(f, v1);
1779          checkCompletedNormally(g, v2);
1780 <        }
1530 <    }
1780 >    }}
1781  
1782      public void testRunAfterBoth_normalCompletion3() {
1783          for (ExecutionMode m : ExecutionMode.values())
1784          for (Integer v1 : new Integer[] { 1, null })
1785 <        for (Integer v2 : new Integer[] { 2, null }) {
1786 <
1785 >        for (Integer v2 : new Integer[] { 2, null })
1786 >    {
1787          final CompletableFuture<Integer> f = new CompletableFuture<>();
1788          final CompletableFuture<Integer> g = new CompletableFuture<>();
1789          final Noop r = new Noop();
# Line 1546 | Line 1796 | public class CompletableFutureTest exten
1796          assertEquals(1, r.invocationCount);
1797          checkCompletedNormally(f, v1);
1798          checkCompletedNormally(g, v2);
1799 <        }
1550 <    }
1799 >    }}
1800  
1801      public void testRunAfterBoth_normalCompletion4() {
1802          for (ExecutionMode m : ExecutionMode.values())
1803          for (Integer v1 : new Integer[] { 1, null })
1804 <        for (Integer v2 : new Integer[] { 2, null }) {
1805 <
1804 >        for (Integer v2 : new Integer[] { 2, null })
1805 >    {
1806          final CompletableFuture<Integer> f = new CompletableFuture<>();
1807          final CompletableFuture<Integer> g = new CompletableFuture<>();
1808          final Noop r = new Noop();
# Line 1566 | Line 1815 | public class CompletableFutureTest exten
1815          assertEquals(1, r.invocationCount);
1816          checkCompletedNormally(f, v1);
1817          checkCompletedNormally(g, v2);
1818 <        }
1570 <    }
1818 >    }}
1819  
1820      /**
1821       * runAfterBoth result completes exceptionally after exceptional
# Line 1575 | Line 1823 | public class CompletableFutureTest exten
1823       */
1824      public void testRunAfterBoth_exceptionalCompletion1() {
1825          for (ExecutionMode m : ExecutionMode.values())
1826 <        for (Integer v1 : new Integer[] { 1, null }) {
1827 <
1826 >        for (Integer v1 : new Integer[] { 1, null })
1827 >    {
1828          final CompletableFuture<Integer> f = new CompletableFuture<>();
1829          final CompletableFuture<Integer> g = new CompletableFuture<>();
1830          final Noop r = new Noop();
# Line 1591 | Line 1839 | public class CompletableFutureTest exten
1839          checkCompletedWithWrappedCFException(f, ex);
1840          assertEquals(0, r.invocationCount);
1841          checkCompletedNormally(g, v1);
1842 <        }
1595 <    }
1842 >    }}
1843  
1844      public void testRunAfterBoth_exceptionalCompletion2() {
1845          for (ExecutionMode m : ExecutionMode.values())
1846 <        for (Integer v1 : new Integer[] { 1, null }) {
1847 <
1846 >        for (Integer v1 : new Integer[] { 1, null })
1847 >    {
1848          final CompletableFuture<Integer> f = new CompletableFuture<>();
1849          final CompletableFuture<Integer> g = new CompletableFuture<>();
1850          final Noop r = new Noop();
# Line 1612 | Line 1859 | public class CompletableFutureTest exten
1859          checkCompletedWithWrappedCFException(g, ex);
1860          assertEquals(0, r.invocationCount);
1861          checkCompletedNormally(f, v1);
1862 <        }
1616 <    }
1862 >    }}
1863  
1864      public void testRunAfterBoth_exceptionalCompletion3() {
1865          for (ExecutionMode m : ExecutionMode.values())
1866 <        for (Integer v1 : new Integer[] { 1, null }) {
1867 <
1866 >        for (Integer v1 : new Integer[] { 1, null })
1867 >    {
1868          final CompletableFuture<Integer> f = new CompletableFuture<>();
1869          final CompletableFuture<Integer> g = new CompletableFuture<>();
1870          final Noop r = new Noop();
# Line 1632 | Line 1878 | public class CompletableFutureTest exten
1878          checkCompletedWithWrappedCFException(g, ex);
1879          assertEquals(0, r.invocationCount);
1880          checkCompletedNormally(f, v1);
1881 <        }
1636 <    }
1881 >    }}
1882  
1883      public void testRunAfterBoth_exceptionalCompletion4() {
1884          for (ExecutionMode m : ExecutionMode.values())
1885 <        for (Integer v1 : new Integer[] { 1, null }) {
1886 <
1885 >        for (Integer v1 : new Integer[] { 1, null })
1886 >    {
1887          final CompletableFuture<Integer> f = new CompletableFuture<>();
1888          final CompletableFuture<Integer> g = new CompletableFuture<>();
1889          final Noop r = new Noop();
# Line 1652 | Line 1897 | public class CompletableFutureTest exten
1897          checkCompletedWithWrappedCFException(f, ex);
1898          assertEquals(0, r.invocationCount);
1899          checkCompletedNormally(g, v1);
1900 <        }
1656 <    }
1900 >    }}
1901  
1902      /**
1903       * runAfterBoth result completes exceptionally if action does
# Line 1661 | Line 1905 | public class CompletableFutureTest exten
1905      public void testRunAfterBoth_actionFailed1() {
1906          for (ExecutionMode m : ExecutionMode.values())
1907          for (Integer v1 : new Integer[] { 1, null })
1908 <        for (Integer v2 : new Integer[] { 2, null }) {
1909 <
1908 >        for (Integer v2 : new Integer[] { 2, null })
1909 >    {
1910          final CompletableFuture<Integer> f = new CompletableFuture<>();
1911          final CompletableFuture<Integer> g = new CompletableFuture<>();
1912 <        final FailingNoop r = new FailingNoop();
1912 >        final FailingRunnable r = new FailingRunnable();
1913          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1914  
1915          f.complete(v1);
# Line 1675 | Line 1919 | public class CompletableFutureTest exten
1919          checkCompletedWithWrappedCFException(h);
1920          checkCompletedNormally(f, v1);
1921          checkCompletedNormally(g, v2);
1922 <        }
1679 <    }
1922 >    }}
1923  
1924      public void testRunAfterBoth_actionFailed2() {
1925          for (ExecutionMode m : ExecutionMode.values())
1926          for (Integer v1 : new Integer[] { 1, null })
1927 <        for (Integer v2 : new Integer[] { 2, null }) {
1928 <
1927 >        for (Integer v2 : new Integer[] { 2, null })
1928 >    {
1929          final CompletableFuture<Integer> f = new CompletableFuture<>();
1930          final CompletableFuture<Integer> g = new CompletableFuture<>();
1931 <        final FailingNoop r = new FailingNoop();
1931 >        final FailingRunnable r = new FailingRunnable();
1932          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1933  
1934          g.complete(v2);
# Line 1695 | Line 1938 | public class CompletableFutureTest exten
1938          checkCompletedWithWrappedCFException(h);
1939          checkCompletedNormally(f, v1);
1940          checkCompletedNormally(g, v2);
1941 <        }
1699 <    }
1941 >    }}
1942  
1943      /**
1944       * runAfterBoth result completes exceptionally if either source cancelled
# Line 1704 | Line 1946 | public class CompletableFutureTest exten
1946      public void testRunAfterBoth_sourceCancelled1() {
1947          for (ExecutionMode m : ExecutionMode.values())
1948          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1949 <        for (Integer v1 : new Integer[] { 1, null }) {
1950 <
1949 >        for (Integer v1 : new Integer[] { 1, null })
1950 >    {
1951          final CompletableFuture<Integer> f = new CompletableFuture<>();
1952          final CompletableFuture<Integer> g = new CompletableFuture<>();
1953          final Noop r = new Noop();
# Line 1719 | Line 1961 | public class CompletableFutureTest exten
1961          checkCancelled(f);
1962          assertEquals(0, r.invocationCount);
1963          checkCompletedNormally(g, v1);
1964 <        }
1723 <    }
1964 >    }}
1965  
1966      public void testRunAfterBoth_sourceCancelled2() {
1967          for (ExecutionMode m : ExecutionMode.values())
1968          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1969 <        for (Integer v1 : new Integer[] { 1, null }) {
1970 <
1969 >        for (Integer v1 : new Integer[] { 1, null })
1970 >    {
1971          final CompletableFuture<Integer> f = new CompletableFuture<>();
1972          final CompletableFuture<Integer> g = new CompletableFuture<>();
1973          final Noop r = new Noop();
# Line 1740 | Line 1981 | public class CompletableFutureTest exten
1981          checkCancelled(g);
1982          assertEquals(0, r.invocationCount);
1983          checkCompletedNormally(f, v1);
1984 <        }
1744 <    }
1984 >    }}
1985  
1986      public void testRunAfterBoth_sourceCancelled3() {
1987          for (ExecutionMode m : ExecutionMode.values())
1988          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1989 <        for (Integer v1 : new Integer[] { 1, null }) {
1990 <
1989 >        for (Integer v1 : new Integer[] { 1, null })
1990 >    {
1991          final CompletableFuture<Integer> f = new CompletableFuture<>();
1992          final CompletableFuture<Integer> g = new CompletableFuture<>();
1993          final Noop r = new Noop();
# Line 1760 | Line 2000 | public class CompletableFutureTest exten
2000          checkCancelled(g);
2001          assertEquals(0, r.invocationCount);
2002          checkCompletedNormally(f, v1);
2003 <        }
1764 <    }
2003 >    }}
2004  
2005      public void testRunAfterBoth_sourceCancelled4() {
2006          for (ExecutionMode m : ExecutionMode.values())
2007          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2008 <        for (Integer v1 : new Integer[] { 1, null }) {
2009 <
2008 >        for (Integer v1 : new Integer[] { 1, null })
2009 >    {
2010          final CompletableFuture<Integer> f = new CompletableFuture<>();
2011          final CompletableFuture<Integer> g = new CompletableFuture<>();
2012          final Noop r = new Noop();
# Line 1780 | Line 2019 | public class CompletableFutureTest exten
2019          checkCancelled(f);
2020          assertEquals(0, r.invocationCount);
2021          checkCompletedNormally(g, v1);
2022 <        }
1784 <    }
2022 >    }}
2023  
2024      /**
2025       * applyToEither result completes normally after normal completion
# Line 1790 | Line 2028 | public class CompletableFutureTest exten
2028      public void testApplyToEither_normalCompletion1() {
2029          for (ExecutionMode m : ExecutionMode.values())
2030          for (Integer v1 : new Integer[] { 1, null })
2031 <        for (Integer v2 : new Integer[] { 2, null }) {
2032 <
2031 >        for (Integer v2 : new Integer[] { 2, null })
2032 >    {
2033          final CompletableFuture<Integer> f = new CompletableFuture<>();
2034          final CompletableFuture<Integer> g = new CompletableFuture<>();
2035          final IncFunction r = new IncFunction();
# Line 1804 | Line 2042 | public class CompletableFutureTest exten
2042          checkCompletedNormally(f, v1);
2043          checkCompletedNormally(g, v2);
2044          checkCompletedNormally(h, inc(v1));
2045 <        }
1808 <    }
2045 >    }}
2046  
2047      public void testApplyToEither_normalCompletion2() {
2048          for (ExecutionMode m : ExecutionMode.values())
2049          for (Integer v1 : new Integer[] { 1, null })
2050 <        for (Integer v2 : new Integer[] { 2, null }) {
2051 <
2050 >        for (Integer v2 : new Integer[] { 2, null })
2051 >    {
2052          final CompletableFuture<Integer> f = new CompletableFuture<>();
2053          final CompletableFuture<Integer> g = new CompletableFuture<>();
2054          final IncFunction r = new IncFunction();
# Line 1824 | Line 2061 | public class CompletableFutureTest exten
2061          checkCompletedNormally(f, v1);
2062          checkCompletedNormally(g, v2);
2063          checkCompletedNormally(h, inc(v2));
2064 <        }
2065 <    }
2064 >        }}
2065 >
2066      public void testApplyToEither_normalCompletion3() {
2067          for (ExecutionMode m : ExecutionMode.values())
2068          for (Integer v1 : new Integer[] { 1, null })
2069 <        for (Integer v2 : new Integer[] { 2, null }) {
2070 <
2069 >        for (Integer v2 : new Integer[] { 2, null })
2070 >    {
2071          final CompletableFuture<Integer> f = new CompletableFuture<>();
2072          final CompletableFuture<Integer> g = new CompletableFuture<>();
2073          final IncFunction r = new IncFunction();
# Line 1846 | Line 2083 | public class CompletableFutureTest exten
2083          assertTrue(Objects.equals(h.join(), inc(v1)) ||
2084                     Objects.equals(h.join(), inc(v2)));
2085          assertEquals(1, r.invocationCount);
2086 <        }
1850 <    }
2086 >    }}
2087  
2088      /**
2089       * applyToEither result completes exceptionally after exceptional
# Line 1855 | Line 2091 | public class CompletableFutureTest exten
2091       */
2092      public void testApplyToEither_exceptionalCompletion1() {
2093          for (ExecutionMode m : ExecutionMode.values())
2094 <        for (Integer v1 : new Integer[] { 1, null }) {
2095 <
2094 >        for (Integer v1 : new Integer[] { 1, null })
2095 >    {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
2098          final IncFunction r = new IncFunction();
# Line 1871 | Line 2107 | public class CompletableFutureTest exten
2107          checkCompletedNormally(g, v1);
2108          checkCompletedWithWrappedCFException(f, ex);
2109          checkCompletedWithWrappedCFException(h, ex);
2110 <        }
1875 <    }
2110 >    }}
2111  
2112      public void testApplyToEither_exceptionalCompletion2() {
2113          for (ExecutionMode m : ExecutionMode.values())
2114 <        for (Integer v1 : new Integer[] { 1, null }) {
2115 <
2114 >        for (Integer v1 : new Integer[] { 1, null })
2115 >    {
2116          final CompletableFuture<Integer> f = new CompletableFuture<>();
2117          final CompletableFuture<Integer> g = new CompletableFuture<>();
2118          final IncFunction r = new IncFunction();
# Line 1892 | Line 2127 | public class CompletableFutureTest exten
2127          checkCompletedNormally(f, v1);
2128          checkCompletedWithWrappedCFException(g, ex);
2129          checkCompletedWithWrappedCFException(h, ex);
2130 <        }
1896 <    }
2130 >    }}
2131  
2132      public void testApplyToEither_exceptionalCompletion3() {
2133          for (ExecutionMode m : ExecutionMode.values())
2134 <        for (Integer v1 : new Integer[] { 1, null }) {
2135 <
2134 >        for (Integer v1 : new Integer[] { 1, null })
2135 >    {
2136          final CompletableFuture<Integer> f = new CompletableFuture<>();
2137          final CompletableFuture<Integer> g = new CompletableFuture<>();
2138          final IncFunction r = new IncFunction();
# Line 1911 | Line 2145 | public class CompletableFutureTest exten
2145          // unspecified behavior
2146          Integer v;
2147          try {
2148 <            assertEquals(h.join(), inc(v1));
2148 >            assertEquals(inc(v1), h.join());
2149              assertEquals(1, r.invocationCount);
2150          } catch (CompletionException ok) {
2151              checkCompletedWithWrappedCFException(h, ex);
# Line 1920 | Line 2154 | public class CompletableFutureTest exten
2154  
2155          checkCompletedWithWrappedCFException(g, ex);
2156          checkCompletedNormally(f, v1);
2157 <        }
1924 <    }
2157 >    }}
2158  
2159      public void testApplyToEither_exceptionalCompletion4() {
2160          for (ExecutionMode m : ExecutionMode.values())
2161 <        for (Integer v1 : new Integer[] { 1, null }) {
2162 <
2161 >        for (Integer v1 : new Integer[] { 1, null })
2162 >    {
2163          final CompletableFuture<Integer> f = new CompletableFuture<>();
2164          final CompletableFuture<Integer> g = new CompletableFuture<>();
2165          final IncFunction r = new IncFunction();
# Line 1939 | Line 2172 | public class CompletableFutureTest exten
2172          // unspecified behavior
2173          Integer v;
2174          try {
2175 <            assertEquals(h.join(), inc(v1));
2175 >            assertEquals(inc(v1), h.join());
2176              assertEquals(1, r.invocationCount);
2177          } catch (CompletionException ok) {
2178              checkCompletedWithWrappedCFException(h, ex);
# Line 1948 | Line 2181 | public class CompletableFutureTest exten
2181  
2182          checkCompletedWithWrappedCFException(f, ex);
2183          checkCompletedNormally(g, v1);
2184 <        }
1952 <    }
2184 >    }}
2185  
2186      /**
2187       * applyToEither result completes exceptionally if action does
# Line 1957 | Line 2189 | public class CompletableFutureTest exten
2189      public void testApplyToEither_actionFailed1() {
2190          for (ExecutionMode m : ExecutionMode.values())
2191          for (Integer v1 : new Integer[] { 1, null })
2192 <        for (Integer v2 : new Integer[] { 2, null }) {
2193 <
2192 >        for (Integer v2 : new Integer[] { 2, null })
2193 >    {
2194          final CompletableFuture<Integer> f = new CompletableFuture<>();
2195          final CompletableFuture<Integer> g = new CompletableFuture<>();
2196          final FailingFunction r = new FailingFunction();
# Line 1969 | Line 2201 | public class CompletableFutureTest exten
2201          g.complete(v2);
2202          checkCompletedNormally(f, v1);
2203          checkCompletedNormally(g, v2);
2204 <        }
1973 <    }
2204 >    }}
2205  
2206      public void testApplyToEither_actionFailed2() {
2207          for (ExecutionMode m : ExecutionMode.values())
2208          for (Integer v1 : new Integer[] { 1, null })
2209 <        for (Integer v2 : new Integer[] { 2, null }) {
2210 <
2209 >        for (Integer v2 : new Integer[] { 2, null })
2210 >    {
2211          final CompletableFuture<Integer> f = new CompletableFuture<>();
2212          final CompletableFuture<Integer> g = new CompletableFuture<>();
2213          final FailingFunction r = new FailingFunction();
# Line 1987 | Line 2218 | public class CompletableFutureTest exten
2218          f.complete(v1);
2219          checkCompletedNormally(f, v1);
2220          checkCompletedNormally(g, v2);
2221 <        }
1991 <    }
2221 >    }}
2222  
2223      /**
2224       * applyToEither result completes exceptionally if either source cancelled
# Line 1996 | Line 2226 | public class CompletableFutureTest exten
2226      public void testApplyToEither_sourceCancelled1() {
2227          for (ExecutionMode m : ExecutionMode.values())
2228          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2229 <        for (Integer v1 : new Integer[] { 1, null }) {
2230 <
2229 >        for (Integer v1 : new Integer[] { 1, null })
2230 >    {
2231          final CompletableFuture<Integer> f = new CompletableFuture<>();
2232          final CompletableFuture<Integer> g = new CompletableFuture<>();
2233          final IncFunction r = new IncFunction();
# Line 2011 | Line 2241 | public class CompletableFutureTest exten
2241          assertEquals(0, r.invocationCount);
2242          checkCompletedNormally(g, v1);
2243          checkCompletedWithWrappedCancellationException(h);
2244 <        }
2015 <    }
2244 >    }}
2245  
2246      public void testApplyToEither_sourceCancelled2() {
2247          for (ExecutionMode m : ExecutionMode.values())
2248          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2249 <        for (Integer v1 : new Integer[] { 1, null }) {
2250 <
2249 >        for (Integer v1 : new Integer[] { 1, null })
2250 >    {
2251          final CompletableFuture<Integer> f = new CompletableFuture<>();
2252          final CompletableFuture<Integer> g = new CompletableFuture<>();
2253          final IncFunction r = new IncFunction();
# Line 2032 | Line 2261 | public class CompletableFutureTest exten
2261          assertEquals(0, r.invocationCount);
2262          checkCompletedNormally(f, v1);
2263          checkCompletedWithWrappedCancellationException(h);
2264 <        }
2036 <    }
2264 >    }}
2265  
2266      public void testApplyToEither_sourceCancelled3() {
2267          for (ExecutionMode m : ExecutionMode.values())
2268          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2269 <        for (Integer v1 : new Integer[] { 1, null }) {
2270 <
2269 >        for (Integer v1 : new Integer[] { 1, null })
2270 >    {
2271          final CompletableFuture<Integer> f = new CompletableFuture<>();
2272          final CompletableFuture<Integer> g = new CompletableFuture<>();
2273          final IncFunction r = new IncFunction();
# Line 2051 | Line 2279 | public class CompletableFutureTest exten
2279          // unspecified behavior
2280          Integer v;
2281          try {
2282 <            assertEquals(h.join(), inc(v1));
2282 >            assertEquals(inc(v1), h.join());
2283              assertEquals(1, r.invocationCount);
2284          } catch (CompletionException ok) {
2285              checkCompletedWithWrappedCancellationException(h);
# Line 2060 | Line 2288 | public class CompletableFutureTest exten
2288  
2289          checkCancelled(g);
2290          checkCompletedNormally(f, v1);
2291 <        }
2064 <    }
2291 >    }}
2292  
2293      public void testApplyToEither_sourceCancelled4() {
2294          for (ExecutionMode m : ExecutionMode.values())
2295          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2296 <        for (Integer v1 : new Integer[] { 1, null }) {
2297 <
2296 >        for (Integer v1 : new Integer[] { 1, null })
2297 >    {
2298          final CompletableFuture<Integer> f = new CompletableFuture<>();
2299          final CompletableFuture<Integer> g = new CompletableFuture<>();
2300          final IncFunction r = new IncFunction();
# Line 2079 | Line 2306 | public class CompletableFutureTest exten
2306          // unspecified behavior
2307          Integer v;
2308          try {
2309 <            assertEquals(h.join(), inc(v1));
2309 >            assertEquals(inc(v1), h.join());
2310              assertEquals(1, r.invocationCount);
2311          } catch (CompletionException ok) {
2312              checkCompletedWithWrappedCancellationException(h);
# Line 2088 | Line 2315 | public class CompletableFutureTest exten
2315  
2316          checkCancelled(f);
2317          checkCompletedNormally(g, v1);
2318 <        }
2092 <    }
2318 >    }}
2319  
2320      /**
2321       * acceptEither result completes normally after normal completion
# Line 2098 | Line 2324 | public class CompletableFutureTest exten
2324      public void testAcceptEither_normalCompletion1() {
2325          for (ExecutionMode m : ExecutionMode.values())
2326          for (Integer v1 : new Integer[] { 1, null })
2327 <        for (Integer v2 : new Integer[] { 2, null }) {
2328 <
2327 >        for (Integer v2 : new Integer[] { 2, null })
2328 >    {
2329          final CompletableFuture<Integer> f = new CompletableFuture<>();
2330          final CompletableFuture<Integer> g = new CompletableFuture<>();
2331          final IncAction r = new IncAction();
# Line 2107 | Line 2333 | public class CompletableFutureTest exten
2333  
2334          f.complete(v1);
2335          checkCompletedNormally(h, null);
2336 <        assertEquals(r.value, inc(v1));
2336 >        assertEquals(inc(v1), r.value);
2337          g.complete(v2);
2338  
2339          checkCompletedNormally(f, v1);
2340          checkCompletedNormally(g, v2);
2341          checkCompletedNormally(h, null);
2342 <        }
2117 <    }
2342 >    }}
2343  
2344      public void testAcceptEither_normalCompletion2() {
2345          for (ExecutionMode m : ExecutionMode.values())
2346          for (Integer v1 : new Integer[] { 1, null })
2347 <        for (Integer v2 : new Integer[] { 2, null }) {
2348 <
2347 >        for (Integer v2 : new Integer[] { 2, null })
2348 >    {
2349          final CompletableFuture<Integer> f = new CompletableFuture<>();
2350          final CompletableFuture<Integer> g = new CompletableFuture<>();
2351          final IncAction r = new IncAction();
# Line 2128 | Line 2353 | public class CompletableFutureTest exten
2353  
2354          g.complete(v2);
2355          checkCompletedNormally(h, null);
2356 <        assertEquals(r.value, inc(v2));
2356 >        assertEquals(inc(v2), r.value);
2357          f.complete(v1);
2358  
2359          checkCompletedNormally(f, v1);
2360          checkCompletedNormally(g, v2);
2361          checkCompletedNormally(h, null);
2362 <        }
2363 <    }
2362 >    }}
2363 >
2364      public void testAcceptEither_normalCompletion3() {
2365          for (ExecutionMode m : ExecutionMode.values())
2366          for (Integer v1 : new Integer[] { 1, null })
2367 <        for (Integer v2 : new Integer[] { 2, null }) {
2368 <
2367 >        for (Integer v2 : new Integer[] { 2, null })
2368 >    {
2369          final CompletableFuture<Integer> f = new CompletableFuture<>();
2370          final CompletableFuture<Integer> g = new CompletableFuture<>();
2371          final IncAction r = new IncAction();
# Line 2156 | Line 2381 | public class CompletableFutureTest exten
2381          // unspecified behavior
2382          assertTrue(Objects.equals(r.value, inc(v1)) ||
2383                     Objects.equals(r.value, inc(v2)));
2384 <        }
2160 <    }
2384 >    }}
2385  
2386      /**
2387       * acceptEither result completes exceptionally after exceptional
# Line 2165 | Line 2389 | public class CompletableFutureTest exten
2389       */
2390      public void testAcceptEither_exceptionalCompletion1() {
2391          for (ExecutionMode m : ExecutionMode.values())
2392 <        for (Integer v1 : new Integer[] { 1, null }) {
2393 <
2392 >        for (Integer v1 : new Integer[] { 1, null })
2393 >    {
2394          final CompletableFuture<Integer> f = new CompletableFuture<>();
2395          final CompletableFuture<Integer> g = new CompletableFuture<>();
2396          final IncAction r = new IncAction();
# Line 2181 | Line 2405 | public class CompletableFutureTest exten
2405          checkCompletedNormally(g, v1);
2406          checkCompletedWithWrappedCFException(f, ex);
2407          checkCompletedWithWrappedCFException(h, ex);
2408 <        }
2185 <    }
2408 >    }}
2409  
2410      public void testAcceptEither_exceptionalCompletion2() {
2411          for (ExecutionMode m : ExecutionMode.values())
2412 <        for (Integer v1 : new Integer[] { 1, null }) {
2413 <
2412 >        for (Integer v1 : new Integer[] { 1, null })
2413 >    {
2414          final CompletableFuture<Integer> f = new CompletableFuture<>();
2415          final CompletableFuture<Integer> g = new CompletableFuture<>();
2416          final IncAction r = new IncAction();
# Line 2202 | Line 2425 | public class CompletableFutureTest exten
2425          checkCompletedNormally(f, v1);
2426          checkCompletedWithWrappedCFException(g, ex);
2427          checkCompletedWithWrappedCFException(h, ex);
2428 <        }
2206 <    }
2428 >    }}
2429  
2430      public void testAcceptEither_exceptionalCompletion3() {
2431          for (ExecutionMode m : ExecutionMode.values())
2432 <        for (Integer v1 : new Integer[] { 1, null }) {
2433 <
2432 >        for (Integer v1 : new Integer[] { 1, null })
2433 >    {
2434          final CompletableFuture<Integer> f = new CompletableFuture<>();
2435          final CompletableFuture<Integer> g = new CompletableFuture<>();
2436          final IncAction r = new IncAction();
# Line 2221 | Line 2443 | public class CompletableFutureTest exten
2443          // unspecified behavior
2444          Integer v;
2445          try {
2446 <            assertEquals(h.join(), null);
2446 >            assertNull(h.join());
2447              assertEquals(1, r.invocationCount);
2448              assertEquals(inc(v1), r.value);
2449          } catch (CompletionException ok) {
# Line 2231 | Line 2453 | public class CompletableFutureTest exten
2453  
2454          checkCompletedWithWrappedCFException(g, ex);
2455          checkCompletedNormally(f, v1);
2456 <        }
2235 <    }
2456 >    }}
2457  
2458      public void testAcceptEither_exceptionalCompletion4() {
2459          for (ExecutionMode m : ExecutionMode.values())
2460 <        for (Integer v1 : new Integer[] { 1, null }) {
2461 <
2460 >        for (Integer v1 : new Integer[] { 1, null })
2461 >    {
2462          final CompletableFuture<Integer> f = new CompletableFuture<>();
2463          final CompletableFuture<Integer> g = new CompletableFuture<>();
2464          final IncAction r = new IncAction();
# Line 2250 | Line 2471 | public class CompletableFutureTest exten
2471          // unspecified behavior
2472          Integer v;
2473          try {
2474 <            assertEquals(h.join(), null);
2474 >            assertNull(h.join());
2475              assertEquals(1, r.invocationCount);
2476              assertEquals(inc(v1), r.value);
2477          } catch (CompletionException ok) {
# Line 2260 | Line 2481 | public class CompletableFutureTest exten
2481  
2482          checkCompletedWithWrappedCFException(f, ex);
2483          checkCompletedNormally(g, v1);
2484 <        }
2264 <    }
2484 >    }}
2485  
2486      /**
2487       * acceptEither result completes exceptionally if action does
# Line 2269 | Line 2489 | public class CompletableFutureTest exten
2489      public void testAcceptEither_actionFailed1() {
2490          for (ExecutionMode m : ExecutionMode.values())
2491          for (Integer v1 : new Integer[] { 1, null })
2492 <        for (Integer v2 : new Integer[] { 2, null }) {
2493 <
2492 >        for (Integer v2 : new Integer[] { 2, null })
2493 >    {
2494          final CompletableFuture<Integer> f = new CompletableFuture<>();
2495          final CompletableFuture<Integer> g = new CompletableFuture<>();
2496          final FailingConsumer r = new FailingConsumer();
# Line 2281 | Line 2501 | public class CompletableFutureTest exten
2501          g.complete(v2);
2502          checkCompletedNormally(f, v1);
2503          checkCompletedNormally(g, v2);
2504 <        }
2285 <    }
2504 >    }}
2505  
2506      public void testAcceptEither_actionFailed2() {
2507          for (ExecutionMode m : ExecutionMode.values())
2508          for (Integer v1 : new Integer[] { 1, null })
2509 <        for (Integer v2 : new Integer[] { 2, null }) {
2510 <
2509 >        for (Integer v2 : new Integer[] { 2, null })
2510 >    {
2511          final CompletableFuture<Integer> f = new CompletableFuture<>();
2512          final CompletableFuture<Integer> g = new CompletableFuture<>();
2513          final FailingConsumer r = new FailingConsumer();
# Line 2299 | Line 2518 | public class CompletableFutureTest exten
2518          f.complete(v1);
2519          checkCompletedNormally(f, v1);
2520          checkCompletedNormally(g, v2);
2521 <        }
2303 <    }
2521 >    }}
2522  
2523      /**
2524       * acceptEither result completes exceptionally if either source cancelled
# Line 2308 | Line 2526 | public class CompletableFutureTest exten
2526      public void testAcceptEither_sourceCancelled1() {
2527          for (ExecutionMode m : ExecutionMode.values())
2528          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2529 <        for (Integer v1 : new Integer[] { 1, null }) {
2530 <
2529 >        for (Integer v1 : new Integer[] { 1, null })
2530 >    {
2531          final CompletableFuture<Integer> f = new CompletableFuture<>();
2532          final CompletableFuture<Integer> g = new CompletableFuture<>();
2533          final IncAction r = new IncAction();
# Line 2323 | Line 2541 | public class CompletableFutureTest exten
2541          assertEquals(0, r.invocationCount);
2542          checkCompletedNormally(g, v1);
2543          checkCompletedWithWrappedCancellationException(h);
2544 <        }
2327 <    }
2544 >    }}
2545  
2546      public void testAcceptEither_sourceCancelled2() {
2547          for (ExecutionMode m : ExecutionMode.values())
2548          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2549 <        for (Integer v1 : new Integer[] { 1, null }) {
2550 <
2549 >        for (Integer v1 : new Integer[] { 1, null })
2550 >    {
2551          final CompletableFuture<Integer> f = new CompletableFuture<>();
2552          final CompletableFuture<Integer> g = new CompletableFuture<>();
2553          final IncAction r = new IncAction();
# Line 2344 | Line 2561 | public class CompletableFutureTest exten
2561          assertEquals(0, r.invocationCount);
2562          checkCompletedNormally(f, v1);
2563          checkCompletedWithWrappedCancellationException(h);
2564 <        }
2348 <    }
2564 >    }}
2565  
2566      public void testAcceptEither_sourceCancelled3() {
2567          for (ExecutionMode m : ExecutionMode.values())
2568          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2569 <        for (Integer v1 : new Integer[] { 1, null }) {
2570 <
2569 >        for (Integer v1 : new Integer[] { 1, null })
2570 >    {
2571          final CompletableFuture<Integer> f = new CompletableFuture<>();
2572          final CompletableFuture<Integer> g = new CompletableFuture<>();
2573          final IncAction r = new IncAction();
# Line 2363 | Line 2579 | public class CompletableFutureTest exten
2579          // unspecified behavior
2580          Integer v;
2581          try {
2582 <            assertEquals(h.join(), null);
2582 >            assertNull(h.join());
2583              assertEquals(1, r.invocationCount);
2584              assertEquals(inc(v1), r.value);
2585          } catch (CompletionException ok) {
# Line 2373 | Line 2589 | public class CompletableFutureTest exten
2589  
2590          checkCancelled(g);
2591          checkCompletedNormally(f, v1);
2592 <        }
2377 <    }
2592 >    }}
2593  
2594      public void testAcceptEither_sourceCancelled4() {
2595          for (ExecutionMode m : ExecutionMode.values())
2596          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2597 <        for (Integer v1 : new Integer[] { 1, null }) {
2598 <
2597 >        for (Integer v1 : new Integer[] { 1, null })
2598 >    {
2599          final CompletableFuture<Integer> f = new CompletableFuture<>();
2600          final CompletableFuture<Integer> g = new CompletableFuture<>();
2601          final IncAction r = new IncAction();
# Line 2392 | Line 2607 | public class CompletableFutureTest exten
2607          // unspecified behavior
2608          Integer v;
2609          try {
2610 <            assertEquals(h.join(), null);
2610 >            assertNull(h.join());
2611              assertEquals(1, r.invocationCount);
2612              assertEquals(inc(v1), r.value);
2613          } catch (CompletionException ok) {
# Line 2402 | Line 2617 | public class CompletableFutureTest exten
2617  
2618          checkCancelled(f);
2619          checkCompletedNormally(g, v1);
2620 <        }
2406 <    }
2620 >    }}
2621  
2622      /**
2623       * runAfterEither result completes normally after normal completion
# Line 2412 | Line 2626 | public class CompletableFutureTest exten
2626      public void testRunAfterEither_normalCompletion1() {
2627          for (ExecutionMode m : ExecutionMode.values())
2628          for (Integer v1 : new Integer[] { 1, null })
2629 <        for (Integer v2 : new Integer[] { 2, null }) {
2630 <
2629 >        for (Integer v2 : new Integer[] { 2, null })
2630 >    {
2631          final CompletableFuture<Integer> f = new CompletableFuture<>();
2632          final CompletableFuture<Integer> g = new CompletableFuture<>();
2633          final Noop r = new Noop();
# Line 2428 | Line 2642 | public class CompletableFutureTest exten
2642          checkCompletedNormally(g, v2);
2643          checkCompletedNormally(h, null);
2644          assertEquals(1, r.invocationCount);
2645 <        }
2432 <    }
2645 >    }}
2646  
2647      public void testRunAfterEither_normalCompletion2() {
2648          for (ExecutionMode m : ExecutionMode.values())
2649          for (Integer v1 : new Integer[] { 1, null })
2650 <        for (Integer v2 : new Integer[] { 2, null }) {
2651 <
2650 >        for (Integer v2 : new Integer[] { 2, null })
2651 >    {
2652          final CompletableFuture<Integer> f = new CompletableFuture<>();
2653          final CompletableFuture<Integer> g = new CompletableFuture<>();
2654          final Noop r = new Noop();
# Line 2450 | Line 2663 | public class CompletableFutureTest exten
2663          checkCompletedNormally(g, v2);
2664          checkCompletedNormally(h, null);
2665          assertEquals(1, r.invocationCount);
2666 <        }
2667 <    }
2666 >        }}
2667 >
2668      public void testRunAfterEither_normalCompletion3() {
2669          for (ExecutionMode m : ExecutionMode.values())
2670          for (Integer v1 : new Integer[] { 1, null })
2671 <        for (Integer v2 : new Integer[] { 2, null }) {
2672 <
2671 >        for (Integer v2 : new Integer[] { 2, null })
2672 >    {
2673          final CompletableFuture<Integer> f = new CompletableFuture<>();
2674          final CompletableFuture<Integer> g = new CompletableFuture<>();
2675          final Noop r = new Noop();
# Line 2469 | Line 2682 | public class CompletableFutureTest exten
2682          checkCompletedNormally(f, v1);
2683          checkCompletedNormally(g, v2);
2684          assertEquals(1, r.invocationCount);
2685 <        }
2473 <    }
2685 >    }}
2686  
2687      /**
2688       * runAfterEither result completes exceptionally after exceptional
# Line 2478 | Line 2690 | public class CompletableFutureTest exten
2690       */
2691      public void testRunAfterEither_exceptionalCompletion1() {
2692          for (ExecutionMode m : ExecutionMode.values())
2693 <        for (Integer v1 : new Integer[] { 1, null }) {
2694 <
2693 >        for (Integer v1 : new Integer[] { 1, null })
2694 >    {
2695          final CompletableFuture<Integer> f = new CompletableFuture<>();
2696          final CompletableFuture<Integer> g = new CompletableFuture<>();
2697          final Noop r = new Noop();
# Line 2494 | Line 2706 | public class CompletableFutureTest exten
2706          checkCompletedNormally(g, v1);
2707          checkCompletedWithWrappedCFException(f, ex);
2708          checkCompletedWithWrappedCFException(h, ex);
2709 <        }
2498 <    }
2709 >    }}
2710  
2711      public void testRunAfterEither_exceptionalCompletion2() {
2712          for (ExecutionMode m : ExecutionMode.values())
2713 <        for (Integer v1 : new Integer[] { 1, null }) {
2714 <
2713 >        for (Integer v1 : new Integer[] { 1, null })
2714 >    {
2715          final CompletableFuture<Integer> f = new CompletableFuture<>();
2716          final CompletableFuture<Integer> g = new CompletableFuture<>();
2717          final Noop r = new Noop();
# Line 2515 | Line 2726 | public class CompletableFutureTest exten
2726          checkCompletedNormally(f, v1);
2727          checkCompletedWithWrappedCFException(g, ex);
2728          checkCompletedWithWrappedCFException(h, ex);
2729 <        }
2519 <    }
2729 >    }}
2730  
2731      public void testRunAfterEither_exceptionalCompletion3() {
2732          for (ExecutionMode m : ExecutionMode.values())
2733 <        for (Integer v1 : new Integer[] { 1, null }) {
2734 <
2733 >        for (Integer v1 : new Integer[] { 1, null })
2734 >    {
2735          final CompletableFuture<Integer> f = new CompletableFuture<>();
2736          final CompletableFuture<Integer> g = new CompletableFuture<>();
2737          final Noop r = new Noop();
# Line 2534 | Line 2744 | public class CompletableFutureTest exten
2744          // unspecified behavior
2745          Integer v;
2746          try {
2747 <            assertEquals(h.join(), null);
2747 >            assertNull(h.join());
2748              assertEquals(1, r.invocationCount);
2749          } catch (CompletionException ok) {
2750              checkCompletedWithWrappedCFException(h, ex);
# Line 2543 | Line 2753 | public class CompletableFutureTest exten
2753  
2754          checkCompletedWithWrappedCFException(g, ex);
2755          checkCompletedNormally(f, v1);
2756 <        }
2547 <    }
2756 >    }}
2757  
2758      public void testRunAfterEither_exceptionalCompletion4() {
2759          for (ExecutionMode m : ExecutionMode.values())
2760 <        for (Integer v1 : new Integer[] { 1, null }) {
2761 <
2760 >        for (Integer v1 : new Integer[] { 1, null })
2761 >    {
2762          final CompletableFuture<Integer> f = new CompletableFuture<>();
2763          final CompletableFuture<Integer> g = new CompletableFuture<>();
2764          final Noop r = new Noop();
# Line 2562 | Line 2771 | public class CompletableFutureTest exten
2771          // unspecified behavior
2772          Integer v;
2773          try {
2774 <            assertEquals(h.join(), null);
2774 >            assertNull(h.join());
2775              assertEquals(1, r.invocationCount);
2776          } catch (CompletionException ok) {
2777              checkCompletedWithWrappedCFException(h, ex);
# Line 2571 | Line 2780 | public class CompletableFutureTest exten
2780  
2781          checkCompletedWithWrappedCFException(f, ex);
2782          checkCompletedNormally(g, v1);
2783 <        }
2575 <    }
2783 >    }}
2784  
2785      /**
2786       * runAfterEither result completes exceptionally if action does
# Line 2580 | Line 2788 | public class CompletableFutureTest exten
2788      public void testRunAfterEither_actionFailed1() {
2789          for (ExecutionMode m : ExecutionMode.values())
2790          for (Integer v1 : new Integer[] { 1, null })
2791 <        for (Integer v2 : new Integer[] { 2, null }) {
2792 <
2791 >        for (Integer v2 : new Integer[] { 2, null })
2792 >    {
2793          final CompletableFuture<Integer> f = new CompletableFuture<>();
2794          final CompletableFuture<Integer> g = new CompletableFuture<>();
2795 <        final FailingNoop r = new FailingNoop();
2795 >        final FailingRunnable r = new FailingRunnable();
2796          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2797  
2798          f.complete(v1);
# Line 2592 | Line 2800 | public class CompletableFutureTest exten
2800          g.complete(v2);
2801          checkCompletedNormally(f, v1);
2802          checkCompletedNormally(g, v2);
2803 <        }
2596 <    }
2803 >    }}
2804  
2805      public void testRunAfterEither_actionFailed2() {
2806          for (ExecutionMode m : ExecutionMode.values())
2807          for (Integer v1 : new Integer[] { 1, null })
2808 <        for (Integer v2 : new Integer[] { 2, null }) {
2809 <
2808 >        for (Integer v2 : new Integer[] { 2, null })
2809 >    {
2810          final CompletableFuture<Integer> f = new CompletableFuture<>();
2811          final CompletableFuture<Integer> g = new CompletableFuture<>();
2812 <        final FailingNoop r = new FailingNoop();
2812 >        final FailingRunnable r = new FailingRunnable();
2813          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2814  
2815          g.complete(v2);
# Line 2610 | Line 2817 | public class CompletableFutureTest exten
2817          f.complete(v1);
2818          checkCompletedNormally(f, v1);
2819          checkCompletedNormally(g, v2);
2820 <        }
2614 <    }
2820 >    }}
2821  
2822      /**
2823       * runAfterEither result completes exceptionally if either source cancelled
# Line 2619 | Line 2825 | public class CompletableFutureTest exten
2825      public void testRunAfterEither_sourceCancelled1() {
2826          for (ExecutionMode m : ExecutionMode.values())
2827          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2828 <        for (Integer v1 : new Integer[] { 1, null }) {
2829 <
2828 >        for (Integer v1 : new Integer[] { 1, null })
2829 >    {
2830          final CompletableFuture<Integer> f = new CompletableFuture<>();
2831          final CompletableFuture<Integer> g = new CompletableFuture<>();
2832          final Noop r = new Noop();
# Line 2634 | Line 2840 | public class CompletableFutureTest exten
2840          assertEquals(0, r.invocationCount);
2841          checkCompletedNormally(g, v1);
2842          checkCompletedWithWrappedCancellationException(h);
2843 <        }
2638 <    }
2843 >    }}
2844  
2845      public void testRunAfterEither_sourceCancelled2() {
2846          for (ExecutionMode m : ExecutionMode.values())
2847          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2848 <        for (Integer v1 : new Integer[] { 1, null }) {
2849 <
2848 >        for (Integer v1 : new Integer[] { 1, null })
2849 >    {
2850          final CompletableFuture<Integer> f = new CompletableFuture<>();
2851          final CompletableFuture<Integer> g = new CompletableFuture<>();
2852          final Noop r = new Noop();
# Line 2655 | Line 2860 | public class CompletableFutureTest exten
2860          assertEquals(0, r.invocationCount);
2861          checkCompletedNormally(f, v1);
2862          checkCompletedWithWrappedCancellationException(h);
2863 <        }
2659 <    }
2863 >    }}
2864  
2865      public void testRunAfterEither_sourceCancelled3() {
2866          for (ExecutionMode m : ExecutionMode.values())
2867          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2868 <        for (Integer v1 : new Integer[] { 1, null }) {
2869 <
2868 >        for (Integer v1 : new Integer[] { 1, null })
2869 >    {
2870          final CompletableFuture<Integer> f = new CompletableFuture<>();
2871          final CompletableFuture<Integer> g = new CompletableFuture<>();
2872          final Noop r = new Noop();
# Line 2674 | Line 2878 | public class CompletableFutureTest exten
2878          // unspecified behavior
2879          Integer v;
2880          try {
2881 <            assertEquals(h.join(), null);
2881 >            assertNull(h.join());
2882              assertEquals(1, r.invocationCount);
2883          } catch (CompletionException ok) {
2884              checkCompletedWithWrappedCancellationException(h);
# Line 2683 | Line 2887 | public class CompletableFutureTest exten
2887  
2888          checkCancelled(g);
2889          checkCompletedNormally(f, v1);
2890 <        }
2687 <    }
2890 >    }}
2891  
2892      public void testRunAfterEither_sourceCancelled4() {
2893          for (ExecutionMode m : ExecutionMode.values())
2894          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2895 <        for (Integer v1 : new Integer[] { 1, null }) {
2896 <
2895 >        for (Integer v1 : new Integer[] { 1, null })
2896 >    {
2897          final CompletableFuture<Integer> f = new CompletableFuture<>();
2898          final CompletableFuture<Integer> g = new CompletableFuture<>();
2899          final Noop r = new Noop();
# Line 2702 | Line 2905 | public class CompletableFutureTest exten
2905          // unspecified behavior
2906          Integer v;
2907          try {
2908 <            assertEquals(h.join(), null);
2908 >            assertNull(h.join());
2909              assertEquals(1, r.invocationCount);
2910          } catch (CompletionException ok) {
2911              checkCompletedWithWrappedCancellationException(h);
# Line 2711 | Line 2914 | public class CompletableFutureTest exten
2914  
2915          checkCancelled(f);
2916          checkCompletedNormally(g, v1);
2917 <        }
2715 <    }
2917 >    }}
2918  
2919      /**
2920       * thenCompose result completes normally after normal completion of source
2921       */
2922 <    public void testThenCompose_normalCompletion1() {
2922 >    public void testThenCompose_normalCompletion() {
2923          for (ExecutionMode m : ExecutionMode.values())
2924 <        for (Integer v1 : new Integer[] { 1, null }) {
2925 <
2924 >        for (boolean createIncomplete : new boolean[] { true, false })
2925 >        for (Integer v1 : new Integer[] { 1, null })
2926 >    {
2927          final CompletableFuture<Integer> f = new CompletableFuture<>();
2928          final CompletableFutureInc r = new CompletableFutureInc();
2929 +        if (!createIncomplete) f.complete(v1);
2930          final CompletableFuture<Integer> g = f.thenCompose(r);
2931 <        f.complete(v1);
2728 <        checkCompletedNormally(g, inc(v1));
2729 <        checkCompletedNormally(f, v1);
2730 <        assertEquals(1, r.invocationCount);
2731 <        }
2732 <    }
2733 <
2734 <    public void testThenCompose_normalCompletion2() {
2735 <        for (ExecutionMode m : ExecutionMode.values())
2736 <        for (Integer v1 : new Integer[] { 1, null }) {
2931 >        if (createIncomplete) f.complete(v1);
2932  
2738        final CompletableFuture<Integer> f = new CompletableFuture<>();
2739        final CompletableFutureInc r = new CompletableFutureInc();
2740        f.complete(v1);
2741        final CompletableFuture<Integer> g = f.thenCompose(r);
2933          checkCompletedNormally(g, inc(v1));
2934          checkCompletedNormally(f, v1);
2935          assertEquals(1, r.invocationCount);
2936 <        }
2746 <    }
2936 >    }}
2937  
2938      /**
2939       * thenCompose result completes exceptionally after exceptional
2940       * completion of source
2941       */
2942 <    public void testThenCompose_exceptionalCompletion1() {
2943 <        for (ExecutionMode m : ExecutionMode.values()) {
2944 <
2942 >    public void testThenCompose_exceptionalCompletion() {
2943 >        for (ExecutionMode m : ExecutionMode.values())
2944 >        for (boolean createIncomplete : new boolean[] { true, false })
2945 >    {
2946          final CFException ex = new CFException();
2947          final CompletableFutureInc r = new CompletableFutureInc();
2948          final CompletableFuture<Integer> f = new CompletableFuture<>();
2949 +        if (!createIncomplete) f.completeExceptionally(ex);
2950          final CompletableFuture<Integer> g = f.thenCompose(r);
2951 <        f.completeExceptionally(ex);
2760 <        checkCompletedWithWrappedCFException(g, ex);
2761 <        checkCompletedWithWrappedCFException(f, ex);
2762 <        }
2763 <    }
2764 <
2765 <    public void testThenCompose_exceptionalCompletion2() {
2766 <        for (ExecutionMode m : ExecutionMode.values()) {
2951 >        if (createIncomplete) f.completeExceptionally(ex);
2952  
2768        final CFException ex = new CFException();
2769        final CompletableFuture<Integer> f = new CompletableFuture<>();
2770        f.completeExceptionally(ex);
2771        final CompletableFutureInc r = new CompletableFutureInc();
2772        final CompletableFuture<Integer> g = f.thenCompose(r);
2953          checkCompletedWithWrappedCFException(g, ex);
2954          checkCompletedWithWrappedCFException(f, ex);
2955 <        }
2956 <    }
2955 >        assertEquals(0, r.invocationCount);
2956 >    }}
2957  
2958      /**
2959       * thenCompose result completes exceptionally if action does
2960       */
2961 <    public void testThenCompose_actionFailed1() {
2961 >    public void testThenCompose_actionFailed() {
2962          for (ExecutionMode m : ExecutionMode.values())
2963 <        for (Integer v1 : new Integer[] { 1, null }) {
2964 <
2963 >        for (boolean createIncomplete : new boolean[] { true, false })
2964 >        for (Integer v1 : new Integer[] { 1, null })
2965 >    {
2966          final CompletableFuture<Integer> f = new CompletableFuture<>();
2967          final FailingCompletableFutureFunction r
2968              = new FailingCompletableFutureFunction();
2969 +        if (!createIncomplete) f.complete(v1);
2970          final CompletableFuture<Integer> g = f.thenCompose(r);
2971 <        f.complete(v1);
2790 <        checkCompletedWithWrappedCFException(g);
2791 <        checkCompletedNormally(f, v1);
2792 <        }
2793 <    }
2794 <
2795 <    public void testThenCompose_actionFailed2() {
2796 <        for (ExecutionMode m : ExecutionMode.values())
2797 <        for (Integer v1 : new Integer[] { 1, null }) {
2971 >        if (createIncomplete) f.complete(v1);
2972  
2799        final CompletableFuture<Integer> f = new CompletableFuture<>();
2800        f.complete(v1);
2801        final FailingCompletableFutureFunction r
2802            = new FailingCompletableFutureFunction();
2803        final CompletableFuture<Integer> g = f.thenCompose(r);
2973          checkCompletedWithWrappedCFException(g);
2974          checkCompletedNormally(f, v1);
2975 <        }
2807 <    }
2975 >    }}
2976  
2977      /**
2978       * thenCompose result completes exceptionally if source cancelled
2979       */
2980 <    public void testThenCompose_sourceCancelled1() {
2980 >    public void testThenCompose_sourceCancelled() {
2981          for (ExecutionMode m : ExecutionMode.values())
2982 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2983 <
2982 >        for (boolean createIncomplete : new boolean[] { true, false })
2983 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2984 >    {
2985          final CompletableFuture<Integer> f = new CompletableFuture<>();
2986          final CompletableFutureInc r = new CompletableFutureInc();
2987 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2988          final CompletableFuture<Integer> g = f.thenCompose(r);
2989 <        assertTrue(f.cancel(mayInterruptIfRunning));
2990 <        checkCompletedWithWrappedCancellationException(g);
2991 <        checkCancelled(f);
2989 >        if (createIncomplete) {
2990 >            checkIncomplete(g);
2991 >            assertTrue(f.cancel(mayInterruptIfRunning));
2992          }
2823    }
2824
2825    public void testThenCompose_sourceCancelled2() {
2826        for (ExecutionMode m : ExecutionMode.values())
2827        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2993  
2829        final CompletableFuture<Integer> f = new CompletableFuture<>();
2830        assertTrue(f.cancel(mayInterruptIfRunning));
2831        final CompletableFutureInc r = new CompletableFutureInc();
2832        final CompletableFuture<Integer> g = f.thenCompose(r);
2994          checkCompletedWithWrappedCancellationException(g);
2995          checkCancelled(f);
2996 <        }
2836 <    }
2837 <
2838 <    // asyncs
2839 <
2840 <    /**
2841 <     * thenRunAsync result completes normally after normal completion of source
2842 <     */
2843 <    public void testThenRunAsync() {
2844 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2845 <        Noop r = new Noop();
2846 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2847 <        f.complete(null);
2848 <        checkCompletedNormally(g, null);
2849 <
2850 <        // reordered version
2851 <        f = new CompletableFuture<>();
2852 <        f.complete(null);
2853 <        r = new Noop();
2854 <        g = f.thenRunAsync(r);
2855 <        checkCompletedNormally(g, null);
2856 <    }
2857 <
2858 <    /**
2859 <     * thenRunAsync result completes exceptionally after exceptional
2860 <     * completion of source
2861 <     */
2862 <    public void testThenRunAsync2() {
2863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2864 <        Noop r = new Noop();
2865 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2866 <        f.completeExceptionally(new CFException());
2867 <        try {
2868 <            g.join();
2869 <            shouldThrow();
2870 <        } catch (CompletionException success) {}
2871 <        checkCompletedWithWrappedCFException(g);
2872 <    }
2873 <
2874 <    /**
2875 <     * thenRunAsync result completes exceptionally if action does
2876 <     */
2877 <    public void testThenRunAsync3() {
2878 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2879 <        FailingNoop r = new FailingNoop();
2880 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2881 <        f.complete(null);
2882 <        checkCompletedWithWrappedCFException(g);
2883 <    }
2884 <
2885 <    /**
2886 <     * thenRunAsync result completes exceptionally if source cancelled
2887 <     */
2888 <    public void testThenRunAsync4() {
2889 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2890 <        Noop r = new Noop();
2891 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2892 <        assertTrue(f.cancel(true));
2893 <        checkCompletedWithWrappedCancellationException(g);
2894 <    }
2895 <
2896 <    /**
2897 <     * thenApplyAsync result completes normally after normal completion of source
2898 <     */
2899 <    public void testThenApplyAsync() {
2900 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2901 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2902 <        f.complete(one);
2903 <        checkCompletedNormally(g, two);
2904 <    }
2905 <
2906 <    /**
2907 <     * thenApplyAsync result completes exceptionally after exceptional
2908 <     * completion of source
2909 <     */
2910 <    public void testThenApplyAsync2() {
2911 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2912 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2913 <        f.completeExceptionally(new CFException());
2914 <        checkCompletedWithWrappedCFException(g);
2915 <    }
2916 <
2917 <    /**
2918 <     * thenApplyAsync result completes exceptionally if action does
2919 <     */
2920 <    public void testThenApplyAsync3() {
2921 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2922 <        FailingFunction r = new FailingFunction();
2923 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2924 <        f.complete(null);
2925 <        checkCompletedWithWrappedCFException(g);
2926 <    }
2927 <
2928 <    /**
2929 <     * thenApplyAsync result completes exceptionally if source cancelled
2930 <     */
2931 <    public void testThenApplyAsync4() {
2932 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2933 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2934 <        assertTrue(f.cancel(true));
2935 <        checkCompletedWithWrappedCancellationException(g);
2936 <    }
2937 <
2938 <    /**
2939 <     * thenAcceptAsync result completes normally after normal
2940 <     * completion of source
2941 <     */
2942 <    public void testThenAcceptAsync() {
2943 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2944 <        IncAction r = new IncAction();
2945 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2946 <        f.complete(one);
2947 <        checkCompletedNormally(g, null);
2948 <        assertEquals(r.value, (Integer) 2);
2949 <    }
2950 <
2951 <    /**
2952 <     * thenAcceptAsync result completes exceptionally after exceptional
2953 <     * completion of source
2954 <     */
2955 <    public void testThenAcceptAsync2() {
2956 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2957 <        IncAction r = new IncAction();
2958 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2959 <        f.completeExceptionally(new CFException());
2960 <        checkCompletedWithWrappedCFException(g);
2961 <    }
2962 <
2963 <    /**
2964 <     * thenAcceptAsync result completes exceptionally if action does
2965 <     */
2966 <    public void testThenAcceptAsync3() {
2967 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2968 <        FailingConsumer r = new FailingConsumer();
2969 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2970 <        f.complete(null);
2971 <        checkCompletedWithWrappedCFException(g);
2972 <    }
2973 <
2974 <    /**
2975 <     * thenAcceptAsync result completes exceptionally if source cancelled
2976 <     */
2977 <    public void testThenAcceptAsync4() {
2978 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2979 <        IncAction r = new IncAction();
2980 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2981 <        assertTrue(f.cancel(true));
2982 <        checkCompletedWithWrappedCancellationException(g);
2983 <    }
2984 <
2985 <    // async with explicit executors
2986 <
2987 <    /**
2988 <     * thenRunAsync result completes normally after normal completion of source
2989 <     */
2990 <    public void testThenRunAsyncE() {
2991 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2992 <        Noop r = new Noop();
2993 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2994 <        f.complete(null);
2995 <        checkCompletedNormally(g, null);
2996 <
2997 <        // reordered version
2998 <        f = new CompletableFuture<>();
2999 <        f.complete(null);
3000 <        r = new Noop();
3001 <        g = f.thenRunAsync(r, new ThreadExecutor());
3002 <        checkCompletedNormally(g, null);
3003 <    }
3004 <
3005 <    /**
3006 <     * thenRunAsync result completes exceptionally after exceptional
3007 <     * completion of source
3008 <     */
3009 <    public void testThenRunAsync2E() {
3010 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3011 <        Noop r = new Noop();
3012 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3013 <        f.completeExceptionally(new CFException());
3014 <        try {
3015 <            g.join();
3016 <            shouldThrow();
3017 <        } catch (CompletionException success) {}
3018 <        checkCompletedWithWrappedCFException(g);
3019 <    }
3020 <
3021 <    /**
3022 <     * thenRunAsync result completes exceptionally if action does
3023 <     */
3024 <    public void testThenRunAsync3E() {
3025 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3026 <        FailingNoop r = new FailingNoop();
3027 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3028 <        f.complete(null);
3029 <        checkCompletedWithWrappedCFException(g);
3030 <    }
3031 <
3032 <    /**
3033 <     * thenRunAsync result completes exceptionally if source cancelled
3034 <     */
3035 <    public void testThenRunAsync4E() {
3036 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3037 <        Noop r = new Noop();
3038 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3039 <        assertTrue(f.cancel(true));
3040 <        checkCompletedWithWrappedCancellationException(g);
3041 <    }
3042 <
3043 <    /**
3044 <     * thenApplyAsync result completes normally after normal completion of source
3045 <     */
3046 <    public void testThenApplyAsyncE() {
3047 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3048 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3049 <        f.complete(one);
3050 <        checkCompletedNormally(g, two);
3051 <    }
3052 <
3053 <    /**
3054 <     * thenApplyAsync result completes exceptionally after exceptional
3055 <     * completion of source
3056 <     */
3057 <    public void testThenApplyAsync2E() {
3058 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3059 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3060 <        f.completeExceptionally(new CFException());
3061 <        checkCompletedWithWrappedCFException(g);
3062 <    }
3063 <
3064 <    /**
3065 <     * thenApplyAsync result completes exceptionally if action does
3066 <     */
3067 <    public void testThenApplyAsync3E() {
3068 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3069 <        FailingFunction r = new FailingFunction();
3070 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3071 <        f.complete(null);
3072 <        checkCompletedWithWrappedCFException(g);
3073 <    }
3074 <
3075 <    /**
3076 <     * thenApplyAsync result completes exceptionally if source cancelled
3077 <     */
3078 <    public void testThenApplyAsync4E() {
3079 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3080 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3081 <        assertTrue(f.cancel(true));
3082 <        checkCompletedWithWrappedCancellationException(g);
3083 <    }
3084 <
3085 <    /**
3086 <     * thenAcceptAsync result completes normally after normal
3087 <     * completion of source
3088 <     */
3089 <    public void testThenAcceptAsyncE() {
3090 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3091 <        IncAction r = new IncAction();
3092 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3093 <        f.complete(one);
3094 <        checkCompletedNormally(g, null);
3095 <        assertEquals(r.value, (Integer) 2);
3096 <    }
3097 <
3098 <    /**
3099 <     * thenAcceptAsync result completes exceptionally after exceptional
3100 <     * completion of source
3101 <     */
3102 <    public void testThenAcceptAsync2E() {
3103 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3104 <        IncAction r = new IncAction();
3105 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3106 <        f.completeExceptionally(new CFException());
3107 <        checkCompletedWithWrappedCFException(g);
3108 <    }
3109 <
3110 <    /**
3111 <     * thenAcceptAsync result completes exceptionally if action does
3112 <     */
3113 <    public void testThenAcceptAsync3E() {
3114 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3115 <        FailingConsumer r = new FailingConsumer();
3116 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3117 <        f.complete(null);
3118 <        checkCompletedWithWrappedCFException(g);
3119 <    }
3120 <
3121 <    /**
3122 <     * thenAcceptAsync result completes exceptionally if source cancelled
3123 <     */
3124 <    public void testThenAcceptAsync4E() {
3125 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3126 <        IncAction r = new IncAction();
3127 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3128 <        assertTrue(f.cancel(true));
3129 <        checkCompletedWithWrappedCancellationException(g);
3130 <    }
2996 >    }}
2997  
2998      // other static methods
2999  
# Line 3328 | Line 3194 | public class CompletableFutureTest exten
3194       * source result.
3195       */
3196      public void testWhenComplete_normalCompletion1() {
3331        for (boolean createIncomplete : new boolean[] { true, false })
3197          for (ExecutionMode m : ExecutionMode.values())
3198 <        for (Integer v1 : new Integer[] { 1, null }) {
3199 <
3200 <        final AtomicInteger a = new AtomicInteger();
3198 >        for (boolean createIncomplete : new boolean[] { true, false })
3199 >        for (Integer v1 : new Integer[] { 1, null })
3200 >    {
3201 >        final AtomicInteger a = new AtomicInteger(0);
3202          final CompletableFuture<Integer> f = new CompletableFuture<>();
3203          if (!createIncomplete) f.complete(v1);
3204 <        final CompletableFuture<Integer> g =
3205 <            m.whenComplete(f,
3206 <                           (Integer x, Throwable t) -> {
3207 <                               threadAssertSame(x, v1);
3208 <                               threadAssertNull(t);
3209 <                               a.getAndIncrement();
3210 <                           });
3204 >        final CompletableFuture<Integer> g = m.whenComplete
3205 >            (f,
3206 >             (Integer x, Throwable t) -> {
3207 >                threadAssertSame(x, v1);
3208 >                threadAssertNull(t);
3209 >                a.getAndIncrement();
3210 >            });
3211          if (createIncomplete) f.complete(v1);
3212 <        checkCompletedNormally(f, v1);
3212 >
3213          checkCompletedNormally(g, v1);
3214 <        assertEquals(a.get(), 1);
3215 <        }
3216 <    }
3214 >        checkCompletedNormally(f, v1);
3215 >        assertEquals(1, a.get());
3216 >    }}
3217  
3218      /**
3219       * whenComplete action executes on exceptional completion, propagating
3220       * source result.
3221       */
3222      public void testWhenComplete_exceptionalCompletion() {
3357        for (boolean createIncomplete : new boolean[] { true, false })
3223          for (ExecutionMode m : ExecutionMode.values())
3224 <        for (Integer v1 : new Integer[] { 1, null }) {
3225 <
3226 <        final AtomicInteger a = new AtomicInteger();
3224 >        for (boolean createIncomplete : new boolean[] { true, false })
3225 >        for (Integer v1 : new Integer[] { 1, null })
3226 >    {
3227 >        final AtomicInteger a = new AtomicInteger(0);
3228          final CFException ex = new CFException();
3229          final CompletableFuture<Integer> f = new CompletableFuture<>();
3230          if (!createIncomplete) f.completeExceptionally(ex);
# Line 3372 | Line 3238 | public class CompletableFutureTest exten
3238          if (createIncomplete) f.completeExceptionally(ex);
3239          checkCompletedWithWrappedCFException(f, ex);
3240          checkCompletedWithWrappedCFException(g, ex);
3241 <        assertEquals(a.get(), 1);
3242 <        }
3243 <    }
3241 >        assertEquals(1, a.get());
3242 >    }}
3243 >
3244 >    /**
3245 >     * whenComplete action executes on cancelled source, propagating
3246 >     * CancellationException.
3247 >     */
3248 >    public void testWhenComplete_sourceCancelled() {
3249 >        for (ExecutionMode m : ExecutionMode.values())
3250 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3251 >        for (boolean createIncomplete : new boolean[] { true, false })
3252 >    {
3253 >        final AtomicInteger a = new AtomicInteger(0);
3254 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3255 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3256 >        final CompletableFuture<Integer> g = m.whenComplete
3257 >            (f,
3258 >             (Integer x, Throwable t) -> {
3259 >                threadAssertNull(x);
3260 >                threadAssertTrue(t instanceof CancellationException);
3261 >                a.getAndIncrement();
3262 >            });
3263 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3264 >
3265 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3266 >        checkCompletedWithWrappedCancellationException(g);
3267 >        checkCancelled(f);
3268 >        assertEquals(1, a.get());
3269 >    }}
3270  
3271      /**
3272       * If a whenComplete action throws an exception when triggered by
# Line 3383 | Line 3275 | public class CompletableFutureTest exten
3275      public void testWhenComplete_actionFailed() {
3276          for (boolean createIncomplete : new boolean[] { true, false })
3277          for (ExecutionMode m : ExecutionMode.values())
3278 <        for (Integer v1 : new Integer[] { 1, null }) {
3279 <
3278 >        for (Integer v1 : new Integer[] { 1, null })
3279 >    {
3280 >        final AtomicInteger a = new AtomicInteger(0);
3281          final CFException ex = new CFException();
3282          final CompletableFuture<Integer> f = new CompletableFuture<>();
3283          if (!createIncomplete) f.complete(v1);
# Line 3393 | Line 3286 | public class CompletableFutureTest exten
3286               (Integer x, Throwable t) -> {
3287                  threadAssertSame(x, v1);
3288                  threadAssertNull(t);
3289 +                a.getAndIncrement();
3290                  throw ex;
3291              });
3292          if (createIncomplete) f.complete(v1);
3293          checkCompletedNormally(f, v1);
3294          checkCompletedWithWrappedCFException(g, ex);
3295 <        }
3296 <    }
3295 >        assertEquals(1, a.get());
3296 >    }}
3297  
3298      /**
3299       * If a whenComplete action throws an exception when triggered by
# Line 3409 | Line 3303 | public class CompletableFutureTest exten
3303      public void testWhenComplete_actionFailedSourceFailed() {
3304          for (boolean createIncomplete : new boolean[] { true, false })
3305          for (ExecutionMode m : ExecutionMode.values())
3306 <        for (Integer v1 : new Integer[] { 1, null }) {
3307 <
3306 >        for (Integer v1 : new Integer[] { 1, null })
3307 >    {
3308 >        final AtomicInteger a = new AtomicInteger(0);
3309          final CFException ex1 = new CFException();
3310          final CFException ex2 = new CFException();
3311          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 3421 | Line 3316 | public class CompletableFutureTest exten
3316               (Integer x, Throwable t) -> {
3317                  threadAssertSame(t, ex1);
3318                  threadAssertNull(x);
3319 +                a.getAndIncrement();
3320                  throw ex2;
3321              });
3322          if (createIncomplete) f.completeExceptionally(ex1);
3323  
3324          checkCompletedWithWrappedCFException(f, ex1);
3325          checkCompletedWithWrappedCFException(g, ex1);
3326 <        }
3327 <    }
3432 <
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 <    }
3326 >        assertEquals(1, a.get());
3327 >    }}
3328  
3329   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines