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.36 by jsr166, Sun Jun 1 23:12:45 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 318 | Line 322 | public class CompletableFutureTest exten
322  
323      // Choose non-commutative actions for better coverage
324  
325 <    // A non-commutative function that handles null values as well
326 <    public static int subtract(Integer x, Integer y) {
327 <        return ((x == null) ? 42 : x.intValue())
325 >    // A non-commutative function that handles and produces null values as well.
326 >    static Integer subtract(Integer x, Integer y) {
327 >        return (x == null && y == null) ? null :
328 >            ((x == null) ? 42 : x.intValue())
329              - ((y == null) ? 99 : y.intValue());
330      }
331  
332 +    // A function that handles and produces null values as well.
333 +    static Integer inc(Integer x) {
334 +        return (x == null) ? null : x + 1;
335 +    }
336 +
337      static final Supplier<Integer> supplyOne =
338          () -> Integer.valueOf(1);
339      static final Function<Integer, Integer> inc =
# Line 331 | Line 341 | public class CompletableFutureTest exten
341      static final BiFunction<Integer, Integer, Integer> subtract =
342          (Integer x, Integer y) -> subtract(x, y);
343      static final class IncAction implements Consumer<Integer> {
344 <        int value;
345 <        public void accept(Integer x) { value = x.intValue() + 1; }
344 >        int invocationCount = 0;
345 >        Integer value;
346 >        public void accept(Integer x) {
347 >            invocationCount++;
348 >            value = inc(x);
349 >        }
350 >    }
351 >    static final class IncFunction implements Function<Integer,Integer> {
352 >        int invocationCount = 0;
353 >        Integer value;
354 >        public Integer apply(Integer x) {
355 >            invocationCount++;
356 >            return value = inc(x);
357 >        }
358      }
359      static final class SubtractAction implements BiConsumer<Integer, Integer> {
360 <        volatile int invocationCount = 0;
361 <        int value;
360 >        int invocationCount = 0;
361 >        Integer value;
362          // Check this action was invoked exactly once when result is computed.
341        public boolean ran() { return invocationCount == 1; }
363          public void accept(Integer x, Integer y) {
364              invocationCount++;
365              value = subtract(x, y);
366          }
367      }
368      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
369 <        volatile int invocationCount = 0;
370 <        int value;
369 >        int invocationCount = 0;
370 >        Integer value;
371          // Check this action was invoked exactly once when result is computed.
351        public boolean ran() { return invocationCount == 1; }
372          public Integer apply(Integer x, Integer y) {
373              invocationCount++;
374 <            return subtract(x, y);
374 >            return value = subtract(x, y);
375          }
376      }
377      static final class Noop implements Runnable {
378 <        boolean ran;
379 <        public void run() { ran = true; }
378 >        int invocationCount = 0;
379 >        public void run() {
380 >            invocationCount++;
381 >        }
382      }
383  
384      static final class FailingSupplier implements Supplier<Integer> {
385 <        boolean ran;
386 <        public Integer get() { ran = true; throw new CFException(); }
385 >        int invocationCount = 0;
386 >        public Integer get() {
387 >            invocationCount++;
388 >            throw new CFException();
389 >        }
390      }
391      static final class FailingConsumer implements Consumer<Integer> {
392 <        boolean ran;
393 <        public void accept(Integer x) { ran = true; throw new CFException(); }
392 >        int invocationCount = 0;
393 >        public void accept(Integer x) {
394 >            invocationCount++;
395 >            throw new CFException();
396 >        }
397      }
398      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
399 <        boolean ran;
400 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
399 >        int invocationCount = 0;
400 >        public void accept(Integer x, Integer y) {
401 >            invocationCount++;
402 >            throw new CFException();
403 >        }
404      }
405      static final class FailingFunction implements Function<Integer, Integer> {
406 <        boolean ran;
407 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
406 >        int invocationCount = 0;
407 >        public Integer apply(Integer x) {
408 >            invocationCount++;
409 >            throw new CFException();
410 >        }
411      }
412      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
413 <        boolean ran;
414 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
413 >        int invocationCount = 0;
414 >        public Integer apply(Integer x, Integer y) {
415 >            invocationCount++;
416 >            throw new CFException();
417 >        }
418      }
419 <    static final class FailingNoop implements Runnable {
420 <        boolean ran;
421 <        public void run() { ran = true; throw new CFException(); }
419 >    static final class FailingRunnable implements Runnable {
420 >        int invocationCount = 0;
421 >        public void run() {
422 >            invocationCount++;
423 >            throw new CFException();
424 >        }
425      }
426  
427      static final class CompletableFutureInc
428          implements Function<Integer, CompletableFuture<Integer>> {
429 <        boolean ran;
429 >        int invocationCount = 0;
430          public CompletableFuture<Integer> apply(Integer x) {
431 <            ran = true;
431 >            invocationCount++;
432              CompletableFuture<Integer> f = new CompletableFuture<>();
433 <            f.complete(Integer.valueOf(x.intValue() + 1));
433 >            f.complete(inc(x));
434              return f;
435          }
436      }
437  
438      static final class FailingCompletableFutureFunction
439          implements Function<Integer, CompletableFuture<Integer>> {
440 <        boolean ran;
440 >        int invocationCount = 0;
441          public CompletableFuture<Integer> apply(Integer x) {
442 <            ran = true; throw new CFException();
442 >            invocationCount++;
443 >            throw new CFException();
444          }
445      }
446  
# Line 413 | Line 454 | public class CompletableFutureTest exten
454          }
455      }
456  
416    static final class ExceptionToInteger implements Function<Throwable, Integer> {
417        public Integer apply(Throwable x) { return Integer.valueOf(3); }
418    }
419
420    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
421        boolean ran;
422        public Integer apply(Integer x, Throwable t) {
423            ran = true;
424            return (t == null) ? two : three;
425        }
426    }
427
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 447 | 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> CompletableFuture<Void> runAfterEither
510 +                (CompletableFuture<T> f,
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,
517 +                 CompletionStage<? extends T> g,
518 +                 Consumer<? super T> a) {
519 +                return f.acceptEither(g, a);
520 +            }
521 +            public <T,U> CompletableFuture<U> applyToEither
522 +                (CompletableFuture<T> f,
523 +                 CompletionStage<? extends T> g,
524 +                 Function<? super T,U> a) {
525 +                return f.applyToEither(g, a);
526 +            }
527          },
528  
529 < //             /** Experimental way to do more testing */
530 < //         REVERSE_DEFAULT {
531 < //             public <T,U> CompletableFuture<Void> runAfterBoth
532 < //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
533 < //                 return g.runAfterBoth(f, a);
534 < //             }
535 < //             public <T,U> CompletableFuture<Void> thenAcceptBoth
536 < //                 (CompletableFuture<T> f,
537 < //                  CompletionStage<? extends U> g,
538 < //                  BiConsumer<? super T,? super U> a) {
539 < //                 return DEFAULT.thenAcceptBoth(f, g, a);
540 < //             }
541 < //         },
542 <
543 <        DEFAULT_ASYNC {
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.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.whenCompleteAsync(a);
560 >            }
561              public <T,U> CompletableFuture<Void> runAfterBoth
562                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
563                  return f.runAfterBothAsync(g, a);
# Line 480 | 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> CompletableFuture<Void> runAfterEither
578 +                (CompletableFuture<T> f,
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,
585 +                 CompletionStage<? extends T> g,
586 +                 Consumer<? super T> a) {
587 +                return f.acceptEitherAsync(g, a);
588 +            }
589 +            public <T,U> CompletableFuture<U> applyToEither
590 +                (CompletableFuture<T> f,
591 +                 CompletionStage<? extends T> g,
592 +                 Function<? super T,U> a) {
593 +                return f.applyToEitherAsync(g, a);
594 +            }
595          },
596  
485 //         REVERSE_DEFAULT_ASYNC {
486 //             public <T,U> CompletableFuture<Void> runAfterBoth
487 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
488 //                 return f.runAfterBothAsync(g, a);
489 //             }
490 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
491 //                 (CompletableFuture<T> f,
492 //                  CompletionStage<? extends U> g,
493 //                  BiConsumer<? super T,? super U> a) {
494 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
495 //             }
496 //         },
497
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, 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, new ThreadExecutor());
627 +            }
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 512 | Line 641 | public class CompletableFutureTest exten
641                   BiFunction<? super T,? super U,? extends V> a) {
642                  return f.thenCombineAsync(g, a, new ThreadExecutor());
643              }
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> CompletableFuture<Void> acceptEither
651 +                (CompletableFuture<T> f,
652 +                 CompletionStage<? extends T> g,
653 +                 Consumer<? super T> a) {
654 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
655 +            }
656 +            public <T,U> CompletableFuture<U> applyToEither
657 +                (CompletableFuture<T> f,
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 524 | Line 687 | public class CompletableFutureTest exten
687              (CompletableFuture<T> f,
688               CompletionStage<? extends U> g,
689               BiFunction<? super T,? super U,? extends V> a);
690 +        public abstract <T> CompletableFuture<Void> runAfterEither
691 +            (CompletableFuture<T> f,
692 +             CompletionStage<?> g,
693 +             java.lang.Runnable a);
694 +        public abstract <T> CompletableFuture<Void> acceptEither
695 +            (CompletableFuture<T> f,
696 +             CompletionStage<? extends T> g,
697 +             Consumer<? super T> a);
698 +        public abstract <T,U> CompletableFuture<U> applyToEither
699 +            (CompletableFuture<T> f,
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 +
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 <        assertTrue(r.ran);
559 <        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 <        assertFalse(r.ran);
804 <        f.completeExceptionally(new CFException());
805 <        checkCompletedNormally(g, three);
806 <        assertTrue(r.ran);
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 <        assertTrue(r.ran);
572 <        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 <        assertFalse(r.ran);
832 <        f.complete(one);
833 <        assertTrue(r.ran);
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 586 | Line 912 | public class CompletableFutureTest exten
912          Noop r = new Noop();
913          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
914          assertNull(f.join());
915 <        assertTrue(r.ran);
915 >        assertEquals(1, r.invocationCount);
916          checkCompletedNormally(f, null);
917      }
918  
# Line 598 | Line 924 | public class CompletableFutureTest exten
924          ThreadExecutor exec = new ThreadExecutor();
925          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
926          assertNull(f.join());
927 <        assertTrue(r.ran);
927 >        assertEquals(1, r.invocationCount);
928          checkCompletedNormally(f, null);
929          assertEquals(1, exec.count.get());
930      }
# Line 607 | 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 <        assertTrue(r.ran);
939 >        assertEquals(1, r.invocationCount);
940      }
941  
942      /**
# Line 640 | Line 966 | public class CompletableFutureTest exten
966          FailingSupplier r = new FailingSupplier();
967          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
968          checkCompletedWithWrappedCFException(f);
969 <        assertTrue(r.ran);
969 >        assertEquals(1, r.invocationCount);
970      }
971  
972      // seq completion methods
# Line 648 | 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 <        assertTrue(r.ran);
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  
662        f = new CompletableFuture<>();
663        f.complete(null);
664        g = f.thenRun(r = new Noop());
988          checkCompletedNormally(g, null);
989 <        assertTrue(r.ran);
990 <    }
989 >        checkCompletedNormally(f, v1);
990 >        assertEquals(1, r.invocationCount);
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 <        assertFalse(r.ran);
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());
1010 <        g = f.thenRun(r = new Noop());
1011 <        checkCompletedWithWrappedCFException(g);
688 <        assertFalse(r.ran);
689 <    }
1008 >        checkCompletedWithWrappedCFException(g, ex);
1009 >        checkCompletedWithWrappedCFException(f, ex);
1010 >        assertEquals(0, r.invocationCount);
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);
708 <    }
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());
726 <        checkCompletedWithWrappedCancellationException(g);
727 <    }
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() {
754 <        CompletableFuture<Integer> f = new CompletableFuture<>();
755 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
756 <        f.complete(one);
757 <        checkCompletedWithWrappedCFException(g);
758 <    }
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 <     * thenAccept result completes normally after normal completion of source
1112 >     * thenApply result completes exceptionally if action does
1113       */
1114 <    public void testThenAccept() {
1115 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1116 <        IncAction r = new IncAction();
1117 <        CompletableFuture<Void> g = f.thenAccept(r);
1118 <        f.complete(one);
1119 <        checkCompletedNormally(g, null);
1120 <        assertEquals(r.value, 2);
1121 <    }
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  
782    /**
783     * thenAccept result completes exceptionally after exceptional
784     * completion of source
785     */
786    public void testThenAccept2() {
787        CompletableFuture<Integer> f = new CompletableFuture<>();
788        IncAction r = new IncAction();
789        CompletableFuture<Void> g = f.thenAccept(r);
790        f.completeExceptionally(new CFException());
1125          checkCompletedWithWrappedCFException(g);
1126 <    }
1126 >        checkCompletedNormally(f, v1);
1127 >    }}
1128  
1129      /**
1130 <     * thenAccept result completes exceptionally if action does
1130 >     * thenAccept result completes normally after normal completion of source
1131       */
1132 <    public void testThenAccept3() {
1133 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1134 <        FailingConsumer r = new FailingConsumer();
1135 <        CompletableFuture<Void> g = f.thenAccept(r);
1136 <        f.complete(one);
1137 <        checkCompletedWithWrappedCFException(g);
1138 <        assertTrue(r.ran);
1139 <    }
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 <    /**
1144 <     * thenAccept result completes exceptionally if source cancelled
1145 <     */
1146 <    public void testThenAccept4() {
1147 <        CompletableFuture<Integer> f = new CompletableFuture<>();
811 <        IncAction r = new IncAction();
812 <        CompletableFuture<Void> g = f.thenAccept(r);
813 <        assertTrue(f.cancel(true));
814 <        checkCompletedWithWrappedCancellationException(g);
815 <    }
1143 >        checkCompletedNormally(g, null);
1144 >        checkCompletedNormally(f, v1);
1145 >        assertEquals(1, r.invocationCount);
1146 >        assertEquals(inc(v1), r.value);
1147 >    }}
1148  
1149      /**
1150 <     * thenCombine result completes normally after normal completion
1151 <     * of sources
1150 >     * thenAccept result completes exceptionally after exceptional
1151 >     * completion of source
1152       */
1153 <    public void testThenCombine_normalCompletion1() {
1153 >    public void testThenAccept_exceptionalCompletion() {
1154          for (ExecutionMode m : ExecutionMode.values())
1155 <        for (Integer v1 : new Integer[] { 1, null })
1156 <        for (Integer v2 : new Integer[] { 2, null }) {
1157 <
1155 >        for (boolean createIncomplete : new boolean[] { true, false })
1156 >    {
1157 >        final CFException ex = new CFException();
1158          final CompletableFuture<Integer> f = new CompletableFuture<>();
1159 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1160 <        final SubtractFunction r = new SubtractFunction();
1161 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
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 <        f.complete(v1);
1165 <        checkIncomplete(h);
1166 <        assertFalse(r.ran());
1167 <        g.complete(v2);
835 <
836 <        checkCompletedNormally(h, subtract(v1, v2));
837 <        checkCompletedNormally(f, v1);
838 <        checkCompletedNormally(g, v2);
839 <        }
840 <    }
1164 >        checkCompletedWithWrappedCFException(g, ex);
1165 >        checkCompletedWithWrappedCFException(f, ex);
1166 >        assertEquals(0, r.invocationCount);
1167 >    }}
1168  
1169 <    public void testThenCombine_normalCompletion2() {
1169 >    /**
1170 >     * thenAccept result completes exceptionally if action does
1171 >     */
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 <        for (Integer v2 : new Integer[] { 2, null }) {
846 <
1176 >    {
1177          final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1179 <        final SubtractFunction r = new SubtractFunction();
1180 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1181 <
852 <        g.complete(v2);
853 <        checkIncomplete(h);
854 <        assertFalse(r.ran());
855 <        f.complete(v1);
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 <        checkCompletedNormally(h, subtract(v1, v2));
1183 >        checkCompletedWithWrappedCFException(g);
1184          checkCompletedNormally(f, v1);
1185 <        checkCompletedNormally(g, v2);
860 <        }
861 <    }
1185 >    }}
1186  
1187 <    public void testThenCombine_normalCompletion3() {
1187 >    /**
1188 >     * thenAccept result completes exceptionally if source cancelled
1189 >     */
1190 >    public void testThenAccept_sourceCancelled() {
1191          for (ExecutionMode m : ExecutionMode.values())
1192 <        for (Integer v1 : new Integer[] { 1, null })
1193 <        for (Integer v2 : new Integer[] { 2, null }) {
1194 <
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1197 <        final SubtractFunction r = new SubtractFunction();
1198 <
1199 <        g.complete(v2);
1200 <        f.complete(v1);
1201 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
875 <
876 <        checkCompletedNormally(h, subtract(v1, v2));
877 <        checkCompletedNormally(f, v1);
878 <        checkCompletedNormally(g, v2);
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          }
880    }
1203  
1204 <    public void testThenCombine_normalCompletion4() {
1204 >        checkCompletedWithWrappedCancellationException(g);
1205 >        checkCancelled(f);
1206 >        assertEquals(0, r.invocationCount);
1207 >    }}
1208 >
1209 >    /**
1210 >     * thenCombine result completes normally after normal completion
1211 >     * of sources
1212 >     */
1213 >    public void testThenCombine_normalCompletion1() {
1214 >        for (boolean createIncomplete : new boolean[] { true, false })
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();
1223 +        CompletableFuture<Integer> h = null;
1224 +        if (createIncomplete) h = m.thenCombine(f, g, r);
1225  
1226 <        f.complete(v1);
1227 <        g.complete(v2);
1228 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1226 >        if (fFirst)
1227 >            f.complete(v1);
1228 >        else
1229 >            g.complete(v2);
1230 >        if (createIncomplete) checkIncomplete(h);
1231 >        assertEquals(0, r.invocationCount);
1232 >        if (!fFirst)
1233 >            f.complete(v1);
1234 >        else
1235 >            g.complete(v2);
1236 >        if (!createIncomplete) h = m.thenCombine(f, g, r);
1237  
1238          checkCompletedNormally(h, subtract(v1, v2));
1239          checkCompletedNormally(f, v1);
1240          checkCompletedNormally(g, v2);
1241 <        }
1242 <    }
1241 >        assertEquals(1, r.invocationCount);
1242 >    }}
1243  
1244      /**
1245       * thenCombine result completes exceptionally after exceptional
# Line 904 | 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 918 | Line 1261 | public class CompletableFutureTest exten
1261  
1262          checkCompletedWithWrappedCFException(h, ex);
1263          checkCompletedWithWrappedCFException(f, ex);
1264 <        assertFalse(r.ran());
1264 >        assertEquals(0, r.invocationCount);
1265          checkCompletedNormally(g, v1);
1266 <        }
924 <    }
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();
1275 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1275 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1276          final CFException ex = new CFException();
1277  
1278          g.completeExceptionally(ex);
# Line 939 | Line 1281 | public class CompletableFutureTest exten
1281  
1282          checkCompletedWithWrappedCFException(h, ex);
1283          checkCompletedWithWrappedCFException(g, ex);
1284 <        assertFalse(r.ran());
1284 >        assertEquals(0, r.invocationCount);
1285          checkCompletedNormally(f, v1);
1286 <        }
945 <    }
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 959 | Line 1300 | public class CompletableFutureTest exten
1300  
1301          checkCompletedWithWrappedCFException(h, ex);
1302          checkCompletedWithWrappedCFException(g, ex);
1303 <        assertFalse(r.ran());
1303 >        assertEquals(0, r.invocationCount);
1304          checkCompletedNormally(f, v1);
1305 <        }
965 <    }
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 975 | Line 1315 | public class CompletableFutureTest exten
1315  
1316          f.completeExceptionally(ex);
1317          g.complete(v1);
1318 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1318 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1319  
1320          checkCompletedWithWrappedCFException(h, ex);
1321          checkCompletedWithWrappedCFException(f, ex);
1322 <        assertFalse(r.ran());
1322 >        assertEquals(0, r.invocationCount);
1323          checkCompletedNormally(g, v1);
1324 <        }
985 <    }
1324 >    }}
1325  
1326      /**
1327       * thenCombine result completes exceptionally if action does
# Line 990 | 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 1004 | Line 1343 | public class CompletableFutureTest exten
1343          checkCompletedWithWrappedCFException(h);
1344          checkCompletedNormally(f, v1);
1345          checkCompletedNormally(g, v2);
1346 <        }
1008 <    }
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 1024 | Line 1362 | public class CompletableFutureTest exten
1362          checkCompletedWithWrappedCFException(h);
1363          checkCompletedNormally(f, v1);
1364          checkCompletedNormally(g, v2);
1365 <        }
1028 <    }
1365 >    }}
1366  
1367      /**
1368       * thenCombine result completes exceptionally if either source cancelled
# Line 1033 | 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 1046 | Line 1383 | public class CompletableFutureTest exten
1383  
1384          checkCompletedWithWrappedCancellationException(h);
1385          checkCancelled(f);
1386 <        assertFalse(r.ran());
1386 >        assertEquals(0, r.invocationCount);
1387          checkCompletedNormally(g, v1);
1388 <        }
1052 <    }
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 1067 | Line 1403 | public class CompletableFutureTest exten
1403  
1404          checkCompletedWithWrappedCancellationException(h);
1405          checkCancelled(g);
1406 <        assertFalse(r.ran());
1406 >        assertEquals(0, r.invocationCount);
1407          checkCompletedNormally(f, v1);
1408 <        }
1073 <    }
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 1087 | Line 1422 | public class CompletableFutureTest exten
1422  
1423          checkCompletedWithWrappedCancellationException(h);
1424          checkCancelled(g);
1425 <        assertFalse(r.ran());
1425 >        assertEquals(0, r.invocationCount);
1426          checkCompletedNormally(f, v1);
1427 <        }
1093 <    }
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 1107 | Line 1441 | public class CompletableFutureTest exten
1441  
1442          checkCompletedWithWrappedCancellationException(h);
1443          checkCancelled(f);
1444 <        assertFalse(r.ran());
1444 >        assertEquals(0, r.invocationCount);
1445          checkCompletedNormally(g, v1);
1446 <        }
1113 <    }
1446 >    }}
1447  
1448      /**
1449       * thenAcceptBoth result completes normally after normal
# Line 1119 | 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 1128 | Line 1461 | public class CompletableFutureTest exten
1461  
1462          f.complete(v1);
1463          checkIncomplete(h);
1464 <        assertEquals(r.value, 0);
1464 >        assertEquals(0, r.invocationCount);
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 <        }
1139 <    }
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 1150 | Line 1482 | public class CompletableFutureTest exten
1482  
1483          g.complete(v2);
1484          checkIncomplete(h);
1485 <        assertEquals(r.value, 0);
1485 >        assertEquals(0, r.invocationCount);
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 <        }
1161 <    }
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 1174 | 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 <        }
1181 <    }
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 1194 | 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 <        }
1201 <    }
1530 >    }}
1531  
1532      /**
1533       * thenAcceptBoth result completes exceptionally after exceptional
# Line 1206 | 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 1220 | Line 1549 | public class CompletableFutureTest exten
1549  
1550          checkCompletedWithWrappedCFException(h, ex);
1551          checkCompletedWithWrappedCFException(f, ex);
1552 <        assertFalse(r.ran());
1552 >        assertEquals(0, r.invocationCount);
1553          checkCompletedNormally(g, v1);
1554 <        }
1226 <    }
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 1241 | Line 1569 | public class CompletableFutureTest exten
1569  
1570          checkCompletedWithWrappedCFException(h, ex);
1571          checkCompletedWithWrappedCFException(g, ex);
1572 <        assertFalse(r.ran());
1572 >        assertEquals(0, r.invocationCount);
1573          checkCompletedNormally(f, v1);
1574 <        }
1247 <    }
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 1261 | Line 1588 | public class CompletableFutureTest exten
1588  
1589          checkCompletedWithWrappedCFException(h, ex);
1590          checkCompletedWithWrappedCFException(g, ex);
1591 <        assertFalse(r.ran());
1591 >        assertEquals(0, r.invocationCount);
1592          checkCompletedNormally(f, v1);
1593 <        }
1267 <    }
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 1281 | Line 1607 | public class CompletableFutureTest exten
1607  
1608          checkCompletedWithWrappedCFException(h, ex);
1609          checkCompletedWithWrappedCFException(f, ex);
1610 <        assertFalse(r.ran());
1610 >        assertEquals(0, r.invocationCount);
1611          checkCompletedNormally(g, v1);
1612 <        }
1287 <    }
1612 >    }}
1613  
1614      /**
1615       * thenAcceptBoth result completes exceptionally if action does
# Line 1292 | 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 1306 | Line 1631 | public class CompletableFutureTest exten
1631          checkCompletedWithWrappedCFException(h);
1632          checkCompletedNormally(f, v1);
1633          checkCompletedNormally(g, v2);
1634 <        }
1310 <    }
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 1326 | Line 1650 | public class CompletableFutureTest exten
1650          checkCompletedWithWrappedCFException(h);
1651          checkCompletedNormally(f, v1);
1652          checkCompletedNormally(g, v2);
1653 <        }
1330 <    }
1653 >    }}
1654  
1655      /**
1656       * thenAcceptBoth result completes exceptionally if either source cancelled
# Line 1335 | 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 1348 | Line 1671 | public class CompletableFutureTest exten
1671  
1672          checkCompletedWithWrappedCancellationException(h);
1673          checkCancelled(f);
1674 <        assertFalse(r.ran());
1674 >        assertEquals(0, r.invocationCount);
1675          checkCompletedNormally(g, v1);
1676 <        }
1354 <    }
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 1369 | Line 1691 | public class CompletableFutureTest exten
1691  
1692          checkCompletedWithWrappedCancellationException(h);
1693          checkCancelled(g);
1694 <        assertFalse(r.ran());
1694 >        assertEquals(0, r.invocationCount);
1695          checkCompletedNormally(f, v1);
1696 <        }
1375 <    }
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 1389 | Line 1710 | public class CompletableFutureTest exten
1710  
1711          checkCompletedWithWrappedCancellationException(h);
1712          checkCancelled(g);
1713 <        assertFalse(r.ran());
1713 >        assertEquals(0, r.invocationCount);
1714          checkCompletedNormally(f, v1);
1715 <        }
1395 <    }
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 1409 | Line 1729 | public class CompletableFutureTest exten
1729  
1730          checkCompletedWithWrappedCancellationException(h);
1731          checkCancelled(f);
1732 <        assertFalse(r.ran());
1732 >        assertEquals(0, r.invocationCount);
1733          checkCompletedNormally(g, v1);
1734 <        }
1415 <    }
1734 >    }}
1735  
1736      /**
1737       * runAfterBoth result completes normally after normal
# Line 1421 | 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 1430 | Line 1749 | public class CompletableFutureTest exten
1749  
1750          f.complete(v1);
1751          checkIncomplete(h);
1752 <        assertFalse(r.ran);
1752 >        assertEquals(0, r.invocationCount);
1753          g.complete(v2);
1754  
1755          checkCompletedNormally(h, null);
1756 <        assertTrue(r.ran);
1756 >        assertEquals(1, r.invocationCount);
1757          checkCompletedNormally(f, v1);
1758          checkCompletedNormally(g, v2);
1759 <        }
1441 <    }
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 1452 | Line 1770 | public class CompletableFutureTest exten
1770  
1771          g.complete(v2);
1772          checkIncomplete(h);
1773 <        assertFalse(r.ran);
1773 >        assertEquals(0, r.invocationCount);
1774          f.complete(v1);
1775  
1776          checkCompletedNormally(h, null);
1777 <        assertTrue(r.ran);
1777 >        assertEquals(1, r.invocationCount);
1778          checkCompletedNormally(f, v1);
1779          checkCompletedNormally(g, v2);
1780 <        }
1463 <    }
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 1476 | Line 1793 | public class CompletableFutureTest exten
1793          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1794  
1795          checkCompletedNormally(h, null);
1796 <        assertTrue(r.ran);
1796 >        assertEquals(1, r.invocationCount);
1797          checkCompletedNormally(f, v1);
1798          checkCompletedNormally(g, v2);
1799 <        }
1483 <    }
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 1496 | Line 1812 | public class CompletableFutureTest exten
1812          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1813  
1814          checkCompletedNormally(h, null);
1815 <        assertTrue(r.ran);
1815 >        assertEquals(1, r.invocationCount);
1816          checkCompletedNormally(f, v1);
1817          checkCompletedNormally(g, v2);
1818 <        }
1503 <    }
1818 >    }}
1819  
1820      /**
1821       * runAfterBoth result completes exceptionally after exceptional
# Line 1508 | 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 1522 | Line 1837 | public class CompletableFutureTest exten
1837  
1838          checkCompletedWithWrappedCFException(h, ex);
1839          checkCompletedWithWrappedCFException(f, ex);
1840 <        assertFalse(r.ran);
1840 >        assertEquals(0, r.invocationCount);
1841          checkCompletedNormally(g, v1);
1842 <        }
1528 <    }
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 1543 | Line 1857 | public class CompletableFutureTest exten
1857  
1858          checkCompletedWithWrappedCFException(h, ex);
1859          checkCompletedWithWrappedCFException(g, ex);
1860 <        assertFalse(r.ran);
1860 >        assertEquals(0, r.invocationCount);
1861          checkCompletedNormally(f, v1);
1862 <        }
1549 <    }
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 1563 | Line 1876 | public class CompletableFutureTest exten
1876  
1877          checkCompletedWithWrappedCFException(h, ex);
1878          checkCompletedWithWrappedCFException(g, ex);
1879 <        assertFalse(r.ran);
1879 >        assertEquals(0, r.invocationCount);
1880          checkCompletedNormally(f, v1);
1881 <        }
1569 <    }
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 1583 | Line 1895 | public class CompletableFutureTest exten
1895  
1896          checkCompletedWithWrappedCFException(h, ex);
1897          checkCompletedWithWrappedCFException(f, ex);
1898 <        assertFalse(r.ran);
1898 >        assertEquals(0, r.invocationCount);
1899          checkCompletedNormally(g, v1);
1900 <        }
1589 <    }
1900 >    }}
1901  
1902      /**
1903       * runAfterBoth result completes exceptionally if action does
# Line 1594 | 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 1608 | Line 1919 | public class CompletableFutureTest exten
1919          checkCompletedWithWrappedCFException(h);
1920          checkCompletedNormally(f, v1);
1921          checkCompletedNormally(g, v2);
1922 <        }
1612 <    }
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 1628 | Line 1938 | public class CompletableFutureTest exten
1938          checkCompletedWithWrappedCFException(h);
1939          checkCompletedNormally(f, v1);
1940          checkCompletedNormally(g, v2);
1941 <        }
1632 <    }
1941 >    }}
1942  
1943      /**
1944       * runAfterBoth result completes exceptionally if either source cancelled
# Line 1637 | 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 1650 | Line 1959 | public class CompletableFutureTest exten
1959  
1960          checkCompletedWithWrappedCancellationException(h);
1961          checkCancelled(f);
1962 <        assertFalse(r.ran);
1962 >        assertEquals(0, r.invocationCount);
1963          checkCompletedNormally(g, v1);
1964 <        }
1656 <    }
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 1671 | Line 1979 | public class CompletableFutureTest exten
1979  
1980          checkCompletedWithWrappedCancellationException(h);
1981          checkCancelled(g);
1982 <        assertFalse(r.ran);
1982 >        assertEquals(0, r.invocationCount);
1983          checkCompletedNormally(f, v1);
1984 <        }
1677 <    }
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 1691 | Line 1998 | public class CompletableFutureTest exten
1998  
1999          checkCompletedWithWrappedCancellationException(h);
2000          checkCancelled(g);
2001 <        assertFalse(r.ran);
2001 >        assertEquals(0, r.invocationCount);
2002          checkCompletedNormally(f, v1);
2003 <        }
1697 <    }
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 1711 | Line 2017 | public class CompletableFutureTest exten
2017  
2018          checkCompletedWithWrappedCancellationException(h);
2019          checkCancelled(f);
2020 <        assertFalse(r.ran);
2020 >        assertEquals(0, r.invocationCount);
2021          checkCompletedNormally(g, v1);
2022 <        }
1717 <    }
2022 >    }}
2023  
2024      /**
2025       * applyToEither result completes normally after normal completion
2026       * of either source
2027       */
2028 <    public void testApplyToEither() {
2029 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2030 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2031 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2032 <        f.complete(one);
2033 <        checkCompletedNormally(g, two);
2034 <        f2.complete(one);
2035 <        checkCompletedNormally(g, two);
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 >    {
2033 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2034 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2035 >        final IncFunction r = new IncFunction();
2036 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2037  
2038 <        f = new CompletableFuture<>();
2039 <        f.complete(one);
2040 <        f2 = new CompletableFuture<>();
1735 <        g = f.applyToEither(f2, inc);
1736 <        checkCompletedNormally(g, two);
1737 <    }
2038 >        f.complete(v1);
2039 >        checkCompletedNormally(h, inc(v1));
2040 >        g.complete(v2);
2041  
2042 <    /**
2043 <     * applyToEither result completes exceptionally after exceptional
2044 <     * completion of either source
2045 <     */
1743 <    public void testApplyToEither2() {
1744 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1745 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1747 <        f.completeExceptionally(new CFException());
1748 <        f2.complete(one);
1749 <        checkCompletedWithWrappedCFException(g);
2042 >        checkCompletedNormally(f, v1);
2043 >        checkCompletedNormally(g, v2);
2044 >        checkCompletedNormally(h, inc(v1));
2045 >    }}
2046  
2047 <        f = new CompletableFuture<>();
2048 <        f2 = new CompletableFuture<>();
2049 <        f2.completeExceptionally(new CFException());
2050 <        g = f.applyToEither(f2, inc);
2051 <        checkCompletedWithWrappedCFException(g);
2052 <    }
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 >    {
2052 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2053 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2054 >        final IncFunction r = new IncFunction();
2055 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2056  
2057 <    /**
2058 <     * applyToEither result completes exceptionally if action does
2059 <     */
1761 <    public void testApplyToEither3() {
1762 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1763 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1764 <        FailingFunction r = new FailingFunction();
1765 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1766 <        f2.complete(two);
1767 <        checkCompletedWithWrappedCFException(g);
1768 <    }
2057 >        g.complete(v2);
2058 >        checkCompletedNormally(h, inc(v2));
2059 >        f.complete(v1);
2060  
2061 <    /**
2062 <     * applyToEither result completes exceptionally if either source cancelled
2063 <     */
2064 <    public void testApplyToEither4() {
1774 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1775 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1776 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1777 <        assertTrue(f.cancel(true));
1778 <        checkCompletedWithWrappedCancellationException(g);
1779 <        f = new CompletableFuture<>();
1780 <        f2 = new CompletableFuture<>();
1781 <        assertTrue(f2.cancel(true));
1782 <        checkCompletedWithWrappedCancellationException(g);
1783 <    }
2061 >        checkCompletedNormally(f, v1);
2062 >        checkCompletedNormally(g, v2);
2063 >        checkCompletedNormally(h, inc(v2));
2064 >        }}
2065  
2066 <    /**
2067 <     * acceptEither result completes normally after normal completion
2068 <     * of either source
2069 <     */
2070 <    public void testAcceptEither() {
2071 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2072 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2073 <        IncAction r = new IncAction();
1793 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1794 <        f.complete(one);
1795 <        checkCompletedNormally(g, null);
1796 <        f2.complete(one);
1797 <        checkCompletedNormally(g, null);
1798 <        assertEquals(r.value, 2);
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 >    {
2071 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2072 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2073 >        final IncFunction r = new IncFunction();
2074  
2075 <        r = new IncAction();
2076 <        f = new CompletableFuture<>();
2077 <        f.complete(one);
2078 <        f2 = new CompletableFuture<>();
2079 <        g = f.acceptEither(f2, r);
2080 <        checkCompletedNormally(g, null);
2081 <        assertEquals(r.value, 2);
2082 <    }
2075 >        f.complete(v1);
2076 >        g.complete(v2);
2077 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2078 >
2079 >        checkCompletedNormally(f, v1);
2080 >        checkCompletedNormally(g, v2);
2081 >
2082 >        // unspecified behavior
2083 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2084 >                   Objects.equals(h.join(), inc(v2)));
2085 >        assertEquals(1, r.invocationCount);
2086 >    }}
2087  
2088      /**
2089 <     * acceptEither result completes exceptionally after exceptional
2089 >     * applyToEither result completes exceptionally after exceptional
2090       * completion of either source
2091       */
2092 <    public void testAcceptEither2() {
2093 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2094 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2095 <        IncAction r = new IncAction();
2096 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2097 <        f.completeExceptionally(new CFException());
2098 <        f2.complete(one);
2099 <        checkCompletedWithWrappedCFException(g);
2092 >    public void testApplyToEither_exceptionalCompletion1() {
2093 >        for (ExecutionMode m : ExecutionMode.values())
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();
2099 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2100 >        final CFException ex = new CFException();
2101  
2102 <        r = new IncAction();
2103 <        f = new CompletableFuture<>();
2104 <        f2 = new CompletableFuture<>();
1825 <        f2.completeExceptionally(new CFException());
1826 <        g = f.acceptEither(f2, r);
1827 <        checkCompletedWithWrappedCFException(g);
1828 <    }
2102 >        f.completeExceptionally(ex);
2103 >        checkCompletedWithWrappedCFException(h, ex);
2104 >        g.complete(v1);
2105  
2106 <    /**
2107 <     * acceptEither result completes exceptionally if action does
2108 <     */
2109 <    public void testAcceptEither3() {
2110 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1835 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1836 <        FailingConsumer r = new FailingConsumer();
1837 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1838 <        f2.complete(two);
1839 <        checkCompletedWithWrappedCFException(g);
1840 <    }
2106 >        assertEquals(0, r.invocationCount);
2107 >        checkCompletedNormally(g, v1);
2108 >        checkCompletedWithWrappedCFException(f, ex);
2109 >        checkCompletedWithWrappedCFException(h, ex);
2110 >    }}
2111  
2112 <    /**
2113 <     * acceptEither result completes exceptionally if either source cancelled
2114 <     */
2115 <    public void testAcceptEither4() {
2116 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2117 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2118 <        IncAction r = new IncAction();
2119 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2120 <        assertTrue(f.cancel(true));
1851 <        checkCompletedWithWrappedCancellationException(g);
1852 <        f = new CompletableFuture<>();
1853 <        f2 = new CompletableFuture<>();
1854 <        assertTrue(f2.cancel(true));
1855 <        checkCompletedWithWrappedCancellationException(g);
1856 <    }
2112 >    public void testApplyToEither_exceptionalCompletion2() {
2113 >        for (ExecutionMode m : ExecutionMode.values())
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();
2119 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2120 >        final CFException ex = new CFException();
2121  
2122 <    /**
2123 <     * runAfterEither result completes normally after normal completion
2124 <     * of either source
1861 <     */
1862 <    public void testRunAfterEither() {
1863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1864 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1865 <        Noop r = new Noop();
1866 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1867 <        f.complete(one);
1868 <        checkCompletedNormally(g, null);
1869 <        f2.complete(one);
1870 <        checkCompletedNormally(g, null);
1871 <        assertTrue(r.ran);
2122 >        g.completeExceptionally(ex);
2123 >        checkCompletedWithWrappedCFException(h, ex);
2124 >        f.complete(v1);
2125  
2126 <        r = new Noop();
2127 <        f = new CompletableFuture<>();
2128 <        f.complete(one);
2129 <        f2 = new CompletableFuture<>();
2130 <        g = f.runAfterEither(f2, r);
1878 <        checkCompletedNormally(g, null);
1879 <        assertTrue(r.ran);
1880 <    }
2126 >        assertEquals(0, r.invocationCount);
2127 >        checkCompletedNormally(f, v1);
2128 >        checkCompletedWithWrappedCFException(g, ex);
2129 >        checkCompletedWithWrappedCFException(h, ex);
2130 >    }}
2131  
2132 <    /**
2133 <     * runAfterEither result completes exceptionally after exceptional
2134 <     * completion of either source
2135 <     */
2136 <    public void testRunAfterEither2() {
2137 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2138 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2139 <        Noop r = new Noop();
1890 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1891 <        f.completeExceptionally(new CFException());
1892 <        f2.complete(one);
1893 <        checkCompletedWithWrappedCFException(g);
2132 >    public void testApplyToEither_exceptionalCompletion3() {
2133 >        for (ExecutionMode m : ExecutionMode.values())
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();
2139 >        final CFException ex = new CFException();
2140  
2141 <        r = new Noop();
2142 <        f = new CompletableFuture<>();
2143 <        f2 = new CompletableFuture<>();
1898 <        f2.completeExceptionally(new CFException());
1899 <        g = f.runAfterEither(f2, r);
1900 <        checkCompletedWithWrappedCFException(g);
1901 <    }
2141 >        g.completeExceptionally(ex);
2142 >        f.complete(v1);
2143 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2144  
2145 <    /**
2146 <     * runAfterEither result completes exceptionally if action does
2147 <     */
2148 <    public void testRunAfterEither3() {
2149 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2150 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2151 <        FailingNoop r = new FailingNoop();
2152 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2153 <        f2.complete(two);
1912 <        checkCompletedWithWrappedCFException(g);
1913 <    }
2145 >        // unspecified behavior
2146 >        Integer v;
2147 >        try {
2148 >            assertEquals(inc(v1), h.join());
2149 >            assertEquals(1, r.invocationCount);
2150 >        } catch (CompletionException ok) {
2151 >            checkCompletedWithWrappedCFException(h, ex);
2152 >            assertEquals(0, r.invocationCount);
2153 >        }
2154  
2155 <    /**
2156 <     * runAfterEither result completes exceptionally if either source cancelled
2157 <     */
1918 <    public void testRunAfterEither4() {
1919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1920 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1921 <        Noop r = new Noop();
1922 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1923 <        assertTrue(f.cancel(true));
1924 <        checkCompletedWithWrappedCancellationException(g);
1925 <        f = new CompletableFuture<>();
1926 <        f2 = new CompletableFuture<>();
1927 <        assertTrue(f2.cancel(true));
1928 <        checkCompletedWithWrappedCancellationException(g);
1929 <    }
2155 >        checkCompletedWithWrappedCFException(g, ex);
2156 >        checkCompletedNormally(f, v1);
2157 >    }}
2158  
2159 <    /**
2160 <     * thenCompose result completes normally after normal completion of source
2161 <     */
2162 <    public void testThenCompose() {
2163 <        CompletableFuture<Integer> f, g;
2164 <        CompletableFutureInc r;
2159 >    public void testApplyToEither_exceptionalCompletion4() {
2160 >        for (ExecutionMode m : ExecutionMode.values())
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();
2166 >        final CFException ex = new CFException();
2167  
2168 <        f = new CompletableFuture<>();
2169 <        g = f.thenCompose(r = new CompletableFutureInc());
2170 <        f.complete(one);
1941 <        checkCompletedNormally(g, two);
1942 <        assertTrue(r.ran);
2168 >        f.completeExceptionally(ex);
2169 >        g.complete(v1);
2170 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2171  
2172 <        f = new CompletableFuture<>();
2173 <        f.complete(one);
2174 <        g = f.thenCompose(r = new CompletableFutureInc());
2175 <        checkCompletedNormally(g, two);
2176 <        assertTrue(r.ran);
2177 <    }
2172 >        // unspecified behavior
2173 >        Integer v;
2174 >        try {
2175 >            assertEquals(inc(v1), h.join());
2176 >            assertEquals(1, r.invocationCount);
2177 >        } catch (CompletionException ok) {
2178 >            checkCompletedWithWrappedCFException(h, ex);
2179 >            assertEquals(0, r.invocationCount);
2180 >        }
2181 >
2182 >        checkCompletedWithWrappedCFException(f, ex);
2183 >        checkCompletedNormally(g, v1);
2184 >    }}
2185  
2186      /**
2187 <     * thenCompose result completes exceptionally after exceptional
1953 <     * completion of source
2187 >     * applyToEither result completes exceptionally if action does
2188       */
2189 <    public void testThenCompose2() {
2190 <        CompletableFuture<Integer> f, g;
2191 <        CompletableFutureInc r;
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 >    {
2194 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2195 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 >        final FailingFunction r = new FailingFunction();
2197 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2198  
2199 <        f = new CompletableFuture<>();
2200 <        g = f.thenCompose(r = new CompletableFutureInc());
2201 <        f.completeExceptionally(new CFException());
2202 <        checkCompletedWithWrappedCFException(g);
2199 >        f.complete(v1);
2200 >        checkCompletedWithWrappedCFException(h);
2201 >        g.complete(v2);
2202 >        checkCompletedNormally(f, v1);
2203 >        checkCompletedNormally(g, v2);
2204 >    }}
2205  
2206 <        f = new CompletableFuture<>();
2207 <        f.completeExceptionally(new CFException());
2208 <        g = f.thenCompose(r = new CompletableFutureInc());
2209 <        checkCompletedWithWrappedCFException(g);
2210 <    }
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 >    {
2211 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2212 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2213 >        final FailingFunction r = new FailingFunction();
2214 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2215 >
2216 >        g.complete(v2);
2217 >        checkCompletedWithWrappedCFException(h);
2218 >        f.complete(v1);
2219 >        checkCompletedNormally(f, v1);
2220 >        checkCompletedNormally(g, v2);
2221 >    }}
2222  
2223      /**
2224 <     * thenCompose result completes exceptionally if action does
2224 >     * applyToEither result completes exceptionally if either source cancelled
2225       */
2226 <    public void testThenCompose3() {
2227 <        CompletableFuture<Integer> f, g;
2228 <        FailingCompletableFutureFunction r;
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 >    {
2231 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2232 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2233 >        final IncFunction r = new IncFunction();
2234 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2235  
2236 <        f = new CompletableFuture<>();
2237 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2238 <        f.complete(one);
1980 <        checkCompletedWithWrappedCFException(g);
2236 >        assertTrue(f.cancel(mayInterruptIfRunning));
2237 >        checkCompletedWithWrappedCancellationException(h);
2238 >        g.complete(v1);
2239  
2240 <        f = new CompletableFuture<>();
2241 <        f.complete(one);
2242 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2243 <        checkCompletedWithWrappedCFException(g);
2244 <    }
2240 >        checkCancelled(f);
2241 >        assertEquals(0, r.invocationCount);
2242 >        checkCompletedNormally(g, v1);
2243 >        checkCompletedWithWrappedCancellationException(h);
2244 >    }}
2245  
2246 <    /**
2247 <     * thenCompose result completes exceptionally if source cancelled
2248 <     */
2249 <    public void testThenCompose4() {
2250 <        CompletableFuture<Integer> f, g;
2251 <        CompletableFutureInc r;
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 >    {
2251 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2252 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2253 >        final IncFunction r = new IncFunction();
2254 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2255  
2256 <        f = new CompletableFuture<>();
2257 <        g = f.thenCompose(r = new CompletableFutureInc());
2258 <        assertTrue(f.cancel(true));
1998 <        checkCompletedWithWrappedCancellationException(g);
2256 >        assertTrue(g.cancel(mayInterruptIfRunning));
2257 >        checkCompletedWithWrappedCancellationException(h);
2258 >        f.complete(v1);
2259  
2260 <        f = new CompletableFuture<>();
2261 <        assertTrue(f.cancel(true));
2262 <        g = f.thenCompose(r = new CompletableFutureInc());
2263 <        checkCompletedWithWrappedCancellationException(g);
2264 <    }
2260 >        checkCancelled(g);
2261 >        assertEquals(0, r.invocationCount);
2262 >        checkCompletedNormally(f, v1);
2263 >        checkCompletedWithWrappedCancellationException(h);
2264 >    }}
2265  
2266 <    // asyncs
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 >    {
2271 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2272 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2273 >        final IncFunction r = new IncFunction();
2274  
2275 <    /**
2276 <     * thenRunAsync result completes normally after normal completion of source
2277 <     */
2011 <    public void testThenRunAsync() {
2012 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2013 <        Noop r = new Noop();
2014 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2015 <        f.complete(null);
2016 <        checkCompletedNormally(g, null);
2275 >        assertTrue(g.cancel(mayInterruptIfRunning));
2276 >        f.complete(v1);
2277 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2278  
2279 <        // reordered version
2280 <        f = new CompletableFuture<>();
2281 <        f.complete(null);
2282 <        r = new Noop();
2283 <        g = f.thenRunAsync(r);
2284 <        checkCompletedNormally(g, null);
2285 <    }
2279 >        // unspecified behavior
2280 >        Integer v;
2281 >        try {
2282 >            assertEquals(inc(v1), h.join());
2283 >            assertEquals(1, r.invocationCount);
2284 >        } catch (CompletionException ok) {
2285 >            checkCompletedWithWrappedCancellationException(h);
2286 >            assertEquals(0, r.invocationCount);
2287 >        }
2288  
2289 <    /**
2290 <     * thenRunAsync result completes exceptionally after exceptional
2291 <     * completion of source
2292 <     */
2293 <    public void testThenRunAsync2() {
2294 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2295 <        Noop r = new Noop();
2296 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2297 <        f.completeExceptionally(new CFException());
2289 >        checkCancelled(g);
2290 >        checkCompletedNormally(f, v1);
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 >    {
2298 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2299 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2300 >        final IncFunction r = new IncFunction();
2301 >
2302 >        assertTrue(f.cancel(mayInterruptIfRunning));
2303 >        g.complete(v1);
2304 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2305 >
2306 >        // unspecified behavior
2307 >        Integer v;
2308          try {
2309 <            g.join();
2310 <            shouldThrow();
2311 <        } catch (CompletionException success) {}
2312 <        checkCompletedWithWrappedCFException(g);
2313 <    }
2309 >            assertEquals(inc(v1), h.join());
2310 >            assertEquals(1, r.invocationCount);
2311 >        } catch (CompletionException ok) {
2312 >            checkCompletedWithWrappedCancellationException(h);
2313 >            assertEquals(0, r.invocationCount);
2314 >        }
2315  
2316 <    /**
2317 <     * thenRunAsync result completes exceptionally if action does
2318 <     */
2045 <    public void testThenRunAsync3() {
2046 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2047 <        FailingNoop r = new FailingNoop();
2048 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2049 <        f.complete(null);
2050 <        checkCompletedWithWrappedCFException(g);
2051 <    }
2316 >        checkCancelled(f);
2317 >        checkCompletedNormally(g, v1);
2318 >    }}
2319  
2320      /**
2321 <     * thenRunAsync result completes exceptionally if source cancelled
2321 >     * acceptEither result completes normally after normal completion
2322 >     * of either source
2323       */
2324 <    public void testThenRunAsync4() {
2325 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2326 <        Noop r = new Noop();
2327 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2328 <        assertTrue(f.cancel(true));
2329 <        checkCompletedWithWrappedCancellationException(g);
2330 <    }
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 >    {
2329 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2330 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2331 >        final IncAction r = new IncAction();
2332 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2333  
2334 <    /**
2335 <     * thenApplyAsync result completes normally after normal completion of source
2336 <     */
2337 <    public void testThenApplyAsync() {
2068 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2069 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2070 <        f.complete(one);
2071 <        checkCompletedNormally(g, two);
2072 <    }
2334 >        f.complete(v1);
2335 >        checkCompletedNormally(h, null);
2336 >        assertEquals(inc(v1), r.value);
2337 >        g.complete(v2);
2338  
2339 <    /**
2340 <     * thenApplyAsync result completes exceptionally after exceptional
2341 <     * completion of source
2342 <     */
2078 <    public void testThenApplyAsync2() {
2079 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2080 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2081 <        f.completeExceptionally(new CFException());
2082 <        checkCompletedWithWrappedCFException(g);
2083 <    }
2339 >        checkCompletedNormally(f, v1);
2340 >        checkCompletedNormally(g, v2);
2341 >        checkCompletedNormally(h, null);
2342 >    }}
2343  
2344 <    /**
2345 <     * thenApplyAsync result completes exceptionally if action does
2346 <     */
2347 <    public void testThenApplyAsync3() {
2348 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2349 <        FailingFunction r = new FailingFunction();
2350 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2351 <        f.complete(null);
2352 <        checkCompletedWithWrappedCFException(g);
2094 <    }
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 >    {
2349 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2350 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2351 >        final IncAction r = new IncAction();
2352 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2353  
2354 <    /**
2355 <     * thenApplyAsync result completes exceptionally if source cancelled
2356 <     */
2357 <    public void testThenApplyAsync4() {
2100 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2101 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2102 <        assertTrue(f.cancel(true));
2103 <        checkCompletedWithWrappedCancellationException(g);
2104 <    }
2354 >        g.complete(v2);
2355 >        checkCompletedNormally(h, null);
2356 >        assertEquals(inc(v2), r.value);
2357 >        f.complete(v1);
2358  
2359 <    /**
2360 <     * thenAcceptAsync result completes normally after normal
2361 <     * completion of source
2362 <     */
2110 <    public void testThenAcceptAsync() {
2111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2112 <        IncAction r = new IncAction();
2113 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2114 <        f.complete(one);
2115 <        checkCompletedNormally(g, null);
2116 <        assertEquals(r.value, 2);
2117 <    }
2359 >        checkCompletedNormally(f, v1);
2360 >        checkCompletedNormally(g, v2);
2361 >        checkCompletedNormally(h, null);
2362 >    }}
2363  
2364 <    /**
2365 <     * thenAcceptAsync result completes exceptionally after exceptional
2366 <     * completion of source
2367 <     */
2368 <    public void testThenAcceptAsync2() {
2369 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2370 <        IncAction r = new IncAction();
2371 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2127 <        f.completeExceptionally(new CFException());
2128 <        checkCompletedWithWrappedCFException(g);
2129 <    }
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 >    {
2369 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2370 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2371 >        final IncAction r = new IncAction();
2372  
2373 <    /**
2374 <     * thenAcceptAsync result completes exceptionally if action does
2375 <     */
2134 <    public void testThenAcceptAsync3() {
2135 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2136 <        FailingConsumer r = new FailingConsumer();
2137 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2138 <        f.complete(null);
2139 <        checkCompletedWithWrappedCFException(g);
2140 <    }
2373 >        f.complete(v1);
2374 >        g.complete(v2);
2375 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2376  
2377 <    /**
2378 <     * thenAcceptAsync result completes exceptionally if source cancelled
2379 <     */
2380 <    public void testThenAcceptAsync4() {
2381 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2382 <        IncAction r = new IncAction();
2383 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2384 <        assertTrue(f.cancel(true));
2150 <        checkCompletedWithWrappedCancellationException(g);
2151 <    }
2377 >        checkCompletedNormally(h, null);
2378 >        checkCompletedNormally(f, v1);
2379 >        checkCompletedNormally(g, v2);
2380 >
2381 >        // unspecified behavior
2382 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2383 >                   Objects.equals(r.value, inc(v2)));
2384 >    }}
2385  
2386      /**
2387 <     * applyToEitherAsync result completes normally after normal
2388 <     * completion of sources
2387 >     * acceptEither result completes exceptionally after exceptional
2388 >     * completion of either source
2389       */
2390 <    public void testApplyToEitherAsync() {
2391 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2392 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2393 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2394 <        f.complete(one);
2395 <        checkCompletedNormally(g, two);
2390 >    public void testAcceptEither_exceptionalCompletion1() {
2391 >        for (ExecutionMode m : ExecutionMode.values())
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();
2397 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2398 >        final CFException ex = new CFException();
2399  
2400 <        f = new CompletableFuture<>();
2401 <        f.complete(one);
2402 <        f2 = new CompletableFuture<>();
2167 <        g = f.applyToEitherAsync(f2, inc);
2168 <        checkCompletedNormally(g, two);
2169 <    }
2400 >        f.completeExceptionally(ex);
2401 >        checkCompletedWithWrappedCFException(h, ex);
2402 >        g.complete(v1);
2403  
2404 <    /**
2405 <     * applyToEitherAsync result completes exceptionally after exceptional
2406 <     * completion of source
2407 <     */
2408 <    public void testApplyToEitherAsync2() {
2176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2177 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2178 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2179 <        f.completeExceptionally(new CFException());
2180 <        checkCompletedWithWrappedCFException(g);
2404 >        assertEquals(0, r.invocationCount);
2405 >        checkCompletedNormally(g, v1);
2406 >        checkCompletedWithWrappedCFException(f, ex);
2407 >        checkCompletedWithWrappedCFException(h, ex);
2408 >    }}
2409  
2410 <        f = new CompletableFuture<>();
2411 <        f2 = new CompletableFuture<>();
2412 <        f2.completeExceptionally(new CFException());
2413 <        g = f.applyToEitherAsync(f2, inc);
2414 <        f.complete(one);
2415 <        checkCompletedWithWrappedCFException(g);
2416 <    }
2410 >    public void testAcceptEither_exceptionalCompletion2() {
2411 >        for (ExecutionMode m : ExecutionMode.values())
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();
2417 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2418 >        final CFException ex = new CFException();
2419  
2420 <    /**
2421 <     * applyToEitherAsync result completes exceptionally if action does
2422 <     */
2193 <    public void testApplyToEitherAsync3() {
2194 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2195 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2196 <        FailingFunction r = new FailingFunction();
2197 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2198 <        f.complete(one);
2199 <        checkCompletedWithWrappedCFException(g);
2200 <    }
2420 >        g.completeExceptionally(ex);
2421 >        checkCompletedWithWrappedCFException(h, ex);
2422 >        f.complete(v1);
2423  
2424 <    /**
2425 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2426 <     */
2427 <    public void testApplyToEitherAsync4() {
2428 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2207 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2208 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2209 <        assertTrue(f.cancel(true));
2210 <        checkCompletedWithWrappedCancellationException(g);
2424 >        assertEquals(0, r.invocationCount);
2425 >        checkCompletedNormally(f, v1);
2426 >        checkCompletedWithWrappedCFException(g, ex);
2427 >        checkCompletedWithWrappedCFException(h, ex);
2428 >    }}
2429  
2430 <        f = new CompletableFuture<>();
2431 <        f2 = new CompletableFuture<>();
2432 <        assertTrue(f2.cancel(true));
2433 <        g = f.applyToEitherAsync(f2, inc);
2434 <        checkCompletedWithWrappedCancellationException(g);
2435 <    }
2430 >    public void testAcceptEither_exceptionalCompletion3() {
2431 >        for (ExecutionMode m : ExecutionMode.values())
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();
2437 >        final CFException ex = new CFException();
2438  
2439 <    /**
2440 <     * acceptEitherAsync result completes normally after normal
2441 <     * completion of sources
2222 <     */
2223 <    public void testAcceptEitherAsync() {
2224 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2225 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226 <        IncAction r = new IncAction();
2227 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2228 <        f.complete(one);
2229 <        checkCompletedNormally(g, null);
2230 <        assertEquals(r.value, 2);
2439 >        g.completeExceptionally(ex);
2440 >        f.complete(v1);
2441 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2442  
2443 <        r = new IncAction();
2444 <        f = new CompletableFuture<>();
2445 <        f.complete(one);
2446 <        f2 = new CompletableFuture<>();
2447 <        g = f.acceptEitherAsync(f2, r);
2448 <        checkCompletedNormally(g, null);
2449 <        assertEquals(r.value, 2);
2450 <    }
2443 >        // unspecified behavior
2444 >        Integer v;
2445 >        try {
2446 >            assertNull(h.join());
2447 >            assertEquals(1, r.invocationCount);
2448 >            assertEquals(inc(v1), r.value);
2449 >        } catch (CompletionException ok) {
2450 >            checkCompletedWithWrappedCFException(h, ex);
2451 >            assertEquals(0, r.invocationCount);
2452 >        }
2453  
2454 <    /**
2455 <     * acceptEitherAsync result completes exceptionally after exceptional
2456 <     * completion of source
2244 <     */
2245 <    public void testAcceptEitherAsync2() {
2246 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2247 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248 <        IncAction r = new IncAction();
2249 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2250 <        f.completeExceptionally(new CFException());
2251 <        checkCompletedWithWrappedCFException(g);
2454 >        checkCompletedWithWrappedCFException(g, ex);
2455 >        checkCompletedNormally(f, v1);
2456 >    }}
2457  
2458 <        r = new IncAction();
2459 <        f = new CompletableFuture<>();
2460 <        f2 = new CompletableFuture<>();
2461 <        f2.completeExceptionally(new CFException());
2462 <        g = f.acceptEitherAsync(f2, r);
2463 <        f.complete(one);
2464 <        checkCompletedWithWrappedCFException(g);
2465 <    }
2458 >    public void testAcceptEither_exceptionalCompletion4() {
2459 >        for (ExecutionMode m : ExecutionMode.values())
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();
2465 >        final CFException ex = new CFException();
2466  
2467 <    /**
2468 <     * acceptEitherAsync result completes exceptionally if action does
2469 <     */
2265 <    public void testAcceptEitherAsync3() {
2266 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2267 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2268 <        FailingConsumer r = new FailingConsumer();
2269 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2270 <        f.complete(one);
2271 <        checkCompletedWithWrappedCFException(g);
2272 <    }
2467 >        f.completeExceptionally(ex);
2468 >        g.complete(v1);
2469 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2470  
2471 <    /**
2472 <     * acceptEitherAsync result completes exceptionally if either
2473 <     * source cancelled
2474 <     */
2475 <    public void testAcceptEitherAsync4() {
2476 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2477 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2478 <        IncAction r = new IncAction();
2479 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2480 <        assertTrue(f.cancel(true));
2284 <        checkCompletedWithWrappedCancellationException(g);
2471 >        // unspecified behavior
2472 >        Integer v;
2473 >        try {
2474 >            assertNull(h.join());
2475 >            assertEquals(1, r.invocationCount);
2476 >            assertEquals(inc(v1), r.value);
2477 >        } catch (CompletionException ok) {
2478 >            checkCompletedWithWrappedCFException(h, ex);
2479 >            assertEquals(0, r.invocationCount);
2480 >        }
2481  
2482 <        r = new IncAction();
2483 <        f = new CompletableFuture<>();
2484 <        f2 = new CompletableFuture<>();
2289 <        assertTrue(f2.cancel(true));
2290 <        g = f.acceptEitherAsync(f2, r);
2291 <        checkCompletedWithWrappedCancellationException(g);
2292 <    }
2482 >        checkCompletedWithWrappedCFException(f, ex);
2483 >        checkCompletedNormally(g, v1);
2484 >    }}
2485  
2486      /**
2487 <     * runAfterEitherAsync result completes normally after normal
2296 <     * completion of sources
2487 >     * acceptEither result completes exceptionally if action does
2488       */
2489 <    public void testRunAfterEitherAsync() {
2490 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2491 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2492 <        Noop r = new Noop();
2493 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2494 <        f.complete(one);
2495 <        checkCompletedNormally(g, null);
2496 <        assertTrue(r.ran);
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 >    {
2494 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2495 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2496 >        final FailingConsumer r = new FailingConsumer();
2497 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2498  
2499 <        r = new Noop();
2500 <        f = new CompletableFuture<>();
2501 <        f.complete(one);
2502 <        f2 = new CompletableFuture<>();
2503 <        g = f.runAfterEitherAsync(f2, r);
2504 <        checkCompletedNormally(g, null);
2313 <        assertTrue(r.ran);
2314 <    }
2499 >        f.complete(v1);
2500 >        checkCompletedWithWrappedCFException(h);
2501 >        g.complete(v2);
2502 >        checkCompletedNormally(f, v1);
2503 >        checkCompletedNormally(g, v2);
2504 >    }}
2505  
2506 <    /**
2507 <     * runAfterEitherAsync result completes exceptionally after exceptional
2508 <     * completion of source
2509 <     */
2510 <    public void testRunAfterEitherAsync2() {
2511 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2512 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2513 <        Noop r = new Noop();
2514 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2325 <        f.completeExceptionally(new CFException());
2326 <        checkCompletedWithWrappedCFException(g);
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 >    {
2511 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2513 >        final FailingConsumer r = new FailingConsumer();
2514 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2515  
2516 <        r = new Noop();
2517 <        f = new CompletableFuture<>();
2518 <        f2 = new CompletableFuture<>();
2519 <        f2.completeExceptionally(new CFException());
2520 <        g = f.runAfterEitherAsync(f2, r);
2521 <        f.complete(one);
2334 <        checkCompletedWithWrappedCFException(g);
2335 <    }
2516 >        g.complete(v2);
2517 >        checkCompletedWithWrappedCFException(h);
2518 >        f.complete(v1);
2519 >        checkCompletedNormally(f, v1);
2520 >        checkCompletedNormally(g, v2);
2521 >    }}
2522  
2523      /**
2524 <     * runAfterEitherAsync result completes exceptionally if action does
2524 >     * acceptEither result completes exceptionally if either source cancelled
2525       */
2526 <    public void testRunAfterEitherAsync3() {
2527 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2528 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2529 <        FailingNoop r = new FailingNoop();
2530 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2531 <        f.complete(one);
2532 <        checkCompletedWithWrappedCFException(g);
2533 <    }
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 >    {
2531 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2532 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2533 >        final IncAction r = new IncAction();
2534 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2535  
2536 <    /**
2537 <     * runAfterEitherAsync result completes exceptionally if either
2538 <     * source cancelled
2352 <     */
2353 <    public void testRunAfterEitherAsync4() {
2354 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2355 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356 <        Noop r = new Noop();
2357 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2358 <        assertTrue(f.cancel(true));
2359 <        checkCompletedWithWrappedCancellationException(g);
2536 >        assertTrue(f.cancel(mayInterruptIfRunning));
2537 >        checkCompletedWithWrappedCancellationException(h);
2538 >        g.complete(v1);
2539  
2540 <        r = new Noop();
2541 <        f = new CompletableFuture<>();
2542 <        f2 = new CompletableFuture<>();
2543 <        assertTrue(f2.cancel(true));
2544 <        g = f.runAfterEitherAsync(f2, r);
2366 <        checkCompletedWithWrappedCancellationException(g);
2367 <    }
2540 >        checkCancelled(f);
2541 >        assertEquals(0, r.invocationCount);
2542 >        checkCompletedNormally(g, v1);
2543 >        checkCompletedWithWrappedCancellationException(h);
2544 >    }}
2545  
2546 <    /**
2547 <     * thenComposeAsync result completes normally after normal
2548 <     * completion of source
2549 <     */
2550 <    public void testThenComposeAsync() {
2551 <        CompletableFuture<Integer> f, g;
2552 <        CompletableFutureInc r;
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 >    {
2551 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2552 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2553 >        final IncAction r = new IncAction();
2554 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2555  
2556 <        f = new CompletableFuture<>();
2557 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2558 <        f.complete(one);
2380 <        checkCompletedNormally(g, two);
2556 >        assertTrue(g.cancel(mayInterruptIfRunning));
2557 >        checkCompletedWithWrappedCancellationException(h);
2558 >        f.complete(v1);
2559  
2560 <        f = new CompletableFuture<>();
2561 <        f.complete(one);
2562 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2563 <        checkCompletedNormally(g, two);
2564 <    }
2560 >        checkCancelled(g);
2561 >        assertEquals(0, r.invocationCount);
2562 >        checkCompletedNormally(f, v1);
2563 >        checkCompletedWithWrappedCancellationException(h);
2564 >    }}
2565  
2566 <    /**
2567 <     * thenComposeAsync result completes exceptionally after
2568 <     * exceptional completion of source
2569 <     */
2570 <    public void testThenComposeAsync2() {
2571 <        CompletableFuture<Integer> f, g;
2572 <        CompletableFutureInc r;
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 >    {
2571 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2572 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2573 >        final IncAction r = new IncAction();
2574  
2575 <        f = new CompletableFuture<>();
2576 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2577 <        f.completeExceptionally(new CFException());
2399 <        checkCompletedWithWrappedCFException(g);
2400 <        assertFalse(r.ran);
2575 >        assertTrue(g.cancel(mayInterruptIfRunning));
2576 >        f.complete(v1);
2577 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2578  
2579 <        f = new CompletableFuture<>();
2580 <        f.completeExceptionally(new CFException());
2581 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2582 <        checkCompletedWithWrappedCFException(g);
2583 <        assertFalse(r.ran);
2584 <    }
2579 >        // unspecified behavior
2580 >        Integer v;
2581 >        try {
2582 >            assertNull(h.join());
2583 >            assertEquals(1, r.invocationCount);
2584 >            assertEquals(inc(v1), r.value);
2585 >        } catch (CompletionException ok) {
2586 >            checkCompletedWithWrappedCancellationException(h);
2587 >            assertEquals(0, r.invocationCount);
2588 >        }
2589  
2590 <    /**
2591 <     * thenComposeAsync result completes exceptionally if action does
2592 <     */
2412 <    public void testThenComposeAsync3() {
2413 <        CompletableFuture<Integer> f, g;
2414 <        FailingCompletableFutureFunction r;
2590 >        checkCancelled(g);
2591 >        checkCompletedNormally(f, v1);
2592 >    }}
2593  
2594 <        f = new CompletableFuture<>();
2595 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2596 <        f.complete(one);
2597 <        checkCompletedWithWrappedCFException(g);
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 >    {
2599 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2600 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2601 >        final IncAction r = new IncAction();
2602  
2603 <        f = new CompletableFuture<>();
2604 <        f.complete(one);
2605 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2606 <        checkCompletedWithWrappedCFException(g);
2607 <    }
2603 >        assertTrue(f.cancel(mayInterruptIfRunning));
2604 >        g.complete(v1);
2605 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2606 >
2607 >        // unspecified behavior
2608 >        Integer v;
2609 >        try {
2610 >            assertNull(h.join());
2611 >            assertEquals(1, r.invocationCount);
2612 >            assertEquals(inc(v1), r.value);
2613 >        } catch (CompletionException ok) {
2614 >            checkCompletedWithWrappedCancellationException(h);
2615 >            assertEquals(0, r.invocationCount);
2616 >        }
2617 >
2618 >        checkCancelled(f);
2619 >        checkCompletedNormally(g, v1);
2620 >    }}
2621  
2622      /**
2623 <     * thenComposeAsync result completes exceptionally if source cancelled
2623 >     * runAfterEither result completes normally after normal completion
2624 >     * of either source
2625       */
2626 <    public void testThenComposeAsync4() {
2627 <        CompletableFuture<Integer> f, g;
2628 <        CompletableFutureInc r;
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 >    {
2631 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2632 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2633 >        final Noop r = new Noop();
2634 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2635  
2636 <        f = new CompletableFuture<>();
2637 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2638 <        assertTrue(f.cancel(true));
2639 <        checkCompletedWithWrappedCancellationException(g);
2636 >        f.complete(v1);
2637 >        checkCompletedNormally(h, null);
2638 >        assertEquals(1, r.invocationCount);
2639 >        g.complete(v2);
2640  
2641 <        f = new CompletableFuture<>();
2642 <        assertTrue(f.cancel(true));
2643 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2644 <        checkCompletedWithWrappedCancellationException(g);
2645 <    }
2641 >        checkCompletedNormally(f, v1);
2642 >        checkCompletedNormally(g, v2);
2643 >        checkCompletedNormally(h, null);
2644 >        assertEquals(1, r.invocationCount);
2645 >    }}
2646  
2647 <    // async with explicit executors
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 >    {
2652 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2653 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2654 >        final Noop r = new Noop();
2655 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2656  
2657 <    /**
2658 <     * thenRunAsync result completes normally after normal completion of source
2659 <     */
2660 <    public void testThenRunAsyncE() {
2451 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2452 <        Noop r = new Noop();
2453 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2454 <        f.complete(null);
2455 <        checkCompletedNormally(g, null);
2657 >        g.complete(v2);
2658 >        checkCompletedNormally(h, null);
2659 >        assertEquals(1, r.invocationCount);
2660 >        f.complete(v1);
2661  
2662 <        // reordered version
2663 <        f = new CompletableFuture<>();
2664 <        f.complete(null);
2665 <        r = new Noop();
2666 <        g = f.thenRunAsync(r, new ThreadExecutor());
2462 <        checkCompletedNormally(g, null);
2463 <    }
2662 >        checkCompletedNormally(f, v1);
2663 >        checkCompletedNormally(g, v2);
2664 >        checkCompletedNormally(h, null);
2665 >        assertEquals(1, r.invocationCount);
2666 >        }}
2667  
2668 <    /**
2669 <     * thenRunAsync result completes exceptionally after exceptional
2670 <     * completion of source
2671 <     */
2672 <    public void testThenRunAsync2E() {
2673 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2674 <        Noop r = new Noop();
2675 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2473 <        f.completeExceptionally(new CFException());
2474 <        try {
2475 <            g.join();
2476 <            shouldThrow();
2477 <        } catch (CompletionException success) {}
2478 <        checkCompletedWithWrappedCFException(g);
2479 <    }
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 >    {
2673 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2674 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2675 >        final Noop r = new Noop();
2676  
2677 <    /**
2678 <     * thenRunAsync result completes exceptionally if action does
2679 <     */
2484 <    public void testThenRunAsync3E() {
2485 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2486 <        FailingNoop r = new FailingNoop();
2487 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2488 <        f.complete(null);
2489 <        checkCompletedWithWrappedCFException(g);
2490 <    }
2677 >        f.complete(v1);
2678 >        g.complete(v2);
2679 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2680  
2681 <    /**
2682 <     * thenRunAsync result completes exceptionally if source cancelled
2683 <     */
2684 <    public void testThenRunAsync4E() {
2685 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2497 <        Noop r = new Noop();
2498 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2499 <        assertTrue(f.cancel(true));
2500 <        checkCompletedWithWrappedCancellationException(g);
2501 <    }
2681 >        checkCompletedNormally(h, null);
2682 >        checkCompletedNormally(f, v1);
2683 >        checkCompletedNormally(g, v2);
2684 >        assertEquals(1, r.invocationCount);
2685 >    }}
2686  
2687      /**
2688 <     * thenApplyAsync result completes normally after normal completion of source
2688 >     * runAfterEither result completes exceptionally after exceptional
2689 >     * completion of either source
2690       */
2691 <    public void testThenApplyAsyncE() {
2692 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2693 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2694 <        f.complete(one);
2695 <        checkCompletedNormally(g, two);
2696 <    }
2691 >    public void testRunAfterEither_exceptionalCompletion1() {
2692 >        for (ExecutionMode m : ExecutionMode.values())
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();
2698 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2699 >        final CFException ex = new CFException();
2700  
2701 <    /**
2702 <     * thenApplyAsync result completes exceptionally after exceptional
2703 <     * completion of source
2516 <     */
2517 <    public void testThenApplyAsync2E() {
2518 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2519 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2520 <        f.completeExceptionally(new CFException());
2521 <        checkCompletedWithWrappedCFException(g);
2522 <    }
2701 >        f.completeExceptionally(ex);
2702 >        checkCompletedWithWrappedCFException(h, ex);
2703 >        g.complete(v1);
2704  
2705 <    /**
2706 <     * thenApplyAsync result completes exceptionally if action does
2707 <     */
2708 <    public void testThenApplyAsync3E() {
2709 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2529 <        FailingFunction r = new FailingFunction();
2530 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2531 <        f.complete(null);
2532 <        checkCompletedWithWrappedCFException(g);
2533 <    }
2705 >        assertEquals(0, r.invocationCount);
2706 >        checkCompletedNormally(g, v1);
2707 >        checkCompletedWithWrappedCFException(f, ex);
2708 >        checkCompletedWithWrappedCFException(h, ex);
2709 >    }}
2710  
2711 <    /**
2712 <     * thenApplyAsync result completes exceptionally if source cancelled
2713 <     */
2714 <    public void testThenApplyAsync4E() {
2715 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2716 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2717 <        assertTrue(f.cancel(true));
2718 <        checkCompletedWithWrappedCancellationException(g);
2719 <    }
2711 >    public void testRunAfterEither_exceptionalCompletion2() {
2712 >        for (ExecutionMode m : ExecutionMode.values())
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();
2718 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2719 >        final CFException ex = new CFException();
2720  
2721 <    /**
2722 <     * thenAcceptAsync result completes normally after normal
2723 <     * completion of source
2548 <     */
2549 <    public void testThenAcceptAsyncE() {
2550 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2551 <        IncAction r = new IncAction();
2552 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2553 <        f.complete(one);
2554 <        checkCompletedNormally(g, null);
2555 <        assertEquals(r.value, 2);
2556 <    }
2721 >        g.completeExceptionally(ex);
2722 >        checkCompletedWithWrappedCFException(h, ex);
2723 >        f.complete(v1);
2724  
2725 <    /**
2726 <     * thenAcceptAsync result completes exceptionally after exceptional
2727 <     * completion of source
2728 <     */
2729 <    public void testThenAcceptAsync2E() {
2563 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2564 <        IncAction r = new IncAction();
2565 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2566 <        f.completeExceptionally(new CFException());
2567 <        checkCompletedWithWrappedCFException(g);
2568 <    }
2725 >        assertEquals(0, r.invocationCount);
2726 >        checkCompletedNormally(f, v1);
2727 >        checkCompletedWithWrappedCFException(g, ex);
2728 >        checkCompletedWithWrappedCFException(h, ex);
2729 >    }}
2730  
2731 <    /**
2732 <     * thenAcceptAsync result completes exceptionally if action does
2733 <     */
2734 <    public void testThenAcceptAsync3E() {
2735 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2736 <        FailingConsumer r = new FailingConsumer();
2737 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2738 <        f.complete(null);
2578 <        checkCompletedWithWrappedCFException(g);
2579 <    }
2731 >    public void testRunAfterEither_exceptionalCompletion3() {
2732 >        for (ExecutionMode m : ExecutionMode.values())
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();
2738 >        final CFException ex = new CFException();
2739  
2740 <    /**
2741 <     * thenAcceptAsync result completes exceptionally if source cancelled
2742 <     */
2584 <    public void testThenAcceptAsync4E() {
2585 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2586 <        IncAction r = new IncAction();
2587 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2588 <        assertTrue(f.cancel(true));
2589 <        checkCompletedWithWrappedCancellationException(g);
2590 <    }
2740 >        g.completeExceptionally(ex);
2741 >        f.complete(v1);
2742 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2743  
2744 <    /**
2745 <     * applyToEitherAsync result completes normally after normal
2746 <     * completion of sources
2747 <     */
2748 <    public void testApplyToEitherAsyncE() {
2749 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2750 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2751 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2752 <        f.complete(one);
2601 <        checkCompletedNormally(g, two);
2744 >        // unspecified behavior
2745 >        Integer v;
2746 >        try {
2747 >            assertNull(h.join());
2748 >            assertEquals(1, r.invocationCount);
2749 >        } catch (CompletionException ok) {
2750 >            checkCompletedWithWrappedCFException(h, ex);
2751 >            assertEquals(0, r.invocationCount);
2752 >        }
2753  
2754 <        f = new CompletableFuture<>();
2755 <        f.complete(one);
2756 <        f2 = new CompletableFuture<>();
2606 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2607 <        checkCompletedNormally(g, two);
2608 <    }
2754 >        checkCompletedWithWrappedCFException(g, ex);
2755 >        checkCompletedNormally(f, v1);
2756 >    }}
2757  
2758 <    /**
2759 <     * applyToEitherAsync result completes exceptionally after exceptional
2760 <     * completion of source
2761 <     */
2762 <    public void testApplyToEitherAsync2E() {
2763 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2764 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2765 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618 <        f.completeExceptionally(new CFException());
2619 <        checkCompletedWithWrappedCFException(g);
2758 >    public void testRunAfterEither_exceptionalCompletion4() {
2759 >        for (ExecutionMode m : ExecutionMode.values())
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();
2765 >        final CFException ex = new CFException();
2766  
2767 <        f = new CompletableFuture<>();
2768 <        f2 = new CompletableFuture<>();
2769 <        f2.completeExceptionally(new CFException());
2624 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625 <        f.complete(one);
2626 <        checkCompletedWithWrappedCFException(g);
2627 <    }
2767 >        f.completeExceptionally(ex);
2768 >        g.complete(v1);
2769 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2770  
2771 <    /**
2772 <     * applyToEitherAsync result completes exceptionally if action does
2773 <     */
2774 <    public void testApplyToEitherAsync3E() {
2775 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2776 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2777 <        FailingFunction r = new FailingFunction();
2778 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2779 <        f.complete(one);
2780 <        checkCompletedWithWrappedCFException(g);
2781 <    }
2771 >        // unspecified behavior
2772 >        Integer v;
2773 >        try {
2774 >            assertNull(h.join());
2775 >            assertEquals(1, r.invocationCount);
2776 >        } catch (CompletionException ok) {
2777 >            checkCompletedWithWrappedCFException(h, ex);
2778 >            assertEquals(0, r.invocationCount);
2779 >        }
2780 >
2781 >        checkCompletedWithWrappedCFException(f, ex);
2782 >        checkCompletedNormally(g, v1);
2783 >    }}
2784  
2785      /**
2786 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2786 >     * runAfterEither result completes exceptionally if action does
2787       */
2788 <    public void testApplyToEitherAsync4E() {
2789 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2790 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2791 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2792 <        assertTrue(f.cancel(true));
2793 <        checkCompletedWithWrappedCancellationException(g);
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 >    {
2793 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2794 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2795 >        final FailingRunnable r = new FailingRunnable();
2796 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2797  
2798 <        f = new CompletableFuture<>();
2799 <        f2 = new CompletableFuture<>();
2800 <        assertTrue(f2.cancel(true));
2801 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2802 <        checkCompletedWithWrappedCancellationException(g);
2803 <    }
2798 >        f.complete(v1);
2799 >        checkCompletedWithWrappedCFException(h);
2800 >        g.complete(v2);
2801 >        checkCompletedNormally(f, v1);
2802 >        checkCompletedNormally(g, v2);
2803 >    }}
2804  
2805 <    /**
2806 <     * acceptEitherAsync result completes normally after normal
2807 <     * completion of sources
2808 <     */
2809 <    public void testAcceptEitherAsyncE() {
2810 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2811 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2812 <        IncAction r = new IncAction();
2813 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2667 <        f.complete(one);
2668 <        checkCompletedNormally(g, null);
2669 <        assertEquals(r.value, 2);
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 >    {
2810 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2811 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2812 >        final FailingRunnable r = new FailingRunnable();
2813 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2814  
2815 <        r = new IncAction();
2816 <        f = new CompletableFuture<>();
2817 <        f.complete(one);
2818 <        f2 = new CompletableFuture<>();
2819 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2820 <        checkCompletedNormally(g, null);
2677 <        assertEquals(r.value, 2);
2678 <    }
2815 >        g.complete(v2);
2816 >        checkCompletedWithWrappedCFException(h);
2817 >        f.complete(v1);
2818 >        checkCompletedNormally(f, v1);
2819 >        checkCompletedNormally(g, v2);
2820 >    }}
2821  
2822      /**
2823 <     * acceptEitherAsync result completes exceptionally after exceptional
2682 <     * completion of source
2823 >     * runAfterEither result completes exceptionally if either source cancelled
2824       */
2825 <    public void testAcceptEitherAsync2E() {
2826 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2827 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2828 <        IncAction r = new IncAction();
2829 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2830 <        f.completeExceptionally(new CFException());
2831 <        checkCompletedWithWrappedCFException(g);
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 >    {
2830 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2831 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2832 >        final Noop r = new Noop();
2833 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2834  
2835 <        r = new IncAction();
2836 <        f = new CompletableFuture<>();
2837 <        f2 = new CompletableFuture<>();
2695 <        f2.completeExceptionally(new CFException());
2696 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2697 <        f.complete(one);
2698 <        checkCompletedWithWrappedCFException(g);
2699 <    }
2835 >        assertTrue(f.cancel(mayInterruptIfRunning));
2836 >        checkCompletedWithWrappedCancellationException(h);
2837 >        g.complete(v1);
2838  
2839 <    /**
2840 <     * acceptEitherAsync result completes exceptionally if action does
2841 <     */
2842 <    public void testAcceptEitherAsync3E() {
2843 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2706 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2707 <        FailingConsumer r = new FailingConsumer();
2708 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2709 <        f.complete(one);
2710 <        checkCompletedWithWrappedCFException(g);
2711 <    }
2839 >        checkCancelled(f);
2840 >        assertEquals(0, r.invocationCount);
2841 >        checkCompletedNormally(g, v1);
2842 >        checkCompletedWithWrappedCancellationException(h);
2843 >    }}
2844  
2845 <    /**
2846 <     * acceptEitherAsync result completes exceptionally if either
2847 <     * source cancelled
2848 <     */
2849 <    public void testAcceptEitherAsync4E() {
2850 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2851 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2852 <        IncAction r = new IncAction();
2853 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722 <        assertTrue(f.cancel(true));
2723 <        checkCompletedWithWrappedCancellationException(g);
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 >    {
2850 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2851 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2852 >        final Noop r = new Noop();
2853 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2854  
2855 <        r = new IncAction();
2856 <        f = new CompletableFuture<>();
2857 <        f2 = new CompletableFuture<>();
2728 <        assertTrue(f2.cancel(true));
2729 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2730 <        checkCompletedWithWrappedCancellationException(g);
2731 <    }
2855 >        assertTrue(g.cancel(mayInterruptIfRunning));
2856 >        checkCompletedWithWrappedCancellationException(h);
2857 >        f.complete(v1);
2858  
2859 <    /**
2860 <     * runAfterEitherAsync result completes normally after normal
2861 <     * completion of sources
2862 <     */
2863 <    public void testRunAfterEitherAsyncE() {
2738 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2739 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740 <        Noop r = new Noop();
2741 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2742 <        f.complete(one);
2743 <        checkCompletedNormally(g, null);
2744 <        assertTrue(r.ran);
2859 >        checkCancelled(g);
2860 >        assertEquals(0, r.invocationCount);
2861 >        checkCompletedNormally(f, v1);
2862 >        checkCompletedWithWrappedCancellationException(h);
2863 >    }}
2864  
2865 <        r = new Noop();
2866 <        f = new CompletableFuture<>();
2867 <        f.complete(one);
2868 <        f2 = new CompletableFuture<>();
2869 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2870 <        checkCompletedNormally(g, null);
2871 <        assertTrue(r.ran);
2872 <    }
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 >    {
2870 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2871 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2872 >        final Noop r = new Noop();
2873  
2874 <    /**
2875 <     * runAfterEitherAsync result completes exceptionally after exceptional
2876 <     * completion of source
2758 <     */
2759 <    public void testRunAfterEitherAsync2E() {
2760 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2761 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2762 <        Noop r = new Noop();
2763 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2764 <        f.completeExceptionally(new CFException());
2765 <        checkCompletedWithWrappedCFException(g);
2874 >        assertTrue(g.cancel(mayInterruptIfRunning));
2875 >        f.complete(v1);
2876 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2877  
2878 <        r = new Noop();
2879 <        f = new CompletableFuture<>();
2880 <        f2 = new CompletableFuture<>();
2881 <        f2.completeExceptionally(new CFException());
2882 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2883 <        f.complete(one);
2884 <        checkCompletedWithWrappedCFException(g);
2885 <    }
2878 >        // unspecified behavior
2879 >        Integer v;
2880 >        try {
2881 >            assertNull(h.join());
2882 >            assertEquals(1, r.invocationCount);
2883 >        } catch (CompletionException ok) {
2884 >            checkCompletedWithWrappedCancellationException(h);
2885 >            assertEquals(0, r.invocationCount);
2886 >        }
2887  
2888 <    /**
2889 <     * runAfterEitherAsync result completes exceptionally if action does
2890 <     */
2891 <    public void testRunAfterEitherAsync3E() {
2892 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2893 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2894 <        FailingNoop r = new FailingNoop();
2895 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2896 <        f.complete(one);
2897 <        checkCompletedWithWrappedCFException(g);
2898 <    }
2888 >        checkCancelled(g);
2889 >        checkCompletedNormally(f, v1);
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 >    {
2897 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2898 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2899 >        final Noop r = new Noop();
2900 >
2901 >        assertTrue(f.cancel(mayInterruptIfRunning));
2902 >        g.complete(v1);
2903 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2904 >
2905 >        // unspecified behavior
2906 >        Integer v;
2907 >        try {
2908 >            assertNull(h.join());
2909 >            assertEquals(1, r.invocationCount);
2910 >        } catch (CompletionException ok) {
2911 >            checkCompletedWithWrappedCancellationException(h);
2912 >            assertEquals(0, r.invocationCount);
2913 >        }
2914 >
2915 >        checkCancelled(f);
2916 >        checkCompletedNormally(g, v1);
2917 >    }}
2918  
2919      /**
2920 <     * runAfterEitherAsync result completes exceptionally if either
2790 <     * source cancelled
2920 >     * thenCompose result completes normally after normal completion of source
2921       */
2922 <    public void testRunAfterEitherAsync4E() {
2923 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2924 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2925 <        Noop r = new Noop();
2926 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2927 <        assertTrue(f.cancel(true));
2928 <        checkCompletedWithWrappedCancellationException(g);
2922 >    public void testThenCompose_normalCompletion() {
2923 >        for (ExecutionMode m : ExecutionMode.values())
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 >        if (createIncomplete) f.complete(v1);
2932  
2933 <        r = new Noop();
2934 <        f = new CompletableFuture<>();
2935 <        f2 = new CompletableFuture<>();
2936 <        assertTrue(f2.cancel(true));
2804 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2805 <        checkCompletedWithWrappedCancellationException(g);
2806 <    }
2933 >        checkCompletedNormally(g, inc(v1));
2934 >        checkCompletedNormally(f, v1);
2935 >        assertEquals(1, r.invocationCount);
2936 >    }}
2937  
2938      /**
2939 <     * thenComposeAsync result completes normally after normal
2939 >     * thenCompose result completes exceptionally after exceptional
2940       * completion of source
2941       */
2942 <    public void testThenComposeAsyncE() {
2943 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2944 <        CompletableFutureInc r = new CompletableFutureInc();
2945 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2946 <        f.complete(one);
2947 <        checkCompletedNormally(g, two);
2948 <    }
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 >        if (createIncomplete) f.completeExceptionally(ex);
2952  
2953 <    /**
2954 <     * thenComposeAsync result completes exceptionally after
2955 <     * exceptional completion of source
2956 <     */
2824 <    public void testThenComposeAsync2E() {
2825 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2826 <        CompletableFutureInc r = new CompletableFutureInc();
2827 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2828 <        f.completeExceptionally(new CFException());
2829 <        checkCompletedWithWrappedCFException(g);
2830 <    }
2953 >        checkCompletedWithWrappedCFException(g, ex);
2954 >        checkCompletedWithWrappedCFException(f, ex);
2955 >        assertEquals(0, r.invocationCount);
2956 >    }}
2957  
2958      /**
2959 <     * thenComposeAsync result completes exceptionally if action does
2959 >     * thenCompose result completes exceptionally if action does
2960       */
2961 <    public void testThenComposeAsync3E() {
2962 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2963 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2964 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2965 <        f.complete(one);
2961 >    public void testThenCompose_actionFailed() {
2962 >        for (ExecutionMode m : ExecutionMode.values())
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 >        if (createIncomplete) f.complete(v1);
2972 >
2973          checkCompletedWithWrappedCFException(g);
2974 <    }
2974 >        checkCompletedNormally(f, v1);
2975 >    }}
2976  
2977      /**
2978 <     * thenComposeAsync result completes exceptionally if source cancelled
2978 >     * thenCompose result completes exceptionally if source cancelled
2979       */
2980 <    public void testThenComposeAsync4E() {
2981 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2982 <        CompletableFutureInc r = new CompletableFutureInc();
2983 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2984 <        assertTrue(f.cancel(true));
2980 >    public void testThenCompose_sourceCancelled() {
2981 >        for (ExecutionMode m : ExecutionMode.values())
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 >        if (createIncomplete) {
2990 >            checkIncomplete(g);
2991 >            assertTrue(f.cancel(mayInterruptIfRunning));
2992 >        }
2993 >
2994          checkCompletedWithWrappedCancellationException(g);
2995 <    }
2995 >        checkCancelled(f);
2996 >    }}
2997  
2998      // other static methods
2999  
# Line 3049 | Line 3193 | public class CompletableFutureTest exten
3193       * whenComplete action executes on normal completion, propagating
3194       * source result.
3195       */
3196 <    public void testWhenComplete1() {
3197 <        final AtomicInteger a = new AtomicInteger();
3198 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3199 <        CompletableFuture<Integer> g =
3200 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3201 <        f.complete(three);
3202 <        checkCompletedNormally(f, three);
3203 <        checkCompletedNormally(g, three);
3204 <        assertEquals(a.get(), 1);
3205 <    }
3206 <
3207 <    /**
3208 <     * whenComplete action executes on exceptional completion, propagating
3209 <     * source result.
3210 <     */
3211 <    public void testWhenComplete2() {
3068 <        final AtomicInteger a = new AtomicInteger();
3069 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3070 <        CompletableFuture<Integer> g =
3071 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3072 <        f.completeExceptionally(new CFException());
3073 <        assertTrue(f.isCompletedExceptionally());
3074 <        assertTrue(g.isCompletedExceptionally());
3075 <        assertEquals(a.get(), 1);
3076 <    }
3077 <
3078 <    /**
3079 <     * If a whenComplete action throws an exception when triggered by
3080 <     * a normal completion, it completes exceptionally
3081 <     */
3082 <    public void testWhenComplete3() {
3083 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3084 <        CompletableFuture<Integer> g =
3085 <            f.whenComplete((Integer x, Throwable t) ->
3086 <                           { throw new CFException(); } );
3087 <        f.complete(three);
3088 <        checkCompletedNormally(f, three);
3089 <        assertTrue(g.isCompletedExceptionally());
3090 <        checkCompletedWithWrappedCFException(g);
3091 <    }
3092 <
3093 <    /**
3094 <     * whenCompleteAsync action executes on normal completion, propagating
3095 <     * source result.
3096 <     */
3097 <    public void testWhenCompleteAsync1() {
3098 <        final AtomicInteger a = new AtomicInteger();
3099 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3100 <        CompletableFuture<Integer> g =
3101 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3102 <        f.complete(three);
3103 <        checkCompletedNormally(f, three);
3104 <        checkCompletedNormally(g, three);
3105 <        assertEquals(a.get(), 1);
3106 <    }
3107 <
3108 <    /**
3109 <     * whenCompleteAsync action executes on exceptional completion, propagating
3110 <     * source result.
3111 <     */
3112 <    public void testWhenCompleteAsync2() {
3113 <        final AtomicInteger a = new AtomicInteger();
3114 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3115 <        CompletableFuture<Integer> g =
3116 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3117 <        f.completeExceptionally(new CFException());
3118 <        checkCompletedWithWrappedCFException(f);
3119 <        checkCompletedWithWrappedCFException(g);
3120 <    }
3196 >    public void testWhenComplete_normalCompletion1() {
3197 >        for (ExecutionMode m : ExecutionMode.values())
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 = 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  
3213 <    /**
3214 <     * If a whenCompleteAsync action throws an exception when
3215 <     * triggered by a normal completion, it completes exceptionally
3216 <     */
3126 <    public void testWhenCompleteAsync3() {
3127 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3128 <        CompletableFuture<Integer> g =
3129 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3130 <                           { throw new CFException(); } );
3131 <        f.complete(three);
3132 <        checkCompletedNormally(f, three);
3133 <        checkCompletedWithWrappedCFException(g);
3134 <    }
3213 >        checkCompletedNormally(g, v1);
3214 >        checkCompletedNormally(f, v1);
3215 >        assertEquals(1, a.get());
3216 >    }}
3217  
3218      /**
3219 <     * whenCompleteAsync action executes on normal completion, propagating
3219 >     * whenComplete action executes on exceptional completion, propagating
3220       * source result.
3221       */
3222 <    public void testWhenCompleteAsync1e() {
3223 <        final AtomicInteger a = new AtomicInteger();
3224 <        ThreadExecutor exec = new ThreadExecutor();
3225 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3226 <        CompletableFuture<Integer> g =
3227 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3228 <                                exec);
3229 <        f.complete(three);
3230 <        checkCompletedNormally(f, three);
3231 <        checkCompletedNormally(g, three);
3232 <        assertEquals(a.get(), 1);
3233 <    }
3222 >    public void testWhenComplete_exceptionalCompletion() {
3223 >        for (ExecutionMode m : ExecutionMode.values())
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);
3231 >        final CompletableFuture<Integer> g = m.whenComplete
3232 >            (f,
3233 >             (Integer x, Throwable t) -> {
3234 >                threadAssertNull(x);
3235 >                threadAssertSame(t, ex);
3236 >                a.getAndIncrement();
3237 >            });
3238 >        if (createIncomplete) f.completeExceptionally(ex);
3239 >        checkCompletedWithWrappedCFException(f, ex);
3240 >        checkCompletedWithWrappedCFException(g, ex);
3241 >        assertEquals(1, a.get());
3242 >    }}
3243  
3244      /**
3245 <     * whenCompleteAsync action executes on exceptional completion, propagating
3246 <     * source result.
3245 >     * whenComplete action executes on cancelled source, propagating
3246 >     * CancellationException.
3247       */
3248 <    public void testWhenCompleteAsync2e() {
3249 <        final AtomicInteger a = new AtomicInteger();
3250 <        ThreadExecutor exec = new ThreadExecutor();
3251 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3252 <        CompletableFuture<Integer> g =
3253 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3254 <                                exec);
3255 <        f.completeExceptionally(new CFException());
3256 <        checkCompletedWithWrappedCFException(f);
3257 <        checkCompletedWithWrappedCFException(g);
3258 <    }
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 <    /**
3266 <     * If a whenCompleteAsync action throws an exception when triggered
3267 <     * by a normal completion, it completes exceptionally
3268 <     */
3269 <    public void testWhenCompleteAsync3e() {
3174 <        ThreadExecutor exec = new ThreadExecutor();
3175 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3176 <        CompletableFuture<Integer> g =
3177 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3178 <                                { throw new CFException(); },
3179 <                                exec);
3180 <        f.complete(three);
3181 <        checkCompletedNormally(f, three);
3182 <        checkCompletedWithWrappedCFException(g);
3183 <    }
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 <     * handleAsync action completes normally with function value on
3273 <     * either normal or exceptional completion of source
3272 >     * If a whenComplete action throws an exception when triggered by
3273 >     * a normal completion, it completes exceptionally
3274       */
3275 <    public void testHandleAsync() {
3276 <        CompletableFuture<Integer> f, g;
3277 <        IntegerHandler r;
3278 <
3279 <        f = new CompletableFuture<>();
3280 <        g = f.handleAsync(r = new IntegerHandler());
3281 <        assertFalse(r.ran);
3282 <        f.completeExceptionally(new CFException());
3283 <        checkCompletedWithWrappedCFException(f);
3284 <        checkCompletedNormally(g, three);
3285 <        assertTrue(r.ran);
3286 <
3287 <        f = new CompletableFuture<>();
3288 <        g = f.handleAsync(r = new IntegerHandler());
3289 <        assertFalse(r.ran);
3290 <        f.completeExceptionally(new CFException());
3291 <        checkCompletedWithWrappedCFException(f);
3292 <        checkCompletedNormally(g, three);
3293 <        assertTrue(r.ran);
3294 <
3295 <        f = new CompletableFuture<>();
3296 <        g = f.handleAsync(r = new IntegerHandler());
3211 <        assertFalse(r.ran);
3212 <        f.complete(one);
3213 <        checkCompletedNormally(f, one);
3214 <        checkCompletedNormally(g, two);
3215 <        assertTrue(r.ran);
3216 <
3217 <        f = new CompletableFuture<>();
3218 <        g = f.handleAsync(r = new IntegerHandler());
3219 <        assertFalse(r.ran);
3220 <        f.complete(one);
3221 <        checkCompletedNormally(f, one);
3222 <        checkCompletedNormally(g, two);
3223 <        assertTrue(r.ran);
3224 <    }
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 >    {
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);
3284 >        final CompletableFuture<Integer> g = m.whenComplete
3285 >            (f,
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 >        assertEquals(1, a.get());
3296 >    }}
3297  
3298      /**
3299 <     * handleAsync action with Executor completes normally with
3300 <     * function value on either normal or exceptional completion of
3301 <     * source
3299 >     * If a whenComplete action throws an exception when triggered by
3300 >     * a source completion that also throws an exception, the source
3301 >     * exception takes precedence.
3302       */
3303 <    public void testHandleAsync2() {
3304 <        CompletableFuture<Integer> f, g;
3305 <        ThreadExecutor exec = new ThreadExecutor();
3306 <        IntegerHandler r;
3307 <
3308 <        f = new CompletableFuture<>();
3309 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3310 <        assertFalse(r.ran);
3311 <        f.completeExceptionally(new CFException());
3312 <        checkCompletedWithWrappedCFException(f);
3313 <        checkCompletedNormally(g, three);
3314 <        assertTrue(r.ran);
3315 <
3316 <        f = new CompletableFuture<>();
3317 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3318 <        assertFalse(r.ran);
3319 <        f.completeExceptionally(new CFException());
3320 <        checkCompletedWithWrappedCFException(f);
3321 <        checkCompletedNormally(g, three);
3322 <        assertTrue(r.ran);
3323 <
3324 <        f = new CompletableFuture<>();
3325 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3326 <        assertFalse(r.ran);
3327 <        f.complete(one);
3256 <        checkCompletedNormally(f, one);
3257 <        checkCompletedNormally(g, two);
3258 <        assertTrue(r.ran);
3259 <
3260 <        f = new CompletableFuture<>();
3261 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3262 <        assertFalse(r.ran);
3263 <        f.complete(one);
3264 <        checkCompletedNormally(f, one);
3265 <        checkCompletedNormally(g, two);
3266 <        assertTrue(r.ran);
3267 <    }
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 >    {
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<>();
3312 >
3313 >        if (!createIncomplete) f.completeExceptionally(ex1);
3314 >        final CompletableFuture<Integer> g = m.whenComplete
3315 >            (f,
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 >        assertEquals(1, a.get());
3327 >    }}
3328  
3329   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines