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.35 by jsr166, Sun Jun 1 22:22:49 2014 UTC vs.
Revision 1.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC

# Line 318 | 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 SubtractAction implements BiConsumer<Integer, Integer> {
349 <        // Handle null values as well
350 <        public int subtract(Integer x, Integer y) {
351 <            return ((x == null) ? 42 : x.intValue())
352 <                - ((y == null) ? 99 : y.intValue());
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 <        int value;
357 <        public boolean ran() { return value != 0; }
356 >    }
357 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
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 +            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 427 | Line 465 | public class CompletableFutureTest exten
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 */
# Line 454 | Line 526 | public class CompletableFutureTest exten
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 {
# Line 480 | Line 586 | public class CompletableFutureTest exten
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
# Line 488 | Line 628 | public class CompletableFutureTest exten
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      /**
# Line 740 | 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 782 | 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);
825 <        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);
833 <        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 +
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  
888        f = new CompletableFuture<>();
889        g = new CompletableFuture<>();
890        assertTrue(f.cancel(true));
891        assertTrue(g.cancel(true));
892        h = f.thenCombine(g, subtract);
1240          checkCompletedWithWrappedCancellationException(h);
1241 +        checkCancelled(f);
1242 +        assertEquals(r.invocationCount, 0);
1243 +        checkCompletedNormally(g, v1);
1244 +        }
1245      }
1246  
1247      /**
# Line 909 | Line 1260 | public class CompletableFutureTest exten
1260  
1261          f.complete(v1);
1262          checkIncomplete(h);
1263 <        assertEquals(r.value, 0);
1263 >        assertFalse(r.ran());
1264          g.complete(v2);
1265  
1266          checkCompletedNormally(h, null);
1267 <        assertEquals(r.value, r.subtract(v1, v2));
1267 >        assertEquals(r.value, subtract(v1, v2));
1268          checkCompletedNormally(f, v1);
1269          checkCompletedNormally(g, v2);
1270          }
# Line 931 | Line 1282 | public class CompletableFutureTest exten
1282  
1283          g.complete(v2);
1284          checkIncomplete(h);
1285 <        assertEquals(r.value, 0);
1285 >        assertFalse(r.ran());
1286          f.complete(v1);
1287  
1288          checkCompletedNormally(h, null);
1289 <        assertEquals(r.value, r.subtract(v1, v2));
1289 >        assertEquals(r.value, subtract(v1, v2));
1290          checkCompletedNormally(f, v1);
1291          checkCompletedNormally(g, v2);
1292          }
# Line 955 | Line 1306 | public class CompletableFutureTest exten
1306          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1307  
1308          checkCompletedNormally(h, null);
1309 <        assertEquals(r.value, r.subtract(v1, v2));
1309 >        assertEquals(r.value, subtract(v1, v2));
1310          checkCompletedNormally(f, v1);
1311          checkCompletedNormally(g, v2);
1312          }
# Line 975 | Line 1326 | public class CompletableFutureTest exten
1326          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1327  
1328          checkCompletedNormally(h, null);
1329 <        assertEquals(r.value, r.subtract(v1, v2));
1329 >        assertEquals(r.value, subtract(v1, v2));
1330          checkCompletedNormally(f, v1);
1331          checkCompletedNormally(g, v2);
1332          }
# Line 1001 | Line 1352 | public class CompletableFutureTest exten
1352  
1353          checkCompletedWithWrappedCFException(h, ex);
1354          checkCompletedWithWrappedCFException(f, ex);
1355 <        assertFalse(r.ran());
1355 >        assertEquals(r.invocationCount, 0);
1356          checkCompletedNormally(g, v1);
1357          }
1358      }
# Line 1022 | Line 1373 | public class CompletableFutureTest exten
1373  
1374          checkCompletedWithWrappedCFException(h, ex);
1375          checkCompletedWithWrappedCFException(g, ex);
1376 <        assertFalse(r.ran());
1376 >        assertEquals(r.invocationCount, 0);
1377          checkCompletedNormally(f, v1);
1378          }
1379      }
# Line 1042 | Line 1393 | public class CompletableFutureTest exten
1393  
1394          checkCompletedWithWrappedCFException(h, ex);
1395          checkCompletedWithWrappedCFException(g, ex);
1396 <        assertFalse(r.ran());
1396 >        assertEquals(r.invocationCount, 0);
1397          checkCompletedNormally(f, v1);
1398          }
1399      }
# Line 1129 | Line 1480 | public class CompletableFutureTest exten
1480  
1481          checkCompletedWithWrappedCancellationException(h);
1482          checkCancelled(f);
1483 <        assertFalse(r.ran());
1483 >        assertEquals(r.invocationCount, 0);
1484          checkCompletedNormally(g, v1);
1485          }
1486      }
# Line 1150 | Line 1501 | public class CompletableFutureTest exten
1501  
1502          checkCompletedWithWrappedCancellationException(h);
1503          checkCancelled(g);
1504 <        assertFalse(r.ran());
1504 >        assertEquals(r.invocationCount, 0);
1505          checkCompletedNormally(f, v1);
1506          }
1507      }
# Line 1170 | Line 1521 | public class CompletableFutureTest exten
1521  
1522          checkCompletedWithWrappedCancellationException(h);
1523          checkCancelled(g);
1524 <        assertFalse(r.ran());
1524 >        assertEquals(r.invocationCount, 0);
1525          checkCompletedNormally(f, v1);
1526          }
1527      }
# Line 1190 | Line 1541 | public class CompletableFutureTest exten
1541  
1542          checkCompletedWithWrappedCancellationException(h);
1543          checkCancelled(f);
1544 <        assertFalse(r.ran());
1544 >        assertEquals(r.invocationCount, 0);
1545          checkCompletedNormally(g, v1);
1546          }
1547      }
# Line 1215 | 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          }
# Line 1237 | 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          }
# Line 1277 | Line 1628 | public class CompletableFutureTest exten
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 1303 | 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      }
# Line 1324 | 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      }
# Line 1344 | Line 1695 | public class CompletableFutureTest exten
1695  
1696          checkCompletedWithWrappedCFException(h, ex);
1697          checkCompletedWithWrappedCFException(g, ex);
1698 <        assertFalse(r.ran);
1698 >        assertEquals(r.invocationCount, 0);
1699          checkCompletedNormally(f, v1);
1700          }
1701      }
# Line 1364 | Line 1715 | public class CompletableFutureTest exten
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 1431 | 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      }
# Line 1452 | 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      }
# Line 1472 | Line 1823 | public class CompletableFutureTest exten
1823  
1824          checkCompletedWithWrappedCancellationException(h);
1825          checkCancelled(g);
1826 <        assertFalse(r.ran);
1826 >        assertEquals(r.invocationCount, 0);
1827          checkCompletedNormally(f, v1);
1828          }
1829      }
# Line 1492 | Line 1843 | public class CompletableFutureTest exten
1843  
1844          checkCompletedWithWrappedCancellationException(h);
1845          checkCancelled(f);
1846 <        assertFalse(r.ran);
1846 >        assertEquals(r.invocationCount, 0);
1847          checkCompletedNormally(g, v1);
1848          }
1849      }
# Line 1501 | 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);
1508 <        f.complete(one);
1509 <        checkCompletedNormally(g, two);
1510 <        f2.complete(one);
1511 <        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<>();
1527 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1528 <        f.completeExceptionally(new CFException());
1529 <        f2.complete(one);
1530 <        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();
1574 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1575 <        f.complete(one);
1576 <        checkCompletedNormally(g, null);
1577 <        f2.complete(one);
1578 <        checkCompletedNormally(g, null);
1579 <        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<>();
1597 <        IncAction r = new IncAction();
1598 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1599 <        f.completeExceptionally(new CFException());
1600 <        f2.complete(one);
1601 <        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();
1647 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1648 <        f.complete(one);
1649 <        checkCompletedNormally(g, null);
1650 <        f2.complete(one);
1651 <        checkCompletedNormally(g, null);
1652 <        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<>();
1670 <        Noop r = new Noop();
1671 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1672 <        f.completeExceptionally(new CFException());
1673 <        f2.complete(one);
1674 <        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 1894 | 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 1932 | Line 3002 | public class CompletableFutureTest exten
3002      }
3003  
3004      /**
1935     * thenCombineAsync result completes normally after normal
1936     * completion of sources
1937     */
1938    public void testThenCombineAsync() {
1939        CompletableFuture<Integer> f, g, h;
1940
1941        f = new CompletableFuture<>();
1942        g = new CompletableFuture<>();
1943        h = f.thenCombineAsync(g, subtract);
1944        f.complete(3);
1945        checkIncomplete(h);
1946        g.complete(1);
1947        checkCompletedNormally(h, 2);
1948
1949        f = new CompletableFuture<>();
1950        g = new CompletableFuture<>();
1951        h = f.thenCombineAsync(g, subtract);
1952        g.complete(1);
1953        checkIncomplete(h);
1954        f.complete(3);
1955        checkCompletedNormally(h, 2);
1956
1957        f = new CompletableFuture<>();
1958        g = new CompletableFuture<>();
1959        g.complete(1);
1960        f.complete(3);
1961        h = f.thenCombineAsync(g, subtract);
1962        checkCompletedNormally(h, 2);
1963    }
1964
1965    /**
1966     * thenCombineAsync result completes exceptionally after exceptional
1967     * completion of either source
1968     */
1969    public void testThenCombineAsync2() {
1970        CompletableFuture<Integer> f, g, h;
1971
1972        f = new CompletableFuture<>();
1973        g = new CompletableFuture<>();
1974        h = f.thenCombineAsync(g, subtract);
1975        f.completeExceptionally(new CFException());
1976        checkIncomplete(h);
1977        g.complete(1);
1978        checkCompletedWithWrappedCFException(h);
1979
1980        f = new CompletableFuture<>();
1981        g = new CompletableFuture<>();
1982        h = f.thenCombineAsync(g, subtract);
1983        g.completeExceptionally(new CFException());
1984        checkIncomplete(h);
1985        f.complete(3);
1986        checkCompletedWithWrappedCFException(h);
1987
1988        f = new CompletableFuture<>();
1989        g = new CompletableFuture<>();
1990        g.completeExceptionally(new CFException());
1991        f.complete(3);
1992        h = f.thenCombineAsync(g, subtract);
1993        checkCompletedWithWrappedCFException(h);
1994    }
1995
1996    /**
1997     * thenCombineAsync result completes exceptionally if action does
1998     */
1999    public void testThenCombineAsync3() {
2000        CompletableFuture<Integer> f = new CompletableFuture<>();
2001        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2002        FailingBiFunction r = new FailingBiFunction();
2003        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
2004        f.complete(one);
2005        checkIncomplete(g);
2006        assertFalse(r.ran);
2007        f2.complete(two);
2008        checkCompletedWithWrappedCFException(g);
2009        assertTrue(r.ran);
2010    }
2011
2012    /**
2013     * thenCombineAsync result completes exceptionally if either source cancelled
2014     */
2015    public void testThenCombineAsync4() {
2016        CompletableFuture<Integer> f, g, h;
2017
2018        f = new CompletableFuture<>();
2019        g = new CompletableFuture<>();
2020        h = f.thenCombineAsync(g, subtract);
2021        assertTrue(f.cancel(true));
2022        checkIncomplete(h);
2023        g.complete(1);
2024        checkCompletedWithWrappedCancellationException(h);
2025
2026        f = new CompletableFuture<>();
2027        g = new CompletableFuture<>();
2028        h = f.thenCombineAsync(g, subtract);
2029        assertTrue(g.cancel(true));
2030        checkIncomplete(h);
2031        f.complete(3);
2032        checkCompletedWithWrappedCancellationException(h);
2033
2034        f = new CompletableFuture<>();
2035        g = new CompletableFuture<>();
2036        g.complete(3);
2037        assertTrue(f.cancel(true));
2038        h = f.thenCombineAsync(g, subtract);
2039        checkCompletedWithWrappedCancellationException(h);
2040
2041        f = new CompletableFuture<>();
2042        g = new CompletableFuture<>();
2043        f.complete(3);
2044        assertTrue(g.cancel(true));
2045        h = f.thenCombineAsync(g, subtract);
2046        checkCompletedWithWrappedCancellationException(h);
2047    }
2048
2049    /**
2050     * applyToEitherAsync result completes normally after normal
2051     * completion of sources
2052     */
2053    public void testApplyToEitherAsync() {
2054        CompletableFuture<Integer> f = new CompletableFuture<>();
2055        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2056        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2057        f.complete(one);
2058        checkCompletedNormally(g, two);
2059
2060        f = new CompletableFuture<>();
2061        f.complete(one);
2062        f2 = new CompletableFuture<>();
2063        g = f.applyToEitherAsync(f2, inc);
2064        checkCompletedNormally(g, two);
2065    }
2066
2067    /**
2068     * applyToEitherAsync result completes exceptionally after exceptional
2069     * completion of source
2070     */
2071    public void testApplyToEitherAsync2() {
2072        CompletableFuture<Integer> f = new CompletableFuture<>();
2073        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2074        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2075        f.completeExceptionally(new CFException());
2076        checkCompletedWithWrappedCFException(g);
2077
2078        f = new CompletableFuture<>();
2079        f2 = new CompletableFuture<>();
2080        f2.completeExceptionally(new CFException());
2081        g = f.applyToEitherAsync(f2, inc);
2082        f.complete(one);
2083        checkCompletedWithWrappedCFException(g);
2084    }
2085
2086    /**
2087     * applyToEitherAsync result completes exceptionally if action does
2088     */
2089    public void testApplyToEitherAsync3() {
2090        CompletableFuture<Integer> f = new CompletableFuture<>();
2091        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2092        FailingFunction r = new FailingFunction();
2093        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2094        f.complete(one);
2095        checkCompletedWithWrappedCFException(g);
2096    }
2097
2098    /**
2099     * applyToEitherAsync result completes exceptionally if either source cancelled
2100     */
2101    public void testApplyToEitherAsync4() {
2102        CompletableFuture<Integer> f = new CompletableFuture<>();
2103        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2104        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2105        assertTrue(f.cancel(true));
2106        checkCompletedWithWrappedCancellationException(g);
2107
2108        f = new CompletableFuture<>();
2109        f2 = new CompletableFuture<>();
2110        assertTrue(f2.cancel(true));
2111        g = f.applyToEitherAsync(f2, inc);
2112        checkCompletedWithWrappedCancellationException(g);
2113    }
2114
2115    /**
2116     * acceptEitherAsync result completes normally after normal
2117     * completion of sources
2118     */
2119    public void testAcceptEitherAsync() {
2120        CompletableFuture<Integer> f = new CompletableFuture<>();
2121        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2122        IncAction r = new IncAction();
2123        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2124        f.complete(one);
2125        checkCompletedNormally(g, null);
2126        assertEquals(r.value, 2);
2127
2128        r = new IncAction();
2129        f = new CompletableFuture<>();
2130        f.complete(one);
2131        f2 = new CompletableFuture<>();
2132        g = f.acceptEitherAsync(f2, r);
2133        checkCompletedNormally(g, null);
2134        assertEquals(r.value, 2);
2135    }
2136
2137    /**
2138     * acceptEitherAsync result completes exceptionally after exceptional
2139     * completion of source
2140     */
2141    public void testAcceptEitherAsync2() {
2142        CompletableFuture<Integer> f = new CompletableFuture<>();
2143        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2144        IncAction r = new IncAction();
2145        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2146        f.completeExceptionally(new CFException());
2147        checkCompletedWithWrappedCFException(g);
2148
2149        r = new IncAction();
2150        f = new CompletableFuture<>();
2151        f2 = new CompletableFuture<>();
2152        f2.completeExceptionally(new CFException());
2153        g = f.acceptEitherAsync(f2, r);
2154        f.complete(one);
2155        checkCompletedWithWrappedCFException(g);
2156    }
2157
2158    /**
2159     * acceptEitherAsync result completes exceptionally if action does
2160     */
2161    public void testAcceptEitherAsync3() {
2162        CompletableFuture<Integer> f = new CompletableFuture<>();
2163        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2164        FailingConsumer r = new FailingConsumer();
2165        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2166        f.complete(one);
2167        checkCompletedWithWrappedCFException(g);
2168    }
2169
2170    /**
2171     * acceptEitherAsync result completes exceptionally if either
2172     * source cancelled
2173     */
2174    public void testAcceptEitherAsync4() {
2175        CompletableFuture<Integer> f = new CompletableFuture<>();
2176        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2177        IncAction r = new IncAction();
2178        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2179        assertTrue(f.cancel(true));
2180        checkCompletedWithWrappedCancellationException(g);
2181
2182        r = new IncAction();
2183        f = new CompletableFuture<>();
2184        f2 = new CompletableFuture<>();
2185        assertTrue(f2.cancel(true));
2186        g = f.acceptEitherAsync(f2, r);
2187        checkCompletedWithWrappedCancellationException(g);
2188    }
2189
2190    /**
2191     * runAfterEitherAsync result completes normally after normal
2192     * completion of sources
2193     */
2194    public void testRunAfterEitherAsync() {
2195        CompletableFuture<Integer> f = new CompletableFuture<>();
2196        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2197        Noop r = new Noop();
2198        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2199        f.complete(one);
2200        checkCompletedNormally(g, null);
2201        assertTrue(r.ran);
2202
2203        r = new Noop();
2204        f = new CompletableFuture<>();
2205        f.complete(one);
2206        f2 = new CompletableFuture<>();
2207        g = f.runAfterEitherAsync(f2, r);
2208        checkCompletedNormally(g, null);
2209        assertTrue(r.ran);
2210    }
2211
2212    /**
2213     * runAfterEitherAsync result completes exceptionally after exceptional
2214     * completion of source
2215     */
2216    public void testRunAfterEitherAsync2() {
2217        CompletableFuture<Integer> f = new CompletableFuture<>();
2218        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2219        Noop r = new Noop();
2220        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2221        f.completeExceptionally(new CFException());
2222        checkCompletedWithWrappedCFException(g);
2223
2224        r = new Noop();
2225        f = new CompletableFuture<>();
2226        f2 = new CompletableFuture<>();
2227        f2.completeExceptionally(new CFException());
2228        g = f.runAfterEitherAsync(f2, r);
2229        f.complete(one);
2230        checkCompletedWithWrappedCFException(g);
2231    }
2232
2233    /**
2234     * runAfterEitherAsync result completes exceptionally if action does
2235     */
2236    public void testRunAfterEitherAsync3() {
2237        CompletableFuture<Integer> f = new CompletableFuture<>();
2238        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2239        FailingNoop r = new FailingNoop();
2240        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2241        f.complete(one);
2242        checkCompletedWithWrappedCFException(g);
2243    }
2244
2245    /**
2246     * runAfterEitherAsync result completes exceptionally if either
2247     * source cancelled
2248     */
2249    public void testRunAfterEitherAsync4() {
2250        CompletableFuture<Integer> f = new CompletableFuture<>();
2251        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2252        Noop r = new Noop();
2253        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2254        assertTrue(f.cancel(true));
2255        checkCompletedWithWrappedCancellationException(g);
2256
2257        r = new Noop();
2258        f = new CompletableFuture<>();
2259        f2 = new CompletableFuture<>();
2260        assertTrue(f2.cancel(true));
2261        g = f.runAfterEitherAsync(f2, r);
2262        checkCompletedWithWrappedCancellationException(g);
2263    }
2264
2265    /**
3005       * thenComposeAsync result completes normally after normal
3006       * completion of source
3007       */
# Line 2448 | 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 2486 | Line 3225 | public class CompletableFutureTest exten
3225      }
3226  
3227      /**
2489     * thenCombineAsync result completes normally after normal
2490     * completion of sources
2491     */
2492    public void testThenCombineAsyncE() {
2493        CompletableFuture<Integer> f, g, h;
2494        ThreadExecutor e = new ThreadExecutor();
2495        int count = 0;
2496
2497        f = new CompletableFuture<>();
2498        g = new CompletableFuture<>();
2499        h = f.thenCombineAsync(g, subtract, e);
2500        f.complete(3);
2501        checkIncomplete(h);
2502        g.complete(1);
2503        checkCompletedNormally(h, 2);
2504        assertEquals(++count, e.count.get());
2505
2506        f = new CompletableFuture<>();
2507        g = new CompletableFuture<>();
2508        h = f.thenCombineAsync(g, subtract, e);
2509        g.complete(1);
2510        checkIncomplete(h);
2511        f.complete(3);
2512        checkCompletedNormally(h, 2);
2513        assertEquals(++count, e.count.get());
2514
2515        f = new CompletableFuture<>();
2516        g = new CompletableFuture<>();
2517        g.complete(1);
2518        f.complete(3);
2519        h = f.thenCombineAsync(g, subtract, e);
2520        checkCompletedNormally(h, 2);
2521        assertEquals(++count, e.count.get());
2522    }
2523
2524    /**
2525     * thenCombineAsync result completes exceptionally after exceptional
2526     * completion of either source
2527     */
2528    public void testThenCombineAsync2E() {
2529        CompletableFuture<Integer> f, g, h;
2530        ThreadExecutor e = new ThreadExecutor();
2531        int count = 0;
2532
2533        f = new CompletableFuture<>();
2534        g = new CompletableFuture<>();
2535        h = f.thenCombineAsync(g, subtract, e);
2536        f.completeExceptionally(new CFException());
2537        checkIncomplete(h);
2538        g.complete(1);
2539        checkCompletedWithWrappedCFException(h);
2540
2541        f = new CompletableFuture<>();
2542        g = new CompletableFuture<>();
2543        h = f.thenCombineAsync(g, subtract, e);
2544        g.completeExceptionally(new CFException());
2545        checkIncomplete(h);
2546        f.complete(3);
2547        checkCompletedWithWrappedCFException(h);
2548
2549        f = new CompletableFuture<>();
2550        g = new CompletableFuture<>();
2551        g.completeExceptionally(new CFException());
2552        h = f.thenCombineAsync(g, subtract, e);
2553        checkIncomplete(h);
2554        f.complete(3);
2555        checkCompletedWithWrappedCFException(h);
2556
2557        assertEquals(0, e.count.get());
2558    }
2559
2560    /**
2561     * thenCombineAsync result completes exceptionally if action does
2562     */
2563    public void testThenCombineAsync3E() {
2564        CompletableFuture<Integer> f = new CompletableFuture<>();
2565        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2566        FailingBiFunction r = new FailingBiFunction();
2567        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2568        f.complete(one);
2569        checkIncomplete(g);
2570        assertFalse(r.ran);
2571        f2.complete(two);
2572        checkCompletedWithWrappedCFException(g);
2573        assertTrue(r.ran);
2574    }
2575
2576    /**
2577     * thenCombineAsync result completes exceptionally if either source cancelled
2578     */
2579    public void testThenCombineAsync4E() {
2580        CompletableFuture<Integer> f, g, h;
2581        ThreadExecutor e = new ThreadExecutor();
2582
2583        f = new CompletableFuture<>();
2584        g = new CompletableFuture<>();
2585        h = f.thenCombineAsync(g, subtract, e);
2586        assertTrue(f.cancel(true));
2587        checkIncomplete(h);
2588        g.complete(1);
2589        checkCompletedWithWrappedCancellationException(h);
2590
2591        f = new CompletableFuture<>();
2592        g = new CompletableFuture<>();
2593        h = f.thenCombineAsync(g, subtract, e);
2594        assertTrue(g.cancel(true));
2595        checkIncomplete(h);
2596        f.complete(3);
2597        checkCompletedWithWrappedCancellationException(h);
2598
2599        f = new CompletableFuture<>();
2600        g = new CompletableFuture<>();
2601        assertTrue(g.cancel(true));
2602        h = f.thenCombineAsync(g, subtract, e);
2603        checkIncomplete(h);
2604        f.complete(3);
2605        checkCompletedWithWrappedCancellationException(h);
2606
2607        f = new CompletableFuture<>();
2608        g = new CompletableFuture<>();
2609        assertTrue(f.cancel(true));
2610        assertTrue(g.cancel(true));
2611        h = f.thenCombineAsync(g, subtract, e);
2612        checkCompletedWithWrappedCancellationException(h);
2613
2614        assertEquals(0, e.count.get());
2615    }
2616
2617    /**
2618     * applyToEitherAsync result completes normally after normal
2619     * completion of sources
2620     */
2621    public void testApplyToEitherAsyncE() {
2622        CompletableFuture<Integer> f = new CompletableFuture<>();
2623        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2624        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625        f.complete(one);
2626        checkCompletedNormally(g, two);
2627
2628        f = new CompletableFuture<>();
2629        f.complete(one);
2630        f2 = new CompletableFuture<>();
2631        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2632        checkCompletedNormally(g, two);
2633    }
2634
2635    /**
2636     * applyToEitherAsync result completes exceptionally after exceptional
2637     * completion of source
2638     */
2639    public void testApplyToEitherAsync2E() {
2640        CompletableFuture<Integer> f = new CompletableFuture<>();
2641        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2642        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2643        f.completeExceptionally(new CFException());
2644        checkCompletedWithWrappedCFException(g);
2645
2646        f = new CompletableFuture<>();
2647        f2 = new CompletableFuture<>();
2648        f2.completeExceptionally(new CFException());
2649        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2650        f.complete(one);
2651        checkCompletedWithWrappedCFException(g);
2652    }
2653
2654    /**
2655     * applyToEitherAsync result completes exceptionally if action does
2656     */
2657    public void testApplyToEitherAsync3E() {
2658        CompletableFuture<Integer> f = new CompletableFuture<>();
2659        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2660        FailingFunction r = new FailingFunction();
2661        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2662        f.complete(one);
2663        checkCompletedWithWrappedCFException(g);
2664    }
2665
2666    /**
2667     * applyToEitherAsync result completes exceptionally if either source cancelled
2668     */
2669    public void testApplyToEitherAsync4E() {
2670        CompletableFuture<Integer> f = new CompletableFuture<>();
2671        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2672        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2673        assertTrue(f.cancel(true));
2674        checkCompletedWithWrappedCancellationException(g);
2675
2676        f = new CompletableFuture<>();
2677        f2 = new CompletableFuture<>();
2678        assertTrue(f2.cancel(true));
2679        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2680        checkCompletedWithWrappedCancellationException(g);
2681    }
2682
2683    /**
2684     * acceptEitherAsync result completes normally after normal
2685     * completion of sources
2686     */
2687    public void testAcceptEitherAsyncE() {
2688        CompletableFuture<Integer> f = new CompletableFuture<>();
2689        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2690        IncAction r = new IncAction();
2691        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2692        f.complete(one);
2693        checkCompletedNormally(g, null);
2694        assertEquals(r.value, 2);
2695
2696        r = new IncAction();
2697        f = new CompletableFuture<>();
2698        f.complete(one);
2699        f2 = new CompletableFuture<>();
2700        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2701        checkCompletedNormally(g, null);
2702        assertEquals(r.value, 2);
2703    }
2704
2705    /**
2706     * acceptEitherAsync result completes exceptionally after exceptional
2707     * completion of source
2708     */
2709    public void testAcceptEitherAsync2E() {
2710        CompletableFuture<Integer> f = new CompletableFuture<>();
2711        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2712        IncAction r = new IncAction();
2713        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2714        f.completeExceptionally(new CFException());
2715        checkCompletedWithWrappedCFException(g);
2716
2717        r = new IncAction();
2718        f = new CompletableFuture<>();
2719        f2 = new CompletableFuture<>();
2720        f2.completeExceptionally(new CFException());
2721        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722        f.complete(one);
2723        checkCompletedWithWrappedCFException(g);
2724    }
2725
2726    /**
2727     * acceptEitherAsync result completes exceptionally if action does
2728     */
2729    public void testAcceptEitherAsync3E() {
2730        CompletableFuture<Integer> f = new CompletableFuture<>();
2731        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2732        FailingConsumer r = new FailingConsumer();
2733        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2734        f.complete(one);
2735        checkCompletedWithWrappedCFException(g);
2736    }
2737
2738    /**
2739     * acceptEitherAsync result completes exceptionally if either
2740     * source cancelled
2741     */
2742    public void testAcceptEitherAsync4E() {
2743        CompletableFuture<Integer> f = new CompletableFuture<>();
2744        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2745        IncAction r = new IncAction();
2746        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2747        assertTrue(f.cancel(true));
2748        checkCompletedWithWrappedCancellationException(g);
2749
2750        r = new IncAction();
2751        f = new CompletableFuture<>();
2752        f2 = new CompletableFuture<>();
2753        assertTrue(f2.cancel(true));
2754        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2755        checkCompletedWithWrappedCancellationException(g);
2756    }
2757
2758    /**
2759     * runAfterEitherAsync result completes normally after normal
2760     * completion of sources
2761     */
2762    public void testRunAfterEitherAsyncE() {
2763        CompletableFuture<Integer> f = new CompletableFuture<>();
2764        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2765        Noop r = new Noop();
2766        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2767        f.complete(one);
2768        checkCompletedNormally(g, null);
2769        assertTrue(r.ran);
2770
2771        r = new Noop();
2772        f = new CompletableFuture<>();
2773        f.complete(one);
2774        f2 = new CompletableFuture<>();
2775        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2776        checkCompletedNormally(g, null);
2777        assertTrue(r.ran);
2778    }
2779
2780    /**
2781     * runAfterEitherAsync result completes exceptionally after exceptional
2782     * completion of source
2783     */
2784    public void testRunAfterEitherAsync2E() {
2785        CompletableFuture<Integer> f = new CompletableFuture<>();
2786        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2787        Noop r = new Noop();
2788        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2789        f.completeExceptionally(new CFException());
2790        checkCompletedWithWrappedCFException(g);
2791
2792        r = new Noop();
2793        f = new CompletableFuture<>();
2794        f2 = new CompletableFuture<>();
2795        f2.completeExceptionally(new CFException());
2796        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797        f.complete(one);
2798        checkCompletedWithWrappedCFException(g);
2799    }
2800
2801    /**
2802     * runAfterEitherAsync result completes exceptionally if action does
2803     */
2804    public void testRunAfterEitherAsync3E() {
2805        CompletableFuture<Integer> f = new CompletableFuture<>();
2806        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2807        FailingNoop r = new FailingNoop();
2808        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2809        f.complete(one);
2810        checkCompletedWithWrappedCFException(g);
2811    }
2812
2813    /**
2814     * runAfterEitherAsync result completes exceptionally if either
2815     * source cancelled
2816     */
2817    public void testRunAfterEitherAsync4E() {
2818        CompletableFuture<Integer> f = new CompletableFuture<>();
2819        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2820        Noop r = new Noop();
2821        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2822        assertTrue(f.cancel(true));
2823        checkCompletedWithWrappedCancellationException(g);
2824
2825        r = new Noop();
2826        f = new CompletableFuture<>();
2827        f2 = new CompletableFuture<>();
2828        assertTrue(f2.cancel(true));
2829        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2830        checkCompletedWithWrappedCancellationException(g);
2831    }
2832
2833    /**
3228       * thenComposeAsync result completes normally after normal
3229       * completion of source
3230       */
# Line 3074 | Line 3468 | public class CompletableFutureTest exten
3468       * whenComplete action executes on normal completion, propagating
3469       * source result.
3470       */
3471 <    public void testWhenComplete1() {
3471 >    public void testWhenComplete_normalCompletion1() {
3472 >        for (ExecutionMode m : ExecutionMode.values())
3473 >        for (Integer v1 : new Integer[] { 1, null }) {
3474 >
3475          final AtomicInteger a = new AtomicInteger();
3476 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3477 <        CompletableFuture<Integer> g =
3478 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3479 <        f.complete(three);
3480 <        checkCompletedNormally(f, three);
3481 <        checkCompletedNormally(g, three);
3476 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3477 >        final CompletableFuture<Integer> g =
3478 >            m.whenComplete(f,
3479 >                           (Integer x, Throwable t) -> {
3480 >                               threadAssertSame(x, v1);
3481 >                               threadAssertNull(t);
3482 >                               a.getAndIncrement();
3483 >                           });
3484 >        f.complete(v1);
3485 >        checkCompletedNormally(f, v1);
3486 >        checkCompletedNormally(g, v1);
3487          assertEquals(a.get(), 1);
3488 +        }
3489      }
3490  
3491      /**
3492       * whenComplete action executes on exceptional completion, propagating
3493       * source result.
3494       */
3495 <    public void testWhenComplete2() {
3495 >    public void testWhenComplete_exceptionalCompletion() {
3496 >        for (ExecutionMode m : ExecutionMode.values())
3497 >        for (Integer v1 : new Integer[] { 1, null }) {
3498 >
3499          final AtomicInteger a = new AtomicInteger();
3500 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3501 <        CompletableFuture<Integer> g =
3502 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3503 <        f.completeExceptionally(new CFException());
3504 <        assertTrue(f.isCompletedExceptionally());
3505 <        assertTrue(g.isCompletedExceptionally());
3500 >        final CFException ex = new CFException();
3501 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3502 >        final CompletableFuture<Integer> g = m.whenComplete
3503 >            (f,
3504 >             (Integer x, Throwable t) -> {
3505 >                threadAssertNull(x);
3506 >                threadAssertSame(t, ex);
3507 >                a.getAndIncrement();
3508 >            });
3509 >        f.completeExceptionally(ex);
3510 >        checkCompletedWithWrappedCFException(f, ex);
3511 >        checkCompletedWithWrappedCFException(g, ex);
3512          assertEquals(a.get(), 1);
3513 +        }
3514      }
3515  
3516      /**
3517       * If a whenComplete action throws an exception when triggered by
3518       * a normal completion, it completes exceptionally
3519       */
3520 <    public void testWhenComplete3() {
3521 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3522 <        CompletableFuture<Integer> g =
3110 <            f.whenComplete((Integer x, Throwable t) ->
3111 <                           { throw new CFException(); } );
3112 <        f.complete(three);
3113 <        checkCompletedNormally(f, three);
3114 <        assertTrue(g.isCompletedExceptionally());
3115 <        checkCompletedWithWrappedCFException(g);
3116 <    }
3117 <
3118 <    /**
3119 <     * whenCompleteAsync action executes on normal completion, propagating
3120 <     * source result.
3121 <     */
3122 <    public void testWhenCompleteAsync1() {
3123 <        final AtomicInteger a = new AtomicInteger();
3124 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3125 <        CompletableFuture<Integer> g =
3126 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3127 <        f.complete(three);
3128 <        checkCompletedNormally(f, three);
3129 <        checkCompletedNormally(g, three);
3130 <        assertEquals(a.get(), 1);
3131 <    }
3132 <
3133 <    /**
3134 <     * whenCompleteAsync action executes on exceptional completion, propagating
3135 <     * source result.
3136 <     */
3137 <    public void testWhenCompleteAsync2() {
3138 <        final AtomicInteger a = new AtomicInteger();
3139 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3140 <        CompletableFuture<Integer> g =
3141 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3142 <        f.completeExceptionally(new CFException());
3143 <        checkCompletedWithWrappedCFException(f);
3144 <        checkCompletedWithWrappedCFException(g);
3145 <    }
3146 <
3147 <    /**
3148 <     * If a whenCompleteAsync action throws an exception when
3149 <     * triggered by a normal completion, it completes exceptionally
3150 <     */
3151 <    public void testWhenCompleteAsync3() {
3152 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3153 <        CompletableFuture<Integer> g =
3154 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3155 <                           { throw new CFException(); } );
3156 <        f.complete(three);
3157 <        checkCompletedNormally(f, three);
3158 <        checkCompletedWithWrappedCFException(g);
3159 <    }
3520 >    public void testWhenComplete_actionFailed() {
3521 >        for (ExecutionMode m : ExecutionMode.values())
3522 >        for (Integer v1 : new Integer[] { 1, null }) {
3523  
3524 <    /**
3525 <     * whenCompleteAsync action executes on normal completion, propagating
3526 <     * source result.
3527 <     */
3528 <    public void testWhenCompleteAsync1e() {
3529 <        final AtomicInteger a = new AtomicInteger();
3530 <        ThreadExecutor exec = new ThreadExecutor();
3531 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3532 <        CompletableFuture<Integer> g =
3533 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3534 <                                exec);
3535 <        f.complete(three);
3536 <        checkCompletedNormally(f, three);
3174 <        checkCompletedNormally(g, three);
3175 <        assertEquals(a.get(), 1);
3524 >        final CFException ex = new CFException();
3525 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3526 >        final CompletableFuture<Integer> g = m.whenComplete
3527 >            (f,
3528 >             (Integer x, Throwable t) -> {
3529 >                threadAssertSame(x, v1);
3530 >                threadAssertNull(t);
3531 >                throw ex;
3532 >            });
3533 >        f.complete(v1);
3534 >        checkCompletedNormally(f, v1);
3535 >        checkCompletedWithWrappedCFException(g, ex);
3536 >        }
3537      }
3538  
3539      /**
3540 <     * whenCompleteAsync action executes on exceptional completion, propagating
3541 <     * source result.
3540 >     * If a whenComplete action throws an exception when triggered by
3541 >     * a source completion that also throws an exception, the source
3542 >     * exception takes precedence.
3543       */
3544 <    public void testWhenCompleteAsync2e() {
3545 <        final AtomicInteger a = new AtomicInteger();
3546 <        ThreadExecutor exec = new ThreadExecutor();
3185 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3186 <        CompletableFuture<Integer> g =
3187 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3188 <                                exec);
3189 <        f.completeExceptionally(new CFException());
3190 <        checkCompletedWithWrappedCFException(f);
3191 <        checkCompletedWithWrappedCFException(g);
3192 <    }
3544 >    public void testWhenComplete_actionFailedSourceFailed() {
3545 >        for (ExecutionMode m : ExecutionMode.values())
3546 >        for (Integer v1 : new Integer[] { 1, null }) {
3547  
3548 <    /**
3549 <     * If a whenCompleteAsync action throws an exception when triggered
3550 <     * by a normal completion, it completes exceptionally
3551 <     */
3552 <    public void testWhenCompleteAsync3e() {
3553 <        ThreadExecutor exec = new ThreadExecutor();
3554 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3555 <        CompletableFuture<Integer> g =
3556 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3557 <                                { throw new CFException(); },
3558 <                                exec);
3559 <        f.complete(three);
3560 <        checkCompletedNormally(f, three);
3561 <        checkCompletedWithWrappedCFException(g);
3548 >        final CFException ex1 = new CFException();
3549 >        final CFException ex2 = new CFException();
3550 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3551 >        final CompletableFuture<Integer> g = m.whenComplete
3552 >            (f,
3553 >             (Integer x, Throwable t) -> {
3554 >                threadAssertSame(t, ex1);
3555 >                threadAssertNull(x);
3556 >                throw ex2;
3557 >            });
3558 >        f.completeExceptionally(ex1);
3559 >        checkCompletedWithWrappedCFException(f, ex1);
3560 >        checkCompletedWithWrappedCFException(g, ex1);
3561 >        }
3562      }
3563  
3564      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines