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.22 by jsr166, Mon Apr 8 20:46:59 2013 UTC vs.
Revision 1.74 by jsr166, Fri Jun 6 21:19:22 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines