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.30 by jsr166, Mon Jul 22 18:25:42 2013 UTC vs.
Revision 1.57 by jsr166, Mon Jun 2 22:48:50 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19 + import java.util.concurrent.CompletionStage;
20 + import java.util.concurrent.ForkJoinPool;
21 + import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.TimeoutException;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 102 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 +                                              CFException ex) {
110 +        try {
111 +            f.get(LONG_DELAY_MS, MILLISECONDS);
112 +            shouldThrow();
113 +        } catch (ExecutionException success) {
114 +            assertSame(ex, success.getCause());
115 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
116 +        try {
117 +            f.join();
118 +            shouldThrow();
119 +        } catch (CompletionException success) {
120 +            assertSame(ex, success.getCause());
121 +        }
122 +        try {
123 +            f.getNow(null);
124 +            shouldThrow();
125 +        } catch (CompletionException success) {
126 +            assertSame(ex, success.getCause());
127 +        }
128 +        try {
129 +            f.get();
130 +            shouldThrow();
131 +        } catch (ExecutionException success) {
132 +            assertSame(ex, success.getCause());
133 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +        assertTrue(f.isDone());
135 +        assertFalse(f.isCancelled());
136 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
137 +    }
138 +
139      void checkCancelled(CompletableFuture<?> f) {
140          try {
141              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 215 | 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 247 | 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 286 | Line 322 | public class CompletableFutureTest exten
322  
323      // Choose non-commutative actions for better coverage
324  
325 +    // A non-commutative function that handles and produces null values as well.
326 +    static Integer subtract(Integer x, Integer y) {
327 +        return (x == null && y == null) ? null :
328 +            ((x == null) ? 42 : x.intValue())
329 +            - ((y == null) ? 99 : y.intValue());
330 +    }
331 +
332 +    // A function that handles and produces null values as well.
333 +    static Integer inc(Integer x) {
334 +        return (x == null) ? null : x + 1;
335 +    }
336 +
337      static final Supplier<Integer> supplyOne =
338          () -> Integer.valueOf(1);
339      static final Function<Integer, Integer> inc =
340          (Integer x) -> Integer.valueOf(x.intValue() + 1);
341      static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
342 >        (Integer x, Integer y) -> subtract(x, y);
343      static final class IncAction implements Consumer<Integer> {
344 <        int value;
345 <        public void accept(Integer x) { value = x.intValue() + 1; }
344 >        int invocationCount = 0;
345 >        Integer value;
346 >        public void accept(Integer x) {
347 >            invocationCount++;
348 >            value = inc(x);
349 >        }
350 >    }
351 >    static final class IncFunction implements Function<Integer,Integer> {
352 >        final ExecutionMode m;
353 >        int invocationCount = 0;
354 >        Integer value;
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 <        int value;
363 >        final ExecutionMode m;
364 >        int invocationCount = 0;
365 >        Integer value;
366 >        // Check this action was invoked exactly once when result is computed.
367 >        SubtractAction(ExecutionMode m) { this.m = m; }
368          public void accept(Integer x, Integer y) {
369 <            value = x.intValue() - y.intValue();
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 >        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();
493 >            new Thread(tg, r).start();
494          }
495      }
496  
497 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
498 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
499 <    }
497 >    /**
498 >     * Permits the testing of parallel code for the 3 different
499 >     * execution modes without repeating all the testing code.
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);
539 >            }
540 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
541 >                (CompletableFuture<T> f,
542 >                 CompletionStage<? extends U> g,
543 >                 BiConsumer<? super T,? super U> a) {
544 >                return f.thenAcceptBoth(g, a);
545 >            }
546 >            public <T,U,V> CompletableFuture<V> thenCombine
547 >                (CompletableFuture<T> f,
548 >                 CompletionStage<? extends U> g,
549 >                 BiFunction<? super T,? super U,? extends V> a) {
550 >                return f.thenCombine(g, a);
551 >            }
552 >            public <T> CompletableFuture<Void> runAfterEither
553 >                (CompletableFuture<T> f,
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,
560 >                 CompletionStage<? extends T> g,
561 >                 Consumer<? super T> a) {
562 >                return f.acceptEither(g, a);
563 >            }
564 >            public <T,U> CompletableFuture<U> applyToEither
565 >                (CompletableFuture<T> f,
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.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.whenCompleteAsync(a);
606 >            }
607 >            public <T,U> CompletableFuture<Void> runAfterBoth
608 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
609 >                return f.runAfterBothAsync(g, a);
610 >            }
611 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
612 >                (CompletableFuture<T> f,
613 >                 CompletionStage<? extends U> g,
614 >                 BiConsumer<? super T,? super U> a) {
615 >                return f.thenAcceptBothAsync(g, a);
616 >            }
617 >            public <T,U,V> CompletableFuture<V> thenCombine
618 >                (CompletableFuture<T> f,
619 >                 CompletionStage<? extends U> g,
620 >                 BiFunction<? super T,? super U,? extends V> a) {
621 >                return f.thenCombineAsync(g, a);
622 >            }
623 >            public <T> CompletableFuture<Void> runAfterEither
624 >                (CompletableFuture<T> f,
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,
631 >                 CompletionStage<? extends T> g,
632 >                 Consumer<? super T> a) {
633 >                return f.acceptEitherAsync(g, a);
634 >            }
635 >            public <T,U> CompletableFuture<U> applyToEither
636 >                (CompletableFuture<T> f,
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, 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, new ThreadExecutor());
676 >            }
677 >            public <T,U> CompletableFuture<Void> runAfterBoth
678 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
679 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
680 >            }
681 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
682 >                (CompletableFuture<T> f,
683 >                 CompletionStage<? extends U> g,
684 >                 BiConsumer<? super T,? super U> a) {
685 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
686 >            }
687 >            public <T,U,V> CompletableFuture<V> thenCombine
688 >                (CompletableFuture<T> f,
689 >                 CompletionStage<? extends U> g,
690 >                 BiFunction<? super T,? super U,? extends V> a) {
691 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
692 >            }
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> CompletableFuture<Void> acceptEither
700 >                (CompletableFuture<T> f,
701 >                 CompletionStage<? extends T> g,
702 >                 Consumer<? super T> a) {
703 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
704 >            }
705 >            public <T,U> CompletableFuture<U> applyToEither
706 >                (CompletableFuture<T> f,
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
733 >            (CompletableFuture<T> f,
734 >             CompletionStage<? extends U> g,
735 >             BiConsumer<? super T,? super U> a);
736 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
737 >            (CompletableFuture<T> f,
738 >             CompletionStage<? extends U> g,
739 >             BiFunction<? super T,? super U,? extends V> a);
740 >        public abstract <T> CompletableFuture<Void> runAfterEither
741 >            (CompletableFuture<T> f,
742 >             CompletionStage<?> g,
743 >             java.lang.Runnable a);
744 >        public abstract <T> CompletableFuture<Void> acceptEither
745 >            (CompletableFuture<T> f,
746 >             CompletionStage<? extends T> g,
747 >             Consumer<? super T> a);
748 >        public abstract <T,U> CompletableFuture<U> applyToEither
749 >            (CompletableFuture<T> f,
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  
368    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
369        boolean ran;
370        public Integer apply(Integer x, Throwable t) {
371            ran = true;
372            return (t == null) ? two : three;
373        }
374    }
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);
789 <
790 <        f = new CompletableFuture<>();
791 <        r = new ExceptionToInteger();
792 <        g = f.exceptionally(r);
793 <        f.complete(one);
794 <        checkCompletedNormally(g, one);
795 <    }
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 >        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 normal
828 >     * completion of source
829 >     */
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 >        checkCompletedNormally(g, inc(v1));
850 >        checkCompletedNormally(f, v1);
851 >        assertEquals(1, a.get());
852 >    }}
853  
854      /**
855 <     * handle action completes normally with function value on either
856 <     * normal or exceptional completion of source
855 >     * handle action completes normally with function value on
856 >     * exceptional completion of source
857       */
858 <    public void testHandle() {
859 <        CompletableFuture<Integer> f, g;
860 <        IntegerHandler r;
861 <
862 <        f = new CompletableFuture<>();
863 <        f.completeExceptionally(new CFException());
864 <        g = f.handle(r = new IntegerHandler());
865 <        assertTrue(r.ran);
866 <        checkCompletedNormally(g, three);
867 <
868 <        f = new CompletableFuture<>();
869 <        g = f.handle(r = new IntegerHandler());
870 <        assertFalse(r.ran);
871 <        f.completeExceptionally(new CFException());
872 <        checkCompletedNormally(g, three);
873 <        assertTrue(r.ran);
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 >        checkCompletedNormally(g, v1);
879 >        checkCompletedWithWrappedCFException(f, ex);
880 >        assertEquals(1, a.get());
881 >    }}
882 >
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 <        f = new CompletableFuture<>();
908 <        f.complete(one);
909 <        g = f.handle(r = new IntegerHandler());
910 <        assertTrue(r.ran);
419 <        checkCompletedNormally(g, two);
420 <
421 <        f = new CompletableFuture<>();
422 <        g = f.handle(r = new IntegerHandler());
423 <        assertFalse(r.ran);
424 <        f.complete(one);
425 <        assertTrue(r.ran);
426 <        checkCompletedNormally(g, two);
427 <    }
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());
448        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 484 | 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 495 | 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  
509        f = new CompletableFuture<>();
510        f.complete(null);
511        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);
535 <        assertFalse(r.ran);
536 <    }
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);
555 <    }
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());
573 <        checkCompletedWithWrappedCancellationException(g);
574 <    }
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() {
601 <        CompletableFuture<Integer> f = new CompletableFuture<>();
602 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
603 <        f.complete(one);
604 <        checkCompletedWithWrappedCFException(g);
605 <    }
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, 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() {
1300 <        CompletableFuture<Integer> f, g, h;
1301 <
1302 <        f = new CompletableFuture<>();
1303 <        g = new CompletableFuture<>();
1304 <        h = f.thenCombine(g, subtract);
1305 <        f.complete(3);
1306 <        checkIncomplete(h);
1307 <        g.complete(1);
1308 <        checkCompletedNormally(h, 2);
1309 <
1310 <        f = new CompletableFuture<>();
1311 <        g = new CompletableFuture<>();
1312 <        h = f.thenCombine(g, subtract);
1313 <        g.complete(1);
1314 <        checkIncomplete(h);
1315 <        f.complete(3);
1316 <        checkCompletedNormally(h, 2);
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 >    {
1306 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1307 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1308 >        final SubtractFunction r = new SubtractFunction(m);
1309 >
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 <        f = new CompletableFuture<>();
1321 <        g = new CompletableFuture<>();
1322 <        g.complete(1);
1323 <        f.complete(3);
1324 <        h = f.thenCombine(g, subtract);
692 <        checkCompletedNormally(h, 2);
693 <    }
1320 >        checkCompletedNormally(h, subtract(v1, v2));
1321 >        checkCompletedNormally(f, v1);
1322 >        checkCompletedNormally(g, v2);
1323 >        assertEquals(1, r.invocationCount);
1324 >    }}
1325  
1326      /**
1327       * thenCombine result completes exceptionally after exceptional
1328       * completion of either source
1329       */
1330 <    public void testThenCombine2() {
1331 <        CompletableFuture<Integer> f, g, h;
1332 <
1333 <        f = new CompletableFuture<>();
1334 <        g = new CompletableFuture<>();
1335 <        h = f.thenCombine(g, subtract);
1336 <        f.completeExceptionally(new CFException());
1337 <        checkIncomplete(h);
1338 <        g.complete(1);
1339 <        checkCompletedWithWrappedCFException(h);
1340 <
1341 <        f = new CompletableFuture<>();
1342 <        g = new CompletableFuture<>();
1343 <        h = f.thenCombine(g, subtract);
1344 <        g.completeExceptionally(new CFException());
1345 <        checkIncomplete(h);
1346 <        f.complete(3);
1347 <        checkCompletedWithWrappedCFException(h);
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 >    {
1336 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1337 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1338 >        final CFException ex = new CFException();
1339 >        final SubtractFunction r = new SubtractFunction(m);
1340 >
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 >        }
1349  
1350 <        f = new CompletableFuture<>();
1351 <        g = new CompletableFuture<>();
1352 <        f.complete(3);
1353 <        g.completeExceptionally(new CFException());
1354 <        h = f.thenCombine(g, subtract);
723 <        checkCompletedWithWrappedCFException(h);
724 <
725 <        f = new CompletableFuture<>();
726 <        g = new CompletableFuture<>();
727 <        f.completeExceptionally(new CFException());
728 <        g.complete(3);
729 <        h = f.thenCombine(g, subtract);
730 <        checkCompletedWithWrappedCFException(h);
731 <    }
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 testThenCombine3() {
1360 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1361 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1362 <        FailingBiFunction r = new FailingBiFunction();
1363 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1364 <        f.complete(one);
1365 <        checkIncomplete(g);
1366 <        assertFalse(r.ran);
1367 <        f2.complete(two);
1368 <        checkCompletedWithWrappedCFException(g);
1369 <        assertTrue(r.ran);
1370 <    }
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 >    {
1365 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1366 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1367 >        final FailingBiFunction r = new FailingBiFunction(m);
1368 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1369 >
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 >    }}
1382  
1383      /**
1384       * thenCombine result completes exceptionally if either source cancelled
1385       */
1386 <    public void testThenCombine4() {
1387 <        CompletableFuture<Integer> f, g, h;
1388 <
1389 <        f = new CompletableFuture<>();
1390 <        g = new CompletableFuture<>();
1391 <        h = f.thenCombine(g, subtract);
1392 <        assertTrue(f.cancel(true));
1393 <        checkIncomplete(h);
1394 <        g.complete(1);
1395 <        checkCompletedWithWrappedCancellationException(h);
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 >    {
1393 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1394 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1395 >        final SubtractFunction r = new SubtractFunction(m);
1396 >
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 >        if (createIncomplete) {
1402 >            checkIncomplete(h);
1403 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1404 >        }
1405  
763        f = new CompletableFuture<>();
764        g = new CompletableFuture<>();
765        h = f.thenCombine(g, subtract);
766        assertTrue(g.cancel(true));
767        checkIncomplete(h);
768        f.complete(3);
1406          checkCompletedWithWrappedCancellationException(h);
1407 <
1408 <        f = new CompletableFuture<>();
1409 <        g = new CompletableFuture<>();
1410 <        assertTrue(f.cancel(true));
774 <        assertTrue(g.cancel(true));
775 <        h = f.thenCombine(g, subtract);
776 <        checkCompletedWithWrappedCancellationException(h);
777 <    }
1407 >        checkCancelled(!fFirst ? f : g);
1408 >        assertEquals(0, r.invocationCount);
1409 >        checkCompletedNormally(fFirst ? f : g, v1);
1410 >    }}
1411  
1412      /**
1413       * thenAcceptBoth result completes normally after normal
1414       * completion of sources
1415       */
1416 <    public void testThenAcceptBoth() {
1417 <        CompletableFuture<Integer> f, g;
1418 <        CompletableFuture<Void> h;
1419 <        SubtractAction r;
1420 <
1421 <        f = new CompletableFuture<>();
1422 <        g = new CompletableFuture<>();
1423 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1424 <        f.complete(3);
1425 <        checkIncomplete(h);
1426 <        g.complete(1);
1427 <        checkCompletedNormally(h, null);
1428 <        assertEquals(r.value, 2);
1429 <
1430 <        f = new CompletableFuture<>();
1431 <        g = new CompletableFuture<>();
1432 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1433 <        g.complete(1);
1434 <        checkIncomplete(h);
1435 <        f.complete(3);
803 <        checkCompletedNormally(h, null);
804 <        assertEquals(r.value, 2);
1416 >    public void testThenAcceptBoth_normalCompletion() {
1417 >        for (ExecutionMode m : ExecutionMode.values())
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 SubtractAction r = new SubtractAction(m);
1426 >
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 >        }
1436  
806        f = new CompletableFuture<>();
807        g = new CompletableFuture<>();
808        g.complete(1);
809        f.complete(3);
810        h = f.thenAcceptBoth(g, r = new SubtractAction());
1437          checkCompletedNormally(h, null);
1438 <        assertEquals(r.value, 2);
1439 <    }
1438 >        assertEquals(subtract(v1, v2), r.value);
1439 >        checkCompletedNormally(f, v1);
1440 >        checkCompletedNormally(g, v2);
1441 >    }}
1442  
1443      /**
1444       * thenAcceptBoth result completes exceptionally after exceptional
1445       * completion of either source
1446       */
1447 <    public void testThenAcceptBoth2() {
1448 <        CompletableFuture<Integer> f, g;
1449 <        CompletableFuture<Void> h;
1450 <        SubtractAction r;
1451 <
1452 <        f = new CompletableFuture<>();
1453 <        g = new CompletableFuture<>();
1454 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1455 <        f.completeExceptionally(new CFException());
1456 <        checkIncomplete(h);
1457 <        g.complete(1);
1458 <        checkCompletedWithWrappedCFException(h);
1459 <
1460 <        f = new CompletableFuture<>();
1461 <        g = new CompletableFuture<>();
1462 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1463 <        g.completeExceptionally(new CFException());
1464 <        checkIncomplete(h);
1465 <        f.complete(3);
838 <        checkCompletedWithWrappedCFException(h);
1447 >    public void testThenAcceptBoth_exceptionalCompletion() {
1448 >        for (ExecutionMode m : ExecutionMode.values())
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<>();
1455 >        final CFException ex = new CFException();
1456 >        final SubtractAction r = new SubtractAction(m);
1457 >
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 >        }
1466  
1467 <        f = new CompletableFuture<>();
1468 <        g = new CompletableFuture<>();
1469 <        f.complete(3);
1470 <        g.completeExceptionally(new CFException());
1471 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
845 <        checkCompletedWithWrappedCFException(h);
846 <
847 <        f = new CompletableFuture<>();
848 <        g = new CompletableFuture<>();
849 <        f.completeExceptionally(new CFException());
850 <        g.complete(3);
851 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
852 <        checkCompletedWithWrappedCFException(h);
853 <    }
1467 >        checkCompletedWithWrappedCFException(h, ex);
1468 >        assertEquals(0, r.invocationCount);
1469 >        checkCompletedNormally(fFirst ? f : g, v1);
1470 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1471 >    }}
1472  
1473      /**
1474       * thenAcceptBoth result completes exceptionally if action does
1475       */
1476 <    public void testThenAcceptBoth3() {
1477 <        CompletableFuture<Integer> f, g;
1478 <        CompletableFuture<Void> h;
1479 <        FailingBiConsumer r;
1480 <
1481 <        f = new CompletableFuture<>();
1482 <        g = new CompletableFuture<>();
1483 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1484 <        f.complete(3);
1485 <        checkIncomplete(h);
1486 <        g.complete(1);
1487 <        checkCompletedWithWrappedCFException(h);
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 >    {
1482 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1483 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1484 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1485 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1486 >
1487 >        if (fFirst) {
1488 >            f.complete(v1);
1489 >            g.complete(v2);
1490 >        } else {
1491 >            g.complete(v2);
1492 >            f.complete(v1);
1493 >        }
1494  
871        f = new CompletableFuture<>();
872        g = new CompletableFuture<>();
873        f.complete(3);
874        g.complete(1);
875        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1495          checkCompletedWithWrappedCFException(h);
1496 <    }
1496 >        checkCompletedNormally(f, v1);
1497 >        checkCompletedNormally(g, v2);
1498 >    }}
1499  
1500      /**
1501       * thenAcceptBoth result completes exceptionally if either source cancelled
1502       */
1503 <    public void testThenAcceptBoth4() {
1504 <        CompletableFuture<Integer> f, g;
1505 <        CompletableFuture<Void> h;
1506 <        SubtractAction r;
1507 <
1508 <        f = new CompletableFuture<>();
1509 <        g = new CompletableFuture<>();
1510 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1511 <        assertTrue(f.cancel(true));
1512 <        checkIncomplete(h);
1513 <        g.complete(1);
1514 <        checkCompletedWithWrappedCancellationException(h);
1515 <
1516 <        f = new CompletableFuture<>();
1517 <        g = new CompletableFuture<>();
1518 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1519 <        assertTrue(g.cancel(true));
1520 <        checkIncomplete(h);
1521 <        f.complete(3);
901 <        checkCompletedWithWrappedCancellationException(h);
1503 >    public void testThenAcceptBoth_sourceCancelled() {
1504 >        for (ExecutionMode m : ExecutionMode.values())
1505 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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 SubtractAction r = new SubtractAction(m);
1513 >
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 >        }
1522  
903        f = new CompletableFuture<>();
904        g = new CompletableFuture<>();
905        f.complete(3);
906        assertTrue(g.cancel(true));
907        h = f.thenAcceptBoth(g, r = new SubtractAction());
1523          checkCompletedWithWrappedCancellationException(h);
1524 <
1525 <        f = new CompletableFuture<>();
1526 <        g = new CompletableFuture<>();
1527 <        assertTrue(f.cancel(true));
913 <        g.complete(3);
914 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
915 <        checkCompletedWithWrappedCancellationException(h);
916 <    }
1524 >        checkCancelled(!fFirst ? f : g);
1525 >        assertEquals(0, r.invocationCount);
1526 >        checkCompletedNormally(fFirst ? f : g, v1);
1527 >    }}
1528  
1529      /**
1530       * runAfterBoth result completes normally after normal
1531       * completion of sources
1532       */
1533 <    public void testRunAfterBoth() {
1534 <        CompletableFuture<Integer> f, g;
1535 <        CompletableFuture<Void> h;
1536 <        Noop r;
1537 <
1538 <        f = new CompletableFuture<>();
1539 <        g = new CompletableFuture<>();
1540 <        h = f.runAfterBoth(g, r = new Noop());
1541 <        f.complete(3);
1542 <        checkIncomplete(h);
1543 <        g.complete(1);
1544 <        checkCompletedNormally(h, null);
1545 <        assertTrue(r.ran);
1546 <
1547 <        f = new CompletableFuture<>();
1548 <        g = new CompletableFuture<>();
1549 <        h = f.runAfterBoth(g, r = new Noop());
1550 <        g.complete(1);
1551 <        checkIncomplete(h);
1552 <        f.complete(3);
942 <        checkCompletedNormally(h, null);
943 <        assertTrue(r.ran);
1533 >    public void testRunAfterBoth_normalCompletion() {
1534 >        for (ExecutionMode m : ExecutionMode.values())
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 Noop r = new Noop(m);
1543 >
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  
945        f = new CompletableFuture<>();
946        g = new CompletableFuture<>();
947        g.complete(1);
948        f.complete(3);
949        h = f.runAfterBoth(g, r = new Noop());
1554          checkCompletedNormally(h, null);
1555 <        assertTrue(r.ran);
1556 <    }
1555 >        assertEquals(1, r.invocationCount);
1556 >        checkCompletedNormally(f, v1);
1557 >        checkCompletedNormally(g, v2);
1558 >    }}
1559  
1560      /**
1561       * runAfterBoth result completes exceptionally after exceptional
1562       * completion of either source
1563       */
1564 <    public void testRunAfterBoth2() {
1565 <        CompletableFuture<Integer> f, g;
1566 <        CompletableFuture<Void> h;
1567 <        Noop r;
1568 <
1569 <        f = new CompletableFuture<>();
1570 <        g = new CompletableFuture<>();
1571 <        h = f.runAfterBoth(g, r = new Noop());
1572 <        f.completeExceptionally(new CFException());
1573 <        checkIncomplete(h);
1574 <        g.complete(1);
1575 <        checkCompletedWithWrappedCFException(h);
1576 <        assertFalse(r.ran);
1577 <
1578 <        f = new CompletableFuture<>();
1579 <        g = new CompletableFuture<>();
1580 <        h = f.runAfterBoth(g, r = new Noop());
1581 <        g.completeExceptionally(new CFException());
1582 <        checkIncomplete(h);
977 <        f.complete(3);
978 <        checkCompletedWithWrappedCFException(h);
979 <        assertFalse(r.ran);
980 <
981 <        f = new CompletableFuture<>();
982 <        g = new CompletableFuture<>();
983 <        g.completeExceptionally(new CFException());
984 <        f.complete(3);
985 <        h = f.runAfterBoth(g, r = new Noop());
986 <        checkCompletedWithWrappedCFException(h);
987 <        assertFalse(r.ran);
1564 >    public void testRunAfterBoth_exceptionalCompletion() {
1565 >        for (ExecutionMode m : ExecutionMode.values())
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 CFException ex = new CFException();
1573 >        final Noop r = new Noop(m);
1574 >
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  
1584 <        f = new CompletableFuture<>();
1585 <        g = new CompletableFuture<>();
1586 <        f.completeExceptionally(new CFException());
1587 <        g.complete(1);
1588 <        h = f.runAfterBoth(g, r = new Noop());
994 <        checkCompletedWithWrappedCFException(h);
995 <        assertFalse(r.ran);
996 <    }
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       * runAfterBoth result completes exceptionally if action does
1592       */
1593 <    public void testRunAfterBoth3() {
1594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1595 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1596 <        FailingNoop r = new FailingNoop();
1597 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1598 <        f.complete(one);
1599 <        checkIncomplete(g);
1600 <        f2.complete(two);
1601 <        checkCompletedWithWrappedCFException(g);
1602 <    }
1603 <
1604 <    /**
1605 <     * runAfterBoth result completes exceptionally if either source cancelled
1606 <     */
1607 <    public void testRunAfterBoth4() {
1608 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1609 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1610 <        Noop r = new Noop();
1611 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1020 <        assertTrue(f.cancel(true));
1021 <        f2.complete(two);
1022 <        checkCompletedWithWrappedCancellationException(g);
1023 <        f = new CompletableFuture<>();
1024 <        f2 = new CompletableFuture<>();
1025 <        r = new Noop();
1026 <        g = f.runAfterBoth(f2, r);
1027 <        f.complete(one);
1028 <        assertTrue(f2.cancel(true));
1029 <        checkCompletedWithWrappedCancellationException(g);
1030 <    }
1031 <
1032 <    /**
1033 <     * applyToEither result completes normally after normal completion
1034 <     * of either source
1035 <     */
1036 <    public void testApplyToEither() {
1037 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1038 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1039 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1040 <        f.complete(one);
1041 <        checkCompletedNormally(g, two);
1042 <        f2.complete(one);
1043 <        checkCompletedNormally(g, two);
1044 <
1045 <        f = new CompletableFuture<>();
1046 <        f.complete(one);
1047 <        f2 = new CompletableFuture<>();
1048 <        g = f.applyToEither(f2, inc);
1049 <        checkCompletedNormally(g, two);
1050 <    }
1051 <
1052 <    /**
1053 <     * applyToEither result completes exceptionally after exceptional
1054 <     * completion of either source
1055 <     */
1056 <    public void testApplyToEither2() {
1057 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1058 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1059 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1060 <        f.completeExceptionally(new CFException());
1061 <        f2.complete(one);
1062 <        checkCompletedWithWrappedCFException(g);
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 >    {
1599 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1600 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1601 >        final FailingRunnable r = new FailingRunnable(m);
1602 >
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 <        f = new CompletableFuture<>();
1614 <        f2 = new CompletableFuture<>();
1615 <        f2.completeExceptionally(new CFException());
1616 <        g = f.applyToEither(f2, inc);
1617 <        checkCompletedWithWrappedCFException(g);
1069 <    }
1613 >        checkCompletedWithWrappedCFException(h1);
1614 >        checkCompletedWithWrappedCFException(h2);
1615 >        checkCompletedNormally(f, v1);
1616 >        checkCompletedNormally(g, v2);
1617 >    }}
1618  
1619      /**
1620 <     * applyToEither result completes exceptionally if action does
1620 >     * runAfterBoth result completes exceptionally if either source cancelled
1621       */
1622 <    public void testApplyToEither3() {
1623 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1624 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1625 <        FailingFunction r = new FailingFunction();
1626 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1627 <        f2.complete(two);
1628 <        checkCompletedWithWrappedCFException(g);
1629 <    }
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 >    {
1629 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1630 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1631 >        final Noop r = new Noop(m);
1632 >
1633 >
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 >        }
1642  
1643 <    /**
1644 <     * applyToEither result completes exceptionally if either source cancelled
1645 <     */
1646 <    public void testApplyToEither4() {
1647 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1088 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1089 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1090 <        assertTrue(f.cancel(true));
1091 <        checkCompletedWithWrappedCancellationException(g);
1092 <        f = new CompletableFuture<>();
1093 <        f2 = new CompletableFuture<>();
1094 <        assertTrue(f2.cancel(true));
1095 <        checkCompletedWithWrappedCancellationException(g);
1096 <    }
1643 >        checkCompletedWithWrappedCancellationException(h);
1644 >        checkCancelled(!fFirst ? f : g);
1645 >        assertEquals(0, r.invocationCount);
1646 >        checkCompletedNormally(fFirst ? f : g, v1);
1647 >    }}
1648  
1649      /**
1650 <     * acceptEither result completes normally after normal completion
1650 >     * applyToEither result completes normally after normal completion
1651       * of either source
1652       */
1653 <    public void testAcceptEither() {
1654 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1655 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1656 <        IncAction r = new IncAction();
1657 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1658 <        f.complete(one);
1659 <        checkCompletedNormally(g, null);
1660 <        f2.complete(one);
1661 <        checkCompletedNormally(g, null);
1662 <        assertEquals(r.value, 2);
1663 <
1664 <        r = new IncAction();
1665 <        f = new CompletableFuture<>();
1666 <        f.complete(one);
1667 <        f2 = new CompletableFuture<>();
1668 <        g = f.acceptEither(f2, r);
1669 <        checkCompletedNormally(g, null);
1670 <        assertEquals(r.value, 2);
1671 <    }
1672 <
1673 <    /**
1123 <     * acceptEither result completes exceptionally after exceptional
1124 <     * completion of either source
1125 <     */
1126 <    public void testAcceptEither2() {
1127 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1128 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1129 <        IncAction r = new IncAction();
1130 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1131 <        f.completeExceptionally(new CFException());
1132 <        f2.complete(one);
1133 <        checkCompletedWithWrappedCFException(g);
1134 <
1135 <        r = new IncAction();
1136 <        f = new CompletableFuture<>();
1137 <        f2 = new CompletableFuture<>();
1138 <        f2.completeExceptionally(new CFException());
1139 <        g = f.acceptEither(f2, r);
1140 <        checkCompletedWithWrappedCFException(g);
1141 <    }
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 >    {
1660 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1661 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1662 >        final IncFunction r = new IncFunction(m);
1663 >
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  
1675 <    /**
1676 <     * acceptEither result completes exceptionally if action does
1677 <     */
1678 <    public void testAcceptEither3() {
1679 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1680 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1681 <        FailingConsumer r = new FailingConsumer();
1682 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1683 <        f2.complete(two);
1684 <        checkCompletedWithWrappedCFException(g);
1685 <    }
1675 >        checkCompletedNormally(f, v1);
1676 >        checkCompletedNormally(g, v2);
1677 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1678 >    }}
1679 >
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 >    {
1686 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1687 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1688 >        final IncFunction r = new IncFunction(m);
1689 >
1690 >        if (fFirst) {
1691 >            f.complete(v1);
1692 >            g.complete(v2);
1693 >        } else {
1694 >            g.complete(v2);
1695 >            f.complete(v1);
1696 >        }
1697  
1698 <    /**
1156 <     * acceptEither result completes exceptionally if either source cancelled
1157 <     */
1158 <    public void testAcceptEither4() {
1159 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1160 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1161 <        IncAction r = new IncAction();
1162 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1163 <        assertTrue(f.cancel(true));
1164 <        checkCompletedWithWrappedCancellationException(g);
1165 <        f = new CompletableFuture<>();
1166 <        f2 = new CompletableFuture<>();
1167 <        assertTrue(f2.cancel(true));
1168 <        checkCompletedWithWrappedCancellationException(g);
1169 <    }
1698 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1699  
1700 <    /**
1701 <     * runAfterEither result completes normally after normal completion
1173 <     * of either source
1174 <     */
1175 <    public void testRunAfterEither() {
1176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1177 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1178 <        Noop r = new Noop();
1179 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1180 <        f.complete(one);
1181 <        checkCompletedNormally(g, null);
1182 <        f2.complete(one);
1183 <        checkCompletedNormally(g, null);
1184 <        assertTrue(r.ran);
1700 >        checkCompletedNormally(f, v1);
1701 >        checkCompletedNormally(g, v2);
1702  
1703 <        r = new Noop();
1704 <        f = new CompletableFuture<>();
1705 <        f.complete(one);
1706 <        f2 = new CompletableFuture<>();
1707 <        g = f.runAfterEither(f2, r);
1191 <        checkCompletedNormally(g, null);
1192 <        assertTrue(r.ran);
1193 <    }
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 <     * runAfterEither result completes exceptionally after exceptional
1710 >     * applyToEither result completes exceptionally after exceptional
1711       * completion of either source
1712       */
1713 <    public void testRunAfterEither2() {
1714 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1715 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1716 <        Noop r = new Noop();
1717 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1718 <        f.completeExceptionally(new CFException());
1719 <        f2.complete(one);
1720 <        checkCompletedWithWrappedCFException(g);
1721 <
1722 <        r = new Noop();
1723 <        f = new CompletableFuture<>();
1724 <        f2 = new CompletableFuture<>();
1725 <        f2.completeExceptionally(new CFException());
1726 <        g = f.runAfterEither(f2, r);
1727 <        checkCompletedWithWrappedCFException(g);
1728 <    }
1729 <
1730 <    /**
1217 <     * runAfterEither result completes exceptionally if action does
1218 <     */
1219 <    public void testRunAfterEither3() {
1220 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1221 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1222 <        FailingNoop r = new FailingNoop();
1223 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1224 <        f2.complete(two);
1225 <        checkCompletedWithWrappedCFException(g);
1226 <    }
1227 <
1228 <    /**
1229 <     * runAfterEither result completes exceptionally if either source cancelled
1230 <     */
1231 <    public void testRunAfterEither4() {
1232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1233 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1234 <        Noop r = new Noop();
1235 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1236 <        assertTrue(f.cancel(true));
1237 <        checkCompletedWithWrappedCancellationException(g);
1238 <        f = new CompletableFuture<>();
1239 <        f2 = new CompletableFuture<>();
1240 <        assertTrue(f2.cancel(true));
1241 <        checkCompletedWithWrappedCancellationException(g);
1242 <    }
1243 <
1244 <    /**
1245 <     * thenCompose result completes normally after normal completion of source
1246 <     */
1247 <    public void testThenCompose() {
1248 <        CompletableFuture<Integer> f, g;
1249 <        CompletableFutureInc r;
1250 <
1251 <        f = new CompletableFuture<>();
1252 <        g = f.thenCompose(r = new CompletableFutureInc());
1253 <        f.complete(one);
1254 <        checkCompletedNormally(g, two);
1255 <        assertTrue(r.ran);
1256 <
1257 <        f = new CompletableFuture<>();
1258 <        f.complete(one);
1259 <        g = f.thenCompose(r = new CompletableFutureInc());
1260 <        checkCompletedNormally(g, two);
1261 <        assertTrue(r.ran);
1262 <    }
1263 <
1264 <    /**
1265 <     * thenCompose result completes exceptionally after exceptional
1266 <     * completion of source
1267 <     */
1268 <    public void testThenCompose2() {
1269 <        CompletableFuture<Integer> f, g;
1270 <        CompletableFutureInc r;
1271 <
1272 <        f = new CompletableFuture<>();
1273 <        g = f.thenCompose(r = new CompletableFutureInc());
1274 <        f.completeExceptionally(new CFException());
1275 <        checkCompletedWithWrappedCFException(g);
1276 <
1277 <        f = new CompletableFuture<>();
1278 <        f.completeExceptionally(new CFException());
1279 <        g = f.thenCompose(r = new CompletableFutureInc());
1280 <        checkCompletedWithWrappedCFException(g);
1281 <    }
1282 <
1283 <    /**
1284 <     * thenCompose result completes exceptionally if action does
1285 <     */
1286 <    public void testThenCompose3() {
1287 <        CompletableFuture<Integer> f, g;
1288 <        FailingCompletableFutureFunction r;
1289 <
1290 <        f = new CompletableFuture<>();
1291 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1292 <        f.complete(one);
1293 <        checkCompletedWithWrappedCFException(g);
1294 <
1295 <        f = new CompletableFuture<>();
1296 <        f.complete(one);
1297 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1298 <        checkCompletedWithWrappedCFException(g);
1299 <    }
1300 <
1301 <    /**
1302 <     * thenCompose result completes exceptionally if source cancelled
1303 <     */
1304 <    public void testThenCompose4() {
1305 <        CompletableFuture<Integer> f, g;
1306 <        CompletableFutureInc r;
1307 <
1308 <        f = new CompletableFuture<>();
1309 <        g = f.thenCompose(r = new CompletableFutureInc());
1310 <        assertTrue(f.cancel(true));
1311 <        checkCompletedWithWrappedCancellationException(g);
1312 <
1313 <        f = new CompletableFuture<>();
1314 <        assertTrue(f.cancel(true));
1315 <        g = f.thenCompose(r = new CompletableFutureInc());
1316 <        checkCompletedWithWrappedCancellationException(g);
1317 <    }
1713 >    public void testApplyToEither_exceptionalCompletion1() {
1714 >        for (ExecutionMode m : ExecutionMode.values())
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<>();
1721 >        final CFException ex = new CFException();
1722 >        final IncFunction r = new IncFunction(m);
1723 >
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 >        }
1731  
1732 <    // asyncs
1732 >        checkCompletedWithWrappedCFException(h, ex);
1733 >        (!fFirst ? f : g).complete(v1);
1734  
1735 <    /**
1736 <     * thenRunAsync result completes normally after normal completion of source
1737 <     */
1738 <    public void testThenRunAsync() {
1739 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1740 <        Noop r = new Noop();
1741 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1742 <        f.complete(null);
1743 <        checkCompletedNormally(g, null);
1744 <
1745 <        // reordered version
1746 <        f = new CompletableFuture<>();
1747 <        f.complete(null);
1748 <        r = new Noop();
1749 <        g = f.thenRunAsync(r);
1750 <        checkCompletedNormally(g, null);
1751 <    }
1735 >        assertEquals(0, r.invocationCount);
1736 >        checkCompletedNormally(!fFirst ? f : g, v1);
1737 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1738 >        checkCompletedWithWrappedCFException(h, ex);
1739 >    }}
1740 >
1741 >    public void testApplyToEither_exceptionalCompletion2() {
1742 >        for (ExecutionMode m : ExecutionMode.values())
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 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 <    /**
1340 <     * thenRunAsync result completes exceptionally after exceptional
1341 <     * completion of source
1342 <     */
1343 <    public void testThenRunAsync2() {
1344 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1345 <        Noop r = new Noop();
1346 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1347 <        f.completeExceptionally(new CFException());
1764 >        // unspecified behavior
1765          try {
1766 <            g.join();
1767 <            shouldThrow();
1768 <        } catch (CompletionException success) {}
1769 <        checkCompletedWithWrappedCFException(g);
1770 <    }
1771 <
1355 <    /**
1356 <     * thenRunAsync result completes exceptionally if action does
1357 <     */
1358 <    public void testThenRunAsync3() {
1359 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1360 <        FailingNoop r = new FailingNoop();
1361 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1362 <        f.complete(null);
1363 <        checkCompletedWithWrappedCFException(g);
1364 <    }
1365 <
1366 <    /**
1367 <     * thenRunAsync result completes exceptionally if source cancelled
1368 <     */
1369 <    public void testThenRunAsync4() {
1370 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1371 <        Noop r = new Noop();
1372 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1373 <        assertTrue(f.cancel(true));
1374 <        checkCompletedWithWrappedCancellationException(g);
1375 <    }
1376 <
1377 <    /**
1378 <     * thenApplyAsync result completes normally after normal completion of source
1379 <     */
1380 <    public void testThenApplyAsync() {
1381 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1382 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1383 <        f.complete(one);
1384 <        checkCompletedNormally(g, two);
1385 <    }
1386 <
1387 <    /**
1388 <     * thenApplyAsync result completes exceptionally after exceptional
1389 <     * completion of source
1390 <     */
1391 <    public void testThenApplyAsync2() {
1392 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1393 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1394 <        f.completeExceptionally(new CFException());
1395 <        checkCompletedWithWrappedCFException(g);
1396 <    }
1397 <
1398 <    /**
1399 <     * thenApplyAsync result completes exceptionally if action does
1400 <     */
1401 <    public void testThenApplyAsync3() {
1402 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1403 <        FailingFunction r = new FailingFunction();
1404 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1405 <        f.complete(null);
1406 <        checkCompletedWithWrappedCFException(g);
1407 <    }
1408 <
1409 <    /**
1410 <     * thenApplyAsync result completes exceptionally if source cancelled
1411 <     */
1412 <    public void testThenApplyAsync4() {
1413 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1414 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1415 <        assertTrue(f.cancel(true));
1416 <        checkCompletedWithWrappedCancellationException(g);
1417 <    }
1418 <
1419 <    /**
1420 <     * thenAcceptAsync result completes normally after normal
1421 <     * completion of source
1422 <     */
1423 <    public void testThenAcceptAsync() {
1424 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1425 <        IncAction r = new IncAction();
1426 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1427 <        f.complete(one);
1428 <        checkCompletedNormally(g, null);
1429 <        assertEquals(r.value, 2);
1430 <    }
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 <    /**
1774 <     * thenAcceptAsync result completes exceptionally after exceptional
1775 <     * completion of source
1776 <     */
1777 <    public void testThenAcceptAsync2() {
1778 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1779 <        IncAction r = new IncAction();
1439 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1440 <        f.completeExceptionally(new CFException());
1441 <        checkCompletedWithWrappedCFException(g);
1442 <    }
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  
1781 <    /**
1782 <     * thenAcceptAsync result completes exceptionally if action does
1783 <     */
1447 <    public void testThenAcceptAsync3() {
1448 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1449 <        FailingConsumer r = new FailingConsumer();
1450 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1451 <        f.complete(null);
1452 <        checkCompletedWithWrappedCFException(g);
1453 <    }
1781 >        checkCompletedWithWrappedCFException(g, ex);
1782 >        checkCompletedNormally(f, v1);
1783 >    }}
1784  
1785      /**
1786 <     * thenAcceptAsync result completes exceptionally if source cancelled
1786 >     * applyToEither result completes exceptionally if action does
1787       */
1788 <    public void testThenAcceptAsync4() {
1789 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1790 <        IncAction r = new IncAction();
1791 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1792 <        assertTrue(f.cancel(true));
1793 <        checkCompletedWithWrappedCancellationException(g);
1794 <    }
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 >    {
1793 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1794 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1795 >        final FailingFunction r = new FailingFunction(m);
1796 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1797 >
1798 >        f.complete(v1);
1799 >        checkCompletedWithWrappedCFException(h);
1800 >        g.complete(v2);
1801 >        checkCompletedNormally(f, v1);
1802 >        checkCompletedNormally(g, v2);
1803 >    }}
1804 >
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 >    {
1810 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1811 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1812 >        final FailingFunction r = new FailingFunction(m);
1813 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1814 >
1815 >        g.complete(v2);
1816 >        checkCompletedWithWrappedCFException(h);
1817 >        f.complete(v1);
1818 >        checkCompletedNormally(f, v1);
1819 >        checkCompletedNormally(g, v2);
1820 >    }}
1821  
1822      /**
1823 <     * thenCombineAsync result completes normally after normal
1468 <     * completion of sources
1823 >     * applyToEither result completes exceptionally if either source cancelled
1824       */
1825 <    public void testThenCombineAsync() {
1826 <        CompletableFuture<Integer> f, g, h;
1827 <
1828 <        f = new CompletableFuture<>();
1829 <        g = new CompletableFuture<>();
1830 <        h = f.thenCombineAsync(g, subtract);
1831 <        f.complete(3);
1832 <        checkIncomplete(h);
1833 <        g.complete(1);
1834 <        checkCompletedNormally(h, 2);
1835 <
1836 <        f = new CompletableFuture<>();
1837 <        g = new CompletableFuture<>();
1838 <        h = f.thenCombineAsync(g, subtract);
1839 <        g.complete(1);
1840 <        checkIncomplete(h);
1841 <        f.complete(3);
1842 <        checkCompletedNormally(h, 2);
1825 >    public void testApplyToEither_sourceCancelled1() {
1826 >        for (ExecutionMode m : ExecutionMode.values())
1827 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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 IncFunction r = new IncFunction(m);
1835 >
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 >        }
1843  
1844 <        f = new CompletableFuture<>();
1845 <        g = new CompletableFuture<>();
1491 <        g.complete(1);
1492 <        f.complete(3);
1493 <        h = f.thenCombineAsync(g, subtract);
1494 <        checkCompletedNormally(h, 2);
1495 <    }
1844 >        checkCompletedWithWrappedCancellationException(h);
1845 >        (!fFirst ? f : g).complete(v1);
1846  
1847 <    /**
1848 <     * thenCombineAsync result completes exceptionally after exceptional
1849 <     * completion of either source
1850 <     */
1851 <    public void testThenCombineAsync2() {
1852 <        CompletableFuture<Integer> f, g, h;
1847 >        assertEquals(0, r.invocationCount);
1848 >        checkCompletedNormally(!fFirst ? f : g, v1);
1849 >        checkCancelled(fFirst ? f : g);
1850 >        checkCompletedWithWrappedCancellationException(h);
1851 >    }}
1852 >
1853 >    public void testApplyToEither_sourceCancelled2() {
1854 >        for (ExecutionMode m : ExecutionMode.values())
1855 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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 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 >        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 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1877  
1878 <        f = new CompletableFuture<>();
1879 <        g = new CompletableFuture<>();
1880 <        h = f.thenCombineAsync(g, subtract);
1881 <        f.completeExceptionally(new CFException());
1882 <        checkIncomplete(h);
1883 <        g.complete(1);
1884 <        checkCompletedWithWrappedCFException(h);
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 <        f = new CompletableFuture<>();
1888 <        g = new CompletableFuture<>();
1889 <        h = f.thenCombineAsync(g, subtract);
1890 <        g.completeExceptionally(new CFException());
1891 <        checkIncomplete(h);
1892 <        f.complete(3);
1893 <        checkCompletedWithWrappedCFException(h);
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  
1895 <        f = new CompletableFuture<>();
1896 <        g = new CompletableFuture<>();
1897 <        g.completeExceptionally(new CFException());
1523 <        f.complete(3);
1524 <        h = f.thenCombineAsync(g, subtract);
1525 <        checkCompletedWithWrappedCFException(h);
1526 <    }
1895 >        checkCancelled(g);
1896 >        checkCompletedNormally(f, v1);
1897 >    }}
1898  
1899      /**
1900 <     * thenCombineAsync result completes exceptionally if action does
1901 <     */
1531 <    public void testThenCombineAsync3() {
1532 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1533 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1534 <        FailingBiFunction r = new FailingBiFunction();
1535 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1536 <        f.complete(one);
1537 <        checkIncomplete(g);
1538 <        assertFalse(r.ran);
1539 <        f2.complete(two);
1540 <        checkCompletedWithWrappedCFException(g);
1541 <        assertTrue(r.ran);
1542 <    }
1543 <
1544 <    /**
1545 <     * thenCombineAsync result completes exceptionally if either source cancelled
1900 >     * acceptEither result completes normally after normal completion
1901 >     * of either source
1902       */
1903 <    public void testThenCombineAsync4() {
1904 <        CompletableFuture<Integer> f, g, h;
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 >    {
1908 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1909 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1910 >        final IncAction r = new IncAction();
1911 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1912  
1913 <        f = new CompletableFuture<>();
1914 <        g = new CompletableFuture<>();
1915 <        h = f.thenCombineAsync(g, subtract);
1916 <        assertTrue(f.cancel(true));
1554 <        checkIncomplete(h);
1555 <        g.complete(1);
1556 <        checkCompletedWithWrappedCancellationException(h);
1557 <
1558 <        f = new CompletableFuture<>();
1559 <        g = new CompletableFuture<>();
1560 <        h = f.thenCombineAsync(g, subtract);
1561 <        assertTrue(g.cancel(true));
1562 <        checkIncomplete(h);
1563 <        f.complete(3);
1564 <        checkCompletedWithWrappedCancellationException(h);
1565 <
1566 <        f = new CompletableFuture<>();
1567 <        g = new CompletableFuture<>();
1568 <        g.complete(3);
1569 <        assertTrue(f.cancel(true));
1570 <        h = f.thenCombineAsync(g, subtract);
1571 <        checkCompletedWithWrappedCancellationException(h);
1913 >        f.complete(v1);
1914 >        checkCompletedNormally(h, null);
1915 >        assertEquals(inc(v1), r.value);
1916 >        g.complete(v2);
1917  
1918 <        f = new CompletableFuture<>();
1919 <        g = new CompletableFuture<>();
1920 <        f.complete(3);
1921 <        assertTrue(g.cancel(true));
1577 <        h = f.thenCombineAsync(g, subtract);
1578 <        checkCompletedWithWrappedCancellationException(h);
1579 <    }
1918 >        checkCompletedNormally(f, v1);
1919 >        checkCompletedNormally(g, v2);
1920 >        checkCompletedNormally(h, null);
1921 >    }}
1922  
1923 <    /**
1924 <     * thenAcceptBothAsync result completes normally after normal
1925 <     * completion of sources
1926 <     */
1927 <    public void testThenAcceptBothAsync() {
1928 <        CompletableFuture<Integer> f, g;
1929 <        CompletableFuture<Void> h;
1930 <        SubtractAction r;
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 >    {
1928 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1929 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1930 >        final IncAction r = new IncAction();
1931 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1932  
1933 <        f = new CompletableFuture<>();
1591 <        g = new CompletableFuture<>();
1592 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1593 <        f.complete(3);
1594 <        checkIncomplete(h);
1595 <        g.complete(1);
1933 >        g.complete(v2);
1934          checkCompletedNormally(h, null);
1935 <        assertEquals(r.value, 2);
1935 >        assertEquals(inc(v2), r.value);
1936 >        f.complete(v1);
1937  
1938 <        f = new CompletableFuture<>();
1939 <        g = new CompletableFuture<>();
1601 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1602 <        g.complete(1);
1603 <        checkIncomplete(h);
1604 <        f.complete(3);
1938 >        checkCompletedNormally(f, v1);
1939 >        checkCompletedNormally(g, v2);
1940          checkCompletedNormally(h, null);
1941 <        assertEquals(r.value, 2);
1941 >    }}
1942 >
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 >    {
1948 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1949 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1950 >        final IncAction r = new IncAction();
1951 >
1952 >        f.complete(v1);
1953 >        g.complete(v2);
1954 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1955  
1608        f = new CompletableFuture<>();
1609        g = new CompletableFuture<>();
1610        g.complete(1);
1611        f.complete(3);
1612        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1956          checkCompletedNormally(h, null);
1957 <        assertEquals(r.value, 2);
1958 <    }
1957 >        checkCompletedNormally(f, v1);
1958 >        checkCompletedNormally(g, v2);
1959 >
1960 >        // unspecified behavior
1961 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
1962 >                   Objects.equals(r.value, inc(v2)));
1963 >    }}
1964  
1965      /**
1966 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1967 <     * completion of source
1966 >     * acceptEither result completes exceptionally after exceptional
1967 >     * completion of either source
1968       */
1969 <    public void testThenAcceptBothAsync2() {
1970 <        CompletableFuture<Integer> f, g;
1971 <        CompletableFuture<Void> h;
1972 <        SubtractAction r;
1969 >    public void testAcceptEither_exceptionalCompletion1() {
1970 >        for (ExecutionMode m : ExecutionMode.values())
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 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 >        checkCompletedWithWrappedCFException(h, ex);
1981 >        g.complete(v1);
1982 >
1983 >        assertEquals(0, r.invocationCount);
1984 >        checkCompletedNormally(g, v1);
1985 >        checkCompletedWithWrappedCFException(f, ex);
1986 >        checkCompletedWithWrappedCFException(h, ex);
1987 >    }}
1988 >
1989 >    public void testAcceptEither_exceptionalCompletion2() {
1990 >        for (ExecutionMode m : ExecutionMode.values())
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 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 >        checkCompletedWithWrappedCFException(h, ex);
2001 >        f.complete(v1);
2002 >
2003 >        assertEquals(0, r.invocationCount);
2004 >        checkCompletedNormally(f, v1);
2005 >        checkCompletedWithWrappedCFException(g, ex);
2006 >        checkCompletedWithWrappedCFException(h, ex);
2007 >    }}
2008 >
2009 >    public void testAcceptEither_exceptionalCompletion3() {
2010 >        for (ExecutionMode m : ExecutionMode.values())
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 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.acceptEither(f, g, r);
2021  
2022 <        f = new CompletableFuture<>();
2023 <        g = new CompletableFuture<>();
2024 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2025 <        f.completeExceptionally(new CFException());
2026 <        checkIncomplete(h);
2027 <        g.complete(1);
2028 <        checkCompletedWithWrappedCFException(h);
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  
2033 <        f = new CompletableFuture<>();
2034 <        g = new CompletableFuture<>();
2035 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2036 <        g.completeExceptionally(new CFException());
2037 <        checkIncomplete(h);
2038 <        f.complete(3);
2039 <        checkCompletedWithWrappedCFException(h);
2033 >        checkCompletedWithWrappedCFException(g, ex);
2034 >        checkCompletedNormally(f, v1);
2035 >    }}
2036 >
2037 >    public void testAcceptEither_exceptionalCompletion4() {
2038 >        for (ExecutionMode m : ExecutionMode.values())
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 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.acceptEither(f, g, r);
2049  
2050 <        f = new CompletableFuture<>();
2051 <        g = new CompletableFuture<>();
2052 <        f.complete(3);
2053 <        g.completeExceptionally(new CFException());
2054 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2055 <        checkCompletedWithWrappedCFException(h);
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  
2061 <        f = new CompletableFuture<>();
2062 <        g = new CompletableFuture<>();
2063 <        f.completeExceptionally(new CFException());
1652 <        g.complete(3);
1653 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1654 <        checkCompletedWithWrappedCFException(h);
1655 <    }
2061 >        checkCompletedWithWrappedCFException(f, ex);
2062 >        checkCompletedNormally(g, v1);
2063 >    }}
2064  
2065      /**
2066 <     * thenAcceptBothAsync result completes exceptionally if action does
2066 >     * acceptEither result completes exceptionally if action does
2067       */
2068 <    public void testThenAcceptBothAsync3() {
2069 <        CompletableFuture<Integer> f, g;
2070 <        CompletableFuture<Void> h;
2071 <        FailingBiConsumer r;
2072 <
2073 <        f = new CompletableFuture<>();
2074 <        g = new CompletableFuture<>();
2075 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
2076 <        f.complete(3);
2077 <        checkIncomplete(h);
2078 <        g.complete(1);
2079 <        checkCompletedWithWrappedCFException(h);
2080 <
2081 <        f = new CompletableFuture<>();
2082 <        g = new CompletableFuture<>();
2083 <        f.complete(3);
2084 <        g.complete(1);
2085 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
2086 <        checkCompletedWithWrappedCFException(h);
2087 <    }
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 >    {
2073 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2074 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2075 >        final FailingConsumer r = new FailingConsumer(m);
2076 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2077 >
2078 >        f.complete(v1);
2079 >        checkCompletedWithWrappedCFException(h);
2080 >        g.complete(v2);
2081 >        checkCompletedNormally(f, v1);
2082 >        checkCompletedNormally(g, v2);
2083 >    }}
2084 >
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 >    {
2090 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2091 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2092 >        final FailingConsumer r = new FailingConsumer(m);
2093 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2094 >
2095 >        g.complete(v2);
2096 >        checkCompletedWithWrappedCFException(h);
2097 >        f.complete(v1);
2098 >        checkCompletedNormally(f, v1);
2099 >        checkCompletedNormally(g, v2);
2100 >    }}
2101  
2102      /**
2103 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2103 >     * acceptEither result completes exceptionally if either source cancelled
2104       */
2105 <    public void testThenAcceptBothAsync4() {
2106 <        CompletableFuture<Integer> f, g;
2107 <        CompletableFuture<Void> h;
2108 <        SubtractAction r;
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 >    {
2110 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2111 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2112 >        final IncAction r = new IncAction();
2113 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2114  
2115 <        f = new CompletableFuture<>();
1690 <        g = new CompletableFuture<>();
1691 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1692 <        assertTrue(f.cancel(true));
1693 <        checkIncomplete(h);
1694 <        g.complete(1);
2115 >        assertTrue(f.cancel(mayInterruptIfRunning));
2116          checkCompletedWithWrappedCancellationException(h);
2117 +        g.complete(v1);
2118  
2119 <        f = new CompletableFuture<>();
2120 <        g = new CompletableFuture<>();
2121 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1700 <        assertTrue(g.cancel(true));
1701 <        checkIncomplete(h);
1702 <        f.complete(3);
1703 <        checkCompletedWithWrappedCancellationException(h);
1704 <
1705 <        f = new CompletableFuture<>();
1706 <        g = new CompletableFuture<>();
1707 <        f.complete(3);
1708 <        assertTrue(g.cancel(true));
1709 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1710 <        checkCompletedWithWrappedCancellationException(h);
1711 <
1712 <        f = new CompletableFuture<>();
1713 <        g = new CompletableFuture<>();
1714 <        assertTrue(f.cancel(true));
1715 <        g.complete(3);
1716 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
2119 >        checkCancelled(f);
2120 >        assertEquals(0, r.invocationCount);
2121 >        checkCompletedNormally(g, v1);
2122          checkCompletedWithWrappedCancellationException(h);
2123 <    }
1719 <
1720 <    /**
1721 <     * runAfterBothAsync result completes normally after normal
1722 <     * completion of sources
1723 <     */
1724 <    public void testRunAfterBothAsync() {
1725 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1726 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1727 <        Noop r = new Noop();
1728 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1729 <        f.complete(one);
1730 <        checkIncomplete(g);
1731 <        f2.complete(two);
1732 <        checkCompletedNormally(g, null);
1733 <        assertTrue(r.ran);
1734 <    }
1735 <
1736 <    /**
1737 <     * runAfterBothAsync result completes exceptionally after exceptional
1738 <     * completion of source
1739 <     */
1740 <    public void testRunAfterBothAsync2() {
1741 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1742 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1743 <        Noop r = new Noop();
1744 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1745 <        f.completeExceptionally(new CFException());
1746 <        f2.complete(two);
1747 <        checkCompletedWithWrappedCFException(g);
1748 <
1749 <        r = new Noop();
1750 <        f = new CompletableFuture<>();
1751 <        f2 = new CompletableFuture<>();
1752 <        g = f.runAfterBothAsync(f2, r);
1753 <        f.complete(one);
1754 <        f2.completeExceptionally(new CFException());
1755 <        checkCompletedWithWrappedCFException(g);
1756 <    }
1757 <
1758 <    /**
1759 <     * runAfterBothAsync result completes exceptionally if action does
1760 <     */
1761 <    public void testRunAfterBothAsync3() {
1762 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1763 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1764 <        FailingNoop r = new FailingNoop();
1765 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1766 <        f.complete(one);
1767 <        checkIncomplete(g);
1768 <        f2.complete(two);
1769 <        checkCompletedWithWrappedCFException(g);
1770 <    }
1771 <
1772 <    /**
1773 <     * runAfterBothAsync result completes exceptionally if either source cancelled
1774 <     */
1775 <    public void testRunAfterBothAsync4() {
1776 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1777 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1778 <        Noop r = new Noop();
1779 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1780 <        assertTrue(f.cancel(true));
1781 <        f2.complete(two);
1782 <        checkCompletedWithWrappedCancellationException(g);
1783 <
1784 <        r = new Noop();
1785 <        f = new CompletableFuture<>();
1786 <        f2 = new CompletableFuture<>();
1787 <        g = f.runAfterBothAsync(f2, r);
1788 <        f.complete(one);
1789 <        assertTrue(f2.cancel(true));
1790 <        checkCompletedWithWrappedCancellationException(g);
1791 <    }
1792 <
1793 <    /**
1794 <     * applyToEitherAsync result completes normally after normal
1795 <     * completion of sources
1796 <     */
1797 <    public void testApplyToEitherAsync() {
1798 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1799 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1800 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1801 <        f.complete(one);
1802 <        checkCompletedNormally(g, two);
1803 <
1804 <        f = new CompletableFuture<>();
1805 <        f.complete(one);
1806 <        f2 = new CompletableFuture<>();
1807 <        g = f.applyToEitherAsync(f2, inc);
1808 <        checkCompletedNormally(g, two);
1809 <    }
1810 <
1811 <    /**
1812 <     * applyToEitherAsync result completes exceptionally after exceptional
1813 <     * completion of source
1814 <     */
1815 <    public void testApplyToEitherAsync2() {
1816 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1817 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1818 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1819 <        f.completeExceptionally(new CFException());
1820 <        checkCompletedWithWrappedCFException(g);
1821 <
1822 <        f = new CompletableFuture<>();
1823 <        f2 = new CompletableFuture<>();
1824 <        f2.completeExceptionally(new CFException());
1825 <        g = f.applyToEitherAsync(f2, inc);
1826 <        f.complete(one);
1827 <        checkCompletedWithWrappedCFException(g);
1828 <    }
1829 <
1830 <    /**
1831 <     * applyToEitherAsync result completes exceptionally if action does
1832 <     */
1833 <    public void testApplyToEitherAsync3() {
1834 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1835 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1836 <        FailingFunction r = new FailingFunction();
1837 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1838 <        f.complete(one);
1839 <        checkCompletedWithWrappedCFException(g);
1840 <    }
1841 <
1842 <    /**
1843 <     * applyToEitherAsync result completes exceptionally if either source cancelled
1844 <     */
1845 <    public void testApplyToEitherAsync4() {
1846 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1847 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1848 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1849 <        assertTrue(f.cancel(true));
1850 <        checkCompletedWithWrappedCancellationException(g);
1851 <
1852 <        f = new CompletableFuture<>();
1853 <        f2 = new CompletableFuture<>();
1854 <        assertTrue(f2.cancel(true));
1855 <        g = f.applyToEitherAsync(f2, inc);
1856 <        checkCompletedWithWrappedCancellationException(g);
1857 <    }
1858 <
1859 <    /**
1860 <     * acceptEitherAsync result completes normally after normal
1861 <     * completion of sources
1862 <     */
1863 <    public void testAcceptEitherAsync() {
1864 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1865 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1866 <        IncAction r = new IncAction();
1867 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1868 <        f.complete(one);
1869 <        checkCompletedNormally(g, null);
1870 <        assertEquals(r.value, 2);
1871 <
1872 <        r = new IncAction();
1873 <        f = new CompletableFuture<>();
1874 <        f.complete(one);
1875 <        f2 = new CompletableFuture<>();
1876 <        g = f.acceptEitherAsync(f2, r);
1877 <        checkCompletedNormally(g, null);
1878 <        assertEquals(r.value, 2);
1879 <    }
1880 <
1881 <    /**
1882 <     * acceptEitherAsync result completes exceptionally after exceptional
1883 <     * completion of source
1884 <     */
1885 <    public void testAcceptEitherAsync2() {
1886 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1887 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1888 <        IncAction r = new IncAction();
1889 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1890 <        f.completeExceptionally(new CFException());
1891 <        checkCompletedWithWrappedCFException(g);
1892 <
1893 <        r = new IncAction();
1894 <        f = new CompletableFuture<>();
1895 <        f2 = new CompletableFuture<>();
1896 <        f2.completeExceptionally(new CFException());
1897 <        g = f.acceptEitherAsync(f2, r);
1898 <        f.complete(one);
1899 <        checkCompletedWithWrappedCFException(g);
1900 <    }
1901 <
1902 <    /**
1903 <     * acceptEitherAsync result completes exceptionally if action does
1904 <     */
1905 <    public void testAcceptEitherAsync3() {
1906 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1907 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1908 <        FailingConsumer r = new FailingConsumer();
1909 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1910 <        f.complete(one);
1911 <        checkCompletedWithWrappedCFException(g);
1912 <    }
1913 <
1914 <    /**
1915 <     * acceptEitherAsync result completes exceptionally if either
1916 <     * source cancelled
1917 <     */
1918 <    public void testAcceptEitherAsync4() {
1919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1920 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1921 <        IncAction r = new IncAction();
1922 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1923 <        assertTrue(f.cancel(true));
1924 <        checkCompletedWithWrappedCancellationException(g);
1925 <
1926 <        r = new IncAction();
1927 <        f = new CompletableFuture<>();
1928 <        f2 = new CompletableFuture<>();
1929 <        assertTrue(f2.cancel(true));
1930 <        g = f.acceptEitherAsync(f2, r);
1931 <        checkCompletedWithWrappedCancellationException(g);
1932 <    }
1933 <
1934 <    /**
1935 <     * runAfterEitherAsync result completes normally after normal
1936 <     * completion of sources
1937 <     */
1938 <    public void testRunAfterEitherAsync() {
1939 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1940 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1941 <        Noop r = new Noop();
1942 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1943 <        f.complete(one);
1944 <        checkCompletedNormally(g, null);
1945 <        assertTrue(r.ran);
1946 <
1947 <        r = new Noop();
1948 <        f = new CompletableFuture<>();
1949 <        f.complete(one);
1950 <        f2 = new CompletableFuture<>();
1951 <        g = f.runAfterEitherAsync(f2, r);
1952 <        checkCompletedNormally(g, null);
1953 <        assertTrue(r.ran);
1954 <    }
1955 <
1956 <    /**
1957 <     * runAfterEitherAsync result completes exceptionally after exceptional
1958 <     * completion of source
1959 <     */
1960 <    public void testRunAfterEitherAsync2() {
1961 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1962 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1963 <        Noop r = new Noop();
1964 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1965 <        f.completeExceptionally(new CFException());
1966 <        checkCompletedWithWrappedCFException(g);
2123 >    }}
2124  
2125 <        r = new Noop();
2126 <        f = new CompletableFuture<>();
2127 <        f2 = new CompletableFuture<>();
2128 <        f2.completeExceptionally(new CFException());
2129 <        g = f.runAfterEitherAsync(f2, r);
2130 <        f.complete(one);
2131 <        checkCompletedWithWrappedCFException(g);
2132 <    }
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 >    {
2130 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2131 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2132 >        final IncAction r = new IncAction();
2133 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2134 >
2135 >        assertTrue(g.cancel(mayInterruptIfRunning));
2136 >        checkCompletedWithWrappedCancellationException(h);
2137 >        f.complete(v1);
2138 >
2139 >        checkCancelled(g);
2140 >        assertEquals(0, r.invocationCount);
2141 >        checkCompletedNormally(f, v1);
2142 >        checkCompletedWithWrappedCancellationException(h);
2143 >    }}
2144 >
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 >    {
2150 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2151 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2152 >        final IncAction r = new IncAction();
2153 >
2154 >        assertTrue(g.cancel(mayInterruptIfRunning));
2155 >        f.complete(v1);
2156 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157  
2158 <    /**
2159 <     * runAfterEitherAsync result completes exceptionally if action does
1979 <     */
1980 <    public void testRunAfterEitherAsync3() {
1981 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1982 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1983 <        FailingNoop r = new FailingNoop();
1984 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1985 <        f.complete(one);
1986 <        checkCompletedWithWrappedCFException(g);
1987 <    }
1988 <
1989 <    /**
1990 <     * runAfterEitherAsync result completes exceptionally if either
1991 <     * source cancelled
1992 <     */
1993 <    public void testRunAfterEitherAsync4() {
1994 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1995 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1996 <        Noop r = new Noop();
1997 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1998 <        assertTrue(f.cancel(true));
1999 <        checkCompletedWithWrappedCancellationException(g);
2000 <
2001 <        r = new Noop();
2002 <        f = new CompletableFuture<>();
2003 <        f2 = new CompletableFuture<>();
2004 <        assertTrue(f2.cancel(true));
2005 <        g = f.runAfterEitherAsync(f2, r);
2006 <        checkCompletedWithWrappedCancellationException(g);
2007 <    }
2008 <
2009 <    /**
2010 <     * thenComposeAsync result completes normally after normal
2011 <     * completion of source
2012 <     */
2013 <    public void testThenComposeAsync() {
2014 <        CompletableFuture<Integer> f, g;
2015 <        CompletableFutureInc r;
2016 <
2017 <        f = new CompletableFuture<>();
2018 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2019 <        f.complete(one);
2020 <        checkCompletedNormally(g, two);
2021 <
2022 <        f = new CompletableFuture<>();
2023 <        f.complete(one);
2024 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2025 <        checkCompletedNormally(g, two);
2026 <    }
2027 <
2028 <    /**
2029 <     * thenComposeAsync result completes exceptionally after
2030 <     * exceptional completion of source
2031 <     */
2032 <    public void testThenComposeAsync2() {
2033 <        CompletableFuture<Integer> f, g;
2034 <        CompletableFutureInc r;
2035 <
2036 <        f = new CompletableFuture<>();
2037 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2038 <        f.completeExceptionally(new CFException());
2039 <        checkCompletedWithWrappedCFException(g);
2040 <        assertFalse(r.ran);
2041 <
2042 <        f = new CompletableFuture<>();
2043 <        f.completeExceptionally(new CFException());
2044 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2045 <        checkCompletedWithWrappedCFException(g);
2046 <        assertFalse(r.ran);
2047 <    }
2048 <
2049 <    /**
2050 <     * thenComposeAsync result completes exceptionally if action does
2051 <     */
2052 <    public void testThenComposeAsync3() {
2053 <        CompletableFuture<Integer> f, g;
2054 <        FailingCompletableFutureFunction r;
2055 <
2056 <        f = new CompletableFuture<>();
2057 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2058 <        f.complete(one);
2059 <        checkCompletedWithWrappedCFException(g);
2060 <
2061 <        f = new CompletableFuture<>();
2062 <        f.complete(one);
2063 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2064 <        checkCompletedWithWrappedCFException(g);
2065 <    }
2066 <
2067 <    /**
2068 <     * thenComposeAsync result completes exceptionally if source cancelled
2069 <     */
2070 <    public void testThenComposeAsync4() {
2071 <        CompletableFuture<Integer> f, g;
2072 <        CompletableFutureInc r;
2073 <
2074 <        f = new CompletableFuture<>();
2075 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2076 <        assertTrue(f.cancel(true));
2077 <        checkCompletedWithWrappedCancellationException(g);
2078 <
2079 <        f = new CompletableFuture<>();
2080 <        assertTrue(f.cancel(true));
2081 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2082 <        checkCompletedWithWrappedCancellationException(g);
2083 <    }
2084 <
2085 <    // async with explicit executors
2086 <
2087 <    /**
2088 <     * thenRunAsync result completes normally after normal completion of source
2089 <     */
2090 <    public void testThenRunAsyncE() {
2091 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2092 <        Noop r = new Noop();
2093 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2094 <        f.complete(null);
2095 <        checkCompletedNormally(g, null);
2096 <
2097 <        // reordered version
2098 <        f = new CompletableFuture<>();
2099 <        f.complete(null);
2100 <        r = new Noop();
2101 <        g = f.thenRunAsync(r, new ThreadExecutor());
2102 <        checkCompletedNormally(g, null);
2103 <    }
2104 <
2105 <    /**
2106 <     * thenRunAsync result completes exceptionally after exceptional
2107 <     * completion of source
2108 <     */
2109 <    public void testThenRunAsync2E() {
2110 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2111 <        Noop r = new Noop();
2112 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2113 <        f.completeExceptionally(new CFException());
2158 >        // unspecified behavior
2159 >        Integer v;
2160          try {
2161 <            g.join();
2162 <            shouldThrow();
2163 <        } catch (CompletionException success) {}
2164 <        checkCompletedWithWrappedCFException(g);
2165 <    }
2166 <
2167 <    /**
2122 <     * thenRunAsync result completes exceptionally if action does
2123 <     */
2124 <    public void testThenRunAsync3E() {
2125 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2126 <        FailingNoop r = new FailingNoop();
2127 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2128 <        f.complete(null);
2129 <        checkCompletedWithWrappedCFException(g);
2130 <    }
2131 <
2132 <    /**
2133 <     * thenRunAsync result completes exceptionally if source cancelled
2134 <     */
2135 <    public void testThenRunAsync4E() {
2136 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2137 <        Noop r = new Noop();
2138 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2139 <        assertTrue(f.cancel(true));
2140 <        checkCompletedWithWrappedCancellationException(g);
2141 <    }
2142 <
2143 <    /**
2144 <     * thenApplyAsync result completes normally after normal completion of source
2145 <     */
2146 <    public void testThenApplyAsyncE() {
2147 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2148 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2149 <        f.complete(one);
2150 <        checkCompletedNormally(g, two);
2151 <    }
2152 <
2153 <    /**
2154 <     * thenApplyAsync result completes exceptionally after exceptional
2155 <     * completion of source
2156 <     */
2157 <    public void testThenApplyAsync2E() {
2158 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2159 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2160 <        f.completeExceptionally(new CFException());
2161 <        checkCompletedWithWrappedCFException(g);
2162 <    }
2163 <
2164 <    /**
2165 <     * thenApplyAsync result completes exceptionally if action does
2166 <     */
2167 <    public void testThenApplyAsync3E() {
2168 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2169 <        FailingFunction r = new FailingFunction();
2170 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2171 <        f.complete(null);
2172 <        checkCompletedWithWrappedCFException(g);
2173 <    }
2174 <
2175 <    /**
2176 <     * thenApplyAsync result completes exceptionally if source cancelled
2177 <     */
2178 <    public void testThenApplyAsync4E() {
2179 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2180 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2181 <        assertTrue(f.cancel(true));
2182 <        checkCompletedWithWrappedCancellationException(g);
2183 <    }
2184 <
2185 <    /**
2186 <     * thenAcceptAsync result completes normally after normal
2187 <     * completion of source
2188 <     */
2189 <    public void testThenAcceptAsyncE() {
2190 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2191 <        IncAction r = new IncAction();
2192 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2193 <        f.complete(one);
2194 <        checkCompletedNormally(g, null);
2195 <        assertEquals(r.value, 2);
2196 <    }
2197 <
2198 <    /**
2199 <     * thenAcceptAsync result completes exceptionally after exceptional
2200 <     * completion of source
2201 <     */
2202 <    public void testThenAcceptAsync2E() {
2203 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2204 <        IncAction r = new IncAction();
2205 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2206 <        f.completeExceptionally(new CFException());
2207 <        checkCompletedWithWrappedCFException(g);
2208 <    }
2209 <
2210 <    /**
2211 <     * thenAcceptAsync result completes exceptionally if action does
2212 <     */
2213 <    public void testThenAcceptAsync3E() {
2214 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2215 <        FailingConsumer r = new FailingConsumer();
2216 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2217 <        f.complete(null);
2218 <        checkCompletedWithWrappedCFException(g);
2219 <    }
2220 <
2221 <    /**
2222 <     * thenAcceptAsync result completes exceptionally if source cancelled
2223 <     */
2224 <    public void testThenAcceptAsync4E() {
2225 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2226 <        IncAction r = new IncAction();
2227 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2228 <        assertTrue(f.cancel(true));
2229 <        checkCompletedWithWrappedCancellationException(g);
2230 <    }
2231 <
2232 <    /**
2233 <     * thenCombineAsync result completes normally after normal
2234 <     * completion of sources
2235 <     */
2236 <    public void testThenCombineAsyncE() {
2237 <        CompletableFuture<Integer> f, g, h;
2238 <        ThreadExecutor e = new ThreadExecutor();
2239 <        int count = 0;
2240 <
2241 <        f = new CompletableFuture<>();
2242 <        g = new CompletableFuture<>();
2243 <        h = f.thenCombineAsync(g, subtract, e);
2244 <        f.complete(3);
2245 <        checkIncomplete(h);
2246 <        g.complete(1);
2247 <        checkCompletedNormally(h, 2);
2248 <        assertEquals(++count, e.count.get());
2249 <
2250 <        f = new CompletableFuture<>();
2251 <        g = new CompletableFuture<>();
2252 <        h = f.thenCombineAsync(g, subtract, e);
2253 <        g.complete(1);
2254 <        checkIncomplete(h);
2255 <        f.complete(3);
2256 <        checkCompletedNormally(h, 2);
2257 <        assertEquals(++count, e.count.get());
2258 <
2259 <        f = new CompletableFuture<>();
2260 <        g = new CompletableFuture<>();
2261 <        g.complete(1);
2262 <        f.complete(3);
2263 <        h = f.thenCombineAsync(g, subtract, e);
2264 <        checkCompletedNormally(h, 2);
2265 <        assertEquals(++count, e.count.get());
2266 <    }
2267 <
2268 <    /**
2269 <     * thenCombineAsync result completes exceptionally after exceptional
2270 <     * completion of either source
2271 <     */
2272 <    public void testThenCombineAsync2E() {
2273 <        CompletableFuture<Integer> f, g, h;
2274 <        ThreadExecutor e = new ThreadExecutor();
2275 <        int count = 0;
2276 <
2277 <        f = new CompletableFuture<>();
2278 <        g = new CompletableFuture<>();
2279 <        h = f.thenCombineAsync(g, subtract, e);
2280 <        f.completeExceptionally(new CFException());
2281 <        checkIncomplete(h);
2282 <        g.complete(1);
2283 <        checkCompletedWithWrappedCFException(h);
2284 <
2285 <        f = new CompletableFuture<>();
2286 <        g = new CompletableFuture<>();
2287 <        h = f.thenCombineAsync(g, subtract, e);
2288 <        g.completeExceptionally(new CFException());
2289 <        checkIncomplete(h);
2290 <        f.complete(3);
2291 <        checkCompletedWithWrappedCFException(h);
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  
2169 <        f = new CompletableFuture<>();
2170 <        g = new CompletableFuture<>();
2171 <        g.completeExceptionally(new CFException());
2172 <        h = f.thenCombineAsync(g, subtract, e);
2173 <        checkIncomplete(h);
2174 <        f.complete(3);
2175 <        checkCompletedWithWrappedCFException(h);
2169 >        checkCancelled(g);
2170 >        checkCompletedNormally(f, v1);
2171 >    }}
2172 >
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 >    {
2178 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180 >        final IncAction r = new IncAction();
2181 >
2182 >        assertTrue(f.cancel(mayInterruptIfRunning));
2183 >        g.complete(v1);
2184 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2185  
2186 <        assertEquals(0, e.count.get());
2187 <    }
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  
2197 <    /**
2198 <     * thenCombineAsync result completes exceptionally if action does
2199 <     */
2307 <    public void testThenCombineAsync3E() {
2308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2309 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2310 <        FailingBiFunction r = new FailingBiFunction();
2311 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2312 <        f.complete(one);
2313 <        checkIncomplete(g);
2314 <        assertFalse(r.ran);
2315 <        f2.complete(two);
2316 <        checkCompletedWithWrappedCFException(g);
2317 <        assertTrue(r.ran);
2318 <    }
2197 >        checkCancelled(f);
2198 >        checkCompletedNormally(g, v1);
2199 >    }}
2200  
2201      /**
2202 <     * thenCombineAsync result completes exceptionally if either source cancelled
2202 >     * runAfterEither result completes normally after normal completion
2203 >     * of either source
2204       */
2205 <    public void testThenCombineAsync4E() {
2206 <        CompletableFuture<Integer> f, g, h;
2207 <        ThreadExecutor e = new ThreadExecutor();
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 >    {
2210 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2211 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2212 >        final Noop r = new Noop(m);
2213 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2214  
2215 <        f = new CompletableFuture<>();
2216 <        g = new CompletableFuture<>();
2217 <        h = f.thenCombineAsync(g, subtract, e);
2218 <        assertTrue(f.cancel(true));
2331 <        checkIncomplete(h);
2332 <        g.complete(1);
2333 <        checkCompletedWithWrappedCancellationException(h);
2334 <
2335 <        f = new CompletableFuture<>();
2336 <        g = new CompletableFuture<>();
2337 <        h = f.thenCombineAsync(g, subtract, e);
2338 <        assertTrue(g.cancel(true));
2339 <        checkIncomplete(h);
2340 <        f.complete(3);
2341 <        checkCompletedWithWrappedCancellationException(h);
2342 <
2343 <        f = new CompletableFuture<>();
2344 <        g = new CompletableFuture<>();
2345 <        assertTrue(g.cancel(true));
2346 <        h = f.thenCombineAsync(g, subtract, e);
2347 <        checkIncomplete(h);
2348 <        f.complete(3);
2349 <        checkCompletedWithWrappedCancellationException(h);
2350 <
2351 <        f = new CompletableFuture<>();
2352 <        g = new CompletableFuture<>();
2353 <        assertTrue(f.cancel(true));
2354 <        assertTrue(g.cancel(true));
2355 <        h = f.thenCombineAsync(g, subtract, e);
2356 <        checkCompletedWithWrappedCancellationException(h);
2215 >        f.complete(v1);
2216 >        checkCompletedNormally(h, null);
2217 >        assertEquals(1, r.invocationCount);
2218 >        g.complete(v2);
2219  
2220 <        assertEquals(0, e.count.get());
2221 <    }
2220 >        checkCompletedNormally(f, v1);
2221 >        checkCompletedNormally(g, v2);
2222 >        checkCompletedNormally(h, null);
2223 >        assertEquals(1, r.invocationCount);
2224 >    }}
2225  
2226 <    /**
2227 <     * thenAcceptBothAsync result completes normally after normal
2228 <     * completion of sources
2229 <     */
2230 <    public void testThenAcceptBothAsyncE() {
2231 <        CompletableFuture<Integer> f, g;
2232 <        CompletableFuture<Void> h;
2233 <        SubtractAction r;
2234 <        ThreadExecutor e = new ThreadExecutor();
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 >    {
2231 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2232 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2233 >        final Noop r = new Noop(m);
2234 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2235  
2236 <        f = new CompletableFuture<>();
2372 <        g = new CompletableFuture<>();
2373 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2374 <        f.complete(3);
2375 <        checkIncomplete(h);
2376 <        g.complete(1);
2236 >        g.complete(v2);
2237          checkCompletedNormally(h, null);
2238 <        assertEquals(r.value, 2);
2238 >        assertEquals(1, r.invocationCount);
2239 >        f.complete(v1);
2240  
2241 <        f = new CompletableFuture<>();
2242 <        g = new CompletableFuture<>();
2382 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2383 <        g.complete(1);
2384 <        checkIncomplete(h);
2385 <        f.complete(3);
2241 >        checkCompletedNormally(f, v1);
2242 >        checkCompletedNormally(g, v2);
2243          checkCompletedNormally(h, null);
2244 <        assertEquals(r.value, 2);
2244 >        assertEquals(1, r.invocationCount);
2245 >        }}
2246  
2247 <        f = new CompletableFuture<>();
2248 <        g = new CompletableFuture<>();
2249 <        g.complete(1);
2250 <        f.complete(3);
2251 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2252 <        checkCompletedNormally(h, null);
2253 <        assertEquals(r.value, 2);
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 >    {
2252 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2253 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2254 >        final Noop r = new Noop(m);
2255 >
2256 >        f.complete(v1);
2257 >        g.complete(v2);
2258 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2259  
2260 <        assertEquals(3, e.count.get());
2261 <    }
2260 >        checkCompletedNormally(h, null);
2261 >        checkCompletedNormally(f, v1);
2262 >        checkCompletedNormally(g, v2);
2263 >        assertEquals(1, r.invocationCount);
2264 >    }}
2265  
2266      /**
2267 <     * thenAcceptBothAsync result completes exceptionally after exceptional
2268 <     * completion of source
2267 >     * runAfterEither result completes exceptionally after exceptional
2268 >     * completion of either source
2269       */
2270 <    public void testThenAcceptBothAsync2E() {
2271 <        CompletableFuture<Integer> f, g;
2272 <        CompletableFuture<Void> h;
2273 <        SubtractAction r;
2274 <        ThreadExecutor e = new ThreadExecutor();
2275 <
2276 <        f = new CompletableFuture<>();
2277 <        g = new CompletableFuture<>();
2278 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2279 <        f.completeExceptionally(new CFException());
2280 <        checkIncomplete(h);
2281 <        g.complete(1);
2282 <        checkCompletedWithWrappedCFException(h);
2270 >    public void testRunAfterEither_exceptionalCompletion1() {
2271 >        for (ExecutionMode m : ExecutionMode.values())
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 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 >        assertEquals(0, r.invocationCount);
2285 >        checkCompletedNormally(g, v1);
2286 >        checkCompletedWithWrappedCFException(f, ex);
2287 >        checkCompletedWithWrappedCFException(h, ex);
2288 >    }}
2289 >
2290 >    public void testRunAfterEither_exceptionalCompletion2() {
2291 >        for (ExecutionMode m : ExecutionMode.values())
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 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 >        assertEquals(0, r.invocationCount);
2305 >        checkCompletedNormally(f, v1);
2306 >        checkCompletedWithWrappedCFException(g, ex);
2307 >        checkCompletedWithWrappedCFException(h, ex);
2308 >    }}
2309 >
2310 >    public void testRunAfterEither_exceptionalCompletion3() {
2311 >        for (ExecutionMode m : ExecutionMode.values())
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 Noop r = new Noop(m);
2317 >        final CFException ex = new CFException();
2318 >
2319 >        g.completeExceptionally(ex);
2320 >        f.complete(v1);
2321 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2322  
2323 <        f = new CompletableFuture<>();
2324 <        g = new CompletableFuture<>();
2325 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2326 <        g.completeExceptionally(new CFException());
2327 <        checkIncomplete(h);
2328 <        f.complete(3);
2329 <        checkCompletedWithWrappedCFException(h);
2323 >        // unspecified behavior
2324 >        Integer v;
2325 >        try {
2326 >            assertNull(h.join());
2327 >            assertEquals(1, r.invocationCount);
2328 >        } catch (CompletionException ok) {
2329 >            checkCompletedWithWrappedCFException(h, ex);
2330 >            assertEquals(0, r.invocationCount);
2331 >        }
2332  
2333 <        f = new CompletableFuture<>();
2334 <        g = new CompletableFuture<>();
2335 <        f.complete(3);
2336 <        g.completeExceptionally(new CFException());
2337 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2338 <        checkCompletedWithWrappedCFException(h);
2333 >        checkCompletedWithWrappedCFException(g, ex);
2334 >        checkCompletedNormally(f, v1);
2335 >    }}
2336 >
2337 >    public void testRunAfterEither_exceptionalCompletion4() {
2338 >        for (ExecutionMode m : ExecutionMode.values())
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 Noop r = new Noop(m);
2344 >        final CFException ex = new CFException();
2345 >
2346 >        f.completeExceptionally(ex);
2347 >        g.complete(v1);
2348 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2349  
2350 <        f = new CompletableFuture<>();
2351 <        g = new CompletableFuture<>();
2352 <        f.completeExceptionally(new CFException());
2353 <        g.complete(3);
2354 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2355 <        checkCompletedWithWrappedCFException(h);
2350 >        // unspecified behavior
2351 >        Integer v;
2352 >        try {
2353 >            assertNull(h.join());
2354 >            assertEquals(1, r.invocationCount);
2355 >        } catch (CompletionException ok) {
2356 >            checkCompletedWithWrappedCFException(h, ex);
2357 >            assertEquals(0, r.invocationCount);
2358 >        }
2359  
2360 <        assertEquals(0, e.count.get());
2361 <    }
2360 >        checkCompletedWithWrappedCFException(f, ex);
2361 >        checkCompletedNormally(g, v1);
2362 >    }}
2363  
2364      /**
2365 <     * thenAcceptBothAsync result completes exceptionally if action does
2365 >     * runAfterEither result completes exceptionally if action does
2366       */
2367 <    public void testThenAcceptBothAsync3E() {
2368 <        CompletableFuture<Integer> f, g;
2369 <        CompletableFuture<Void> h;
2370 <        FailingBiConsumer r;
2371 <        ThreadExecutor e = new ThreadExecutor();
2372 <
2373 <        f = new CompletableFuture<>();
2374 <        g = new CompletableFuture<>();
2375 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2376 <        f.complete(3);
2377 <        checkIncomplete(h);
2378 <        g.complete(1);
2379 <        checkCompletedWithWrappedCFException(h);
2380 <
2381 <        f = new CompletableFuture<>();
2382 <        g = new CompletableFuture<>();
2383 <        f.complete(3);
2384 <        g.complete(1);
2385 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2386 <        checkCompletedWithWrappedCFException(h);
2387 <
2388 <        assertEquals(2, e.count.get());
2389 <    }
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 >    {
2372 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2373 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
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 >    }}
2383 >
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 >    {
2389 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2390 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
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 >    }}
2400  
2401      /**
2402 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2402 >     * runAfterEither result completes exceptionally if either source cancelled
2403       */
2404 <    public void testThenAcceptBothAsync4E() {
2405 <        CompletableFuture<Integer> f, g;
2406 <        CompletableFuture<Void> h;
2407 <        SubtractAction r;
2408 <        ThreadExecutor e = new ThreadExecutor();
2409 <
2410 <        f = new CompletableFuture<>();
2411 <        g = new CompletableFuture<>();
2412 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2482 <        assertTrue(f.cancel(true));
2483 <        checkIncomplete(h);
2484 <        g.complete(1);
2485 <        checkCompletedWithWrappedCancellationException(h);
2486 <
2487 <        f = new CompletableFuture<>();
2488 <        g = new CompletableFuture<>();
2489 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2490 <        assertTrue(g.cancel(true));
2491 <        checkIncomplete(h);
2492 <        f.complete(3);
2493 <        checkCompletedWithWrappedCancellationException(h);
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 >    {
2409 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2410 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2411 >        final Noop r = new Noop(m);
2412 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2413  
2414 <        f = new CompletableFuture<>();
2496 <        g = new CompletableFuture<>();
2497 <        f.complete(3);
2498 <        assertTrue(g.cancel(true));
2499 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2414 >        assertTrue(f.cancel(mayInterruptIfRunning));
2415          checkCompletedWithWrappedCancellationException(h);
2416 +        g.complete(v1);
2417  
2418 <        f = new CompletableFuture<>();
2419 <        g = new CompletableFuture<>();
2420 <        assertTrue(f.cancel(true));
2505 <        g.complete(3);
2506 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2418 >        checkCancelled(f);
2419 >        assertEquals(0, r.invocationCount);
2420 >        checkCompletedNormally(g, v1);
2421          checkCompletedWithWrappedCancellationException(h);
2422 +    }}
2423  
2424 <        assertEquals(0, e.count.get());
2425 <    }
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 >    {
2429 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2430 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
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 >        assertEquals(0, r.invocationCount);
2440 >        checkCompletedNormally(f, v1);
2441 >        checkCompletedWithWrappedCancellationException(h);
2442 >    }}
2443 >
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 >    {
2449 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2450 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2451 >        final Noop r = new Noop(m);
2452 >
2453 >        assertTrue(g.cancel(mayInterruptIfRunning));
2454 >        f.complete(v1);
2455 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456  
2457 <    /**
2458 <     * runAfterBothAsync result completes normally after normal
2459 <     * completion of sources
2460 <     */
2461 <    public void testRunAfterBothAsyncE() {
2462 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2463 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2464 <        Noop r = new Noop();
2465 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2521 <        f.complete(one);
2522 <        checkIncomplete(g);
2523 <        f2.complete(two);
2524 <        checkCompletedNormally(g, null);
2525 <        assertTrue(r.ran);
2526 <    }
2527 <
2528 <    /**
2529 <     * runAfterBothAsync result completes exceptionally after exceptional
2530 <     * completion of source
2531 <     */
2532 <    public void testRunAfterBothAsync2E() {
2533 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2534 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2535 <        Noop r = new Noop();
2536 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2537 <        f.completeExceptionally(new CFException());
2538 <        f2.complete(two);
2539 <        checkCompletedWithWrappedCFException(g);
2540 <
2541 <        r = new Noop();
2542 <        f = new CompletableFuture<>();
2543 <        f2 = new CompletableFuture<>();
2544 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2545 <        f.complete(one);
2546 <        f2.completeExceptionally(new CFException());
2547 <        checkCompletedWithWrappedCFException(g);
2548 <    }
2549 <
2550 <    /**
2551 <     * runAfterBothAsync result completes exceptionally if action does
2552 <     */
2553 <    public void testRunAfterBothAsync3E() {
2554 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2555 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2556 <        FailingNoop r = new FailingNoop();
2557 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2558 <        f.complete(one);
2559 <        checkIncomplete(g);
2560 <        f2.complete(two);
2561 <        checkCompletedWithWrappedCFException(g);
2562 <    }
2563 <
2564 <    /**
2565 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2566 <     */
2567 <    public void testRunAfterBothAsync4E() {
2568 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2569 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2570 <        Noop r = new Noop();
2571 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2572 <        assertTrue(f.cancel(true));
2573 <        f2.complete(two);
2574 <        checkCompletedWithWrappedCancellationException(g);
2575 <
2576 <        r = new Noop();
2577 <        f = new CompletableFuture<>();
2578 <        f2 = new CompletableFuture<>();
2579 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2580 <        f.complete(one);
2581 <        assertTrue(f2.cancel(true));
2582 <        checkCompletedWithWrappedCancellationException(g);
2583 <    }
2584 <
2585 <    /**
2586 <     * applyToEitherAsync result completes normally after normal
2587 <     * completion of sources
2588 <     */
2589 <    public void testApplyToEitherAsyncE() {
2590 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2591 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2592 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2593 <        f.complete(one);
2594 <        checkCompletedNormally(g, two);
2595 <
2596 <        f = new CompletableFuture<>();
2597 <        f.complete(one);
2598 <        f2 = new CompletableFuture<>();
2599 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2600 <        checkCompletedNormally(g, two);
2601 <    }
2602 <
2603 <    /**
2604 <     * applyToEitherAsync result completes exceptionally after exceptional
2605 <     * completion of source
2606 <     */
2607 <    public void testApplyToEitherAsync2E() {
2608 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2609 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2610 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2611 <        f.completeExceptionally(new CFException());
2612 <        checkCompletedWithWrappedCFException(g);
2613 <
2614 <        f = new CompletableFuture<>();
2615 <        f2 = new CompletableFuture<>();
2616 <        f2.completeExceptionally(new CFException());
2617 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618 <        f.complete(one);
2619 <        checkCompletedWithWrappedCFException(g);
2620 <    }
2621 <
2622 <    /**
2623 <     * applyToEitherAsync result completes exceptionally if action does
2624 <     */
2625 <    public void testApplyToEitherAsync3E() {
2626 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2627 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2628 <        FailingFunction r = new FailingFunction();
2629 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2630 <        f.complete(one);
2631 <        checkCompletedWithWrappedCFException(g);
2632 <    }
2633 <
2634 <    /**
2635 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2636 <     */
2637 <    public void testApplyToEitherAsync4E() {
2638 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2639 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2640 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2641 <        assertTrue(f.cancel(true));
2642 <        checkCompletedWithWrappedCancellationException(g);
2643 <
2644 <        f = new CompletableFuture<>();
2645 <        f2 = new CompletableFuture<>();
2646 <        assertTrue(f2.cancel(true));
2647 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2648 <        checkCompletedWithWrappedCancellationException(g);
2649 <    }
2650 <
2651 <    /**
2652 <     * acceptEitherAsync result completes normally after normal
2653 <     * completion of sources
2654 <     */
2655 <    public void testAcceptEitherAsyncE() {
2656 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2657 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2658 <        IncAction r = new IncAction();
2659 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2660 <        f.complete(one);
2661 <        checkCompletedNormally(g, null);
2662 <        assertEquals(r.value, 2);
2663 <
2664 <        r = new IncAction();
2665 <        f = new CompletableFuture<>();
2666 <        f.complete(one);
2667 <        f2 = new CompletableFuture<>();
2668 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2669 <        checkCompletedNormally(g, null);
2670 <        assertEquals(r.value, 2);
2671 <    }
2672 <
2673 <    /**
2674 <     * acceptEitherAsync result completes exceptionally after exceptional
2675 <     * completion of source
2676 <     */
2677 <    public void testAcceptEitherAsync2E() {
2678 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2679 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2680 <        IncAction r = new IncAction();
2681 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2682 <        f.completeExceptionally(new CFException());
2683 <        checkCompletedWithWrappedCFException(g);
2684 <
2685 <        r = new IncAction();
2686 <        f = new CompletableFuture<>();
2687 <        f2 = new CompletableFuture<>();
2688 <        f2.completeExceptionally(new CFException());
2689 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2690 <        f.complete(one);
2691 <        checkCompletedWithWrappedCFException(g);
2692 <    }
2457 >        // unspecified behavior
2458 >        Integer v;
2459 >        try {
2460 >            assertNull(h.join());
2461 >            assertEquals(1, r.invocationCount);
2462 >        } catch (CompletionException ok) {
2463 >            checkCompletedWithWrappedCancellationException(h);
2464 >            assertEquals(0, r.invocationCount);
2465 >        }
2466  
2467 <    /**
2468 <     * acceptEitherAsync result completes exceptionally if action does
2469 <     */
2470 <    public void testAcceptEitherAsync3E() {
2471 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2472 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2473 <        FailingConsumer r = new FailingConsumer();
2474 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2475 <        f.complete(one);
2476 <        checkCompletedWithWrappedCFException(g);
2477 <    }
2467 >        checkCancelled(g);
2468 >        checkCompletedNormally(f, v1);
2469 >    }}
2470 >
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 >    {
2476 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2477 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2478 >        final Noop r = new Noop(m);
2479 >
2480 >        assertTrue(f.cancel(mayInterruptIfRunning));
2481 >        g.complete(v1);
2482 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2483  
2484 <    /**
2485 <     * acceptEitherAsync result completes exceptionally if either
2486 <     * source cancelled
2487 <     */
2488 <    public void testAcceptEitherAsync4E() {
2489 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2490 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2491 <        IncAction r = new IncAction();
2492 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2715 <        assertTrue(f.cancel(true));
2716 <        checkCompletedWithWrappedCancellationException(g);
2484 >        // unspecified behavior
2485 >        Integer v;
2486 >        try {
2487 >            assertNull(h.join());
2488 >            assertEquals(1, r.invocationCount);
2489 >        } catch (CompletionException ok) {
2490 >            checkCompletedWithWrappedCancellationException(h);
2491 >            assertEquals(0, r.invocationCount);
2492 >        }
2493  
2494 <        r = new IncAction();
2495 <        f = new CompletableFuture<>();
2496 <        f2 = new CompletableFuture<>();
2721 <        assertTrue(f2.cancel(true));
2722 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2723 <        checkCompletedWithWrappedCancellationException(g);
2724 <    }
2494 >        checkCancelled(f);
2495 >        checkCompletedNormally(g, v1);
2496 >    }}
2497  
2498      /**
2499 <     * runAfterEitherAsync result completes normally after normal
2728 <     * completion of sources
2499 >     * thenCompose result completes normally after normal completion of source
2500       */
2501 <    public void testRunAfterEitherAsyncE() {
2502 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2503 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2504 <        Noop r = new Noop();
2505 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2506 <        f.complete(one);
2507 <        checkCompletedNormally(g, null);
2508 <        assertTrue(r.ran);
2509 <
2510 <        r = new Noop();
2511 <        f = new CompletableFuture<>();
2512 <        f.complete(one);
2513 <        f2 = new CompletableFuture<>();
2514 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2515 <        checkCompletedNormally(g, null);
2745 <        assertTrue(r.ran);
2746 <    }
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 >        checkCompletedNormally(g, inc(v1));
2513 >        checkCompletedNormally(f, v1);
2514 >        assertEquals(1, r.invocationCount);
2515 >    }}
2516  
2517      /**
2518 <     * runAfterEitherAsync result completes exceptionally after exceptional
2518 >     * thenCompose result completes exceptionally after exceptional
2519       * completion of source
2520       */
2521 <    public void testRunAfterEitherAsync2E() {
2522 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2523 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2524 <        Noop r = new Noop();
2525 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2526 <        f.completeExceptionally(new CFException());
2527 <        checkCompletedWithWrappedCFException(g);
2528 <
2529 <        r = new Noop();
2530 <        f = new CompletableFuture<>();
2531 <        f2 = new CompletableFuture<>();
2532 <        f2.completeExceptionally(new CFException());
2533 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2534 <        f.complete(one);
2535 <        checkCompletedWithWrappedCFException(g);
2767 <    }
2768 <
2769 <    /**
2770 <     * runAfterEitherAsync result completes exceptionally if action does
2771 <     */
2772 <    public void testRunAfterEitherAsync3E() {
2773 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2774 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2775 <        FailingNoop r = new FailingNoop();
2776 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2777 <        f.complete(one);
2778 <        checkCompletedWithWrappedCFException(g);
2779 <    }
2780 <
2781 <    /**
2782 <     * runAfterEitherAsync result completes exceptionally if either
2783 <     * source cancelled
2784 <     */
2785 <    public void testRunAfterEitherAsync4E() {
2786 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2787 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2788 <        Noop r = new Noop();
2789 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2790 <        assertTrue(f.cancel(true));
2791 <        checkCompletedWithWrappedCancellationException(g);
2792 <
2793 <        r = new Noop();
2794 <        f = new CompletableFuture<>();
2795 <        f2 = new CompletableFuture<>();
2796 <        assertTrue(f2.cancel(true));
2797 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2798 <        checkCompletedWithWrappedCancellationException(g);
2799 <    }
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 >        checkCompletedWithWrappedCFException(g, ex);
2533 >        checkCompletedWithWrappedCFException(f, ex);
2534 >        assertEquals(0, r.invocationCount);
2535 >    }}
2536  
2537      /**
2538 <     * thenComposeAsync result completes normally after normal
2803 <     * completion of source
2538 >     * thenCompose result completes exceptionally if action does
2539       */
2540 <    public void testThenComposeAsyncE() {
2541 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2542 <        CompletableFutureInc r = new CompletableFutureInc();
2543 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2544 <        f.complete(one);
2545 <        checkCompletedNormally(g, two);
2546 <    }
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  
2813    /**
2814     * thenComposeAsync result completes exceptionally after
2815     * exceptional completion of source
2816     */
2817    public void testThenComposeAsync2E() {
2818        CompletableFuture<Integer> f = new CompletableFuture<>();
2819        CompletableFutureInc r = new CompletableFutureInc();
2820        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2821        f.completeExceptionally(new CFException());
2552          checkCompletedWithWrappedCFException(g);
2553 <    }
2553 >        checkCompletedNormally(f, v1);
2554 >    }}
2555  
2556      /**
2557 <     * thenComposeAsync result completes exceptionally if action does
2557 >     * thenCompose result completes exceptionally if source cancelled
2558       */
2559 <    public void testThenComposeAsync3E() {
2560 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2561 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2562 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2563 <        f.complete(one);
2564 <        checkCompletedWithWrappedCFException(g);
2565 <    }
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  
2836    /**
2837     * thenComposeAsync result completes exceptionally if source cancelled
2838     */
2839    public void testThenComposeAsync4E() {
2840        CompletableFuture<Integer> f = new CompletableFuture<>();
2841        CompletableFutureInc r = new CompletableFutureInc();
2842        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2843        assertTrue(f.cancel(true));
2573          checkCompletedWithWrappedCancellationException(g);
2574 <    }
2574 >        checkCancelled(f);
2575 >    }}
2576  
2577      // other static methods
2578  
# Line 2931 | Line 2661 | public class CompletableFutureTest exten
2661          ThreadExecutor exec = new ThreadExecutor();
2662  
2663          Runnable[] throwingActions = {
2664 <            () -> { CompletableFuture.supplyAsync(null); },
2665 <            () -> { CompletableFuture.supplyAsync(null, exec); },
2666 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2667 <
2668 <            () -> { CompletableFuture.runAsync(null); },
2669 <            () -> { CompletableFuture.runAsync(null, exec); },
2670 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
2671 <
2672 <            () -> { f.completeExceptionally(null); },
2673 <
2674 <            () -> { f.thenApply(null); },
2675 <            () -> { f.thenApplyAsync(null); },
2676 <            () -> { f.thenApplyAsync((x) -> x, null); },
2677 <            () -> { f.thenApplyAsync(null, exec); },
2678 <
2679 <            () -> { f.thenAccept(null); },
2680 <            () -> { f.thenAcceptAsync(null); },
2681 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2682 <            () -> { f.thenAcceptAsync(null, exec); },
2683 <
2684 <            () -> { f.thenRun(null); },
2685 <            () -> { f.thenRunAsync(null); },
2686 <            () -> { f.thenRunAsync(() -> { ; }, null); },
2687 <            () -> { f.thenRunAsync(null, exec); },
2688 <
2689 <            () -> { f.thenCombine(g, null); },
2690 <            () -> { f.thenCombineAsync(g, null); },
2691 <            () -> { f.thenCombineAsync(g, null, exec); },
2692 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2693 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2694 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2695 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2696 <
2697 <            () -> { f.thenAcceptBoth(g, null); },
2698 <            () -> { f.thenAcceptBothAsync(g, null); },
2699 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
2700 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2701 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2702 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2703 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2704 <
2705 <            () -> { f.runAfterBoth(g, null); },
2706 <            () -> { f.runAfterBothAsync(g, null); },
2707 <            () -> { f.runAfterBothAsync(g, null, exec); },
2708 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2709 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2710 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2711 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2712 <
2713 <            () -> { f.applyToEither(g, null); },
2714 <            () -> { f.applyToEitherAsync(g, null); },
2715 <            () -> { f.applyToEitherAsync(g, null, exec); },
2716 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
2717 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2718 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2719 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2720 <
2721 <            () -> { f.acceptEither(g, null); },
2722 <            () -> { f.acceptEitherAsync(g, null); },
2723 <            () -> { f.acceptEitherAsync(g, null, exec); },
2724 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2725 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2726 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2727 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2728 <
2729 <            () -> { f.runAfterEither(g, null); },
2730 <            () -> { f.runAfterEitherAsync(g, null); },
2731 <            () -> { f.runAfterEitherAsync(g, null, exec); },
2732 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
2733 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2734 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2735 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2736 <
2737 <            () -> { f.thenCompose(null); },
2738 <            () -> { f.thenComposeAsync(null); },
2739 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2740 <            () -> { f.thenComposeAsync(null, exec); },
2741 <
2742 <            () -> { f.exceptionally(null); },
2743 <
2744 <            () -> { f.handle(null); },
2745 <
2746 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2747 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2748 <            () -> { CompletableFuture.allOf(f, null); },
2749 <            () -> { CompletableFuture.allOf(null, f); },
2750 <
2751 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2752 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2753 <            () -> { CompletableFuture.anyOf(f, null); },
2754 <            () -> { CompletableFuture.anyOf(null, f); },
2664 >            () -> CompletableFuture.supplyAsync(null),
2665 >            () -> CompletableFuture.supplyAsync(null, exec),
2666 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
2667 >
2668 >            () -> CompletableFuture.runAsync(null),
2669 >            () -> CompletableFuture.runAsync(null, exec),
2670 >            () -> CompletableFuture.runAsync(() -> {}, null),
2671 >
2672 >            () -> f.completeExceptionally(null),
2673 >
2674 >            () -> f.thenApply(null),
2675 >            () -> f.thenApplyAsync(null),
2676 >            () -> f.thenApplyAsync((x) -> x, null),
2677 >            () -> f.thenApplyAsync(null, exec),
2678 >
2679 >            () -> f.thenAccept(null),
2680 >            () -> f.thenAcceptAsync(null),
2681 >            () -> f.thenAcceptAsync((x) -> {} , null),
2682 >            () -> f.thenAcceptAsync(null, exec),
2683 >
2684 >            () -> f.thenRun(null),
2685 >            () -> f.thenRunAsync(null),
2686 >            () -> f.thenRunAsync(() -> {} , null),
2687 >            () -> f.thenRunAsync(null, exec),
2688 >
2689 >            () -> f.thenCombine(g, null),
2690 >            () -> f.thenCombineAsync(g, null),
2691 >            () -> f.thenCombineAsync(g, null, exec),
2692 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
2693 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
2694 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
2695 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
2696 >
2697 >            () -> f.thenAcceptBoth(g, null),
2698 >            () -> f.thenAcceptBothAsync(g, null),
2699 >            () -> f.thenAcceptBothAsync(g, null, exec),
2700 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
2701 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
2702 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
2703 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
2704 >
2705 >            () -> f.runAfterBoth(g, null),
2706 >            () -> f.runAfterBothAsync(g, null),
2707 >            () -> f.runAfterBothAsync(g, null, exec),
2708 >            () -> f.runAfterBoth(nullFuture, () -> {}),
2709 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
2710 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
2711 >            () -> f.runAfterBothAsync(g, () -> {}, null),
2712 >
2713 >            () -> f.applyToEither(g, null),
2714 >            () -> f.applyToEitherAsync(g, null),
2715 >            () -> f.applyToEitherAsync(g, null, exec),
2716 >            () -> f.applyToEither(nullFuture, (x) -> x),
2717 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
2718 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
2719 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
2720 >
2721 >            () -> f.acceptEither(g, null),
2722 >            () -> f.acceptEitherAsync(g, null),
2723 >            () -> f.acceptEitherAsync(g, null, exec),
2724 >            () -> f.acceptEither(nullFuture, (x) -> {}),
2725 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
2726 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
2727 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
2728 >
2729 >            () -> f.runAfterEither(g, null),
2730 >            () -> f.runAfterEitherAsync(g, null),
2731 >            () -> f.runAfterEitherAsync(g, null, exec),
2732 >            () -> f.runAfterEither(nullFuture, () -> {}),
2733 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
2734 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
2735 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
2736 >
2737 >            () -> f.thenCompose(null),
2738 >            () -> f.thenComposeAsync(null),
2739 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2740 >            () -> f.thenComposeAsync(null, exec),
2741 >
2742 >            () -> f.exceptionally(null),
2743 >
2744 >            () -> f.handle(null),
2745 >
2746 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
2747 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
2748 >            () -> CompletableFuture.allOf(f, null),
2749 >            () -> CompletableFuture.allOf(null, f),
2750 >
2751 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
2752 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
2753 >            () -> CompletableFuture.anyOf(f, null),
2754 >            () -> CompletableFuture.anyOf(null, f),
2755 >
2756 >            () -> f.obtrudeException(null),
2757          };
2758  
2759          assertThrows(NullPointerException.class, throwingActions);
# Line 3040 | 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 <    }
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 >        checkCompletedNormally(g, v1);
2793 >        checkCompletedNormally(f, v1);
2794 >        assertEquals(1, a.get());
2795 >    }}
2796  
2797      /**
2798       * whenComplete action executes on exceptional completion, propagating
2799       * source result.
2800       */
2801 <    public void testWhenComplete2() {
2802 <        final AtomicInteger a = new AtomicInteger();
2803 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2804 <        CompletableFuture<Integer> g =
2805 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
2806 <        f.completeExceptionally(new CFException());
2807 <        assertTrue(f.isCompletedExceptionally());
2808 <        assertTrue(g.isCompletedExceptionally());
2809 <        assertEquals(a.get(), 1);
2810 <    }
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 >     * whenComplete action executes on cancelled source, propagating
2825 >     * CancellationException.
2826 >     */
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 >        //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       * If a whenComplete action throws an exception when triggered by
2852       * a normal completion, it completes exceptionally
2853       */
2854 <    public void testWhenComplete3() {
2855 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2856 <        CompletableFuture<Integer> g =
2857 <            f.whenComplete((Integer x, Throwable t) ->
2858 <                           { throw new CFException(); } );
2859 <        f.complete(three);
2860 <        checkCompletedNormally(f, three);
2861 <        assertTrue(g.isCompletedExceptionally());
2862 <        checkCompletedWithWrappedCFException(g);
2863 <    }
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 <     * whenCompleteAsync action executes on normal completion, propagating
2879 <     * source result.
2880 <     */
3088 <    public void testWhenCompleteAsync1() {
3089 <        final AtomicInteger a = new AtomicInteger();
3090 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3091 <        CompletableFuture<Integer> g =
3092 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3093 <        f.complete(three);
3094 <        checkCompletedNormally(f, three);
3095 <        checkCompletedNormally(g, three);
3096 <        assertEquals(a.get(), 1);
3097 <    }
3098 <
3099 <    /**
3100 <     * whenCompleteAsync action executes on exceptional completion, propagating
3101 <     * source result.
3102 <     */
3103 <    public void testWhenCompleteAsync2() {
3104 <        final AtomicInteger a = new AtomicInteger();
3105 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3106 <        CompletableFuture<Integer> g =
3107 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3108 <        f.completeExceptionally(new CFException());
3109 <        checkCompletedWithWrappedCFException(f);
3110 <        checkCompletedWithWrappedCFException(g);
3111 <    }
3112 <
3113 <    /**
3114 <     * If a whenCompleteAsync action throws an exception when
3115 <     * triggered by a normal completion, it completes exceptionally
3116 <     */
3117 <    public void testWhenCompleteAsync3() {
3118 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3119 <        CompletableFuture<Integer> g =
3120 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3121 <                           { throw new CFException(); } );
3122 <        f.complete(three);
3123 <        checkCompletedNormally(f, three);
3124 <        checkCompletedWithWrappedCFException(g);
3125 <    }
3126 <
3127 <    /**
3128 <     * whenCompleteAsync action executes on normal completion, propagating
3129 <     * source result.
3130 <     */
3131 <    public void testWhenCompleteAsync1e() {
3132 <        final AtomicInteger a = new AtomicInteger();
3133 <        ThreadExecutor exec = new ThreadExecutor();
3134 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3135 <        CompletableFuture<Integer> g =
3136 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3137 <                                exec);
3138 <        f.complete(three);
3139 <        checkCompletedNormally(f, three);
3140 <        checkCompletedNormally(g, three);
3141 <        assertEquals(a.get(), 1);
3142 <    }
3143 <
3144 <    /**
3145 <     * whenCompleteAsync action executes on exceptional completion, propagating
3146 <     * source result.
3147 <     */
3148 <    public void testWhenCompleteAsync2e() {
3149 <        final AtomicInteger a = new AtomicInteger();
3150 <        ThreadExecutor exec = new ThreadExecutor();
3151 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3152 <        CompletableFuture<Integer> g =
3153 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3154 <                                exec);
3155 <        f.completeExceptionally(new CFException());
3156 <        checkCompletedWithWrappedCFException(f);
3157 <        checkCompletedWithWrappedCFException(g);
3158 <    }
3159 <
3160 <    /**
3161 <     * If a whenCompleteAsync action throws an exception when triggered
3162 <     * by a normal completion, it completes exceptionally
3163 <     */
3164 <    public void testWhenCompleteAsync3e() {
3165 <        ThreadExecutor exec = new ThreadExecutor();
3166 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3167 <        CompletableFuture<Integer> g =
3168 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3169 <                                { throw new CFException(); },
3170 <                                exec);
3171 <        f.complete(three);
3172 <        checkCompletedNormally(f, three);
3173 <        checkCompletedWithWrappedCFException(g);
3174 <    }
3175 <
3176 <    /**
3177 <     * handleAsync action completes normally with function value on
3178 <     * either normal or exceptional completion of source
3179 <     */
3180 <    public void testHandleAsync() {
3181 <        CompletableFuture<Integer> f, g;
3182 <        IntegerHandler r;
3183 <
3184 <        f = new CompletableFuture<>();
3185 <        g = f.handleAsync(r = new IntegerHandler());
3186 <        assertFalse(r.ran);
3187 <        f.completeExceptionally(new CFException());
3188 <        checkCompletedWithWrappedCFException(f);
3189 <        checkCompletedNormally(g, three);
3190 <        assertTrue(r.ran);
3191 <
3192 <        f = new CompletableFuture<>();
3193 <        g = f.handleAsync(r = new IntegerHandler());
3194 <        assertFalse(r.ran);
3195 <        f.completeExceptionally(new CFException());
3196 <        checkCompletedWithWrappedCFException(f);
3197 <        checkCompletedNormally(g, three);
3198 <        assertTrue(r.ran);
3199 <
3200 <        f = new CompletableFuture<>();
3201 <        g = f.handleAsync(r = new IntegerHandler());
3202 <        assertFalse(r.ran);
3203 <        f.complete(one);
3204 <        checkCompletedNormally(f, one);
3205 <        checkCompletedNormally(g, two);
3206 <        assertTrue(r.ran);
3207 <
3208 <        f = new CompletableFuture<>();
3209 <        g = f.handleAsync(r = new IntegerHandler());
3210 <        assertFalse(r.ran);
3211 <        f.complete(one);
3212 <        checkCompletedNormally(f, one);
3213 <        checkCompletedNormally(g, two);
3214 <        assertTrue(r.ran);
3215 <    }
3216 <
3217 <    /**
3218 <     * handleAsync action with Executor completes normally with
3219 <     * function value on either normal or exceptional completion of
3220 <     * 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);
3247 <        checkCompletedNormally(f, one);
3248 <        checkCompletedNormally(g, two);
3249 <        assertTrue(r.ran);
3250 <
3251 <        f = new CompletableFuture<>();
3252 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3253 <        assertFalse(r.ran);
3254 <        f.complete(one);
3255 <        checkCompletedNormally(f, one);
3256 <        checkCompletedNormally(g, two);
3257 <        assertTrue(r.ran);
3258 <    }
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