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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines