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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines