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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines