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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines