ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.41 by jsr166, Mon Jun 2 01:46:11 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19 + import java.util.concurrent.CompletionStage;
20   import java.util.concurrent.TimeoutException;
21   import java.util.concurrent.atomic.AtomicInteger;
22   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 317 | Line 318 | public class CompletableFutureTest exten
318  
319      // Choose non-commutative actions for better coverage
320  
321 +    // A non-commutative function that handles and produces null values as well.
322 +    static Integer subtract(Integer x, Integer y) {
323 +        return (x == null && y == null) ? null :
324 +            ((x == null) ? 42 : x.intValue())
325 +            - ((y == null) ? 99 : y.intValue());
326 +    }
327 +
328 +    // A function that handles and produces null values as well.
329 +    static Integer inc(Integer x) {
330 +        return (x == null) ? null : x + 1;
331 +    }
332 +
333      static final Supplier<Integer> supplyOne =
334          () -> Integer.valueOf(1);
335      static final Function<Integer, Integer> inc =
336          (Integer x) -> Integer.valueOf(x.intValue() + 1);
337      static final BiFunction<Integer, Integer, Integer> subtract =
338 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
338 >        (Integer x, Integer y) -> subtract(x, y);
339      static final class IncAction implements Consumer<Integer> {
340 <        int value;
341 <        public void accept(Integer x) { value = x.intValue() + 1; }
340 >        int invocationCount = 0;
341 >        Integer value;
342 >        public boolean ran() { return invocationCount == 1; }
343 >        public void accept(Integer x) {
344 >            invocationCount++;
345 >            value = inc(x);
346 >        }
347 >    }
348 >    static final class IncFunction implements Function<Integer,Integer> {
349 >        int invocationCount = 0;
350 >        Integer value;
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356      }
357      static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 <        int value;
358 >        int invocationCount = 0;
359 >        Integer value;
360 >        // Check this action was invoked exactly once when result is computed.
361 >        public boolean ran() { return invocationCount == 1; }
362          public void accept(Integer x, Integer y) {
363 <            value = x.intValue() - y.intValue();
363 >            invocationCount++;
364 >            value = subtract(x, y);
365 >        }
366 >    }
367 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
368 >        int invocationCount = 0;
369 >        Integer value;
370 >        // Check this action was invoked exactly once when result is computed.
371 >        public boolean ran() { return invocationCount == 1; }
372 >        public Integer apply(Integer x, Integer y) {
373 >            invocationCount++;
374 >            return value = subtract(x, y);
375          }
376      }
377      static final class Noop implements Runnable {
378 +        int invocationCount = 0;
379          boolean ran;
380 <        public void run() { ran = true; }
380 >        public void run() {
381 >            invocationCount++;
382 >            ran = true;
383 >        }
384      }
385  
386      static final class FailingSupplier implements Supplier<Integer> {
# Line 405 | Line 450 | public class CompletableFutureTest exten
450      }
451  
452      /**
453 +     * Permits the testing of parallel code for the 3 different
454 +     * execution modes without repeating all the testing code.
455 +     */
456 +    enum ExecutionMode {
457 +        DEFAULT {
458 +            public <T,U> CompletableFuture<Void> runAfterBoth
459 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
460 +                return f.runAfterBoth(g, a);
461 +            }
462 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
463 +                (CompletableFuture<T> f,
464 +                 CompletionStage<? extends U> g,
465 +                 BiConsumer<? super T,? super U> a) {
466 +                return f.thenAcceptBoth(g, a);
467 +            }
468 +            public <T,U,V> CompletableFuture<V> thenCombine
469 +                (CompletableFuture<T> f,
470 +                 CompletionStage<? extends U> g,
471 +                 BiFunction<? super T,? super U,? extends V> a) {
472 +                return f.thenCombine(g, a);
473 +            }
474 +            public <T,U> CompletableFuture<U> applyToEither
475 +                (CompletableFuture<T> f,
476 +                 CompletionStage<? extends T> g,
477 +                 Function<? super T,U> a) {
478 +                return f.applyToEither(g, a);
479 +            }
480 +            public <T> CompletableFuture<Void> acceptEither
481 +                (CompletableFuture<T> f,
482 +                 CompletionStage<? extends T> g,
483 +                 Consumer<? super T> a) {
484 +                return f.acceptEither(g, a);
485 +            }
486 +            public <T> CompletableFuture<Void> runAfterEither
487 +                (CompletableFuture<T> f,
488 +                 CompletionStage<?> g,
489 +                 java.lang.Runnable a) {
490 +                return f.runAfterEither(g, a);
491 +            }
492 +            public <T,U> CompletableFuture<U> thenCompose
493 +                (CompletableFuture<T> f,
494 +                 Function<? super T,? extends CompletionStage<U>> a) {
495 +                return f.thenCompose(a);
496 +            }
497 +            public <T> CompletableFuture<T> whenComplete
498 +                (CompletableFuture<T> f,
499 +                 BiConsumer<? super T,? super Throwable> a) {
500 +                return f.whenComplete(a);
501 +            }
502 +        },
503 +
504 + //             /** Experimental way to do more testing */
505 + //         REVERSE_DEFAULT {
506 + //             public <T,U> CompletableFuture<Void> runAfterBoth
507 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
508 + //                 return g.runAfterBoth(f, a);
509 + //             }
510 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
511 + //                 (CompletableFuture<T> f,
512 + //                  CompletionStage<? extends U> g,
513 + //                  BiConsumer<? super T,? super U> a) {
514 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
515 + //             }
516 + //         },
517 +
518 +        DEFAULT_ASYNC {
519 +            public <T,U> CompletableFuture<Void> runAfterBoth
520 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
521 +                return f.runAfterBothAsync(g, a);
522 +            }
523 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
524 +                (CompletableFuture<T> f,
525 +                 CompletionStage<? extends U> g,
526 +                 BiConsumer<? super T,? super U> a) {
527 +                return f.thenAcceptBothAsync(g, a);
528 +            }
529 +            public <T,U,V> CompletableFuture<V> thenCombine
530 +                (CompletableFuture<T> f,
531 +                 CompletionStage<? extends U> g,
532 +                 BiFunction<? super T,? super U,? extends V> a) {
533 +                return f.thenCombineAsync(g, a);
534 +            }
535 +            public <T,U> CompletableFuture<U> applyToEither
536 +                (CompletableFuture<T> f,
537 +                 CompletionStage<? extends T> g,
538 +                 Function<? super T,U> a) {
539 +                return f.applyToEitherAsync(g, a);
540 +            }
541 +            public <T> CompletableFuture<Void> acceptEither
542 +                (CompletableFuture<T> f,
543 +                 CompletionStage<? extends T> g,
544 +                 Consumer<? super T> a) {
545 +                return f.acceptEitherAsync(g, a);
546 +            }
547 +            public <T> CompletableFuture<Void> runAfterEither
548 +                (CompletableFuture<T> f,
549 +                 CompletionStage<?> g,
550 +                 java.lang.Runnable a) {
551 +                return f.runAfterEitherAsync(g, a);
552 +            }
553 +            public <T,U> CompletableFuture<U> thenCompose
554 +                (CompletableFuture<T> f,
555 +                 Function<? super T,? extends CompletionStage<U>> a) {
556 +                return f.thenComposeAsync(a);
557 +            }
558 +            public <T> CompletableFuture<T> whenComplete
559 +                (CompletableFuture<T> f,
560 +                 BiConsumer<? super T,? super Throwable> a) {
561 +                return f.whenCompleteAsync(a);
562 +            }
563 +        },
564 +
565 + //         REVERSE_DEFAULT_ASYNC {
566 + //             public <T,U> CompletableFuture<Void> runAfterBoth
567 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
568 + //                 return f.runAfterBothAsync(g, a);
569 + //             }
570 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
571 + //                 (CompletableFuture<T> f,
572 + //                  CompletionStage<? extends U> g,
573 + //                  BiConsumer<? super T,? super U> a) {
574 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
575 + //             }
576 + //         },
577 +
578 +        EXECUTOR {
579 +            public <T,U> CompletableFuture<Void> runAfterBoth
580 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
581 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
582 +            }
583 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
584 +                (CompletableFuture<T> f,
585 +                 CompletionStage<? extends U> g,
586 +                 BiConsumer<? super T,? super U> a) {
587 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
588 +            }
589 +            public <T,U,V> CompletableFuture<V> thenCombine
590 +                (CompletableFuture<T> f,
591 +                 CompletionStage<? extends U> g,
592 +                 BiFunction<? super T,? super U,? extends V> a) {
593 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
594 +            }
595 +            public <T,U> CompletableFuture<U> applyToEither
596 +                (CompletableFuture<T> f,
597 +                 CompletionStage<? extends T> g,
598 +                 Function<? super T,U> a) {
599 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600 +            }
601 +            public <T> CompletableFuture<Void> acceptEither
602 +                (CompletableFuture<T> f,
603 +                 CompletionStage<? extends T> g,
604 +                 Consumer<? super T> a) {
605 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606 +            }
607 +            public <T> CompletableFuture<Void> runAfterEither
608 +                (CompletableFuture<T> f,
609 +                 CompletionStage<?> g,
610 +                 java.lang.Runnable a) {
611 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
612 +            }
613 +            public <T,U> CompletableFuture<U> thenCompose
614 +                (CompletableFuture<T> f,
615 +                 Function<? super T,? extends CompletionStage<U>> a) {
616 +                return f.thenComposeAsync(a, new ThreadExecutor());
617 +            }
618 +            public <T> CompletableFuture<T> whenComplete
619 +                (CompletableFuture<T> f,
620 +                 BiConsumer<? super T,? super Throwable> a) {
621 +                return f.whenCompleteAsync(a, new ThreadExecutor());
622 +            }
623 +        };
624 +
625 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
626 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
627 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
628 +            (CompletableFuture<T> f,
629 +             CompletionStage<? extends U> g,
630 +             BiConsumer<? super T,? super U> a);
631 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
632 +            (CompletableFuture<T> f,
633 +             CompletionStage<? extends U> g,
634 +             BiFunction<? super T,? super U,? extends V> a);
635 +        public abstract <T,U> CompletableFuture<U> applyToEither
636 +            (CompletableFuture<T> f,
637 +             CompletionStage<? extends T> g,
638 +             Function<? super T,U> a);
639 +        public abstract <T> CompletableFuture<Void> acceptEither
640 +            (CompletableFuture<T> f,
641 +             CompletionStage<? extends T> g,
642 +             Consumer<? super T> a);
643 +        public abstract <T> CompletableFuture<Void> runAfterEither
644 +            (CompletableFuture<T> f,
645 +             CompletionStage<?> g,
646 +             java.lang.Runnable a);
647 +        public abstract <T,U> CompletableFuture<U> thenCompose
648 +            (CompletableFuture<T> f,
649 +             Function<? super T,? extends CompletionStage<U>> a);
650 +        public abstract <T> CompletableFuture<T> whenComplete
651 +            (CompletableFuture<T> f,
652 +             BiConsumer<? super T,? super Throwable> a);
653 +
654 +
655 +    }
656 +
657 +    /**
658       * exceptionally action completes with function value on source
659       * exception; otherwise with source value
660       */
# Line 654 | Line 904 | public class CompletableFutureTest exten
904          CompletableFuture<Void> g = f.thenAccept(r);
905          f.complete(one);
906          checkCompletedNormally(g, null);
907 <        assertEquals(r.value, 2);
907 >        assertEquals(r.value, (Integer) 2);
908      }
909  
910      /**
# Line 696 | Line 946 | public class CompletableFutureTest exten
946       * thenCombine result completes normally after normal completion
947       * of sources
948       */
949 <    public void testThenCombine() {
950 <        CompletableFuture<Integer> f, g, h;
949 >    public void testThenCombine_normalCompletion1() {
950 >        for (ExecutionMode m : ExecutionMode.values())
951 >        for (Integer v1 : new Integer[] { 1, null })
952 >        for (Integer v2 : new Integer[] { 2, null }) {
953  
954 <        f = new CompletableFuture<>();
955 <        g = new CompletableFuture<>();
956 <        h = f.thenCombine(g, subtract);
957 <        f.complete(3);
954 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
955 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
956 >        final SubtractFunction r = new SubtractFunction();
957 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
958 >
959 >        f.complete(v1);
960          checkIncomplete(h);
961 <        g.complete(1);
962 <        checkCompletedNormally(h, 2);
961 >        assertEquals(r.invocationCount, 0);
962 >        g.complete(v2);
963  
964 <        f = new CompletableFuture<>();
965 <        g = new CompletableFuture<>();
966 <        h = f.thenCombine(g, subtract);
967 <        g.complete(1);
964 >        checkCompletedNormally(h, subtract(v1, v2));
965 >        checkCompletedNormally(f, v1);
966 >        checkCompletedNormally(g, v2);
967 >        assertEquals(r.invocationCount, 1);
968 >        }
969 >    }
970 >
971 >    public void testThenCombine_normalCompletion2() {
972 >        for (ExecutionMode m : ExecutionMode.values())
973 >        for (Integer v1 : new Integer[] { 1, null })
974 >        for (Integer v2 : new Integer[] { 2, null }) {
975 >
976 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
977 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
978 >        final SubtractFunction r = new SubtractFunction();
979 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
980 >
981 >        g.complete(v2);
982          checkIncomplete(h);
983 <        f.complete(3);
984 <        checkCompletedNormally(h, 2);
983 >        assertEquals(r.invocationCount, 0);
984 >        f.complete(v1);
985  
986 <        f = new CompletableFuture<>();
987 <        g = new CompletableFuture<>();
988 <        g.complete(1);
989 <        f.complete(3);
990 <        h = f.thenCombine(g, subtract);
991 <        checkCompletedNormally(h, 2);
986 >        checkCompletedNormally(h, subtract(v1, v2));
987 >        checkCompletedNormally(f, v1);
988 >        checkCompletedNormally(g, v2);
989 >        assertEquals(r.invocationCount, 1);
990 >        }
991 >    }
992 >
993 >    public void testThenCombine_normalCompletion3() {
994 >        for (ExecutionMode m : ExecutionMode.values())
995 >        for (Integer v1 : new Integer[] { 1, null })
996 >        for (Integer v2 : new Integer[] { 2, null }) {
997 >
998 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
999 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1000 >        final SubtractFunction r = new SubtractFunction();
1001 >
1002 >        g.complete(v2);
1003 >        f.complete(v1);
1004 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1005 >
1006 >        checkCompletedNormally(h, subtract(v1, v2));
1007 >        checkCompletedNormally(f, v1);
1008 >        checkCompletedNormally(g, v2);
1009 >        assertEquals(r.invocationCount, 1);
1010 >        }
1011 >    }
1012 >
1013 >    public void testThenCombine_normalCompletion4() {
1014 >        for (ExecutionMode m : ExecutionMode.values())
1015 >        for (Integer v1 : new Integer[] { 1, null })
1016 >        for (Integer v2 : new Integer[] { 2, null }) {
1017 >
1018 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1020 >        final SubtractFunction r = new SubtractFunction();
1021 >
1022 >        f.complete(v1);
1023 >        g.complete(v2);
1024 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1025 >
1026 >        checkCompletedNormally(h, subtract(v1, v2));
1027 >        checkCompletedNormally(f, v1);
1028 >        checkCompletedNormally(g, v2);
1029 >        assertEquals(r.invocationCount, 1);
1030 >        }
1031      }
1032  
1033      /**
1034       * thenCombine result completes exceptionally after exceptional
1035       * completion of either source
1036       */
1037 <    public void testThenCombine2() {
1038 <        CompletableFuture<Integer> f, g, h;
1037 >    public void testThenCombine_exceptionalCompletion1() {
1038 >        for (ExecutionMode m : ExecutionMode.values())
1039 >        for (Integer v1 : new Integer[] { 1, null }) {
1040  
1041 <        f = new CompletableFuture<>();
1042 <        g = new CompletableFuture<>();
1043 <        h = f.thenCombine(g, subtract);
1044 <        f.completeExceptionally(new CFException());
1041 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1042 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1043 >        final SubtractFunction r = new SubtractFunction();
1044 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1045 >        final CFException ex = new CFException();
1046 >
1047 >        f.completeExceptionally(ex);
1048          checkIncomplete(h);
1049 <        g.complete(1);
739 <        checkCompletedWithWrappedCFException(h);
1049 >        g.complete(v1);
1050  
1051 <        f = new CompletableFuture<>();
1052 <        g = new CompletableFuture<>();
1053 <        h = f.thenCombine(g, subtract);
1054 <        g.completeExceptionally(new CFException());
1051 >        checkCompletedWithWrappedCFException(h, ex);
1052 >        checkCompletedWithWrappedCFException(f, ex);
1053 >        assertEquals(r.invocationCount, 0);
1054 >        checkCompletedNormally(g, v1);
1055 >        }
1056 >    }
1057 >
1058 >    public void testThenCombine_exceptionalCompletion2() {
1059 >        for (ExecutionMode m : ExecutionMode.values())
1060 >        for (Integer v1 : new Integer[] { 1, null }) {
1061 >
1062 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1063 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1064 >        final SubtractFunction r = new SubtractFunction();
1065 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1066 >        final CFException ex = new CFException();
1067 >
1068 >        g.completeExceptionally(ex);
1069          checkIncomplete(h);
1070 <        f.complete(3);
747 <        checkCompletedWithWrappedCFException(h);
1070 >        f.complete(v1);
1071  
1072 <        f = new CompletableFuture<>();
1073 <        g = new CompletableFuture<>();
1074 <        f.complete(3);
1075 <        g.completeExceptionally(new CFException());
1076 <        h = f.thenCombine(g, subtract);
1077 <        checkCompletedWithWrappedCFException(h);
1072 >        checkCompletedWithWrappedCFException(h, ex);
1073 >        checkCompletedWithWrappedCFException(g, ex);
1074 >        assertEquals(r.invocationCount, 0);
1075 >        checkCompletedNormally(f, v1);
1076 >        }
1077 >    }
1078  
1079 <        f = new CompletableFuture<>();
1080 <        g = new CompletableFuture<>();
1081 <        f.completeExceptionally(new CFException());
1082 <        g.complete(3);
1083 <        h = f.thenCombine(g, subtract);
1084 <        checkCompletedWithWrappedCFException(h);
1079 >    public void testThenCombine_exceptionalCompletion3() {
1080 >        for (ExecutionMode m : ExecutionMode.values())
1081 >        for (Integer v1 : new Integer[] { 1, null }) {
1082 >
1083 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1085 >        final SubtractFunction r = new SubtractFunction();
1086 >        final CFException ex = new CFException();
1087 >
1088 >        g.completeExceptionally(ex);
1089 >        f.complete(v1);
1090 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1091 >
1092 >        checkCompletedWithWrappedCFException(h, ex);
1093 >        checkCompletedWithWrappedCFException(g, ex);
1094 >        assertEquals(r.invocationCount, 0);
1095 >        checkCompletedNormally(f, v1);
1096 >        }
1097 >    }
1098 >
1099 >    public void testThenCombine_exceptionalCompletion4() {
1100 >        for (ExecutionMode m : ExecutionMode.values())
1101 >        for (Integer v1 : new Integer[] { 1, null }) {
1102 >
1103 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1104 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1105 >        final SubtractFunction r = new SubtractFunction();
1106 >        final CFException ex = new CFException();
1107 >
1108 >        f.completeExceptionally(ex);
1109 >        g.complete(v1);
1110 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1111 >
1112 >        checkCompletedWithWrappedCFException(h, ex);
1113 >        checkCompletedWithWrappedCFException(f, ex);
1114 >        assertEquals(r.invocationCount, 0);
1115 >        checkCompletedNormally(g, v1);
1116 >        }
1117      }
1118  
1119      /**
1120       * thenCombine result completes exceptionally if action does
1121       */
1122 <    public void testThenCombine3() {
1123 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1124 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1125 <        FailingBiFunction r = new FailingBiFunction();
1126 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1127 <        f.complete(one);
1128 <        checkIncomplete(g);
1129 <        assertFalse(r.ran);
1130 <        f2.complete(two);
1131 <        checkCompletedWithWrappedCFException(g);
1132 <        assertTrue(r.ran);
1122 >    public void testThenCombine_actionFailed1() {
1123 >        for (ExecutionMode m : ExecutionMode.values())
1124 >        for (Integer v1 : new Integer[] { 1, null })
1125 >        for (Integer v2 : new Integer[] { 2, null }) {
1126 >
1127 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1128 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1129 >        final FailingBiFunction r = new FailingBiFunction();
1130 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1131 >
1132 >        f.complete(v1);
1133 >        checkIncomplete(h);
1134 >        g.complete(v2);
1135 >
1136 >        checkCompletedWithWrappedCFException(h);
1137 >        checkCompletedNormally(f, v1);
1138 >        checkCompletedNormally(g, v2);
1139 >        }
1140 >    }
1141 >
1142 >    public void testThenCombine_actionFailed2() {
1143 >        for (ExecutionMode m : ExecutionMode.values())
1144 >        for (Integer v1 : new Integer[] { 1, null })
1145 >        for (Integer v2 : new Integer[] { 2, null }) {
1146 >
1147 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1148 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1149 >        final FailingBiFunction r = new FailingBiFunction();
1150 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1151 >
1152 >        g.complete(v2);
1153 >        checkIncomplete(h);
1154 >        f.complete(v1);
1155 >
1156 >        checkCompletedWithWrappedCFException(h);
1157 >        checkCompletedNormally(f, v1);
1158 >        checkCompletedNormally(g, v2);
1159 >        }
1160      }
1161  
1162      /**
1163       * thenCombine result completes exceptionally if either source cancelled
1164       */
1165 <    public void testThenCombine4() {
1166 <        CompletableFuture<Integer> f, g, h;
1165 >    public void testThenCombine_sourceCancelled1() {
1166 >        for (ExecutionMode m : ExecutionMode.values())
1167 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1168 >        for (Integer v1 : new Integer[] { 1, null }) {
1169  
1170 <        f = new CompletableFuture<>();
1171 <        g = new CompletableFuture<>();
1172 <        h = f.thenCombine(g, subtract);
1173 <        assertTrue(f.cancel(true));
1170 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1171 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1172 >        final SubtractFunction r = new SubtractFunction();
1173 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1174 >
1175 >        assertTrue(f.cancel(mayInterruptIfRunning));
1176          checkIncomplete(h);
1177 <        g.complete(1);
1177 >        g.complete(v1);
1178 >
1179          checkCompletedWithWrappedCancellationException(h);
1180 +        checkCancelled(f);
1181 +        assertEquals(r.invocationCount, 0);
1182 +        checkCompletedNormally(g, v1);
1183 +        }
1184 +    }
1185  
1186 <        f = new CompletableFuture<>();
1187 <        g = new CompletableFuture<>();
1188 <        h = f.thenCombine(g, subtract);
1189 <        assertTrue(g.cancel(true));
1186 >    public void testThenCombine_sourceCancelled2() {
1187 >        for (ExecutionMode m : ExecutionMode.values())
1188 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1189 >        for (Integer v1 : new Integer[] { 1, null }) {
1190 >
1191 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1192 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1193 >        final SubtractFunction r = new SubtractFunction();
1194 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1195 >
1196 >        assertTrue(g.cancel(mayInterruptIfRunning));
1197          checkIncomplete(h);
1198 <        f.complete(3);
1198 >        f.complete(v1);
1199 >
1200          checkCompletedWithWrappedCancellationException(h);
1201 +        checkCancelled(g);
1202 +        assertEquals(r.invocationCount, 0);
1203 +        checkCompletedNormally(f, v1);
1204 +        }
1205 +    }
1206 +
1207 +    public void testThenCombine_sourceCancelled3() {
1208 +        for (ExecutionMode m : ExecutionMode.values())
1209 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1210 +        for (Integer v1 : new Integer[] { 1, null }) {
1211 +
1212 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1213 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1214 +        final SubtractFunction r = new SubtractFunction();
1215 +
1216 +        assertTrue(g.cancel(mayInterruptIfRunning));
1217 +        f.complete(v1);
1218 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1219  
802        f = new CompletableFuture<>();
803        g = new CompletableFuture<>();
804        assertTrue(f.cancel(true));
805        assertTrue(g.cancel(true));
806        h = f.thenCombine(g, subtract);
1220          checkCompletedWithWrappedCancellationException(h);
1221 +        checkCancelled(g);
1222 +        assertEquals(r.invocationCount, 0);
1223 +        checkCompletedNormally(f, v1);
1224 +        }
1225 +    }
1226 +
1227 +    public void testThenCombine_sourceCancelled4() {
1228 +        for (ExecutionMode m : ExecutionMode.values())
1229 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1230 +        for (Integer v1 : new Integer[] { 1, null }) {
1231 +
1232 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1233 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1234 +        final SubtractFunction r = new SubtractFunction();
1235 +
1236 +        assertTrue(f.cancel(mayInterruptIfRunning));
1237 +        g.complete(v1);
1238 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1239 +
1240 +        checkCompletedWithWrappedCancellationException(h);
1241 +        checkCancelled(f);
1242 +        assertEquals(r.invocationCount, 0);
1243 +        checkCompletedNormally(g, v1);
1244 +        }
1245      }
1246  
1247      /**
1248       * thenAcceptBoth result completes normally after normal
1249       * completion of sources
1250       */
1251 <    public void testThenAcceptBoth() {
1252 <        CompletableFuture<Integer> f, g;
1253 <        CompletableFuture<Void> h;
1254 <        SubtractAction r;
1251 >    public void testThenAcceptBoth_normalCompletion1() {
1252 >        for (ExecutionMode m : ExecutionMode.values())
1253 >        for (Integer v1 : new Integer[] { 1, null })
1254 >        for (Integer v2 : new Integer[] { 2, null }) {
1255  
1256 <        f = new CompletableFuture<>();
1257 <        g = new CompletableFuture<>();
1258 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1259 <        f.complete(3);
1256 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1257 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1258 >        final SubtractAction r = new SubtractAction();
1259 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1260 >
1261 >        f.complete(v1);
1262          checkIncomplete(h);
1263 <        g.complete(1);
1263 >        assertFalse(r.ran());
1264 >        g.complete(v2);
1265 >
1266          checkCompletedNormally(h, null);
1267 <        assertEquals(r.value, 2);
1267 >        assertEquals(r.value, subtract(v1, v2));
1268 >        checkCompletedNormally(f, v1);
1269 >        checkCompletedNormally(g, v2);
1270 >        }
1271 >    }
1272  
1273 <        f = new CompletableFuture<>();
1274 <        g = new CompletableFuture<>();
1275 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1276 <        g.complete(1);
1273 >    public void testThenAcceptBoth_normalCompletion2() {
1274 >        for (ExecutionMode m : ExecutionMode.values())
1275 >        for (Integer v1 : new Integer[] { 1, null })
1276 >        for (Integer v2 : new Integer[] { 2, null }) {
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 >
1283 >        g.complete(v2);
1284          checkIncomplete(h);
1285 <        f.complete(3);
1285 >        assertFalse(r.ran());
1286 >        f.complete(v1);
1287 >
1288          checkCompletedNormally(h, null);
1289 <        assertEquals(r.value, 2);
1289 >        assertEquals(r.value, subtract(v1, v2));
1290 >        checkCompletedNormally(f, v1);
1291 >        checkCompletedNormally(g, v2);
1292 >        }
1293 >    }
1294 >
1295 >    public void testThenAcceptBoth_normalCompletion3() {
1296 >        for (ExecutionMode m : ExecutionMode.values())
1297 >        for (Integer v1 : new Integer[] { 1, null })
1298 >        for (Integer v2 : new Integer[] { 2, null }) {
1299 >
1300 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1301 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1302 >        final SubtractAction r = new SubtractAction();
1303 >
1304 >        g.complete(v2);
1305 >        f.complete(v1);
1306 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1307  
837        f = new CompletableFuture<>();
838        g = new CompletableFuture<>();
839        g.complete(1);
840        f.complete(3);
841        h = f.thenAcceptBoth(g, r = new SubtractAction());
1308          checkCompletedNormally(h, null);
1309 <        assertEquals(r.value, 2);
1309 >        assertEquals(r.value, subtract(v1, v2));
1310 >        checkCompletedNormally(f, v1);
1311 >        checkCompletedNormally(g, v2);
1312 >        }
1313 >    }
1314 >
1315 >    public void testThenAcceptBoth_normalCompletion4() {
1316 >        for (ExecutionMode m : ExecutionMode.values())
1317 >        for (Integer v1 : new Integer[] { 1, null })
1318 >        for (Integer v2 : new Integer[] { 2, null }) {
1319 >
1320 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 >        final SubtractAction r = new SubtractAction();
1323 >
1324 >        f.complete(v1);
1325 >        g.complete(v2);
1326 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1327 >
1328 >        checkCompletedNormally(h, null);
1329 >        assertEquals(r.value, subtract(v1, v2));
1330 >        checkCompletedNormally(f, v1);
1331 >        checkCompletedNormally(g, v2);
1332 >        }
1333      }
1334  
1335      /**
1336       * thenAcceptBoth result completes exceptionally after exceptional
1337       * completion of either source
1338       */
1339 <    public void testThenAcceptBoth2() {
1340 <        CompletableFuture<Integer> f, g;
1341 <        CompletableFuture<Void> h;
853 <        SubtractAction r;
1339 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1340 >        for (ExecutionMode m : ExecutionMode.values())
1341 >        for (Integer v1 : new Integer[] { 1, null }) {
1342  
1343 <        f = new CompletableFuture<>();
1344 <        g = new CompletableFuture<>();
1345 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1346 <        f.completeExceptionally(new CFException());
1343 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1344 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1345 >        final SubtractAction r = new SubtractAction();
1346 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1347 >        final CFException ex = new CFException();
1348 >
1349 >        f.completeExceptionally(ex);
1350          checkIncomplete(h);
1351 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
1351 >        g.complete(v1);
1352  
1353 <        f = new CompletableFuture<>();
1354 <        g = new CompletableFuture<>();
1355 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1356 <        g.completeExceptionally(new CFException());
1353 >        checkCompletedWithWrappedCFException(h, ex);
1354 >        checkCompletedWithWrappedCFException(f, ex);
1355 >        assertEquals(r.invocationCount, 0);
1356 >        checkCompletedNormally(g, v1);
1357 >        }
1358 >    }
1359 >
1360 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1361 >        for (ExecutionMode m : ExecutionMode.values())
1362 >        for (Integer v1 : new Integer[] { 1, null }) {
1363 >
1364 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1365 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1366 >        final SubtractAction r = new SubtractAction();
1367 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1368 >        final CFException ex = new CFException();
1369 >
1370 >        g.completeExceptionally(ex);
1371          checkIncomplete(h);
1372 <        f.complete(3);
869 <        checkCompletedWithWrappedCFException(h);
1372 >        f.complete(v1);
1373  
1374 <        f = new CompletableFuture<>();
1375 <        g = new CompletableFuture<>();
1376 <        f.complete(3);
1377 <        g.completeExceptionally(new CFException());
1378 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1379 <        checkCompletedWithWrappedCFException(h);
1374 >        checkCompletedWithWrappedCFException(h, ex);
1375 >        checkCompletedWithWrappedCFException(g, ex);
1376 >        assertEquals(r.invocationCount, 0);
1377 >        checkCompletedNormally(f, v1);
1378 >        }
1379 >    }
1380  
1381 <        f = new CompletableFuture<>();
1382 <        g = new CompletableFuture<>();
1383 <        f.completeExceptionally(new CFException());
1384 <        g.complete(3);
1385 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1386 <        checkCompletedWithWrappedCFException(h);
1381 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1382 >        for (ExecutionMode m : ExecutionMode.values())
1383 >        for (Integer v1 : new Integer[] { 1, null }) {
1384 >
1385 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1386 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1387 >        final SubtractAction r = new SubtractAction();
1388 >        final CFException ex = new CFException();
1389 >
1390 >        g.completeExceptionally(ex);
1391 >        f.complete(v1);
1392 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1393 >
1394 >        checkCompletedWithWrappedCFException(h, ex);
1395 >        checkCompletedWithWrappedCFException(g, ex);
1396 >        assertEquals(r.invocationCount, 0);
1397 >        checkCompletedNormally(f, v1);
1398 >        }
1399 >    }
1400 >
1401 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1402 >        for (ExecutionMode m : ExecutionMode.values())
1403 >        for (Integer v1 : new Integer[] { 1, null }) {
1404 >
1405 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1406 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1407 >        final SubtractAction r = new SubtractAction();
1408 >        final CFException ex = new CFException();
1409 >
1410 >        f.completeExceptionally(ex);
1411 >        g.complete(v1);
1412 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1413 >
1414 >        checkCompletedWithWrappedCFException(h, ex);
1415 >        checkCompletedWithWrappedCFException(f, ex);
1416 >        assertFalse(r.ran());
1417 >        checkCompletedNormally(g, v1);
1418 >        }
1419      }
1420  
1421      /**
1422       * thenAcceptBoth result completes exceptionally if action does
1423       */
1424 <    public void testThenAcceptBoth3() {
1425 <        CompletableFuture<Integer> f, g;
1426 <        CompletableFuture<Void> h;
1427 <        FailingBiConsumer r;
1424 >    public void testThenAcceptBoth_actionFailed1() {
1425 >        for (ExecutionMode m : ExecutionMode.values())
1426 >        for (Integer v1 : new Integer[] { 1, null })
1427 >        for (Integer v2 : new Integer[] { 2, null }) {
1428  
1429 <        f = new CompletableFuture<>();
1430 <        g = new CompletableFuture<>();
1431 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1432 <        f.complete(3);
1429 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1430 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1431 >        final FailingBiConsumer r = new FailingBiConsumer();
1432 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1433 >
1434 >        f.complete(v1);
1435          checkIncomplete(h);
1436 <        g.complete(1);
1436 >        g.complete(v2);
1437 >
1438          checkCompletedWithWrappedCFException(h);
1439 +        checkCompletedNormally(f, v1);
1440 +        checkCompletedNormally(g, v2);
1441 +        }
1442 +    }
1443 +
1444 +    public void testThenAcceptBoth_actionFailed2() {
1445 +        for (ExecutionMode m : ExecutionMode.values())
1446 +        for (Integer v1 : new Integer[] { 1, null })
1447 +        for (Integer v2 : new Integer[] { 2, null }) {
1448 +
1449 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1450 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1451 +        final FailingBiConsumer r = new FailingBiConsumer();
1452 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1453 +
1454 +        g.complete(v2);
1455 +        checkIncomplete(h);
1456 +        f.complete(v1);
1457  
902        f = new CompletableFuture<>();
903        g = new CompletableFuture<>();
904        f.complete(3);
905        g.complete(1);
906        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1458          checkCompletedWithWrappedCFException(h);
1459 +        checkCompletedNormally(f, v1);
1460 +        checkCompletedNormally(g, v2);
1461 +        }
1462      }
1463  
1464      /**
1465       * thenAcceptBoth result completes exceptionally if either source cancelled
1466       */
1467 <    public void testThenAcceptBoth4() {
1468 <        CompletableFuture<Integer> f, g;
1469 <        CompletableFuture<Void> h;
1470 <        SubtractAction r;
1467 >    public void testThenAcceptBoth_sourceCancelled1() {
1468 >        for (ExecutionMode m : ExecutionMode.values())
1469 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1470 >        for (Integer v1 : new Integer[] { 1, null }) {
1471  
1472 <        f = new CompletableFuture<>();
1473 <        g = new CompletableFuture<>();
1474 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1475 <        assertTrue(f.cancel(true));
1472 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1473 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1474 >        final SubtractAction r = new SubtractAction();
1475 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1476 >
1477 >        assertTrue(f.cancel(mayInterruptIfRunning));
1478          checkIncomplete(h);
1479 <        g.complete(1);
1479 >        g.complete(v1);
1480 >
1481          checkCompletedWithWrappedCancellationException(h);
1482 +        checkCancelled(f);
1483 +        assertEquals(r.invocationCount, 0);
1484 +        checkCompletedNormally(g, v1);
1485 +        }
1486 +    }
1487  
1488 <        f = new CompletableFuture<>();
1489 <        g = new CompletableFuture<>();
1490 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1491 <        assertTrue(g.cancel(true));
1488 >    public void testThenAcceptBoth_sourceCancelled2() {
1489 >        for (ExecutionMode m : ExecutionMode.values())
1490 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1491 >        for (Integer v1 : new Integer[] { 1, null }) {
1492 >
1493 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1494 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1495 >        final SubtractAction r = new SubtractAction();
1496 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1497 >
1498 >        assertTrue(g.cancel(mayInterruptIfRunning));
1499          checkIncomplete(h);
1500 <        f.complete(3);
1500 >        f.complete(v1);
1501 >
1502          checkCompletedWithWrappedCancellationException(h);
1503 +        checkCancelled(g);
1504 +        assertEquals(r.invocationCount, 0);
1505 +        checkCompletedNormally(f, v1);
1506 +        }
1507 +    }
1508 +
1509 +    public void testThenAcceptBoth_sourceCancelled3() {
1510 +        for (ExecutionMode m : ExecutionMode.values())
1511 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1512 +        for (Integer v1 : new Integer[] { 1, null }) {
1513 +
1514 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1515 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1516 +        final SubtractAction r = new SubtractAction();
1517 +
1518 +        assertTrue(g.cancel(mayInterruptIfRunning));
1519 +        f.complete(v1);
1520 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1521  
934        f = new CompletableFuture<>();
935        g = new CompletableFuture<>();
936        f.complete(3);
937        assertTrue(g.cancel(true));
938        h = f.thenAcceptBoth(g, r = new SubtractAction());
1522          checkCompletedWithWrappedCancellationException(h);
1523 +        checkCancelled(g);
1524 +        assertEquals(r.invocationCount, 0);
1525 +        checkCompletedNormally(f, v1);
1526 +        }
1527 +    }
1528 +
1529 +    public void testThenAcceptBoth_sourceCancelled4() {
1530 +        for (ExecutionMode m : ExecutionMode.values())
1531 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1532 +        for (Integer v1 : new Integer[] { 1, null }) {
1533 +
1534 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1535 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1536 +        final SubtractAction r = new SubtractAction();
1537 +
1538 +        assertTrue(f.cancel(mayInterruptIfRunning));
1539 +        g.complete(v1);
1540 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1541  
941        f = new CompletableFuture<>();
942        g = new CompletableFuture<>();
943        assertTrue(f.cancel(true));
944        g.complete(3);
945        h = f.thenAcceptBoth(g, r = new SubtractAction());
1542          checkCompletedWithWrappedCancellationException(h);
1543 +        checkCancelled(f);
1544 +        assertEquals(r.invocationCount, 0);
1545 +        checkCompletedNormally(g, v1);
1546 +        }
1547      }
1548  
1549      /**
# Line 951 | Line 1551 | public class CompletableFutureTest exten
1551       * completion of sources
1552       */
1553      public void testRunAfterBoth_normalCompletion1() {
1554 +        for (ExecutionMode m : ExecutionMode.values())
1555          for (Integer v1 : new Integer[] { 1, null })
1556          for (Integer v2 : new Integer[] { 2, null }) {
1557  
1558          final CompletableFuture<Integer> f = new CompletableFuture<>();
1559          final CompletableFuture<Integer> g = new CompletableFuture<>();
1560          final Noop r = new Noop();
1561 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1561 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1562  
1563          f.complete(v1);
1564          checkIncomplete(h);
# Line 965 | Line 1566 | public class CompletableFutureTest exten
1566          g.complete(v2);
1567  
1568          checkCompletedNormally(h, null);
1569 <        assertTrue(r.ran);
1569 >        assertEquals(r.invocationCount, 1);
1570          checkCompletedNormally(f, v1);
1571          checkCompletedNormally(g, v2);
1572          }
1573      }
1574  
1575      public void testRunAfterBoth_normalCompletion2() {
1576 +        for (ExecutionMode m : ExecutionMode.values())
1577          for (Integer v1 : new Integer[] { 1, null })
1578          for (Integer v2 : new Integer[] { 2, null }) {
1579  
1580          final CompletableFuture<Integer> f = new CompletableFuture<>();
1581          final CompletableFuture<Integer> g = new CompletableFuture<>();
1582          final Noop r = new Noop();
1583 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1583 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1584  
1585          g.complete(v2);
1586          checkIncomplete(h);
# Line 986 | Line 1588 | public class CompletableFutureTest exten
1588          f.complete(v1);
1589  
1590          checkCompletedNormally(h, null);
1591 <        assertTrue(r.ran);
1591 >        assertEquals(r.invocationCount, 1);
1592          checkCompletedNormally(f, v1);
1593          checkCompletedNormally(g, v2);
1594          }
1595      }
1596  
1597      public void testRunAfterBoth_normalCompletion3() {
1598 +        for (ExecutionMode m : ExecutionMode.values())
1599          for (Integer v1 : new Integer[] { 1, null })
1600          for (Integer v2 : new Integer[] { 2, null }) {
1601  
# Line 1002 | Line 1605 | public class CompletableFutureTest exten
1605  
1606          g.complete(v2);
1607          f.complete(v1);
1608 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1608 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1609  
1610          checkCompletedNormally(h, null);
1611          assertTrue(r.ran);
# Line 1012 | Line 1615 | public class CompletableFutureTest exten
1615      }
1616  
1617      public void testRunAfterBoth_normalCompletion4() {
1618 +        for (ExecutionMode m : ExecutionMode.values())
1619          for (Integer v1 : new Integer[] { 1, null })
1620          for (Integer v2 : new Integer[] { 2, null }) {
1621  
# Line 1021 | Line 1625 | public class CompletableFutureTest exten
1625  
1626          f.complete(v1);
1627          g.complete(v2);
1628 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1628 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1629  
1630          checkCompletedNormally(h, null);
1631 <        assertTrue(r.ran);
1631 >        assertEquals(r.invocationCount, 1);
1632          checkCompletedNormally(f, v1);
1633          checkCompletedNormally(g, v2);
1634          }
# Line 1035 | Line 1639 | public class CompletableFutureTest exten
1639       * completion of either source
1640       */
1641      public void testRunAfterBoth_exceptionalCompletion1() {
1642 +        for (ExecutionMode m : ExecutionMode.values())
1643          for (Integer v1 : new Integer[] { 1, null }) {
1644  
1645          final CompletableFuture<Integer> f = new CompletableFuture<>();
1646          final CompletableFuture<Integer> g = new CompletableFuture<>();
1647          final Noop r = new Noop();
1648 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1648 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1649          final CFException ex = new CFException();
1650  
1651          f.completeExceptionally(ex);
# Line 1049 | Line 1654 | public class CompletableFutureTest exten
1654  
1655          checkCompletedWithWrappedCFException(h, ex);
1656          checkCompletedWithWrappedCFException(f, ex);
1657 <        assertFalse(r.ran);
1657 >        assertEquals(r.invocationCount, 0);
1658          checkCompletedNormally(g, v1);
1659          }
1660      }
1661  
1662      public void testRunAfterBoth_exceptionalCompletion2() {
1663 +        for (ExecutionMode m : ExecutionMode.values())
1664          for (Integer v1 : new Integer[] { 1, null }) {
1665  
1666          final CompletableFuture<Integer> f = new CompletableFuture<>();
1667          final CompletableFuture<Integer> g = new CompletableFuture<>();
1668          final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1669 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670          final CFException ex = new CFException();
1671  
1672          g.completeExceptionally(ex);
# Line 1069 | Line 1675 | public class CompletableFutureTest exten
1675  
1676          checkCompletedWithWrappedCFException(h, ex);
1677          checkCompletedWithWrappedCFException(g, ex);
1678 <        assertFalse(r.ran);
1678 >        assertEquals(r.invocationCount, 0);
1679          checkCompletedNormally(f, v1);
1680          }
1681      }
1682  
1683      public void testRunAfterBoth_exceptionalCompletion3() {
1684 +        for (ExecutionMode m : ExecutionMode.values())
1685          for (Integer v1 : new Integer[] { 1, null }) {
1686  
1687          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 1084 | Line 1691 | public class CompletableFutureTest exten
1691  
1692          g.completeExceptionally(ex);
1693          f.complete(v1);
1694 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1694 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1695  
1696          checkCompletedWithWrappedCFException(h, ex);
1697          checkCompletedWithWrappedCFException(g, ex);
1698 <        assertFalse(r.ran);
1698 >        assertEquals(r.invocationCount, 0);
1699          checkCompletedNormally(f, v1);
1700          }
1701      }
1702  
1703      public void testRunAfterBoth_exceptionalCompletion4() {
1704 +        for (ExecutionMode m : ExecutionMode.values())
1705          for (Integer v1 : new Integer[] { 1, null }) {
1706  
1707          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 1103 | Line 1711 | public class CompletableFutureTest exten
1711  
1712          f.completeExceptionally(ex);
1713          g.complete(v1);
1714 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1714 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715  
1716          checkCompletedWithWrappedCFException(h, ex);
1717          checkCompletedWithWrappedCFException(f, ex);
1718 <        assertFalse(r.ran);
1718 >        assertEquals(r.invocationCount, 0);
1719          checkCompletedNormally(g, v1);
1720          }
1721      }
# Line 1116 | Line 1724 | public class CompletableFutureTest exten
1724       * runAfterBoth result completes exceptionally if action does
1725       */
1726      public void testRunAfterBoth_actionFailed1() {
1727 +        for (ExecutionMode m : ExecutionMode.values())
1728          for (Integer v1 : new Integer[] { 1, null })
1729          for (Integer v2 : new Integer[] { 2, null }) {
1730  
1731          final CompletableFuture<Integer> f = new CompletableFuture<>();
1732          final CompletableFuture<Integer> g = new CompletableFuture<>();
1733          final FailingNoop r = new FailingNoop();
1734 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1734 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1735  
1736          f.complete(v1);
1737          checkIncomplete(h);
# Line 1135 | Line 1744 | public class CompletableFutureTest exten
1744      }
1745  
1746      public void testRunAfterBoth_actionFailed2() {
1747 +        for (ExecutionMode m : ExecutionMode.values())
1748          for (Integer v1 : new Integer[] { 1, null })
1749          for (Integer v2 : new Integer[] { 2, null }) {
1750  
1751          final CompletableFuture<Integer> f = new CompletableFuture<>();
1752          final CompletableFuture<Integer> g = new CompletableFuture<>();
1753          final FailingNoop r = new FailingNoop();
1754 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1754 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1755  
1756          g.complete(v2);
1757          checkIncomplete(h);
# Line 1157 | Line 1767 | public class CompletableFutureTest exten
1767       * runAfterBoth result completes exceptionally if either source cancelled
1768       */
1769      public void testRunAfterBoth_sourceCancelled1() {
1770 +        for (ExecutionMode m : ExecutionMode.values())
1771          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1772          for (Integer v1 : new Integer[] { 1, null }) {
1773  
1774          final CompletableFuture<Integer> f = new CompletableFuture<>();
1775          final CompletableFuture<Integer> g = new CompletableFuture<>();
1776          final Noop r = new Noop();
1777 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1777 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1778  
1779          assertTrue(f.cancel(mayInterruptIfRunning));
1780          checkIncomplete(h);
# Line 1171 | Line 1782 | public class CompletableFutureTest exten
1782  
1783          checkCompletedWithWrappedCancellationException(h);
1784          checkCancelled(f);
1785 <        assertFalse(r.ran);
1785 >        assertEquals(r.invocationCount, 0);
1786          checkCompletedNormally(g, v1);
1787          }
1788      }
1789  
1790      public void testRunAfterBoth_sourceCancelled2() {
1791 +        for (ExecutionMode m : ExecutionMode.values())
1792          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1793          for (Integer v1 : new Integer[] { 1, null }) {
1794  
1795          final CompletableFuture<Integer> f = new CompletableFuture<>();
1796          final CompletableFuture<Integer> g = new CompletableFuture<>();
1797          final Noop r = new Noop();
1798 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1798 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1799  
1800          assertTrue(g.cancel(mayInterruptIfRunning));
1801          checkIncomplete(h);
# Line 1191 | Line 1803 | public class CompletableFutureTest exten
1803  
1804          checkCompletedWithWrappedCancellationException(h);
1805          checkCancelled(g);
1806 <        assertFalse(r.ran);
1806 >        assertEquals(r.invocationCount, 0);
1807          checkCompletedNormally(f, v1);
1808          }
1809      }
1810  
1811      public void testRunAfterBoth_sourceCancelled3() {
1812 +        for (ExecutionMode m : ExecutionMode.values())
1813          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1814          for (Integer v1 : new Integer[] { 1, null }) {
1815  
# Line 1206 | Line 1819 | public class CompletableFutureTest exten
1819  
1820          assertTrue(g.cancel(mayInterruptIfRunning));
1821          f.complete(v1);
1822 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1822 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1823  
1824          checkCompletedWithWrappedCancellationException(h);
1825          checkCancelled(g);
1826 <        assertFalse(r.ran);
1826 >        assertEquals(r.invocationCount, 0);
1827          checkCompletedNormally(f, v1);
1828          }
1829      }
1830  
1831      public void testRunAfterBoth_sourceCancelled4() {
1832 +        for (ExecutionMode m : ExecutionMode.values())
1833          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1834          for (Integer v1 : new Integer[] { 1, null }) {
1835  
# Line 1225 | Line 1839 | public class CompletableFutureTest exten
1839  
1840          assertTrue(f.cancel(mayInterruptIfRunning));
1841          g.complete(v1);
1842 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1842 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843  
1844          checkCompletedWithWrappedCancellationException(h);
1845          checkCancelled(f);
1846 <        assertFalse(r.ran);
1846 >        assertEquals(r.invocationCount, 0);
1847          checkCompletedNormally(g, v1);
1848          }
1849      }
# Line 1238 | Line 1852 | public class CompletableFutureTest exten
1852       * applyToEither result completes normally after normal completion
1853       * of either source
1854       */
1855 <    public void testApplyToEither() {
1856 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1857 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1858 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1245 <        f.complete(one);
1246 <        checkCompletedNormally(g, two);
1247 <        f2.complete(one);
1248 <        checkCompletedNormally(g, two);
1855 >    public void testApplyToEither_normalCompletion1() {
1856 >        for (ExecutionMode m : ExecutionMode.values())
1857 >        for (Integer v1 : new Integer[] { 1, null })
1858 >        for (Integer v2 : new Integer[] { 2, null }) {
1859  
1860 <        f = new CompletableFuture<>();
1861 <        f.complete(one);
1862 <        f2 = new CompletableFuture<>();
1863 <        g = f.applyToEither(f2, inc);
1864 <        checkCompletedNormally(g, two);
1860 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1861 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1862 >        final IncFunction r = new IncFunction();
1863 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1864 >
1865 >        f.complete(v1);
1866 >        checkCompletedNormally(h, inc(v1));
1867 >        g.complete(v2);
1868 >
1869 >        checkCompletedNormally(f, v1);
1870 >        checkCompletedNormally(g, v2);
1871 >        checkCompletedNormally(h, inc(v1));
1872 >        }
1873 >    }
1874 >
1875 >    public void testApplyToEither_normalCompletion2() {
1876 >        for (ExecutionMode m : ExecutionMode.values())
1877 >        for (Integer v1 : new Integer[] { 1, null })
1878 >        for (Integer v2 : new Integer[] { 2, null }) {
1879 >
1880 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1881 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1882 >        final IncFunction r = new IncFunction();
1883 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1884 >
1885 >        g.complete(v2);
1886 >        checkCompletedNormally(h, inc(v2));
1887 >        f.complete(v1);
1888 >
1889 >        checkCompletedNormally(f, v1);
1890 >        checkCompletedNormally(g, v2);
1891 >        checkCompletedNormally(h, inc(v2));
1892 >        }
1893 >    }
1894 >    public void testApplyToEither_normalCompletion3() {
1895 >        for (ExecutionMode m : ExecutionMode.values())
1896 >        for (Integer v1 : new Integer[] { 1, null })
1897 >        for (Integer v2 : new Integer[] { 2, null }) {
1898 >
1899 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1900 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1901 >        final IncFunction r = new IncFunction();
1902 >
1903 >        f.complete(v1);
1904 >        g.complete(v2);
1905 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1906 >
1907 >        checkCompletedNormally(f, v1);
1908 >        checkCompletedNormally(g, v2);
1909 >
1910 >        // unspecified behavior
1911 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1912 >                   Objects.equals(h.join(), inc(v2)));
1913 >        assertEquals(r.invocationCount, 1);
1914 >        }
1915      }
1916  
1917      /**
1918       * applyToEither result completes exceptionally after exceptional
1919       * completion of either source
1920       */
1921 <    public void testApplyToEither2() {
1922 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1923 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1264 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1265 <        f.completeExceptionally(new CFException());
1266 <        f2.complete(one);
1267 <        checkCompletedWithWrappedCFException(g);
1921 >    public void testApplyToEither_exceptionalCompletion1() {
1922 >        for (ExecutionMode m : ExecutionMode.values())
1923 >        for (Integer v1 : new Integer[] { 1, null }) {
1924  
1925 <        f = new CompletableFuture<>();
1926 <        f2 = new CompletableFuture<>();
1927 <        f2.completeExceptionally(new CFException());
1928 <        g = f.applyToEither(f2, inc);
1929 <        checkCompletedWithWrappedCFException(g);
1925 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1926 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1927 >        final IncFunction r = new IncFunction();
1928 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1929 >        final CFException ex = new CFException();
1930 >
1931 >        f.completeExceptionally(ex);
1932 >        checkCompletedWithWrappedCFException(h, ex);
1933 >        g.complete(v1);
1934 >
1935 >        assertEquals(r.invocationCount, 0);
1936 >        checkCompletedNormally(g, v1);
1937 >        checkCompletedWithWrappedCFException(f, ex);
1938 >        checkCompletedWithWrappedCFException(h, ex);
1939 >        }
1940 >    }
1941 >
1942 >    public void testApplyToEither_exceptionalCompletion2() {
1943 >        for (ExecutionMode m : ExecutionMode.values())
1944 >        for (Integer v1 : new Integer[] { 1, null }) {
1945 >
1946 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1947 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1948 >        final IncFunction r = new IncFunction();
1949 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1950 >        final CFException ex = new CFException();
1951 >
1952 >        g.completeExceptionally(ex);
1953 >        checkCompletedWithWrappedCFException(h, ex);
1954 >        f.complete(v1);
1955 >
1956 >        assertEquals(r.invocationCount, 0);
1957 >        checkCompletedNormally(f, v1);
1958 >        checkCompletedWithWrappedCFException(g, ex);
1959 >        checkCompletedWithWrappedCFException(h, ex);
1960 >        }
1961 >    }
1962 >
1963 >    public void testApplyToEither_exceptionalCompletion3() {
1964 >        for (ExecutionMode m : ExecutionMode.values())
1965 >        for (Integer v1 : new Integer[] { 1, null }) {
1966 >
1967 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1968 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1969 >        final IncFunction r = new IncFunction();
1970 >        final CFException ex = new CFException();
1971 >
1972 >        g.completeExceptionally(ex);
1973 >        f.complete(v1);
1974 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1975 >
1976 >        // unspecified behavior
1977 >        Integer v;
1978 >        try {
1979 >            assertEquals(h.join(), inc(v1));
1980 >            assertEquals(r.invocationCount, 1);
1981 >        } catch (CompletionException ok) {
1982 >            checkCompletedWithWrappedCFException(h, ex);
1983 >            assertEquals(r.invocationCount, 0);
1984 >        }
1985 >
1986 >        checkCompletedWithWrappedCFException(g, ex);
1987 >        checkCompletedNormally(f, v1);
1988 >        }
1989 >    }
1990 >
1991 >    public void testApplyToEither_exceptionalCompletion4() {
1992 >        for (ExecutionMode m : ExecutionMode.values())
1993 >        for (Integer v1 : new Integer[] { 1, null }) {
1994 >
1995 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1996 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1997 >        final IncFunction r = new IncFunction();
1998 >        final CFException ex = new CFException();
1999 >
2000 >        f.completeExceptionally(ex);
2001 >        g.complete(v1);
2002 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2003 >
2004 >        // unspecified behavior
2005 >        Integer v;
2006 >        try {
2007 >            assertEquals(h.join(), inc(v1));
2008 >            assertEquals(r.invocationCount, 1);
2009 >        } catch (CompletionException ok) {
2010 >            checkCompletedWithWrappedCFException(h, ex);
2011 >            assertEquals(r.invocationCount, 0);
2012 >        }
2013 >
2014 >        checkCompletedWithWrappedCFException(f, ex);
2015 >        checkCompletedNormally(g, v1);
2016 >        }
2017      }
2018  
2019      /**
2020       * applyToEither result completes exceptionally if action does
2021       */
2022 <    public void testApplyToEither3() {
2023 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2024 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2025 <        FailingFunction r = new FailingFunction();
2026 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2027 <        f2.complete(two);
2028 <        checkCompletedWithWrappedCFException(g);
2022 >    public void testApplyToEither_actionFailed1() {
2023 >        for (ExecutionMode m : ExecutionMode.values())
2024 >        for (Integer v1 : new Integer[] { 1, null })
2025 >        for (Integer v2 : new Integer[] { 2, null }) {
2026 >
2027 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2028 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2029 >        final FailingFunction r = new FailingFunction();
2030 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2031 >
2032 >        f.complete(v1);
2033 >        checkCompletedWithWrappedCFException(h);
2034 >        g.complete(v2);
2035 >        checkCompletedNormally(f, v1);
2036 >        checkCompletedNormally(g, v2);
2037 >        }
2038 >    }
2039 >
2040 >    public void testApplyToEither_actionFailed2() {
2041 >        for (ExecutionMode m : ExecutionMode.values())
2042 >        for (Integer v1 : new Integer[] { 1, null })
2043 >        for (Integer v2 : new Integer[] { 2, null }) {
2044 >
2045 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2046 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 >        final FailingFunction r = new FailingFunction();
2048 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2049 >
2050 >        g.complete(v2);
2051 >        checkCompletedWithWrappedCFException(h);
2052 >        f.complete(v1);
2053 >        checkCompletedNormally(f, v1);
2054 >        checkCompletedNormally(g, v2);
2055 >        }
2056      }
2057  
2058      /**
2059       * applyToEither result completes exceptionally if either source cancelled
2060       */
2061 <    public void testApplyToEither4() {
2062 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2063 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2064 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2065 <        assertTrue(f.cancel(true));
2066 <        checkCompletedWithWrappedCancellationException(g);
2067 <        f = new CompletableFuture<>();
2068 <        f2 = new CompletableFuture<>();
2069 <        assertTrue(f2.cancel(true));
2070 <        checkCompletedWithWrappedCancellationException(g);
2061 >    public void testApplyToEither_sourceCancelled1() {
2062 >        for (ExecutionMode m : ExecutionMode.values())
2063 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2064 >        for (Integer v1 : new Integer[] { 1, null }) {
2065 >
2066 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2067 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2068 >        final IncFunction r = new IncFunction();
2069 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2070 >
2071 >        assertTrue(f.cancel(mayInterruptIfRunning));
2072 >        checkCompletedWithWrappedCancellationException(h);
2073 >        g.complete(v1);
2074 >
2075 >        checkCancelled(f);
2076 >        assertEquals(r.invocationCount, 0);
2077 >        checkCompletedNormally(g, v1);
2078 >        checkCompletedWithWrappedCancellationException(h);
2079 >        }
2080 >    }
2081 >
2082 >    public void testApplyToEither_sourceCancelled2() {
2083 >        for (ExecutionMode m : ExecutionMode.values())
2084 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2085 >        for (Integer v1 : new Integer[] { 1, null }) {
2086 >
2087 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2088 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2089 >        final IncFunction r = new IncFunction();
2090 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 >
2092 >        assertTrue(g.cancel(mayInterruptIfRunning));
2093 >        checkCompletedWithWrappedCancellationException(h);
2094 >        f.complete(v1);
2095 >
2096 >        checkCancelled(g);
2097 >        assertEquals(r.invocationCount, 0);
2098 >        checkCompletedNormally(f, v1);
2099 >        checkCompletedWithWrappedCancellationException(h);
2100 >        }
2101 >    }
2102 >
2103 >    public void testApplyToEither_sourceCancelled3() {
2104 >        for (ExecutionMode m : ExecutionMode.values())
2105 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2106 >        for (Integer v1 : new Integer[] { 1, null }) {
2107 >
2108 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2109 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2110 >        final IncFunction r = new IncFunction();
2111 >
2112 >        assertTrue(g.cancel(mayInterruptIfRunning));
2113 >        f.complete(v1);
2114 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2115 >
2116 >        // unspecified behavior
2117 >        Integer v;
2118 >        try {
2119 >            assertEquals(h.join(), inc(v1));
2120 >            assertEquals(r.invocationCount, 1);
2121 >        } catch (CompletionException ok) {
2122 >            checkCompletedWithWrappedCancellationException(h);
2123 >            assertEquals(r.invocationCount, 0);
2124 >        }
2125 >
2126 >        checkCancelled(g);
2127 >        checkCompletedNormally(f, v1);
2128 >        }
2129 >    }
2130 >
2131 >    public void testApplyToEither_sourceCancelled4() {
2132 >        for (ExecutionMode m : ExecutionMode.values())
2133 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2134 >        for (Integer v1 : new Integer[] { 1, null }) {
2135 >
2136 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2137 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2138 >        final IncFunction r = new IncFunction();
2139 >
2140 >        assertTrue(f.cancel(mayInterruptIfRunning));
2141 >        g.complete(v1);
2142 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2143 >
2144 >        // unspecified behavior
2145 >        Integer v;
2146 >        try {
2147 >            assertEquals(h.join(), inc(v1));
2148 >            assertEquals(r.invocationCount, 1);
2149 >        } catch (CompletionException ok) {
2150 >            checkCompletedWithWrappedCancellationException(h);
2151 >            assertEquals(r.invocationCount, 0);
2152 >        }
2153 >
2154 >        checkCancelled(f);
2155 >        checkCompletedNormally(g, v1);
2156 >        }
2157      }
2158  
2159      /**
2160       * acceptEither result completes normally after normal completion
2161       * of either source
2162       */
2163 <    public void testAcceptEither() {
2164 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2165 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2166 <        IncAction r = new IncAction();
1311 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1312 <        f.complete(one);
1313 <        checkCompletedNormally(g, null);
1314 <        f2.complete(one);
1315 <        checkCompletedNormally(g, null);
1316 <        assertEquals(r.value, 2);
2163 >    public void testAcceptEither_normalCompletion1() {
2164 >        for (ExecutionMode m : ExecutionMode.values())
2165 >        for (Integer v1 : new Integer[] { 1, null })
2166 >        for (Integer v2 : new Integer[] { 2, null }) {
2167  
2168 <        r = new IncAction();
2169 <        f = new CompletableFuture<>();
2170 <        f.complete(one);
2171 <        f2 = new CompletableFuture<>();
2172 <        g = f.acceptEither(f2, r);
2173 <        checkCompletedNormally(g, null);
2174 <        assertEquals(r.value, 2);
2168 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2169 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2170 >        final IncAction r = new IncAction();
2171 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2172 >
2173 >        f.complete(v1);
2174 >        checkCompletedNormally(h, null);
2175 >        assertEquals(r.value, inc(v1));
2176 >        g.complete(v2);
2177 >
2178 >        checkCompletedNormally(f, v1);
2179 >        checkCompletedNormally(g, v2);
2180 >        checkCompletedNormally(h, null);
2181 >        }
2182 >    }
2183 >
2184 >    public void testAcceptEither_normalCompletion2() {
2185 >        for (ExecutionMode m : ExecutionMode.values())
2186 >        for (Integer v1 : new Integer[] { 1, null })
2187 >        for (Integer v2 : new Integer[] { 2, null }) {
2188 >
2189 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2190 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2191 >        final IncAction r = new IncAction();
2192 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2193 >
2194 >        g.complete(v2);
2195 >        checkCompletedNormally(h, null);
2196 >        assertEquals(r.value, inc(v2));
2197 >        f.complete(v1);
2198 >
2199 >        checkCompletedNormally(f, v1);
2200 >        checkCompletedNormally(g, v2);
2201 >        checkCompletedNormally(h, null);
2202 >        }
2203 >    }
2204 >    public void testAcceptEither_normalCompletion3() {
2205 >        for (ExecutionMode m : ExecutionMode.values())
2206 >        for (Integer v1 : new Integer[] { 1, null })
2207 >        for (Integer v2 : new Integer[] { 2, null }) {
2208 >
2209 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2210 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2211 >        final IncAction r = new IncAction();
2212 >
2213 >        f.complete(v1);
2214 >        g.complete(v2);
2215 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2216 >
2217 >        checkCompletedNormally(h, null);
2218 >        checkCompletedNormally(f, v1);
2219 >        checkCompletedNormally(g, v2);
2220 >
2221 >        // unspecified behavior
2222 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2223 >                   Objects.equals(r.value, inc(v2)));
2224 >        }
2225      }
2226  
2227      /**
2228       * acceptEither result completes exceptionally after exceptional
2229       * completion of either source
2230       */
2231 <    public void testAcceptEither2() {
2232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2233 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1334 <        IncAction r = new IncAction();
1335 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1336 <        f.completeExceptionally(new CFException());
1337 <        f2.complete(one);
1338 <        checkCompletedWithWrappedCFException(g);
2231 >    public void testAcceptEither_exceptionalCompletion1() {
2232 >        for (ExecutionMode m : ExecutionMode.values())
2233 >        for (Integer v1 : new Integer[] { 1, null }) {
2234  
2235 <        r = new IncAction();
2236 <        f = new CompletableFuture<>();
2237 <        f2 = new CompletableFuture<>();
2238 <        f2.completeExceptionally(new CFException());
2239 <        g = f.acceptEither(f2, r);
2240 <        checkCompletedWithWrappedCFException(g);
2235 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2236 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2237 >        final IncAction r = new IncAction();
2238 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2239 >        final CFException ex = new CFException();
2240 >
2241 >        f.completeExceptionally(ex);
2242 >        checkCompletedWithWrappedCFException(h, ex);
2243 >        g.complete(v1);
2244 >
2245 >        assertEquals(r.invocationCount, 0);
2246 >        checkCompletedNormally(g, v1);
2247 >        checkCompletedWithWrappedCFException(f, ex);
2248 >        checkCompletedWithWrappedCFException(h, ex);
2249 >        }
2250 >    }
2251 >
2252 >    public void testAcceptEither_exceptionalCompletion2() {
2253 >        for (ExecutionMode m : ExecutionMode.values())
2254 >        for (Integer v1 : new Integer[] { 1, null }) {
2255 >
2256 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2257 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2258 >        final IncAction r = new IncAction();
2259 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2260 >        final CFException ex = new CFException();
2261 >
2262 >        g.completeExceptionally(ex);
2263 >        checkCompletedWithWrappedCFException(h, ex);
2264 >        f.complete(v1);
2265 >
2266 >        assertEquals(r.invocationCount, 0);
2267 >        checkCompletedNormally(f, v1);
2268 >        checkCompletedWithWrappedCFException(g, ex);
2269 >        checkCompletedWithWrappedCFException(h, ex);
2270 >        }
2271 >    }
2272 >
2273 >    public void testAcceptEither_exceptionalCompletion3() {
2274 >        for (ExecutionMode m : ExecutionMode.values())
2275 >        for (Integer v1 : new Integer[] { 1, null }) {
2276 >
2277 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2278 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2279 >        final IncAction r = new IncAction();
2280 >        final CFException ex = new CFException();
2281 >
2282 >        g.completeExceptionally(ex);
2283 >        f.complete(v1);
2284 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2285 >
2286 >        // unspecified behavior
2287 >        Integer v;
2288 >        try {
2289 >            assertEquals(h.join(), null);
2290 >            assertEquals(r.invocationCount, 1);
2291 >            assertEquals(inc(v1), r.value);
2292 >        } catch (CompletionException ok) {
2293 >            checkCompletedWithWrappedCFException(h, ex);
2294 >            assertEquals(r.invocationCount, 0);
2295 >        }
2296 >
2297 >        checkCompletedWithWrappedCFException(g, ex);
2298 >        checkCompletedNormally(f, v1);
2299 >        }
2300 >    }
2301 >
2302 >    public void testAcceptEither_exceptionalCompletion4() {
2303 >        for (ExecutionMode m : ExecutionMode.values())
2304 >        for (Integer v1 : new Integer[] { 1, null }) {
2305 >
2306 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2307 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2308 >        final IncAction r = new IncAction();
2309 >        final CFException ex = new CFException();
2310 >
2311 >        f.completeExceptionally(ex);
2312 >        g.complete(v1);
2313 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2314 >
2315 >        // unspecified behavior
2316 >        Integer v;
2317 >        try {
2318 >            assertEquals(h.join(), null);
2319 >            assertEquals(r.invocationCount, 1);
2320 >            assertEquals(inc(v1), r.value);
2321 >        } catch (CompletionException ok) {
2322 >            checkCompletedWithWrappedCFException(h, ex);
2323 >            assertEquals(r.invocationCount, 0);
2324 >        }
2325 >
2326 >        checkCompletedWithWrappedCFException(f, ex);
2327 >        checkCompletedNormally(g, v1);
2328 >        }
2329      }
2330  
2331      /**
2332       * acceptEither result completes exceptionally if action does
2333       */
2334 <    public void testAcceptEither3() {
2335 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2336 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2337 <        FailingConsumer r = new FailingConsumer();
2338 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2339 <        f2.complete(two);
2340 <        checkCompletedWithWrappedCFException(g);
2334 >    public void testAcceptEither_actionFailed1() {
2335 >        for (ExecutionMode m : ExecutionMode.values())
2336 >        for (Integer v1 : new Integer[] { 1, null })
2337 >        for (Integer v2 : new Integer[] { 2, null }) {
2338 >
2339 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2340 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2341 >        final FailingConsumer r = new FailingConsumer();
2342 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2343 >
2344 >        f.complete(v1);
2345 >        checkCompletedWithWrappedCFException(h);
2346 >        g.complete(v2);
2347 >        checkCompletedNormally(f, v1);
2348 >        checkCompletedNormally(g, v2);
2349 >        }
2350 >    }
2351 >
2352 >    public void testAcceptEither_actionFailed2() {
2353 >        for (ExecutionMode m : ExecutionMode.values())
2354 >        for (Integer v1 : new Integer[] { 1, null })
2355 >        for (Integer v2 : new Integer[] { 2, null }) {
2356 >
2357 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2358 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2359 >        final FailingConsumer r = new FailingConsumer();
2360 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2361 >
2362 >        g.complete(v2);
2363 >        checkCompletedWithWrappedCFException(h);
2364 >        f.complete(v1);
2365 >        checkCompletedNormally(f, v1);
2366 >        checkCompletedNormally(g, v2);
2367 >        }
2368      }
2369  
2370      /**
2371       * acceptEither result completes exceptionally if either source cancelled
2372       */
2373 <    public void testAcceptEither4() {
2374 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2375 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2376 <        IncAction r = new IncAction();
2377 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2378 <        assertTrue(f.cancel(true));
2379 <        checkCompletedWithWrappedCancellationException(g);
2380 <        f = new CompletableFuture<>();
2381 <        f2 = new CompletableFuture<>();
2382 <        assertTrue(f2.cancel(true));
2383 <        checkCompletedWithWrappedCancellationException(g);
2373 >    public void testAcceptEither_sourceCancelled1() {
2374 >        for (ExecutionMode m : ExecutionMode.values())
2375 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2376 >        for (Integer v1 : new Integer[] { 1, null }) {
2377 >
2378 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2379 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 >        final IncAction r = new IncAction();
2381 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2382 >
2383 >        assertTrue(f.cancel(mayInterruptIfRunning));
2384 >        checkCompletedWithWrappedCancellationException(h);
2385 >        g.complete(v1);
2386 >
2387 >        checkCancelled(f);
2388 >        assertEquals(r.invocationCount, 0);
2389 >        checkCompletedNormally(g, v1);
2390 >        checkCompletedWithWrappedCancellationException(h);
2391 >        }
2392 >    }
2393 >
2394 >    public void testAcceptEither_sourceCancelled2() {
2395 >        for (ExecutionMode m : ExecutionMode.values())
2396 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2397 >        for (Integer v1 : new Integer[] { 1, null }) {
2398 >
2399 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2400 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2401 >        final IncAction r = new IncAction();
2402 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2403 >
2404 >        assertTrue(g.cancel(mayInterruptIfRunning));
2405 >        checkCompletedWithWrappedCancellationException(h);
2406 >        f.complete(v1);
2407 >
2408 >        checkCancelled(g);
2409 >        assertEquals(r.invocationCount, 0);
2410 >        checkCompletedNormally(f, v1);
2411 >        checkCompletedWithWrappedCancellationException(h);
2412 >        }
2413 >    }
2414 >
2415 >    public void testAcceptEither_sourceCancelled3() {
2416 >        for (ExecutionMode m : ExecutionMode.values())
2417 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2418 >        for (Integer v1 : new Integer[] { 1, null }) {
2419 >
2420 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2421 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2422 >        final IncAction r = new IncAction();
2423 >
2424 >        assertTrue(g.cancel(mayInterruptIfRunning));
2425 >        f.complete(v1);
2426 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2427 >
2428 >        // unspecified behavior
2429 >        Integer v;
2430 >        try {
2431 >            assertEquals(h.join(), null);
2432 >            assertEquals(r.invocationCount, 1);
2433 >            assertEquals(inc(v1), r.value);
2434 >        } catch (CompletionException ok) {
2435 >            checkCompletedWithWrappedCancellationException(h);
2436 >            assertEquals(r.invocationCount, 0);
2437 >        }
2438 >
2439 >        checkCancelled(g);
2440 >        checkCompletedNormally(f, v1);
2441 >        }
2442 >    }
2443 >
2444 >    public void testAcceptEither_sourceCancelled4() {
2445 >        for (ExecutionMode m : ExecutionMode.values())
2446 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2447 >        for (Integer v1 : new Integer[] { 1, null }) {
2448 >
2449 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2450 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2451 >        final IncAction r = new IncAction();
2452 >
2453 >        assertTrue(f.cancel(mayInterruptIfRunning));
2454 >        g.complete(v1);
2455 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456 >
2457 >        // unspecified behavior
2458 >        Integer v;
2459 >        try {
2460 >            assertEquals(h.join(), null);
2461 >            assertEquals(r.invocationCount, 1);
2462 >            assertEquals(inc(v1), r.value);
2463 >        } catch (CompletionException ok) {
2464 >            checkCompletedWithWrappedCancellationException(h);
2465 >            assertEquals(r.invocationCount, 0);
2466 >        }
2467 >
2468 >        checkCancelled(f);
2469 >        checkCompletedNormally(g, v1);
2470 >        }
2471      }
2472  
2473      /**
2474       * runAfterEither result completes normally after normal completion
2475       * of either source
2476       */
2477 <    public void testRunAfterEither() {
2478 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2479 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2480 <        Noop r = new Noop();
1384 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1385 <        f.complete(one);
1386 <        checkCompletedNormally(g, null);
1387 <        f2.complete(one);
1388 <        checkCompletedNormally(g, null);
1389 <        assertTrue(r.ran);
2477 >    public void testRunAfterEither_normalCompletion1() {
2478 >        for (ExecutionMode m : ExecutionMode.values())
2479 >        for (Integer v1 : new Integer[] { 1, null })
2480 >        for (Integer v2 : new Integer[] { 2, null }) {
2481  
2482 <        r = new Noop();
2483 <        f = new CompletableFuture<>();
2484 <        f.complete(one);
2485 <        f2 = new CompletableFuture<>();
2486 <        g = f.runAfterEither(f2, r);
2487 <        checkCompletedNormally(g, null);
2488 <        assertTrue(r.ran);
2482 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2483 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2484 >        final Noop r = new Noop();
2485 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2486 >
2487 >        f.complete(v1);
2488 >        checkCompletedNormally(h, null);
2489 >        assertEquals(r.invocationCount, 1);
2490 >        g.complete(v2);
2491 >
2492 >        checkCompletedNormally(f, v1);
2493 >        checkCompletedNormally(g, v2);
2494 >        checkCompletedNormally(h, null);
2495 >        assertEquals(r.invocationCount, 1);
2496 >        }
2497 >    }
2498 >
2499 >    public void testRunAfterEither_normalCompletion2() {
2500 >        for (ExecutionMode m : ExecutionMode.values())
2501 >        for (Integer v1 : new Integer[] { 1, null })
2502 >        for (Integer v2 : new Integer[] { 2, null }) {
2503 >
2504 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2505 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2506 >        final Noop r = new Noop();
2507 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2508 >
2509 >        g.complete(v2);
2510 >        checkCompletedNormally(h, null);
2511 >        assertEquals(r.invocationCount, 1);
2512 >        f.complete(v1);
2513 >
2514 >        checkCompletedNormally(f, v1);
2515 >        checkCompletedNormally(g, v2);
2516 >        checkCompletedNormally(h, null);
2517 >        assertEquals(r.invocationCount, 1);
2518 >        }
2519 >    }
2520 >    public void testRunAfterEither_normalCompletion3() {
2521 >        for (ExecutionMode m : ExecutionMode.values())
2522 >        for (Integer v1 : new Integer[] { 1, null })
2523 >        for (Integer v2 : new Integer[] { 2, null }) {
2524 >
2525 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2526 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2527 >        final Noop r = new Noop();
2528 >
2529 >        f.complete(v1);
2530 >        g.complete(v2);
2531 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2532 >
2533 >        checkCompletedNormally(h, null);
2534 >        checkCompletedNormally(f, v1);
2535 >        checkCompletedNormally(g, v2);
2536 >        assertEquals(r.invocationCount, 1);
2537 >        }
2538      }
2539  
2540      /**
2541       * runAfterEither result completes exceptionally after exceptional
2542       * completion of either source
2543       */
2544 <    public void testRunAfterEither2() {
2545 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2546 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1407 <        Noop r = new Noop();
1408 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1409 <        f.completeExceptionally(new CFException());
1410 <        f2.complete(one);
1411 <        checkCompletedWithWrappedCFException(g);
2544 >    public void testRunAfterEither_exceptionalCompletion1() {
2545 >        for (ExecutionMode m : ExecutionMode.values())
2546 >        for (Integer v1 : new Integer[] { 1, null }) {
2547  
2548 <        r = new Noop();
2549 <        f = new CompletableFuture<>();
2550 <        f2 = new CompletableFuture<>();
2551 <        f2.completeExceptionally(new CFException());
2552 <        g = f.runAfterEither(f2, r);
2553 <        checkCompletedWithWrappedCFException(g);
2548 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2549 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2550 >        final Noop r = new Noop();
2551 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2552 >        final CFException ex = new CFException();
2553 >
2554 >        f.completeExceptionally(ex);
2555 >        checkCompletedWithWrappedCFException(h, ex);
2556 >        g.complete(v1);
2557 >
2558 >        assertEquals(r.invocationCount, 0);
2559 >        checkCompletedNormally(g, v1);
2560 >        checkCompletedWithWrappedCFException(f, ex);
2561 >        checkCompletedWithWrappedCFException(h, ex);
2562 >        }
2563 >    }
2564 >
2565 >    public void testRunAfterEither_exceptionalCompletion2() {
2566 >        for (ExecutionMode m : ExecutionMode.values())
2567 >        for (Integer v1 : new Integer[] { 1, null }) {
2568 >
2569 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2570 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2571 >        final Noop r = new Noop();
2572 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2573 >        final CFException ex = new CFException();
2574 >
2575 >        g.completeExceptionally(ex);
2576 >        checkCompletedWithWrappedCFException(h, ex);
2577 >        f.complete(v1);
2578 >
2579 >        assertEquals(r.invocationCount, 0);
2580 >        checkCompletedNormally(f, v1);
2581 >        checkCompletedWithWrappedCFException(g, ex);
2582 >        checkCompletedWithWrappedCFException(h, ex);
2583 >        }
2584 >    }
2585 >
2586 >    public void testRunAfterEither_exceptionalCompletion3() {
2587 >        for (ExecutionMode m : ExecutionMode.values())
2588 >        for (Integer v1 : new Integer[] { 1, null }) {
2589 >
2590 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2591 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2592 >        final Noop r = new Noop();
2593 >        final CFException ex = new CFException();
2594 >
2595 >        g.completeExceptionally(ex);
2596 >        f.complete(v1);
2597 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2598 >
2599 >        // unspecified behavior
2600 >        Integer v;
2601 >        try {
2602 >            assertEquals(h.join(), null);
2603 >            assertEquals(r.invocationCount, 1);
2604 >        } catch (CompletionException ok) {
2605 >            checkCompletedWithWrappedCFException(h, ex);
2606 >            assertEquals(r.invocationCount, 0);
2607 >        }
2608 >
2609 >        checkCompletedWithWrappedCFException(g, ex);
2610 >        checkCompletedNormally(f, v1);
2611 >        }
2612 >    }
2613 >
2614 >    public void testRunAfterEither_exceptionalCompletion4() {
2615 >        for (ExecutionMode m : ExecutionMode.values())
2616 >        for (Integer v1 : new Integer[] { 1, null }) {
2617 >
2618 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2619 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2620 >        final Noop r = new Noop();
2621 >        final CFException ex = new CFException();
2622 >
2623 >        f.completeExceptionally(ex);
2624 >        g.complete(v1);
2625 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2626 >
2627 >        // unspecified behavior
2628 >        Integer v;
2629 >        try {
2630 >            assertEquals(h.join(), null);
2631 >            assertEquals(r.invocationCount, 1);
2632 >        } catch (CompletionException ok) {
2633 >            checkCompletedWithWrappedCFException(h, ex);
2634 >            assertEquals(r.invocationCount, 0);
2635 >        }
2636 >
2637 >        checkCompletedWithWrappedCFException(f, ex);
2638 >        checkCompletedNormally(g, v1);
2639 >        }
2640      }
2641  
2642      /**
2643       * runAfterEither result completes exceptionally if action does
2644       */
2645 <    public void testRunAfterEither3() {
2646 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2647 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2648 <        FailingNoop r = new FailingNoop();
2649 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2650 <        f2.complete(two);
2651 <        checkCompletedWithWrappedCFException(g);
2645 >    public void testRunAfterEither_actionFailed1() {
2646 >        for (ExecutionMode m : ExecutionMode.values())
2647 >        for (Integer v1 : new Integer[] { 1, null })
2648 >        for (Integer v2 : new Integer[] { 2, null }) {
2649 >
2650 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2651 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2652 >        final FailingNoop r = new FailingNoop();
2653 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2654 >
2655 >        f.complete(v1);
2656 >        checkCompletedWithWrappedCFException(h);
2657 >        g.complete(v2);
2658 >        checkCompletedNormally(f, v1);
2659 >        checkCompletedNormally(g, v2);
2660 >        }
2661 >    }
2662 >
2663 >    public void testRunAfterEither_actionFailed2() {
2664 >        for (ExecutionMode m : ExecutionMode.values())
2665 >        for (Integer v1 : new Integer[] { 1, null })
2666 >        for (Integer v2 : new Integer[] { 2, null }) {
2667 >
2668 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2669 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2670 >        final FailingNoop r = new FailingNoop();
2671 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2672 >
2673 >        g.complete(v2);
2674 >        checkCompletedWithWrappedCFException(h);
2675 >        f.complete(v1);
2676 >        checkCompletedNormally(f, v1);
2677 >        checkCompletedNormally(g, v2);
2678 >        }
2679      }
2680  
2681      /**
2682       * runAfterEither result completes exceptionally if either source cancelled
2683       */
2684 <    public void testRunAfterEither4() {
2685 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2686 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2687 <        Noop r = new Noop();
2688 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2689 <        assertTrue(f.cancel(true));
2690 <        checkCompletedWithWrappedCancellationException(g);
2691 <        f = new CompletableFuture<>();
2692 <        f2 = new CompletableFuture<>();
2693 <        assertTrue(f2.cancel(true));
2694 <        checkCompletedWithWrappedCancellationException(g);
2684 >    public void testRunAfterEither_sourceCancelled1() {
2685 >        for (ExecutionMode m : ExecutionMode.values())
2686 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2687 >        for (Integer v1 : new Integer[] { 1, null }) {
2688 >
2689 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2690 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2691 >        final Noop r = new Noop();
2692 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2693 >
2694 >        assertTrue(f.cancel(mayInterruptIfRunning));
2695 >        checkCompletedWithWrappedCancellationException(h);
2696 >        g.complete(v1);
2697 >
2698 >        checkCancelled(f);
2699 >        assertEquals(r.invocationCount, 0);
2700 >        checkCompletedNormally(g, v1);
2701 >        checkCompletedWithWrappedCancellationException(h);
2702 >        }
2703 >    }
2704 >
2705 >    public void testRunAfterEither_sourceCancelled2() {
2706 >        for (ExecutionMode m : ExecutionMode.values())
2707 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2708 >        for (Integer v1 : new Integer[] { 1, null }) {
2709 >
2710 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2712 >        final Noop r = new Noop();
2713 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 >
2715 >        assertTrue(g.cancel(mayInterruptIfRunning));
2716 >        checkCompletedWithWrappedCancellationException(h);
2717 >        f.complete(v1);
2718 >
2719 >        checkCancelled(g);
2720 >        assertEquals(r.invocationCount, 0);
2721 >        checkCompletedNormally(f, v1);
2722 >        checkCompletedWithWrappedCancellationException(h);
2723 >        }
2724 >    }
2725 >
2726 >    public void testRunAfterEither_sourceCancelled3() {
2727 >        for (ExecutionMode m : ExecutionMode.values())
2728 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2729 >        for (Integer v1 : new Integer[] { 1, null }) {
2730 >
2731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733 >        final Noop r = new Noop();
2734 >
2735 >        assertTrue(g.cancel(mayInterruptIfRunning));
2736 >        f.complete(v1);
2737 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2738 >
2739 >        // unspecified behavior
2740 >        Integer v;
2741 >        try {
2742 >            assertEquals(h.join(), null);
2743 >            assertEquals(r.invocationCount, 1);
2744 >        } catch (CompletionException ok) {
2745 >            checkCompletedWithWrappedCancellationException(h);
2746 >            assertEquals(r.invocationCount, 0);
2747 >        }
2748 >
2749 >        checkCancelled(g);
2750 >        checkCompletedNormally(f, v1);
2751 >        }
2752 >    }
2753 >
2754 >    public void testRunAfterEither_sourceCancelled4() {
2755 >        for (ExecutionMode m : ExecutionMode.values())
2756 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2757 >        for (Integer v1 : new Integer[] { 1, null }) {
2758 >
2759 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2760 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2761 >        final Noop r = new Noop();
2762 >
2763 >        assertTrue(f.cancel(mayInterruptIfRunning));
2764 >        g.complete(v1);
2765 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2766 >
2767 >        // unspecified behavior
2768 >        Integer v;
2769 >        try {
2770 >            assertEquals(h.join(), null);
2771 >            assertEquals(r.invocationCount, 1);
2772 >        } catch (CompletionException ok) {
2773 >            checkCompletedWithWrappedCancellationException(h);
2774 >            assertEquals(r.invocationCount, 0);
2775 >        }
2776 >
2777 >        checkCancelled(f);
2778 >        checkCompletedNormally(g, v1);
2779 >        }
2780      }
2781  
2782      /**
# Line 1631 | Line 2964 | public class CompletableFutureTest exten
2964          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2965          f.complete(one);
2966          checkCompletedNormally(g, null);
2967 <        assertEquals(r.value, 2);
2967 >        assertEquals(r.value, (Integer) 2);
2968      }
2969  
2970      /**
# Line 1669 | Line 3002 | public class CompletableFutureTest exten
3002      }
3003  
3004      /**
1672     * thenCombineAsync result completes normally after normal
1673     * completion of sources
1674     */
1675    public void testThenCombineAsync() {
1676        CompletableFuture<Integer> f, g, h;
1677
1678        f = new CompletableFuture<>();
1679        g = new CompletableFuture<>();
1680        h = f.thenCombineAsync(g, subtract);
1681        f.complete(3);
1682        checkIncomplete(h);
1683        g.complete(1);
1684        checkCompletedNormally(h, 2);
1685
1686        f = new CompletableFuture<>();
1687        g = new CompletableFuture<>();
1688        h = f.thenCombineAsync(g, subtract);
1689        g.complete(1);
1690        checkIncomplete(h);
1691        f.complete(3);
1692        checkCompletedNormally(h, 2);
1693
1694        f = new CompletableFuture<>();
1695        g = new CompletableFuture<>();
1696        g.complete(1);
1697        f.complete(3);
1698        h = f.thenCombineAsync(g, subtract);
1699        checkCompletedNormally(h, 2);
1700    }
1701
1702    /**
1703     * thenCombineAsync result completes exceptionally after exceptional
1704     * completion of either source
1705     */
1706    public void testThenCombineAsync2() {
1707        CompletableFuture<Integer> f, g, h;
1708
1709        f = new CompletableFuture<>();
1710        g = new CompletableFuture<>();
1711        h = f.thenCombineAsync(g, subtract);
1712        f.completeExceptionally(new CFException());
1713        checkIncomplete(h);
1714        g.complete(1);
1715        checkCompletedWithWrappedCFException(h);
1716
1717        f = new CompletableFuture<>();
1718        g = new CompletableFuture<>();
1719        h = f.thenCombineAsync(g, subtract);
1720        g.completeExceptionally(new CFException());
1721        checkIncomplete(h);
1722        f.complete(3);
1723        checkCompletedWithWrappedCFException(h);
1724
1725        f = new CompletableFuture<>();
1726        g = new CompletableFuture<>();
1727        g.completeExceptionally(new CFException());
1728        f.complete(3);
1729        h = f.thenCombineAsync(g, subtract);
1730        checkCompletedWithWrappedCFException(h);
1731    }
1732
1733    /**
1734     * thenCombineAsync result completes exceptionally if action does
1735     */
1736    public void testThenCombineAsync3() {
1737        CompletableFuture<Integer> f = new CompletableFuture<>();
1738        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1739        FailingBiFunction r = new FailingBiFunction();
1740        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1741        f.complete(one);
1742        checkIncomplete(g);
1743        assertFalse(r.ran);
1744        f2.complete(two);
1745        checkCompletedWithWrappedCFException(g);
1746        assertTrue(r.ran);
1747    }
1748
1749    /**
1750     * thenCombineAsync result completes exceptionally if either source cancelled
1751     */
1752    public void testThenCombineAsync4() {
1753        CompletableFuture<Integer> f, g, h;
1754
1755        f = new CompletableFuture<>();
1756        g = new CompletableFuture<>();
1757        h = f.thenCombineAsync(g, subtract);
1758        assertTrue(f.cancel(true));
1759        checkIncomplete(h);
1760        g.complete(1);
1761        checkCompletedWithWrappedCancellationException(h);
1762
1763        f = new CompletableFuture<>();
1764        g = new CompletableFuture<>();
1765        h = f.thenCombineAsync(g, subtract);
1766        assertTrue(g.cancel(true));
1767        checkIncomplete(h);
1768        f.complete(3);
1769        checkCompletedWithWrappedCancellationException(h);
1770
1771        f = new CompletableFuture<>();
1772        g = new CompletableFuture<>();
1773        g.complete(3);
1774        assertTrue(f.cancel(true));
1775        h = f.thenCombineAsync(g, subtract);
1776        checkCompletedWithWrappedCancellationException(h);
1777
1778        f = new CompletableFuture<>();
1779        g = new CompletableFuture<>();
1780        f.complete(3);
1781        assertTrue(g.cancel(true));
1782        h = f.thenCombineAsync(g, subtract);
1783        checkCompletedWithWrappedCancellationException(h);
1784    }
1785
1786    /**
1787     * thenAcceptBothAsync result completes normally after normal
1788     * completion of sources
1789     */
1790    public void testThenAcceptBothAsync() {
1791        CompletableFuture<Integer> f, g;
1792        CompletableFuture<Void> h;
1793        SubtractAction r;
1794
1795        f = new CompletableFuture<>();
1796        g = new CompletableFuture<>();
1797        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1798        f.complete(3);
1799        checkIncomplete(h);
1800        g.complete(1);
1801        checkCompletedNormally(h, null);
1802        assertEquals(r.value, 2);
1803
1804        f = new CompletableFuture<>();
1805        g = new CompletableFuture<>();
1806        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1807        g.complete(1);
1808        checkIncomplete(h);
1809        f.complete(3);
1810        checkCompletedNormally(h, null);
1811        assertEquals(r.value, 2);
1812
1813        f = new CompletableFuture<>();
1814        g = new CompletableFuture<>();
1815        g.complete(1);
1816        f.complete(3);
1817        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1818        checkCompletedNormally(h, null);
1819        assertEquals(r.value, 2);
1820    }
1821
1822    /**
1823     * thenAcceptBothAsync result completes exceptionally after exceptional
1824     * completion of source
1825     */
1826    public void testThenAcceptBothAsync2() {
1827        CompletableFuture<Integer> f, g;
1828        CompletableFuture<Void> h;
1829        SubtractAction r;
1830
1831        f = new CompletableFuture<>();
1832        g = new CompletableFuture<>();
1833        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1834        f.completeExceptionally(new CFException());
1835        checkIncomplete(h);
1836        g.complete(1);
1837        checkCompletedWithWrappedCFException(h);
1838
1839        f = new CompletableFuture<>();
1840        g = new CompletableFuture<>();
1841        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842        g.completeExceptionally(new CFException());
1843        checkIncomplete(h);
1844        f.complete(3);
1845        checkCompletedWithWrappedCFException(h);
1846
1847        f = new CompletableFuture<>();
1848        g = new CompletableFuture<>();
1849        f.complete(3);
1850        g.completeExceptionally(new CFException());
1851        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1852        checkCompletedWithWrappedCFException(h);
1853
1854        f = new CompletableFuture<>();
1855        g = new CompletableFuture<>();
1856        f.completeExceptionally(new CFException());
1857        g.complete(3);
1858        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1859        checkCompletedWithWrappedCFException(h);
1860    }
1861
1862    /**
1863     * thenAcceptBothAsync result completes exceptionally if action does
1864     */
1865    public void testThenAcceptBothAsync3() {
1866        CompletableFuture<Integer> f, g;
1867        CompletableFuture<Void> h;
1868        FailingBiConsumer r;
1869
1870        f = new CompletableFuture<>();
1871        g = new CompletableFuture<>();
1872        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1873        f.complete(3);
1874        checkIncomplete(h);
1875        g.complete(1);
1876        checkCompletedWithWrappedCFException(h);
1877
1878        f = new CompletableFuture<>();
1879        g = new CompletableFuture<>();
1880        f.complete(3);
1881        g.complete(1);
1882        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1883        checkCompletedWithWrappedCFException(h);
1884    }
1885
1886    /**
1887     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1888     */
1889    public void testThenAcceptBothAsync4() {
1890        CompletableFuture<Integer> f, g;
1891        CompletableFuture<Void> h;
1892        SubtractAction r;
1893
1894        f = new CompletableFuture<>();
1895        g = new CompletableFuture<>();
1896        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1897        assertTrue(f.cancel(true));
1898        checkIncomplete(h);
1899        g.complete(1);
1900        checkCompletedWithWrappedCancellationException(h);
1901
1902        f = new CompletableFuture<>();
1903        g = new CompletableFuture<>();
1904        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1905        assertTrue(g.cancel(true));
1906        checkIncomplete(h);
1907        f.complete(3);
1908        checkCompletedWithWrappedCancellationException(h);
1909
1910        f = new CompletableFuture<>();
1911        g = new CompletableFuture<>();
1912        f.complete(3);
1913        assertTrue(g.cancel(true));
1914        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1915        checkCompletedWithWrappedCancellationException(h);
1916
1917        f = new CompletableFuture<>();
1918        g = new CompletableFuture<>();
1919        assertTrue(f.cancel(true));
1920        g.complete(3);
1921        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1922        checkCompletedWithWrappedCancellationException(h);
1923    }
1924
1925    /**
1926     * runAfterBothAsync result completes normally after normal
1927     * completion of sources
1928     */
1929    public void testRunAfterBothAsync() {
1930        CompletableFuture<Integer> f = new CompletableFuture<>();
1931        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1932        Noop r = new Noop();
1933        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1934        f.complete(one);
1935        checkIncomplete(g);
1936        f2.complete(two);
1937        checkCompletedNormally(g, null);
1938        assertTrue(r.ran);
1939    }
1940
1941    /**
1942     * runAfterBothAsync result completes exceptionally after exceptional
1943     * completion of source
1944     */
1945    public void testRunAfterBothAsync2() {
1946        CompletableFuture<Integer> f = new CompletableFuture<>();
1947        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1948        Noop r = new Noop();
1949        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1950        f.completeExceptionally(new CFException());
1951        f2.complete(two);
1952        checkCompletedWithWrappedCFException(g);
1953
1954        r = new Noop();
1955        f = new CompletableFuture<>();
1956        f2 = new CompletableFuture<>();
1957        g = f.runAfterBothAsync(f2, r);
1958        f.complete(one);
1959        f2.completeExceptionally(new CFException());
1960        checkCompletedWithWrappedCFException(g);
1961    }
1962
1963    /**
1964     * runAfterBothAsync result completes exceptionally if action does
1965     */
1966    public void testRunAfterBothAsync3() {
1967        CompletableFuture<Integer> f = new CompletableFuture<>();
1968        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1969        FailingNoop r = new FailingNoop();
1970        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1971        f.complete(one);
1972        checkIncomplete(g);
1973        f2.complete(two);
1974        checkCompletedWithWrappedCFException(g);
1975    }
1976
1977    /**
1978     * runAfterBothAsync result completes exceptionally if either source cancelled
1979     */
1980    public void testRunAfterBothAsync4() {
1981        CompletableFuture<Integer> f = new CompletableFuture<>();
1982        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1983        Noop r = new Noop();
1984        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1985        assertTrue(f.cancel(true));
1986        f2.complete(two);
1987        checkCompletedWithWrappedCancellationException(g);
1988
1989        r = new Noop();
1990        f = new CompletableFuture<>();
1991        f2 = new CompletableFuture<>();
1992        g = f.runAfterBothAsync(f2, r);
1993        f.complete(one);
1994        assertTrue(f2.cancel(true));
1995        checkCompletedWithWrappedCancellationException(g);
1996    }
1997
1998    /**
1999     * applyToEitherAsync result completes normally after normal
2000     * completion of sources
2001     */
2002    public void testApplyToEitherAsync() {
2003        CompletableFuture<Integer> f = new CompletableFuture<>();
2004        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2005        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2006        f.complete(one);
2007        checkCompletedNormally(g, two);
2008
2009        f = new CompletableFuture<>();
2010        f.complete(one);
2011        f2 = new CompletableFuture<>();
2012        g = f.applyToEitherAsync(f2, inc);
2013        checkCompletedNormally(g, two);
2014    }
2015
2016    /**
2017     * applyToEitherAsync result completes exceptionally after exceptional
2018     * completion of source
2019     */
2020    public void testApplyToEitherAsync2() {
2021        CompletableFuture<Integer> f = new CompletableFuture<>();
2022        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2023        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2024        f.completeExceptionally(new CFException());
2025        checkCompletedWithWrappedCFException(g);
2026
2027        f = new CompletableFuture<>();
2028        f2 = new CompletableFuture<>();
2029        f2.completeExceptionally(new CFException());
2030        g = f.applyToEitherAsync(f2, inc);
2031        f.complete(one);
2032        checkCompletedWithWrappedCFException(g);
2033    }
2034
2035    /**
2036     * applyToEitherAsync result completes exceptionally if action does
2037     */
2038    public void testApplyToEitherAsync3() {
2039        CompletableFuture<Integer> f = new CompletableFuture<>();
2040        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2041        FailingFunction r = new FailingFunction();
2042        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2043        f.complete(one);
2044        checkCompletedWithWrappedCFException(g);
2045    }
2046
2047    /**
2048     * applyToEitherAsync result completes exceptionally if either source cancelled
2049     */
2050    public void testApplyToEitherAsync4() {
2051        CompletableFuture<Integer> f = new CompletableFuture<>();
2052        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2053        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2054        assertTrue(f.cancel(true));
2055        checkCompletedWithWrappedCancellationException(g);
2056
2057        f = new CompletableFuture<>();
2058        f2 = new CompletableFuture<>();
2059        assertTrue(f2.cancel(true));
2060        g = f.applyToEitherAsync(f2, inc);
2061        checkCompletedWithWrappedCancellationException(g);
2062    }
2063
2064    /**
2065     * acceptEitherAsync result completes normally after normal
2066     * completion of sources
2067     */
2068    public void testAcceptEitherAsync() {
2069        CompletableFuture<Integer> f = new CompletableFuture<>();
2070        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2071        IncAction r = new IncAction();
2072        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2073        f.complete(one);
2074        checkCompletedNormally(g, null);
2075        assertEquals(r.value, 2);
2076
2077        r = new IncAction();
2078        f = new CompletableFuture<>();
2079        f.complete(one);
2080        f2 = new CompletableFuture<>();
2081        g = f.acceptEitherAsync(f2, r);
2082        checkCompletedNormally(g, null);
2083        assertEquals(r.value, 2);
2084    }
2085
2086    /**
2087     * acceptEitherAsync result completes exceptionally after exceptional
2088     * completion of source
2089     */
2090    public void testAcceptEitherAsync2() {
2091        CompletableFuture<Integer> f = new CompletableFuture<>();
2092        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2093        IncAction r = new IncAction();
2094        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2095        f.completeExceptionally(new CFException());
2096        checkCompletedWithWrappedCFException(g);
2097
2098        r = new IncAction();
2099        f = new CompletableFuture<>();
2100        f2 = new CompletableFuture<>();
2101        f2.completeExceptionally(new CFException());
2102        g = f.acceptEitherAsync(f2, r);
2103        f.complete(one);
2104        checkCompletedWithWrappedCFException(g);
2105    }
2106
2107    /**
2108     * acceptEitherAsync result completes exceptionally if action does
2109     */
2110    public void testAcceptEitherAsync3() {
2111        CompletableFuture<Integer> f = new CompletableFuture<>();
2112        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2113        FailingConsumer r = new FailingConsumer();
2114        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2115        f.complete(one);
2116        checkCompletedWithWrappedCFException(g);
2117    }
2118
2119    /**
2120     * acceptEitherAsync result completes exceptionally if either
2121     * source cancelled
2122     */
2123    public void testAcceptEitherAsync4() {
2124        CompletableFuture<Integer> f = new CompletableFuture<>();
2125        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2126        IncAction r = new IncAction();
2127        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2128        assertTrue(f.cancel(true));
2129        checkCompletedWithWrappedCancellationException(g);
2130
2131        r = new IncAction();
2132        f = new CompletableFuture<>();
2133        f2 = new CompletableFuture<>();
2134        assertTrue(f2.cancel(true));
2135        g = f.acceptEitherAsync(f2, r);
2136        checkCompletedWithWrappedCancellationException(g);
2137    }
2138
2139    /**
2140     * runAfterEitherAsync result completes normally after normal
2141     * completion of sources
2142     */
2143    public void testRunAfterEitherAsync() {
2144        CompletableFuture<Integer> f = new CompletableFuture<>();
2145        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2146        Noop r = new Noop();
2147        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2148        f.complete(one);
2149        checkCompletedNormally(g, null);
2150        assertTrue(r.ran);
2151
2152        r = new Noop();
2153        f = new CompletableFuture<>();
2154        f.complete(one);
2155        f2 = new CompletableFuture<>();
2156        g = f.runAfterEitherAsync(f2, r);
2157        checkCompletedNormally(g, null);
2158        assertTrue(r.ran);
2159    }
2160
2161    /**
2162     * runAfterEitherAsync result completes exceptionally after exceptional
2163     * completion of source
2164     */
2165    public void testRunAfterEitherAsync2() {
2166        CompletableFuture<Integer> f = new CompletableFuture<>();
2167        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2168        Noop r = new Noop();
2169        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2170        f.completeExceptionally(new CFException());
2171        checkCompletedWithWrappedCFException(g);
2172
2173        r = new Noop();
2174        f = new CompletableFuture<>();
2175        f2 = new CompletableFuture<>();
2176        f2.completeExceptionally(new CFException());
2177        g = f.runAfterEitherAsync(f2, r);
2178        f.complete(one);
2179        checkCompletedWithWrappedCFException(g);
2180    }
2181
2182    /**
2183     * runAfterEitherAsync result completes exceptionally if action does
2184     */
2185    public void testRunAfterEitherAsync3() {
2186        CompletableFuture<Integer> f = new CompletableFuture<>();
2187        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2188        FailingNoop r = new FailingNoop();
2189        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2190        f.complete(one);
2191        checkCompletedWithWrappedCFException(g);
2192    }
2193
2194    /**
2195     * runAfterEitherAsync result completes exceptionally if either
2196     * source cancelled
2197     */
2198    public void testRunAfterEitherAsync4() {
2199        CompletableFuture<Integer> f = new CompletableFuture<>();
2200        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2201        Noop r = new Noop();
2202        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2203        assertTrue(f.cancel(true));
2204        checkCompletedWithWrappedCancellationException(g);
2205
2206        r = new Noop();
2207        f = new CompletableFuture<>();
2208        f2 = new CompletableFuture<>();
2209        assertTrue(f2.cancel(true));
2210        g = f.runAfterEitherAsync(f2, r);
2211        checkCompletedWithWrappedCancellationException(g);
2212    }
2213
2214    /**
3005       * thenComposeAsync result completes normally after normal
3006       * completion of source
3007       */
# Line 2397 | Line 3187 | public class CompletableFutureTest exten
3187          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3188          f.complete(one);
3189          checkCompletedNormally(g, null);
3190 <        assertEquals(r.value, 2);
3190 >        assertEquals(r.value, (Integer) 2);
3191      }
3192  
3193      /**
# Line 2434 | Line 3224 | public class CompletableFutureTest exten
3224          checkCompletedWithWrappedCancellationException(g);
3225      }
3226  
2437    /**
2438     * thenCombineAsync result completes normally after normal
2439     * completion of sources
2440     */
2441    public void testThenCombineAsyncE() {
2442        CompletableFuture<Integer> f, g, h;
2443        ThreadExecutor e = new ThreadExecutor();
2444        int count = 0;
2445
2446        f = new CompletableFuture<>();
2447        g = new CompletableFuture<>();
2448        h = f.thenCombineAsync(g, subtract, e);
2449        f.complete(3);
2450        checkIncomplete(h);
2451        g.complete(1);
2452        checkCompletedNormally(h, 2);
2453        assertEquals(++count, e.count.get());
2454
2455        f = new CompletableFuture<>();
2456        g = new CompletableFuture<>();
2457        h = f.thenCombineAsync(g, subtract, e);
2458        g.complete(1);
2459        checkIncomplete(h);
2460        f.complete(3);
2461        checkCompletedNormally(h, 2);
2462        assertEquals(++count, e.count.get());
2463
2464        f = new CompletableFuture<>();
2465        g = new CompletableFuture<>();
2466        g.complete(1);
2467        f.complete(3);
2468        h = f.thenCombineAsync(g, subtract, e);
2469        checkCompletedNormally(h, 2);
2470        assertEquals(++count, e.count.get());
2471    }
2472
2473    /**
2474     * thenCombineAsync result completes exceptionally after exceptional
2475     * completion of either source
2476     */
2477    public void testThenCombineAsync2E() {
2478        CompletableFuture<Integer> f, g, h;
2479        ThreadExecutor e = new ThreadExecutor();
2480        int count = 0;
2481
2482        f = new CompletableFuture<>();
2483        g = new CompletableFuture<>();
2484        h = f.thenCombineAsync(g, subtract, e);
2485        f.completeExceptionally(new CFException());
2486        checkIncomplete(h);
2487        g.complete(1);
2488        checkCompletedWithWrappedCFException(h);
2489
2490        f = new CompletableFuture<>();
2491        g = new CompletableFuture<>();
2492        h = f.thenCombineAsync(g, subtract, e);
2493        g.completeExceptionally(new CFException());
2494        checkIncomplete(h);
2495        f.complete(3);
2496        checkCompletedWithWrappedCFException(h);
2497
2498        f = new CompletableFuture<>();
2499        g = new CompletableFuture<>();
2500        g.completeExceptionally(new CFException());
2501        h = f.thenCombineAsync(g, subtract, e);
2502        checkIncomplete(h);
2503        f.complete(3);
2504        checkCompletedWithWrappedCFException(h);
2505
2506        assertEquals(0, e.count.get());
2507    }
2508
2509    /**
2510     * thenCombineAsync result completes exceptionally if action does
2511     */
2512    public void testThenCombineAsync3E() {
2513        CompletableFuture<Integer> f = new CompletableFuture<>();
2514        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2515        FailingBiFunction r = new FailingBiFunction();
2516        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2517        f.complete(one);
2518        checkIncomplete(g);
2519        assertFalse(r.ran);
2520        f2.complete(two);
2521        checkCompletedWithWrappedCFException(g);
2522        assertTrue(r.ran);
2523    }
2524
2525    /**
2526     * thenCombineAsync result completes exceptionally if either source cancelled
2527     */
2528    public void testThenCombineAsync4E() {
2529        CompletableFuture<Integer> f, g, h;
2530        ThreadExecutor e = new ThreadExecutor();
2531
2532        f = new CompletableFuture<>();
2533        g = new CompletableFuture<>();
2534        h = f.thenCombineAsync(g, subtract, e);
2535        assertTrue(f.cancel(true));
2536        checkIncomplete(h);
2537        g.complete(1);
2538        checkCompletedWithWrappedCancellationException(h);
2539
2540        f = new CompletableFuture<>();
2541        g = new CompletableFuture<>();
2542        h = f.thenCombineAsync(g, subtract, e);
2543        assertTrue(g.cancel(true));
2544        checkIncomplete(h);
2545        f.complete(3);
2546        checkCompletedWithWrappedCancellationException(h);
2547
2548        f = new CompletableFuture<>();
2549        g = new CompletableFuture<>();
2550        assertTrue(g.cancel(true));
2551        h = f.thenCombineAsync(g, subtract, e);
2552        checkIncomplete(h);
2553        f.complete(3);
2554        checkCompletedWithWrappedCancellationException(h);
2555
2556        f = new CompletableFuture<>();
2557        g = new CompletableFuture<>();
2558        assertTrue(f.cancel(true));
2559        assertTrue(g.cancel(true));
2560        h = f.thenCombineAsync(g, subtract, e);
2561        checkCompletedWithWrappedCancellationException(h);
2562
2563        assertEquals(0, e.count.get());
2564    }
2565
2566    /**
2567     * thenAcceptBothAsync result completes normally after normal
2568     * completion of sources
2569     */
2570    public void testThenAcceptBothAsyncE() {
2571        CompletableFuture<Integer> f, g;
2572        CompletableFuture<Void> h;
2573        SubtractAction r;
2574        ThreadExecutor e = new ThreadExecutor();
2575
2576        f = new CompletableFuture<>();
2577        g = new CompletableFuture<>();
2578        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2579        f.complete(3);
2580        checkIncomplete(h);
2581        g.complete(1);
2582        checkCompletedNormally(h, null);
2583        assertEquals(r.value, 2);
2584
2585        f = new CompletableFuture<>();
2586        g = new CompletableFuture<>();
2587        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2588        g.complete(1);
2589        checkIncomplete(h);
2590        f.complete(3);
2591        checkCompletedNormally(h, null);
2592        assertEquals(r.value, 2);
2593
2594        f = new CompletableFuture<>();
2595        g = new CompletableFuture<>();
2596        g.complete(1);
2597        f.complete(3);
2598        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2599        checkCompletedNormally(h, null);
2600        assertEquals(r.value, 2);
2601
2602        assertEquals(3, e.count.get());
2603    }
2604
2605    /**
2606     * thenAcceptBothAsync result completes exceptionally after exceptional
2607     * completion of source
2608     */
2609    public void testThenAcceptBothAsync2E() {
2610        CompletableFuture<Integer> f, g;
2611        CompletableFuture<Void> h;
2612        SubtractAction r;
2613        ThreadExecutor e = new ThreadExecutor();
2614
2615        f = new CompletableFuture<>();
2616        g = new CompletableFuture<>();
2617        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2618        f.completeExceptionally(new CFException());
2619        checkIncomplete(h);
2620        g.complete(1);
2621        checkCompletedWithWrappedCFException(h);
2622
2623        f = new CompletableFuture<>();
2624        g = new CompletableFuture<>();
2625        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2626        g.completeExceptionally(new CFException());
2627        checkIncomplete(h);
2628        f.complete(3);
2629        checkCompletedWithWrappedCFException(h);
2630
2631        f = new CompletableFuture<>();
2632        g = new CompletableFuture<>();
2633        f.complete(3);
2634        g.completeExceptionally(new CFException());
2635        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2636        checkCompletedWithWrappedCFException(h);
2637
2638        f = new CompletableFuture<>();
2639        g = new CompletableFuture<>();
2640        f.completeExceptionally(new CFException());
2641        g.complete(3);
2642        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2643        checkCompletedWithWrappedCFException(h);
2644
2645        assertEquals(0, e.count.get());
2646    }
2647
2648    /**
2649     * thenAcceptBothAsync result completes exceptionally if action does
2650     */
2651    public void testThenAcceptBothAsync3E() {
2652        CompletableFuture<Integer> f, g;
2653        CompletableFuture<Void> h;
2654        FailingBiConsumer r;
2655        ThreadExecutor e = new ThreadExecutor();
2656
2657        f = new CompletableFuture<>();
2658        g = new CompletableFuture<>();
2659        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2660        f.complete(3);
2661        checkIncomplete(h);
2662        g.complete(1);
2663        checkCompletedWithWrappedCFException(h);
2664
2665        f = new CompletableFuture<>();
2666        g = new CompletableFuture<>();
2667        f.complete(3);
2668        g.complete(1);
2669        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2670        checkCompletedWithWrappedCFException(h);
2671
2672        assertEquals(2, e.count.get());
2673    }
2674
2675    /**
2676     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2677     */
2678    public void testThenAcceptBothAsync4E() {
2679        CompletableFuture<Integer> f, g;
2680        CompletableFuture<Void> h;
2681        SubtractAction r;
2682        ThreadExecutor e = new ThreadExecutor();
2683
2684        f = new CompletableFuture<>();
2685        g = new CompletableFuture<>();
2686        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2687        assertTrue(f.cancel(true));
2688        checkIncomplete(h);
2689        g.complete(1);
2690        checkCompletedWithWrappedCancellationException(h);
2691
2692        f = new CompletableFuture<>();
2693        g = new CompletableFuture<>();
2694        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2695        assertTrue(g.cancel(true));
2696        checkIncomplete(h);
2697        f.complete(3);
2698        checkCompletedWithWrappedCancellationException(h);
2699
2700        f = new CompletableFuture<>();
2701        g = new CompletableFuture<>();
2702        f.complete(3);
2703        assertTrue(g.cancel(true));
2704        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2705        checkCompletedWithWrappedCancellationException(h);
2706
2707        f = new CompletableFuture<>();
2708        g = new CompletableFuture<>();
2709        assertTrue(f.cancel(true));
2710        g.complete(3);
2711        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2712        checkCompletedWithWrappedCancellationException(h);
2713
2714        assertEquals(0, e.count.get());
2715    }
2716
2717    /**
2718     * runAfterBothAsync result completes normally after normal
2719     * completion of sources
2720     */
2721    public void testRunAfterBothAsyncE() {
2722        CompletableFuture<Integer> f = new CompletableFuture<>();
2723        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2724        Noop r = new Noop();
2725        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2726        f.complete(one);
2727        checkIncomplete(g);
2728        f2.complete(two);
2729        checkCompletedNormally(g, null);
2730        assertTrue(r.ran);
2731    }
2732
2733    /**
2734     * runAfterBothAsync result completes exceptionally after exceptional
2735     * completion of source
2736     */
2737    public void testRunAfterBothAsync2E() {
2738        CompletableFuture<Integer> f = new CompletableFuture<>();
2739        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740        Noop r = new Noop();
2741        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2742        f.completeExceptionally(new CFException());
2743        f2.complete(two);
2744        checkCompletedWithWrappedCFException(g);
2745
2746        r = new Noop();
2747        f = new CompletableFuture<>();
2748        f2 = new CompletableFuture<>();
2749        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2750        f.complete(one);
2751        f2.completeExceptionally(new CFException());
2752        checkCompletedWithWrappedCFException(g);
2753    }
2754
2755    /**
2756     * runAfterBothAsync result completes exceptionally if action does
2757     */
2758    public void testRunAfterBothAsync3E() {
2759        CompletableFuture<Integer> f = new CompletableFuture<>();
2760        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2761        FailingNoop r = new FailingNoop();
2762        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2763        f.complete(one);
2764        checkIncomplete(g);
2765        f2.complete(two);
2766        checkCompletedWithWrappedCFException(g);
2767    }
2768
2769    /**
2770     * runAfterBothAsync result completes exceptionally if either source cancelled
2771     */
2772    public void testRunAfterBothAsync4E() {
2773        CompletableFuture<Integer> f = new CompletableFuture<>();
2774        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2775        Noop r = new Noop();
2776        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2777        assertTrue(f.cancel(true));
2778        f2.complete(two);
2779        checkCompletedWithWrappedCancellationException(g);
2780
2781        r = new Noop();
2782        f = new CompletableFuture<>();
2783        f2 = new CompletableFuture<>();
2784        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2785        f.complete(one);
2786        assertTrue(f2.cancel(true));
2787        checkCompletedWithWrappedCancellationException(g);
2788    }
2789
2790    /**
2791     * applyToEitherAsync result completes normally after normal
2792     * completion of sources
2793     */
2794    public void testApplyToEitherAsyncE() {
2795        CompletableFuture<Integer> f = new CompletableFuture<>();
2796        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2797        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2798        f.complete(one);
2799        checkCompletedNormally(g, two);
2800
2801        f = new CompletableFuture<>();
2802        f.complete(one);
2803        f2 = new CompletableFuture<>();
2804        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2805        checkCompletedNormally(g, two);
2806    }
2807
2808    /**
2809     * applyToEitherAsync result completes exceptionally after exceptional
2810     * completion of source
2811     */
2812    public void testApplyToEitherAsync2E() {
2813        CompletableFuture<Integer> f = new CompletableFuture<>();
2814        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2815        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2816        f.completeExceptionally(new CFException());
2817        checkCompletedWithWrappedCFException(g);
2818
2819        f = new CompletableFuture<>();
2820        f2 = new CompletableFuture<>();
2821        f2.completeExceptionally(new CFException());
2822        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2823        f.complete(one);
2824        checkCompletedWithWrappedCFException(g);
2825    }
2826
2827    /**
2828     * applyToEitherAsync result completes exceptionally if action does
2829     */
2830    public void testApplyToEitherAsync3E() {
2831        CompletableFuture<Integer> f = new CompletableFuture<>();
2832        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2833        FailingFunction r = new FailingFunction();
2834        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2835        f.complete(one);
2836        checkCompletedWithWrappedCFException(g);
2837    }
2838
2839    /**
2840     * applyToEitherAsync result completes exceptionally if either source cancelled
2841     */
2842    public void testApplyToEitherAsync4E() {
2843        CompletableFuture<Integer> f = new CompletableFuture<>();
2844        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2845        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2846        assertTrue(f.cancel(true));
2847        checkCompletedWithWrappedCancellationException(g);
2848
2849        f = new CompletableFuture<>();
2850        f2 = new CompletableFuture<>();
2851        assertTrue(f2.cancel(true));
2852        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2853        checkCompletedWithWrappedCancellationException(g);
2854    }
2855
2856    /**
2857     * acceptEitherAsync result completes normally after normal
2858     * completion of sources
2859     */
2860    public void testAcceptEitherAsyncE() {
2861        CompletableFuture<Integer> f = new CompletableFuture<>();
2862        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2863        IncAction r = new IncAction();
2864        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2865        f.complete(one);
2866        checkCompletedNormally(g, null);
2867        assertEquals(r.value, 2);
2868
2869        r = new IncAction();
2870        f = new CompletableFuture<>();
2871        f.complete(one);
2872        f2 = new CompletableFuture<>();
2873        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2874        checkCompletedNormally(g, null);
2875        assertEquals(r.value, 2);
2876    }
2877
2878    /**
2879     * acceptEitherAsync result completes exceptionally after exceptional
2880     * completion of source
2881     */
2882    public void testAcceptEitherAsync2E() {
2883        CompletableFuture<Integer> f = new CompletableFuture<>();
2884        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2885        IncAction r = new IncAction();
2886        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2887        f.completeExceptionally(new CFException());
2888        checkCompletedWithWrappedCFException(g);
2889
2890        r = new IncAction();
2891        f = new CompletableFuture<>();
2892        f2 = new CompletableFuture<>();
2893        f2.completeExceptionally(new CFException());
2894        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2895        f.complete(one);
2896        checkCompletedWithWrappedCFException(g);
2897    }
2898
2899    /**
2900     * acceptEitherAsync result completes exceptionally if action does
2901     */
2902    public void testAcceptEitherAsync3E() {
2903        CompletableFuture<Integer> f = new CompletableFuture<>();
2904        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2905        FailingConsumer r = new FailingConsumer();
2906        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2907        f.complete(one);
2908        checkCompletedWithWrappedCFException(g);
2909    }
2910
2911    /**
2912     * acceptEitherAsync result completes exceptionally if either
2913     * source cancelled
2914     */
2915    public void testAcceptEitherAsync4E() {
2916        CompletableFuture<Integer> f = new CompletableFuture<>();
2917        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2918        IncAction r = new IncAction();
2919        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2920        assertTrue(f.cancel(true));
2921        checkCompletedWithWrappedCancellationException(g);
2922
2923        r = new IncAction();
2924        f = new CompletableFuture<>();
2925        f2 = new CompletableFuture<>();
2926        assertTrue(f2.cancel(true));
2927        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2928        checkCompletedWithWrappedCancellationException(g);
2929    }
2930
2931    /**
2932     * runAfterEitherAsync result completes normally after normal
2933     * completion of sources
2934     */
2935    public void testRunAfterEitherAsyncE() {
2936        CompletableFuture<Integer> f = new CompletableFuture<>();
2937        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2938        Noop r = new Noop();
2939        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2940        f.complete(one);
2941        checkCompletedNormally(g, null);
2942        assertTrue(r.ran);
2943
2944        r = new Noop();
2945        f = new CompletableFuture<>();
2946        f.complete(one);
2947        f2 = new CompletableFuture<>();
2948        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2949        checkCompletedNormally(g, null);
2950        assertTrue(r.ran);
2951    }
2952
2953    /**
2954     * runAfterEitherAsync result completes exceptionally after exceptional
2955     * completion of source
2956     */
2957    public void testRunAfterEitherAsync2E() {
2958        CompletableFuture<Integer> f = new CompletableFuture<>();
2959        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2960        Noop r = new Noop();
2961        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2962        f.completeExceptionally(new CFException());
2963        checkCompletedWithWrappedCFException(g);
2964
2965        r = new Noop();
2966        f = new CompletableFuture<>();
2967        f2 = new CompletableFuture<>();
2968        f2.completeExceptionally(new CFException());
2969        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2970        f.complete(one);
2971        checkCompletedWithWrappedCFException(g);
2972    }
2973
2974    /**
2975     * runAfterEitherAsync result completes exceptionally if action does
2976     */
2977    public void testRunAfterEitherAsync3E() {
2978        CompletableFuture<Integer> f = new CompletableFuture<>();
2979        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2980        FailingNoop r = new FailingNoop();
2981        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2982        f.complete(one);
2983        checkCompletedWithWrappedCFException(g);
2984    }
2985
2986    /**
2987     * runAfterEitherAsync result completes exceptionally if either
2988     * source cancelled
2989     */
2990    public void testRunAfterEitherAsync4E() {
2991        CompletableFuture<Integer> f = new CompletableFuture<>();
2992        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2993        Noop r = new Noop();
2994        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2995        assertTrue(f.cancel(true));
2996        checkCompletedWithWrappedCancellationException(g);
2997
2998        r = new Noop();
2999        f = new CompletableFuture<>();
3000        f2 = new CompletableFuture<>();
3001        assertTrue(f2.cancel(true));
3002        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3003        checkCompletedWithWrappedCancellationException(g);
3004    }
3005
3227      /**
3228       * thenComposeAsync result completes normally after normal
3229       * completion of source

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines