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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines