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.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.76 by jsr166, Sat Jun 7 21:45:13 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines