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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines