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.28 by jsr166, Sun Jul 14 16:34:49 2013 UTC vs.
Revision 1.60 by jsr166, Tue Jun 3 06:16:41 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines