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.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC vs.
Revision 1.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC

# Line 17 | Line 17 | 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 103 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 129 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 216 | 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 247 | 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 257 | 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());
265 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298          f = new CompletableFuture<>();
299 +        for (int i = 0; i < 2; i++) {
300 +            f.obtrudeException(ex = new CFException());
301 +            checkCompletedExceptionally(f, ex);
302 +        }
303 +
304 +        f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
275 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320          CompletableFuture<Integer> f = new CompletableFuture<>();
321 <        assertEquals(f.getNumberOfDependents(), 0);
322 <        CompletableFuture g = f.thenRun(new Noop());
323 <        assertEquals(f.getNumberOfDependents(), 1);
324 <        assertEquals(g.getNumberOfDependents(), 0);
325 <        CompletableFuture h = f.thenRun(new Noop());
326 <        assertEquals(f.getNumberOfDependents(), 2);
321 >        assertEquals(0, f.getNumberOfDependents());
322 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
323 >        assertEquals(1, f.getNumberOfDependents());
324 >        assertEquals(0, g.getNumberOfDependents());
325 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
326 >        assertEquals(2, f.getNumberOfDependents());
327          f.complete(1);
328          checkCompletedNormally(g, null);
329 <        assertEquals(f.getNumberOfDependents(), 0);
330 <        assertEquals(g.getNumberOfDependents(), 0);
329 >        assertEquals(0, f.getNumberOfDependents());
330 >        assertEquals(0, g.getNumberOfDependents());
331      }
332  
333      /**
# Line 306 | 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 316 | Line 363 | public class CompletableFutureTest exten
363          checkCompletedNormally(f, "test");
364      }
365  
366 <    // Choose non-commutative actions for better coverage
366 >    abstract class CheckedAction {
367 >        int invocationCount = 0;
368 >        final ExecutionMode m;
369 >        CheckedAction(ExecutionMode m) { this.m = m; }
370 >        void invoked() {
371 >            m.checkExecutionMode();
372 >            assertEquals(0, invocationCount++);
373 >        }
374 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
375 >        void assertInvoked() { assertEquals(1, invocationCount); }
376 >    }
377  
378 <    // A non-commutative function that handles and produces null values as well.
379 <    static Integer subtract(Integer x, Integer y) {
380 <        return (x == null && y == null) ? null :
381 <            ((x == null) ? 42 : x.intValue())
382 <            - ((y == null) ? 99 : y.intValue());
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 >    class IntegerSupplier extends CheckedAction
388 >        implements Supplier<Integer>
389 >    {
390 >        final Integer value;
391 >        IntegerSupplier(ExecutionMode m, Integer value) {
392 >            super(m);
393 >            this.value = value;
394 >        }
395 >        public Integer get() {
396 >            invoked();
397 >            return value;
398 >        }
399      }
400  
401      // A function that handles and produces null values as well.
# Line 330 | Line 403 | public class CompletableFutureTest exten
403          return (x == null) ? null : x + 1;
404      }
405  
406 <    static final Supplier<Integer> supplyOne =
407 <        () -> Integer.valueOf(1);
408 <    static final Function<Integer, Integer> inc =
409 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
337 <    static final BiFunction<Integer, Integer, Integer> subtract =
338 <        (Integer x, Integer y) -> subtract(x, y);
339 <    static final class IncAction implements Consumer<Integer> {
340 <        int invocationCount = 0;
341 <        Integer value;
342 <        public boolean ran() { return invocationCount == 1; }
406 >    class NoopConsumer extends CheckedIntegerAction
407 >        implements Consumer<Integer>
408 >    {
409 >        NoopConsumer(ExecutionMode m) { super(m); }
410          public void accept(Integer x) {
411 <            invocationCount++;
412 <            value = inc(x);
411 >            invoked();
412 >            value = x;
413          }
414      }
415 <    static final class IncFunction implements Function<Integer,Integer> {
416 <        int invocationCount = 0;
417 <        Integer value;
418 <        public boolean ran() { return invocationCount == 1; }
415 >
416 >    class IncFunction extends CheckedIntegerAction
417 >        implements Function<Integer,Integer>
418 >    {
419 >        IncFunction(ExecutionMode m) { super(m); }
420          public Integer apply(Integer x) {
421 <            invocationCount++;
421 >            invoked();
422              return value = inc(x);
423          }
424      }
425 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
426 <        int invocationCount = 0;
427 <        Integer value;
428 <        // Check this action was invoked exactly once when result is computed.
429 <        public boolean ran() { return invocationCount == 1; }
425 >
426 >    // Choose non-commutative actions for better coverage
427 >    // A non-commutative function that handles and produces null values as well.
428 >    static Integer subtract(Integer x, Integer y) {
429 >        return (x == null && y == null) ? null :
430 >            ((x == null) ? 42 : x.intValue())
431 >            - ((y == null) ? 99 : y.intValue());
432 >    }
433 >
434 >    class SubtractAction extends CheckedIntegerAction
435 >        implements BiConsumer<Integer, Integer>
436 >    {
437 >        SubtractAction(ExecutionMode m) { super(m); }
438          public void accept(Integer x, Integer y) {
439 <            invocationCount++;
439 >            invoked();
440              value = subtract(x, y);
441          }
442      }
443 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
444 <        int invocationCount = 0;
445 <        Integer value;
446 <        // Check this action was invoked exactly once when result is computed.
447 <        public boolean ran() { return invocationCount == 1; }
443 >
444 >    class SubtractFunction extends CheckedIntegerAction
445 >        implements BiFunction<Integer, Integer, Integer>
446 >    {
447 >        SubtractFunction(ExecutionMode m) { super(m); }
448          public Integer apply(Integer x, Integer y) {
449 <            invocationCount++;
449 >            invoked();
450              return value = subtract(x, y);
451          }
452      }
453 <    static final class Noop implements Runnable {
454 <        int invocationCount = 0;
455 <        boolean ran;
453 >
454 >    class Noop extends CheckedAction implements Runnable {
455 >        Noop(ExecutionMode m) { super(m); }
456          public void run() {
457 <            invocationCount++;
382 <            ran = true;
457 >            invoked();
458          }
459      }
460  
461 <    static final class FailingSupplier implements Supplier<Integer> {
462 <        boolean ran;
463 <        public Integer get() { ran = true; throw new CFException(); }
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 <    static final class FailingConsumer implements Consumer<Integer> {
471 <        boolean ran;
472 <        public void accept(Integer x) { ran = true; throw new CFException(); }
470 >
471 >    class FailingConsumer extends CheckedIntegerAction
472 >        implements Consumer<Integer>
473 >    {
474 >        FailingConsumer(ExecutionMode m) { super(m); }
475 >        public void accept(Integer x) {
476 >            invoked();
477 >            value = x;
478 >            throw new CFException();
479 >        }
480      }
481 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
482 <        boolean ran;
483 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
481 >
482 >    class FailingBiConsumer extends CheckedIntegerAction
483 >        implements BiConsumer<Integer, Integer>
484 >    {
485 >        FailingBiConsumer(ExecutionMode m) { super(m); }
486 >        public void accept(Integer x, Integer y) {
487 >            invoked();
488 >            value = subtract(x, y);
489 >            throw new CFException();
490 >        }
491      }
492 <    static final class FailingFunction implements Function<Integer, Integer> {
493 <        boolean ran;
494 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
492 >
493 >    class FailingFunction extends CheckedIntegerAction
494 >        implements Function<Integer, Integer>
495 >    {
496 >        FailingFunction(ExecutionMode m) { super(m); }
497 >        public Integer apply(Integer x) {
498 >            invoked();
499 >            value = x;
500 >            throw new CFException();
501 >        }
502      }
503 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
504 <        boolean ran;
505 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
503 >
504 >    class FailingBiFunction extends CheckedIntegerAction
505 >        implements BiFunction<Integer, Integer, Integer>
506 >    {
507 >        FailingBiFunction(ExecutionMode m) { super(m); }
508 >        public Integer apply(Integer x, Integer y) {
509 >            invoked();
510 >            value = subtract(x, y);
511 >            throw new CFException();
512 >        }
513      }
514 <    static final class FailingNoop implements Runnable {
515 <        boolean ran;
516 <        public void run() { ran = true; throw new CFException(); }
514 >
515 >    class FailingRunnable extends CheckedAction implements Runnable {
516 >        FailingRunnable(ExecutionMode m) { super(m); }
517 >        public void run() {
518 >            invoked();
519 >            throw new CFException();
520 >        }
521      }
522  
523 <    static final class CompletableFutureInc
524 <        implements Function<Integer, CompletableFuture<Integer>> {
525 <        boolean ran;
523 >
524 >    class CompletableFutureInc extends CheckedIntegerAction
525 >        implements Function<Integer, CompletableFuture<Integer>>
526 >    {
527 >        CompletableFutureInc(ExecutionMode m) { super(m); }
528          public CompletableFuture<Integer> apply(Integer x) {
529 <            ran = true;
529 >            invoked();
530 >            value = x;
531              CompletableFuture<Integer> f = new CompletableFuture<>();
532 <            f.complete(Integer.valueOf(x.intValue() + 1));
532 >            f.complete(inc(x));
533              return f;
534          }
535      }
536  
537 <    static final class FailingCompletableFutureFunction
538 <        implements Function<Integer, CompletableFuture<Integer>> {
539 <        boolean ran;
537 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
538 >        implements Function<Integer, CompletableFuture<Integer>>
539 >    {
540 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
541          public CompletableFuture<Integer> apply(Integer x) {
542 <            ran = true; throw new CFException();
542 >            invoked();
543 >            value = x;
544 >            throw new CFException();
545          }
546      }
547  
548      // Used for explicit executor tests
549      static final class ThreadExecutor implements Executor {
550 <        AtomicInteger count = new AtomicInteger(0);
550 >        final AtomicInteger count = new AtomicInteger(0);
551 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
552 >        static boolean startedCurrentThread() {
553 >            return Thread.currentThread().getThreadGroup() == tg;
554 >        }
555  
556          public void execute(Runnable r) {
557              count.getAndIncrement();
558 <            new Thread(r).start();
437 <        }
438 <    }
439 <
440 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
441 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
442 <    }
443 <
444 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
445 <        boolean ran;
446 <        public Integer apply(Integer x, Throwable t) {
447 <            ran = true;
448 <            return (t == null) ? two : three;
558 >            new Thread(tg, r).start();
559          }
560      }
561  
562      /**
563       * Permits the testing of parallel code for the 3 different
564 <     * execution modes without repeating all the testing code.
564 >     * execution modes without copy/pasting all the test methods.
565       */
566      enum ExecutionMode {
567          DEFAULT {
568 +            public void checkExecutionMode() {
569 +                assertFalse(ThreadExecutor.startedCurrentThread());
570 +                assertNull(ForkJoinTask.getPool());
571 +            }
572 +            public CompletableFuture<Void> runAsync(Runnable a) {
573 +                throw new UnsupportedOperationException();
574 +            }
575 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576 +                throw new UnsupportedOperationException();
577 +            }
578 +            public <T> CompletableFuture<Void> thenRun
579 +                (CompletableFuture<T> f, Runnable a) {
580 +                return f.thenRun(a);
581 +            }
582 +            public <T> CompletableFuture<Void> thenAccept
583 +                (CompletableFuture<T> f, Consumer<? super T> a) {
584 +                return f.thenAccept(a);
585 +            }
586 +            public <T,U> CompletableFuture<U> thenApply
587 +                (CompletableFuture<T> f, Function<? super T,U> a) {
588 +                return f.thenApply(a);
589 +            }
590 +            public <T,U> CompletableFuture<U> thenCompose
591 +                (CompletableFuture<T> f,
592 +                 Function<? super T,? extends CompletionStage<U>> a) {
593 +                return f.thenCompose(a);
594 +            }
595 +            public <T,U> CompletableFuture<U> handle
596 +                (CompletableFuture<T> f,
597 +                 BiFunction<? super T,Throwable,? extends U> a) {
598 +                return f.handle(a);
599 +            }
600 +            public <T> CompletableFuture<T> whenComplete
601 +                (CompletableFuture<T> f,
602 +                 BiConsumer<? super T,? super Throwable> a) {
603 +                return f.whenComplete(a);
604 +            }
605              public <T,U> CompletableFuture<Void> runAfterBoth
606                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
607                  return f.runAfterBoth(g, a);
# Line 471 | Line 618 | public class CompletableFutureTest exten
618                   BiFunction<? super T,? super U,? extends V> a) {
619                  return f.thenCombine(g, a);
620              }
621 <            public <T,U> CompletableFuture<U> applyToEither
621 >            public <T> CompletableFuture<Void> runAfterEither
622                  (CompletableFuture<T> f,
623 <                 CompletionStage<? extends T> g,
624 <                 Function<? super T,U> a) {
625 <                return f.applyToEither(g, a);
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,
# Line 483 | Line 630 | public class CompletableFutureTest exten
630                   Consumer<? super T> a) {
631                  return f.acceptEither(g, a);
632              }
633 <            public <T> CompletableFuture<Void> runAfterEither
633 >            public <T,U> CompletableFuture<U> applyToEither
634                  (CompletableFuture<T> f,
635 <                 CompletionStage<?> g,
636 <                 java.lang.Runnable a) {
637 <                return f.runAfterEither(g, a);
635 >                 CompletionStage<? extends T> g,
636 >                 Function<? super T,U> a) {
637 >                return f.applyToEither(g, a);
638 >            }
639 >        },
640 >
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.thenCompose(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.whenComplete(a);
677 >                return f.whenCompleteAsync(a);
678              }
502        },
503
504 //             /** Experimental way to do more testing */
505 //         REVERSE_DEFAULT {
506 //             public <T,U> CompletableFuture<Void> runAfterBoth
507 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
508 //                 return g.runAfterBoth(f, a);
509 //             }
510 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
511 //                 (CompletableFuture<T> f,
512 //                  CompletionStage<? extends U> g,
513 //                  BiConsumer<? super T,? super U> a) {
514 //                 return DEFAULT.thenAcceptBoth(f, g, a);
515 //             }
516 //         },
517
518        DEFAULT_ASYNC {
679              public <T,U> CompletableFuture<Void> runAfterBoth
680                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
681                  return f.runAfterBothAsync(g, a);
# Line 532 | Line 692 | public class CompletableFutureTest exten
692                   BiFunction<? super T,? super U,? extends V> a) {
693                  return f.thenCombineAsync(g, a);
694              }
695 <            public <T,U> CompletableFuture<U> applyToEither
695 >            public <T> CompletableFuture<Void> runAfterEither
696                  (CompletableFuture<T> f,
697 <                 CompletionStage<? extends T> g,
698 <                 Function<? super T,U> a) {
699 <                return f.applyToEitherAsync(g, a);
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,
# Line 544 | Line 704 | public class CompletableFutureTest exten
704                   Consumer<? super T> a) {
705                  return f.acceptEitherAsync(g, a);
706              }
707 <            public <T> CompletableFuture<Void> runAfterEither
707 >            public <T,U> CompletableFuture<U> applyToEither
708                  (CompletableFuture<T> f,
709 <                 CompletionStage<?> g,
710 <                 java.lang.Runnable a) {
711 <                return f.runAfterEitherAsync(g, a);
709 >                 CompletionStage<? extends T> g,
710 >                 Function<? super T,U> a) {
711 >                return f.applyToEitherAsync(g, a);
712 >            }
713 >        },
714 >
715 >        EXECUTOR {
716 >            public void checkExecutionMode() {
717 >                assertTrue(ThreadExecutor.startedCurrentThread());
718 >            }
719 >            public CompletableFuture<Void> runAsync(Runnable a) {
720 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
721 >            }
722 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724 >            }
725 >            public <T> CompletableFuture<Void> thenRun
726 >                (CompletableFuture<T> f, Runnable a) {
727 >                return f.thenRunAsync(a, new ThreadExecutor());
728 >            }
729 >            public <T> CompletableFuture<Void> thenAccept
730 >                (CompletableFuture<T> f, Consumer<? super T> a) {
731 >                return f.thenAcceptAsync(a, new ThreadExecutor());
732 >            }
733 >            public <T,U> CompletableFuture<U> thenApply
734 >                (CompletableFuture<T> f, Function<? super T,U> a) {
735 >                return f.thenApplyAsync(a, new ThreadExecutor());
736              }
737              public <T,U> CompletableFuture<U> thenCompose
738                  (CompletableFuture<T> f,
739                   Function<? super T,? extends CompletionStage<U>> a) {
740 <                return f.thenComposeAsync(a);
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);
750 >                return f.whenCompleteAsync(a, new ThreadExecutor());
751              }
563        },
564
565 //         REVERSE_DEFAULT_ASYNC {
566 //             public <T,U> CompletableFuture<Void> runAfterBoth
567 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
568 //                 return f.runAfterBothAsync(g, a);
569 //             }
570 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
571 //                 (CompletableFuture<T> f,
572 //                  CompletionStage<? extends U> g,
573 //                  BiConsumer<? super T,? super U> a) {
574 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
575 //             }
576 //         },
577
578        EXECUTOR {
752              public <T,U> CompletableFuture<Void> runAfterBoth
753                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
754                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 592 | Line 765 | public class CompletableFutureTest exten
765                   BiFunction<? super T,? super U,? extends V> a) {
766                  return f.thenCombineAsync(g, a, new ThreadExecutor());
767              }
595            public <T,U> CompletableFuture<U> applyToEither
596                (CompletableFuture<T> f,
597                 CompletionStage<? extends T> g,
598                 Function<? super T,U> a) {
599                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600            }
601            public <T> CompletableFuture<Void> acceptEither
602                (CompletableFuture<T> f,
603                 CompletionStage<? extends T> g,
604                 Consumer<? super T> a) {
605                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606            }
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,U> CompletableFuture<U> thenCompose
774 >            public <T> CompletableFuture<Void> acceptEither
775                  (CompletableFuture<T> f,
776 <                 Function<? super T,? extends CompletionStage<U>> a) {
777 <                return f.thenComposeAsync(a, new ThreadExecutor());
776 >                 CompletionStage<? extends T> g,
777 >                 Consumer<? super T> a) {
778 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
779              }
780 <            public <T> CompletableFuture<T> whenComplete
780 >            public <T,U> CompletableFuture<U> applyToEither
781                  (CompletableFuture<T> f,
782 <                 BiConsumer<? super T,? super Throwable> a) {
783 <                return f.whenCompleteAsync(a, new ThreadExecutor());
782 >                 CompletionStage<? extends T> g,
783 >                 Function<? super T,U> a) {
784 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
785              }
786          };
787  
788 +        public abstract void checkExecutionMode();
789 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
790 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791 +        public abstract <T> CompletableFuture<Void> thenRun
792 +            (CompletableFuture<T> f, Runnable a);
793 +        public abstract <T> CompletableFuture<Void> thenAccept
794 +            (CompletableFuture<T> f, Consumer<? super T> a);
795 +        public abstract <T,U> CompletableFuture<U> thenApply
796 +            (CompletableFuture<T> f, Function<? super T,U> a);
797 +        public abstract <T,U> CompletableFuture<U> thenCompose
798 +            (CompletableFuture<T> f,
799 +             Function<? super T,? extends CompletionStage<U>> a);
800 +        public abstract <T,U> CompletableFuture<U> handle
801 +            (CompletableFuture<T> f,
802 +             BiFunction<? super T,Throwable,? extends U> a);
803 +        public abstract <T> CompletableFuture<T> whenComplete
804 +            (CompletableFuture<T> f,
805 +             BiConsumer<? super T,? super Throwable> a);
806          public abstract <T,U> CompletableFuture<Void> runAfterBoth
807              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
808          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 632 | Line 813 | public class CompletableFutureTest exten
813              (CompletableFuture<T> f,
814               CompletionStage<? extends U> g,
815               BiFunction<? super T,? super U,? extends V> a);
635        public abstract <T,U> CompletableFuture<U> applyToEither
636            (CompletableFuture<T> f,
637             CompletionStage<? extends T> g,
638             Function<? super T,U> a);
639        public abstract <T> CompletableFuture<Void> acceptEither
640            (CompletableFuture<T> f,
641             CompletionStage<? extends T> g,
642             Consumer<? super T> a);
816          public abstract <T> CompletableFuture<Void> runAfterEither
817              (CompletableFuture<T> f,
818               CompletionStage<?> g,
819               java.lang.Runnable a);
820 <        public abstract <T,U> CompletableFuture<U> thenCompose
820 >        public abstract <T> CompletableFuture<Void> acceptEither
821              (CompletableFuture<T> f,
822 <             Function<? super T,? extends CompletionStage<U>> a);
823 <        public abstract <T> CompletableFuture<T> whenComplete
822 >             CompletionStage<? extends T> g,
823 >             Consumer<? super T> a);
824 >        public abstract <T,U> CompletableFuture<U> applyToEither
825              (CompletableFuture<T> f,
826 <             BiConsumer<? super T,? super Throwable> a);
827 <
654 <
826 >             CompletionStage<? extends T> g,
827 >             Function<? super T,U> a);
828      }
829  
830      /**
831 <     * exceptionally action completes with function value on source
832 <     * exception; otherwise with source value
831 >     * exceptionally action is not invoked when source completes
832 >     * normally, and source result is propagated
833       */
834 <    public void testExceptionally() {
835 <        CompletableFuture<Integer> f = new CompletableFuture<>();
836 <        ExceptionToInteger r = new ExceptionToInteger();
837 <        CompletableFuture<Integer> g = f.exceptionally(r);
838 <        f.completeExceptionally(new CFException());
839 <        checkCompletedNormally(g, three);
834 >    public void testExceptionally_normalCompletion() {
835 >        for (boolean createIncomplete : new boolean[] { true, false })
836 >        for (Integer v1 : new Integer[] { 1, null })
837 >    {
838 >        final AtomicInteger a = new AtomicInteger(0);
839 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
840 >        if (!createIncomplete) f.complete(v1);
841 >        final CompletableFuture<Integer> g = f.exceptionally
842 >            ((Throwable t) -> {
843 >                // Should not be called
844 >                a.getAndIncrement();
845 >                throw new AssertionError();
846 >            });
847 >        if (createIncomplete) f.complete(v1);
848  
849 <        f = new CompletableFuture<>();
850 <        r = new ExceptionToInteger();
851 <        g = f.exceptionally(r);
852 <        f.complete(one);
672 <        checkCompletedNormally(g, one);
673 <    }
849 >        checkCompletedNormally(g, v1);
850 >        checkCompletedNormally(f, v1);
851 >        assertEquals(0, a.get());
852 >    }}
853  
854      /**
855 <     * handle action completes normally with function value on either
856 <     * normal or exceptional completion of source
855 >     * exceptionally action completes with function value on source
856 >     * exception
857       */
858 <    public void testHandle() {
859 <        CompletableFuture<Integer> f, g;
860 <        IntegerHandler r;
861 <
862 <        f = new CompletableFuture<>();
863 <        f.completeExceptionally(new CFException());
864 <        g = f.handle(r = new IntegerHandler());
865 <        assertTrue(r.ran);
866 <        checkCompletedNormally(g, three);
858 >    public void testExceptionally_exceptionalCompletion() {
859 >        for (boolean createIncomplete : new boolean[] { true, false })
860 >        for (Integer v1 : new Integer[] { 1, null })
861 >    {
862 >        final AtomicInteger a = new AtomicInteger(0);
863 >        final CFException ex = new CFException();
864 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
865 >        if (!createIncomplete) f.completeExceptionally(ex);
866 >        final CompletableFuture<Integer> g = f.exceptionally
867 >            ((Throwable t) -> {
868 >                ExecutionMode.DEFAULT.checkExecutionMode();
869 >                threadAssertSame(t, ex);
870 >                a.getAndIncrement();
871 >                return v1;
872 >            });
873 >        if (createIncomplete) f.completeExceptionally(ex);
874  
875 <        f = new CompletableFuture<>();
876 <        g = f.handle(r = new IntegerHandler());
877 <        assertFalse(r.ran);
692 <        f.completeExceptionally(new CFException());
693 <        checkCompletedNormally(g, three);
694 <        assertTrue(r.ran);
875 >        checkCompletedNormally(g, v1);
876 >        assertEquals(1, a.get());
877 >    }}
878  
879 <        f = new CompletableFuture<>();
880 <        f.complete(one);
881 <        g = f.handle(r = new IntegerHandler());
882 <        assertTrue(r.ran);
883 <        checkCompletedNormally(g, two);
879 >    public void testExceptionally_exceptionalCompletionActionFailed() {
880 >        for (boolean createIncomplete : new boolean[] { true, false })
881 >        for (Integer v1 : new Integer[] { 1, null })
882 >    {
883 >        final AtomicInteger a = new AtomicInteger(0);
884 >        final CFException ex1 = new CFException();
885 >        final CFException ex2 = new CFException();
886 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
887 >        if (!createIncomplete) f.completeExceptionally(ex1);
888 >        final CompletableFuture<Integer> g = f.exceptionally
889 >            ((Throwable t) -> {
890 >                ExecutionMode.DEFAULT.checkExecutionMode();
891 >                threadAssertSame(t, ex1);
892 >                a.getAndIncrement();
893 >                throw ex2;
894 >            });
895 >        if (createIncomplete) f.completeExceptionally(ex1);
896  
897 <        f = new CompletableFuture<>();
898 <        g = f.handle(r = new IntegerHandler());
899 <        assertFalse(r.ran);
705 <        f.complete(one);
706 <        assertTrue(r.ran);
707 <        checkCompletedNormally(g, two);
708 <    }
897 >        checkCompletedWithWrappedException(g, ex2);
898 >        assertEquals(1, a.get());
899 >    }}
900  
901      /**
902 <     * runAsync completes after running Runnable
902 >     * whenComplete action executes on normal completion, propagating
903 >     * source result.
904       */
905 <    public void testRunAsync() {
906 <        Noop r = new Noop();
907 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
908 <        assertNull(f.join());
909 <        assertTrue(r.ran);
910 <        checkCompletedNormally(f, null);
911 <    }
905 >    public void testWhenComplete_normalCompletion1() {
906 >        for (ExecutionMode m : ExecutionMode.values())
907 >        for (boolean createIncomplete : new boolean[] { true, false })
908 >        for (Integer v1 : new Integer[] { 1, null })
909 >    {
910 >        final AtomicInteger a = new AtomicInteger(0);
911 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
912 >        if (!createIncomplete) f.complete(v1);
913 >        final CompletableFuture<Integer> g = m.whenComplete
914 >            (f,
915 >             (Integer x, Throwable t) -> {
916 >                m.checkExecutionMode();
917 >                threadAssertSame(x, v1);
918 >                threadAssertNull(t);
919 >                a.getAndIncrement();
920 >            });
921 >        if (createIncomplete) f.complete(v1);
922  
923 <    /**
924 <     * runAsync with executor completes after running Runnable
925 <     */
926 <    public void testRunAsync2() {
725 <        Noop r = new Noop();
726 <        ThreadExecutor exec = new ThreadExecutor();
727 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
728 <        assertNull(f.join());
729 <        assertTrue(r.ran);
730 <        checkCompletedNormally(f, null);
731 <        assertEquals(1, exec.count.get());
732 <    }
923 >        checkCompletedNormally(g, v1);
924 >        checkCompletedNormally(f, v1);
925 >        assertEquals(1, a.get());
926 >    }}
927  
928      /**
929 <     * failing runAsync completes exceptionally after running Runnable
929 >     * whenComplete action executes on exceptional completion, propagating
930 >     * source result.
931       */
932 <    public void testRunAsync3() {
933 <        FailingNoop r = new FailingNoop();
934 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
935 <        checkCompletedWithWrappedCFException(f);
936 <        assertTrue(r.ran);
937 <    }
932 >    public void testWhenComplete_exceptionalCompletion() {
933 >        for (ExecutionMode m : ExecutionMode.values())
934 >        for (boolean createIncomplete : new boolean[] { true, false })
935 >        for (Integer v1 : new Integer[] { 1, null })
936 >    {
937 >        final AtomicInteger a = new AtomicInteger(0);
938 >        final CFException ex = new CFException();
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) f.completeExceptionally(ex);
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertNull(x);
946 >                threadAssertSame(t, ex);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) f.completeExceptionally(ex);
950  
951 <    /**
952 <     * supplyAsync completes with result of supplier
953 <     */
954 <    public void testSupplyAsync() {
748 <        CompletableFuture<Integer> f;
749 <        f = CompletableFuture.supplyAsync(supplyOne);
750 <        assertEquals(f.join(), one);
751 <        checkCompletedNormally(f, one);
752 <    }
951 >        checkCompletedWithWrappedException(g, ex);
952 >        checkCompletedExceptionally(f, ex);
953 >        assertEquals(1, a.get());
954 >    }}
955  
956      /**
957 <     * supplyAsync with executor completes with result of supplier
957 >     * whenComplete action executes on cancelled source, propagating
958 >     * CancellationException.
959       */
960 <    public void testSupplyAsync2() {
961 <        CompletableFuture<Integer> f;
962 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
963 <        assertEquals(f.join(), one);
964 <        checkCompletedNormally(f, one);
965 <    }
960 >    public void testWhenComplete_sourceCancelled() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
963 >        for (boolean createIncomplete : new boolean[] { true, false })
964 >    {
965 >        final AtomicInteger a = new AtomicInteger(0);
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
968 >        final CompletableFuture<Integer> g = m.whenComplete
969 >            (f,
970 >             (Integer x, Throwable t) -> {
971 >                m.checkExecutionMode();
972 >                threadAssertNull(x);
973 >                threadAssertTrue(t instanceof CancellationException);
974 >                a.getAndIncrement();
975 >            });
976 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
977 >
978 >        checkCompletedWithWrappedCancellationException(g);
979 >        checkCancelled(f);
980 >        assertEquals(1, a.get());
981 >    }}
982  
983      /**
984 <     * Failing supplyAsync completes exceptionally
984 >     * If a whenComplete action throws an exception when triggered by
985 >     * a normal completion, it completes exceptionally
986       */
987 <    public void testSupplyAsync3() {
988 <        FailingSupplier r = new FailingSupplier();
989 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
990 <        checkCompletedWithWrappedCFException(f);
991 <        assertTrue(r.ran);
992 <    }
987 >    public void testWhenComplete_actionFailed() {
988 >        for (boolean createIncomplete : new boolean[] { true, false })
989 >        for (ExecutionMode m : ExecutionMode.values())
990 >        for (Integer v1 : new Integer[] { 1, null })
991 >    {
992 >        final AtomicInteger a = new AtomicInteger(0);
993 >        final CFException ex = new CFException();
994 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
995 >        if (!createIncomplete) f.complete(v1);
996 >        final CompletableFuture<Integer> g = m.whenComplete
997 >            (f,
998 >             (Integer x, Throwable t) -> {
999 >                m.checkExecutionMode();
1000 >                threadAssertSame(x, v1);
1001 >                threadAssertNull(t);
1002 >                a.getAndIncrement();
1003 >                throw ex;
1004 >            });
1005 >        if (createIncomplete) f.complete(v1);
1006  
1007 <    // seq completion methods
1007 >        checkCompletedWithWrappedException(g, ex);
1008 >        checkCompletedNormally(f, v1);
1009 >        assertEquals(1, a.get());
1010 >    }}
1011  
1012      /**
1013 <     * thenRun result completes normally after normal completion of source
1013 >     * If a whenComplete action throws an exception when triggered by
1014 >     * a source completion that also throws an exception, the source
1015 >     * exception takes precedence.
1016       */
1017 <    public void testThenRun() {
1018 <        CompletableFuture<Integer> f;
1019 <        CompletableFuture<Void> g;
1020 <        Noop r;
1017 >    public void testWhenComplete_actionFailedSourceFailed() {
1018 >        for (boolean createIncomplete : new boolean[] { true, false })
1019 >        for (ExecutionMode m : ExecutionMode.values())
1020 >        for (Integer v1 : new Integer[] { 1, null })
1021 >    {
1022 >        final AtomicInteger a = new AtomicInteger(0);
1023 >        final CFException ex1 = new CFException();
1024 >        final CFException ex2 = new CFException();
1025 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1026  
1027 <        f = new CompletableFuture<>();
1028 <        g = f.thenRun(r = new Noop());
1029 <        f.complete(null);
1030 <        checkCompletedNormally(g, null);
1031 <        assertTrue(r.ran);
1027 >        if (!createIncomplete) f.completeExceptionally(ex1);
1028 >        final CompletableFuture<Integer> g = m.whenComplete
1029 >            (f,
1030 >             (Integer x, Throwable t) -> {
1031 >                m.checkExecutionMode();
1032 >                threadAssertSame(t, ex1);
1033 >                threadAssertNull(x);
1034 >                a.getAndIncrement();
1035 >                throw ex2;
1036 >            });
1037 >        if (createIncomplete) f.completeExceptionally(ex1);
1038  
1039 <        f = new CompletableFuture<>();
1040 <        f.complete(null);
1041 <        g = f.thenRun(r = new Noop());
1042 <        checkCompletedNormally(g, null);
794 <        assertTrue(r.ran);
795 <    }
1039 >        checkCompletedWithWrappedException(g, ex1);
1040 >        checkCompletedExceptionally(f, ex1);
1041 >        assertEquals(1, a.get());
1042 >    }}
1043  
1044      /**
1045 <     * thenRun result completes exceptionally after exceptional
1045 >     * handle action completes normally with function value on normal
1046       * completion of source
1047       */
1048 <    public void testThenRun2() {
1049 <        CompletableFuture<Integer> f;
1050 <        CompletableFuture<Void> g;
1051 <        Noop r;
1052 <
1053 <        f = new CompletableFuture<>();
1054 <        g = f.thenRun(r = new Noop());
1055 <        f.completeExceptionally(new CFException());
1056 <        checkCompletedWithWrappedCFException(g);
1057 <        assertFalse(r.ran);
1048 >    public void testHandle_normalCompletion() {
1049 >        for (ExecutionMode m : ExecutionMode.values())
1050 >        for (boolean createIncomplete : new boolean[] { true, false })
1051 >        for (Integer v1 : new Integer[] { 1, null })
1052 >    {
1053 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1054 >        final AtomicInteger a = new AtomicInteger(0);
1055 >        if (!createIncomplete) f.complete(v1);
1056 >        final CompletableFuture<Integer> g = m.handle
1057 >            (f,
1058 >             (Integer x, Throwable t) -> {
1059 >                m.checkExecutionMode();
1060 >                threadAssertSame(x, v1);
1061 >                threadAssertNull(t);
1062 >                a.getAndIncrement();
1063 >                return inc(v1);
1064 >            });
1065 >        if (createIncomplete) f.complete(v1);
1066  
1067 <        f = new CompletableFuture<>();
1068 <        f.completeExceptionally(new CFException());
1069 <        g = f.thenRun(r = new Noop());
1070 <        checkCompletedWithWrappedCFException(g);
816 <        assertFalse(r.ran);
817 <    }
1067 >        checkCompletedNormally(g, inc(v1));
1068 >        checkCompletedNormally(f, v1);
1069 >        assertEquals(1, a.get());
1070 >    }}
1071  
1072      /**
1073 <     * thenRun result completes exceptionally if action does
1073 >     * handle action completes normally with function value on
1074 >     * exceptional completion of source
1075       */
1076 <    public void testThenRun3() {
1077 <        CompletableFuture<Integer> f;
1078 <        CompletableFuture<Void> g;
1079 <        FailingNoop r;
1080 <
1081 <        f = new CompletableFuture<>();
1082 <        g = f.thenRun(r = new FailingNoop());
1083 <        f.complete(null);
1084 <        checkCompletedWithWrappedCFException(g);
1076 >    public void testHandle_exceptionalCompletion() {
1077 >        for (ExecutionMode m : ExecutionMode.values())
1078 >        for (boolean createIncomplete : new boolean[] { true, false })
1079 >        for (Integer v1 : new Integer[] { 1, null })
1080 >    {
1081 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1082 >        final AtomicInteger a = new AtomicInteger(0);
1083 >        final CFException ex = new CFException();
1084 >        if (!createIncomplete) f.completeExceptionally(ex);
1085 >        final CompletableFuture<Integer> g = m.handle
1086 >            (f,
1087 >             (Integer x, Throwable t) -> {
1088 >                m.checkExecutionMode();
1089 >                threadAssertNull(x);
1090 >                threadAssertSame(t, ex);
1091 >                a.getAndIncrement();
1092 >                return v1;
1093 >            });
1094 >        if (createIncomplete) f.completeExceptionally(ex);
1095  
1096 <        f = new CompletableFuture<>();
1097 <        f.complete(null);
1098 <        g = f.thenRun(r = new FailingNoop());
1099 <        checkCompletedWithWrappedCFException(g);
836 <    }
1096 >        checkCompletedNormally(g, v1);
1097 >        checkCompletedExceptionally(f, ex);
1098 >        assertEquals(1, a.get());
1099 >    }}
1100  
1101      /**
1102 <     * thenRun result completes exceptionally if source cancelled
1102 >     * handle action completes normally with function value on
1103 >     * cancelled source
1104       */
1105 <    public void testThenRun4() {
1106 <        CompletableFuture<Integer> f;
1107 <        CompletableFuture<Void> g;
1108 <        Noop r;
1109 <
1110 <        f = new CompletableFuture<>();
1111 <        g = f.thenRun(r = new Noop());
1112 <        assertTrue(f.cancel(true));
1113 <        checkCompletedWithWrappedCancellationException(g);
1105 >    public void testHandle_sourceCancelled() {
1106 >        for (ExecutionMode m : ExecutionMode.values())
1107 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1108 >        for (boolean createIncomplete : new boolean[] { true, false })
1109 >        for (Integer v1 : new Integer[] { 1, null })
1110 >    {
1111 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1112 >        final AtomicInteger a = new AtomicInteger(0);
1113 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1114 >        final CompletableFuture<Integer> g = m.handle
1115 >            (f,
1116 >             (Integer x, Throwable t) -> {
1117 >                m.checkExecutionMode();
1118 >                threadAssertNull(x);
1119 >                threadAssertTrue(t instanceof CancellationException);
1120 >                a.getAndIncrement();
1121 >                return v1;
1122 >            });
1123 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1124  
1125 <        f = new CompletableFuture<>();
1126 <        assertTrue(f.cancel(true));
1127 <        g = f.thenRun(r = new Noop());
1128 <        checkCompletedWithWrappedCancellationException(g);
855 <    }
1125 >        checkCompletedNormally(g, v1);
1126 >        checkCancelled(f);
1127 >        assertEquals(1, a.get());
1128 >    }}
1129  
1130      /**
1131 <     * thenApply result completes normally after normal completion of source
1131 >     * handle result completes exceptionally if action does
1132       */
1133 <    public void testThenApply() {
1134 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1135 <        CompletableFuture<Integer> g = f.thenApply(inc);
1136 <        f.complete(one);
1137 <        checkCompletedNormally(g, two);
1138 <    }
1133 >    public void testHandle_sourceFailedActionFailed() {
1134 >        for (ExecutionMode m : ExecutionMode.values())
1135 >        for (boolean createIncomplete : new boolean[] { true, false })
1136 >    {
1137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1138 >        final AtomicInteger a = new AtomicInteger(0);
1139 >        final CFException ex1 = new CFException();
1140 >        final CFException ex2 = new CFException();
1141 >        if (!createIncomplete) f.completeExceptionally(ex1);
1142 >        final CompletableFuture<Integer> g = m.handle
1143 >            (f,
1144 >             (Integer x, Throwable t) -> {
1145 >                m.checkExecutionMode();
1146 >                threadAssertNull(x);
1147 >                threadAssertSame(ex1, t);
1148 >                a.getAndIncrement();
1149 >                throw ex2;
1150 >            });
1151 >        if (createIncomplete) f.completeExceptionally(ex1);
1152  
1153 <    /**
1154 <     * thenApply result completes exceptionally after exceptional
1155 <     * completion of source
1156 <     */
871 <    public void testThenApply2() {
872 <        CompletableFuture<Integer> f = new CompletableFuture<>();
873 <        CompletableFuture<Integer> g = f.thenApply(inc);
874 <        f.completeExceptionally(new CFException());
875 <        checkCompletedWithWrappedCFException(g);
876 <    }
1153 >        checkCompletedWithWrappedException(g, ex2);
1154 >        checkCompletedExceptionally(f, ex1);
1155 >        assertEquals(1, a.get());
1156 >    }}
1157  
1158 <    /**
1159 <     * thenApply result completes exceptionally if action does
1160 <     */
1161 <    public void testThenApply3() {
1162 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1163 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1164 <        f.complete(one);
1165 <        checkCompletedWithWrappedCFException(g);
1166 <    }
1158 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1159 >        for (ExecutionMode m : ExecutionMode.values())
1160 >        for (boolean createIncomplete : new boolean[] { true, false })
1161 >        for (Integer v1 : new Integer[] { 1, null })
1162 >    {
1163 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 >        final AtomicInteger a = new AtomicInteger(0);
1165 >        final CFException ex = new CFException();
1166 >        if (!createIncomplete) f.complete(v1);
1167 >        final CompletableFuture<Integer> g = m.handle
1168 >            (f,
1169 >             (Integer x, Throwable t) -> {
1170 >                m.checkExecutionMode();
1171 >                threadAssertSame(x, v1);
1172 >                threadAssertNull(t);
1173 >                a.getAndIncrement();
1174 >                throw ex;
1175 >            });
1176 >        if (createIncomplete) f.complete(v1);
1177  
1178 <    /**
1179 <     * thenApply result completes exceptionally if source cancelled
1180 <     */
1181 <    public void testThenApply4() {
892 <        CompletableFuture<Integer> f = new CompletableFuture<>();
893 <        CompletableFuture<Integer> g = f.thenApply(inc);
894 <        assertTrue(f.cancel(true));
895 <        checkCompletedWithWrappedCancellationException(g);
896 <    }
1178 >        checkCompletedWithWrappedException(g, ex);
1179 >        checkCompletedNormally(f, v1);
1180 >        assertEquals(1, a.get());
1181 >    }}
1182  
1183      /**
1184 <     * thenAccept result completes normally after normal completion of source
1184 >     * runAsync completes after running Runnable
1185       */
1186 <    public void testThenAccept() {
1187 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1188 <        IncAction r = new IncAction();
1189 <        CompletableFuture<Void> g = f.thenAccept(r);
1190 <        f.complete(one);
1191 <        checkCompletedNormally(g, null);
1192 <        assertEquals(r.value, (Integer) 2);
1193 <    }
1186 >    public void testRunAsync_normalCompletion() {
1187 >        ExecutionMode[] executionModes = {
1188 >            ExecutionMode.ASYNC,
1189 >            ExecutionMode.EXECUTOR,
1190 >        };
1191 >        for (ExecutionMode m : executionModes)
1192 >    {
1193 >        final Noop r = new Noop(m);
1194 >        final CompletableFuture<Void> f = m.runAsync(r);
1195 >        assertNull(f.join());
1196 >        checkCompletedNormally(f, null);
1197 >        r.assertInvoked();
1198 >    }}
1199  
1200      /**
1201 <     * thenAccept result completes exceptionally after exceptional
912 <     * completion of source
1201 >     * failing runAsync completes exceptionally after running Runnable
1202       */
1203 <    public void testThenAccept2() {
1204 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1205 <        IncAction r = new IncAction();
1206 <        CompletableFuture<Void> g = f.thenAccept(r);
1207 <        f.completeExceptionally(new CFException());
1208 <        checkCompletedWithWrappedCFException(g);
1209 <    }
1203 >    public void testRunAsync_exceptionalCompletion() {
1204 >        ExecutionMode[] executionModes = {
1205 >            ExecutionMode.ASYNC,
1206 >            ExecutionMode.EXECUTOR,
1207 >        };
1208 >        for (ExecutionMode m : executionModes)
1209 >    {
1210 >        final FailingRunnable r = new FailingRunnable(m);
1211 >        final CompletableFuture<Void> f = m.runAsync(r);
1212 >        checkCompletedWithWrappedCFException(f);
1213 >        r.assertInvoked();
1214 >    }}
1215  
1216      /**
1217 <     * thenAccept result completes exceptionally if action does
1217 >     * supplyAsync completes with result of supplier
1218       */
1219 <    public void testThenAccept3() {
1220 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1221 <        FailingConsumer r = new FailingConsumer();
1222 <        CompletableFuture<Void> g = f.thenAccept(r);
1223 <        f.complete(one);
1224 <        checkCompletedWithWrappedCFException(g);
1225 <        assertTrue(r.ran);
1226 <    }
1219 >    public void testSupplyAsync_normalCompletion() {
1220 >        ExecutionMode[] executionModes = {
1221 >            ExecutionMode.ASYNC,
1222 >            ExecutionMode.EXECUTOR,
1223 >        };
1224 >        for (ExecutionMode m : executionModes)
1225 >        for (Integer v1 : new Integer[] { 1, null })
1226 >    {
1227 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1228 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1229 >        assertSame(v1, f.join());
1230 >        checkCompletedNormally(f, v1);
1231 >        r.assertInvoked();
1232 >    }}
1233  
1234      /**
1235 <     * thenAccept result completes exceptionally if source cancelled
1235 >     * Failing supplyAsync completes exceptionally
1236       */
1237 <    public void testThenAccept4() {
1238 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1239 <        IncAction r = new IncAction();
1240 <        CompletableFuture<Void> g = f.thenAccept(r);
1241 <        assertTrue(f.cancel(true));
1242 <        checkCompletedWithWrappedCancellationException(g);
1243 <    }
1237 >    public void testSupplyAsync_exceptionalCompletion() {
1238 >        ExecutionMode[] executionModes = {
1239 >            ExecutionMode.ASYNC,
1240 >            ExecutionMode.EXECUTOR,
1241 >        };
1242 >        for (ExecutionMode m : executionModes)
1243 >    {
1244 >        FailingSupplier r = new FailingSupplier(m);
1245 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1246 >        checkCompletedWithWrappedCFException(f);
1247 >        r.assertInvoked();
1248 >    }}
1249 >
1250 >    // seq completion methods
1251  
1252      /**
1253 <     * thenCombine result completes normally after normal completion
947 <     * of sources
1253 >     * thenRun result completes normally after normal completion of source
1254       */
1255 <    public void testThenCombine_normalCompletion1() {
1255 >    public void testThenRun_normalCompletion() {
1256          for (ExecutionMode m : ExecutionMode.values())
1257 +        for (boolean createIncomplete : new boolean[] { true, false })
1258          for (Integer v1 : new Integer[] { 1, null })
1259 <        for (Integer v2 : new Integer[] { 2, null }) {
953 <
1259 >    {
1260          final CompletableFuture<Integer> f = new CompletableFuture<>();
1261 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1262 <        final SubtractFunction r = new SubtractFunction();
1263 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1264 <
1265 <        f.complete(v1);
1266 <        checkIncomplete(h);
1267 <        assertEquals(r.invocationCount, 0);
962 <        g.complete(v2);
1261 >        final Noop r = new Noop(m);
1262 >        if (!createIncomplete) f.complete(v1);
1263 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1264 >        if (createIncomplete) {
1265 >            checkIncomplete(g);
1266 >            f.complete(v1);
1267 >        }
1268  
1269 <        checkCompletedNormally(h, subtract(v1, v2));
1269 >        checkCompletedNormally(g, null);
1270          checkCompletedNormally(f, v1);
1271 <        checkCompletedNormally(g, v2);
1272 <        assertEquals(r.invocationCount, 1);
968 <        }
969 <    }
1271 >        r.assertInvoked();
1272 >    }}
1273  
1274 <    public void testThenCombine_normalCompletion2() {
1274 >    /**
1275 >     * thenRun result completes exceptionally after exceptional
1276 >     * completion of source
1277 >     */
1278 >    public void testThenRun_exceptionalCompletion() {
1279          for (ExecutionMode m : ExecutionMode.values())
1280 <        for (Integer v1 : new Integer[] { 1, null })
1281 <        for (Integer v2 : new Integer[] { 2, null }) {
1282 <
1280 >        for (boolean createIncomplete : new boolean[] { true, false })
1281 >    {
1282 >        final CFException ex = new CFException();
1283          final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1285 <        final SubtractFunction r = new SubtractFunction();
1286 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1287 <
1288 <        g.complete(v2);
1289 <        checkIncomplete(h);
983 <        assertEquals(r.invocationCount, 0);
984 <        f.complete(v1);
985 <
986 <        checkCompletedNormally(h, subtract(v1, v2));
987 <        checkCompletedNormally(f, v1);
988 <        checkCompletedNormally(g, v2);
989 <        assertEquals(r.invocationCount, 1);
1284 >        final Noop r = new Noop(m);
1285 >        if (!createIncomplete) f.completeExceptionally(ex);
1286 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1287 >        if (createIncomplete) {
1288 >            checkIncomplete(g);
1289 >            f.completeExceptionally(ex);
1290          }
991    }
1291  
1292 <    public void testThenCombine_normalCompletion3() {
1293 <        for (ExecutionMode m : ExecutionMode.values())
1294 <        for (Integer v1 : new Integer[] { 1, null })
1295 <        for (Integer v2 : new Integer[] { 2, null }) {
1292 >        checkCompletedWithWrappedException(g, ex);
1293 >        checkCompletedExceptionally(f, ex);
1294 >        r.assertNotInvoked();
1295 >    }}
1296  
1297 +    /**
1298 +     * thenRun result completes exceptionally if source cancelled
1299 +     */
1300 +    public void testThenRun_sourceCancelled() {
1301 +        for (ExecutionMode m : ExecutionMode.values())
1302 +        for (boolean createIncomplete : new boolean[] { true, false })
1303 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1304 +    {
1305          final CompletableFuture<Integer> f = new CompletableFuture<>();
1306 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1307 <        final SubtractFunction r = new SubtractFunction();
1308 <
1309 <        g.complete(v2);
1310 <        f.complete(v1);
1311 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1005 <
1006 <        checkCompletedNormally(h, subtract(v1, v2));
1007 <        checkCompletedNormally(f, v1);
1008 <        checkCompletedNormally(g, v2);
1009 <        assertEquals(r.invocationCount, 1);
1306 >        final Noop r = new Noop(m);
1307 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1309 >        if (createIncomplete) {
1310 >            checkIncomplete(g);
1311 >            assertTrue(f.cancel(mayInterruptIfRunning));
1312          }
1011    }
1313  
1314 <    public void testThenCombine_normalCompletion4() {
1314 >        checkCompletedWithWrappedCancellationException(g);
1315 >        checkCancelled(f);
1316 >        r.assertNotInvoked();
1317 >    }}
1318 >
1319 >    /**
1320 >     * thenRun result completes exceptionally if action does
1321 >     */
1322 >    public void testThenRun_actionFailed() {
1323          for (ExecutionMode m : ExecutionMode.values())
1324 +        for (boolean createIncomplete : new boolean[] { true, false })
1325          for (Integer v1 : new Integer[] { 1, null })
1326 <        for (Integer v2 : new Integer[] { 2, null }) {
1017 <
1326 >    {
1327          final CompletableFuture<Integer> f = new CompletableFuture<>();
1328 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1329 <        final SubtractFunction r = new SubtractFunction();
1330 <
1331 <        f.complete(v1);
1332 <        g.complete(v2);
1333 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1328 >        final FailingRunnable r = new FailingRunnable(m);
1329 >        if (!createIncomplete) f.complete(v1);
1330 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1331 >        if (createIncomplete) {
1332 >            checkIncomplete(g);
1333 >            f.complete(v1);
1334 >        }
1335  
1336 <        checkCompletedNormally(h, subtract(v1, v2));
1336 >        checkCompletedWithWrappedCFException(g);
1337          checkCompletedNormally(f, v1);
1338 <        checkCompletedNormally(g, v2);
1029 <        assertEquals(r.invocationCount, 1);
1030 <        }
1031 <    }
1338 >    }}
1339  
1340      /**
1341 <     * thenCombine result completes exceptionally after exceptional
1035 <     * completion of either source
1341 >     * thenApply result completes normally after normal completion of source
1342       */
1343 <    public void testThenCombine_exceptionalCompletion1() {
1343 >    public void testThenApply_normalCompletion() {
1344          for (ExecutionMode m : ExecutionMode.values())
1345 <        for (Integer v1 : new Integer[] { 1, null }) {
1346 <
1345 >        for (boolean createIncomplete : new boolean[] { true, false })
1346 >        for (Integer v1 : new Integer[] { 1, null })
1347 >    {
1348          final CompletableFuture<Integer> f = new CompletableFuture<>();
1349 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1350 <        final SubtractFunction r = new SubtractFunction();
1351 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1352 <        final CFException ex = new CFException();
1353 <
1354 <        f.completeExceptionally(ex);
1048 <        checkIncomplete(h);
1049 <        g.complete(v1);
1050 <
1051 <        checkCompletedWithWrappedCFException(h, ex);
1052 <        checkCompletedWithWrappedCFException(f, ex);
1053 <        assertEquals(r.invocationCount, 0);
1054 <        checkCompletedNormally(g, v1);
1349 >        final IncFunction r = new IncFunction(m);
1350 >        if (!createIncomplete) f.complete(v1);
1351 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1352 >        if (createIncomplete) {
1353 >            checkIncomplete(g);
1354 >            f.complete(v1);
1355          }
1056    }
1057
1058    public void testThenCombine_exceptionalCompletion2() {
1059        for (ExecutionMode m : ExecutionMode.values())
1060        for (Integer v1 : new Integer[] { 1, null }) {
1061
1062        final CompletableFuture<Integer> f = new CompletableFuture<>();
1063        final CompletableFuture<Integer> g = new CompletableFuture<>();
1064        final SubtractFunction r = new SubtractFunction();
1065        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1066        final CFException ex = new CFException();
1356  
1357 <        g.completeExceptionally(ex);
1069 <        checkIncomplete(h);
1070 <        f.complete(v1);
1071 <
1072 <        checkCompletedWithWrappedCFException(h, ex);
1073 <        checkCompletedWithWrappedCFException(g, ex);
1074 <        assertEquals(r.invocationCount, 0);
1357 >        checkCompletedNormally(g, inc(v1));
1358          checkCompletedNormally(f, v1);
1359 <        }
1360 <    }
1359 >        r.assertValue(inc(v1));
1360 >    }}
1361  
1362 <    public void testThenCombine_exceptionalCompletion3() {
1362 >    /**
1363 >     * thenApply result completes exceptionally after exceptional
1364 >     * completion of source
1365 >     */
1366 >    public void testThenApply_exceptionalCompletion() {
1367          for (ExecutionMode m : ExecutionMode.values())
1368 <        for (Integer v1 : new Integer[] { 1, null }) {
1369 <
1083 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1085 <        final SubtractFunction r = new SubtractFunction();
1368 >        for (boolean createIncomplete : new boolean[] { true, false })
1369 >    {
1370          final CFException ex = new CFException();
1371 <
1372 <        g.completeExceptionally(ex);
1373 <        f.complete(v1);
1374 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1375 <
1376 <        checkCompletedWithWrappedCFException(h, ex);
1377 <        checkCompletedWithWrappedCFException(g, ex);
1094 <        assertEquals(r.invocationCount, 0);
1095 <        checkCompletedNormally(f, v1);
1371 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1372 >        final IncFunction r = new IncFunction(m);
1373 >        if (!createIncomplete) f.completeExceptionally(ex);
1374 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1375 >        if (createIncomplete) {
1376 >            checkIncomplete(g);
1377 >            f.completeExceptionally(ex);
1378          }
1097    }
1379  
1380 <    public void testThenCombine_exceptionalCompletion4() {
1381 <        for (ExecutionMode m : ExecutionMode.values())
1382 <        for (Integer v1 : new Integer[] { 1, null }) {
1380 >        checkCompletedWithWrappedException(g, ex);
1381 >        checkCompletedExceptionally(f, ex);
1382 >        r.assertNotInvoked();
1383 >    }}
1384  
1385 +    /**
1386 +     * thenApply result completes exceptionally if source cancelled
1387 +     */
1388 +    public void testThenApply_sourceCancelled() {
1389 +        for (ExecutionMode m : ExecutionMode.values())
1390 +        for (boolean createIncomplete : new boolean[] { true, false })
1391 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1392 +    {
1393          final CompletableFuture<Integer> f = new CompletableFuture<>();
1394 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1395 <        final SubtractFunction r = new SubtractFunction();
1396 <        final CFException ex = new CFException();
1397 <
1398 <        f.completeExceptionally(ex);
1399 <        g.complete(v1);
1110 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1111 <
1112 <        checkCompletedWithWrappedCFException(h, ex);
1113 <        checkCompletedWithWrappedCFException(f, ex);
1114 <        assertEquals(r.invocationCount, 0);
1115 <        checkCompletedNormally(g, v1);
1394 >        final IncFunction r = new IncFunction(m);
1395 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1397 >        if (createIncomplete) {
1398 >            checkIncomplete(g);
1399 >            assertTrue(f.cancel(mayInterruptIfRunning));
1400          }
1401 <    }
1401 >
1402 >        checkCompletedWithWrappedCancellationException(g);
1403 >        checkCancelled(f);
1404 >        r.assertNotInvoked();
1405 >    }}
1406  
1407      /**
1408 <     * thenCombine result completes exceptionally if action does
1408 >     * thenApply result completes exceptionally if action does
1409       */
1410 <    public void testThenCombine_actionFailed1() {
1410 >    public void testThenApply_actionFailed() {
1411          for (ExecutionMode m : ExecutionMode.values())
1412 +        for (boolean createIncomplete : new boolean[] { true, false })
1413          for (Integer v1 : new Integer[] { 1, null })
1414 <        for (Integer v2 : new Integer[] { 2, null }) {
1126 <
1414 >    {
1415          final CompletableFuture<Integer> f = new CompletableFuture<>();
1416 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1417 <        final FailingBiFunction r = new FailingBiFunction();
1418 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1419 <
1420 <        f.complete(v1);
1421 <        checkIncomplete(h);
1422 <        g.complete(v2);
1416 >        final FailingFunction r = new FailingFunction(m);
1417 >        if (!createIncomplete) f.complete(v1);
1418 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1419 >        if (createIncomplete) {
1420 >            checkIncomplete(g);
1421 >            f.complete(v1);
1422 >        }
1423  
1424 <        checkCompletedWithWrappedCFException(h);
1424 >        checkCompletedWithWrappedCFException(g);
1425          checkCompletedNormally(f, v1);
1426 <        checkCompletedNormally(g, v2);
1139 <        }
1140 <    }
1426 >    }}
1427  
1428 <    public void testThenCombine_actionFailed2() {
1428 >    /**
1429 >     * thenAccept result completes normally after normal completion of source
1430 >     */
1431 >    public void testThenAccept_normalCompletion() {
1432          for (ExecutionMode m : ExecutionMode.values())
1433 +        for (boolean createIncomplete : new boolean[] { true, false })
1434          for (Integer v1 : new Integer[] { 1, null })
1435 <        for (Integer v2 : new Integer[] { 2, null }) {
1146 <
1435 >    {
1436          final CompletableFuture<Integer> f = new CompletableFuture<>();
1437 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1438 <        final FailingBiFunction r = new FailingBiFunction();
1439 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1440 <
1441 <        g.complete(v2);
1442 <        checkIncomplete(h);
1443 <        f.complete(v1);
1437 >        final NoopConsumer r = new NoopConsumer(m);
1438 >        if (!createIncomplete) f.complete(v1);
1439 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1440 >        if (createIncomplete) {
1441 >            checkIncomplete(g);
1442 >            f.complete(v1);
1443 >        }
1444  
1445 <        checkCompletedWithWrappedCFException(h);
1445 >        checkCompletedNormally(g, null);
1446 >        r.assertValue(v1);
1447          checkCompletedNormally(f, v1);
1448 <        checkCompletedNormally(g, v2);
1159 <        }
1160 <    }
1448 >    }}
1449  
1450      /**
1451 <     * thenCombine result completes exceptionally if either source cancelled
1451 >     * thenAccept result completes exceptionally after exceptional
1452 >     * completion of source
1453       */
1454 <    public void testThenCombine_sourceCancelled1() {
1454 >    public void testThenAccept_exceptionalCompletion() {
1455          for (ExecutionMode m : ExecutionMode.values())
1456 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1457 <        for (Integer v1 : new Integer[] { 1, null }) {
1458 <
1456 >        for (boolean createIncomplete : new boolean[] { true, false })
1457 >    {
1458 >        final CFException ex = new CFException();
1459          final CompletableFuture<Integer> f = new CompletableFuture<>();
1460 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1461 <        final SubtractFunction r = new SubtractFunction();
1462 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1463 <
1464 <        assertTrue(f.cancel(mayInterruptIfRunning));
1465 <        checkIncomplete(h);
1177 <        g.complete(v1);
1178 <
1179 <        checkCompletedWithWrappedCancellationException(h);
1180 <        checkCancelled(f);
1181 <        assertEquals(r.invocationCount, 0);
1182 <        checkCompletedNormally(g, v1);
1460 >        final NoopConsumer r = new NoopConsumer(m);
1461 >        if (!createIncomplete) f.completeExceptionally(ex);
1462 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1463 >        if (createIncomplete) {
1464 >            checkIncomplete(g);
1465 >            f.completeExceptionally(ex);
1466          }
1184    }
1185
1186    public void testThenCombine_sourceCancelled2() {
1187        for (ExecutionMode m : ExecutionMode.values())
1188        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1189        for (Integer v1 : new Integer[] { 1, null }) {
1467  
1468 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1469 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1470 <        final SubtractFunction r = new SubtractFunction();
1471 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1468 >        checkCompletedWithWrappedException(g, ex);
1469 >        checkCompletedExceptionally(f, ex);
1470 >        r.assertNotInvoked();
1471 >    }}
1472  
1473 <        assertTrue(g.cancel(mayInterruptIfRunning));
1474 <        checkIncomplete(h);
1475 <        f.complete(v1);
1476 <
1200 <        checkCompletedWithWrappedCancellationException(h);
1201 <        checkCancelled(g);
1202 <        assertEquals(r.invocationCount, 0);
1203 <        checkCompletedNormally(f, v1);
1204 <        }
1205 <    }
1206 <
1207 <    public void testThenCombine_sourceCancelled3() {
1473 >    /**
1474 >     * thenAccept result completes exceptionally if source cancelled
1475 >     */
1476 >    public void testThenAccept_sourceCancelled() {
1477          for (ExecutionMode m : ExecutionMode.values())
1478 +        for (boolean createIncomplete : new boolean[] { true, false })
1479          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1480 <        for (Integer v1 : new Integer[] { 1, null }) {
1211 <
1480 >    {
1481          final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1483 <        final SubtractFunction r = new SubtractFunction();
1484 <
1485 <        assertTrue(g.cancel(mayInterruptIfRunning));
1486 <        f.complete(v1);
1487 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1219 <
1220 <        checkCompletedWithWrappedCancellationException(h);
1221 <        checkCancelled(g);
1222 <        assertEquals(r.invocationCount, 0);
1223 <        checkCompletedNormally(f, v1);
1482 >        final NoopConsumer r = new NoopConsumer(m);
1483 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1484 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1485 >        if (createIncomplete) {
1486 >            checkIncomplete(g);
1487 >            assertTrue(f.cancel(mayInterruptIfRunning));
1488          }
1225    }
1226
1227    public void testThenCombine_sourceCancelled4() {
1228        for (ExecutionMode m : ExecutionMode.values())
1229        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1230        for (Integer v1 : new Integer[] { 1, null }) {
1489  
1490 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1233 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1234 <        final SubtractFunction r = new SubtractFunction();
1235 <
1236 <        assertTrue(f.cancel(mayInterruptIfRunning));
1237 <        g.complete(v1);
1238 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1239 <
1240 <        checkCompletedWithWrappedCancellationException(h);
1490 >        checkCompletedWithWrappedCancellationException(g);
1491          checkCancelled(f);
1492 <        assertEquals(r.invocationCount, 0);
1493 <        checkCompletedNormally(g, v1);
1244 <        }
1245 <    }
1492 >        r.assertNotInvoked();
1493 >    }}
1494  
1495      /**
1496 <     * thenAcceptBoth result completes normally after normal
1249 <     * completion of sources
1496 >     * thenAccept result completes exceptionally if action does
1497       */
1498 <    public void testThenAcceptBoth_normalCompletion1() {
1498 >    public void testThenAccept_actionFailed() {
1499          for (ExecutionMode m : ExecutionMode.values())
1500 +        for (boolean createIncomplete : new boolean[] { true, false })
1501          for (Integer v1 : new Integer[] { 1, null })
1502 <        for (Integer v2 : new Integer[] { 2, null }) {
1255 <
1502 >    {
1503          final CompletableFuture<Integer> f = new CompletableFuture<>();
1504 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1505 <        final SubtractAction r = new SubtractAction();
1506 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1507 <
1508 <        f.complete(v1);
1509 <        checkIncomplete(h);
1263 <        assertFalse(r.ran());
1264 <        g.complete(v2);
1265 <
1266 <        checkCompletedNormally(h, null);
1267 <        assertEquals(r.value, subtract(v1, v2));
1268 <        checkCompletedNormally(f, v1);
1269 <        checkCompletedNormally(g, v2);
1504 >        final FailingConsumer r = new FailingConsumer(m);
1505 >        if (!createIncomplete) f.complete(v1);
1506 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1507 >        if (createIncomplete) {
1508 >            checkIncomplete(g);
1509 >            f.complete(v1);
1510          }
1271    }
1272
1273    public void testThenAcceptBoth_normalCompletion2() {
1274        for (ExecutionMode m : ExecutionMode.values())
1275        for (Integer v1 : new Integer[] { 1, null })
1276        for (Integer v2 : new Integer[] { 2, null }) {
1511  
1512 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1279 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1280 <        final SubtractAction r = new SubtractAction();
1281 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1282 <
1283 <        g.complete(v2);
1284 <        checkIncomplete(h);
1285 <        assertFalse(r.ran());
1286 <        f.complete(v1);
1287 <
1288 <        checkCompletedNormally(h, null);
1289 <        assertEquals(r.value, subtract(v1, v2));
1512 >        checkCompletedWithWrappedCFException(g);
1513          checkCompletedNormally(f, v1);
1514 <        checkCompletedNormally(g, v2);
1292 <        }
1293 <    }
1514 >    }}
1515  
1516 <    public void testThenAcceptBoth_normalCompletion3() {
1516 >    /**
1517 >     * thenCombine result completes normally after normal completion
1518 >     * of sources
1519 >     */
1520 >    public void testThenCombine_normalCompletion() {
1521          for (ExecutionMode m : ExecutionMode.values())
1522 +        for (boolean createIncomplete : new boolean[] { true, false })
1523 +        for (boolean fFirst : new boolean[] { true, false })
1524          for (Integer v1 : new Integer[] { 1, null })
1525 <        for (Integer v2 : new Integer[] { 2, null }) {
1526 <
1525 >        for (Integer v2 : new Integer[] { 2, null })
1526 >    {
1527          final CompletableFuture<Integer> f = new CompletableFuture<>();
1528          final CompletableFuture<Integer> g = new CompletableFuture<>();
1529 <        final SubtractAction r = new SubtractAction();
1303 <
1304 <        g.complete(v2);
1305 <        f.complete(v1);
1306 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1529 >        final SubtractFunction r = new SubtractFunction(m);
1530  
1531 <        checkCompletedNormally(h, null);
1532 <        assertEquals(r.value, subtract(v1, v2));
1533 <        checkCompletedNormally(f, v1);
1534 <        checkCompletedNormally(g, v2);
1531 >        if (fFirst) f.complete(v1); else g.complete(v2);
1532 >        if (!createIncomplete)
1533 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1534 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1535 >        if (createIncomplete) {
1536 >            checkIncomplete(h);
1537 >            r.assertNotInvoked();
1538 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1539          }
1313    }
1540  
1541 <    public void testThenAcceptBoth_normalCompletion4() {
1316 <        for (ExecutionMode m : ExecutionMode.values())
1317 <        for (Integer v1 : new Integer[] { 1, null })
1318 <        for (Integer v2 : new Integer[] { 2, null }) {
1319 <
1320 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 <        final SubtractAction r = new SubtractAction();
1323 <
1324 <        f.complete(v1);
1325 <        g.complete(v2);
1326 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1327 <
1328 <        checkCompletedNormally(h, null);
1329 <        assertEquals(r.value, subtract(v1, v2));
1541 >        checkCompletedNormally(h, subtract(v1, v2));
1542          checkCompletedNormally(f, v1);
1543          checkCompletedNormally(g, v2);
1544 <        }
1545 <    }
1544 >        r.assertValue(subtract(v1, v2));
1545 >    }}
1546  
1547      /**
1548 <     * thenAcceptBoth result completes exceptionally after exceptional
1548 >     * thenCombine result completes exceptionally after exceptional
1549       * completion of either source
1550       */
1551 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1551 >    public void testThenCombine_exceptionalCompletion() {
1552          for (ExecutionMode m : ExecutionMode.values())
1553 <        for (Integer v1 : new Integer[] { 1, null }) {
1554 <
1553 >        for (boolean createIncomplete : new boolean[] { true, false })
1554 >        for (boolean fFirst : new boolean[] { true, false })
1555 >        for (Integer v1 : new Integer[] { 1, null })
1556 >    {
1557          final CompletableFuture<Integer> f = new CompletableFuture<>();
1558          final CompletableFuture<Integer> g = new CompletableFuture<>();
1345        final SubtractAction r = new SubtractAction();
1346        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1559          final CFException ex = new CFException();
1560 +        final SubtractFunction r = new SubtractFunction(m);
1561  
1562 <        f.completeExceptionally(ex);
1563 <        checkIncomplete(h);
1564 <        g.complete(v1);
1565 <
1566 <        checkCompletedWithWrappedCFException(h, ex);
1567 <        checkCompletedWithWrappedCFException(f, ex);
1568 <        assertEquals(r.invocationCount, 0);
1356 <        checkCompletedNormally(g, v1);
1562 >        (fFirst ? f : g).complete(v1);
1563 >        if (!createIncomplete)
1564 >            (!fFirst ? f : g).completeExceptionally(ex);
1565 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1566 >        if (createIncomplete) {
1567 >            checkIncomplete(h);
1568 >            (!fFirst ? f : g).completeExceptionally(ex);
1569          }
1358    }
1359
1360    public void testThenAcceptBoth_exceptionalCompletion2() {
1361        for (ExecutionMode m : ExecutionMode.values())
1362        for (Integer v1 : new Integer[] { 1, null }) {
1570  
1571 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1572 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1573 <        final SubtractAction r = new SubtractAction();
1574 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1575 <        final CFException ex = new CFException();
1571 >        checkCompletedWithWrappedException(h, ex);
1572 >        r.assertNotInvoked();
1573 >        checkCompletedNormally(fFirst ? f : g, v1);
1574 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1575 >    }}
1576  
1577 <        g.completeExceptionally(ex);
1578 <        checkIncomplete(h);
1579 <        f.complete(v1);
1580 <
1374 <        checkCompletedWithWrappedCFException(h, ex);
1375 <        checkCompletedWithWrappedCFException(g, ex);
1376 <        assertEquals(r.invocationCount, 0);
1377 <        checkCompletedNormally(f, v1);
1378 <        }
1379 <    }
1380 <
1381 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1577 >    /**
1578 >     * thenCombine result completes exceptionally if either source cancelled
1579 >     */
1580 >    public void testThenCombine_sourceCancelled() {
1581          for (ExecutionMode m : ExecutionMode.values())
1582 <        for (Integer v1 : new Integer[] { 1, null }) {
1583 <
1582 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583 >        for (boolean createIncomplete : new boolean[] { true, false })
1584 >        for (boolean fFirst : new boolean[] { true, false })
1585 >        for (Integer v1 : new Integer[] { 1, null })
1586 >    {
1587          final CompletableFuture<Integer> f = new CompletableFuture<>();
1588          final CompletableFuture<Integer> g = new CompletableFuture<>();
1589 <        final SubtractAction r = new SubtractAction();
1388 <        final CFException ex = new CFException();
1389 <
1390 <        g.completeExceptionally(ex);
1391 <        f.complete(v1);
1392 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1589 >        final SubtractFunction r = new SubtractFunction(m);
1590  
1591 <        checkCompletedWithWrappedCFException(h, ex);
1592 <        checkCompletedWithWrappedCFException(g, ex);
1593 <        assertEquals(r.invocationCount, 0);
1594 <        checkCompletedNormally(f, v1);
1591 >        (fFirst ? f : g).complete(v1);
1592 >        if (!createIncomplete)
1593 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1594 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1595 >        if (createIncomplete) {
1596 >            checkIncomplete(h);
1597 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1598          }
1399    }
1400
1401    public void testThenAcceptBoth_exceptionalCompletion4() {
1402        for (ExecutionMode m : ExecutionMode.values())
1403        for (Integer v1 : new Integer[] { 1, null }) {
1404
1405        final CompletableFuture<Integer> f = new CompletableFuture<>();
1406        final CompletableFuture<Integer> g = new CompletableFuture<>();
1407        final SubtractAction r = new SubtractAction();
1408        final CFException ex = new CFException();
1409
1410        f.completeExceptionally(ex);
1411        g.complete(v1);
1412        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1599  
1600 <        checkCompletedWithWrappedCFException(h, ex);
1601 <        checkCompletedWithWrappedCFException(f, ex);
1602 <        assertFalse(r.ran());
1603 <        checkCompletedNormally(g, v1);
1604 <        }
1419 <    }
1600 >        checkCompletedWithWrappedCancellationException(h);
1601 >        checkCancelled(!fFirst ? f : g);
1602 >        r.assertNotInvoked();
1603 >        checkCompletedNormally(fFirst ? f : g, v1);
1604 >    }}
1605  
1606      /**
1607 <     * thenAcceptBoth result completes exceptionally if action does
1607 >     * thenCombine result completes exceptionally if action does
1608       */
1609 <    public void testThenAcceptBoth_actionFailed1() {
1609 >    public void testThenCombine_actionFailed() {
1610          for (ExecutionMode m : ExecutionMode.values())
1611 +        for (boolean fFirst : new boolean[] { true, false })
1612          for (Integer v1 : new Integer[] { 1, null })
1613 <        for (Integer v2 : new Integer[] { 2, null }) {
1614 <
1613 >        for (Integer v2 : new Integer[] { 2, null })
1614 >    {
1615          final CompletableFuture<Integer> f = new CompletableFuture<>();
1616          final CompletableFuture<Integer> g = new CompletableFuture<>();
1617 <        final FailingBiConsumer r = new FailingBiConsumer();
1618 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1617 >        final FailingBiFunction r = new FailingBiFunction(m);
1618 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1619  
1620 <        f.complete(v1);
1621 <        checkIncomplete(h);
1622 <        g.complete(v2);
1620 >        if (fFirst) {
1621 >            f.complete(v1);
1622 >            g.complete(v2);
1623 >        } else {
1624 >            g.complete(v2);
1625 >            f.complete(v1);
1626 >        }
1627  
1628          checkCompletedWithWrappedCFException(h);
1629          checkCompletedNormally(f, v1);
1630          checkCompletedNormally(g, v2);
1631 <        }
1442 <    }
1631 >    }}
1632  
1633 <    public void testThenAcceptBoth_actionFailed2() {
1633 >    /**
1634 >     * thenAcceptBoth result completes normally after normal
1635 >     * completion of sources
1636 >     */
1637 >    public void testThenAcceptBoth_normalCompletion() {
1638          for (ExecutionMode m : ExecutionMode.values())
1639 +        for (boolean createIncomplete : new boolean[] { true, false })
1640 +        for (boolean fFirst : new boolean[] { true, false })
1641          for (Integer v1 : new Integer[] { 1, null })
1642 <        for (Integer v2 : new Integer[] { 2, null }) {
1643 <
1642 >        for (Integer v2 : new Integer[] { 2, null })
1643 >    {
1644          final CompletableFuture<Integer> f = new CompletableFuture<>();
1645          final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 <        final FailingBiConsumer r = new FailingBiConsumer();
1452 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1646 >        final SubtractAction r = new SubtractAction(m);
1647  
1648 <        g.complete(v2);
1649 <        checkIncomplete(h);
1650 <        f.complete(v1);
1648 >        if (fFirst) f.complete(v1); else g.complete(v2);
1649 >        if (!createIncomplete)
1650 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1651 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1652 >        if (createIncomplete) {
1653 >            checkIncomplete(h);
1654 >            r.assertNotInvoked();
1655 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1656 >        }
1657  
1658 <        checkCompletedWithWrappedCFException(h);
1658 >        checkCompletedNormally(h, null);
1659 >        r.assertValue(subtract(v1, v2));
1660          checkCompletedNormally(f, v1);
1661          checkCompletedNormally(g, v2);
1662 <        }
1462 <    }
1662 >    }}
1663  
1664      /**
1665 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1665 >     * thenAcceptBoth result completes exceptionally after exceptional
1666 >     * completion of either source
1667       */
1668 <    public void testThenAcceptBoth_sourceCancelled1() {
1668 >    public void testThenAcceptBoth_exceptionalCompletion() {
1669          for (ExecutionMode m : ExecutionMode.values())
1670 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1671 <        for (Integer v1 : new Integer[] { 1, null }) {
1672 <
1670 >        for (boolean createIncomplete : new boolean[] { true, false })
1671 >        for (boolean fFirst : new boolean[] { true, false })
1672 >        for (Integer v1 : new Integer[] { 1, null })
1673 >    {
1674          final CompletableFuture<Integer> f = new CompletableFuture<>();
1675          final CompletableFuture<Integer> g = new CompletableFuture<>();
1676 <        final SubtractAction r = new SubtractAction();
1677 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1476 <
1477 <        assertTrue(f.cancel(mayInterruptIfRunning));
1478 <        checkIncomplete(h);
1479 <        g.complete(v1);
1480 <
1481 <        checkCompletedWithWrappedCancellationException(h);
1482 <        checkCancelled(f);
1483 <        assertEquals(r.invocationCount, 0);
1484 <        checkCompletedNormally(g, v1);
1485 <        }
1486 <    }
1487 <
1488 <    public void testThenAcceptBoth_sourceCancelled2() {
1489 <        for (ExecutionMode m : ExecutionMode.values())
1490 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1491 <        for (Integer v1 : new Integer[] { 1, null }) {
1676 >        final CFException ex = new CFException();
1677 >        final SubtractAction r = new SubtractAction(m);
1678  
1679 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1680 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1681 <        final SubtractAction r = new SubtractAction();
1679 >        (fFirst ? f : g).complete(v1);
1680 >        if (!createIncomplete)
1681 >            (!fFirst ? f : g).completeExceptionally(ex);
1682          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1683 <
1684 <        assertTrue(g.cancel(mayInterruptIfRunning));
1685 <        checkIncomplete(h);
1500 <        f.complete(v1);
1501 <
1502 <        checkCompletedWithWrappedCancellationException(h);
1503 <        checkCancelled(g);
1504 <        assertEquals(r.invocationCount, 0);
1505 <        checkCompletedNormally(f, v1);
1683 >        if (createIncomplete) {
1684 >            checkIncomplete(h);
1685 >            (!fFirst ? f : g).completeExceptionally(ex);
1686          }
1507    }
1508
1509    public void testThenAcceptBoth_sourceCancelled3() {
1510        for (ExecutionMode m : ExecutionMode.values())
1511        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1512        for (Integer v1 : new Integer[] { 1, null }) {
1513
1514        final CompletableFuture<Integer> f = new CompletableFuture<>();
1515        final CompletableFuture<Integer> g = new CompletableFuture<>();
1516        final SubtractAction r = new SubtractAction();
1517
1518        assertTrue(g.cancel(mayInterruptIfRunning));
1519        f.complete(v1);
1520        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1687  
1688 <        checkCompletedWithWrappedCancellationException(h);
1689 <        checkCancelled(g);
1690 <        assertEquals(r.invocationCount, 0);
1691 <        checkCompletedNormally(f, v1);
1692 <        }
1527 <    }
1688 >        checkCompletedWithWrappedException(h, ex);
1689 >        r.assertNotInvoked();
1690 >        checkCompletedNormally(fFirst ? f : g, v1);
1691 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1692 >    }}
1693  
1694 <    public void testThenAcceptBoth_sourceCancelled4() {
1694 >    /**
1695 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1696 >     */
1697 >    public void testThenAcceptBoth_sourceCancelled() {
1698          for (ExecutionMode m : ExecutionMode.values())
1699          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700 <        for (Integer v1 : new Integer[] { 1, null }) {
1701 <
1700 >        for (boolean createIncomplete : new boolean[] { true, false })
1701 >        for (boolean fFirst : new boolean[] { true, false })
1702 >        for (Integer v1 : new Integer[] { 1, null })
1703 >    {
1704          final CompletableFuture<Integer> f = new CompletableFuture<>();
1705          final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 <        final SubtractAction r = new SubtractAction();
1706 >        final SubtractAction r = new SubtractAction(m);
1707  
1708 <        assertTrue(f.cancel(mayInterruptIfRunning));
1709 <        g.complete(v1);
1708 >        (fFirst ? f : g).complete(v1);
1709 >        if (!createIncomplete)
1710 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1711          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1712 +        if (createIncomplete) {
1713 +            checkIncomplete(h);
1714 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1715 +        }
1716  
1717          checkCompletedWithWrappedCancellationException(h);
1718 <        checkCancelled(f);
1719 <        assertEquals(r.invocationCount, 0);
1720 <        checkCompletedNormally(g, v1);
1721 <        }
1547 <    }
1718 >        checkCancelled(!fFirst ? f : g);
1719 >        r.assertNotInvoked();
1720 >        checkCompletedNormally(fFirst ? f : g, v1);
1721 >    }}
1722  
1723      /**
1724 <     * runAfterBoth result completes normally after normal
1551 <     * completion of sources
1724 >     * thenAcceptBoth result completes exceptionally if action does
1725       */
1726 <    public void testRunAfterBoth_normalCompletion1() {
1726 >    public void testThenAcceptBoth_actionFailed() {
1727          for (ExecutionMode m : ExecutionMode.values())
1728 +        for (boolean fFirst : new boolean[] { true, false })
1729          for (Integer v1 : new Integer[] { 1, null })
1730 <        for (Integer v2 : new Integer[] { 2, null }) {
1731 <
1730 >        for (Integer v2 : new Integer[] { 2, null })
1731 >    {
1732          final CompletableFuture<Integer> f = new CompletableFuture<>();
1733          final CompletableFuture<Integer> g = new CompletableFuture<>();
1734 <        final Noop r = new Noop();
1735 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1562 <
1563 <        f.complete(v1);
1564 <        checkIncomplete(h);
1565 <        assertFalse(r.ran);
1566 <        g.complete(v2);
1734 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1735 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1736  
1737 <        checkCompletedNormally(h, null);
1738 <        assertEquals(r.invocationCount, 1);
1739 <        checkCompletedNormally(f, v1);
1740 <        checkCompletedNormally(g, v2);
1737 >        if (fFirst) {
1738 >            f.complete(v1);
1739 >            g.complete(v2);
1740 >        } else {
1741 >            g.complete(v2);
1742 >            f.complete(v1);
1743          }
1573    }
1574
1575    public void testRunAfterBoth_normalCompletion2() {
1576        for (ExecutionMode m : ExecutionMode.values())
1577        for (Integer v1 : new Integer[] { 1, null })
1578        for (Integer v2 : new Integer[] { 2, null }) {
1744  
1745 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1581 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1582 <        final Noop r = new Noop();
1583 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1584 <
1585 <        g.complete(v2);
1586 <        checkIncomplete(h);
1587 <        assertFalse(r.ran);
1588 <        f.complete(v1);
1589 <
1590 <        checkCompletedNormally(h, null);
1591 <        assertEquals(r.invocationCount, 1);
1745 >        checkCompletedWithWrappedCFException(h);
1746          checkCompletedNormally(f, v1);
1747          checkCompletedNormally(g, v2);
1748 <        }
1595 <    }
1748 >    }}
1749  
1750 <    public void testRunAfterBoth_normalCompletion3() {
1750 >    /**
1751 >     * runAfterBoth result completes normally after normal
1752 >     * completion of sources
1753 >     */
1754 >    public void testRunAfterBoth_normalCompletion() {
1755          for (ExecutionMode m : ExecutionMode.values())
1756 +        for (boolean createIncomplete : new boolean[] { true, false })
1757 +        for (boolean fFirst : new boolean[] { true, false })
1758          for (Integer v1 : new Integer[] { 1, null })
1759 <        for (Integer v2 : new Integer[] { 2, null }) {
1760 <
1759 >        for (Integer v2 : new Integer[] { 2, null })
1760 >    {
1761          final CompletableFuture<Integer> f = new CompletableFuture<>();
1762          final CompletableFuture<Integer> g = new CompletableFuture<>();
1763 <        final Noop r = new Noop();
1763 >        final Noop r = new Noop(m);
1764  
1765 <        g.complete(v2);
1766 <        f.complete(v1);
1765 >        if (fFirst) f.complete(v1); else g.complete(v2);
1766 >        if (!createIncomplete)
1767 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1768          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1769 <
1770 <        checkCompletedNormally(h, null);
1771 <        assertTrue(r.ran);
1772 <        checkCompletedNormally(f, v1);
1613 <        checkCompletedNormally(g, v2);
1769 >        if (createIncomplete) {
1770 >            checkIncomplete(h);
1771 >            r.assertNotInvoked();
1772 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1773          }
1615    }
1616
1617    public void testRunAfterBoth_normalCompletion4() {
1618        for (ExecutionMode m : ExecutionMode.values())
1619        for (Integer v1 : new Integer[] { 1, null })
1620        for (Integer v2 : new Integer[] { 2, null }) {
1621
1622        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623        final CompletableFuture<Integer> g = new CompletableFuture<>();
1624        final Noop r = new Noop();
1625
1626        f.complete(v1);
1627        g.complete(v2);
1628        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1774  
1775          checkCompletedNormally(h, null);
1776 <        assertEquals(r.invocationCount, 1);
1776 >        r.assertInvoked();
1777          checkCompletedNormally(f, v1);
1778          checkCompletedNormally(g, v2);
1779 <        }
1635 <    }
1779 >    }}
1780  
1781      /**
1782       * runAfterBoth result completes exceptionally after exceptional
1783       * completion of either source
1784       */
1785 <    public void testRunAfterBoth_exceptionalCompletion1() {
1642 <        for (ExecutionMode m : ExecutionMode.values())
1643 <        for (Integer v1 : new Integer[] { 1, null }) {
1644 <
1645 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1646 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1647 <        final Noop r = new Noop();
1648 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1649 <        final CFException ex = new CFException();
1650 <
1651 <        f.completeExceptionally(ex);
1652 <        checkIncomplete(h);
1653 <        g.complete(v1);
1654 <
1655 <        checkCompletedWithWrappedCFException(h, ex);
1656 <        checkCompletedWithWrappedCFException(f, ex);
1657 <        assertEquals(r.invocationCount, 0);
1658 <        checkCompletedNormally(g, v1);
1659 <        }
1660 <    }
1661 <
1662 <    public void testRunAfterBoth_exceptionalCompletion2() {
1663 <        for (ExecutionMode m : ExecutionMode.values())
1664 <        for (Integer v1 : new Integer[] { 1, null }) {
1665 <
1666 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 <        final CFException ex = new CFException();
1671 <
1672 <        g.completeExceptionally(ex);
1673 <        checkIncomplete(h);
1674 <        f.complete(v1);
1675 <
1676 <        checkCompletedWithWrappedCFException(h, ex);
1677 <        checkCompletedWithWrappedCFException(g, ex);
1678 <        assertEquals(r.invocationCount, 0);
1679 <        checkCompletedNormally(f, v1);
1680 <        }
1681 <    }
1682 <
1683 <    public void testRunAfterBoth_exceptionalCompletion3() {
1785 >    public void testRunAfterBoth_exceptionalCompletion() {
1786          for (ExecutionMode m : ExecutionMode.values())
1787 <        for (Integer v1 : new Integer[] { 1, null }) {
1788 <
1787 >        for (boolean createIncomplete : new boolean[] { true, false })
1788 >        for (boolean fFirst : new boolean[] { true, false })
1789 >        for (Integer v1 : new Integer[] { 1, null })
1790 >    {
1791          final CompletableFuture<Integer> f = new CompletableFuture<>();
1792          final CompletableFuture<Integer> g = new CompletableFuture<>();
1689        final Noop r = new Noop();
1793          final CFException ex = new CFException();
1794 +        final Noop r = new Noop(m);
1795  
1796 <        g.completeExceptionally(ex);
1797 <        f.complete(v1);
1796 >        (fFirst ? f : g).complete(v1);
1797 >        if (!createIncomplete)
1798 >            (!fFirst ? f : g).completeExceptionally(ex);
1799          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1800 <
1801 <        checkCompletedWithWrappedCFException(h, ex);
1802 <        checkCompletedWithWrappedCFException(g, ex);
1698 <        assertEquals(r.invocationCount, 0);
1699 <        checkCompletedNormally(f, v1);
1800 >        if (createIncomplete) {
1801 >            checkIncomplete(h);
1802 >            (!fFirst ? f : g).completeExceptionally(ex);
1803          }
1701    }
1702
1703    public void testRunAfterBoth_exceptionalCompletion4() {
1704        for (ExecutionMode m : ExecutionMode.values())
1705        for (Integer v1 : new Integer[] { 1, null }) {
1804  
1805 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1806 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1807 <        final Noop r = new Noop();
1808 <        final CFException ex = new CFException();
1809 <
1712 <        f.completeExceptionally(ex);
1713 <        g.complete(v1);
1714 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715 <
1716 <        checkCompletedWithWrappedCFException(h, ex);
1717 <        checkCompletedWithWrappedCFException(f, ex);
1718 <        assertEquals(r.invocationCount, 0);
1719 <        checkCompletedNormally(g, v1);
1720 <        }
1721 <    }
1805 >        checkCompletedWithWrappedException(h, ex);
1806 >        r.assertNotInvoked();
1807 >        checkCompletedNormally(fFirst ? f : g, v1);
1808 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1809 >    }}
1810  
1811      /**
1812 <     * runAfterBoth result completes exceptionally if action does
1812 >     * runAfterBoth result completes exceptionally if either source cancelled
1813       */
1814 <    public void testRunAfterBoth_actionFailed1() {
1814 >    public void testRunAfterBoth_sourceCancelled() {
1815          for (ExecutionMode m : ExecutionMode.values())
1816 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817 +        for (boolean createIncomplete : new boolean[] { true, false })
1818 +        for (boolean fFirst : new boolean[] { true, false })
1819          for (Integer v1 : new Integer[] { 1, null })
1820 <        for (Integer v2 : new Integer[] { 2, null }) {
1730 <
1820 >    {
1821          final CompletableFuture<Integer> f = new CompletableFuture<>();
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823 <        final FailingNoop r = new FailingNoop();
1734 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1735 <
1736 <        f.complete(v1);
1737 <        checkIncomplete(h);
1738 <        g.complete(v2);
1739 <
1740 <        checkCompletedWithWrappedCFException(h);
1741 <        checkCompletedNormally(f, v1);
1742 <        checkCompletedNormally(g, v2);
1743 <        }
1744 <    }
1745 <
1746 <    public void testRunAfterBoth_actionFailed2() {
1747 <        for (ExecutionMode m : ExecutionMode.values())
1748 <        for (Integer v1 : new Integer[] { 1, null })
1749 <        for (Integer v2 : new Integer[] { 2, null }) {
1823 >        final Noop r = new Noop(m);
1824  
1825 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1826 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1827 <        final FailingNoop r = new FailingNoop();
1825 >        (fFirst ? f : g).complete(v1);
1826 >        if (!createIncomplete)
1827 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1828          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1829 <
1830 <        g.complete(v2);
1831 <        checkIncomplete(h);
1758 <        f.complete(v1);
1759 <
1760 <        checkCompletedWithWrappedCFException(h);
1761 <        checkCompletedNormally(f, v1);
1762 <        checkCompletedNormally(g, v2);
1829 >        if (createIncomplete) {
1830 >            checkIncomplete(h);
1831 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1832          }
1764    }
1765
1766    /**
1767     * runAfterBoth result completes exceptionally if either source cancelled
1768     */
1769    public void testRunAfterBoth_sourceCancelled1() {
1770        for (ExecutionMode m : ExecutionMode.values())
1771        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1772        for (Integer v1 : new Integer[] { 1, null }) {
1773
1774        final CompletableFuture<Integer> f = new CompletableFuture<>();
1775        final CompletableFuture<Integer> g = new CompletableFuture<>();
1776        final Noop r = new Noop();
1777        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1778
1779        assertTrue(f.cancel(mayInterruptIfRunning));
1780        checkIncomplete(h);
1781        g.complete(v1);
1833  
1834          checkCompletedWithWrappedCancellationException(h);
1835 <        checkCancelled(f);
1836 <        assertEquals(r.invocationCount, 0);
1837 <        checkCompletedNormally(g, v1);
1838 <        }
1788 <    }
1835 >        checkCancelled(!fFirst ? f : g);
1836 >        r.assertNotInvoked();
1837 >        checkCompletedNormally(fFirst ? f : g, v1);
1838 >    }}
1839  
1840 <    public void testRunAfterBoth_sourceCancelled2() {
1840 >    /**
1841 >     * runAfterBoth result completes exceptionally if action does
1842 >     */
1843 >    public void testRunAfterBoth_actionFailed() {
1844          for (ExecutionMode m : ExecutionMode.values())
1845 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1846 <        for (Integer v1 : new Integer[] { 1, null }) {
1847 <
1845 >        for (boolean fFirst : new boolean[] { true, false })
1846 >        for (Integer v1 : new Integer[] { 1, null })
1847 >        for (Integer v2 : new Integer[] { 2, null })
1848 >    {
1849          final CompletableFuture<Integer> f = new CompletableFuture<>();
1850          final CompletableFuture<Integer> g = new CompletableFuture<>();
1851 <        final Noop r = new Noop();
1852 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1799 <
1800 <        assertTrue(g.cancel(mayInterruptIfRunning));
1801 <        checkIncomplete(h);
1802 <        f.complete(v1);
1851 >        final FailingRunnable r1 = new FailingRunnable(m);
1852 >        final FailingRunnable r2 = new FailingRunnable(m);
1853  
1854 <        checkCompletedWithWrappedCancellationException(h);
1855 <        checkCancelled(g);
1856 <        assertEquals(r.invocationCount, 0);
1857 <        checkCompletedNormally(f, v1);
1854 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1855 >        if (fFirst) {
1856 >            f.complete(v1);
1857 >            g.complete(v2);
1858 >        } else {
1859 >            g.complete(v2);
1860 >            f.complete(v1);
1861          }
1862 <    }
1810 <
1811 <    public void testRunAfterBoth_sourceCancelled3() {
1812 <        for (ExecutionMode m : ExecutionMode.values())
1813 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1814 <        for (Integer v1 : new Integer[] { 1, null }) {
1862 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1863  
1864 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1865 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 <        final Noop r = new Noop();
1819 <
1820 <        assertTrue(g.cancel(mayInterruptIfRunning));
1821 <        f.complete(v1);
1822 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1823 <
1824 <        checkCompletedWithWrappedCancellationException(h);
1825 <        checkCancelled(g);
1826 <        assertEquals(r.invocationCount, 0);
1864 >        checkCompletedWithWrappedCFException(h1);
1865 >        checkCompletedWithWrappedCFException(h2);
1866          checkCompletedNormally(f, v1);
1867 <        }
1868 <    }
1830 <
1831 <    public void testRunAfterBoth_sourceCancelled4() {
1832 <        for (ExecutionMode m : ExecutionMode.values())
1833 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1834 <        for (Integer v1 : new Integer[] { 1, null }) {
1835 <
1836 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1837 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1838 <        final Noop r = new Noop();
1839 <
1840 <        assertTrue(f.cancel(mayInterruptIfRunning));
1841 <        g.complete(v1);
1842 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843 <
1844 <        checkCompletedWithWrappedCancellationException(h);
1845 <        checkCancelled(f);
1846 <        assertEquals(r.invocationCount, 0);
1847 <        checkCompletedNormally(g, v1);
1848 <        }
1849 <    }
1867 >        checkCompletedNormally(g, v2);
1868 >    }}
1869  
1870      /**
1871       * applyToEither result completes normally after normal completion
1872       * of either source
1873       */
1874 <    public void testApplyToEither_normalCompletion1() {
1874 >    public void testApplyToEither_normalCompletion() {
1875          for (ExecutionMode m : ExecutionMode.values())
1876          for (Integer v1 : new Integer[] { 1, null })
1877 <        for (Integer v2 : new Integer[] { 2, null }) {
1878 <
1877 >        for (Integer v2 : new Integer[] { 2, null })
1878 >    {
1879          final CompletableFuture<Integer> f = new CompletableFuture<>();
1880          final CompletableFuture<Integer> g = new CompletableFuture<>();
1881 <        final IncFunction r = new IncFunction();
1882 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1881 >        final IncFunction[] rs = new IncFunction[6];
1882 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1883  
1884 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1885 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1886 +        checkIncomplete(h0);
1887 +        checkIncomplete(h1);
1888 +        rs[0].assertNotInvoked();
1889 +        rs[1].assertNotInvoked();
1890          f.complete(v1);
1891 <        checkCompletedNormally(h, inc(v1));
1891 >        checkCompletedNormally(h0, inc(v1));
1892 >        checkCompletedNormally(h1, inc(v1));
1893 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1894 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1895 >        checkCompletedNormally(h2, inc(v1));
1896 >        checkCompletedNormally(h3, inc(v1));
1897          g.complete(v2);
1898  
1899 <        checkCompletedNormally(f, v1);
1900 <        checkCompletedNormally(g, v2);
1901 <        checkCompletedNormally(h, inc(v1));
1902 <        }
1903 <    }
1904 <
1905 <    public void testApplyToEither_normalCompletion2() {
1906 <        for (ExecutionMode m : ExecutionMode.values())
1907 <        for (Integer v1 : new Integer[] { 1, null })
1878 <        for (Integer v2 : new Integer[] { 2, null }) {
1879 <
1880 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1881 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1882 <        final IncFunction r = new IncFunction();
1883 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1884 <
1885 <        g.complete(v2);
1886 <        checkCompletedNormally(h, inc(v2));
1887 <        f.complete(v1);
1899 >        // unspecified behavior - both source completions available
1900 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1901 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1902 >        rs[4].assertValue(h4.join());
1903 >        rs[5].assertValue(h5.join());
1904 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1905 >                   Objects.equals(inc(v2), h4.join()));
1906 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1907 >                   Objects.equals(inc(v2), h5.join()));
1908  
1909          checkCompletedNormally(f, v1);
1910          checkCompletedNormally(g, v2);
1911 <        checkCompletedNormally(h, inc(v2));
1912 <        }
1913 <    }
1914 <    public void testApplyToEither_normalCompletion3() {
1915 <        for (ExecutionMode m : ExecutionMode.values())
1916 <        for (Integer v1 : new Integer[] { 1, null })
1897 <        for (Integer v2 : new Integer[] { 2, null }) {
1898 <
1899 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1900 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1901 <        final IncFunction r = new IncFunction();
1902 <
1903 <        f.complete(v1);
1904 <        g.complete(v2);
1905 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1906 <
1907 <        checkCompletedNormally(f, v1);
1908 <        checkCompletedNormally(g, v2);
1909 <
1910 <        // unspecified behavior
1911 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1912 <                   Objects.equals(h.join(), inc(v2)));
1913 <        assertEquals(r.invocationCount, 1);
1914 <        }
1915 <    }
1911 >        checkCompletedNormally(h0, inc(v1));
1912 >        checkCompletedNormally(h1, inc(v1));
1913 >        checkCompletedNormally(h2, inc(v1));
1914 >        checkCompletedNormally(h3, inc(v1));
1915 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1916 >    }}
1917  
1918      /**
1919       * applyToEither result completes exceptionally after exceptional
1920       * completion of either source
1921       */
1922 <    public void testApplyToEither_exceptionalCompletion1() {
1922 >    public void testApplyToEither_exceptionalCompletion() {
1923          for (ExecutionMode m : ExecutionMode.values())
1924 <        for (Integer v1 : new Integer[] { 1, null }) {
1925 <
1924 >        for (Integer v1 : new Integer[] { 1, null })
1925 >    {
1926          final CompletableFuture<Integer> f = new CompletableFuture<>();
1927          final CompletableFuture<Integer> g = new CompletableFuture<>();
1927        final IncFunction r = new IncFunction();
1928        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1928          final CFException ex = new CFException();
1929 +        final IncFunction[] rs = new IncFunction[6];
1930 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1931  
1932 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1933 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1934 +        checkIncomplete(h0);
1935 +        checkIncomplete(h1);
1936 +        rs[0].assertNotInvoked();
1937 +        rs[1].assertNotInvoked();
1938          f.completeExceptionally(ex);
1939 <        checkCompletedWithWrappedCFException(h, ex);
1939 >        checkCompletedWithWrappedException(h0, ex);
1940 >        checkCompletedWithWrappedException(h1, ex);
1941 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1942 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1943 >        checkCompletedWithWrappedException(h2, ex);
1944 >        checkCompletedWithWrappedException(h3, ex);
1945          g.complete(v1);
1946  
1947 <        assertEquals(r.invocationCount, 0);
1948 <        checkCompletedNormally(g, v1);
1949 <        checkCompletedWithWrappedCFException(f, ex);
1950 <        checkCompletedWithWrappedCFException(h, ex);
1947 >        // unspecified behavior - both source completions available
1948 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1949 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1950 >        try {
1951 >            assertEquals(inc(v1), h4.join());
1952 >            rs[4].assertValue(inc(v1));
1953 >        } catch (CompletionException ok) {
1954 >            checkCompletedWithWrappedException(h4, ex);
1955 >            rs[4].assertNotInvoked();
1956          }
1957 <    }
1957 >        try {
1958 >            assertEquals(inc(v1), h5.join());
1959 >            rs[5].assertValue(inc(v1));
1960 >        } catch (CompletionException ok) {
1961 >            checkCompletedWithWrappedException(h5, ex);
1962 >            rs[5].assertNotInvoked();
1963 >        }
1964 >
1965 >        checkCompletedExceptionally(f, ex);
1966 >        checkCompletedNormally(g, v1);
1967 >        checkCompletedWithWrappedException(h0, ex);
1968 >        checkCompletedWithWrappedException(h1, ex);
1969 >        checkCompletedWithWrappedException(h2, ex);
1970 >        checkCompletedWithWrappedException(h3, ex);
1971 >        checkCompletedWithWrappedException(h4, ex);
1972 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1973 >    }}
1974  
1975      public void testApplyToEither_exceptionalCompletion2() {
1976          for (ExecutionMode m : ExecutionMode.values())
1977 <        for (Integer v1 : new Integer[] { 1, null }) {
1978 <
1977 >        for (boolean fFirst : new boolean[] { true, false })
1978 >        for (Integer v1 : new Integer[] { 1, null })
1979 >    {
1980          final CompletableFuture<Integer> f = new CompletableFuture<>();
1981          final CompletableFuture<Integer> g = new CompletableFuture<>();
1948        final IncFunction r = new IncFunction();
1949        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1982          final CFException ex = new CFException();
1983 +        final IncFunction[] rs = new IncFunction[6];
1984 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1985  
1986 <        g.completeExceptionally(ex);
1987 <        checkCompletedWithWrappedCFException(h, ex);
1988 <        f.complete(v1);
1989 <
1990 <        assertEquals(r.invocationCount, 0);
1991 <        checkCompletedNormally(f, v1);
1992 <        checkCompletedWithWrappedCFException(g, ex);
1993 <        checkCompletedWithWrappedCFException(h, ex);
1986 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1987 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1988 >        if (fFirst) {
1989 >            f.complete(v1);
1990 >            g.completeExceptionally(ex);
1991 >        } else {
1992 >            g.completeExceptionally(ex);
1993 >            f.complete(v1);
1994          }
1995 <    }
1996 <
1963 <    public void testApplyToEither_exceptionalCompletion3() {
1964 <        for (ExecutionMode m : ExecutionMode.values())
1965 <        for (Integer v1 : new Integer[] { 1, null }) {
1966 <
1967 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1968 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1969 <        final IncFunction r = new IncFunction();
1970 <        final CFException ex = new CFException();
1995 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1996 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1997  
1998 <        g.completeExceptionally(ex);
1973 <        f.complete(v1);
1974 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1975 <
1976 <        // unspecified behavior
1977 <        Integer v;
1998 >        // unspecified behavior - both source completions available
1999          try {
2000 <            assertEquals(h.join(), inc(v1));
2001 <            assertEquals(r.invocationCount, 1);
2000 >            assertEquals(inc(v1), h0.join());
2001 >            rs[0].assertValue(inc(v1));
2002          } catch (CompletionException ok) {
2003 <            checkCompletedWithWrappedCFException(h, ex);
2004 <            assertEquals(r.invocationCount, 0);
2003 >            checkCompletedWithWrappedException(h0, ex);
2004 >            rs[0].assertNotInvoked();
2005          }
2006 <
2007 <        checkCompletedWithWrappedCFException(g, ex);
2008 <        checkCompletedNormally(f, v1);
2006 >        try {
2007 >            assertEquals(inc(v1), h1.join());
2008 >            rs[1].assertValue(inc(v1));
2009 >        } catch (CompletionException ok) {
2010 >            checkCompletedWithWrappedException(h1, ex);
2011 >            rs[1].assertNotInvoked();
2012          }
1989    }
1990
1991    public void testApplyToEither_exceptionalCompletion4() {
1992        for (ExecutionMode m : ExecutionMode.values())
1993        for (Integer v1 : new Integer[] { 1, null }) {
1994
1995        final CompletableFuture<Integer> f = new CompletableFuture<>();
1996        final CompletableFuture<Integer> g = new CompletableFuture<>();
1997        final IncFunction r = new IncFunction();
1998        final CFException ex = new CFException();
1999
2000        f.completeExceptionally(ex);
2001        g.complete(v1);
2002        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2003
2004        // unspecified behavior
2005        Integer v;
2013          try {
2014 <            assertEquals(h.join(), inc(v1));
2015 <            assertEquals(r.invocationCount, 1);
2014 >            assertEquals(inc(v1), h2.join());
2015 >            rs[2].assertValue(inc(v1));
2016          } catch (CompletionException ok) {
2017 <            checkCompletedWithWrappedCFException(h, ex);
2018 <            assertEquals(r.invocationCount, 0);
2017 >            checkCompletedWithWrappedException(h2, ex);
2018 >            rs[2].assertNotInvoked();
2019          }
2020 <
2021 <        checkCompletedWithWrappedCFException(f, ex);
2022 <        checkCompletedNormally(g, v1);
2020 >        try {
2021 >            assertEquals(inc(v1), h3.join());
2022 >            rs[3].assertValue(inc(v1));
2023 >        } catch (CompletionException ok) {
2024 >            checkCompletedWithWrappedException(h3, ex);
2025 >            rs[3].assertNotInvoked();
2026          }
2017    }
2018
2019    /**
2020     * applyToEither result completes exceptionally if action does
2021     */
2022    public void testApplyToEither_actionFailed1() {
2023        for (ExecutionMode m : ExecutionMode.values())
2024        for (Integer v1 : new Integer[] { 1, null })
2025        for (Integer v2 : new Integer[] { 2, null }) {
2027  
2027        final CompletableFuture<Integer> f = new CompletableFuture<>();
2028        final CompletableFuture<Integer> g = new CompletableFuture<>();
2029        final FailingFunction r = new FailingFunction();
2030        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2031
2032        f.complete(v1);
2033        checkCompletedWithWrappedCFException(h);
2034        g.complete(v2);
2028          checkCompletedNormally(f, v1);
2029 <        checkCompletedNormally(g, v2);
2030 <        }
2038 <    }
2039 <
2040 <    public void testApplyToEither_actionFailed2() {
2041 <        for (ExecutionMode m : ExecutionMode.values())
2042 <        for (Integer v1 : new Integer[] { 1, null })
2043 <        for (Integer v2 : new Integer[] { 2, null }) {
2044 <
2045 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2046 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 <        final FailingFunction r = new FailingFunction();
2048 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2049 <
2050 <        g.complete(v2);
2051 <        checkCompletedWithWrappedCFException(h);
2052 <        f.complete(v1);
2053 <        checkCompletedNormally(f, v1);
2054 <        checkCompletedNormally(g, v2);
2055 <        }
2056 <    }
2029 >        checkCompletedExceptionally(g, ex);
2030 >    }}
2031  
2032      /**
2033       * applyToEither result completes exceptionally if either source cancelled
2034       */
2035 <    public void testApplyToEither_sourceCancelled1() {
2035 >    public void testApplyToEither_sourceCancelled() {
2036          for (ExecutionMode m : ExecutionMode.values())
2037          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2038 <        for (Integer v1 : new Integer[] { 1, null }) {
2039 <
2038 >        for (Integer v1 : new Integer[] { 1, null })
2039 >    {
2040          final CompletableFuture<Integer> f = new CompletableFuture<>();
2041          final CompletableFuture<Integer> g = new CompletableFuture<>();
2042 <        final IncFunction r = new IncFunction();
2043 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2042 >        final IncFunction[] rs = new IncFunction[6];
2043 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2044  
2045 <        assertTrue(f.cancel(mayInterruptIfRunning));
2046 <        checkCompletedWithWrappedCancellationException(h);
2045 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2046 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2047 >        checkIncomplete(h0);
2048 >        checkIncomplete(h1);
2049 >        rs[0].assertNotInvoked();
2050 >        rs[1].assertNotInvoked();
2051 >        f.cancel(mayInterruptIfRunning);
2052 >        checkCompletedWithWrappedCancellationException(h0);
2053 >        checkCompletedWithWrappedCancellationException(h1);
2054 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2055 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2056 >        checkCompletedWithWrappedCancellationException(h2);
2057 >        checkCompletedWithWrappedCancellationException(h3);
2058          g.complete(v1);
2059  
2060 +        // unspecified behavior - both source completions available
2061 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2062 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2063 +        try {
2064 +            assertEquals(inc(v1), h4.join());
2065 +            rs[4].assertValue(inc(v1));
2066 +        } catch (CompletionException ok) {
2067 +            checkCompletedWithWrappedCancellationException(h4);
2068 +            rs[4].assertNotInvoked();
2069 +        }
2070 +        try {
2071 +            assertEquals(inc(v1), h5.join());
2072 +            rs[5].assertValue(inc(v1));
2073 +        } catch (CompletionException ok) {
2074 +            checkCompletedWithWrappedCancellationException(h5);
2075 +            rs[5].assertNotInvoked();
2076 +        }
2077 +
2078          checkCancelled(f);
2076        assertEquals(r.invocationCount, 0);
2079          checkCompletedNormally(g, v1);
2080 <        checkCompletedWithWrappedCancellationException(h);
2081 <        }
2082 <    }
2080 >        checkCompletedWithWrappedCancellationException(h0);
2081 >        checkCompletedWithWrappedCancellationException(h1);
2082 >        checkCompletedWithWrappedCancellationException(h2);
2083 >        checkCompletedWithWrappedCancellationException(h3);
2084 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2085 >    }}
2086  
2087      public void testApplyToEither_sourceCancelled2() {
2088          for (ExecutionMode m : ExecutionMode.values())
2089          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2090 <        for (Integer v1 : new Integer[] { 1, null }) {
2091 <
2090 >        for (boolean fFirst : new boolean[] { true, false })
2091 >        for (Integer v1 : new Integer[] { 1, null })
2092 >    {
2093          final CompletableFuture<Integer> f = new CompletableFuture<>();
2094          final CompletableFuture<Integer> g = new CompletableFuture<>();
2095 <        final IncFunction r = new IncFunction();
2096 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 <
2092 <        assertTrue(g.cancel(mayInterruptIfRunning));
2093 <        checkCompletedWithWrappedCancellationException(h);
2094 <        f.complete(v1);
2095 >        final IncFunction[] rs = new IncFunction[6];
2096 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2097  
2098 <        checkCancelled(g);
2099 <        assertEquals(r.invocationCount, 0);
2100 <        checkCompletedNormally(f, v1);
2101 <        checkCompletedWithWrappedCancellationException(h);
2098 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2099 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2100 >        if (fFirst) {
2101 >            f.complete(v1);
2102 >            g.cancel(mayInterruptIfRunning);
2103 >        } else {
2104 >            g.cancel(mayInterruptIfRunning);
2105 >            f.complete(v1);
2106          }
2107 <    }
2108 <
2103 <    public void testApplyToEither_sourceCancelled3() {
2104 <        for (ExecutionMode m : ExecutionMode.values())
2105 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2106 <        for (Integer v1 : new Integer[] { 1, null }) {
2107 <
2108 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2109 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2110 <        final IncFunction r = new IncFunction();
2111 <
2112 <        assertTrue(g.cancel(mayInterruptIfRunning));
2113 <        f.complete(v1);
2114 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2107 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2108 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2109  
2110 <        // unspecified behavior
2117 <        Integer v;
2110 >        // unspecified behavior - both source completions available
2111          try {
2112 <            assertEquals(h.join(), inc(v1));
2113 <            assertEquals(r.invocationCount, 1);
2112 >            assertEquals(inc(v1), h0.join());
2113 >            rs[0].assertValue(inc(v1));
2114          } catch (CompletionException ok) {
2115 <            checkCompletedWithWrappedCancellationException(h);
2116 <            assertEquals(r.invocationCount, 0);
2115 >            checkCompletedWithWrappedCancellationException(h0);
2116 >            rs[0].assertNotInvoked();
2117          }
2118 <
2119 <        checkCancelled(g);
2120 <        checkCompletedNormally(f, v1);
2118 >        try {
2119 >            assertEquals(inc(v1), h1.join());
2120 >            rs[1].assertValue(inc(v1));
2121 >        } catch (CompletionException ok) {
2122 >            checkCompletedWithWrappedCancellationException(h1);
2123 >            rs[1].assertNotInvoked();
2124          }
2129    }
2130
2131    public void testApplyToEither_sourceCancelled4() {
2132        for (ExecutionMode m : ExecutionMode.values())
2133        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2134        for (Integer v1 : new Integer[] { 1, null }) {
2135
2136        final CompletableFuture<Integer> f = new CompletableFuture<>();
2137        final CompletableFuture<Integer> g = new CompletableFuture<>();
2138        final IncFunction r = new IncFunction();
2139
2140        assertTrue(f.cancel(mayInterruptIfRunning));
2141        g.complete(v1);
2142        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2143
2144        // unspecified behavior
2145        Integer v;
2125          try {
2126 <            assertEquals(h.join(), inc(v1));
2127 <            assertEquals(r.invocationCount, 1);
2126 >            assertEquals(inc(v1), h2.join());
2127 >            rs[2].assertValue(inc(v1));
2128          } catch (CompletionException ok) {
2129 <            checkCompletedWithWrappedCancellationException(h);
2130 <            assertEquals(r.invocationCount, 0);
2129 >            checkCompletedWithWrappedCancellationException(h2);
2130 >            rs[2].assertNotInvoked();
2131          }
2132 <
2133 <        checkCancelled(f);
2134 <        checkCompletedNormally(g, v1);
2132 >        try {
2133 >            assertEquals(inc(v1), h3.join());
2134 >            rs[3].assertValue(inc(v1));
2135 >        } catch (CompletionException ok) {
2136 >            checkCompletedWithWrappedCancellationException(h3);
2137 >            rs[3].assertNotInvoked();
2138          }
2139 <    }
2139 >
2140 >        checkCompletedNormally(f, v1);
2141 >        checkCancelled(g);
2142 >    }}
2143  
2144      /**
2145 <     * acceptEither result completes normally after normal completion
2161 <     * of either source
2145 >     * applyToEither result completes exceptionally if action does
2146       */
2147 <    public void testAcceptEither_normalCompletion1() {
2147 >    public void testApplyToEither_actionFailed() {
2148          for (ExecutionMode m : ExecutionMode.values())
2149          for (Integer v1 : new Integer[] { 1, null })
2150 <        for (Integer v2 : new Integer[] { 2, null }) {
2151 <
2150 >        for (Integer v2 : new Integer[] { 2, null })
2151 >    {
2152          final CompletableFuture<Integer> f = new CompletableFuture<>();
2153          final CompletableFuture<Integer> g = new CompletableFuture<>();
2154 <        final IncAction r = new IncAction();
2155 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2154 >        final FailingFunction[] rs = new FailingFunction[6];
2155 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2156  
2157 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2158 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2159          f.complete(v1);
2160 <        checkCompletedNormally(h, null);
2161 <        assertEquals(r.value, inc(v1));
2162 <        g.complete(v2);
2163 <
2164 <        checkCompletedNormally(f, v1);
2165 <        checkCompletedNormally(g, v2);
2166 <        checkCompletedNormally(h, null);
2181 <        }
2182 <    }
2160 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2161 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2162 >        checkCompletedWithWrappedCFException(h0);
2163 >        checkCompletedWithWrappedCFException(h1);
2164 >        checkCompletedWithWrappedCFException(h2);
2165 >        checkCompletedWithWrappedCFException(h3);
2166 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2167  
2168 <    public void testAcceptEither_normalCompletion2() {
2185 <        for (ExecutionMode m : ExecutionMode.values())
2186 <        for (Integer v1 : new Integer[] { 1, null })
2187 <        for (Integer v2 : new Integer[] { 2, null }) {
2168 >        g.complete(v2);
2169  
2170 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2171 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2172 <        final IncAction r = new IncAction();
2192 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2170 >        // unspecified behavior - both source completions available
2171 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2172 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2173  
2174 <        g.complete(v2);
2175 <        checkCompletedNormally(h, null);
2176 <        assertEquals(r.value, inc(v2));
2177 <        f.complete(v1);
2174 >        checkCompletedWithWrappedCFException(h4);
2175 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2176 >                   Objects.equals(v2, rs[4].value));
2177 >        checkCompletedWithWrappedCFException(h5);
2178 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2179 >                   Objects.equals(v2, rs[5].value));
2180  
2181          checkCompletedNormally(f, v1);
2182          checkCompletedNormally(g, v2);
2183 <        checkCompletedNormally(h, null);
2184 <        }
2185 <    }
2186 <    public void testAcceptEither_normalCompletion3() {
2183 >    }}
2184 >
2185 >    /**
2186 >     * acceptEither result completes normally after normal completion
2187 >     * of either source
2188 >     */
2189 >    public void testAcceptEither_normalCompletion() {
2190          for (ExecutionMode m : ExecutionMode.values())
2191          for (Integer v1 : new Integer[] { 1, null })
2192 <        for (Integer v2 : new Integer[] { 2, null }) {
2193 <
2192 >        for (Integer v2 : new Integer[] { 2, null })
2193 >    {
2194          final CompletableFuture<Integer> f = new CompletableFuture<>();
2195          final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 <        final IncAction r = new IncAction();
2197 <
2198 <        f.complete(v1);
2199 <        g.complete(v2);
2200 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2196 >        final NoopConsumer[] rs = new NoopConsumer[6];
2197 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2198 >
2199 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2200 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2201 >        checkIncomplete(h0);
2202 >        checkIncomplete(h1);
2203 >        rs[0].assertNotInvoked();
2204 >        rs[1].assertNotInvoked();
2205 >        f.complete(v1);
2206 >        checkCompletedNormally(h0, null);
2207 >        checkCompletedNormally(h1, null);
2208 >        rs[0].assertValue(v1);
2209 >        rs[1].assertValue(v1);
2210 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2211 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2212 >        checkCompletedNormally(h2, null);
2213 >        checkCompletedNormally(h3, null);
2214 >        rs[2].assertValue(v1);
2215 >        rs[3].assertValue(v1);
2216 >        g.complete(v2);
2217 >
2218 >        // unspecified behavior - both source completions available
2219 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2220 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2221 >        checkCompletedNormally(h4, null);
2222 >        checkCompletedNormally(h5, null);
2223 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2224 >                   Objects.equals(v2, rs[4].value));
2225 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2226 >                   Objects.equals(v2, rs[5].value));
2227  
2217        checkCompletedNormally(h, null);
2228          checkCompletedNormally(f, v1);
2229          checkCompletedNormally(g, v2);
2230 <
2231 <        // unspecified behavior
2232 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2233 <                   Objects.equals(r.value, inc(v2)));
2234 <        }
2235 <    }
2230 >        checkCompletedNormally(h0, null);
2231 >        checkCompletedNormally(h1, null);
2232 >        checkCompletedNormally(h2, null);
2233 >        checkCompletedNormally(h3, null);
2234 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2235 >    }}
2236  
2237      /**
2238       * acceptEither result completes exceptionally after exceptional
2239       * completion of either source
2240       */
2241 <    public void testAcceptEither_exceptionalCompletion1() {
2241 >    public void testAcceptEither_exceptionalCompletion() {
2242          for (ExecutionMode m : ExecutionMode.values())
2243 <        for (Integer v1 : new Integer[] { 1, null }) {
2244 <
2243 >        for (Integer v1 : new Integer[] { 1, null })
2244 >    {
2245          final CompletableFuture<Integer> f = new CompletableFuture<>();
2246          final CompletableFuture<Integer> g = new CompletableFuture<>();
2237        final IncAction r = new IncAction();
2238        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2247          final CFException ex = new CFException();
2248 +        final NoopConsumer[] rs = new NoopConsumer[6];
2249 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2250  
2251 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2252 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2253 +        checkIncomplete(h0);
2254 +        checkIncomplete(h1);
2255 +        rs[0].assertNotInvoked();
2256 +        rs[1].assertNotInvoked();
2257          f.completeExceptionally(ex);
2258 <        checkCompletedWithWrappedCFException(h, ex);
2258 >        checkCompletedWithWrappedException(h0, ex);
2259 >        checkCompletedWithWrappedException(h1, ex);
2260 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2261 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2262 >        checkCompletedWithWrappedException(h2, ex);
2263 >        checkCompletedWithWrappedException(h3, ex);
2264 >
2265          g.complete(v1);
2266  
2267 <        assertEquals(r.invocationCount, 0);
2268 <        checkCompletedNormally(g, v1);
2269 <        checkCompletedWithWrappedCFException(f, ex);
2270 <        checkCompletedWithWrappedCFException(h, ex);
2267 >        // unspecified behavior - both source completions available
2268 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2269 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2270 >        try {
2271 >            assertNull(h4.join());
2272 >            rs[4].assertValue(v1);
2273 >        } catch (CompletionException ok) {
2274 >            checkCompletedWithWrappedException(h4, ex);
2275 >            rs[4].assertNotInvoked();
2276          }
2277 <    }
2277 >        try {
2278 >            assertNull(h5.join());
2279 >            rs[5].assertValue(v1);
2280 >        } catch (CompletionException ok) {
2281 >            checkCompletedWithWrappedException(h5, ex);
2282 >            rs[5].assertNotInvoked();
2283 >        }
2284 >
2285 >        checkCompletedExceptionally(f, ex);
2286 >        checkCompletedNormally(g, v1);
2287 >        checkCompletedWithWrappedException(h0, ex);
2288 >        checkCompletedWithWrappedException(h1, ex);
2289 >        checkCompletedWithWrappedException(h2, ex);
2290 >        checkCompletedWithWrappedException(h3, ex);
2291 >        checkCompletedWithWrappedException(h4, ex);
2292 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2293 >    }}
2294  
2295      public void testAcceptEither_exceptionalCompletion2() {
2296          for (ExecutionMode m : ExecutionMode.values())
2297 <        for (Integer v1 : new Integer[] { 1, null }) {
2298 <
2297 >        for (boolean fFirst : new boolean[] { true, false })
2298 >        for (Integer v1 : new Integer[] { 1, null })
2299 >    {
2300          final CompletableFuture<Integer> f = new CompletableFuture<>();
2301          final CompletableFuture<Integer> g = new CompletableFuture<>();
2258        final IncAction r = new IncAction();
2259        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2302          final CFException ex = new CFException();
2303 +        final NoopConsumer[] rs = new NoopConsumer[6];
2304 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2305  
2306 <        g.completeExceptionally(ex);
2307 <        checkCompletedWithWrappedCFException(h, ex);
2308 <        f.complete(v1);
2309 <
2310 <        assertEquals(r.invocationCount, 0);
2311 <        checkCompletedNormally(f, v1);
2312 <        checkCompletedWithWrappedCFException(g, ex);
2313 <        checkCompletedWithWrappedCFException(h, ex);
2306 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2307 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2308 >        if (fFirst) {
2309 >            f.complete(v1);
2310 >            g.completeExceptionally(ex);
2311 >        } else {
2312 >            g.completeExceptionally(ex);
2313 >            f.complete(v1);
2314          }
2315 <    }
2316 <
2273 <    public void testAcceptEither_exceptionalCompletion3() {
2274 <        for (ExecutionMode m : ExecutionMode.values())
2275 <        for (Integer v1 : new Integer[] { 1, null }) {
2276 <
2277 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2278 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2279 <        final IncAction r = new IncAction();
2280 <        final CFException ex = new CFException();
2315 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2316 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2317  
2318 <        g.completeExceptionally(ex);
2283 <        f.complete(v1);
2284 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2285 <
2286 <        // unspecified behavior
2287 <        Integer v;
2318 >        // unspecified behavior - both source completions available
2319          try {
2320 <            assertEquals(h.join(), null);
2321 <            assertEquals(r.invocationCount, 1);
2291 <            assertEquals(inc(v1), r.value);
2320 >            assertEquals(null, h0.join());
2321 >            rs[0].assertValue(v1);
2322          } catch (CompletionException ok) {
2323 <            checkCompletedWithWrappedCFException(h, ex);
2324 <            assertEquals(r.invocationCount, 0);
2323 >            checkCompletedWithWrappedException(h0, ex);
2324 >            rs[0].assertNotInvoked();
2325          }
2296
2297        checkCompletedWithWrappedCFException(g, ex);
2298        checkCompletedNormally(f, v1);
2299        }
2300    }
2301
2302    public void testAcceptEither_exceptionalCompletion4() {
2303        for (ExecutionMode m : ExecutionMode.values())
2304        for (Integer v1 : new Integer[] { 1, null }) {
2305
2306        final CompletableFuture<Integer> f = new CompletableFuture<>();
2307        final CompletableFuture<Integer> g = new CompletableFuture<>();
2308        final IncAction r = new IncAction();
2309        final CFException ex = new CFException();
2310
2311        f.completeExceptionally(ex);
2312        g.complete(v1);
2313        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2314
2315        // unspecified behavior
2316        Integer v;
2326          try {
2327 <            assertEquals(h.join(), null);
2328 <            assertEquals(r.invocationCount, 1);
2320 <            assertEquals(inc(v1), r.value);
2327 >            assertEquals(null, h1.join());
2328 >            rs[1].assertValue(v1);
2329          } catch (CompletionException ok) {
2330 <            checkCompletedWithWrappedCFException(h, ex);
2331 <            assertEquals(r.invocationCount, 0);
2330 >            checkCompletedWithWrappedException(h1, ex);
2331 >            rs[1].assertNotInvoked();
2332          }
2333 <
2334 <        checkCompletedWithWrappedCFException(f, ex);
2335 <        checkCompletedNormally(g, v1);
2333 >        try {
2334 >            assertEquals(null, h2.join());
2335 >            rs[2].assertValue(v1);
2336 >        } catch (CompletionException ok) {
2337 >            checkCompletedWithWrappedException(h2, ex);
2338 >            rs[2].assertNotInvoked();
2339          }
2340 <    }
2341 <
2342 <    /**
2343 <     * acceptEither result completes exceptionally if action does
2344 <     */
2345 <    public void testAcceptEither_actionFailed1() {
2335 <        for (ExecutionMode m : ExecutionMode.values())
2336 <        for (Integer v1 : new Integer[] { 1, null })
2337 <        for (Integer v2 : new Integer[] { 2, null }) {
2338 <
2339 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2340 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2341 <        final FailingConsumer r = new FailingConsumer();
2342 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2343 <
2344 <        f.complete(v1);
2345 <        checkCompletedWithWrappedCFException(h);
2346 <        g.complete(v2);
2347 <        checkCompletedNormally(f, v1);
2348 <        checkCompletedNormally(g, v2);
2340 >        try {
2341 >            assertEquals(null, h3.join());
2342 >            rs[3].assertValue(v1);
2343 >        } catch (CompletionException ok) {
2344 >            checkCompletedWithWrappedException(h3, ex);
2345 >            rs[3].assertNotInvoked();
2346          }
2350    }
2351
2352    public void testAcceptEither_actionFailed2() {
2353        for (ExecutionMode m : ExecutionMode.values())
2354        for (Integer v1 : new Integer[] { 1, null })
2355        for (Integer v2 : new Integer[] { 2, null }) {
2347  
2357        final CompletableFuture<Integer> f = new CompletableFuture<>();
2358        final CompletableFuture<Integer> g = new CompletableFuture<>();
2359        final FailingConsumer r = new FailingConsumer();
2360        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2361
2362        g.complete(v2);
2363        checkCompletedWithWrappedCFException(h);
2364        f.complete(v1);
2348          checkCompletedNormally(f, v1);
2349 <        checkCompletedNormally(g, v2);
2350 <        }
2368 <    }
2349 >        checkCompletedExceptionally(g, ex);
2350 >    }}
2351  
2352      /**
2353       * acceptEither result completes exceptionally if either source cancelled
2354       */
2355 <    public void testAcceptEither_sourceCancelled1() {
2355 >    public void testAcceptEither_sourceCancelled() {
2356          for (ExecutionMode m : ExecutionMode.values())
2357          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2358 <        for (Integer v1 : new Integer[] { 1, null }) {
2359 <
2378 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2379 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 <        final IncAction r = new IncAction();
2381 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2382 <
2383 <        assertTrue(f.cancel(mayInterruptIfRunning));
2384 <        checkCompletedWithWrappedCancellationException(h);
2385 <        g.complete(v1);
2386 <
2387 <        checkCancelled(f);
2388 <        assertEquals(r.invocationCount, 0);
2389 <        checkCompletedNormally(g, v1);
2390 <        checkCompletedWithWrappedCancellationException(h);
2391 <        }
2392 <    }
2393 <
2394 <    public void testAcceptEither_sourceCancelled2() {
2395 <        for (ExecutionMode m : ExecutionMode.values())
2396 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2397 <        for (Integer v1 : new Integer[] { 1, null }) {
2398 <
2358 >        for (Integer v1 : new Integer[] { 1, null })
2359 >    {
2360          final CompletableFuture<Integer> f = new CompletableFuture<>();
2361          final CompletableFuture<Integer> g = new CompletableFuture<>();
2362 <        final IncAction r = new IncAction();
2363 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2403 <
2404 <        assertTrue(g.cancel(mayInterruptIfRunning));
2405 <        checkCompletedWithWrappedCancellationException(h);
2406 <        f.complete(v1);
2407 <
2408 <        checkCancelled(g);
2409 <        assertEquals(r.invocationCount, 0);
2410 <        checkCompletedNormally(f, v1);
2411 <        checkCompletedWithWrappedCancellationException(h);
2412 <        }
2413 <    }
2362 >        final NoopConsumer[] rs = new NoopConsumer[6];
2363 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2364  
2365 <    public void testAcceptEither_sourceCancelled3() {
2366 <        for (ExecutionMode m : ExecutionMode.values())
2367 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2368 <        for (Integer v1 : new Integer[] { 1, null }) {
2369 <
2370 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2371 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2372 <        final IncAction r = new IncAction();
2365 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2366 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2367 >        checkIncomplete(h0);
2368 >        checkIncomplete(h1);
2369 >        rs[0].assertNotInvoked();
2370 >        rs[1].assertNotInvoked();
2371 >        f.cancel(mayInterruptIfRunning);
2372 >        checkCompletedWithWrappedCancellationException(h0);
2373 >        checkCompletedWithWrappedCancellationException(h1);
2374 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2375 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2376 >        checkCompletedWithWrappedCancellationException(h2);
2377 >        checkCompletedWithWrappedCancellationException(h3);
2378  
2379 <        assertTrue(g.cancel(mayInterruptIfRunning));
2425 <        f.complete(v1);
2426 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2379 >        g.complete(v1);
2380  
2381 <        // unspecified behavior
2382 <        Integer v;
2381 >        // unspecified behavior - both source completions available
2382 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2383 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2384          try {
2385 <            assertEquals(h.join(), null);
2386 <            assertEquals(r.invocationCount, 1);
2433 <            assertEquals(inc(v1), r.value);
2385 >            assertNull(h4.join());
2386 >            rs[4].assertValue(v1);
2387          } catch (CompletionException ok) {
2388 <            checkCompletedWithWrappedCancellationException(h);
2389 <            assertEquals(r.invocationCount, 0);
2437 <        }
2438 <
2439 <        checkCancelled(g);
2440 <        checkCompletedNormally(f, v1);
2388 >            checkCompletedWithWrappedCancellationException(h4);
2389 >            rs[4].assertNotInvoked();
2390          }
2442    }
2443
2444    public void testAcceptEither_sourceCancelled4() {
2445        for (ExecutionMode m : ExecutionMode.values())
2446        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2447        for (Integer v1 : new Integer[] { 1, null }) {
2448
2449        final CompletableFuture<Integer> f = new CompletableFuture<>();
2450        final CompletableFuture<Integer> g = new CompletableFuture<>();
2451        final IncAction r = new IncAction();
2452
2453        assertTrue(f.cancel(mayInterruptIfRunning));
2454        g.complete(v1);
2455        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456
2457        // unspecified behavior
2458        Integer v;
2391          try {
2392 <            assertEquals(h.join(), null);
2393 <            assertEquals(r.invocationCount, 1);
2462 <            assertEquals(inc(v1), r.value);
2392 >            assertNull(h5.join());
2393 >            rs[5].assertValue(v1);
2394          } catch (CompletionException ok) {
2395 <            checkCompletedWithWrappedCancellationException(h);
2396 <            assertEquals(r.invocationCount, 0);
2395 >            checkCompletedWithWrappedCancellationException(h5);
2396 >            rs[5].assertNotInvoked();
2397          }
2398  
2399          checkCancelled(f);
2400          checkCompletedNormally(g, v1);
2401 <        }
2402 <    }
2401 >        checkCompletedWithWrappedCancellationException(h0);
2402 >        checkCompletedWithWrappedCancellationException(h1);
2403 >        checkCompletedWithWrappedCancellationException(h2);
2404 >        checkCompletedWithWrappedCancellationException(h3);
2405 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2406 >    }}
2407  
2408      /**
2409 <     * runAfterEither result completes normally after normal completion
2475 <     * of either source
2409 >     * acceptEither result completes exceptionally if action does
2410       */
2411 <    public void testRunAfterEither_normalCompletion1() {
2411 >    public void testAcceptEither_actionFailed() {
2412          for (ExecutionMode m : ExecutionMode.values())
2413          for (Integer v1 : new Integer[] { 1, null })
2414 <        for (Integer v2 : new Integer[] { 2, null }) {
2415 <
2414 >        for (Integer v2 : new Integer[] { 2, null })
2415 >    {
2416          final CompletableFuture<Integer> f = new CompletableFuture<>();
2417          final CompletableFuture<Integer> g = new CompletableFuture<>();
2418 <        final Noop r = new Noop();
2419 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2418 >        final FailingConsumer[] rs = new FailingConsumer[6];
2419 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2420  
2421 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2422 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2423          f.complete(v1);
2424 <        checkCompletedNormally(h, null);
2425 <        assertEquals(r.invocationCount, 1);
2426 <        g.complete(v2);
2424 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2425 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2426 >        checkCompletedWithWrappedCFException(h0);
2427 >        checkCompletedWithWrappedCFException(h1);
2428 >        checkCompletedWithWrappedCFException(h2);
2429 >        checkCompletedWithWrappedCFException(h3);
2430 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2431  
2432 <        checkCompletedNormally(f, v1);
2493 <        checkCompletedNormally(g, v2);
2494 <        checkCompletedNormally(h, null);
2495 <        assertEquals(r.invocationCount, 1);
2496 <        }
2497 <    }
2432 >        g.complete(v2);
2433  
2434 <    public void testRunAfterEither_normalCompletion2() {
2435 <        for (ExecutionMode m : ExecutionMode.values())
2436 <        for (Integer v1 : new Integer[] { 1, null })
2502 <        for (Integer v2 : new Integer[] { 2, null }) {
2434 >        // unspecified behavior - both source completions available
2435 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2436 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2437  
2438 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2439 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2440 <        final Noop r = new Noop();
2441 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2442 <
2443 <        g.complete(v2);
2510 <        checkCompletedNormally(h, null);
2511 <        assertEquals(r.invocationCount, 1);
2512 <        f.complete(v1);
2438 >        checkCompletedWithWrappedCFException(h4);
2439 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2440 >                   Objects.equals(v2, rs[4].value));
2441 >        checkCompletedWithWrappedCFException(h5);
2442 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2443 >                   Objects.equals(v2, rs[5].value));
2444  
2445          checkCompletedNormally(f, v1);
2446          checkCompletedNormally(g, v2);
2447 <        checkCompletedNormally(h, null);
2448 <        assertEquals(r.invocationCount, 1);
2449 <        }
2450 <    }
2451 <    public void testRunAfterEither_normalCompletion3() {
2447 >    }}
2448 >
2449 >    /**
2450 >     * runAfterEither result completes normally after normal completion
2451 >     * of either source
2452 >     */
2453 >    public void testRunAfterEither_normalCompletion() {
2454          for (ExecutionMode m : ExecutionMode.values())
2455          for (Integer v1 : new Integer[] { 1, null })
2456 <        for (Integer v2 : new Integer[] { 2, null }) {
2457 <
2456 >        for (Integer v2 : new Integer[] { 2, null })
2457 >    {
2458          final CompletableFuture<Integer> f = new CompletableFuture<>();
2459          final CompletableFuture<Integer> g = new CompletableFuture<>();
2460 <        final Noop r = new Noop();
2460 >        final Noop[] rs = new Noop[6];
2461 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2462  
2463 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2464 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2465 +        checkIncomplete(h0);
2466 +        checkIncomplete(h1);
2467 +        rs[0].assertNotInvoked();
2468 +        rs[1].assertNotInvoked();
2469          f.complete(v1);
2470 +        checkCompletedNormally(h0, null);
2471 +        checkCompletedNormally(h1, null);
2472 +        rs[0].assertInvoked();
2473 +        rs[1].assertInvoked();
2474 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2475 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2476 +        checkCompletedNormally(h2, null);
2477 +        checkCompletedNormally(h3, null);
2478 +        rs[2].assertInvoked();
2479 +        rs[3].assertInvoked();
2480 +
2481          g.complete(v2);
2531        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2482  
2483 <        checkCompletedNormally(h, null);
2483 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2484 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2485 >
2486          checkCompletedNormally(f, v1);
2487          checkCompletedNormally(g, v2);
2488 <        assertEquals(r.invocationCount, 1);
2489 <        }
2490 <    }
2488 >        checkCompletedNormally(h0, null);
2489 >        checkCompletedNormally(h1, null);
2490 >        checkCompletedNormally(h2, null);
2491 >        checkCompletedNormally(h3, null);
2492 >        checkCompletedNormally(h4, null);
2493 >        checkCompletedNormally(h5, null);
2494 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2495 >    }}
2496  
2497      /**
2498       * runAfterEither result completes exceptionally after exceptional
2499       * completion of either source
2500       */
2501 <    public void testRunAfterEither_exceptionalCompletion1() {
2501 >    public void testRunAfterEither_exceptionalCompletion() {
2502          for (ExecutionMode m : ExecutionMode.values())
2503 <        for (Integer v1 : new Integer[] { 1, null }) {
2504 <
2503 >        for (Integer v1 : new Integer[] { 1, null })
2504 >    {
2505          final CompletableFuture<Integer> f = new CompletableFuture<>();
2506          final CompletableFuture<Integer> g = new CompletableFuture<>();
2550        final Noop r = new Noop();
2551        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2507          final CFException ex = new CFException();
2508 +        final Noop[] rs = new Noop[6];
2509 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2510  
2511 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2512 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2513 +        checkIncomplete(h0);
2514 +        checkIncomplete(h1);
2515 +        rs[0].assertNotInvoked();
2516 +        rs[1].assertNotInvoked();
2517          f.completeExceptionally(ex);
2518 <        checkCompletedWithWrappedCFException(h, ex);
2518 >        checkCompletedWithWrappedException(h0, ex);
2519 >        checkCompletedWithWrappedException(h1, ex);
2520 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2521 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2522 >        checkCompletedWithWrappedException(h2, ex);
2523 >        checkCompletedWithWrappedException(h3, ex);
2524 >
2525          g.complete(v1);
2526  
2527 <        assertEquals(r.invocationCount, 0);
2528 <        checkCompletedNormally(g, v1);
2529 <        checkCompletedWithWrappedCFException(f, ex);
2530 <        checkCompletedWithWrappedCFException(h, ex);
2527 >        // unspecified behavior - both source completions available
2528 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2529 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2530 >        try {
2531 >            assertNull(h4.join());
2532 >            rs[4].assertInvoked();
2533 >        } catch (CompletionException ok) {
2534 >            checkCompletedWithWrappedException(h4, ex);
2535 >            rs[4].assertNotInvoked();
2536 >        }
2537 >        try {
2538 >            assertNull(h5.join());
2539 >            rs[5].assertInvoked();
2540 >        } catch (CompletionException ok) {
2541 >            checkCompletedWithWrappedException(h5, ex);
2542 >            rs[5].assertNotInvoked();
2543          }
2544 <    }
2544 >
2545 >        checkCompletedExceptionally(f, ex);
2546 >        checkCompletedNormally(g, v1);
2547 >        checkCompletedWithWrappedException(h0, ex);
2548 >        checkCompletedWithWrappedException(h1, ex);
2549 >        checkCompletedWithWrappedException(h2, ex);
2550 >        checkCompletedWithWrappedException(h3, ex);
2551 >        checkCompletedWithWrappedException(h4, ex);
2552 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2553 >    }}
2554  
2555      public void testRunAfterEither_exceptionalCompletion2() {
2556          for (ExecutionMode m : ExecutionMode.values())
2557 <        for (Integer v1 : new Integer[] { 1, null }) {
2558 <
2557 >        for (boolean fFirst : new boolean[] { true, false })
2558 >        for (Integer v1 : new Integer[] { 1, null })
2559 >    {
2560          final CompletableFuture<Integer> f = new CompletableFuture<>();
2561          final CompletableFuture<Integer> g = new CompletableFuture<>();
2571        final Noop r = new Noop();
2572        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2562          final CFException ex = new CFException();
2563 +        final Noop[] rs = new Noop[6];
2564 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2565  
2566 <        g.completeExceptionally(ex);
2567 <        checkCompletedWithWrappedCFException(h, ex);
2568 <        f.complete(v1);
2569 <
2570 <        assertEquals(r.invocationCount, 0);
2571 <        checkCompletedNormally(f, v1);
2572 <        checkCompletedWithWrappedCFException(g, ex);
2573 <        checkCompletedWithWrappedCFException(h, ex);
2566 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2567 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2568 >        if (fFirst) {
2569 >            f.complete(v1);
2570 >            g.completeExceptionally(ex);
2571 >        } else {
2572 >            g.completeExceptionally(ex);
2573 >            f.complete(v1);
2574          }
2575 <    }
2575 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2576 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2577  
2578 <    public void testRunAfterEither_exceptionalCompletion3() {
2579 <        for (ExecutionMode m : ExecutionMode.values())
2580 <        for (Integer v1 : new Integer[] { 1, null }) {
2581 <
2582 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2583 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2584 <        final Noop r = new Noop();
2585 <        final CFException ex = new CFException();
2594 <
2595 <        g.completeExceptionally(ex);
2596 <        f.complete(v1);
2597 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2598 <
2599 <        // unspecified behavior
2600 <        Integer v;
2578 >        // unspecified behavior - both source completions available
2579 >        try {
2580 >            assertEquals(null, h0.join());
2581 >            rs[0].assertInvoked();
2582 >        } catch (CompletionException ok) {
2583 >            checkCompletedWithWrappedException(h0, ex);
2584 >            rs[0].assertNotInvoked();
2585 >        }
2586          try {
2587 <            assertEquals(h.join(), null);
2588 <            assertEquals(r.invocationCount, 1);
2587 >            assertEquals(null, h1.join());
2588 >            rs[1].assertInvoked();
2589          } catch (CompletionException ok) {
2590 <            checkCompletedWithWrappedCFException(h, ex);
2591 <            assertEquals(r.invocationCount, 0);
2590 >            checkCompletedWithWrappedException(h1, ex);
2591 >            rs[1].assertNotInvoked();
2592 >        }
2593 >        try {
2594 >            assertEquals(null, h2.join());
2595 >            rs[2].assertInvoked();
2596 >        } catch (CompletionException ok) {
2597 >            checkCompletedWithWrappedException(h2, ex);
2598 >            rs[2].assertNotInvoked();
2599 >        }
2600 >        try {
2601 >            assertEquals(null, h3.join());
2602 >            rs[3].assertInvoked();
2603 >        } catch (CompletionException ok) {
2604 >            checkCompletedWithWrappedException(h3, ex);
2605 >            rs[3].assertNotInvoked();
2606          }
2607  
2609        checkCompletedWithWrappedCFException(g, ex);
2608          checkCompletedNormally(f, v1);
2609 <        }
2610 <    }
2609 >        checkCompletedExceptionally(g, ex);
2610 >    }}
2611  
2612 <    public void testRunAfterEither_exceptionalCompletion4() {
2612 >    /**
2613 >     * runAfterEither result completes exceptionally if either source cancelled
2614 >     */
2615 >    public void testRunAfterEither_sourceCancelled() {
2616          for (ExecutionMode m : ExecutionMode.values())
2617 <        for (Integer v1 : new Integer[] { 1, null }) {
2618 <
2617 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2618 >        for (Integer v1 : new Integer[] { 1, null })
2619 >    {
2620          final CompletableFuture<Integer> f = new CompletableFuture<>();
2621          final CompletableFuture<Integer> g = new CompletableFuture<>();
2622 <        final Noop r = new Noop();
2623 <        final CFException ex = new CFException();
2622 >        final Noop[] rs = new Noop[6];
2623 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2624 >
2625 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2626 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2627 >        checkIncomplete(h0);
2628 >        checkIncomplete(h1);
2629 >        rs[0].assertNotInvoked();
2630 >        rs[1].assertNotInvoked();
2631 >        f.cancel(mayInterruptIfRunning);
2632 >        checkCompletedWithWrappedCancellationException(h0);
2633 >        checkCompletedWithWrappedCancellationException(h1);
2634 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2635 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2636 >        checkCompletedWithWrappedCancellationException(h2);
2637 >        checkCompletedWithWrappedCancellationException(h3);
2638  
2623        f.completeExceptionally(ex);
2639          g.complete(v1);
2625        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640  
2641 <        // unspecified behavior
2642 <        Integer v;
2641 >        // unspecified behavior - both source completions available
2642 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2643 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2644 >        try {
2645 >            assertNull(h4.join());
2646 >            rs[4].assertInvoked();
2647 >        } catch (CompletionException ok) {
2648 >            checkCompletedWithWrappedCancellationException(h4);
2649 >            rs[4].assertNotInvoked();
2650 >        }
2651          try {
2652 <            assertEquals(h.join(), null);
2653 <            assertEquals(r.invocationCount, 1);
2652 >            assertNull(h5.join());
2653 >            rs[5].assertInvoked();
2654          } catch (CompletionException ok) {
2655 <            checkCompletedWithWrappedCFException(h, ex);
2656 <            assertEquals(r.invocationCount, 0);
2655 >            checkCompletedWithWrappedCancellationException(h5);
2656 >            rs[5].assertNotInvoked();
2657          }
2658  
2659 <        checkCompletedWithWrappedCFException(f, ex);
2659 >        checkCancelled(f);
2660          checkCompletedNormally(g, v1);
2661 <        }
2662 <    }
2661 >        checkCompletedWithWrappedCancellationException(h0);
2662 >        checkCompletedWithWrappedCancellationException(h1);
2663 >        checkCompletedWithWrappedCancellationException(h2);
2664 >        checkCompletedWithWrappedCancellationException(h3);
2665 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2666 >    }}
2667  
2668      /**
2669       * runAfterEither result completes exceptionally if action does
2670       */
2671 <    public void testRunAfterEither_actionFailed1() {
2671 >    public void testRunAfterEither_actionFailed() {
2672          for (ExecutionMode m : ExecutionMode.values())
2673          for (Integer v1 : new Integer[] { 1, null })
2674 <        for (Integer v2 : new Integer[] { 2, null }) {
2675 <
2674 >        for (Integer v2 : new Integer[] { 2, null })
2675 >    {
2676          final CompletableFuture<Integer> f = new CompletableFuture<>();
2677          final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 <        final FailingNoop r = new FailingNoop();
2679 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2678 >        final FailingRunnable[] rs = new FailingRunnable[6];
2679 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2680  
2681 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683          f.complete(v1);
2684 <        checkCompletedWithWrappedCFException(h);
2684 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2685 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2686 >        checkCompletedWithWrappedCFException(h0);
2687 >        checkCompletedWithWrappedCFException(h1);
2688 >        checkCompletedWithWrappedCFException(h2);
2689 >        checkCompletedWithWrappedCFException(h3);
2690 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2691          g.complete(v2);
2692 <        checkCompletedNormally(f, v1);
2693 <        checkCompletedNormally(g, v2);
2694 <        }
2695 <    }
2662 <
2663 <    public void testRunAfterEither_actionFailed2() {
2664 <        for (ExecutionMode m : ExecutionMode.values())
2665 <        for (Integer v1 : new Integer[] { 1, null })
2666 <        for (Integer v2 : new Integer[] { 2, null }) {
2667 <
2668 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2669 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2670 <        final FailingNoop r = new FailingNoop();
2671 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2692 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2693 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2694 >        checkCompletedWithWrappedCFException(h4);
2695 >        checkCompletedWithWrappedCFException(h5);
2696  
2673        g.complete(v2);
2674        checkCompletedWithWrappedCFException(h);
2675        f.complete(v1);
2697          checkCompletedNormally(f, v1);
2698          checkCompletedNormally(g, v2);
2699 <        }
2700 <    }
2699 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2700 >    }}
2701  
2702      /**
2703 <     * runAfterEither result completes exceptionally if either source cancelled
2703 >     * thenCompose result completes normally after normal completion of source
2704       */
2705 <    public void testRunAfterEither_sourceCancelled1() {
2685 <        for (ExecutionMode m : ExecutionMode.values())
2686 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2687 <        for (Integer v1 : new Integer[] { 1, null }) {
2688 <
2689 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2690 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2691 <        final Noop r = new Noop();
2692 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2693 <
2694 <        assertTrue(f.cancel(mayInterruptIfRunning));
2695 <        checkCompletedWithWrappedCancellationException(h);
2696 <        g.complete(v1);
2697 <
2698 <        checkCancelled(f);
2699 <        assertEquals(r.invocationCount, 0);
2700 <        checkCompletedNormally(g, v1);
2701 <        checkCompletedWithWrappedCancellationException(h);
2702 <        }
2703 <    }
2704 <
2705 <    public void testRunAfterEither_sourceCancelled2() {
2705 >    public void testThenCompose_normalCompletion() {
2706          for (ExecutionMode m : ExecutionMode.values())
2707 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2708 <        for (Integer v1 : new Integer[] { 1, null }) {
2709 <
2710 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2712 <        final Noop r = new Noop();
2713 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 <
2715 <        assertTrue(g.cancel(mayInterruptIfRunning));
2716 <        checkCompletedWithWrappedCancellationException(h);
2717 <        f.complete(v1);
2718 <
2719 <        checkCancelled(g);
2720 <        assertEquals(r.invocationCount, 0);
2721 <        checkCompletedNormally(f, v1);
2722 <        checkCompletedWithWrappedCancellationException(h);
2723 <        }
2724 <    }
2725 <
2726 <    public void testRunAfterEither_sourceCancelled3() {
2727 <        for (ExecutionMode m : ExecutionMode.values())
2728 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2729 <        for (Integer v1 : new Integer[] { 1, null }) {
2730 <
2707 >        for (boolean createIncomplete : new boolean[] { true, false })
2708 >        for (Integer v1 : new Integer[] { 1, null })
2709 >    {
2710          final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2712 <        final Noop r = new Noop();
2713 <
2714 <        assertTrue(g.cancel(mayInterruptIfRunning));
2736 <        f.complete(v1);
2737 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2711 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2712 >        if (!createIncomplete) f.complete(v1);
2713 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2714 >        if (createIncomplete) f.complete(v1);
2715  
2716 <        // unspecified behavior
2740 <        Integer v;
2741 <        try {
2742 <            assertEquals(h.join(), null);
2743 <            assertEquals(r.invocationCount, 1);
2744 <        } catch (CompletionException ok) {
2745 <            checkCompletedWithWrappedCancellationException(h);
2746 <            assertEquals(r.invocationCount, 0);
2747 <        }
2748 <
2749 <        checkCancelled(g);
2716 >        checkCompletedNormally(g, inc(v1));
2717          checkCompletedNormally(f, v1);
2718 <        }
2719 <    }
2753 <
2754 <    public void testRunAfterEither_sourceCancelled4() {
2755 <        for (ExecutionMode m : ExecutionMode.values())
2756 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2757 <        for (Integer v1 : new Integer[] { 1, null }) {
2758 <
2759 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2760 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2761 <        final Noop r = new Noop();
2762 <
2763 <        assertTrue(f.cancel(mayInterruptIfRunning));
2764 <        g.complete(v1);
2765 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2766 <
2767 <        // unspecified behavior
2768 <        Integer v;
2769 <        try {
2770 <            assertEquals(h.join(), null);
2771 <            assertEquals(r.invocationCount, 1);
2772 <        } catch (CompletionException ok) {
2773 <            checkCompletedWithWrappedCancellationException(h);
2774 <            assertEquals(r.invocationCount, 0);
2775 <        }
2776 <
2777 <        checkCancelled(f);
2778 <        checkCompletedNormally(g, v1);
2779 <        }
2780 <    }
2781 <
2782 <    /**
2783 <     * thenCompose result completes normally after normal completion of source
2784 <     */
2785 <    public void testThenCompose() {
2786 <        CompletableFuture<Integer> f, g;
2787 <        CompletableFutureInc r;
2788 <
2789 <        f = new CompletableFuture<>();
2790 <        g = f.thenCompose(r = new CompletableFutureInc());
2791 <        f.complete(one);
2792 <        checkCompletedNormally(g, two);
2793 <        assertTrue(r.ran);
2794 <
2795 <        f = new CompletableFuture<>();
2796 <        f.complete(one);
2797 <        g = f.thenCompose(r = new CompletableFutureInc());
2798 <        checkCompletedNormally(g, two);
2799 <        assertTrue(r.ran);
2800 <    }
2718 >        r.assertValue(v1);
2719 >    }}
2720  
2721      /**
2722       * thenCompose result completes exceptionally after exceptional
2723       * completion of source
2724       */
2725 <    public void testThenCompose2() {
2726 <        CompletableFuture<Integer> f, g;
2727 <        CompletableFutureInc r;
2728 <
2729 <        f = new CompletableFuture<>();
2730 <        g = f.thenCompose(r = new CompletableFutureInc());
2731 <        f.completeExceptionally(new CFException());
2732 <        checkCompletedWithWrappedCFException(g);
2725 >    public void testThenCompose_exceptionalCompletion() {
2726 >        for (ExecutionMode m : ExecutionMode.values())
2727 >        for (boolean createIncomplete : new boolean[] { true, false })
2728 >    {
2729 >        final CFException ex = new CFException();
2730 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 >        if (!createIncomplete) f.completeExceptionally(ex);
2733 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2734 >        if (createIncomplete) f.completeExceptionally(ex);
2735  
2736 <        f = new CompletableFuture<>();
2737 <        f.completeExceptionally(new CFException());
2738 <        g = f.thenCompose(r = new CompletableFutureInc());
2739 <        checkCompletedWithWrappedCFException(g);
2819 <    }
2736 >        checkCompletedWithWrappedException(g, ex);
2737 >        checkCompletedExceptionally(f, ex);
2738 >        r.assertNotInvoked();
2739 >    }}
2740  
2741      /**
2742       * thenCompose result completes exceptionally if action does
2743       */
2744 <    public void testThenCompose3() {
2745 <        CompletableFuture<Integer> f, g;
2746 <        FailingCompletableFutureFunction r;
2747 <
2748 <        f = new CompletableFuture<>();
2749 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2750 <        f.complete(one);
2751 <        checkCompletedWithWrappedCFException(g);
2744 >    public void testThenCompose_actionFailed() {
2745 >        for (ExecutionMode m : ExecutionMode.values())
2746 >        for (boolean createIncomplete : new boolean[] { true, false })
2747 >        for (Integer v1 : new Integer[] { 1, null })
2748 >    {
2749 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2750 >        final FailingCompletableFutureFunction r
2751 >            = new FailingCompletableFutureFunction(m);
2752 >        if (!createIncomplete) f.complete(v1);
2753 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2754 >        if (createIncomplete) f.complete(v1);
2755  
2833        f = new CompletableFuture<>();
2834        f.complete(one);
2835        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2756          checkCompletedWithWrappedCFException(g);
2757 <    }
2757 >        checkCompletedNormally(f, v1);
2758 >    }}
2759  
2760      /**
2761       * thenCompose result completes exceptionally if source cancelled
2762       */
2763 <    public void testThenCompose4() {
2764 <        CompletableFuture<Integer> f, g;
2765 <        CompletableFutureInc r;
2766 <
2767 <        f = new CompletableFuture<>();
2768 <        g = f.thenCompose(r = new CompletableFutureInc());
2769 <        assertTrue(f.cancel(true));
2770 <        checkCompletedWithWrappedCancellationException(g);
2771 <
2772 <        f = new CompletableFuture<>();
2773 <        assertTrue(f.cancel(true));
2774 <        g = f.thenCompose(r = new CompletableFutureInc());
2775 <        checkCompletedWithWrappedCancellationException(g);
2855 <    }
2856 <
2857 <    // asyncs
2858 <
2859 <    /**
2860 <     * thenRunAsync result completes normally after normal completion of source
2861 <     */
2862 <    public void testThenRunAsync() {
2863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2864 <        Noop r = new Noop();
2865 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2866 <        f.complete(null);
2867 <        checkCompletedNormally(g, null);
2868 <
2869 <        // reordered version
2870 <        f = new CompletableFuture<>();
2871 <        f.complete(null);
2872 <        r = new Noop();
2873 <        g = f.thenRunAsync(r);
2874 <        checkCompletedNormally(g, null);
2875 <    }
2876 <
2877 <    /**
2878 <     * thenRunAsync result completes exceptionally after exceptional
2879 <     * completion of source
2880 <     */
2881 <    public void testThenRunAsync2() {
2882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2883 <        Noop r = new Noop();
2884 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2885 <        f.completeExceptionally(new CFException());
2886 <        try {
2887 <            g.join();
2888 <            shouldThrow();
2889 <        } catch (CompletionException success) {}
2890 <        checkCompletedWithWrappedCFException(g);
2891 <    }
2892 <
2893 <    /**
2894 <     * thenRunAsync result completes exceptionally if action does
2895 <     */
2896 <    public void testThenRunAsync3() {
2897 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2898 <        FailingNoop r = new FailingNoop();
2899 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2900 <        f.complete(null);
2901 <        checkCompletedWithWrappedCFException(g);
2902 <    }
2903 <
2904 <    /**
2905 <     * thenRunAsync result completes exceptionally if source cancelled
2906 <     */
2907 <    public void testThenRunAsync4() {
2908 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2909 <        Noop r = new Noop();
2910 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2911 <        assertTrue(f.cancel(true));
2912 <        checkCompletedWithWrappedCancellationException(g);
2913 <    }
2914 <
2915 <    /**
2916 <     * thenApplyAsync result completes normally after normal completion of source
2917 <     */
2918 <    public void testThenApplyAsync() {
2919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2920 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2921 <        f.complete(one);
2922 <        checkCompletedNormally(g, two);
2923 <    }
2924 <
2925 <    /**
2926 <     * thenApplyAsync result completes exceptionally after exceptional
2927 <     * completion of source
2928 <     */
2929 <    public void testThenApplyAsync2() {
2930 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2931 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2932 <        f.completeExceptionally(new CFException());
2933 <        checkCompletedWithWrappedCFException(g);
2934 <    }
2935 <
2936 <    /**
2937 <     * thenApplyAsync result completes exceptionally if action does
2938 <     */
2939 <    public void testThenApplyAsync3() {
2940 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2941 <        FailingFunction r = new FailingFunction();
2942 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2943 <        f.complete(null);
2944 <        checkCompletedWithWrappedCFException(g);
2945 <    }
2946 <
2947 <    /**
2948 <     * thenApplyAsync result completes exceptionally if source cancelled
2949 <     */
2950 <    public void testThenApplyAsync4() {
2951 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2952 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2953 <        assertTrue(f.cancel(true));
2954 <        checkCompletedWithWrappedCancellationException(g);
2955 <    }
2956 <
2957 <    /**
2958 <     * thenAcceptAsync result completes normally after normal
2959 <     * completion of source
2960 <     */
2961 <    public void testThenAcceptAsync() {
2962 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2963 <        IncAction r = new IncAction();
2964 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2965 <        f.complete(one);
2966 <        checkCompletedNormally(g, null);
2967 <        assertEquals(r.value, (Integer) 2);
2968 <    }
2969 <
2970 <    /**
2971 <     * thenAcceptAsync result completes exceptionally after exceptional
2972 <     * completion of source
2973 <     */
2974 <    public void testThenAcceptAsync2() {
2975 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2976 <        IncAction r = new IncAction();
2977 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2978 <        f.completeExceptionally(new CFException());
2979 <        checkCompletedWithWrappedCFException(g);
2980 <    }
2981 <
2982 <    /**
2983 <     * thenAcceptAsync result completes exceptionally if action does
2984 <     */
2985 <    public void testThenAcceptAsync3() {
2986 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2987 <        FailingConsumer r = new FailingConsumer();
2988 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2989 <        f.complete(null);
2990 <        checkCompletedWithWrappedCFException(g);
2991 <    }
2992 <
2993 <    /**
2994 <     * thenAcceptAsync result completes exceptionally if source cancelled
2995 <     */
2996 <    public void testThenAcceptAsync4() {
2997 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2998 <        IncAction r = new IncAction();
2999 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3000 <        assertTrue(f.cancel(true));
3001 <        checkCompletedWithWrappedCancellationException(g);
3002 <    }
3003 <
3004 <    /**
3005 <     * thenComposeAsync result completes normally after normal
3006 <     * completion of source
3007 <     */
3008 <    public void testThenComposeAsync() {
3009 <        CompletableFuture<Integer> f, g;
3010 <        CompletableFutureInc r;
3011 <
3012 <        f = new CompletableFuture<>();
3013 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3014 <        f.complete(one);
3015 <        checkCompletedNormally(g, two);
3016 <
3017 <        f = new CompletableFuture<>();
3018 <        f.complete(one);
3019 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3020 <        checkCompletedNormally(g, two);
3021 <    }
3022 <
3023 <    /**
3024 <     * thenComposeAsync result completes exceptionally after
3025 <     * exceptional completion of source
3026 <     */
3027 <    public void testThenComposeAsync2() {
3028 <        CompletableFuture<Integer> f, g;
3029 <        CompletableFutureInc r;
3030 <
3031 <        f = new CompletableFuture<>();
3032 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3033 <        f.completeExceptionally(new CFException());
3034 <        checkCompletedWithWrappedCFException(g);
3035 <        assertFalse(r.ran);
3036 <
3037 <        f = new CompletableFuture<>();
3038 <        f.completeExceptionally(new CFException());
3039 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3040 <        checkCompletedWithWrappedCFException(g);
3041 <        assertFalse(r.ran);
3042 <    }
3043 <
3044 <    /**
3045 <     * thenComposeAsync result completes exceptionally if action does
3046 <     */
3047 <    public void testThenComposeAsync3() {
3048 <        CompletableFuture<Integer> f, g;
3049 <        FailingCompletableFutureFunction r;
3050 <
3051 <        f = new CompletableFuture<>();
3052 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3053 <        f.complete(one);
3054 <        checkCompletedWithWrappedCFException(g);
3055 <
3056 <        f = new CompletableFuture<>();
3057 <        f.complete(one);
3058 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3059 <        checkCompletedWithWrappedCFException(g);
3060 <    }
3061 <
3062 <    /**
3063 <     * thenComposeAsync result completes exceptionally if source cancelled
3064 <     */
3065 <    public void testThenComposeAsync4() {
3066 <        CompletableFuture<Integer> f, g;
3067 <        CompletableFutureInc r;
3068 <
3069 <        f = new CompletableFuture<>();
3070 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3071 <        assertTrue(f.cancel(true));
3072 <        checkCompletedWithWrappedCancellationException(g);
3073 <
3074 <        f = new CompletableFuture<>();
3075 <        assertTrue(f.cancel(true));
3076 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3077 <        checkCompletedWithWrappedCancellationException(g);
3078 <    }
3079 <
3080 <    // async with explicit executors
3081 <
3082 <    /**
3083 <     * thenRunAsync result completes normally after normal completion of source
3084 <     */
3085 <    public void testThenRunAsyncE() {
3086 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3087 <        Noop r = new Noop();
3088 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3089 <        f.complete(null);
3090 <        checkCompletedNormally(g, null);
3091 <
3092 <        // reordered version
3093 <        f = new CompletableFuture<>();
3094 <        f.complete(null);
3095 <        r = new Noop();
3096 <        g = f.thenRunAsync(r, new ThreadExecutor());
3097 <        checkCompletedNormally(g, null);
3098 <    }
3099 <
3100 <    /**
3101 <     * thenRunAsync result completes exceptionally after exceptional
3102 <     * completion of source
3103 <     */
3104 <    public void testThenRunAsync2E() {
3105 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3106 <        Noop r = new Noop();
3107 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3108 <        f.completeExceptionally(new CFException());
3109 <        try {
3110 <            g.join();
3111 <            shouldThrow();
3112 <        } catch (CompletionException success) {}
3113 <        checkCompletedWithWrappedCFException(g);
3114 <    }
3115 <
3116 <    /**
3117 <     * thenRunAsync result completes exceptionally if action does
3118 <     */
3119 <    public void testThenRunAsync3E() {
3120 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3121 <        FailingNoop r = new FailingNoop();
3122 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3123 <        f.complete(null);
3124 <        checkCompletedWithWrappedCFException(g);
3125 <    }
3126 <
3127 <    /**
3128 <     * thenRunAsync result completes exceptionally if source cancelled
3129 <     */
3130 <    public void testThenRunAsync4E() {
3131 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3132 <        Noop r = new Noop();
3133 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3134 <        assertTrue(f.cancel(true));
3135 <        checkCompletedWithWrappedCancellationException(g);
3136 <    }
3137 <
3138 <    /**
3139 <     * thenApplyAsync result completes normally after normal completion of source
3140 <     */
3141 <    public void testThenApplyAsyncE() {
3142 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3143 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3144 <        f.complete(one);
3145 <        checkCompletedNormally(g, two);
3146 <    }
3147 <
3148 <    /**
3149 <     * thenApplyAsync result completes exceptionally after exceptional
3150 <     * completion of source
3151 <     */
3152 <    public void testThenApplyAsync2E() {
3153 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3154 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3155 <        f.completeExceptionally(new CFException());
3156 <        checkCompletedWithWrappedCFException(g);
3157 <    }
3158 <
3159 <    /**
3160 <     * thenApplyAsync result completes exceptionally if action does
3161 <     */
3162 <    public void testThenApplyAsync3E() {
3163 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3164 <        FailingFunction r = new FailingFunction();
3165 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3166 <        f.complete(null);
3167 <        checkCompletedWithWrappedCFException(g);
3168 <    }
3169 <
3170 <    /**
3171 <     * thenApplyAsync result completes exceptionally if source cancelled
3172 <     */
3173 <    public void testThenApplyAsync4E() {
3174 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3175 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3176 <        assertTrue(f.cancel(true));
3177 <        checkCompletedWithWrappedCancellationException(g);
3178 <    }
3179 <
3180 <    /**
3181 <     * thenAcceptAsync result completes normally after normal
3182 <     * completion of source
3183 <     */
3184 <    public void testThenAcceptAsyncE() {
3185 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3186 <        IncAction r = new IncAction();
3187 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3188 <        f.complete(one);
3189 <        checkCompletedNormally(g, null);
3190 <        assertEquals(r.value, (Integer) 2);
3191 <    }
3192 <
3193 <    /**
3194 <     * thenAcceptAsync result completes exceptionally after exceptional
3195 <     * completion of source
3196 <     */
3197 <    public void testThenAcceptAsync2E() {
3198 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3199 <        IncAction r = new IncAction();
3200 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3201 <        f.completeExceptionally(new CFException());
3202 <        checkCompletedWithWrappedCFException(g);
3203 <    }
3204 <
3205 <    /**
3206 <     * thenAcceptAsync result completes exceptionally if action does
3207 <     */
3208 <    public void testThenAcceptAsync3E() {
3209 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3210 <        FailingConsumer r = new FailingConsumer();
3211 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3212 <        f.complete(null);
3213 <        checkCompletedWithWrappedCFException(g);
3214 <    }
3215 <
3216 <    /**
3217 <     * thenAcceptAsync result completes exceptionally if source cancelled
3218 <     */
3219 <    public void testThenAcceptAsync4E() {
3220 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3221 <        IncAction r = new IncAction();
3222 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3223 <        assertTrue(f.cancel(true));
3224 <        checkCompletedWithWrappedCancellationException(g);
3225 <    }
3226 <
3227 <    /**
3228 <     * thenComposeAsync result completes normally after normal
3229 <     * completion of source
3230 <     */
3231 <    public void testThenComposeAsyncE() {
3232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3233 <        CompletableFutureInc r = new CompletableFutureInc();
3234 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3235 <        f.complete(one);
3236 <        checkCompletedNormally(g, two);
3237 <    }
3238 <
3239 <    /**
3240 <     * thenComposeAsync result completes exceptionally after
3241 <     * exceptional completion of source
3242 <     */
3243 <    public void testThenComposeAsync2E() {
3244 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3245 <        CompletableFutureInc r = new CompletableFutureInc();
3246 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3247 <        f.completeExceptionally(new CFException());
3248 <        checkCompletedWithWrappedCFException(g);
3249 <    }
3250 <
3251 <    /**
3252 <     * thenComposeAsync result completes exceptionally if action does
3253 <     */
3254 <    public void testThenComposeAsync3E() {
3255 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3256 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3257 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3258 <        f.complete(one);
3259 <        checkCompletedWithWrappedCFException(g);
3260 <    }
2763 >    public void testThenCompose_sourceCancelled() {
2764 >        for (ExecutionMode m : ExecutionMode.values())
2765 >        for (boolean createIncomplete : new boolean[] { true, false })
2766 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2767 >    {
2768 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2769 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2770 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2771 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2772 >        if (createIncomplete) {
2773 >            checkIncomplete(g);
2774 >            assertTrue(f.cancel(mayInterruptIfRunning));
2775 >        }
2776  
3262    /**
3263     * thenComposeAsync result completes exceptionally if source cancelled
3264     */
3265    public void testThenComposeAsync4E() {
3266        CompletableFuture<Integer> f = new CompletableFuture<>();
3267        CompletableFutureInc r = new CompletableFutureInc();
3268        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3269        assertTrue(f.cancel(true));
2777          checkCompletedWithWrappedCancellationException(g);
2778 <    }
2778 >        checkCancelled(f);
2779 >    }}
2780  
2781      // other static methods
2782  
# Line 3287 | Line 2795 | public class CompletableFutureTest exten
2795       */
2796      public void testAllOf_normal() throws Exception {
2797          for (int k = 1; k < 20; ++k) {
2798 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2798 >            CompletableFuture<Integer>[] fs
2799 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2800              for (int i = 0; i < k; ++i)
2801                  fs[i] = new CompletableFuture<>();
2802              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3301 | Line 2810 | public class CompletableFutureTest exten
2810          }
2811      }
2812  
2813 +    public void testAllOf_backwards() throws Exception {
2814 +        for (int k = 1; k < 20; ++k) {
2815 +            CompletableFuture<Integer>[] fs
2816 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2817 +            for (int i = 0; i < k; ++i)
2818 +                fs[i] = new CompletableFuture<>();
2819 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2820 +            for (int i = k - 1; i >= 0; i--) {
2821 +                checkIncomplete(f);
2822 +                checkIncomplete(CompletableFuture.allOf(fs));
2823 +                fs[i].complete(one);
2824 +            }
2825 +            checkCompletedNormally(f, null);
2826 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2827 +        }
2828 +    }
2829 +
2830      /**
2831       * anyOf(no component futures) returns an incomplete future
2832       */
# Line 3359 | Line 2885 | public class CompletableFutureTest exten
2885          Runnable[] throwingActions = {
2886              () -> CompletableFuture.supplyAsync(null),
2887              () -> CompletableFuture.supplyAsync(null, exec),
2888 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2888 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2889  
2890              () -> CompletableFuture.runAsync(null),
2891              () -> CompletableFuture.runAsync(null, exec),
# Line 3432 | Line 2958 | public class CompletableFutureTest exten
2958  
2959              () -> f.thenCompose(null),
2960              () -> f.thenComposeAsync(null),
2961 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2961 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2962              () -> f.thenComposeAsync(null, exec),
2963  
2964              () -> f.exceptionally(null),
# Line 3464 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
3467    /**
3468     * whenComplete action executes on normal completion, propagating
3469     * source result.
3470     */
3471    public void testWhenComplete_normalCompletion1() {
3472        for (ExecutionMode m : ExecutionMode.values())
3473        for (Integer v1 : new Integer[] { 1, null }) {
3474
3475        final AtomicInteger a = new AtomicInteger();
3476        final CompletableFuture<Integer> f = new CompletableFuture<>();
3477        final CompletableFuture<Integer> g =
3478            m.whenComplete(f,
3479                           (Integer x, Throwable t) -> {
3480                               threadAssertSame(x, v1);
3481                               threadAssertNull(t);
3482                               a.getAndIncrement();
3483                           });
3484        f.complete(v1);
3485        checkCompletedNormally(f, v1);
3486        checkCompletedNormally(g, v1);
3487        assertEquals(a.get(), 1);
3488        }
3489    }
3490
3491    /**
3492     * whenComplete action executes on exceptional completion, propagating
3493     * source result.
3494     */
3495    public void testWhenComplete_exceptionalCompletion() {
3496        for (ExecutionMode m : ExecutionMode.values())
3497        for (Integer v1 : new Integer[] { 1, null }) {
3498
3499        final AtomicInteger a = new AtomicInteger();
3500        final CFException ex = new CFException();
3501        final CompletableFuture<Integer> f = new CompletableFuture<>();
3502        final CompletableFuture<Integer> g = m.whenComplete
3503            (f,
3504             (Integer x, Throwable t) -> {
3505                threadAssertNull(x);
3506                threadAssertSame(t, ex);
3507                a.getAndIncrement();
3508            });
3509        f.completeExceptionally(ex);
3510        checkCompletedWithWrappedCFException(f, ex);
3511        checkCompletedWithWrappedCFException(g, ex);
3512        assertEquals(a.get(), 1);
3513        }
3514    }
3515
3516    /**
3517     * If a whenComplete action throws an exception when triggered by
3518     * a normal completion, it completes exceptionally
3519     */
3520    public void testWhenComplete_actionFailed() {
3521        for (ExecutionMode m : ExecutionMode.values())
3522        for (Integer v1 : new Integer[] { 1, null }) {
3523
3524        final CFException ex = new CFException();
3525        final CompletableFuture<Integer> f = new CompletableFuture<>();
3526        final CompletableFuture<Integer> g = m.whenComplete
3527            (f,
3528             (Integer x, Throwable t) -> {
3529                threadAssertSame(x, v1);
3530                threadAssertNull(t);
3531                throw ex;
3532            });
3533        f.complete(v1);
3534        checkCompletedNormally(f, v1);
3535        checkCompletedWithWrappedCFException(g, ex);
3536        }
3537    }
3538
3539    /**
3540     * If a whenComplete action throws an exception when triggered by
3541     * a source completion that also throws an exception, the source
3542     * exception takes precedence.
3543     */
3544    public void testWhenComplete_actionFailedSourceFailed() {
3545        for (ExecutionMode m : ExecutionMode.values())
3546        for (Integer v1 : new Integer[] { 1, null }) {
3547
3548        final CFException ex1 = new CFException();
3549        final CFException ex2 = new CFException();
3550        final CompletableFuture<Integer> f = new CompletableFuture<>();
3551        final CompletableFuture<Integer> g = m.whenComplete
3552            (f,
3553             (Integer x, Throwable t) -> {
3554                threadAssertSame(t, ex1);
3555                threadAssertNull(x);
3556                throw ex2;
3557            });
3558        f.completeExceptionally(ex1);
3559        checkCompletedWithWrappedCFException(f, ex1);
3560        checkCompletedWithWrappedCFException(g, ex1);
3561        }
3562    }
3563
3564    /**
3565     * handleAsync action completes normally with function value on
3566     * either normal or exceptional completion of source
3567     */
3568    public void testHandleAsync() {
3569        CompletableFuture<Integer> f, g;
3570        IntegerHandler r;
3571
3572        f = new CompletableFuture<>();
3573        g = f.handleAsync(r = new IntegerHandler());
3574        assertFalse(r.ran);
3575        f.completeExceptionally(new CFException());
3576        checkCompletedWithWrappedCFException(f);
3577        checkCompletedNormally(g, three);
3578        assertTrue(r.ran);
3579
3580        f = new CompletableFuture<>();
3581        g = f.handleAsync(r = new IntegerHandler());
3582        assertFalse(r.ran);
3583        f.completeExceptionally(new CFException());
3584        checkCompletedWithWrappedCFException(f);
3585        checkCompletedNormally(g, three);
3586        assertTrue(r.ran);
3587
3588        f = new CompletableFuture<>();
3589        g = f.handleAsync(r = new IntegerHandler());
3590        assertFalse(r.ran);
3591        f.complete(one);
3592        checkCompletedNormally(f, one);
3593        checkCompletedNormally(g, two);
3594        assertTrue(r.ran);
3595
3596        f = new CompletableFuture<>();
3597        g = f.handleAsync(r = new IntegerHandler());
3598        assertFalse(r.ran);
3599        f.complete(one);
3600        checkCompletedNormally(f, one);
3601        checkCompletedNormally(g, two);
3602        assertTrue(r.ran);
3603    }
3604
3605    /**
3606     * handleAsync action with Executor completes normally with
3607     * function value on either normal or exceptional completion of
3608     * source
3609     */
3610    public void testHandleAsync2() {
3611        CompletableFuture<Integer> f, g;
3612        ThreadExecutor exec = new ThreadExecutor();
3613        IntegerHandler r;
3614
3615        f = new CompletableFuture<>();
3616        g = f.handleAsync(r = new IntegerHandler(), exec);
3617        assertFalse(r.ran);
3618        f.completeExceptionally(new CFException());
3619        checkCompletedWithWrappedCFException(f);
3620        checkCompletedNormally(g, three);
3621        assertTrue(r.ran);
3622
3623        f = new CompletableFuture<>();
3624        g = f.handleAsync(r = new IntegerHandler(), exec);
3625        assertFalse(r.ran);
3626        f.completeExceptionally(new CFException());
3627        checkCompletedWithWrappedCFException(f);
3628        checkCompletedNormally(g, three);
3629        assertTrue(r.ran);
3630
3631        f = new CompletableFuture<>();
3632        g = f.handleAsync(r = new IntegerHandler(), exec);
3633        assertFalse(r.ran);
3634        f.complete(one);
3635        checkCompletedNormally(f, one);
3636        checkCompletedNormally(g, two);
3637        assertTrue(r.ran);
3638
3639        f = new CompletableFuture<>();
3640        g = f.handleAsync(r = new IntegerHandler(), exec);
3641        assertFalse(r.ran);
3642        f.complete(one);
3643        checkCompletedNormally(f, one);
3644        checkCompletedNormally(g, two);
3645        assertTrue(r.ran);
3646    }
3647
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines