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.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.55 by jsr166, Mon Jun 2 21:41:37 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   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 246 | 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 278 | Line 283 | public class CompletableFutureTest exten
283       */
284      public void testGetNumberOfDependents() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286 <        assertEquals(f.getNumberOfDependents(), 0);
287 <        CompletableFuture g = f.thenRun(new Noop());
288 <        assertEquals(f.getNumberOfDependents(), 1);
289 <        assertEquals(g.getNumberOfDependents(), 0);
290 <        CompletableFuture h = f.thenRun(new Noop());
291 <        assertEquals(f.getNumberOfDependents(), 2);
286 >        assertEquals(0, f.getNumberOfDependents());
287 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
288 >        assertEquals(1, f.getNumberOfDependents());
289 >        assertEquals(0, g.getNumberOfDependents());
290 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
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 317 | Line 322 | public class CompletableFutureTest exten
322  
323      // Choose non-commutative actions for better coverage
324  
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 =
340          (Integer x) -> Integer.valueOf(x.intValue() + 1);
341      static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
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 <        int value;
360 >        int invocationCount = 0;
361 >        Integer value;
362 >        // Check this action was invoked exactly once when result is computed.
363          public void accept(Integer x, Integer y) {
364 <            value = x.intValue() - y.intValue();
364 >            invocationCount++;
365 >            value = subtract(x, y);
366 >        }
367 >    }
368 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
369 >        int invocationCount = 0;
370 >        Integer value;
371 >        // Check this action was invoked exactly once when result is computed.
372 >        public Integer apply(Integer x, Integer y) {
373 >            invocationCount++;
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 >        final ExecutionMode m;
379 >        int invocationCount = 0;
380 >        Noop(ExecutionMode m) { this.m = m; }
381 >        public void run() {
382 >            m.checkExecutionMode();
383 >            invocationCount++;
384 >        }
385      }
386  
387      static final class FailingSupplier implements Supplier<Integer> {
388 <        boolean ran;
389 <        public Integer get() { ran = true; throw new CFException(); }
388 >        int invocationCount = 0;
389 >        public Integer get() {
390 >            invocationCount++;
391 >            throw new CFException();
392 >        }
393      }
394      static final class FailingConsumer implements Consumer<Integer> {
395 <        boolean ran;
396 <        public void accept(Integer x) { ran = true; throw new CFException(); }
395 >        int invocationCount = 0;
396 >        public void accept(Integer x) {
397 >            invocationCount++;
398 >            throw new CFException();
399 >        }
400      }
401      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
402 <        boolean ran;
403 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
402 >        int invocationCount = 0;
403 >        public void accept(Integer x, Integer y) {
404 >            invocationCount++;
405 >            throw new CFException();
406 >        }
407      }
408      static final class FailingFunction implements Function<Integer, Integer> {
409 <        boolean ran;
410 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
409 >        int invocationCount = 0;
410 >        public Integer apply(Integer x) {
411 >            invocationCount++;
412 >            throw new CFException();
413 >        }
414      }
415      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
416 <        boolean ran;
417 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
416 >        int invocationCount = 0;
417 >        public Integer apply(Integer x, Integer y) {
418 >            invocationCount++;
419 >            throw new CFException();
420 >        }
421      }
422 <    static final class FailingNoop implements Runnable {
423 <        boolean ran;
424 <        public void run() { ran = true; throw new CFException(); }
422 >    static final class FailingRunnable implements Runnable {
423 >        int invocationCount = 0;
424 >        public void run() {
425 >            invocationCount++;
426 >            throw new CFException();
427 >        }
428      }
429  
430      static final class CompletableFutureInc
431          implements Function<Integer, CompletableFuture<Integer>> {
432 <        boolean ran;
432 >        int invocationCount = 0;
433          public CompletableFuture<Integer> apply(Integer x) {
434 <            ran = true;
434 >            invocationCount++;
435              CompletableFuture<Integer> f = new CompletableFuture<>();
436 <            f.complete(Integer.valueOf(x.intValue() + 1));
436 >            f.complete(inc(x));
437              return f;
438          }
439      }
440  
441      static final class FailingCompletableFutureFunction
442          implements Function<Integer, CompletableFuture<Integer>> {
443 <        boolean ran;
443 >        int invocationCount = 0;
444          public CompletableFuture<Integer> apply(Integer x) {
445 <            ran = true; throw new CFException();
445 >            invocationCount++;
446 >            throw new CFException();
447          }
448      }
449  
# Line 392 | Line 457 | public class CompletableFutureTest exten
457          }
458      }
459  
460 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
461 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
462 <    }
460 >    /**
461 >     * Permits the testing of parallel code for the 3 different
462 >     * execution modes without repeating all the testing code.
463 >     */
464 >    enum ExecutionMode {
465 >        DEFAULT {
466 >            public void checkExecutionMode() {
467 >                assertNull(ForkJoinTask.getPool());
468 >            }
469 >            public <T> CompletableFuture<Void> thenRun
470 >                (CompletableFuture<T> f, Runnable a) {
471 >                return f.thenRun(a);
472 >            }
473 >            public <T> CompletableFuture<Void> thenAccept
474 >                (CompletableFuture<T> f, Consumer<? super T> a) {
475 >                return f.thenAccept(a);
476 >            }
477 >            public <T,U> CompletableFuture<U> thenApply
478 >                (CompletableFuture<T> f, Function<? super T,U> a) {
479 >                return f.thenApply(a);
480 >            }
481 >            public <T,U> CompletableFuture<U> thenCompose
482 >                (CompletableFuture<T> f,
483 >                 Function<? super T,? extends CompletionStage<U>> a) {
484 >                return f.thenCompose(a);
485 >            }
486 >            public <T,U> CompletableFuture<U> handle
487 >                (CompletableFuture<T> f,
488 >                 BiFunction<? super T,Throwable,? extends U> a) {
489 >                return f.handle(a);
490 >            }
491 >            public <T> CompletableFuture<T> whenComplete
492 >                (CompletableFuture<T> f,
493 >                 BiConsumer<? super T,? super Throwable> a) {
494 >                return f.whenComplete(a);
495 >            }
496 >            public <T,U> CompletableFuture<Void> runAfterBoth
497 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
498 >                return f.runAfterBoth(g, a);
499 >            }
500 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
501 >                (CompletableFuture<T> f,
502 >                 CompletionStage<? extends U> g,
503 >                 BiConsumer<? super T,? super U> a) {
504 >                return f.thenAcceptBoth(g, a);
505 >            }
506 >            public <T,U,V> CompletableFuture<V> thenCombine
507 >                (CompletableFuture<T> f,
508 >                 CompletionStage<? extends U> g,
509 >                 BiFunction<? super T,? super U,? extends V> a) {
510 >                return f.thenCombine(g, a);
511 >            }
512 >            public <T> CompletableFuture<Void> runAfterEither
513 >                (CompletableFuture<T> f,
514 >                 CompletionStage<?> g,
515 >                 java.lang.Runnable a) {
516 >                return f.runAfterEither(g, a);
517 >            }
518 >            public <T> CompletableFuture<Void> acceptEither
519 >                (CompletableFuture<T> f,
520 >                 CompletionStage<? extends T> g,
521 >                 Consumer<? super T> a) {
522 >                return f.acceptEither(g, a);
523 >            }
524 >            public <T,U> CompletableFuture<U> applyToEither
525 >                (CompletableFuture<T> f,
526 >                 CompletionStage<? extends T> g,
527 >                 Function<? super T,U> a) {
528 >                return f.applyToEither(g, a);
529 >            }
530 >        },
531  
532 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
533 <        boolean ran;
534 <        public Integer apply(Integer x, Throwable t) {
535 <            ran = true;
536 <            return (t == null) ? two : three;
537 <        }
532 >        ASYNC {
533 >            public void checkExecutionMode() {
534 >                assertSame(ForkJoinPool.commonPool(),
535 >                           ForkJoinTask.getPool());
536 >            }
537 >            public <T> CompletableFuture<Void> thenRun
538 >                (CompletableFuture<T> f, Runnable a) {
539 >                return f.thenRunAsync(a);
540 >            }
541 >            public <T> CompletableFuture<Void> thenAccept
542 >                (CompletableFuture<T> f, Consumer<? super T> a) {
543 >                return f.thenAcceptAsync(a);
544 >            }
545 >            public <T,U> CompletableFuture<U> thenApply
546 >                (CompletableFuture<T> f, Function<? super T,U> a) {
547 >                return f.thenApplyAsync(a);
548 >            }
549 >            public <T,U> CompletableFuture<U> thenCompose
550 >                (CompletableFuture<T> f,
551 >                 Function<? super T,? extends CompletionStage<U>> a) {
552 >                return f.thenComposeAsync(a);
553 >            }
554 >            public <T,U> CompletableFuture<U> handle
555 >                (CompletableFuture<T> f,
556 >                 BiFunction<? super T,Throwable,? extends U> a) {
557 >                return f.handleAsync(a);
558 >            }
559 >            public <T> CompletableFuture<T> whenComplete
560 >                (CompletableFuture<T> f,
561 >                 BiConsumer<? super T,? super Throwable> a) {
562 >                return f.whenCompleteAsync(a);
563 >            }
564 >            public <T,U> CompletableFuture<Void> runAfterBoth
565 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
566 >                return f.runAfterBothAsync(g, a);
567 >            }
568 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
569 >                (CompletableFuture<T> f,
570 >                 CompletionStage<? extends U> g,
571 >                 BiConsumer<? super T,? super U> a) {
572 >                return f.thenAcceptBothAsync(g, a);
573 >            }
574 >            public <T,U,V> CompletableFuture<V> thenCombine
575 >                (CompletableFuture<T> f,
576 >                 CompletionStage<? extends U> g,
577 >                 BiFunction<? super T,? super U,? extends V> a) {
578 >                return f.thenCombineAsync(g, a);
579 >            }
580 >            public <T> CompletableFuture<Void> runAfterEither
581 >                (CompletableFuture<T> f,
582 >                 CompletionStage<?> g,
583 >                 java.lang.Runnable a) {
584 >                return f.runAfterEitherAsync(g, a);
585 >            }
586 >            public <T> CompletableFuture<Void> acceptEither
587 >                (CompletableFuture<T> f,
588 >                 CompletionStage<? extends T> g,
589 >                 Consumer<? super T> a) {
590 >                return f.acceptEitherAsync(g, a);
591 >            }
592 >            public <T,U> CompletableFuture<U> applyToEither
593 >                (CompletableFuture<T> f,
594 >                 CompletionStage<? extends T> g,
595 >                 Function<? super T,U> a) {
596 >                return f.applyToEitherAsync(g, a);
597 >            }
598 >        },
599 >
600 >        EXECUTOR {
601 >            public void checkExecutionMode() {
602 >                //TODO
603 >            }
604 >            public <T> CompletableFuture<Void> thenRun
605 >                (CompletableFuture<T> f, Runnable a) {
606 >                return f.thenRunAsync(a, new ThreadExecutor());
607 >            }
608 >            public <T> CompletableFuture<Void> thenAccept
609 >                (CompletableFuture<T> f, Consumer<? super T> a) {
610 >                return f.thenAcceptAsync(a, new ThreadExecutor());
611 >            }
612 >            public <T,U> CompletableFuture<U> thenApply
613 >                (CompletableFuture<T> f, Function<? super T,U> a) {
614 >                return f.thenApplyAsync(a, new ThreadExecutor());
615 >            }
616 >            public <T,U> CompletableFuture<U> thenCompose
617 >                (CompletableFuture<T> f,
618 >                 Function<? super T,? extends CompletionStage<U>> a) {
619 >                return f.thenComposeAsync(a, new ThreadExecutor());
620 >            }
621 >            public <T,U> CompletableFuture<U> handle
622 >                (CompletableFuture<T> f,
623 >                 BiFunction<? super T,Throwable,? extends U> a) {
624 >                return f.handleAsync(a, new ThreadExecutor());
625 >            }
626 >            public <T> CompletableFuture<T> whenComplete
627 >                (CompletableFuture<T> f,
628 >                 BiConsumer<? super T,? super Throwable> a) {
629 >                return f.whenCompleteAsync(a, new ThreadExecutor());
630 >            }
631 >            public <T,U> CompletableFuture<Void> runAfterBoth
632 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
633 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
634 >            }
635 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
636 >                (CompletableFuture<T> f,
637 >                 CompletionStage<? extends U> g,
638 >                 BiConsumer<? super T,? super U> a) {
639 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
640 >            }
641 >            public <T,U,V> CompletableFuture<V> thenCombine
642 >                (CompletableFuture<T> f,
643 >                 CompletionStage<? extends U> g,
644 >                 BiFunction<? super T,? super U,? extends V> a) {
645 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
646 >            }
647 >            public <T> CompletableFuture<Void> runAfterEither
648 >                (CompletableFuture<T> f,
649 >                 CompletionStage<?> g,
650 >                 java.lang.Runnable a) {
651 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
652 >            }
653 >            public <T> CompletableFuture<Void> acceptEither
654 >                (CompletableFuture<T> f,
655 >                 CompletionStage<? extends T> g,
656 >                 Consumer<? super T> a) {
657 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
658 >            }
659 >            public <T,U> CompletableFuture<U> applyToEither
660 >                (CompletableFuture<T> f,
661 >                 CompletionStage<? extends T> g,
662 >                 Function<? super T,U> a) {
663 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
664 >            }
665 >        };
666 >
667 >        public abstract void checkExecutionMode();
668 >        public abstract <T> CompletableFuture<Void> thenRun
669 >            (CompletableFuture<T> f, Runnable a);
670 >        public abstract <T> CompletableFuture<Void> thenAccept
671 >            (CompletableFuture<T> f, Consumer<? super T> a);
672 >        public abstract <T,U> CompletableFuture<U> thenApply
673 >            (CompletableFuture<T> f, Function<? super T,U> a);
674 >        public abstract <T,U> CompletableFuture<U> thenCompose
675 >            (CompletableFuture<T> f,
676 >             Function<? super T,? extends CompletionStage<U>> a);
677 >        public abstract <T,U> CompletableFuture<U> handle
678 >            (CompletableFuture<T> f,
679 >             BiFunction<? super T,Throwable,? extends U> a);
680 >        public abstract <T> CompletableFuture<T> whenComplete
681 >            (CompletableFuture<T> f,
682 >             BiConsumer<? super T,? super Throwable> a);
683 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
684 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
685 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
686 >            (CompletableFuture<T> f,
687 >             CompletionStage<? extends U> g,
688 >             BiConsumer<? super T,? super U> a);
689 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
690 >            (CompletableFuture<T> f,
691 >             CompletionStage<? extends U> g,
692 >             BiFunction<? super T,? super U,? extends V> a);
693 >        public abstract <T> CompletableFuture<Void> runAfterEither
694 >            (CompletableFuture<T> f,
695 >             CompletionStage<?> g,
696 >             java.lang.Runnable a);
697 >        public abstract <T> CompletableFuture<Void> acceptEither
698 >            (CompletableFuture<T> f,
699 >             CompletionStage<? extends T> g,
700 >             Consumer<? super T> a);
701 >        public abstract <T,U> CompletableFuture<U> applyToEither
702 >            (CompletableFuture<T> f,
703 >             CompletionStage<? extends T> g,
704 >             Function<? super T,U> a);
705      }
706  
707      /**
708 +     * exceptionally action is not invoked when source completes
709 +     * normally, and source result is propagated
710 +     */
711 +    public void testExceptionally_normalCompletion() {
712 +        for (boolean createIncomplete : new boolean[] { true, false })
713 +        for (Integer v1 : new Integer[] { 1, null })
714 +    {
715 +        final AtomicInteger a = new AtomicInteger(0);
716 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
717 +        if (!createIncomplete) f.complete(v1);
718 +        final CompletableFuture<Integer> g = f.exceptionally
719 +            ((Throwable t) -> {
720 +                // Should not be called
721 +                a.getAndIncrement();
722 +                throw new AssertionError();
723 +            });
724 +        if (createIncomplete) f.complete(v1);
725 +
726 +        checkCompletedNormally(g, v1);
727 +        checkCompletedNormally(f, v1);
728 +        assertEquals(0, a.get());
729 +    }}
730 +
731 +
732 +    /**
733       * exceptionally action completes with function value on source
734 <     * exception; otherwise with source value
734 >     * exception
735       */
736 <    public void testExceptionally() {
737 <        CompletableFuture<Integer> f = new CompletableFuture<>();
738 <        ExceptionToInteger r = new ExceptionToInteger();
739 <        CompletableFuture<Integer> g = f.exceptionally(r);
740 <        f.completeExceptionally(new CFException());
741 <        checkCompletedNormally(g, three);
736 >    public void testExceptionally_exceptionalCompletion() {
737 >        for (boolean createIncomplete : new boolean[] { true, false })
738 >        for (Integer v1 : new Integer[] { 1, null })
739 >    {
740 >        final AtomicInteger a = new AtomicInteger(0);
741 >        final CFException ex = new CFException();
742 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
743 >        if (!createIncomplete) f.completeExceptionally(ex);
744 >        final CompletableFuture<Integer> g = f.exceptionally
745 >            ((Throwable t) -> {
746 >                threadAssertSame(t, ex);
747 >                a.getAndIncrement();
748 >                return v1;
749 >            });
750 >        if (createIncomplete) f.completeExceptionally(ex);
751  
752 <        f = new CompletableFuture<>();
753 <        r = new ExceptionToInteger();
754 <        g = f.exceptionally(r);
755 <        f.complete(one);
756 <        checkCompletedNormally(g, one);
757 <    }
752 >        checkCompletedNormally(g, v1);
753 >        assertEquals(1, a.get());
754 >    }}
755 >
756 >    public void testExceptionally_exceptionalCompletionActionFailed() {
757 >        for (boolean createIncomplete : new boolean[] { true, false })
758 >        for (Integer v1 : new Integer[] { 1, null })
759 >    {
760 >        final AtomicInteger a = new AtomicInteger(0);
761 >        final CFException ex1 = new CFException();
762 >        final CFException ex2 = new CFException();
763 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
764 >        if (!createIncomplete) f.completeExceptionally(ex1);
765 >        final CompletableFuture<Integer> g = f.exceptionally
766 >            ((Throwable t) -> {
767 >                threadAssertSame(t, ex1);
768 >                a.getAndIncrement();
769 >                throw ex2;
770 >            });
771 >        if (createIncomplete) f.completeExceptionally(ex1);
772 >
773 >        checkCompletedWithWrappedCFException(g, ex2);
774 >        assertEquals(1, a.get());
775 >    }}
776 >
777 >    /**
778 >     * handle action completes normally with function value on normal
779 >     * completion of source
780 >     */
781 >    public void testHandle_normalCompletion() {
782 >        for (ExecutionMode m : ExecutionMode.values())
783 >        for (boolean createIncomplete : new boolean[] { true, false })
784 >        for (Integer v1 : new Integer[] { 1, null })
785 >    {
786 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
787 >        final AtomicInteger a = new AtomicInteger(0);
788 >        if (!createIncomplete) f.complete(v1);
789 >        final CompletableFuture<Integer> g = m.handle
790 >            (f,
791 >             (Integer x, Throwable t) -> {
792 >                threadAssertSame(x, v1);
793 >                threadAssertNull(t);
794 >                a.getAndIncrement();
795 >                return inc(v1);
796 >            });
797 >        if (createIncomplete) f.complete(v1);
798 >
799 >        checkCompletedNormally(g, inc(v1));
800 >        checkCompletedNormally(f, v1);
801 >        assertEquals(1, a.get());
802 >    }}
803  
804      /**
805 <     * handle action completes normally with function value on either
806 <     * normal or exceptional completion of source
805 >     * handle action completes normally with function value on
806 >     * exceptional completion of source
807       */
808 <    public void testHandle() {
809 <        CompletableFuture<Integer> f, g;
810 <        IntegerHandler r;
808 >    public void testHandle_exceptionalCompletion() {
809 >        for (ExecutionMode m : ExecutionMode.values())
810 >        for (boolean createIncomplete : new boolean[] { true, false })
811 >        for (Integer v1 : new Integer[] { 1, null })
812 >    {
813 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
814 >        final AtomicInteger a = new AtomicInteger(0);
815 >        final CFException ex = new CFException();
816 >        if (!createIncomplete) f.completeExceptionally(ex);
817 >        final CompletableFuture<Integer> g = m.handle
818 >            (f,
819 >             (Integer x, Throwable t) -> {
820 >                threadAssertNull(x);
821 >                threadAssertSame(t, ex);
822 >                a.getAndIncrement();
823 >                return v1;
824 >            });
825 >        if (createIncomplete) f.completeExceptionally(ex);
826  
827 <        f = new CompletableFuture<>();
828 <        f.completeExceptionally(new CFException());
829 <        g = f.handle(r = new IntegerHandler());
830 <        assertTrue(r.ran);
437 <        checkCompletedNormally(g, three);
827 >        checkCompletedNormally(g, v1);
828 >        checkCompletedWithWrappedCFException(f, ex);
829 >        assertEquals(1, a.get());
830 >    }}
831  
832 <        f = new CompletableFuture<>();
833 <        g = f.handle(r = new IntegerHandler());
834 <        assertFalse(r.ran);
835 <        f.completeExceptionally(new CFException());
836 <        checkCompletedNormally(g, three);
837 <        assertTrue(r.ran);
832 >    /**
833 >     * handle action completes normally with function value on
834 >     * cancelled source
835 >     */
836 >    public void testHandle_sourceCancelled() {
837 >        for (ExecutionMode m : ExecutionMode.values())
838 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
839 >        for (boolean createIncomplete : new boolean[] { true, false })
840 >        for (Integer v1 : new Integer[] { 1, null })
841 >    {
842 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
843 >        final AtomicInteger a = new AtomicInteger(0);
844 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
845 >        final CompletableFuture<Integer> g = m.handle
846 >            (f,
847 >             (Integer x, Throwable t) -> {
848 >                threadAssertNull(x);
849 >                threadAssertTrue(t instanceof CancellationException);
850 >                a.getAndIncrement();
851 >                return v1;
852 >            });
853 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
854  
855 <        f = new CompletableFuture<>();
856 <        f.complete(one);
857 <        g = f.handle(r = new IntegerHandler());
858 <        assertTrue(r.ran);
450 <        checkCompletedNormally(g, two);
855 >        checkCompletedNormally(g, v1);
856 >        checkCancelled(f);
857 >        assertEquals(1, a.get());
858 >    }}
859  
860 <        f = new CompletableFuture<>();
861 <        g = f.handle(r = new IntegerHandler());
862 <        assertFalse(r.ran);
863 <        f.complete(one);
864 <        assertTrue(r.ran);
865 <        checkCompletedNormally(g, two);
866 <    }
860 >    /**
861 >     * handle result completes exceptionally if action does
862 >     */
863 >    public void testHandle_sourceFailedActionFailed() {
864 >        for (ExecutionMode m : ExecutionMode.values())
865 >        for (boolean createIncomplete : new boolean[] { true, false })
866 >    {
867 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
868 >        final AtomicInteger a = new AtomicInteger(0);
869 >        final CFException ex1 = new CFException();
870 >        final CFException ex2 = new CFException();
871 >        if (!createIncomplete) f.completeExceptionally(ex1);
872 >        final CompletableFuture<Integer> g = m.handle
873 >            (f,
874 >             (Integer x, Throwable t) -> {
875 >                threadAssertNull(x);
876 >                threadAssertSame(ex1, t);
877 >                a.getAndIncrement();
878 >                throw ex2;
879 >            });
880 >        if (createIncomplete) f.completeExceptionally(ex1);
881 >
882 >        checkCompletedWithWrappedCFException(g, ex2);
883 >        checkCompletedWithWrappedCFException(f, ex1);
884 >        assertEquals(1, a.get());
885 >    }}
886 >
887 >    public void testHandle_sourceCompletedNormallyActionFailed() {
888 >        for (ExecutionMode m : ExecutionMode.values())
889 >        for (boolean createIncomplete : new boolean[] { true, false })
890 >        for (Integer v1 : new Integer[] { 1, null })
891 >    {
892 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
893 >        final AtomicInteger a = new AtomicInteger(0);
894 >        final CFException ex = new CFException();
895 >        if (!createIncomplete) f.complete(v1);
896 >        final CompletableFuture<Integer> g = m.handle
897 >            (f,
898 >             (Integer x, Throwable t) -> {
899 >                threadAssertSame(x, v1);
900 >                threadAssertNull(t);
901 >                a.getAndIncrement();
902 >                throw ex;
903 >            });
904 >        if (createIncomplete) f.complete(v1);
905 >
906 >        checkCompletedWithWrappedCFException(g, ex);
907 >        checkCompletedNormally(f, v1);
908 >        assertEquals(1, a.get());
909 >    }}
910  
911      /**
912       * runAsync completes after running Runnable
913       */
914      public void testRunAsync() {
915 <        Noop r = new Noop();
915 >        Noop r = new Noop(ExecutionMode.ASYNC);
916          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
917          assertNull(f.join());
918 <        assertTrue(r.ran);
918 >        assertEquals(1, r.invocationCount);
919          checkCompletedNormally(f, null);
920      }
921  
# Line 472 | Line 923 | public class CompletableFutureTest exten
923       * runAsync with executor completes after running Runnable
924       */
925      public void testRunAsync2() {
926 <        Noop r = new Noop();
926 >        Noop r = new Noop(ExecutionMode.EXECUTOR);
927          ThreadExecutor exec = new ThreadExecutor();
928          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
929          assertNull(f.join());
930 <        assertTrue(r.ran);
930 >        assertEquals(1, r.invocationCount);
931          checkCompletedNormally(f, null);
932          assertEquals(1, exec.count.get());
933      }
# Line 485 | Line 936 | public class CompletableFutureTest exten
936       * failing runAsync completes exceptionally after running Runnable
937       */
938      public void testRunAsync3() {
939 <        FailingNoop r = new FailingNoop();
939 >        FailingRunnable r = new FailingRunnable();
940          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
941          checkCompletedWithWrappedCFException(f);
942 <        assertTrue(r.ran);
942 >        assertEquals(1, r.invocationCount);
943      }
944  
945      /**
# Line 518 | Line 969 | public class CompletableFutureTest exten
969          FailingSupplier r = new FailingSupplier();
970          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
971          checkCompletedWithWrappedCFException(f);
972 <        assertTrue(r.ran);
972 >        assertEquals(1, r.invocationCount);
973      }
974  
975      // seq completion methods
# Line 526 | Line 977 | public class CompletableFutureTest exten
977      /**
978       * thenRun result completes normally after normal completion of source
979       */
980 <    public void testThenRun() {
981 <        CompletableFuture<Integer> f;
982 <        CompletableFuture<Void> g;
983 <        Noop r;
984 <
985 <        f = new CompletableFuture<>();
986 <        g = f.thenRun(r = new Noop());
987 <        f.complete(null);
988 <        checkCompletedNormally(g, null);
989 <        assertTrue(r.ran);
980 >    public void testThenRun_normalCompletion() {
981 >        for (ExecutionMode m : ExecutionMode.values())
982 >        for (boolean createIncomplete : new boolean[] { true, false })
983 >        for (Integer v1 : new Integer[] { 1, null })
984 >    {
985 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
986 >        final Noop r = new Noop(m);
987 >        if (!createIncomplete) f.complete(v1);
988 >        final CompletableFuture<Void> g = m.thenRun(f, r);
989 >        if (createIncomplete) {
990 >            checkIncomplete(g);
991 >            f.complete(v1);
992 >        }
993  
540        f = new CompletableFuture<>();
541        f.complete(null);
542        g = f.thenRun(r = new Noop());
994          checkCompletedNormally(g, null);
995 <        assertTrue(r.ran);
996 <    }
995 >        checkCompletedNormally(f, v1);
996 >        assertEquals(1, r.invocationCount);
997 >    }}
998  
999      /**
1000       * thenRun result completes exceptionally after exceptional
1001       * completion of source
1002       */
1003 <    public void testThenRun2() {
1004 <        CompletableFuture<Integer> f;
1005 <        CompletableFuture<Void> g;
1006 <        Noop r;
1007 <
1008 <        f = new CompletableFuture<>();
1009 <        g = f.thenRun(r = new Noop());
1010 <        f.completeExceptionally(new CFException());
1011 <        checkCompletedWithWrappedCFException(g);
1012 <        assertFalse(r.ran);
1003 >    public void testThenRun_exceptionalCompletion() {
1004 >        for (ExecutionMode m : ExecutionMode.values())
1005 >        for (boolean createIncomplete : new boolean[] { true, false })
1006 >    {
1007 >        final CFException ex = new CFException();
1008 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1009 >        final Noop r = new Noop(m);
1010 >        if (!createIncomplete) f.completeExceptionally(ex);
1011 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1012 >        if (createIncomplete) {
1013 >            checkIncomplete(g);
1014 >            f.completeExceptionally(ex);
1015 >        }
1016  
1017 <        f = new CompletableFuture<>();
1018 <        f.completeExceptionally(new CFException());
1019 <        g = f.thenRun(r = new Noop());
1020 <        checkCompletedWithWrappedCFException(g);
566 <        assertFalse(r.ran);
567 <    }
1017 >        checkCompletedWithWrappedCFException(g, ex);
1018 >        checkCompletedWithWrappedCFException(f, ex);
1019 >        assertEquals(0, r.invocationCount);
1020 >    }}
1021  
1022      /**
1023 <     * thenRun result completes exceptionally if action does
1023 >     * thenRun result completes exceptionally if source cancelled
1024       */
1025 <    public void testThenRun3() {
1026 <        CompletableFuture<Integer> f;
1027 <        CompletableFuture<Void> g;
1028 <        FailingNoop r;
1029 <
1030 <        f = new CompletableFuture<>();
1031 <        g = f.thenRun(r = new FailingNoop());
1032 <        f.complete(null);
1033 <        checkCompletedWithWrappedCFException(g);
1025 >    public void testThenRun_sourceCancelled() {
1026 >        for (ExecutionMode m : ExecutionMode.values())
1027 >        for (boolean createIncomplete : new boolean[] { true, false })
1028 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1029 >    {
1030 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1031 >        final Noop r = new Noop(m);
1032 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1033 >        final CompletableFuture<Void> g = f.thenRun(r);
1034 >        if (createIncomplete) {
1035 >            checkIncomplete(g);
1036 >            assertTrue(f.cancel(mayInterruptIfRunning));
1037 >        }
1038  
1039 <        f = new CompletableFuture<>();
1040 <        f.complete(null);
1041 <        g = f.thenRun(r = new FailingNoop());
1042 <        checkCompletedWithWrappedCFException(g);
586 <    }
1039 >        checkCompletedWithWrappedCancellationException(g);
1040 >        checkCancelled(f);
1041 >        assertEquals(0, r.invocationCount);
1042 >    }}
1043  
1044      /**
1045 <     * thenRun result completes exceptionally if source cancelled
1045 >     * thenRun result completes exceptionally if action does
1046       */
1047 <    public void testThenRun4() {
1048 <        CompletableFuture<Integer> f;
1049 <        CompletableFuture<Void> g;
1050 <        Noop r;
1051 <
1052 <        f = new CompletableFuture<>();
1053 <        g = f.thenRun(r = new Noop());
1054 <        assertTrue(f.cancel(true));
1055 <        checkCompletedWithWrappedCancellationException(g);
1047 >    public void testThenRun_actionFailed() {
1048 >        for (ExecutionMode m : ExecutionMode.values())
1049 >        for (boolean createIncomplete : new boolean[] { true, false })
1050 >        for (Integer v1 : new Integer[] { 1, null })
1051 >    {
1052 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1053 >        final FailingRunnable r = new FailingRunnable();
1054 >        if (!createIncomplete) f.complete(v1);
1055 >        final CompletableFuture<Void> g = f.thenRun(r);
1056 >        if (createIncomplete) {
1057 >            checkIncomplete(g);
1058 >            f.complete(v1);
1059 >        }
1060  
1061 <        f = new CompletableFuture<>();
1062 <        assertTrue(f.cancel(true));
1063 <        g = f.thenRun(r = new Noop());
604 <        checkCompletedWithWrappedCancellationException(g);
605 <    }
1061 >        checkCompletedWithWrappedCFException(g);
1062 >        checkCompletedNormally(f, v1);
1063 >    }}
1064  
1065      /**
1066       * thenApply result completes normally after normal completion of source
1067       */
1068 <    public void testThenApply() {
1069 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1070 <        CompletableFuture<Integer> g = f.thenApply(inc);
1071 <        f.complete(one);
1072 <        checkCompletedNormally(g, two);
1073 <    }
1068 >    public void testThenApply_normalCompletion() {
1069 >        for (ExecutionMode m : ExecutionMode.values())
1070 >        for (boolean createIncomplete : new boolean[] { true, false })
1071 >        for (Integer v1 : new Integer[] { 1, null })
1072 >    {
1073 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1074 >        final IncFunction r = new IncFunction();
1075 >        if (!createIncomplete) f.complete(v1);
1076 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1077 >        if (createIncomplete) {
1078 >            checkIncomplete(g);
1079 >            f.complete(v1);
1080 >        }
1081 >
1082 >        checkCompletedNormally(g, inc(v1));
1083 >        checkCompletedNormally(f, v1);
1084 >        assertEquals(1, r.invocationCount);
1085 >    }}
1086  
1087      /**
1088       * thenApply result completes exceptionally after exceptional
1089       * completion of source
1090       */
1091 <    public void testThenApply2() {
1092 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1093 <        CompletableFuture<Integer> g = f.thenApply(inc);
1094 <        f.completeExceptionally(new CFException());
1095 <        checkCompletedWithWrappedCFException(g);
1096 <    }
1091 >    public void testThenApply_exceptionalCompletion() {
1092 >        for (ExecutionMode m : ExecutionMode.values())
1093 >        for (boolean createIncomplete : new boolean[] { true, false })
1094 >    {
1095 >        final CFException ex = new CFException();
1096 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1097 >        final IncFunction r = new IncFunction();
1098 >        if (!createIncomplete) f.completeExceptionally(ex);
1099 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1100 >        if (createIncomplete) {
1101 >            checkIncomplete(g);
1102 >            f.completeExceptionally(ex);
1103 >        }
1104  
1105 <    /**
1106 <     * thenApply result completes exceptionally if action does
1107 <     */
1108 <    public void testThenApply3() {
632 <        CompletableFuture<Integer> f = new CompletableFuture<>();
633 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
634 <        f.complete(one);
635 <        checkCompletedWithWrappedCFException(g);
636 <    }
1105 >        checkCompletedWithWrappedCFException(g, ex);
1106 >        checkCompletedWithWrappedCFException(f, ex);
1107 >        assertEquals(0, r.invocationCount);
1108 >    }}
1109  
1110      /**
1111       * thenApply result completes exceptionally if source cancelled
1112       */
1113 <    public void testThenApply4() {
1114 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1115 <        CompletableFuture<Integer> g = f.thenApply(inc);
1116 <        assertTrue(f.cancel(true));
1113 >    public void testThenApply_sourceCancelled() {
1114 >        for (ExecutionMode m : ExecutionMode.values())
1115 >        for (boolean createIncomplete : new boolean[] { true, false })
1116 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1117 >    {
1118 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1119 >        final IncFunction r = new IncFunction();
1120 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1121 >        final CompletableFuture<Integer> g = f.thenApply(r);
1122 >        if (createIncomplete) {
1123 >            checkIncomplete(g);
1124 >            assertTrue(f.cancel(mayInterruptIfRunning));
1125 >        }
1126 >
1127          checkCompletedWithWrappedCancellationException(g);
1128 <    }
1128 >        checkCancelled(f);
1129 >        assertEquals(0, r.invocationCount);
1130 >    }}
1131 >
1132 >    /**
1133 >     * thenApply result completes exceptionally if action does
1134 >     */
1135 >    public void testThenApply_actionFailed() {
1136 >        for (ExecutionMode m : ExecutionMode.values())
1137 >        for (boolean createIncomplete : new boolean[] { true, false })
1138 >        for (Integer v1 : new Integer[] { 1, null })
1139 >    {
1140 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1141 >        final FailingFunction r = new FailingFunction();
1142 >        if (!createIncomplete) f.complete(v1);
1143 >        final CompletableFuture<Integer> g = f.thenApply(r);
1144 >        if (createIncomplete) {
1145 >            checkIncomplete(g);
1146 >            f.complete(v1);
1147 >        }
1148 >
1149 >        checkCompletedWithWrappedCFException(g);
1150 >        checkCompletedNormally(f, v1);
1151 >    }}
1152  
1153      /**
1154       * thenAccept result completes normally after normal completion of source
1155       */
1156 <    public void testThenAccept() {
1157 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1158 <        IncAction r = new IncAction();
1159 <        CompletableFuture<Void> g = f.thenAccept(r);
1160 <        f.complete(one);
1156 >    public void testThenAccept_normalCompletion() {
1157 >        for (ExecutionMode m : ExecutionMode.values())
1158 >        for (boolean createIncomplete : new boolean[] { true, false })
1159 >        for (Integer v1 : new Integer[] { 1, null })
1160 >    {
1161 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1162 >        final IncAction r = new IncAction();
1163 >        if (!createIncomplete) f.complete(v1);
1164 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1165 >        if (createIncomplete) {
1166 >            checkIncomplete(g);
1167 >            f.complete(v1);
1168 >        }
1169 >
1170          checkCompletedNormally(g, null);
1171 <        assertEquals(r.value, 2);
1172 <    }
1171 >        checkCompletedNormally(f, v1);
1172 >        assertEquals(1, r.invocationCount);
1173 >        assertEquals(inc(v1), r.value);
1174 >    }}
1175  
1176      /**
1177       * thenAccept result completes exceptionally after exceptional
1178       * completion of source
1179       */
1180 <    public void testThenAccept2() {
1181 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1182 <        IncAction r = new IncAction();
1183 <        CompletableFuture<Void> g = f.thenAccept(r);
1184 <        f.completeExceptionally(new CFException());
1185 <        checkCompletedWithWrappedCFException(g);
1186 <    }
1180 >    public void testThenAccept_exceptionalCompletion() {
1181 >        for (ExecutionMode m : ExecutionMode.values())
1182 >        for (boolean createIncomplete : new boolean[] { true, false })
1183 >    {
1184 >        final CFException ex = new CFException();
1185 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1186 >        final IncAction r = new IncAction();
1187 >        if (!createIncomplete) f.completeExceptionally(ex);
1188 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1189 >        if (createIncomplete) {
1190 >            checkIncomplete(g);
1191 >            f.completeExceptionally(ex);
1192 >        }
1193 >
1194 >        checkCompletedWithWrappedCFException(g, ex);
1195 >        checkCompletedWithWrappedCFException(f, ex);
1196 >        assertEquals(0, r.invocationCount);
1197 >    }}
1198  
1199      /**
1200       * thenAccept result completes exceptionally if action does
1201       */
1202 <    public void testThenAccept3() {
1203 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1204 <        FailingConsumer r = new FailingConsumer();
1205 <        CompletableFuture<Void> g = f.thenAccept(r);
1206 <        f.complete(one);
1202 >    public void testThenAccept_actionFailed() {
1203 >        for (ExecutionMode m : ExecutionMode.values())
1204 >        for (boolean createIncomplete : new boolean[] { true, false })
1205 >        for (Integer v1 : new Integer[] { 1, null })
1206 >    {
1207 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1208 >        final FailingConsumer r = new FailingConsumer();
1209 >        if (!createIncomplete) f.complete(v1);
1210 >        final CompletableFuture<Void> g = f.thenAccept(r);
1211 >        if (createIncomplete) {
1212 >            checkIncomplete(g);
1213 >            f.complete(v1);
1214 >        }
1215 >
1216          checkCompletedWithWrappedCFException(g);
1217 <        assertTrue(r.ran);
1218 <    }
1217 >        checkCompletedNormally(f, v1);
1218 >    }}
1219  
1220      /**
1221       * thenAccept result completes exceptionally if source cancelled
1222       */
1223 <    public void testThenAccept4() {
1224 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1225 <        IncAction r = new IncAction();
1226 <        CompletableFuture<Void> g = f.thenAccept(r);
1227 <        assertTrue(f.cancel(true));
1223 >    public void testThenAccept_sourceCancelled() {
1224 >        for (ExecutionMode m : ExecutionMode.values())
1225 >        for (boolean createIncomplete : new boolean[] { true, false })
1226 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1227 >    {
1228 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1229 >        final IncAction r = new IncAction();
1230 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1231 >        final CompletableFuture<Void> g = f.thenAccept(r);
1232 >        if (createIncomplete) {
1233 >            checkIncomplete(g);
1234 >            assertTrue(f.cancel(mayInterruptIfRunning));
1235 >        }
1236 >
1237          checkCompletedWithWrappedCancellationException(g);
1238 <    }
1238 >        checkCancelled(f);
1239 >        assertEquals(0, r.invocationCount);
1240 >    }}
1241  
1242      /**
1243       * thenCombine result completes normally after normal completion
1244       * of sources
1245       */
1246 <    public void testThenCombine() {
1247 <        CompletableFuture<Integer> f, g, h;
1248 <
1249 <        f = new CompletableFuture<>();
1250 <        g = new CompletableFuture<>();
1251 <        h = f.thenCombine(g, subtract);
1252 <        f.complete(3);
1253 <        checkIncomplete(h);
1254 <        g.complete(1);
1255 <        checkCompletedNormally(h, 2);
1246 >    public void testThenCombine_normalCompletion() {
1247 >        for (ExecutionMode m : ExecutionMode.values())
1248 >        for (boolean createIncomplete : new boolean[] { true, false })
1249 >        for (boolean fFirst : new boolean[] { true, false })
1250 >        for (Integer v1 : new Integer[] { 1, null })
1251 >        for (Integer v2 : new Integer[] { 2, null })
1252 >    {
1253 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1254 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1255 >        final SubtractFunction r = new SubtractFunction();
1256  
1257 <        f = new CompletableFuture<>();
1258 <        g = new CompletableFuture<>();
1259 <        h = f.thenCombine(g, subtract);
1260 <        g.complete(1);
1261 <        checkIncomplete(h);
1262 <        f.complete(3);
1263 <        checkCompletedNormally(h, 2);
1257 >        if (fFirst) f.complete(v1); else g.complete(v2);
1258 >        if (!createIncomplete)
1259 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1260 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1261 >        if (createIncomplete) {
1262 >            checkIncomplete(h);
1263 >            assertEquals(0, r.invocationCount);
1264 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1265 >        }
1266  
1267 <        f = new CompletableFuture<>();
1268 <        g = new CompletableFuture<>();
1269 <        g.complete(1);
1270 <        f.complete(3);
1271 <        h = f.thenCombine(g, subtract);
723 <        checkCompletedNormally(h, 2);
724 <    }
1267 >        checkCompletedNormally(h, subtract(v1, v2));
1268 >        checkCompletedNormally(f, v1);
1269 >        checkCompletedNormally(g, v2);
1270 >        assertEquals(1, r.invocationCount);
1271 >    }}
1272  
1273      /**
1274       * thenCombine result completes exceptionally after exceptional
1275       * completion of either source
1276       */
1277 <    public void testThenCombine2() {
1278 <        CompletableFuture<Integer> f, g, h;
1279 <
1280 <        f = new CompletableFuture<>();
1281 <        g = new CompletableFuture<>();
1282 <        h = f.thenCombine(g, subtract);
1283 <        f.completeExceptionally(new CFException());
1284 <        checkIncomplete(h);
1285 <        g.complete(1);
1286 <        checkCompletedWithWrappedCFException(h);
740 <
741 <        f = new CompletableFuture<>();
742 <        g = new CompletableFuture<>();
743 <        h = f.thenCombine(g, subtract);
744 <        g.completeExceptionally(new CFException());
745 <        checkIncomplete(h);
746 <        f.complete(3);
747 <        checkCompletedWithWrappedCFException(h);
1277 >    public void testThenCombine_exceptionalCompletion() {
1278 >        for (ExecutionMode m : ExecutionMode.values())
1279 >        for (boolean createIncomplete : new boolean[] { true, false })
1280 >        for (boolean fFirst : new boolean[] { true, false })
1281 >        for (Integer v1 : new Integer[] { 1, null })
1282 >    {
1283 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1285 >        final CFException ex = new CFException();
1286 >        final SubtractFunction r = new SubtractFunction();
1287  
1288 <        f = new CompletableFuture<>();
1289 <        g = new CompletableFuture<>();
1290 <        f.complete(3);
1291 <        g.completeExceptionally(new CFException());
1292 <        h = f.thenCombine(g, subtract);
1293 <        checkCompletedWithWrappedCFException(h);
1288 >        (fFirst ? f : g).complete(v1);
1289 >        if (!createIncomplete)
1290 >            (!fFirst ? f : g).completeExceptionally(ex);
1291 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1292 >        if (createIncomplete) {
1293 >            checkIncomplete(h);
1294 >            (!fFirst ? f : g).completeExceptionally(ex);
1295 >        }
1296  
1297 <        f = new CompletableFuture<>();
1298 <        g = new CompletableFuture<>();
1299 <        f.completeExceptionally(new CFException());
1300 <        g.complete(3);
1301 <        h = f.thenCombine(g, subtract);
761 <        checkCompletedWithWrappedCFException(h);
762 <    }
1297 >        checkCompletedWithWrappedCFException(h, ex);
1298 >        assertEquals(0, r.invocationCount);
1299 >        checkCompletedNormally(fFirst ? f : g, v1);
1300 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1301 >    }}
1302  
1303      /**
1304       * thenCombine result completes exceptionally if action does
1305       */
1306 <    public void testThenCombine3() {
1307 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1308 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1309 <        FailingBiFunction r = new FailingBiFunction();
1310 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1311 <        f.complete(one);
1312 <        checkIncomplete(g);
1313 <        assertFalse(r.ran);
1314 <        f2.complete(two);
1315 <        checkCompletedWithWrappedCFException(g);
1316 <        assertTrue(r.ran);
1317 <    }
1306 >    public void testThenCombine_actionFailed() {
1307 >        for (ExecutionMode m : ExecutionMode.values())
1308 >        for (boolean fFirst : new boolean[] { true, false })
1309 >        for (Integer v1 : new Integer[] { 1, null })
1310 >        for (Integer v2 : new Integer[] { 2, null })
1311 >    {
1312 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1313 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1314 >        final FailingBiFunction r = new FailingBiFunction();
1315 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1316 >
1317 >        if (fFirst) {
1318 >            f.complete(v1);
1319 >            g.complete(v2);
1320 >        } else {
1321 >            g.complete(v2);
1322 >            f.complete(v1);
1323 >        }
1324 >
1325 >        checkCompletedWithWrappedCFException(h);
1326 >        checkCompletedNormally(f, v1);
1327 >        checkCompletedNormally(g, v2);
1328 >    }}
1329  
1330      /**
1331       * thenCombine result completes exceptionally if either source cancelled
1332       */
1333 <    public void testThenCombine4() {
1334 <        CompletableFuture<Integer> f, g, h;
1335 <
1336 <        f = new CompletableFuture<>();
1337 <        g = new CompletableFuture<>();
1338 <        h = f.thenCombine(g, subtract);
1339 <        assertTrue(f.cancel(true));
1340 <        checkIncomplete(h);
1341 <        g.complete(1);
1342 <        checkCompletedWithWrappedCancellationException(h);
1333 >    public void testThenCombine_sourceCancelled() {
1334 >        for (ExecutionMode m : ExecutionMode.values())
1335 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1336 >        for (boolean createIncomplete : new boolean[] { true, false })
1337 >        for (boolean fFirst : new boolean[] { true, false })
1338 >        for (Integer v1 : new Integer[] { 1, null })
1339 >    {
1340 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1341 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1342 >        final SubtractFunction r = new SubtractFunction();
1343  
1344 <        f = new CompletableFuture<>();
1345 <        g = new CompletableFuture<>();
1346 <        h = f.thenCombine(g, subtract);
1347 <        assertTrue(g.cancel(true));
1348 <        checkIncomplete(h);
1349 <        f.complete(3);
1350 <        checkCompletedWithWrappedCancellationException(h);
1344 >        (fFirst ? f : g).complete(v1);
1345 >        if (!createIncomplete)
1346 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1347 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1348 >        if (createIncomplete) {
1349 >            checkIncomplete(h);
1350 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1351 >        }
1352  
802        f = new CompletableFuture<>();
803        g = new CompletableFuture<>();
804        assertTrue(f.cancel(true));
805        assertTrue(g.cancel(true));
806        h = f.thenCombine(g, subtract);
1353          checkCompletedWithWrappedCancellationException(h);
1354 <    }
1354 >        checkCancelled(!fFirst ? f : g);
1355 >        assertEquals(0, r.invocationCount);
1356 >        checkCompletedNormally(fFirst ? f : g, v1);
1357 >    }}
1358  
1359      /**
1360       * thenAcceptBoth result completes normally after normal
1361       * completion of sources
1362       */
1363 <    public void testThenAcceptBoth() {
1364 <        CompletableFuture<Integer> f, g;
1365 <        CompletableFuture<Void> h;
1366 <        SubtractAction r;
1367 <
1368 <        f = new CompletableFuture<>();
1369 <        g = new CompletableFuture<>();
1370 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1371 <        f.complete(3);
1372 <        checkIncomplete(h);
824 <        g.complete(1);
825 <        checkCompletedNormally(h, null);
826 <        assertEquals(r.value, 2);
1363 >    public void testThenAcceptBoth_normalCompletion() {
1364 >        for (ExecutionMode m : ExecutionMode.values())
1365 >        for (boolean createIncomplete : new boolean[] { true, false })
1366 >        for (boolean fFirst : new boolean[] { true, false })
1367 >        for (Integer v1 : new Integer[] { 1, null })
1368 >        for (Integer v2 : new Integer[] { 2, null })
1369 >    {
1370 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1371 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1372 >        final SubtractAction r = new SubtractAction();
1373  
1374 <        f = new CompletableFuture<>();
1375 <        g = new CompletableFuture<>();
1376 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1377 <        g.complete(1);
1378 <        checkIncomplete(h);
1379 <        f.complete(3);
1380 <        checkCompletedNormally(h, null);
1381 <        assertEquals(r.value, 2);
1374 >        if (fFirst) f.complete(v1); else g.complete(v2);
1375 >        if (!createIncomplete)
1376 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1377 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1378 >        if (createIncomplete) {
1379 >            checkIncomplete(h);
1380 >            assertEquals(0, r.invocationCount);
1381 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1382 >        }
1383  
837        f = new CompletableFuture<>();
838        g = new CompletableFuture<>();
839        g.complete(1);
840        f.complete(3);
841        h = f.thenAcceptBoth(g, r = new SubtractAction());
1384          checkCompletedNormally(h, null);
1385 <        assertEquals(r.value, 2);
1386 <    }
1385 >        assertEquals(subtract(v1, v2), r.value);
1386 >        checkCompletedNormally(f, v1);
1387 >        checkCompletedNormally(g, v2);
1388 >    }}
1389  
1390      /**
1391       * thenAcceptBoth result completes exceptionally after exceptional
1392       * completion of either source
1393       */
1394 <    public void testThenAcceptBoth2() {
1395 <        CompletableFuture<Integer> f, g;
1396 <        CompletableFuture<Void> h;
1397 <        SubtractAction r;
1398 <
1399 <        f = new CompletableFuture<>();
1400 <        g = new CompletableFuture<>();
1401 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1402 <        f.completeExceptionally(new CFException());
1403 <        checkIncomplete(h);
860 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
862 <
863 <        f = new CompletableFuture<>();
864 <        g = new CompletableFuture<>();
865 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
866 <        g.completeExceptionally(new CFException());
867 <        checkIncomplete(h);
868 <        f.complete(3);
869 <        checkCompletedWithWrappedCFException(h);
1394 >    public void testThenAcceptBoth_exceptionalCompletion() {
1395 >        for (ExecutionMode m : ExecutionMode.values())
1396 >        for (boolean createIncomplete : new boolean[] { true, false })
1397 >        for (boolean fFirst : new boolean[] { true, false })
1398 >        for (Integer v1 : new Integer[] { 1, null })
1399 >    {
1400 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1401 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1402 >        final CFException ex = new CFException();
1403 >        final SubtractAction r = new SubtractAction();
1404  
1405 <        f = new CompletableFuture<>();
1406 <        g = new CompletableFuture<>();
1407 <        f.complete(3);
1408 <        g.completeExceptionally(new CFException());
1409 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1410 <        checkCompletedWithWrappedCFException(h);
1405 >        (fFirst ? f : g).complete(v1);
1406 >        if (!createIncomplete)
1407 >            (!fFirst ? f : g).completeExceptionally(ex);
1408 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1409 >        if (createIncomplete) {
1410 >            checkIncomplete(h);
1411 >            (!fFirst ? f : g).completeExceptionally(ex);
1412 >        }
1413  
1414 <        f = new CompletableFuture<>();
1415 <        g = new CompletableFuture<>();
1416 <        f.completeExceptionally(new CFException());
1417 <        g.complete(3);
1418 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
883 <        checkCompletedWithWrappedCFException(h);
884 <    }
1414 >        checkCompletedWithWrappedCFException(h, ex);
1415 >        assertEquals(0, r.invocationCount);
1416 >        checkCompletedNormally(fFirst ? f : g, v1);
1417 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1418 >    }}
1419  
1420      /**
1421       * thenAcceptBoth result completes exceptionally if action does
1422       */
1423 <    public void testThenAcceptBoth3() {
1424 <        CompletableFuture<Integer> f, g;
1425 <        CompletableFuture<Void> h;
1426 <        FailingBiConsumer r;
1423 >    public void testThenAcceptBoth_actionFailed() {
1424 >        for (ExecutionMode m : ExecutionMode.values())
1425 >        for (boolean fFirst : new boolean[] { true, false })
1426 >        for (Integer v1 : new Integer[] { 1, null })
1427 >        for (Integer v2 : new Integer[] { 2, null })
1428 >    {
1429 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1430 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1431 >        final FailingBiConsumer r = new FailingBiConsumer();
1432 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1433  
1434 <        f = new CompletableFuture<>();
1435 <        g = new CompletableFuture<>();
1436 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1437 <        f.complete(3);
1438 <        checkIncomplete(h);
1439 <        g.complete(1);
1440 <        checkCompletedWithWrappedCFException(h);
1434 >        if (fFirst) {
1435 >            f.complete(v1);
1436 >            g.complete(v2);
1437 >        } else {
1438 >            g.complete(v2);
1439 >            f.complete(v1);
1440 >        }
1441  
902        f = new CompletableFuture<>();
903        g = new CompletableFuture<>();
904        f.complete(3);
905        g.complete(1);
906        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1442          checkCompletedWithWrappedCFException(h);
1443 <    }
1443 >        checkCompletedNormally(f, v1);
1444 >        checkCompletedNormally(g, v2);
1445 >    }}
1446  
1447      /**
1448       * thenAcceptBoth result completes exceptionally if either source cancelled
1449       */
1450 <    public void testThenAcceptBoth4() {
1451 <        CompletableFuture<Integer> f, g;
1452 <        CompletableFuture<Void> h;
1453 <        SubtractAction r;
1454 <
1455 <        f = new CompletableFuture<>();
1456 <        g = new CompletableFuture<>();
1457 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1458 <        assertTrue(f.cancel(true));
1459 <        checkIncomplete(h);
923 <        g.complete(1);
924 <        checkCompletedWithWrappedCancellationException(h);
925 <
926 <        f = new CompletableFuture<>();
927 <        g = new CompletableFuture<>();
928 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
929 <        assertTrue(g.cancel(true));
930 <        checkIncomplete(h);
931 <        f.complete(3);
932 <        checkCompletedWithWrappedCancellationException(h);
1450 >    public void testThenAcceptBoth_sourceCancelled() {
1451 >        for (ExecutionMode m : ExecutionMode.values())
1452 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1453 >        for (boolean createIncomplete : new boolean[] { true, false })
1454 >        for (boolean fFirst : new boolean[] { true, false })
1455 >        for (Integer v1 : new Integer[] { 1, null })
1456 >    {
1457 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1458 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1459 >        final SubtractAction r = new SubtractAction();
1460  
1461 <        f = new CompletableFuture<>();
1462 <        g = new CompletableFuture<>();
1463 <        f.complete(3);
1464 <        assertTrue(g.cancel(true));
1465 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1466 <        checkCompletedWithWrappedCancellationException(h);
1461 >        (fFirst ? f : g).complete(v1);
1462 >        if (!createIncomplete)
1463 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1464 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1465 >        if (createIncomplete) {
1466 >            checkIncomplete(h);
1467 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1468 >        }
1469  
941        f = new CompletableFuture<>();
942        g = new CompletableFuture<>();
943        assertTrue(f.cancel(true));
944        g.complete(3);
945        h = f.thenAcceptBoth(g, r = new SubtractAction());
1470          checkCompletedWithWrappedCancellationException(h);
1471 <    }
1471 >        checkCancelled(!fFirst ? f : g);
1472 >        assertEquals(0, r.invocationCount);
1473 >        checkCompletedNormally(fFirst ? f : g, v1);
1474 >    }}
1475  
1476      /**
1477       * runAfterBoth result completes normally after normal
1478       * completion of sources
1479       */
1480 <    public void testRunAfterBoth_normalCompletion1() {
1480 >    public void testRunAfterBoth_normalCompletion() {
1481 >        for (ExecutionMode m : ExecutionMode.values())
1482 >        for (boolean createIncomplete : new boolean[] { true, false })
1483 >        for (boolean fFirst : new boolean[] { true, false })
1484          for (Integer v1 : new Integer[] { 1, null })
1485 <        for (Integer v2 : new Integer[] { 2, null }) {
1486 <
1485 >        for (Integer v2 : new Integer[] { 2, null })
1486 >    {
1487          final CompletableFuture<Integer> f = new CompletableFuture<>();
1488          final CompletableFuture<Integer> g = new CompletableFuture<>();
1489 <        final Noop r = new Noop();
960 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1489 >        final Noop r = new Noop(m);
1490  
1491 <        f.complete(v1);
1492 <        checkIncomplete(h);
1493 <        assertFalse(r.ran);
1494 <        g.complete(v2);
1491 >        if (fFirst) f.complete(v1); else g.complete(v2);
1492 >        if (!createIncomplete)
1493 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1494 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1495 >        if (createIncomplete) {
1496 >            checkIncomplete(h);
1497 >            assertEquals(0, r.invocationCount);
1498 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1499 >        }
1500  
1501          checkCompletedNormally(h, null);
1502 <        assertTrue(r.ran);
1502 >        assertEquals(1, r.invocationCount);
1503          checkCompletedNormally(f, v1);
1504          checkCompletedNormally(g, v2);
1505 <        }
972 <    }
1505 >    }}
1506  
1507 <    public void testRunAfterBoth_normalCompletion2() {
1507 >    /**
1508 >     * runAfterBoth result completes exceptionally after exceptional
1509 >     * completion of either source
1510 >     */
1511 >    public void testRunAfterBoth_exceptionalCompletion() {
1512 >        for (ExecutionMode m : ExecutionMode.values())
1513 >        for (boolean createIncomplete : new boolean[] { true, false })
1514 >        for (boolean fFirst : new boolean[] { true, false })
1515          for (Integer v1 : new Integer[] { 1, null })
1516 <        for (Integer v2 : new Integer[] { 2, null }) {
977 <
1516 >    {
1517          final CompletableFuture<Integer> f = new CompletableFuture<>();
1518          final CompletableFuture<Integer> g = new CompletableFuture<>();
1519 <        final Noop r = new Noop();
1520 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
982 <
983 <        g.complete(v2);
984 <        checkIncomplete(h);
985 <        assertFalse(r.ran);
986 <        f.complete(v1);
1519 >        final CFException ex = new CFException();
1520 >        final Noop r = new Noop(m);
1521  
1522 <        checkCompletedNormally(h, null);
1523 <        assertTrue(r.ran);
1524 <        checkCompletedNormally(f, v1);
1525 <        checkCompletedNormally(g, v2);
1522 >        (fFirst ? f : g).complete(v1);
1523 >        if (!createIncomplete)
1524 >            (!fFirst ? f : g).completeExceptionally(ex);
1525 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1526 >        if (createIncomplete) {
1527 >            checkIncomplete(h);
1528 >            (!fFirst ? f : g).completeExceptionally(ex);
1529          }
993    }
1530  
1531 <    public void testRunAfterBoth_normalCompletion3() {
1532 <        for (Integer v1 : new Integer[] { 1, null })
1533 <        for (Integer v2 : new Integer[] { 2, null }) {
1531 >        checkCompletedWithWrappedCFException(h, ex);
1532 >        assertEquals(0, r.invocationCount);
1533 >        checkCompletedNormally(fFirst ? f : g, v1);
1534 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1535 >    }}
1536  
1537 +    /**
1538 +     * runAfterBoth result completes exceptionally if action does
1539 +     */
1540 +    public void testRunAfterBoth_actionFailed() {
1541 +        for (ExecutionMode m : ExecutionMode.values())
1542 +        for (boolean fFirst : new boolean[] { true, false })
1543 +        for (Integer v1 : new Integer[] { 1, null })
1544 +        for (Integer v2 : new Integer[] { 2, null })
1545 +    {
1546          final CompletableFuture<Integer> f = new CompletableFuture<>();
1547          final CompletableFuture<Integer> g = new CompletableFuture<>();
1548 <        final Noop r = new Noop();
1548 >        final FailingRunnable r = new FailingRunnable();
1549  
1550 <        g.complete(v2);
1551 <        f.complete(v1);
1552 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1550 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1551 >        if (fFirst) {
1552 >            f.complete(v1);
1553 >            g.complete(v2);
1554 >        } else {
1555 >            g.complete(v2);
1556 >            f.complete(v1);
1557 >        }
1558 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1559  
1560 <        checkCompletedNormally(h, null);
1561 <        assertTrue(r.ran);
1560 >        checkCompletedWithWrappedCFException(h1);
1561 >        checkCompletedWithWrappedCFException(h2);
1562          checkCompletedNormally(f, v1);
1563          checkCompletedNormally(g, v2);
1564 <        }
1012 <    }
1564 >    }}
1565  
1566 <    public void testRunAfterBoth_normalCompletion4() {
1566 >    /**
1567 >     * runAfterBoth result completes exceptionally if either source cancelled
1568 >     */
1569 >    public void testRunAfterBoth_sourceCancelled() {
1570 >        for (ExecutionMode m : ExecutionMode.values())
1571 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1572 >        for (boolean createIncomplete : new boolean[] { true, false })
1573 >        for (boolean fFirst : new boolean[] { true, false })
1574          for (Integer v1 : new Integer[] { 1, null })
1575 <        for (Integer v2 : new Integer[] { 2, null }) {
1017 <
1575 >    {
1576          final CompletableFuture<Integer> f = new CompletableFuture<>();
1577          final CompletableFuture<Integer> g = new CompletableFuture<>();
1578 <        final Noop r = new Noop();
1578 >        final Noop r = new Noop(m);
1579  
1022        f.complete(v1);
1023        g.complete(v2);
1024        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1580  
1581 <        checkCompletedNormally(h, null);
1582 <        assertTrue(r.ran);
1583 <        checkCompletedNormally(f, v1);
1584 <        checkCompletedNormally(g, v2);
1581 >        (fFirst ? f : g).complete(v1);
1582 >        if (!createIncomplete)
1583 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1584 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1585 >        if (createIncomplete) {
1586 >            checkIncomplete(h);
1587 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1588          }
1589 <    }
1589 >
1590 >        checkCompletedWithWrappedCancellationException(h);
1591 >        checkCancelled(!fFirst ? f : g);
1592 >        assertEquals(0, r.invocationCount);
1593 >        checkCompletedNormally(fFirst ? f : g, v1);
1594 >    }}
1595  
1596      /**
1597 <     * runAfterBoth result completes exceptionally after exceptional
1598 <     * completion of either source
1597 >     * applyToEither result completes normally after normal completion
1598 >     * of either source
1599       */
1600 <    public void testRunAfterBoth_exceptionalCompletion1() {
1601 <        for (Integer v1 : new Integer[] { 1, null }) {
1602 <
1600 >    public void testApplyToEither_normalCompletion() {
1601 >        for (ExecutionMode m : ExecutionMode.values())
1602 >        for (boolean createIncomplete : new boolean[] { true, false })
1603 >        for (boolean fFirst : new boolean[] { true, false })
1604 >        for (Integer v1 : new Integer[] { 1, null })
1605 >        for (Integer v2 : new Integer[] { 2, null })
1606 >    {
1607          final CompletableFuture<Integer> f = new CompletableFuture<>();
1608          final CompletableFuture<Integer> g = new CompletableFuture<>();
1609 <        final Noop r = new Noop();
1043 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1044 <        final CFException ex = new CFException();
1609 >        final IncFunction r = new IncFunction();
1610  
1611 <        f.completeExceptionally(ex);
1612 <        checkIncomplete(h);
1613 <        g.complete(v1);
1614 <
1615 <        checkCompletedWithWrappedCFException(h, ex);
1616 <        checkCompletedWithWrappedCFException(f, ex);
1617 <        assertFalse(r.ran);
1053 <        checkCompletedNormally(g, v1);
1611 >        if (!createIncomplete)
1612 >            if (fFirst) f.complete(v1); else g.complete(v2);
1613 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1614 >        if (createIncomplete) {
1615 >            checkIncomplete(h);
1616 >            assertEquals(0, r.invocationCount);
1617 >            if (fFirst) f.complete(v1); else g.complete(v2);
1618          }
1619 <    }
1619 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1620 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1621  
1622 <    public void testRunAfterBoth_exceptionalCompletion2() {
1623 <        for (Integer v1 : new Integer[] { 1, null }) {
1622 >        checkCompletedNormally(f, v1);
1623 >        checkCompletedNormally(g, v2);
1624 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1625 >    }}
1626  
1627 +    public void testApplyToEither_normalCompletionBothAvailable() {
1628 +        for (ExecutionMode m : ExecutionMode.values())
1629 +        for (boolean fFirst : new boolean[] { true, false })
1630 +        for (Integer v1 : new Integer[] { 1, null })
1631 +        for (Integer v2 : new Integer[] { 2, null })
1632 +    {
1633          final CompletableFuture<Integer> f = new CompletableFuture<>();
1634          final CompletableFuture<Integer> g = new CompletableFuture<>();
1635 <        final Noop r = new Noop();
1063 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1064 <        final CFException ex = new CFException();
1635 >        final IncFunction r = new IncFunction();
1636  
1637 <        g.completeExceptionally(ex);
1638 <        checkIncomplete(h);
1639 <        f.complete(v1);
1637 >        if (fFirst) {
1638 >            f.complete(v1);
1639 >            g.complete(v2);
1640 >        } else {
1641 >            g.complete(v2);
1642 >            f.complete(v1);
1643 >        }
1644 >
1645 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1646  
1070        checkCompletedWithWrappedCFException(h, ex);
1071        checkCompletedWithWrappedCFException(g, ex);
1072        assertFalse(r.ran);
1647          checkCompletedNormally(f, v1);
1648 <        }
1075 <    }
1648 >        checkCompletedNormally(g, v2);
1649  
1650 <    public void testRunAfterBoth_exceptionalCompletion3() {
1651 <        for (Integer v1 : new Integer[] { 1, null }) {
1650 >        // unspecified behavior
1651 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1652 >                   Objects.equals(h.join(), inc(v2)));
1653 >        assertEquals(1, r.invocationCount);
1654 >    }}
1655  
1656 +    /**
1657 +     * applyToEither result completes exceptionally after exceptional
1658 +     * completion of either source
1659 +     */
1660 +    public void testApplyToEither_exceptionalCompletion1() {
1661 +        for (ExecutionMode m : ExecutionMode.values())
1662 +        for (boolean createIncomplete : new boolean[] { true, false })
1663 +        for (boolean fFirst : new boolean[] { true, false })
1664 +        for (Integer v1 : new Integer[] { 1, null })
1665 +    {
1666          final CompletableFuture<Integer> f = new CompletableFuture<>();
1667          final CompletableFuture<Integer> g = new CompletableFuture<>();
1082        final Noop r = new Noop();
1668          final CFException ex = new CFException();
1669 +        final IncFunction r = new IncFunction();
1670  
1671 <        g.completeExceptionally(ex);
1672 <        f.complete(v1);
1673 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1671 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1672 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1673 >        if (createIncomplete) {
1674 >            checkIncomplete(h);
1675 >            assertEquals(0, r.invocationCount);
1676 >            (fFirst ? f : g).completeExceptionally(ex);
1677 >        }
1678  
1679          checkCompletedWithWrappedCFException(h, ex);
1680 <        checkCompletedWithWrappedCFException(g, ex);
1091 <        assertFalse(r.ran);
1092 <        checkCompletedNormally(f, v1);
1093 <        }
1094 <    }
1680 >        (!fFirst ? f : g).complete(v1);
1681  
1682 <    public void testRunAfterBoth_exceptionalCompletion4() {
1683 <        for (Integer v1 : new Integer[] { 1, null }) {
1682 >        assertEquals(0, r.invocationCount);
1683 >        checkCompletedNormally(!fFirst ? f : g, v1);
1684 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1685 >        checkCompletedWithWrappedCFException(h, ex);
1686 >    }}
1687  
1688 +    public void testApplyToEither_exceptionalCompletion2() {
1689 +        for (ExecutionMode m : ExecutionMode.values())
1690 +        for (boolean reverseArgs : new boolean[] { true, false })
1691 +        for (boolean fFirst : new boolean[] { true, false })
1692 +        for (Integer v1 : new Integer[] { 1, null })
1693 +    {
1694          final CompletableFuture<Integer> f = new CompletableFuture<>();
1695          final CompletableFuture<Integer> g = new CompletableFuture<>();
1696 <        final Noop r = new Noop();
1696 >        final IncFunction r1 = new IncFunction();
1697 >        final IncFunction r2 = new IncFunction();
1698          final CFException ex = new CFException();
1699 +        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1700 +        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1701 +        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1702 +        if (fFirst) {
1703 +            f.complete(v1);
1704 +            g.completeExceptionally(ex);
1705 +        } else {
1706 +            g.completeExceptionally(ex);
1707 +            f.complete(v1);
1708 +        }
1709 +        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1710 +
1711 +        // unspecified behavior
1712 +        try {
1713 +            assertEquals(inc(v1), h1.join());
1714 +            assertEquals(1, r1.invocationCount);
1715 +        } catch (CompletionException ok) {
1716 +            checkCompletedWithWrappedCFException(h1, ex);
1717 +            assertEquals(0, r1.invocationCount);
1718 +        }
1719  
1720 <        f.completeExceptionally(ex);
1721 <        g.complete(v1);
1722 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1723 <
1724 <        checkCompletedWithWrappedCFException(h, ex);
1725 <        checkCompletedWithWrappedCFException(f, ex);
1110 <        assertFalse(r.ran);
1111 <        checkCompletedNormally(g, v1);
1720 >        try {
1721 >            assertEquals(inc(v1), h2.join());
1722 >            assertEquals(1, r2.invocationCount);
1723 >        } catch (CompletionException ok) {
1724 >            checkCompletedWithWrappedCFException(h2, ex);
1725 >            assertEquals(0, r2.invocationCount);
1726          }
1727 <    }
1727 >
1728 >        checkCompletedWithWrappedCFException(g, ex);
1729 >        checkCompletedNormally(f, v1);
1730 >    }}
1731  
1732      /**
1733 <     * runAfterBoth result completes exceptionally if action does
1733 >     * applyToEither result completes exceptionally if action does
1734       */
1735 <    public void testRunAfterBoth_actionFailed1() {
1735 >    public void testApplyToEither_actionFailed1() {
1736 >        for (ExecutionMode m : ExecutionMode.values())
1737          for (Integer v1 : new Integer[] { 1, null })
1738 <        for (Integer v2 : new Integer[] { 2, null }) {
1739 <
1738 >        for (Integer v2 : new Integer[] { 2, null })
1739 >    {
1740          final CompletableFuture<Integer> f = new CompletableFuture<>();
1741          final CompletableFuture<Integer> g = new CompletableFuture<>();
1742 <        final FailingNoop r = new FailingNoop();
1743 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1742 >        final FailingFunction r = new FailingFunction();
1743 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1744  
1745          f.complete(v1);
1128        checkIncomplete(h);
1129        g.complete(v2);
1130
1746          checkCompletedWithWrappedCFException(h);
1747 +        g.complete(v2);
1748          checkCompletedNormally(f, v1);
1749          checkCompletedNormally(g, v2);
1750 <        }
1135 <    }
1750 >    }}
1751  
1752 <    public void testRunAfterBoth_actionFailed2() {
1752 >    public void testApplyToEither_actionFailed2() {
1753 >        for (ExecutionMode m : ExecutionMode.values())
1754          for (Integer v1 : new Integer[] { 1, null })
1755 <        for (Integer v2 : new Integer[] { 2, null }) {
1756 <
1755 >        for (Integer v2 : new Integer[] { 2, null })
1756 >    {
1757          final CompletableFuture<Integer> f = new CompletableFuture<>();
1758          final CompletableFuture<Integer> g = new CompletableFuture<>();
1759 <        final FailingNoop r = new FailingNoop();
1760 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1759 >        final FailingFunction r = new FailingFunction();
1760 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1761  
1762          g.complete(v2);
1147        checkIncomplete(h);
1148        f.complete(v1);
1149
1763          checkCompletedWithWrappedCFException(h);
1764 +        f.complete(v1);
1765          checkCompletedNormally(f, v1);
1766          checkCompletedNormally(g, v2);
1767 <        }
1154 <    }
1767 >    }}
1768  
1769      /**
1770 <     * runAfterBoth result completes exceptionally if either source cancelled
1770 >     * applyToEither result completes exceptionally if either source cancelled
1771       */
1772 <    public void testRunAfterBoth_sourceCancelled1() {
1772 >    public void testApplyToEither_sourceCancelled1() {
1773 >        for (ExecutionMode m : ExecutionMode.values())
1774          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1775 <        for (Integer v1 : new Integer[] { 1, null }) {
1776 <
1775 >        for (boolean createIncomplete : new boolean[] { true, false })
1776 >        for (boolean fFirst : new boolean[] { true, false })
1777 >        for (Integer v1 : new Integer[] { 1, null })
1778 >    {
1779          final CompletableFuture<Integer> f = new CompletableFuture<>();
1780          final CompletableFuture<Integer> g = new CompletableFuture<>();
1781 <        final Noop r = new Noop();
1166 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1781 >        final IncFunction r = new IncFunction();
1782  
1783 <        assertTrue(f.cancel(mayInterruptIfRunning));
1784 <        checkIncomplete(h);
1785 <        g.complete(v1);
1783 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1784 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1785 >        if (createIncomplete) {
1786 >            checkIncomplete(h);
1787 >            assertEquals(0, r.invocationCount);
1788 >            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1789 >        }
1790  
1791          checkCompletedWithWrappedCancellationException(h);
1792 <        checkCancelled(f);
1174 <        assertFalse(r.ran);
1175 <        checkCompletedNormally(g, v1);
1176 <        }
1177 <    }
1792 >        (!fFirst ? f : g).complete(v1);
1793  
1794 <    public void testRunAfterBoth_sourceCancelled2() {
1795 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1796 <        for (Integer v1 : new Integer[] { 1, null }) {
1794 >        assertEquals(0, r.invocationCount);
1795 >        checkCompletedNormally(!fFirst ? f : g, v1);
1796 >        checkCancelled(fFirst ? f : g);
1797 >        checkCompletedWithWrappedCancellationException(h);
1798 >    }}
1799  
1800 +    public void testApplyToEither_sourceCancelled2() {
1801 +        for (ExecutionMode m : ExecutionMode.values())
1802 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1803 +        for (boolean reverseArgs : new boolean[] { true, false })
1804 +        for (boolean fFirst : new boolean[] { true, false })
1805 +        for (Integer v1 : new Integer[] { 1, null })
1806 +    {
1807          final CompletableFuture<Integer> f = new CompletableFuture<>();
1808          final CompletableFuture<Integer> g = new CompletableFuture<>();
1809 <        final Noop r = new Noop();
1810 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1809 >        final IncFunction r1 = new IncFunction();
1810 >        final IncFunction r2 = new IncFunction();
1811 >        final CFException ex = new CFException();
1812 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1813 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1814  
1815 <        assertTrue(g.cancel(mayInterruptIfRunning));
1816 <        checkIncomplete(h);
1817 <        f.complete(v1);
1815 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1816 >        if (fFirst) {
1817 >            f.complete(v1);
1818 >            assertTrue(g.cancel(mayInterruptIfRunning));
1819 >        } else {
1820 >            assertTrue(g.cancel(mayInterruptIfRunning));
1821 >            f.complete(v1);
1822 >        }
1823 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1824 >
1825 >        // unspecified behavior
1826 >        try {
1827 >            assertEquals(inc(v1), h1.join());
1828 >            assertEquals(1, r1.invocationCount);
1829 >        } catch (CompletionException ok) {
1830 >            checkCompletedWithWrappedCancellationException(h1);
1831 >            assertEquals(0, r1.invocationCount);
1832 >        }
1833  
1834 <        checkCompletedWithWrappedCancellationException(h);
1835 <        checkCancelled(g);
1836 <        assertFalse(r.ran);
1837 <        checkCompletedNormally(f, v1);
1834 >        try {
1835 >            assertEquals(inc(v1), h2.join());
1836 >            assertEquals(1, r2.invocationCount);
1837 >        } catch (CompletionException ok) {
1838 >            checkCompletedWithWrappedCancellationException(h2);
1839 >            assertEquals(0, r2.invocationCount);
1840          }
1197    }
1841  
1842 <    public void testRunAfterBoth_sourceCancelled3() {
1843 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1844 <        for (Integer v1 : new Integer[] { 1, null }) {
1842 >        checkCancelled(g);
1843 >        checkCompletedNormally(f, v1);
1844 >    }}
1845  
1846 +    /**
1847 +     * acceptEither result completes normally after normal completion
1848 +     * of either source
1849 +     */
1850 +    public void testAcceptEither_normalCompletion1() {
1851 +        for (ExecutionMode m : ExecutionMode.values())
1852 +        for (Integer v1 : new Integer[] { 1, null })
1853 +        for (Integer v2 : new Integer[] { 2, null })
1854 +    {
1855          final CompletableFuture<Integer> f = new CompletableFuture<>();
1856          final CompletableFuture<Integer> g = new CompletableFuture<>();
1857 <        final Noop r = new Noop();
1857 >        final IncAction r = new IncAction();
1858 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1859  
1207        assertTrue(g.cancel(mayInterruptIfRunning));
1860          f.complete(v1);
1861 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1861 >        checkCompletedNormally(h, null);
1862 >        assertEquals(inc(v1), r.value);
1863 >        g.complete(v2);
1864  
1211        checkCompletedWithWrappedCancellationException(h);
1212        checkCancelled(g);
1213        assertFalse(r.ran);
1865          checkCompletedNormally(f, v1);
1866 <        }
1867 <    }
1868 <
1218 <    public void testRunAfterBoth_sourceCancelled4() {
1219 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1220 <        for (Integer v1 : new Integer[] { 1, null }) {
1866 >        checkCompletedNormally(g, v2);
1867 >        checkCompletedNormally(h, null);
1868 >    }}
1869  
1870 +    public void testAcceptEither_normalCompletion2() {
1871 +        for (ExecutionMode m : ExecutionMode.values())
1872 +        for (Integer v1 : new Integer[] { 1, null })
1873 +        for (Integer v2 : new Integer[] { 2, null })
1874 +    {
1875          final CompletableFuture<Integer> f = new CompletableFuture<>();
1876          final CompletableFuture<Integer> g = new CompletableFuture<>();
1877 <        final Noop r = new Noop();
1878 <
1226 <        assertTrue(f.cancel(mayInterruptIfRunning));
1227 <        g.complete(v1);
1228 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1229 <
1230 <        checkCompletedWithWrappedCancellationException(h);
1231 <        checkCancelled(f);
1232 <        assertFalse(r.ran);
1233 <        checkCompletedNormally(g, v1);
1234 <        }
1235 <    }
1877 >        final IncAction r = new IncAction();
1878 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1879  
1880 <    /**
1881 <     * applyToEither result completes normally after normal completion
1882 <     * of either source
1883 <     */
1241 <    public void testApplyToEither() {
1242 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1243 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1244 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1245 <        f.complete(one);
1246 <        checkCompletedNormally(g, two);
1247 <        f2.complete(one);
1248 <        checkCompletedNormally(g, two);
1249 <
1250 <        f = new CompletableFuture<>();
1251 <        f.complete(one);
1252 <        f2 = new CompletableFuture<>();
1253 <        g = f.applyToEither(f2, inc);
1254 <        checkCompletedNormally(g, two);
1255 <    }
1256 <
1257 <    /**
1258 <     * applyToEither result completes exceptionally after exceptional
1259 <     * completion of either source
1260 <     */
1261 <    public void testApplyToEither2() {
1262 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1263 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1264 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1265 <        f.completeExceptionally(new CFException());
1266 <        f2.complete(one);
1267 <        checkCompletedWithWrappedCFException(g);
1880 >        g.complete(v2);
1881 >        checkCompletedNormally(h, null);
1882 >        assertEquals(inc(v2), r.value);
1883 >        f.complete(v1);
1884  
1885 <        f = new CompletableFuture<>();
1886 <        f2 = new CompletableFuture<>();
1887 <        f2.completeExceptionally(new CFException());
1888 <        g = f.applyToEither(f2, inc);
1273 <        checkCompletedWithWrappedCFException(g);
1274 <    }
1885 >        checkCompletedNormally(f, v1);
1886 >        checkCompletedNormally(g, v2);
1887 >        checkCompletedNormally(h, null);
1888 >    }}
1889  
1890 <    /**
1891 <     * applyToEither result completes exceptionally if action does
1892 <     */
1893 <    public void testApplyToEither3() {
1894 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1895 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1896 <        FailingFunction r = new FailingFunction();
1897 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1284 <        f2.complete(two);
1285 <        checkCompletedWithWrappedCFException(g);
1286 <    }
1890 >    public void testAcceptEither_normalCompletion3() {
1891 >        for (ExecutionMode m : ExecutionMode.values())
1892 >        for (Integer v1 : new Integer[] { 1, null })
1893 >        for (Integer v2 : new Integer[] { 2, null })
1894 >    {
1895 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1896 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1897 >        final IncAction r = new IncAction();
1898  
1899 <    /**
1900 <     * applyToEither result completes exceptionally if either source cancelled
1901 <     */
1291 <    public void testApplyToEither4() {
1292 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1293 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1294 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1295 <        assertTrue(f.cancel(true));
1296 <        checkCompletedWithWrappedCancellationException(g);
1297 <        f = new CompletableFuture<>();
1298 <        f2 = new CompletableFuture<>();
1299 <        assertTrue(f2.cancel(true));
1300 <        checkCompletedWithWrappedCancellationException(g);
1301 <    }
1899 >        f.complete(v1);
1900 >        g.complete(v2);
1901 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1902  
1903 <    /**
1904 <     * acceptEither result completes normally after normal completion
1905 <     * of either source
1306 <     */
1307 <    public void testAcceptEither() {
1308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1309 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1310 <        IncAction r = new IncAction();
1311 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1312 <        f.complete(one);
1313 <        checkCompletedNormally(g, null);
1314 <        f2.complete(one);
1315 <        checkCompletedNormally(g, null);
1316 <        assertEquals(r.value, 2);
1903 >        checkCompletedNormally(h, null);
1904 >        checkCompletedNormally(f, v1);
1905 >        checkCompletedNormally(g, v2);
1906  
1907 <        r = new IncAction();
1908 <        f = new CompletableFuture<>();
1909 <        f.complete(one);
1910 <        f2 = new CompletableFuture<>();
1322 <        g = f.acceptEither(f2, r);
1323 <        checkCompletedNormally(g, null);
1324 <        assertEquals(r.value, 2);
1325 <    }
1907 >        // unspecified behavior
1908 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
1909 >                   Objects.equals(r.value, inc(v2)));
1910 >    }}
1911  
1912      /**
1913       * acceptEither result completes exceptionally after exceptional
1914       * completion of either source
1915       */
1916 <    public void testAcceptEither2() {
1917 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1918 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1919 <        IncAction r = new IncAction();
1920 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1921 <        f.completeExceptionally(new CFException());
1922 <        f2.complete(one);
1923 <        checkCompletedWithWrappedCFException(g);
1924 <
1340 <        r = new IncAction();
1341 <        f = new CompletableFuture<>();
1342 <        f2 = new CompletableFuture<>();
1343 <        f2.completeExceptionally(new CFException());
1344 <        g = f.acceptEither(f2, r);
1345 <        checkCompletedWithWrappedCFException(g);
1346 <    }
1347 <
1348 <    /**
1349 <     * acceptEither result completes exceptionally if action does
1350 <     */
1351 <    public void testAcceptEither3() {
1352 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1353 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1354 <        FailingConsumer r = new FailingConsumer();
1355 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1356 <        f2.complete(two);
1357 <        checkCompletedWithWrappedCFException(g);
1358 <    }
1359 <
1360 <    /**
1361 <     * acceptEither result completes exceptionally if either source cancelled
1362 <     */
1363 <    public void testAcceptEither4() {
1364 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1365 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1366 <        IncAction r = new IncAction();
1367 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1368 <        assertTrue(f.cancel(true));
1369 <        checkCompletedWithWrappedCancellationException(g);
1370 <        f = new CompletableFuture<>();
1371 <        f2 = new CompletableFuture<>();
1372 <        assertTrue(f2.cancel(true));
1373 <        checkCompletedWithWrappedCancellationException(g);
1374 <    }
1375 <
1376 <    /**
1377 <     * runAfterEither result completes normally after normal completion
1378 <     * of either source
1379 <     */
1380 <    public void testRunAfterEither() {
1381 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1382 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1383 <        Noop r = new Noop();
1384 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1385 <        f.complete(one);
1386 <        checkCompletedNormally(g, null);
1387 <        f2.complete(one);
1388 <        checkCompletedNormally(g, null);
1389 <        assertTrue(r.ran);
1390 <
1391 <        r = new Noop();
1392 <        f = new CompletableFuture<>();
1393 <        f.complete(one);
1394 <        f2 = new CompletableFuture<>();
1395 <        g = f.runAfterEither(f2, r);
1396 <        checkCompletedNormally(g, null);
1397 <        assertTrue(r.ran);
1398 <    }
1399 <
1400 <    /**
1401 <     * runAfterEither result completes exceptionally after exceptional
1402 <     * completion of either source
1403 <     */
1404 <    public void testRunAfterEither2() {
1405 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1406 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1407 <        Noop r = new Noop();
1408 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1409 <        f.completeExceptionally(new CFException());
1410 <        f2.complete(one);
1411 <        checkCompletedWithWrappedCFException(g);
1412 <
1413 <        r = new Noop();
1414 <        f = new CompletableFuture<>();
1415 <        f2 = new CompletableFuture<>();
1416 <        f2.completeExceptionally(new CFException());
1417 <        g = f.runAfterEither(f2, r);
1418 <        checkCompletedWithWrappedCFException(g);
1419 <    }
1420 <
1421 <    /**
1422 <     * runAfterEither result completes exceptionally if action does
1423 <     */
1424 <    public void testRunAfterEither3() {
1425 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1426 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1427 <        FailingNoop r = new FailingNoop();
1428 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1429 <        f2.complete(two);
1430 <        checkCompletedWithWrappedCFException(g);
1431 <    }
1432 <
1433 <    /**
1434 <     * runAfterEither result completes exceptionally if either source cancelled
1435 <     */
1436 <    public void testRunAfterEither4() {
1437 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1438 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1439 <        Noop r = new Noop();
1440 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1441 <        assertTrue(f.cancel(true));
1442 <        checkCompletedWithWrappedCancellationException(g);
1443 <        f = new CompletableFuture<>();
1444 <        f2 = new CompletableFuture<>();
1445 <        assertTrue(f2.cancel(true));
1446 <        checkCompletedWithWrappedCancellationException(g);
1447 <    }
1448 <
1449 <    /**
1450 <     * thenCompose result completes normally after normal completion of source
1451 <     */
1452 <    public void testThenCompose() {
1453 <        CompletableFuture<Integer> f, g;
1454 <        CompletableFutureInc r;
1455 <
1456 <        f = new CompletableFuture<>();
1457 <        g = f.thenCompose(r = new CompletableFutureInc());
1458 <        f.complete(one);
1459 <        checkCompletedNormally(g, two);
1460 <        assertTrue(r.ran);
1461 <
1462 <        f = new CompletableFuture<>();
1463 <        f.complete(one);
1464 <        g = f.thenCompose(r = new CompletableFutureInc());
1465 <        checkCompletedNormally(g, two);
1466 <        assertTrue(r.ran);
1467 <    }
1468 <
1469 <    /**
1470 <     * thenCompose result completes exceptionally after exceptional
1471 <     * completion of source
1472 <     */
1473 <    public void testThenCompose2() {
1474 <        CompletableFuture<Integer> f, g;
1475 <        CompletableFutureInc r;
1476 <
1477 <        f = new CompletableFuture<>();
1478 <        g = f.thenCompose(r = new CompletableFutureInc());
1479 <        f.completeExceptionally(new CFException());
1480 <        checkCompletedWithWrappedCFException(g);
1481 <
1482 <        f = new CompletableFuture<>();
1483 <        f.completeExceptionally(new CFException());
1484 <        g = f.thenCompose(r = new CompletableFutureInc());
1485 <        checkCompletedWithWrappedCFException(g);
1486 <    }
1487 <
1488 <    /**
1489 <     * thenCompose result completes exceptionally if action does
1490 <     */
1491 <    public void testThenCompose3() {
1492 <        CompletableFuture<Integer> f, g;
1493 <        FailingCompletableFutureFunction r;
1494 <
1495 <        f = new CompletableFuture<>();
1496 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1497 <        f.complete(one);
1498 <        checkCompletedWithWrappedCFException(g);
1916 >    public void testAcceptEither_exceptionalCompletion1() {
1917 >        for (ExecutionMode m : ExecutionMode.values())
1918 >        for (Integer v1 : new Integer[] { 1, null })
1919 >    {
1920 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1921 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1922 >        final IncAction r = new IncAction();
1923 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1924 >        final CFException ex = new CFException();
1925  
1926 <        f = new CompletableFuture<>();
1927 <        f.complete(one);
1928 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1503 <        checkCompletedWithWrappedCFException(g);
1504 <    }
1926 >        f.completeExceptionally(ex);
1927 >        checkCompletedWithWrappedCFException(h, ex);
1928 >        g.complete(v1);
1929  
1930 <    /**
1931 <     * thenCompose result completes exceptionally if source cancelled
1932 <     */
1933 <    public void testThenCompose4() {
1934 <        CompletableFuture<Integer> f, g;
1511 <        CompletableFutureInc r;
1930 >        assertEquals(0, r.invocationCount);
1931 >        checkCompletedNormally(g, v1);
1932 >        checkCompletedWithWrappedCFException(f, ex);
1933 >        checkCompletedWithWrappedCFException(h, ex);
1934 >    }}
1935  
1936 <        f = new CompletableFuture<>();
1937 <        g = f.thenCompose(r = new CompletableFutureInc());
1938 <        assertTrue(f.cancel(true));
1939 <        checkCompletedWithWrappedCancellationException(g);
1936 >    public void testAcceptEither_exceptionalCompletion2() {
1937 >        for (ExecutionMode m : ExecutionMode.values())
1938 >        for (Integer v1 : new Integer[] { 1, null })
1939 >    {
1940 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1941 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1942 >        final IncAction r = new IncAction();
1943 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1944 >        final CFException ex = new CFException();
1945  
1946 <        f = new CompletableFuture<>();
1947 <        assertTrue(f.cancel(true));
1948 <        g = f.thenCompose(r = new CompletableFutureInc());
1521 <        checkCompletedWithWrappedCancellationException(g);
1522 <    }
1946 >        g.completeExceptionally(ex);
1947 >        checkCompletedWithWrappedCFException(h, ex);
1948 >        f.complete(v1);
1949  
1950 <    // asyncs
1950 >        assertEquals(0, r.invocationCount);
1951 >        checkCompletedNormally(f, v1);
1952 >        checkCompletedWithWrappedCFException(g, ex);
1953 >        checkCompletedWithWrappedCFException(h, ex);
1954 >    }}
1955  
1956 <    /**
1957 <     * thenRunAsync result completes normally after normal completion of source
1958 <     */
1959 <    public void testThenRunAsync() {
1960 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1961 <        Noop r = new Noop();
1962 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1963 <        f.complete(null);
1534 <        checkCompletedNormally(g, null);
1956 >    public void testAcceptEither_exceptionalCompletion3() {
1957 >        for (ExecutionMode m : ExecutionMode.values())
1958 >        for (Integer v1 : new Integer[] { 1, null })
1959 >    {
1960 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1961 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1962 >        final IncAction r = new IncAction();
1963 >        final CFException ex = new CFException();
1964  
1965 <        // reordered version
1966 <        f = new CompletableFuture<>();
1967 <        f.complete(null);
1539 <        r = new Noop();
1540 <        g = f.thenRunAsync(r);
1541 <        checkCompletedNormally(g, null);
1542 <    }
1965 >        g.completeExceptionally(ex);
1966 >        f.complete(v1);
1967 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1968  
1969 <    /**
1970 <     * thenRunAsync result completes exceptionally after exceptional
1546 <     * completion of source
1547 <     */
1548 <    public void testThenRunAsync2() {
1549 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1550 <        Noop r = new Noop();
1551 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1552 <        f.completeExceptionally(new CFException());
1969 >        // unspecified behavior
1970 >        Integer v;
1971          try {
1972 <            g.join();
1973 <            shouldThrow();
1974 <        } catch (CompletionException success) {}
1975 <        checkCompletedWithWrappedCFException(g);
1976 <    }
1977 <
1978 <    /**
1561 <     * thenRunAsync result completes exceptionally if action does
1562 <     */
1563 <    public void testThenRunAsync3() {
1564 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1565 <        FailingNoop r = new FailingNoop();
1566 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1567 <        f.complete(null);
1568 <        checkCompletedWithWrappedCFException(g);
1569 <    }
1570 <
1571 <    /**
1572 <     * thenRunAsync result completes exceptionally if source cancelled
1573 <     */
1574 <    public void testThenRunAsync4() {
1575 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1576 <        Noop r = new Noop();
1577 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1578 <        assertTrue(f.cancel(true));
1579 <        checkCompletedWithWrappedCancellationException(g);
1580 <    }
1581 <
1582 <    /**
1583 <     * thenApplyAsync result completes normally after normal completion of source
1584 <     */
1585 <    public void testThenApplyAsync() {
1586 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1587 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1588 <        f.complete(one);
1589 <        checkCompletedNormally(g, two);
1590 <    }
1591 <
1592 <    /**
1593 <     * thenApplyAsync result completes exceptionally after exceptional
1594 <     * completion of source
1595 <     */
1596 <    public void testThenApplyAsync2() {
1597 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1598 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1599 <        f.completeExceptionally(new CFException());
1600 <        checkCompletedWithWrappedCFException(g);
1601 <    }
1602 <
1603 <    /**
1604 <     * thenApplyAsync result completes exceptionally if action does
1605 <     */
1606 <    public void testThenApplyAsync3() {
1607 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1608 <        FailingFunction r = new FailingFunction();
1609 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1610 <        f.complete(null);
1611 <        checkCompletedWithWrappedCFException(g);
1612 <    }
1613 <
1614 <    /**
1615 <     * thenApplyAsync result completes exceptionally if source cancelled
1616 <     */
1617 <    public void testThenApplyAsync4() {
1618 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1619 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1620 <        assertTrue(f.cancel(true));
1621 <        checkCompletedWithWrappedCancellationException(g);
1622 <    }
1623 <
1624 <    /**
1625 <     * thenAcceptAsync result completes normally after normal
1626 <     * completion of source
1627 <     */
1628 <    public void testThenAcceptAsync() {
1629 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1630 <        IncAction r = new IncAction();
1631 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1632 <        f.complete(one);
1633 <        checkCompletedNormally(g, null);
1634 <        assertEquals(r.value, 2);
1635 <    }
1636 <
1637 <    /**
1638 <     * thenAcceptAsync result completes exceptionally after exceptional
1639 <     * completion of source
1640 <     */
1641 <    public void testThenAcceptAsync2() {
1642 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1643 <        IncAction r = new IncAction();
1644 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1645 <        f.completeExceptionally(new CFException());
1646 <        checkCompletedWithWrappedCFException(g);
1647 <    }
1648 <
1649 <    /**
1650 <     * thenAcceptAsync result completes exceptionally if action does
1651 <     */
1652 <    public void testThenAcceptAsync3() {
1653 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1654 <        FailingConsumer r = new FailingConsumer();
1655 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1656 <        f.complete(null);
1657 <        checkCompletedWithWrappedCFException(g);
1658 <    }
1972 >            assertNull(h.join());
1973 >            assertEquals(1, r.invocationCount);
1974 >            assertEquals(inc(v1), r.value);
1975 >        } catch (CompletionException ok) {
1976 >            checkCompletedWithWrappedCFException(h, ex);
1977 >            assertEquals(0, r.invocationCount);
1978 >        }
1979  
1980 <    /**
1981 <     * thenAcceptAsync result completes exceptionally if source cancelled
1982 <     */
1663 <    public void testThenAcceptAsync4() {
1664 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1665 <        IncAction r = new IncAction();
1666 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1667 <        assertTrue(f.cancel(true));
1668 <        checkCompletedWithWrappedCancellationException(g);
1669 <    }
1980 >        checkCompletedWithWrappedCFException(g, ex);
1981 >        checkCompletedNormally(f, v1);
1982 >    }}
1983  
1984 <    /**
1985 <     * thenCombineAsync result completes normally after normal
1986 <     * completion of sources
1987 <     */
1988 <    public void testThenCombineAsync() {
1989 <        CompletableFuture<Integer> f, g, h;
1984 >    public void testAcceptEither_exceptionalCompletion4() {
1985 >        for (ExecutionMode m : ExecutionMode.values())
1986 >        for (Integer v1 : new Integer[] { 1, null })
1987 >    {
1988 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1989 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1990 >        final IncAction r = new IncAction();
1991 >        final CFException ex = new CFException();
1992  
1993 <        f = new CompletableFuture<>();
1994 <        g = new CompletableFuture<>();
1995 <        h = f.thenCombineAsync(g, subtract);
1681 <        f.complete(3);
1682 <        checkIncomplete(h);
1683 <        g.complete(1);
1684 <        checkCompletedNormally(h, 2);
1993 >        f.completeExceptionally(ex);
1994 >        g.complete(v1);
1995 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1996  
1997 <        f = new CompletableFuture<>();
1998 <        g = new CompletableFuture<>();
1999 <        h = f.thenCombineAsync(g, subtract);
2000 <        g.complete(1);
2001 <        checkIncomplete(h);
2002 <        f.complete(3);
2003 <        checkCompletedNormally(h, 2);
1997 >        // unspecified behavior
1998 >        Integer v;
1999 >        try {
2000 >            assertNull(h.join());
2001 >            assertEquals(1, r.invocationCount);
2002 >            assertEquals(inc(v1), r.value);
2003 >        } catch (CompletionException ok) {
2004 >            checkCompletedWithWrappedCFException(h, ex);
2005 >            assertEquals(0, r.invocationCount);
2006 >        }
2007  
2008 <        f = new CompletableFuture<>();
2009 <        g = new CompletableFuture<>();
2010 <        g.complete(1);
1697 <        f.complete(3);
1698 <        h = f.thenCombineAsync(g, subtract);
1699 <        checkCompletedNormally(h, 2);
1700 <    }
2008 >        checkCompletedWithWrappedCFException(f, ex);
2009 >        checkCompletedNormally(g, v1);
2010 >    }}
2011  
2012      /**
2013 <     * thenCombineAsync result completes exceptionally after exceptional
1704 <     * completion of either source
2013 >     * acceptEither result completes exceptionally if action does
2014       */
2015 <    public void testThenCombineAsync2() {
2016 <        CompletableFuture<Integer> f, g, h;
2015 >    public void testAcceptEither_actionFailed1() {
2016 >        for (ExecutionMode m : ExecutionMode.values())
2017 >        for (Integer v1 : new Integer[] { 1, null })
2018 >        for (Integer v2 : new Integer[] { 2, null })
2019 >    {
2020 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2021 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2022 >        final FailingConsumer r = new FailingConsumer();
2023 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2024  
2025 <        f = new CompletableFuture<>();
1710 <        g = new CompletableFuture<>();
1711 <        h = f.thenCombineAsync(g, subtract);
1712 <        f.completeExceptionally(new CFException());
1713 <        checkIncomplete(h);
1714 <        g.complete(1);
2025 >        f.complete(v1);
2026          checkCompletedWithWrappedCFException(h);
2027 +        g.complete(v2);
2028 +        checkCompletedNormally(f, v1);
2029 +        checkCompletedNormally(g, v2);
2030 +    }}
2031  
2032 <        f = new CompletableFuture<>();
2033 <        g = new CompletableFuture<>();
2034 <        h = f.thenCombineAsync(g, subtract);
2035 <        g.completeExceptionally(new CFException());
2036 <        checkIncomplete(h);
2037 <        f.complete(3);
2038 <        checkCompletedWithWrappedCFException(h);
2032 >    public void testAcceptEither_actionFailed2() {
2033 >        for (ExecutionMode m : ExecutionMode.values())
2034 >        for (Integer v1 : new Integer[] { 1, null })
2035 >        for (Integer v2 : new Integer[] { 2, null })
2036 >    {
2037 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2038 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 >        final FailingConsumer r = new FailingConsumer();
2040 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2041  
2042 <        f = new CompletableFuture<>();
1726 <        g = new CompletableFuture<>();
1727 <        g.completeExceptionally(new CFException());
1728 <        f.complete(3);
1729 <        h = f.thenCombineAsync(g, subtract);
2042 >        g.complete(v2);
2043          checkCompletedWithWrappedCFException(h);
2044 <    }
2045 <
2046 <    /**
2047 <     * thenCombineAsync result completes exceptionally if action does
1735 <     */
1736 <    public void testThenCombineAsync3() {
1737 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1738 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1739 <        FailingBiFunction r = new FailingBiFunction();
1740 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1741 <        f.complete(one);
1742 <        checkIncomplete(g);
1743 <        assertFalse(r.ran);
1744 <        f2.complete(two);
1745 <        checkCompletedWithWrappedCFException(g);
1746 <        assertTrue(r.ran);
1747 <    }
2044 >        f.complete(v1);
2045 >        checkCompletedNormally(f, v1);
2046 >        checkCompletedNormally(g, v2);
2047 >    }}
2048  
2049      /**
2050 <     * thenCombineAsync result completes exceptionally if either source cancelled
2050 >     * acceptEither result completes exceptionally if either source cancelled
2051       */
2052 <    public void testThenCombineAsync4() {
2053 <        CompletableFuture<Integer> f, g, h;
2054 <
2055 <        f = new CompletableFuture<>();
2056 <        g = new CompletableFuture<>();
2057 <        h = f.thenCombineAsync(g, subtract);
2058 <        assertTrue(f.cancel(true));
2059 <        checkIncomplete(h);
2060 <        g.complete(1);
1761 <        checkCompletedWithWrappedCancellationException(h);
1762 <
1763 <        f = new CompletableFuture<>();
1764 <        g = new CompletableFuture<>();
1765 <        h = f.thenCombineAsync(g, subtract);
1766 <        assertTrue(g.cancel(true));
1767 <        checkIncomplete(h);
1768 <        f.complete(3);
1769 <        checkCompletedWithWrappedCancellationException(h);
1770 <
1771 <        f = new CompletableFuture<>();
1772 <        g = new CompletableFuture<>();
1773 <        g.complete(3);
1774 <        assertTrue(f.cancel(true));
1775 <        h = f.thenCombineAsync(g, subtract);
1776 <        checkCompletedWithWrappedCancellationException(h);
2052 >    public void testAcceptEither_sourceCancelled1() {
2053 >        for (ExecutionMode m : ExecutionMode.values())
2054 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2055 >        for (Integer v1 : new Integer[] { 1, null })
2056 >    {
2057 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 >        final IncAction r = new IncAction();
2060 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2061  
2062 <        f = new CompletableFuture<>();
1779 <        g = new CompletableFuture<>();
1780 <        f.complete(3);
1781 <        assertTrue(g.cancel(true));
1782 <        h = f.thenCombineAsync(g, subtract);
2062 >        assertTrue(f.cancel(mayInterruptIfRunning));
2063          checkCompletedWithWrappedCancellationException(h);
2064 <    }
1785 <
1786 <    /**
1787 <     * thenAcceptBothAsync result completes normally after normal
1788 <     * completion of sources
1789 <     */
1790 <    public void testThenAcceptBothAsync() {
1791 <        CompletableFuture<Integer> f, g;
1792 <        CompletableFuture<Void> h;
1793 <        SubtractAction r;
1794 <
1795 <        f = new CompletableFuture<>();
1796 <        g = new CompletableFuture<>();
1797 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1798 <        f.complete(3);
1799 <        checkIncomplete(h);
1800 <        g.complete(1);
1801 <        checkCompletedNormally(h, null);
1802 <        assertEquals(r.value, 2);
1803 <
1804 <        f = new CompletableFuture<>();
1805 <        g = new CompletableFuture<>();
1806 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1807 <        g.complete(1);
1808 <        checkIncomplete(h);
1809 <        f.complete(3);
1810 <        checkCompletedNormally(h, null);
1811 <        assertEquals(r.value, 2);
1812 <
1813 <        f = new CompletableFuture<>();
1814 <        g = new CompletableFuture<>();
1815 <        g.complete(1);
1816 <        f.complete(3);
1817 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1818 <        checkCompletedNormally(h, null);
1819 <        assertEquals(r.value, 2);
1820 <    }
1821 <
1822 <    /**
1823 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1824 <     * completion of source
1825 <     */
1826 <    public void testThenAcceptBothAsync2() {
1827 <        CompletableFuture<Integer> f, g;
1828 <        CompletableFuture<Void> h;
1829 <        SubtractAction r;
1830 <
1831 <        f = new CompletableFuture<>();
1832 <        g = new CompletableFuture<>();
1833 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1834 <        f.completeExceptionally(new CFException());
1835 <        checkIncomplete(h);
1836 <        g.complete(1);
1837 <        checkCompletedWithWrappedCFException(h);
1838 <
1839 <        f = new CompletableFuture<>();
1840 <        g = new CompletableFuture<>();
1841 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842 <        g.completeExceptionally(new CFException());
1843 <        checkIncomplete(h);
1844 <        f.complete(3);
1845 <        checkCompletedWithWrappedCFException(h);
1846 <
1847 <        f = new CompletableFuture<>();
1848 <        g = new CompletableFuture<>();
1849 <        f.complete(3);
1850 <        g.completeExceptionally(new CFException());
1851 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1852 <        checkCompletedWithWrappedCFException(h);
1853 <
1854 <        f = new CompletableFuture<>();
1855 <        g = new CompletableFuture<>();
1856 <        f.completeExceptionally(new CFException());
1857 <        g.complete(3);
1858 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1859 <        checkCompletedWithWrappedCFException(h);
1860 <    }
1861 <
1862 <    /**
1863 <     * thenAcceptBothAsync result completes exceptionally if action does
1864 <     */
1865 <    public void testThenAcceptBothAsync3() {
1866 <        CompletableFuture<Integer> f, g;
1867 <        CompletableFuture<Void> h;
1868 <        FailingBiConsumer r;
1869 <
1870 <        f = new CompletableFuture<>();
1871 <        g = new CompletableFuture<>();
1872 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1873 <        f.complete(3);
1874 <        checkIncomplete(h);
1875 <        g.complete(1);
1876 <        checkCompletedWithWrappedCFException(h);
1877 <
1878 <        f = new CompletableFuture<>();
1879 <        g = new CompletableFuture<>();
1880 <        f.complete(3);
1881 <        g.complete(1);
1882 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1883 <        checkCompletedWithWrappedCFException(h);
1884 <    }
1885 <
1886 <    /**
1887 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1888 <     */
1889 <    public void testThenAcceptBothAsync4() {
1890 <        CompletableFuture<Integer> f, g;
1891 <        CompletableFuture<Void> h;
1892 <        SubtractAction r;
2064 >        g.complete(v1);
2065  
2066 <        f = new CompletableFuture<>();
2067 <        g = new CompletableFuture<>();
2068 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1897 <        assertTrue(f.cancel(true));
1898 <        checkIncomplete(h);
1899 <        g.complete(1);
2066 >        checkCancelled(f);
2067 >        assertEquals(0, r.invocationCount);
2068 >        checkCompletedNormally(g, v1);
2069          checkCompletedWithWrappedCancellationException(h);
2070 +    }}
2071  
2072 <        f = new CompletableFuture<>();
2073 <        g = new CompletableFuture<>();
2074 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2075 <        assertTrue(g.cancel(true));
2076 <        checkIncomplete(h);
2077 <        f.complete(3);
2078 <        checkCompletedWithWrappedCancellationException(h);
2072 >    public void testAcceptEither_sourceCancelled2() {
2073 >        for (ExecutionMode m : ExecutionMode.values())
2074 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2075 >        for (Integer v1 : new Integer[] { 1, null })
2076 >    {
2077 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2078 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2079 >        final IncAction r = new IncAction();
2080 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2081  
2082 <        f = new CompletableFuture<>();
1911 <        g = new CompletableFuture<>();
1912 <        f.complete(3);
1913 <        assertTrue(g.cancel(true));
1914 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2082 >        assertTrue(g.cancel(mayInterruptIfRunning));
2083          checkCompletedWithWrappedCancellationException(h);
2084 +        f.complete(v1);
2085  
2086 <        f = new CompletableFuture<>();
2087 <        g = new CompletableFuture<>();
2088 <        assertTrue(f.cancel(true));
1920 <        g.complete(3);
1921 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2086 >        checkCancelled(g);
2087 >        assertEquals(0, r.invocationCount);
2088 >        checkCompletedNormally(f, v1);
2089          checkCompletedWithWrappedCancellationException(h);
2090 <    }
1924 <
1925 <    /**
1926 <     * runAfterBothAsync result completes normally after normal
1927 <     * completion of sources
1928 <     */
1929 <    public void testRunAfterBothAsync() {
1930 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1931 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1932 <        Noop r = new Noop();
1933 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1934 <        f.complete(one);
1935 <        checkIncomplete(g);
1936 <        f2.complete(two);
1937 <        checkCompletedNormally(g, null);
1938 <        assertTrue(r.ran);
1939 <    }
1940 <
1941 <    /**
1942 <     * runAfterBothAsync result completes exceptionally after exceptional
1943 <     * completion of source
1944 <     */
1945 <    public void testRunAfterBothAsync2() {
1946 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1947 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1948 <        Noop r = new Noop();
1949 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1950 <        f.completeExceptionally(new CFException());
1951 <        f2.complete(two);
1952 <        checkCompletedWithWrappedCFException(g);
1953 <
1954 <        r = new Noop();
1955 <        f = new CompletableFuture<>();
1956 <        f2 = new CompletableFuture<>();
1957 <        g = f.runAfterBothAsync(f2, r);
1958 <        f.complete(one);
1959 <        f2.completeExceptionally(new CFException());
1960 <        checkCompletedWithWrappedCFException(g);
1961 <    }
1962 <
1963 <    /**
1964 <     * runAfterBothAsync result completes exceptionally if action does
1965 <     */
1966 <    public void testRunAfterBothAsync3() {
1967 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1968 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1969 <        FailingNoop r = new FailingNoop();
1970 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1971 <        f.complete(one);
1972 <        checkIncomplete(g);
1973 <        f2.complete(two);
1974 <        checkCompletedWithWrappedCFException(g);
1975 <    }
1976 <
1977 <    /**
1978 <     * runAfterBothAsync result completes exceptionally if either source cancelled
1979 <     */
1980 <    public void testRunAfterBothAsync4() {
1981 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1982 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1983 <        Noop r = new Noop();
1984 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1985 <        assertTrue(f.cancel(true));
1986 <        f2.complete(two);
1987 <        checkCompletedWithWrappedCancellationException(g);
1988 <
1989 <        r = new Noop();
1990 <        f = new CompletableFuture<>();
1991 <        f2 = new CompletableFuture<>();
1992 <        g = f.runAfterBothAsync(f2, r);
1993 <        f.complete(one);
1994 <        assertTrue(f2.cancel(true));
1995 <        checkCompletedWithWrappedCancellationException(g);
1996 <    }
1997 <
1998 <    /**
1999 <     * applyToEitherAsync result completes normally after normal
2000 <     * completion of sources
2001 <     */
2002 <    public void testApplyToEitherAsync() {
2003 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2004 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2005 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2006 <        f.complete(one);
2007 <        checkCompletedNormally(g, two);
2008 <
2009 <        f = new CompletableFuture<>();
2010 <        f.complete(one);
2011 <        f2 = new CompletableFuture<>();
2012 <        g = f.applyToEitherAsync(f2, inc);
2013 <        checkCompletedNormally(g, two);
2014 <    }
2015 <
2016 <    /**
2017 <     * applyToEitherAsync result completes exceptionally after exceptional
2018 <     * completion of source
2019 <     */
2020 <    public void testApplyToEitherAsync2() {
2021 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2022 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2023 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2024 <        f.completeExceptionally(new CFException());
2025 <        checkCompletedWithWrappedCFException(g);
2026 <
2027 <        f = new CompletableFuture<>();
2028 <        f2 = new CompletableFuture<>();
2029 <        f2.completeExceptionally(new CFException());
2030 <        g = f.applyToEitherAsync(f2, inc);
2031 <        f.complete(one);
2032 <        checkCompletedWithWrappedCFException(g);
2033 <    }
2034 <
2035 <    /**
2036 <     * applyToEitherAsync result completes exceptionally if action does
2037 <     */
2038 <    public void testApplyToEitherAsync3() {
2039 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2040 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2041 <        FailingFunction r = new FailingFunction();
2042 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2043 <        f.complete(one);
2044 <        checkCompletedWithWrappedCFException(g);
2045 <    }
2046 <
2047 <    /**
2048 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2049 <     */
2050 <    public void testApplyToEitherAsync4() {
2051 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2052 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2053 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2054 <        assertTrue(f.cancel(true));
2055 <        checkCompletedWithWrappedCancellationException(g);
2056 <
2057 <        f = new CompletableFuture<>();
2058 <        f2 = new CompletableFuture<>();
2059 <        assertTrue(f2.cancel(true));
2060 <        g = f.applyToEitherAsync(f2, inc);
2061 <        checkCompletedWithWrappedCancellationException(g);
2062 <    }
2063 <
2064 <    /**
2065 <     * acceptEitherAsync result completes normally after normal
2066 <     * completion of sources
2067 <     */
2068 <    public void testAcceptEitherAsync() {
2069 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2070 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2071 <        IncAction r = new IncAction();
2072 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2073 <        f.complete(one);
2074 <        checkCompletedNormally(g, null);
2075 <        assertEquals(r.value, 2);
2076 <
2077 <        r = new IncAction();
2078 <        f = new CompletableFuture<>();
2079 <        f.complete(one);
2080 <        f2 = new CompletableFuture<>();
2081 <        g = f.acceptEitherAsync(f2, r);
2082 <        checkCompletedNormally(g, null);
2083 <        assertEquals(r.value, 2);
2084 <    }
2085 <
2086 <    /**
2087 <     * acceptEitherAsync result completes exceptionally after exceptional
2088 <     * completion of source
2089 <     */
2090 <    public void testAcceptEitherAsync2() {
2091 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2092 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2093 <        IncAction r = new IncAction();
2094 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2095 <        f.completeExceptionally(new CFException());
2096 <        checkCompletedWithWrappedCFException(g);
2097 <
2098 <        r = new IncAction();
2099 <        f = new CompletableFuture<>();
2100 <        f2 = new CompletableFuture<>();
2101 <        f2.completeExceptionally(new CFException());
2102 <        g = f.acceptEitherAsync(f2, r);
2103 <        f.complete(one);
2104 <        checkCompletedWithWrappedCFException(g);
2105 <    }
2106 <
2107 <    /**
2108 <     * acceptEitherAsync result completes exceptionally if action does
2109 <     */
2110 <    public void testAcceptEitherAsync3() {
2111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2112 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2113 <        FailingConsumer r = new FailingConsumer();
2114 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2115 <        f.complete(one);
2116 <        checkCompletedWithWrappedCFException(g);
2117 <    }
2118 <
2119 <    /**
2120 <     * acceptEitherAsync result completes exceptionally if either
2121 <     * source cancelled
2122 <     */
2123 <    public void testAcceptEitherAsync4() {
2124 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2126 <        IncAction r = new IncAction();
2127 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2128 <        assertTrue(f.cancel(true));
2129 <        checkCompletedWithWrappedCancellationException(g);
2130 <
2131 <        r = new IncAction();
2132 <        f = new CompletableFuture<>();
2133 <        f2 = new CompletableFuture<>();
2134 <        assertTrue(f2.cancel(true));
2135 <        g = f.acceptEitherAsync(f2, r);
2136 <        checkCompletedWithWrappedCancellationException(g);
2137 <    }
2138 <
2139 <    /**
2140 <     * runAfterEitherAsync result completes normally after normal
2141 <     * completion of sources
2142 <     */
2143 <    public void testRunAfterEitherAsync() {
2144 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2145 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2146 <        Noop r = new Noop();
2147 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2148 <        f.complete(one);
2149 <        checkCompletedNormally(g, null);
2150 <        assertTrue(r.ran);
2151 <
2152 <        r = new Noop();
2153 <        f = new CompletableFuture<>();
2154 <        f.complete(one);
2155 <        f2 = new CompletableFuture<>();
2156 <        g = f.runAfterEitherAsync(f2, r);
2157 <        checkCompletedNormally(g, null);
2158 <        assertTrue(r.ran);
2159 <    }
2160 <
2161 <    /**
2162 <     * runAfterEitherAsync result completes exceptionally after exceptional
2163 <     * completion of source
2164 <     */
2165 <    public void testRunAfterEitherAsync2() {
2166 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2167 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2168 <        Noop r = new Noop();
2169 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2170 <        f.completeExceptionally(new CFException());
2171 <        checkCompletedWithWrappedCFException(g);
2172 <
2173 <        r = new Noop();
2174 <        f = new CompletableFuture<>();
2175 <        f2 = new CompletableFuture<>();
2176 <        f2.completeExceptionally(new CFException());
2177 <        g = f.runAfterEitherAsync(f2, r);
2178 <        f.complete(one);
2179 <        checkCompletedWithWrappedCFException(g);
2180 <    }
2181 <
2182 <    /**
2183 <     * runAfterEitherAsync result completes exceptionally if action does
2184 <     */
2185 <    public void testRunAfterEitherAsync3() {
2186 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2187 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2188 <        FailingNoop r = new FailingNoop();
2189 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2190 <        f.complete(one);
2191 <        checkCompletedWithWrappedCFException(g);
2192 <    }
2193 <
2194 <    /**
2195 <     * runAfterEitherAsync result completes exceptionally if either
2196 <     * source cancelled
2197 <     */
2198 <    public void testRunAfterEitherAsync4() {
2199 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2200 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2201 <        Noop r = new Noop();
2202 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2203 <        assertTrue(f.cancel(true));
2204 <        checkCompletedWithWrappedCancellationException(g);
2205 <
2206 <        r = new Noop();
2207 <        f = new CompletableFuture<>();
2208 <        f2 = new CompletableFuture<>();
2209 <        assertTrue(f2.cancel(true));
2210 <        g = f.runAfterEitherAsync(f2, r);
2211 <        checkCompletedWithWrappedCancellationException(g);
2212 <    }
2213 <
2214 <    /**
2215 <     * thenComposeAsync result completes normally after normal
2216 <     * completion of source
2217 <     */
2218 <    public void testThenComposeAsync() {
2219 <        CompletableFuture<Integer> f, g;
2220 <        CompletableFutureInc r;
2221 <
2222 <        f = new CompletableFuture<>();
2223 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2224 <        f.complete(one);
2225 <        checkCompletedNormally(g, two);
2226 <
2227 <        f = new CompletableFuture<>();
2228 <        f.complete(one);
2229 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2230 <        checkCompletedNormally(g, two);
2231 <    }
2232 <
2233 <    /**
2234 <     * thenComposeAsync result completes exceptionally after
2235 <     * exceptional completion of source
2236 <     */
2237 <    public void testThenComposeAsync2() {
2238 <        CompletableFuture<Integer> f, g;
2239 <        CompletableFutureInc r;
2240 <
2241 <        f = new CompletableFuture<>();
2242 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2243 <        f.completeExceptionally(new CFException());
2244 <        checkCompletedWithWrappedCFException(g);
2245 <        assertFalse(r.ran);
2246 <
2247 <        f = new CompletableFuture<>();
2248 <        f.completeExceptionally(new CFException());
2249 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2250 <        checkCompletedWithWrappedCFException(g);
2251 <        assertFalse(r.ran);
2252 <    }
2253 <
2254 <    /**
2255 <     * thenComposeAsync result completes exceptionally if action does
2256 <     */
2257 <    public void testThenComposeAsync3() {
2258 <        CompletableFuture<Integer> f, g;
2259 <        FailingCompletableFutureFunction r;
2260 <
2261 <        f = new CompletableFuture<>();
2262 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2263 <        f.complete(one);
2264 <        checkCompletedWithWrappedCFException(g);
2265 <
2266 <        f = new CompletableFuture<>();
2267 <        f.complete(one);
2268 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2269 <        checkCompletedWithWrappedCFException(g);
2270 <    }
2271 <
2272 <    /**
2273 <     * thenComposeAsync result completes exceptionally if source cancelled
2274 <     */
2275 <    public void testThenComposeAsync4() {
2276 <        CompletableFuture<Integer> f, g;
2277 <        CompletableFutureInc r;
2090 >    }}
2091  
2092 <        f = new CompletableFuture<>();
2093 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2094 <        assertTrue(f.cancel(true));
2095 <        checkCompletedWithWrappedCancellationException(g);
2096 <
2097 <        f = new CompletableFuture<>();
2098 <        assertTrue(f.cancel(true));
2099 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2287 <        checkCompletedWithWrappedCancellationException(g);
2288 <    }
2289 <
2290 <    // async with explicit executors
2291 <
2292 <    /**
2293 <     * thenRunAsync result completes normally after normal completion of source
2294 <     */
2295 <    public void testThenRunAsyncE() {
2296 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2297 <        Noop r = new Noop();
2298 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2299 <        f.complete(null);
2300 <        checkCompletedNormally(g, null);
2092 >    public void testAcceptEither_sourceCancelled3() {
2093 >        for (ExecutionMode m : ExecutionMode.values())
2094 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2095 >        for (Integer v1 : new Integer[] { 1, null })
2096 >    {
2097 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2098 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2099 >        final IncAction r = new IncAction();
2100  
2101 <        // reordered version
2102 <        f = new CompletableFuture<>();
2103 <        f.complete(null);
2305 <        r = new Noop();
2306 <        g = f.thenRunAsync(r, new ThreadExecutor());
2307 <        checkCompletedNormally(g, null);
2308 <    }
2101 >        assertTrue(g.cancel(mayInterruptIfRunning));
2102 >        f.complete(v1);
2103 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2104  
2105 <    /**
2106 <     * thenRunAsync result completes exceptionally after exceptional
2312 <     * completion of source
2313 <     */
2314 <    public void testThenRunAsync2E() {
2315 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2316 <        Noop r = new Noop();
2317 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2318 <        f.completeExceptionally(new CFException());
2105 >        // unspecified behavior
2106 >        Integer v;
2107          try {
2108 <            g.join();
2109 <            shouldThrow();
2110 <        } catch (CompletionException success) {}
2111 <        checkCompletedWithWrappedCFException(g);
2112 <    }
2108 >            assertNull(h.join());
2109 >            assertEquals(1, r.invocationCount);
2110 >            assertEquals(inc(v1), r.value);
2111 >        } catch (CompletionException ok) {
2112 >            checkCompletedWithWrappedCancellationException(h);
2113 >            assertEquals(0, r.invocationCount);
2114 >        }
2115  
2116 <    /**
2117 <     * thenRunAsync result completes exceptionally if action does
2118 <     */
2329 <    public void testThenRunAsync3E() {
2330 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2331 <        FailingNoop r = new FailingNoop();
2332 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2333 <        f.complete(null);
2334 <        checkCompletedWithWrappedCFException(g);
2335 <    }
2116 >        checkCancelled(g);
2117 >        checkCompletedNormally(f, v1);
2118 >    }}
2119  
2120 <    /**
2121 <     * thenRunAsync result completes exceptionally if source cancelled
2122 <     */
2123 <    public void testThenRunAsync4E() {
2124 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        Noop r = new Noop();
2126 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2127 <        assertTrue(f.cancel(true));
2345 <        checkCompletedWithWrappedCancellationException(g);
2346 <    }
2120 >    public void testAcceptEither_sourceCancelled4() {
2121 >        for (ExecutionMode m : ExecutionMode.values())
2122 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2123 >        for (Integer v1 : new Integer[] { 1, null })
2124 >    {
2125 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2126 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2127 >        final IncAction r = new IncAction();
2128  
2129 <    /**
2130 <     * thenApplyAsync result completes normally after normal completion of source
2131 <     */
2351 <    public void testThenApplyAsyncE() {
2352 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2353 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2354 <        f.complete(one);
2355 <        checkCompletedNormally(g, two);
2356 <    }
2129 >        assertTrue(f.cancel(mayInterruptIfRunning));
2130 >        g.complete(v1);
2131 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2132  
2133 <    /**
2134 <     * thenApplyAsync result completes exceptionally after exceptional
2135 <     * completion of source
2136 <     */
2137 <    public void testThenApplyAsync2E() {
2138 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2139 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2140 <        f.completeExceptionally(new CFException());
2141 <        checkCompletedWithWrappedCFException(g);
2142 <    }
2133 >        // unspecified behavior
2134 >        Integer v;
2135 >        try {
2136 >            assertNull(h.join());
2137 >            assertEquals(1, r.invocationCount);
2138 >            assertEquals(inc(v1), r.value);
2139 >        } catch (CompletionException ok) {
2140 >            checkCompletedWithWrappedCancellationException(h);
2141 >            assertEquals(0, r.invocationCount);
2142 >        }
2143  
2144 <    /**
2145 <     * thenApplyAsync result completes exceptionally if action does
2146 <     */
2372 <    public void testThenApplyAsync3E() {
2373 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2374 <        FailingFunction r = new FailingFunction();
2375 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2376 <        f.complete(null);
2377 <        checkCompletedWithWrappedCFException(g);
2378 <    }
2144 >        checkCancelled(f);
2145 >        checkCompletedNormally(g, v1);
2146 >    }}
2147  
2148      /**
2149 <     * thenApplyAsync result completes exceptionally if source cancelled
2149 >     * runAfterEither result completes normally after normal completion
2150 >     * of either source
2151       */
2152 <    public void testThenApplyAsync4E() {
2153 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2154 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2155 <        assertTrue(f.cancel(true));
2156 <        checkCompletedWithWrappedCancellationException(g);
2157 <    }
2152 >    public void testRunAfterEither_normalCompletion1() {
2153 >        for (ExecutionMode m : ExecutionMode.values())
2154 >        for (Integer v1 : new Integer[] { 1, null })
2155 >        for (Integer v2 : new Integer[] { 2, null })
2156 >    {
2157 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2158 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 >        final Noop r = new Noop(m);
2160 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2161  
2162 <    /**
2163 <     * thenAcceptAsync result completes normally after normal
2164 <     * completion of source
2165 <     */
2394 <    public void testThenAcceptAsyncE() {
2395 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2396 <        IncAction r = new IncAction();
2397 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2398 <        f.complete(one);
2399 <        checkCompletedNormally(g, null);
2400 <        assertEquals(r.value, 2);
2401 <    }
2162 >        f.complete(v1);
2163 >        checkCompletedNormally(h, null);
2164 >        assertEquals(1, r.invocationCount);
2165 >        g.complete(v2);
2166  
2167 <    /**
2168 <     * thenAcceptAsync result completes exceptionally after exceptional
2169 <     * completion of source
2170 <     */
2171 <    public void testThenAcceptAsync2E() {
2408 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2409 <        IncAction r = new IncAction();
2410 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2411 <        f.completeExceptionally(new CFException());
2412 <        checkCompletedWithWrappedCFException(g);
2413 <    }
2167 >        checkCompletedNormally(f, v1);
2168 >        checkCompletedNormally(g, v2);
2169 >        checkCompletedNormally(h, null);
2170 >        assertEquals(1, r.invocationCount);
2171 >    }}
2172  
2173 <    /**
2174 <     * thenAcceptAsync result completes exceptionally if action does
2175 <     */
2176 <    public void testThenAcceptAsync3E() {
2177 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2178 <        FailingConsumer r = new FailingConsumer();
2179 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2180 <        f.complete(null);
2181 <        checkCompletedWithWrappedCFException(g);
2424 <    }
2173 >    public void testRunAfterEither_normalCompletion2() {
2174 >        for (ExecutionMode m : ExecutionMode.values())
2175 >        for (Integer v1 : new Integer[] { 1, null })
2176 >        for (Integer v2 : new Integer[] { 2, null })
2177 >    {
2178 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180 >        final Noop r = new Noop(m);
2181 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2182  
2183 <    /**
2184 <     * thenAcceptAsync result completes exceptionally if source cancelled
2185 <     */
2186 <    public void testThenAcceptAsync4E() {
2430 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2431 <        IncAction r = new IncAction();
2432 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2433 <        assertTrue(f.cancel(true));
2434 <        checkCompletedWithWrappedCancellationException(g);
2435 <    }
2183 >        g.complete(v2);
2184 >        checkCompletedNormally(h, null);
2185 >        assertEquals(1, r.invocationCount);
2186 >        f.complete(v1);
2187  
2188 <    /**
2189 <     * thenCombineAsync result completes normally after normal
2190 <     * completion of sources
2191 <     */
2192 <    public void testThenCombineAsyncE() {
2442 <        CompletableFuture<Integer> f, g, h;
2443 <        ThreadExecutor e = new ThreadExecutor();
2444 <        int count = 0;
2188 >        checkCompletedNormally(f, v1);
2189 >        checkCompletedNormally(g, v2);
2190 >        checkCompletedNormally(h, null);
2191 >        assertEquals(1, r.invocationCount);
2192 >        }}
2193  
2194 <        f = new CompletableFuture<>();
2195 <        g = new CompletableFuture<>();
2196 <        h = f.thenCombineAsync(g, subtract, e);
2197 <        f.complete(3);
2198 <        checkIncomplete(h);
2199 <        g.complete(1);
2200 <        checkCompletedNormally(h, 2);
2201 <        assertEquals(++count, e.count.get());
2194 >    public void testRunAfterEither_normalCompletion3() {
2195 >        for (ExecutionMode m : ExecutionMode.values())
2196 >        for (Integer v1 : new Integer[] { 1, null })
2197 >        for (Integer v2 : new Integer[] { 2, null })
2198 >    {
2199 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2200 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2201 >        final Noop r = new Noop(m);
2202  
2203 <        f = new CompletableFuture<>();
2204 <        g = new CompletableFuture<>();
2205 <        h = f.thenCombineAsync(g, subtract, e);
2458 <        g.complete(1);
2459 <        checkIncomplete(h);
2460 <        f.complete(3);
2461 <        checkCompletedNormally(h, 2);
2462 <        assertEquals(++count, e.count.get());
2203 >        f.complete(v1);
2204 >        g.complete(v2);
2205 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2206  
2207 <        f = new CompletableFuture<>();
2208 <        g = new CompletableFuture<>();
2209 <        g.complete(1);
2210 <        f.complete(3);
2211 <        h = f.thenCombineAsync(g, subtract, e);
2469 <        checkCompletedNormally(h, 2);
2470 <        assertEquals(++count, e.count.get());
2471 <    }
2207 >        checkCompletedNormally(h, null);
2208 >        checkCompletedNormally(f, v1);
2209 >        checkCompletedNormally(g, v2);
2210 >        assertEquals(1, r.invocationCount);
2211 >    }}
2212  
2213      /**
2214 <     * thenCombineAsync result completes exceptionally after exceptional
2214 >     * runAfterEither result completes exceptionally after exceptional
2215       * completion of either source
2216       */
2217 <    public void testThenCombineAsync2E() {
2218 <        CompletableFuture<Integer> f, g, h;
2219 <        ThreadExecutor e = new ThreadExecutor();
2220 <        int count = 0;
2221 <
2222 <        f = new CompletableFuture<>();
2223 <        g = new CompletableFuture<>();
2224 <        h = f.thenCombineAsync(g, subtract, e);
2225 <        f.completeExceptionally(new CFException());
2486 <        checkIncomplete(h);
2487 <        g.complete(1);
2488 <        checkCompletedWithWrappedCFException(h);
2489 <
2490 <        f = new CompletableFuture<>();
2491 <        g = new CompletableFuture<>();
2492 <        h = f.thenCombineAsync(g, subtract, e);
2493 <        g.completeExceptionally(new CFException());
2494 <        checkIncomplete(h);
2495 <        f.complete(3);
2496 <        checkCompletedWithWrappedCFException(h);
2497 <
2498 <        f = new CompletableFuture<>();
2499 <        g = new CompletableFuture<>();
2500 <        g.completeExceptionally(new CFException());
2501 <        h = f.thenCombineAsync(g, subtract, e);
2502 <        checkIncomplete(h);
2503 <        f.complete(3);
2504 <        checkCompletedWithWrappedCFException(h);
2217 >    public void testRunAfterEither_exceptionalCompletion1() {
2218 >        for (ExecutionMode m : ExecutionMode.values())
2219 >        for (Integer v1 : new Integer[] { 1, null })
2220 >    {
2221 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2222 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2223 >        final Noop r = new Noop(m);
2224 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2225 >        final CFException ex = new CFException();
2226  
2227 <        assertEquals(0, e.count.get());
2228 <    }
2227 >        f.completeExceptionally(ex);
2228 >        checkCompletedWithWrappedCFException(h, ex);
2229 >        g.complete(v1);
2230  
2231 <    /**
2232 <     * thenCombineAsync result completes exceptionally if action does
2233 <     */
2234 <    public void testThenCombineAsync3E() {
2235 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2514 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2515 <        FailingBiFunction r = new FailingBiFunction();
2516 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2517 <        f.complete(one);
2518 <        checkIncomplete(g);
2519 <        assertFalse(r.ran);
2520 <        f2.complete(two);
2521 <        checkCompletedWithWrappedCFException(g);
2522 <        assertTrue(r.ran);
2523 <    }
2231 >        assertEquals(0, r.invocationCount);
2232 >        checkCompletedNormally(g, v1);
2233 >        checkCompletedWithWrappedCFException(f, ex);
2234 >        checkCompletedWithWrappedCFException(h, ex);
2235 >    }}
2236  
2237 <    /**
2238 <     * thenCombineAsync result completes exceptionally if either source cancelled
2239 <     */
2240 <    public void testThenCombineAsync4E() {
2241 <        CompletableFuture<Integer> f, g, h;
2242 <        ThreadExecutor e = new ThreadExecutor();
2237 >    public void testRunAfterEither_exceptionalCompletion2() {
2238 >        for (ExecutionMode m : ExecutionMode.values())
2239 >        for (Integer v1 : new Integer[] { 1, null })
2240 >    {
2241 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 >        final Noop r = new Noop(m);
2244 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2245 >        final CFException ex = new CFException();
2246  
2247 <        f = new CompletableFuture<>();
2248 <        g = new CompletableFuture<>();
2249 <        h = f.thenCombineAsync(g, subtract, e);
2535 <        assertTrue(f.cancel(true));
2536 <        checkIncomplete(h);
2537 <        g.complete(1);
2538 <        checkCompletedWithWrappedCancellationException(h);
2247 >        g.completeExceptionally(ex);
2248 >        checkCompletedWithWrappedCFException(h, ex);
2249 >        f.complete(v1);
2250  
2251 <        f = new CompletableFuture<>();
2252 <        g = new CompletableFuture<>();
2253 <        h = f.thenCombineAsync(g, subtract, e);
2254 <        assertTrue(g.cancel(true));
2255 <        checkIncomplete(h);
2545 <        f.complete(3);
2546 <        checkCompletedWithWrappedCancellationException(h);
2251 >        assertEquals(0, r.invocationCount);
2252 >        checkCompletedNormally(f, v1);
2253 >        checkCompletedWithWrappedCFException(g, ex);
2254 >        checkCompletedWithWrappedCFException(h, ex);
2255 >    }}
2256  
2257 <        f = new CompletableFuture<>();
2258 <        g = new CompletableFuture<>();
2259 <        assertTrue(g.cancel(true));
2260 <        h = f.thenCombineAsync(g, subtract, e);
2261 <        checkIncomplete(h);
2262 <        f.complete(3);
2263 <        checkCompletedWithWrappedCancellationException(h);
2257 >    public void testRunAfterEither_exceptionalCompletion3() {
2258 >        for (ExecutionMode m : ExecutionMode.values())
2259 >        for (Integer v1 : new Integer[] { 1, null })
2260 >    {
2261 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2262 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2263 >        final Noop r = new Noop(m);
2264 >        final CFException ex = new CFException();
2265  
2266 <        f = new CompletableFuture<>();
2267 <        g = new CompletableFuture<>();
2268 <        assertTrue(f.cancel(true));
2559 <        assertTrue(g.cancel(true));
2560 <        h = f.thenCombineAsync(g, subtract, e);
2561 <        checkCompletedWithWrappedCancellationException(h);
2266 >        g.completeExceptionally(ex);
2267 >        f.complete(v1);
2268 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2269  
2270 <        assertEquals(0, e.count.get());
2271 <    }
2270 >        // unspecified behavior
2271 >        Integer v;
2272 >        try {
2273 >            assertNull(h.join());
2274 >            assertEquals(1, r.invocationCount);
2275 >        } catch (CompletionException ok) {
2276 >            checkCompletedWithWrappedCFException(h, ex);
2277 >            assertEquals(0, r.invocationCount);
2278 >        }
2279  
2280 <    /**
2281 <     * thenAcceptBothAsync result completes normally after normal
2282 <     * completion of sources
2569 <     */
2570 <    public void testThenAcceptBothAsyncE() {
2571 <        CompletableFuture<Integer> f, g;
2572 <        CompletableFuture<Void> h;
2573 <        SubtractAction r;
2574 <        ThreadExecutor e = new ThreadExecutor();
2280 >        checkCompletedWithWrappedCFException(g, ex);
2281 >        checkCompletedNormally(f, v1);
2282 >    }}
2283  
2284 <        f = new CompletableFuture<>();
2285 <        g = new CompletableFuture<>();
2286 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2287 <        f.complete(3);
2288 <        checkIncomplete(h);
2289 <        g.complete(1);
2290 <        checkCompletedNormally(h, null);
2291 <        assertEquals(r.value, 2);
2284 >    public void testRunAfterEither_exceptionalCompletion4() {
2285 >        for (ExecutionMode m : ExecutionMode.values())
2286 >        for (Integer v1 : new Integer[] { 1, null })
2287 >    {
2288 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2289 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2290 >        final Noop r = new Noop(m);
2291 >        final CFException ex = new CFException();
2292  
2293 <        f = new CompletableFuture<>();
2294 <        g = new CompletableFuture<>();
2295 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2588 <        g.complete(1);
2589 <        checkIncomplete(h);
2590 <        f.complete(3);
2591 <        checkCompletedNormally(h, null);
2592 <        assertEquals(r.value, 2);
2293 >        f.completeExceptionally(ex);
2294 >        g.complete(v1);
2295 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2296  
2297 <        f = new CompletableFuture<>();
2298 <        g = new CompletableFuture<>();
2299 <        g.complete(1);
2300 <        f.complete(3);
2301 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2302 <        checkCompletedNormally(h, null);
2303 <        assertEquals(r.value, 2);
2297 >        // unspecified behavior
2298 >        Integer v;
2299 >        try {
2300 >            assertNull(h.join());
2301 >            assertEquals(1, r.invocationCount);
2302 >        } catch (CompletionException ok) {
2303 >            checkCompletedWithWrappedCFException(h, ex);
2304 >            assertEquals(0, r.invocationCount);
2305 >        }
2306  
2307 <        assertEquals(3, e.count.get());
2308 <    }
2307 >        checkCompletedWithWrappedCFException(f, ex);
2308 >        checkCompletedNormally(g, v1);
2309 >    }}
2310  
2311      /**
2312 <     * thenAcceptBothAsync result completes exceptionally after exceptional
2607 <     * completion of source
2312 >     * runAfterEither result completes exceptionally if action does
2313       */
2314 <    public void testThenAcceptBothAsync2E() {
2315 <        CompletableFuture<Integer> f, g;
2316 <        CompletableFuture<Void> h;
2317 <        SubtractAction r;
2318 <        ThreadExecutor e = new ThreadExecutor();
2319 <
2320 <        f = new CompletableFuture<>();
2321 <        g = new CompletableFuture<>();
2322 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2618 <        f.completeExceptionally(new CFException());
2619 <        checkIncomplete(h);
2620 <        g.complete(1);
2621 <        checkCompletedWithWrappedCFException(h);
2622 <
2623 <        f = new CompletableFuture<>();
2624 <        g = new CompletableFuture<>();
2625 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2626 <        g.completeExceptionally(new CFException());
2627 <        checkIncomplete(h);
2628 <        f.complete(3);
2629 <        checkCompletedWithWrappedCFException(h);
2630 <
2631 <        f = new CompletableFuture<>();
2632 <        g = new CompletableFuture<>();
2633 <        f.complete(3);
2634 <        g.completeExceptionally(new CFException());
2635 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2636 <        checkCompletedWithWrappedCFException(h);
2314 >    public void testRunAfterEither_actionFailed1() {
2315 >        for (ExecutionMode m : ExecutionMode.values())
2316 >        for (Integer v1 : new Integer[] { 1, null })
2317 >        for (Integer v2 : new Integer[] { 2, null })
2318 >    {
2319 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2320 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 >        final FailingRunnable r = new FailingRunnable();
2322 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2323  
2324 <        f = new CompletableFuture<>();
2639 <        g = new CompletableFuture<>();
2640 <        f.completeExceptionally(new CFException());
2641 <        g.complete(3);
2642 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2324 >        f.complete(v1);
2325          checkCompletedWithWrappedCFException(h);
2326 +        g.complete(v2);
2327 +        checkCompletedNormally(f, v1);
2328 +        checkCompletedNormally(g, v2);
2329 +    }}
2330  
2331 <        assertEquals(0, e.count.get());
2332 <    }
2333 <
2334 <    /**
2335 <     * thenAcceptBothAsync result completes exceptionally if action does
2336 <     */
2337 <    public void testThenAcceptBothAsync3E() {
2338 <        CompletableFuture<Integer> f, g;
2339 <        CompletableFuture<Void> h;
2654 <        FailingBiConsumer r;
2655 <        ThreadExecutor e = new ThreadExecutor();
2656 <
2657 <        f = new CompletableFuture<>();
2658 <        g = new CompletableFuture<>();
2659 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2660 <        f.complete(3);
2661 <        checkIncomplete(h);
2662 <        g.complete(1);
2663 <        checkCompletedWithWrappedCFException(h);
2331 >    public void testRunAfterEither_actionFailed2() {
2332 >        for (ExecutionMode m : ExecutionMode.values())
2333 >        for (Integer v1 : new Integer[] { 1, null })
2334 >        for (Integer v2 : new Integer[] { 2, null })
2335 >    {
2336 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2337 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2338 >        final FailingRunnable r = new FailingRunnable();
2339 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2340  
2341 <        f = new CompletableFuture<>();
2666 <        g = new CompletableFuture<>();
2667 <        f.complete(3);
2668 <        g.complete(1);
2669 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2341 >        g.complete(v2);
2342          checkCompletedWithWrappedCFException(h);
2343 <
2344 <        assertEquals(2, e.count.get());
2345 <    }
2343 >        f.complete(v1);
2344 >        checkCompletedNormally(f, v1);
2345 >        checkCompletedNormally(g, v2);
2346 >    }}
2347  
2348      /**
2349 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2349 >     * runAfterEither result completes exceptionally if either source cancelled
2350       */
2351 <    public void testThenAcceptBothAsync4E() {
2352 <        CompletableFuture<Integer> f, g;
2353 <        CompletableFuture<Void> h;
2354 <        SubtractAction r;
2355 <        ThreadExecutor e = new ThreadExecutor();
2351 >    public void testRunAfterEither_sourceCancelled1() {
2352 >        for (ExecutionMode m : ExecutionMode.values())
2353 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2354 >        for (Integer v1 : new Integer[] { 1, null })
2355 >    {
2356 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2357 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2358 >        final Noop r = new Noop(m);
2359 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2360  
2361 <        f = new CompletableFuture<>();
2685 <        g = new CompletableFuture<>();
2686 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2687 <        assertTrue(f.cancel(true));
2688 <        checkIncomplete(h);
2689 <        g.complete(1);
2361 >        assertTrue(f.cancel(mayInterruptIfRunning));
2362          checkCompletedWithWrappedCancellationException(h);
2363 +        g.complete(v1);
2364  
2365 <        f = new CompletableFuture<>();
2366 <        g = new CompletableFuture<>();
2367 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2695 <        assertTrue(g.cancel(true));
2696 <        checkIncomplete(h);
2697 <        f.complete(3);
2365 >        checkCancelled(f);
2366 >        assertEquals(0, r.invocationCount);
2367 >        checkCompletedNormally(g, v1);
2368          checkCompletedWithWrappedCancellationException(h);
2369 +    }}
2370  
2371 <        f = new CompletableFuture<>();
2372 <        g = new CompletableFuture<>();
2373 <        f.complete(3);
2374 <        assertTrue(g.cancel(true));
2375 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2376 <        checkCompletedWithWrappedCancellationException(h);
2371 >    public void testRunAfterEither_sourceCancelled2() {
2372 >        for (ExecutionMode m : ExecutionMode.values())
2373 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2374 >        for (Integer v1 : new Integer[] { 1, null })
2375 >    {
2376 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2377 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2378 >        final Noop r = new Noop(m);
2379 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2380  
2381 <        f = new CompletableFuture<>();
2708 <        g = new CompletableFuture<>();
2709 <        assertTrue(f.cancel(true));
2710 <        g.complete(3);
2711 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2381 >        assertTrue(g.cancel(mayInterruptIfRunning));
2382          checkCompletedWithWrappedCancellationException(h);
2383 +        f.complete(v1);
2384  
2385 <        assertEquals(0, e.count.get());
2386 <    }
2387 <
2388 <    /**
2389 <     * runAfterBothAsync result completes normally after normal
2719 <     * completion of sources
2720 <     */
2721 <    public void testRunAfterBothAsyncE() {
2722 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2723 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2724 <        Noop r = new Noop();
2725 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2726 <        f.complete(one);
2727 <        checkIncomplete(g);
2728 <        f2.complete(two);
2729 <        checkCompletedNormally(g, null);
2730 <        assertTrue(r.ran);
2731 <    }
2732 <
2733 <    /**
2734 <     * runAfterBothAsync result completes exceptionally after exceptional
2735 <     * completion of source
2736 <     */
2737 <    public void testRunAfterBothAsync2E() {
2738 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2739 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740 <        Noop r = new Noop();
2741 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2742 <        f.completeExceptionally(new CFException());
2743 <        f2.complete(two);
2744 <        checkCompletedWithWrappedCFException(g);
2745 <
2746 <        r = new Noop();
2747 <        f = new CompletableFuture<>();
2748 <        f2 = new CompletableFuture<>();
2749 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2750 <        f.complete(one);
2751 <        f2.completeExceptionally(new CFException());
2752 <        checkCompletedWithWrappedCFException(g);
2753 <    }
2754 <
2755 <    /**
2756 <     * runAfterBothAsync result completes exceptionally if action does
2757 <     */
2758 <    public void testRunAfterBothAsync3E() {
2759 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2760 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2761 <        FailingNoop r = new FailingNoop();
2762 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2763 <        f.complete(one);
2764 <        checkIncomplete(g);
2765 <        f2.complete(two);
2766 <        checkCompletedWithWrappedCFException(g);
2767 <    }
2768 <
2769 <    /**
2770 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2771 <     */
2772 <    public void testRunAfterBothAsync4E() {
2773 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2774 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2775 <        Noop r = new Noop();
2776 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2777 <        assertTrue(f.cancel(true));
2778 <        f2.complete(two);
2779 <        checkCompletedWithWrappedCancellationException(g);
2780 <
2781 <        r = new Noop();
2782 <        f = new CompletableFuture<>();
2783 <        f2 = new CompletableFuture<>();
2784 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2785 <        f.complete(one);
2786 <        assertTrue(f2.cancel(true));
2787 <        checkCompletedWithWrappedCancellationException(g);
2788 <    }
2789 <
2790 <    /**
2791 <     * applyToEitherAsync result completes normally after normal
2792 <     * completion of sources
2793 <     */
2794 <    public void testApplyToEitherAsyncE() {
2795 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2796 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2797 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2798 <        f.complete(one);
2799 <        checkCompletedNormally(g, two);
2800 <
2801 <        f = new CompletableFuture<>();
2802 <        f.complete(one);
2803 <        f2 = new CompletableFuture<>();
2804 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2805 <        checkCompletedNormally(g, two);
2806 <    }
2807 <
2808 <    /**
2809 <     * applyToEitherAsync result completes exceptionally after exceptional
2810 <     * completion of source
2811 <     */
2812 <    public void testApplyToEitherAsync2E() {
2813 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2814 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2815 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2816 <        f.completeExceptionally(new CFException());
2817 <        checkCompletedWithWrappedCFException(g);
2818 <
2819 <        f = new CompletableFuture<>();
2820 <        f2 = new CompletableFuture<>();
2821 <        f2.completeExceptionally(new CFException());
2822 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2823 <        f.complete(one);
2824 <        checkCompletedWithWrappedCFException(g);
2825 <    }
2826 <
2827 <    /**
2828 <     * applyToEitherAsync result completes exceptionally if action does
2829 <     */
2830 <    public void testApplyToEitherAsync3E() {
2831 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2832 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2833 <        FailingFunction r = new FailingFunction();
2834 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2835 <        f.complete(one);
2836 <        checkCompletedWithWrappedCFException(g);
2837 <    }
2838 <
2839 <    /**
2840 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2841 <     */
2842 <    public void testApplyToEitherAsync4E() {
2843 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2844 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2845 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2846 <        assertTrue(f.cancel(true));
2847 <        checkCompletedWithWrappedCancellationException(g);
2385 >        checkCancelled(g);
2386 >        assertEquals(0, r.invocationCount);
2387 >        checkCompletedNormally(f, v1);
2388 >        checkCompletedWithWrappedCancellationException(h);
2389 >    }}
2390  
2391 <        f = new CompletableFuture<>();
2392 <        f2 = new CompletableFuture<>();
2393 <        assertTrue(f2.cancel(true));
2394 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2395 <        checkCompletedWithWrappedCancellationException(g);
2396 <    }
2391 >    public void testRunAfterEither_sourceCancelled3() {
2392 >        for (ExecutionMode m : ExecutionMode.values())
2393 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2394 >        for (Integer v1 : new Integer[] { 1, null })
2395 >    {
2396 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2397 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2398 >        final Noop r = new Noop(m);
2399  
2400 <    /**
2401 <     * acceptEitherAsync result completes normally after normal
2402 <     * completion of sources
2859 <     */
2860 <    public void testAcceptEitherAsyncE() {
2861 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2862 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2863 <        IncAction r = new IncAction();
2864 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2865 <        f.complete(one);
2866 <        checkCompletedNormally(g, null);
2867 <        assertEquals(r.value, 2);
2400 >        assertTrue(g.cancel(mayInterruptIfRunning));
2401 >        f.complete(v1);
2402 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2403  
2404 <        r = new IncAction();
2405 <        f = new CompletableFuture<>();
2406 <        f.complete(one);
2407 <        f2 = new CompletableFuture<>();
2408 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2409 <        checkCompletedNormally(g, null);
2410 <        assertEquals(r.value, 2);
2411 <    }
2404 >        // unspecified behavior
2405 >        Integer v;
2406 >        try {
2407 >            assertNull(h.join());
2408 >            assertEquals(1, r.invocationCount);
2409 >        } catch (CompletionException ok) {
2410 >            checkCompletedWithWrappedCancellationException(h);
2411 >            assertEquals(0, r.invocationCount);
2412 >        }
2413  
2414 <    /**
2415 <     * acceptEitherAsync result completes exceptionally after exceptional
2416 <     * completion of source
2881 <     */
2882 <    public void testAcceptEitherAsync2E() {
2883 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2884 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2885 <        IncAction r = new IncAction();
2886 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2887 <        f.completeExceptionally(new CFException());
2888 <        checkCompletedWithWrappedCFException(g);
2414 >        checkCancelled(g);
2415 >        checkCompletedNormally(f, v1);
2416 >    }}
2417  
2418 <        r = new IncAction();
2419 <        f = new CompletableFuture<>();
2420 <        f2 = new CompletableFuture<>();
2421 <        f2.completeExceptionally(new CFException());
2422 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2423 <        f.complete(one);
2424 <        checkCompletedWithWrappedCFException(g);
2425 <    }
2418 >    public void testRunAfterEither_sourceCancelled4() {
2419 >        for (ExecutionMode m : ExecutionMode.values())
2420 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2421 >        for (Integer v1 : new Integer[] { 1, null })
2422 >    {
2423 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2424 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2425 >        final Noop r = new Noop(m);
2426  
2427 <    /**
2428 <     * acceptEitherAsync result completes exceptionally if action does
2429 <     */
2902 <    public void testAcceptEitherAsync3E() {
2903 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2904 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2905 <        FailingConsumer r = new FailingConsumer();
2906 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2907 <        f.complete(one);
2908 <        checkCompletedWithWrappedCFException(g);
2909 <    }
2427 >        assertTrue(f.cancel(mayInterruptIfRunning));
2428 >        g.complete(v1);
2429 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2430  
2431 <    /**
2432 <     * acceptEitherAsync result completes exceptionally if either
2433 <     * source cancelled
2434 <     */
2435 <    public void testAcceptEitherAsync4E() {
2436 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2437 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2438 <        IncAction r = new IncAction();
2439 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2920 <        assertTrue(f.cancel(true));
2921 <        checkCompletedWithWrappedCancellationException(g);
2431 >        // unspecified behavior
2432 >        Integer v;
2433 >        try {
2434 >            assertNull(h.join());
2435 >            assertEquals(1, r.invocationCount);
2436 >        } catch (CompletionException ok) {
2437 >            checkCompletedWithWrappedCancellationException(h);
2438 >            assertEquals(0, r.invocationCount);
2439 >        }
2440  
2441 <        r = new IncAction();
2442 <        f = new CompletableFuture<>();
2443 <        f2 = new CompletableFuture<>();
2926 <        assertTrue(f2.cancel(true));
2927 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2928 <        checkCompletedWithWrappedCancellationException(g);
2929 <    }
2441 >        checkCancelled(f);
2442 >        checkCompletedNormally(g, v1);
2443 >    }}
2444  
2445      /**
2446 <     * runAfterEitherAsync result completes normally after normal
2933 <     * completion of sources
2446 >     * thenCompose result completes normally after normal completion of source
2447       */
2448 <    public void testRunAfterEitherAsyncE() {
2449 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2450 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2451 <        Noop r = new Noop();
2452 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2453 <        f.complete(one);
2454 <        checkCompletedNormally(g, null);
2455 <        assertTrue(r.ran);
2448 >    public void testThenCompose_normalCompletion() {
2449 >        for (ExecutionMode m : ExecutionMode.values())
2450 >        for (boolean createIncomplete : new boolean[] { true, false })
2451 >        for (Integer v1 : new Integer[] { 1, null })
2452 >    {
2453 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2454 >        final CompletableFutureInc r = new CompletableFutureInc();
2455 >        if (!createIncomplete) f.complete(v1);
2456 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2457 >        if (createIncomplete) f.complete(v1);
2458  
2459 <        r = new Noop();
2460 <        f = new CompletableFuture<>();
2461 <        f.complete(one);
2462 <        f2 = new CompletableFuture<>();
2948 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2949 <        checkCompletedNormally(g, null);
2950 <        assertTrue(r.ran);
2951 <    }
2459 >        checkCompletedNormally(g, inc(v1));
2460 >        checkCompletedNormally(f, v1);
2461 >        assertEquals(1, r.invocationCount);
2462 >    }}
2463  
2464      /**
2465 <     * runAfterEitherAsync result completes exceptionally after exceptional
2465 >     * thenCompose result completes exceptionally after exceptional
2466       * completion of source
2467       */
2468 <    public void testRunAfterEitherAsync2E() {
2469 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2470 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2471 <        Noop r = new Noop();
2472 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2473 <        f.completeExceptionally(new CFException());
2474 <        checkCompletedWithWrappedCFException(g);
2475 <
2476 <        r = new Noop();
2477 <        f = new CompletableFuture<>();
2967 <        f2 = new CompletableFuture<>();
2968 <        f2.completeExceptionally(new CFException());
2969 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2970 <        f.complete(one);
2971 <        checkCompletedWithWrappedCFException(g);
2972 <    }
2973 <
2974 <    /**
2975 <     * runAfterEitherAsync result completes exceptionally if action does
2976 <     */
2977 <    public void testRunAfterEitherAsync3E() {
2978 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2979 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2980 <        FailingNoop r = new FailingNoop();
2981 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2982 <        f.complete(one);
2983 <        checkCompletedWithWrappedCFException(g);
2984 <    }
2985 <
2986 <    /**
2987 <     * runAfterEitherAsync result completes exceptionally if either
2988 <     * source cancelled
2989 <     */
2990 <    public void testRunAfterEitherAsync4E() {
2991 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2992 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2993 <        Noop r = new Noop();
2994 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2995 <        assertTrue(f.cancel(true));
2996 <        checkCompletedWithWrappedCancellationException(g);
2468 >    public void testThenCompose_exceptionalCompletion() {
2469 >        for (ExecutionMode m : ExecutionMode.values())
2470 >        for (boolean createIncomplete : new boolean[] { true, false })
2471 >    {
2472 >        final CFException ex = new CFException();
2473 >        final CompletableFutureInc r = new CompletableFutureInc();
2474 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2475 >        if (!createIncomplete) f.completeExceptionally(ex);
2476 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2477 >        if (createIncomplete) f.completeExceptionally(ex);
2478  
2479 <        r = new Noop();
2480 <        f = new CompletableFuture<>();
2481 <        f2 = new CompletableFuture<>();
2482 <        assertTrue(f2.cancel(true));
3002 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3003 <        checkCompletedWithWrappedCancellationException(g);
3004 <    }
2479 >        checkCompletedWithWrappedCFException(g, ex);
2480 >        checkCompletedWithWrappedCFException(f, ex);
2481 >        assertEquals(0, r.invocationCount);
2482 >    }}
2483  
2484      /**
2485 <     * thenComposeAsync result completes normally after normal
3008 <     * completion of source
2485 >     * thenCompose result completes exceptionally if action does
2486       */
2487 <    public void testThenComposeAsyncE() {
2488 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2489 <        CompletableFutureInc r = new CompletableFutureInc();
2490 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2491 <        f.complete(one);
2492 <        checkCompletedNormally(g, two);
2493 <    }
2487 >    public void testThenCompose_actionFailed() {
2488 >        for (ExecutionMode m : ExecutionMode.values())
2489 >        for (boolean createIncomplete : new boolean[] { true, false })
2490 >        for (Integer v1 : new Integer[] { 1, null })
2491 >    {
2492 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2493 >        final FailingCompletableFutureFunction r
2494 >            = new FailingCompletableFutureFunction();
2495 >        if (!createIncomplete) f.complete(v1);
2496 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2497 >        if (createIncomplete) f.complete(v1);
2498  
3018    /**
3019     * thenComposeAsync result completes exceptionally after
3020     * exceptional completion of source
3021     */
3022    public void testThenComposeAsync2E() {
3023        CompletableFuture<Integer> f = new CompletableFuture<>();
3024        CompletableFutureInc r = new CompletableFutureInc();
3025        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3026        f.completeExceptionally(new CFException());
2499          checkCompletedWithWrappedCFException(g);
2500 <    }
2500 >        checkCompletedNormally(f, v1);
2501 >    }}
2502  
2503      /**
2504 <     * thenComposeAsync result completes exceptionally if action does
2504 >     * thenCompose result completes exceptionally if source cancelled
2505       */
2506 <    public void testThenComposeAsync3E() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2508 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2509 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2510 <        f.complete(one);
2511 <        checkCompletedWithWrappedCFException(g);
2512 <    }
2506 >    public void testThenCompose_sourceCancelled() {
2507 >        for (ExecutionMode m : ExecutionMode.values())
2508 >        for (boolean createIncomplete : new boolean[] { true, false })
2509 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2510 >    {
2511 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 >        final CompletableFutureInc r = new CompletableFutureInc();
2513 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2514 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2515 >        if (createIncomplete) {
2516 >            checkIncomplete(g);
2517 >            assertTrue(f.cancel(mayInterruptIfRunning));
2518 >        }
2519  
3041    /**
3042     * thenComposeAsync result completes exceptionally if source cancelled
3043     */
3044    public void testThenComposeAsync4E() {
3045        CompletableFuture<Integer> f = new CompletableFuture<>();
3046        CompletableFutureInc r = new CompletableFutureInc();
3047        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3048        assertTrue(f.cancel(true));
2520          checkCompletedWithWrappedCancellationException(g);
2521 <    }
2521 >        checkCancelled(f);
2522 >    }}
2523  
2524      // other static methods
2525  
# Line 3247 | Line 2719 | public class CompletableFutureTest exten
2719       * whenComplete action executes on normal completion, propagating
2720       * source result.
2721       */
2722 <    public void testWhenComplete1() {
2723 <        final AtomicInteger a = new AtomicInteger();
2724 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2725 <        CompletableFuture<Integer> g =
2726 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
2727 <        f.complete(three);
2728 <        checkCompletedNormally(f, three);
2729 <        checkCompletedNormally(g, three);
2730 <        assertEquals(a.get(), 1);
2731 <    }
2732 <
2733 <    /**
2734 <     * whenComplete action executes on exceptional completion, propagating
2735 <     * source result.
2736 <     */
2737 <    public void testWhenComplete2() {
3266 <        final AtomicInteger a = new AtomicInteger();
3267 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3268 <        CompletableFuture<Integer> g =
3269 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3270 <        f.completeExceptionally(new CFException());
3271 <        assertTrue(f.isCompletedExceptionally());
3272 <        assertTrue(g.isCompletedExceptionally());
3273 <        assertEquals(a.get(), 1);
3274 <    }
3275 <
3276 <    /**
3277 <     * If a whenComplete action throws an exception when triggered by
3278 <     * a normal completion, it completes exceptionally
3279 <     */
3280 <    public void testWhenComplete3() {
3281 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3282 <        CompletableFuture<Integer> g =
3283 <            f.whenComplete((Integer x, Throwable t) ->
3284 <                           { throw new CFException(); } );
3285 <        f.complete(three);
3286 <        checkCompletedNormally(f, three);
3287 <        assertTrue(g.isCompletedExceptionally());
3288 <        checkCompletedWithWrappedCFException(g);
3289 <    }
3290 <
3291 <    /**
3292 <     * whenCompleteAsync action executes on normal completion, propagating
3293 <     * source result.
3294 <     */
3295 <    public void testWhenCompleteAsync1() {
3296 <        final AtomicInteger a = new AtomicInteger();
3297 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3298 <        CompletableFuture<Integer> g =
3299 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3300 <        f.complete(three);
3301 <        checkCompletedNormally(f, three);
3302 <        checkCompletedNormally(g, three);
3303 <        assertEquals(a.get(), 1);
3304 <    }
3305 <
3306 <    /**
3307 <     * whenCompleteAsync action executes on exceptional completion, propagating
3308 <     * source result.
3309 <     */
3310 <    public void testWhenCompleteAsync2() {
3311 <        final AtomicInteger a = new AtomicInteger();
3312 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 <        CompletableFuture<Integer> g =
3314 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3315 <        f.completeExceptionally(new CFException());
3316 <        checkCompletedWithWrappedCFException(f);
3317 <        checkCompletedWithWrappedCFException(g);
3318 <    }
2722 >    public void testWhenComplete_normalCompletion1() {
2723 >        for (ExecutionMode m : ExecutionMode.values())
2724 >        for (boolean createIncomplete : new boolean[] { true, false })
2725 >        for (Integer v1 : new Integer[] { 1, null })
2726 >    {
2727 >        final AtomicInteger a = new AtomicInteger(0);
2728 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2729 >        if (!createIncomplete) f.complete(v1);
2730 >        final CompletableFuture<Integer> g = m.whenComplete
2731 >            (f,
2732 >             (Integer x, Throwable t) -> {
2733 >                threadAssertSame(x, v1);
2734 >                threadAssertNull(t);
2735 >                a.getAndIncrement();
2736 >            });
2737 >        if (createIncomplete) f.complete(v1);
2738  
2739 <    /**
2740 <     * If a whenCompleteAsync action throws an exception when
2741 <     * triggered by a normal completion, it completes exceptionally
2742 <     */
3324 <    public void testWhenCompleteAsync3() {
3325 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3326 <        CompletableFuture<Integer> g =
3327 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3328 <                           { throw new CFException(); } );
3329 <        f.complete(three);
3330 <        checkCompletedNormally(f, three);
3331 <        checkCompletedWithWrappedCFException(g);
3332 <    }
2739 >        checkCompletedNormally(g, v1);
2740 >        checkCompletedNormally(f, v1);
2741 >        assertEquals(1, a.get());
2742 >    }}
2743  
2744      /**
2745 <     * whenCompleteAsync action executes on normal completion, propagating
2745 >     * whenComplete action executes on exceptional completion, propagating
2746       * source result.
2747       */
2748 <    public void testWhenCompleteAsync1e() {
2749 <        final AtomicInteger a = new AtomicInteger();
2750 <        ThreadExecutor exec = new ThreadExecutor();
2751 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2752 <        CompletableFuture<Integer> g =
2753 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
2754 <                                exec);
2755 <        f.complete(three);
2756 <        checkCompletedNormally(f, three);
2757 <        checkCompletedNormally(g, three);
2758 <        assertEquals(a.get(), 1);
2759 <    }
2748 >    public void testWhenComplete_exceptionalCompletion() {
2749 >        for (ExecutionMode m : ExecutionMode.values())
2750 >        for (boolean createIncomplete : new boolean[] { true, false })
2751 >        for (Integer v1 : new Integer[] { 1, null })
2752 >    {
2753 >        final AtomicInteger a = new AtomicInteger(0);
2754 >        final CFException ex = new CFException();
2755 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2756 >        if (!createIncomplete) f.completeExceptionally(ex);
2757 >        final CompletableFuture<Integer> g = m.whenComplete
2758 >            (f,
2759 >             (Integer x, Throwable t) -> {
2760 >                threadAssertNull(x);
2761 >                threadAssertSame(t, ex);
2762 >                a.getAndIncrement();
2763 >            });
2764 >        if (createIncomplete) f.completeExceptionally(ex);
2765 >        checkCompletedWithWrappedCFException(f, ex);
2766 >        checkCompletedWithWrappedCFException(g, ex);
2767 >        assertEquals(1, a.get());
2768 >    }}
2769  
2770      /**
2771 <     * whenCompleteAsync action executes on exceptional completion, propagating
2772 <     * source result.
2771 >     * whenComplete action executes on cancelled source, propagating
2772 >     * CancellationException.
2773       */
2774 <    public void testWhenCompleteAsync2e() {
2775 <        final AtomicInteger a = new AtomicInteger();
2776 <        ThreadExecutor exec = new ThreadExecutor();
2777 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2778 <        CompletableFuture<Integer> g =
2779 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
2780 <                                exec);
2781 <        f.completeExceptionally(new CFException());
2782 <        checkCompletedWithWrappedCFException(f);
2783 <        checkCompletedWithWrappedCFException(g);
2784 <    }
2774 >    public void testWhenComplete_sourceCancelled() {
2775 >        for (ExecutionMode m : ExecutionMode.values())
2776 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2777 >        for (boolean createIncomplete : new boolean[] { true, false })
2778 >    {
2779 >        final AtomicInteger a = new AtomicInteger(0);
2780 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2781 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2782 >        final CompletableFuture<Integer> g = m.whenComplete
2783 >            (f,
2784 >             (Integer x, Throwable t) -> {
2785 >                threadAssertNull(x);
2786 >                threadAssertTrue(t instanceof CancellationException);
2787 >                a.getAndIncrement();
2788 >            });
2789 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2790  
2791 <    /**
2792 <     * If a whenCompleteAsync action throws an exception when triggered
2793 <     * by a normal completion, it completes exceptionally
2794 <     */
2795 <    public void testWhenCompleteAsync3e() {
3372 <        ThreadExecutor exec = new ThreadExecutor();
3373 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3374 <        CompletableFuture<Integer> g =
3375 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3376 <                                { throw new CFException(); },
3377 <                                exec);
3378 <        f.complete(three);
3379 <        checkCompletedNormally(f, three);
3380 <        checkCompletedWithWrappedCFException(g);
3381 <    }
2791 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2792 >        checkCompletedWithWrappedCancellationException(g);
2793 >        checkCancelled(f);
2794 >        assertEquals(1, a.get());
2795 >    }}
2796  
2797      /**
2798 <     * handleAsync action completes normally with function value on
2799 <     * either normal or exceptional completion of source
2798 >     * If a whenComplete action throws an exception when triggered by
2799 >     * a normal completion, it completes exceptionally
2800       */
2801 <    public void testHandleAsync() {
2802 <        CompletableFuture<Integer> f, g;
2803 <        IntegerHandler r;
2804 <
2805 <        f = new CompletableFuture<>();
2806 <        g = f.handleAsync(r = new IntegerHandler());
2807 <        assertFalse(r.ran);
2808 <        f.completeExceptionally(new CFException());
2809 <        checkCompletedWithWrappedCFException(f);
2810 <        checkCompletedNormally(g, three);
2811 <        assertTrue(r.ran);
2812 <
2813 <        f = new CompletableFuture<>();
2814 <        g = f.handleAsync(r = new IntegerHandler());
2815 <        assertFalse(r.ran);
2816 <        f.completeExceptionally(new CFException());
2817 <        checkCompletedWithWrappedCFException(f);
2818 <        checkCompletedNormally(g, three);
2819 <        assertTrue(r.ran);
2820 <
2821 <        f = new CompletableFuture<>();
2822 <        g = f.handleAsync(r = new IntegerHandler());
3409 <        assertFalse(r.ran);
3410 <        f.complete(one);
3411 <        checkCompletedNormally(f, one);
3412 <        checkCompletedNormally(g, two);
3413 <        assertTrue(r.ran);
3414 <
3415 <        f = new CompletableFuture<>();
3416 <        g = f.handleAsync(r = new IntegerHandler());
3417 <        assertFalse(r.ran);
3418 <        f.complete(one);
3419 <        checkCompletedNormally(f, one);
3420 <        checkCompletedNormally(g, two);
3421 <        assertTrue(r.ran);
3422 <    }
2801 >    public void testWhenComplete_actionFailed() {
2802 >        for (boolean createIncomplete : new boolean[] { true, false })
2803 >        for (ExecutionMode m : ExecutionMode.values())
2804 >        for (Integer v1 : new Integer[] { 1, null })
2805 >    {
2806 >        final AtomicInteger a = new AtomicInteger(0);
2807 >        final CFException ex = new CFException();
2808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2809 >        if (!createIncomplete) f.complete(v1);
2810 >        final CompletableFuture<Integer> g = m.whenComplete
2811 >            (f,
2812 >             (Integer x, Throwable t) -> {
2813 >                threadAssertSame(x, v1);
2814 >                threadAssertNull(t);
2815 >                a.getAndIncrement();
2816 >                throw ex;
2817 >            });
2818 >        if (createIncomplete) f.complete(v1);
2819 >        checkCompletedNormally(f, v1);
2820 >        checkCompletedWithWrappedCFException(g, ex);
2821 >        assertEquals(1, a.get());
2822 >    }}
2823  
2824      /**
2825 <     * handleAsync action with Executor completes normally with
2826 <     * function value on either normal or exceptional completion of
2827 <     * source
2825 >     * If a whenComplete action throws an exception when triggered by
2826 >     * a source completion that also throws an exception, the source
2827 >     * exception takes precedence.
2828       */
2829 <    public void testHandleAsync2() {
2830 <        CompletableFuture<Integer> f, g;
2831 <        ThreadExecutor exec = new ThreadExecutor();
2832 <        IntegerHandler r;
2833 <
2834 <        f = new CompletableFuture<>();
2835 <        g = f.handleAsync(r = new IntegerHandler(), exec);
2836 <        assertFalse(r.ran);
2837 <        f.completeExceptionally(new CFException());
3438 <        checkCompletedWithWrappedCFException(f);
3439 <        checkCompletedNormally(g, three);
3440 <        assertTrue(r.ran);
3441 <
3442 <        f = new CompletableFuture<>();
3443 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3444 <        assertFalse(r.ran);
3445 <        f.completeExceptionally(new CFException());
3446 <        checkCompletedWithWrappedCFException(f);
3447 <        checkCompletedNormally(g, three);
3448 <        assertTrue(r.ran);
3449 <
3450 <        f = new CompletableFuture<>();
3451 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3452 <        assertFalse(r.ran);
3453 <        f.complete(one);
3454 <        checkCompletedNormally(f, one);
3455 <        checkCompletedNormally(g, two);
3456 <        assertTrue(r.ran);
2829 >    public void testWhenComplete_actionFailedSourceFailed() {
2830 >        for (boolean createIncomplete : new boolean[] { true, false })
2831 >        for (ExecutionMode m : ExecutionMode.values())
2832 >        for (Integer v1 : new Integer[] { 1, null })
2833 >    {
2834 >        final AtomicInteger a = new AtomicInteger(0);
2835 >        final CFException ex1 = new CFException();
2836 >        final CFException ex2 = new CFException();
2837 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2838  
2839 <        f = new CompletableFuture<>();
2840 <        g = f.handleAsync(r = new IntegerHandler(), exec);
2841 <        assertFalse(r.ran);
2842 <        f.complete(one);
2843 <        checkCompletedNormally(f, one);
2844 <        checkCompletedNormally(g, two);
2845 <        assertTrue(r.ran);
2846 <    }
2839 >        if (!createIncomplete) f.completeExceptionally(ex1);
2840 >        final CompletableFuture<Integer> g = m.whenComplete
2841 >            (f,
2842 >             (Integer x, Throwable t) -> {
2843 >                threadAssertSame(t, ex1);
2844 >                threadAssertNull(x);
2845 >                a.getAndIncrement();
2846 >                throw ex2;
2847 >            });
2848 >        if (createIncomplete) f.completeExceptionally(ex1);
2849 >
2850 >        checkCompletedWithWrappedCFException(f, ex1);
2851 >        checkCompletedWithWrappedCFException(g, ex1);
2852 >        assertEquals(1, a.get());
2853 >    }}
2854  
2855   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines