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.27 by jsr166, Mon Jul 8 21:12:28 2013 UTC vs.
Revision 1.58 by jsr166, Mon Jun 2 23:10:08 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines