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.22 by jsr166, Mon Apr 8 20:46:59 2013 UTC vs.
Revision 1.46 by jsr166, Mon Jun 2 17:41:22 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines