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.38 by jsr166, Sun Jun 1 23:51:44 2014 UTC vs.
Revision 1.56 by jsr166, Mon Jun 2 22:20:32 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines