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.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines