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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines