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.38 by jsr166, Sun Jun 1 23:51:44 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          boolean ran;
379          public void run() { ran = true; }
# Line 427 | Line 461 | public class CompletableFutureTest exten
461                   BiConsumer<? super T,? super U> a) {
462                  return f.thenAcceptBoth(g, a);
463              }
464 +            public <T,U,V> CompletableFuture<V> thenCombine
465 +                (CompletableFuture<T> f,
466 +                 CompletionStage<? extends U> g,
467 +                 BiFunction<? super T,? super U,? extends V> a) {
468 +                return f.thenCombine(g, a);
469 +            }
470 +            public <T,U> CompletableFuture<U> applyToEither
471 +                (CompletableFuture<T> f,
472 +                 CompletionStage<? extends T> g,
473 +                 Function<? super T,U> a) {
474 +                return f.applyToEither(g, a);
475 +            }
476 +            public <T> CompletableFuture<Void> acceptEither
477 +                (CompletableFuture<T> f,
478 +                 CompletionStage<? extends T> g,
479 +                 Consumer<? super T> a) {
480 +                return f.acceptEither(g, a);
481 +            }
482 +            public <T> CompletableFuture<Void> runAfterEither
483 +                (CompletableFuture<T> f,
484 +                 CompletionStage<?> g,
485 +                 java.lang.Runnable a) {
486 +                return f.runAfterEither(g, a);
487 +            }
488 +            public <T,U> CompletableFuture<U> thenCompose
489 +                (CompletableFuture<T> f,
490 +                 Function<? super T,? extends CompletionStage<U>> a) {
491 +                return f.thenCompose(a);
492 +            }
493 +            public <T> CompletableFuture<T> whenComplete
494 +                (CompletableFuture<T> f,
495 +                 BiConsumer<? super T,? super Throwable> a) {
496 +                return f.whenComplete(a);
497 +            }
498          },
499  
500   //             /** Experimental way to do more testing */
# Line 454 | Line 522 | public class CompletableFutureTest exten
522                   BiConsumer<? super T,? super U> a) {
523                  return f.thenAcceptBothAsync(g, a);
524              }
525 +            public <T,U,V> CompletableFuture<V> thenCombine
526 +                (CompletableFuture<T> f,
527 +                 CompletionStage<? extends U> g,
528 +                 BiFunction<? super T,? super U,? extends V> a) {
529 +                return f.thenCombineAsync(g, a);
530 +            }
531 +            public <T,U> CompletableFuture<U> applyToEither
532 +                (CompletableFuture<T> f,
533 +                 CompletionStage<? extends T> g,
534 +                 Function<? super T,U> a) {
535 +                return f.applyToEitherAsync(g, a);
536 +            }
537 +            public <T> CompletableFuture<Void> acceptEither
538 +                (CompletableFuture<T> f,
539 +                 CompletionStage<? extends T> g,
540 +                 Consumer<? super T> a) {
541 +                return f.acceptEitherAsync(g, a);
542 +            }
543 +            public <T> CompletableFuture<Void> runAfterEither
544 +                (CompletableFuture<T> f,
545 +                 CompletionStage<?> g,
546 +                 java.lang.Runnable a) {
547 +                return f.runAfterEitherAsync(g, a);
548 +            }
549 +            public <T,U> CompletableFuture<U> thenCompose
550 +                (CompletableFuture<T> f,
551 +                 Function<? super T,? extends CompletionStage<U>> a) {
552 +                return f.thenComposeAsync(a);
553 +            }
554 +            public <T> CompletableFuture<T> whenComplete
555 +                (CompletableFuture<T> f,
556 +                 BiConsumer<? super T,? super Throwable> a) {
557 +                return f.whenCompleteAsync(a);
558 +            }
559          },
560  
561   //         REVERSE_DEFAULT_ASYNC {
# Line 480 | Line 582 | public class CompletableFutureTest exten
582                   BiConsumer<? super T,? super U> a) {
583                  return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
584              }
585 +            public <T,U,V> CompletableFuture<V> thenCombine
586 +                (CompletableFuture<T> f,
587 +                 CompletionStage<? extends U> g,
588 +                 BiFunction<? super T,? super U,? extends V> a) {
589 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
590 +            }
591 +            public <T,U> CompletableFuture<U> applyToEither
592 +                (CompletableFuture<T> f,
593 +                 CompletionStage<? extends T> g,
594 +                 Function<? super T,U> a) {
595 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
596 +            }
597 +            public <T> CompletableFuture<Void> acceptEither
598 +                (CompletableFuture<T> f,
599 +                 CompletionStage<? extends T> g,
600 +                 Consumer<? super T> a) {
601 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
602 +            }
603 +            public <T> CompletableFuture<Void> runAfterEither
604 +                (CompletableFuture<T> f,
605 +                 CompletionStage<?> g,
606 +                 java.lang.Runnable a) {
607 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
608 +            }
609 +            public <T,U> CompletableFuture<U> thenCompose
610 +                (CompletableFuture<T> f,
611 +                 Function<? super T,? extends CompletionStage<U>> a) {
612 +                return f.thenComposeAsync(a, new ThreadExecutor());
613 +            }
614 +            public <T> CompletableFuture<T> whenComplete
615 +                (CompletableFuture<T> f,
616 +                 BiConsumer<? super T,? super Throwable> a) {
617 +                return f.whenCompleteAsync(a, new ThreadExecutor());
618 +            }
619          };
620  
621          public abstract <T,U> CompletableFuture<Void> runAfterBoth
# Line 488 | Line 624 | public class CompletableFutureTest exten
624              (CompletableFuture<T> f,
625               CompletionStage<? extends U> g,
626               BiConsumer<? super T,? super U> a);
627 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
628 +            (CompletableFuture<T> f,
629 +             CompletionStage<? extends U> g,
630 +             BiFunction<? super T,? super U,? extends V> a);
631 +        public abstract <T,U> CompletableFuture<U> applyToEither
632 +            (CompletableFuture<T> f,
633 +             CompletionStage<? extends T> g,
634 +             Function<? super T,U> a);
635 +        public abstract <T> CompletableFuture<Void> acceptEither
636 +            (CompletableFuture<T> f,
637 +             CompletionStage<? extends T> g,
638 +             Consumer<? super T> a);
639 +        public abstract <T> CompletableFuture<Void> runAfterEither
640 +            (CompletableFuture<T> f,
641 +             CompletionStage<?> g,
642 +             java.lang.Runnable a);
643 +        public abstract <T,U> CompletableFuture<U> thenCompose
644 +            (CompletableFuture<T> f,
645 +             Function<? super T,? extends CompletionStage<U>> a);
646 +        public abstract <T> CompletableFuture<T> whenComplete
647 +            (CompletableFuture<T> f,
648 +             BiConsumer<? super T,? super Throwable> a);
649 +
650 +
651      }
652  
653      /**
# Line 740 | Line 900 | public class CompletableFutureTest exten
900          CompletableFuture<Void> g = f.thenAccept(r);
901          f.complete(one);
902          checkCompletedNormally(g, null);
903 <        assertEquals(r.value, 2);
903 >        assertEquals(r.value, (Integer) 2);
904      }
905  
906      /**
# Line 782 | Line 942 | public class CompletableFutureTest exten
942       * thenCombine result completes normally after normal completion
943       * of sources
944       */
945 <    public void testThenCombine() {
946 <        CompletableFuture<Integer> f, g, h;
945 >    public void testThenCombine_normalCompletion1() {
946 >        for (ExecutionMode m : ExecutionMode.values())
947 >        for (Integer v1 : new Integer[] { 1, null })
948 >        for (Integer v2 : new Integer[] { 2, null }) {
949  
950 <        f = new CompletableFuture<>();
951 <        g = new CompletableFuture<>();
952 <        h = f.thenCombine(g, subtract);
953 <        f.complete(3);
950 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
951 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
952 >        final SubtractFunction r = new SubtractFunction();
953 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
954 >
955 >        f.complete(v1);
956          checkIncomplete(h);
957 <        g.complete(1);
958 <        checkCompletedNormally(h, 2);
957 >        assertFalse(r.ran());
958 >        g.complete(v2);
959  
960 <        f = new CompletableFuture<>();
961 <        g = new CompletableFuture<>();
962 <        h = f.thenCombine(g, subtract);
963 <        g.complete(1);
960 >        checkCompletedNormally(h, subtract(v1, v2));
961 >        checkCompletedNormally(f, v1);
962 >        checkCompletedNormally(g, v2);
963 >        }
964 >    }
965 >
966 >    public void testThenCombine_normalCompletion2() {
967 >        for (ExecutionMode m : ExecutionMode.values())
968 >        for (Integer v1 : new Integer[] { 1, null })
969 >        for (Integer v2 : new Integer[] { 2, null }) {
970 >
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
973 >        final SubtractFunction r = new SubtractFunction();
974 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
975 >
976 >        g.complete(v2);
977          checkIncomplete(h);
978 <        f.complete(3);
979 <        checkCompletedNormally(h, 2);
978 >        assertFalse(r.ran());
979 >        f.complete(v1);
980  
981 <        f = new CompletableFuture<>();
982 <        g = new CompletableFuture<>();
983 <        g.complete(1);
984 <        f.complete(3);
985 <        h = f.thenCombine(g, subtract);
986 <        checkCompletedNormally(h, 2);
981 >        checkCompletedNormally(h, subtract(v1, v2));
982 >        checkCompletedNormally(f, v1);
983 >        checkCompletedNormally(g, v2);
984 >        }
985 >    }
986 >
987 >    public void testThenCombine_normalCompletion3() {
988 >        for (ExecutionMode m : ExecutionMode.values())
989 >        for (Integer v1 : new Integer[] { 1, null })
990 >        for (Integer v2 : new Integer[] { 2, null }) {
991 >
992 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
993 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
994 >        final SubtractFunction r = new SubtractFunction();
995 >
996 >        g.complete(v2);
997 >        f.complete(v1);
998 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
999 >
1000 >        checkCompletedNormally(h, subtract(v1, v2));
1001 >        checkCompletedNormally(f, v1);
1002 >        checkCompletedNormally(g, v2);
1003 >        }
1004 >    }
1005 >
1006 >    public void testThenCombine_normalCompletion4() {
1007 >        for (ExecutionMode m : ExecutionMode.values())
1008 >        for (Integer v1 : new Integer[] { 1, null })
1009 >        for (Integer v2 : new Integer[] { 2, null }) {
1010 >
1011 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1012 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1013 >        final SubtractFunction r = new SubtractFunction();
1014 >
1015 >        f.complete(v1);
1016 >        g.complete(v2);
1017 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1018 >
1019 >        checkCompletedNormally(h, subtract(v1, v2));
1020 >        checkCompletedNormally(f, v1);
1021 >        checkCompletedNormally(g, v2);
1022 >        }
1023      }
1024  
1025      /**
1026       * thenCombine result completes exceptionally after exceptional
1027       * completion of either source
1028       */
1029 <    public void testThenCombine2() {
1030 <        CompletableFuture<Integer> f, g, h;
1029 >    public void testThenCombine_exceptionalCompletion1() {
1030 >        for (ExecutionMode m : ExecutionMode.values())
1031 >        for (Integer v1 : new Integer[] { 1, null }) {
1032  
1033 <        f = new CompletableFuture<>();
1034 <        g = new CompletableFuture<>();
1035 <        h = f.thenCombine(g, subtract);
1036 <        f.completeExceptionally(new CFException());
1033 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1034 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1035 >        final SubtractFunction r = new SubtractFunction();
1036 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1037 >        final CFException ex = new CFException();
1038 >
1039 >        f.completeExceptionally(ex);
1040          checkIncomplete(h);
1041 <        g.complete(1);
825 <        checkCompletedWithWrappedCFException(h);
1041 >        g.complete(v1);
1042  
1043 <        f = new CompletableFuture<>();
1044 <        g = new CompletableFuture<>();
1045 <        h = f.thenCombine(g, subtract);
1046 <        g.completeExceptionally(new CFException());
1043 >        checkCompletedWithWrappedCFException(h, ex);
1044 >        checkCompletedWithWrappedCFException(f, ex);
1045 >        assertFalse(r.ran());
1046 >        checkCompletedNormally(g, v1);
1047 >        }
1048 >    }
1049 >
1050 >    public void testThenCombine_exceptionalCompletion2() {
1051 >        for (ExecutionMode m : ExecutionMode.values())
1052 >        for (Integer v1 : new Integer[] { 1, null }) {
1053 >
1054 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1055 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1056 >        final SubtractFunction r = new SubtractFunction();
1057 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1058 >        final CFException ex = new CFException();
1059 >
1060 >        g.completeExceptionally(ex);
1061          checkIncomplete(h);
1062 <        f.complete(3);
833 <        checkCompletedWithWrappedCFException(h);
1062 >        f.complete(v1);
1063  
1064 <        f = new CompletableFuture<>();
1065 <        g = new CompletableFuture<>();
1066 <        f.complete(3);
1067 <        g.completeExceptionally(new CFException());
1068 <        h = f.thenCombine(g, subtract);
1069 <        checkCompletedWithWrappedCFException(h);
1064 >        checkCompletedWithWrappedCFException(h, ex);
1065 >        checkCompletedWithWrappedCFException(g, ex);
1066 >        assertFalse(r.ran());
1067 >        checkCompletedNormally(f, v1);
1068 >        }
1069 >    }
1070  
1071 <        f = new CompletableFuture<>();
1072 <        g = new CompletableFuture<>();
1073 <        f.completeExceptionally(new CFException());
1074 <        g.complete(3);
1075 <        h = f.thenCombine(g, subtract);
1076 <        checkCompletedWithWrappedCFException(h);
1071 >    public void testThenCombine_exceptionalCompletion3() {
1072 >        for (ExecutionMode m : ExecutionMode.values())
1073 >        for (Integer v1 : new Integer[] { 1, null }) {
1074 >
1075 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1076 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1077 >        final SubtractFunction r = new SubtractFunction();
1078 >        final CFException ex = new CFException();
1079 >
1080 >        g.completeExceptionally(ex);
1081 >        f.complete(v1);
1082 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1083 >
1084 >        checkCompletedWithWrappedCFException(h, ex);
1085 >        checkCompletedWithWrappedCFException(g, ex);
1086 >        assertFalse(r.ran());
1087 >        checkCompletedNormally(f, v1);
1088 >        }
1089 >    }
1090 >
1091 >    public void testThenCombine_exceptionalCompletion4() {
1092 >        for (ExecutionMode m : ExecutionMode.values())
1093 >        for (Integer v1 : new Integer[] { 1, null }) {
1094 >
1095 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1096 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1097 >        final SubtractFunction r = new SubtractFunction();
1098 >        final CFException ex = new CFException();
1099 >
1100 >        f.completeExceptionally(ex);
1101 >        g.complete(v1);
1102 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1103 >
1104 >        checkCompletedWithWrappedCFException(h, ex);
1105 >        checkCompletedWithWrappedCFException(f, ex);
1106 >        assertFalse(r.ran());
1107 >        checkCompletedNormally(g, v1);
1108 >        }
1109      }
1110  
1111      /**
1112       * thenCombine result completes exceptionally if action does
1113       */
1114 <    public void testThenCombine3() {
1115 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1116 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1117 <        FailingBiFunction r = new FailingBiFunction();
1118 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1119 <        f.complete(one);
1120 <        checkIncomplete(g);
1121 <        assertFalse(r.ran);
1122 <        f2.complete(two);
1123 <        checkCompletedWithWrappedCFException(g);
1124 <        assertTrue(r.ran);
1114 >    public void testThenCombine_actionFailed1() {
1115 >        for (ExecutionMode m : ExecutionMode.values())
1116 >        for (Integer v1 : new Integer[] { 1, null })
1117 >        for (Integer v2 : new Integer[] { 2, null }) {
1118 >
1119 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1120 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1121 >        final FailingBiFunction r = new FailingBiFunction();
1122 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1123 >
1124 >        f.complete(v1);
1125 >        checkIncomplete(h);
1126 >        g.complete(v2);
1127 >
1128 >        checkCompletedWithWrappedCFException(h);
1129 >        checkCompletedNormally(f, v1);
1130 >        checkCompletedNormally(g, v2);
1131 >        }
1132 >    }
1133 >
1134 >    public void testThenCombine_actionFailed2() {
1135 >        for (ExecutionMode m : ExecutionMode.values())
1136 >        for (Integer v1 : new Integer[] { 1, null })
1137 >        for (Integer v2 : new Integer[] { 2, null }) {
1138 >
1139 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1140 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1141 >        final FailingBiFunction r = new FailingBiFunction();
1142 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1143 >
1144 >        g.complete(v2);
1145 >        checkIncomplete(h);
1146 >        f.complete(v1);
1147 >
1148 >        checkCompletedWithWrappedCFException(h);
1149 >        checkCompletedNormally(f, v1);
1150 >        checkCompletedNormally(g, v2);
1151 >        }
1152      }
1153  
1154      /**
1155       * thenCombine result completes exceptionally if either source cancelled
1156       */
1157 <    public void testThenCombine4() {
1158 <        CompletableFuture<Integer> f, g, h;
1157 >    public void testThenCombine_sourceCancelled1() {
1158 >        for (ExecutionMode m : ExecutionMode.values())
1159 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1160 >        for (Integer v1 : new Integer[] { 1, null }) {
1161  
1162 <        f = new CompletableFuture<>();
1163 <        g = new CompletableFuture<>();
1164 <        h = f.thenCombine(g, subtract);
1165 <        assertTrue(f.cancel(true));
1162 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1163 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1164 >        final SubtractFunction r = new SubtractFunction();
1165 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1166 >
1167 >        assertTrue(f.cancel(mayInterruptIfRunning));
1168          checkIncomplete(h);
1169 <        g.complete(1);
1169 >        g.complete(v1);
1170 >
1171          checkCompletedWithWrappedCancellationException(h);
1172 +        checkCancelled(f);
1173 +        assertFalse(r.ran());
1174 +        checkCompletedNormally(g, v1);
1175 +        }
1176 +    }
1177  
1178 <        f = new CompletableFuture<>();
1179 <        g = new CompletableFuture<>();
1180 <        h = f.thenCombine(g, subtract);
1181 <        assertTrue(g.cancel(true));
1178 >    public void testThenCombine_sourceCancelled2() {
1179 >        for (ExecutionMode m : ExecutionMode.values())
1180 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1181 >        for (Integer v1 : new Integer[] { 1, null }) {
1182 >
1183 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1184 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1185 >        final SubtractFunction r = new SubtractFunction();
1186 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1187 >
1188 >        assertTrue(g.cancel(mayInterruptIfRunning));
1189          checkIncomplete(h);
1190 <        f.complete(3);
1190 >        f.complete(v1);
1191 >
1192          checkCompletedWithWrappedCancellationException(h);
1193 +        checkCancelled(g);
1194 +        assertFalse(r.ran());
1195 +        checkCompletedNormally(f, v1);
1196 +        }
1197 +    }
1198 +
1199 +    public void testThenCombine_sourceCancelled3() {
1200 +        for (ExecutionMode m : ExecutionMode.values())
1201 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1202 +        for (Integer v1 : new Integer[] { 1, null }) {
1203 +
1204 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1206 +        final SubtractFunction r = new SubtractFunction();
1207 +
1208 +        assertTrue(g.cancel(mayInterruptIfRunning));
1209 +        f.complete(v1);
1210 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1211  
888        f = new CompletableFuture<>();
889        g = new CompletableFuture<>();
890        assertTrue(f.cancel(true));
891        assertTrue(g.cancel(true));
892        h = f.thenCombine(g, subtract);
1212          checkCompletedWithWrappedCancellationException(h);
1213 +        checkCancelled(g);
1214 +        assertFalse(r.ran());
1215 +        checkCompletedNormally(f, v1);
1216 +        }
1217 +    }
1218 +
1219 +    public void testThenCombine_sourceCancelled4() {
1220 +        for (ExecutionMode m : ExecutionMode.values())
1221 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1222 +        for (Integer v1 : new Integer[] { 1, null }) {
1223 +
1224 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1225 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1226 +        final SubtractFunction r = new SubtractFunction();
1227 +
1228 +        assertTrue(f.cancel(mayInterruptIfRunning));
1229 +        g.complete(v1);
1230 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1231 +
1232 +        checkCompletedWithWrappedCancellationException(h);
1233 +        checkCancelled(f);
1234 +        assertFalse(r.ran());
1235 +        checkCompletedNormally(g, v1);
1236 +        }
1237      }
1238  
1239      /**
# Line 909 | Line 1252 | public class CompletableFutureTest exten
1252  
1253          f.complete(v1);
1254          checkIncomplete(h);
1255 <        assertEquals(r.value, 0);
1255 >        assertFalse(r.ran());
1256          g.complete(v2);
1257  
1258          checkCompletedNormally(h, null);
1259 <        assertEquals(r.value, r.subtract(v1, v2));
1259 >        assertEquals(r.value, subtract(v1, v2));
1260          checkCompletedNormally(f, v1);
1261          checkCompletedNormally(g, v2);
1262          }
# Line 931 | Line 1274 | public class CompletableFutureTest exten
1274  
1275          g.complete(v2);
1276          checkIncomplete(h);
1277 <        assertEquals(r.value, 0);
1277 >        assertFalse(r.ran());
1278          f.complete(v1);
1279  
1280          checkCompletedNormally(h, null);
1281 <        assertEquals(r.value, r.subtract(v1, v2));
1281 >        assertEquals(r.value, subtract(v1, v2));
1282          checkCompletedNormally(f, v1);
1283          checkCompletedNormally(g, v2);
1284          }
# Line 955 | Line 1298 | public class CompletableFutureTest exten
1298          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1299  
1300          checkCompletedNormally(h, null);
1301 <        assertEquals(r.value, r.subtract(v1, v2));
1301 >        assertEquals(r.value, subtract(v1, v2));
1302          checkCompletedNormally(f, v1);
1303          checkCompletedNormally(g, v2);
1304          }
# Line 975 | Line 1318 | public class CompletableFutureTest exten
1318          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1319  
1320          checkCompletedNormally(h, null);
1321 <        assertEquals(r.value, r.subtract(v1, v2));
1321 >        assertEquals(r.value, subtract(v1, v2));
1322          checkCompletedNormally(f, v1);
1323          checkCompletedNormally(g, v2);
1324          }
# Line 1576 | Line 1919 | public class CompletableFutureTest exten
1919          checkCompletedNormally(g, null);
1920          f2.complete(one);
1921          checkCompletedNormally(g, null);
1922 <        assertEquals(r.value, 2);
1922 >        assertEquals(r.value, (Integer) 2);
1923  
1924          r = new IncAction();
1925          f = new CompletableFuture<>();
# Line 1584 | Line 1927 | public class CompletableFutureTest exten
1927          f2 = new CompletableFuture<>();
1928          g = f.acceptEither(f2, r);
1929          checkCompletedNormally(g, null);
1930 <        assertEquals(r.value, 2);
1930 >        assertEquals(r.value, (Integer) 2);
1931      }
1932  
1933      /**
# Line 1894 | Line 2237 | public class CompletableFutureTest exten
2237          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2238          f.complete(one);
2239          checkCompletedNormally(g, null);
2240 <        assertEquals(r.value, 2);
2240 >        assertEquals(r.value, (Integer) 2);
2241      }
2242  
2243      /**
# Line 1932 | Line 2275 | public class CompletableFutureTest exten
2275      }
2276  
2277      /**
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    /**
2278       * applyToEitherAsync result completes normally after normal
2279       * completion of sources
2280       */
# Line 2123 | Line 2351 | public class CompletableFutureTest exten
2351          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2352          f.complete(one);
2353          checkCompletedNormally(g, null);
2354 <        assertEquals(r.value, 2);
2354 >        assertEquals(r.value, (Integer) 2);
2355  
2356          r = new IncAction();
2357          f = new CompletableFuture<>();
# Line 2131 | Line 2359 | public class CompletableFutureTest exten
2359          f2 = new CompletableFuture<>();
2360          g = f.acceptEitherAsync(f2, r);
2361          checkCompletedNormally(g, null);
2362 <        assertEquals(r.value, 2);
2362 >        assertEquals(r.value, (Integer) 2);
2363      }
2364  
2365      /**
# Line 2448 | Line 2676 | public class CompletableFutureTest exten
2676          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2677          f.complete(one);
2678          checkCompletedNormally(g, null);
2679 <        assertEquals(r.value, 2);
2679 >        assertEquals(r.value, (Integer) 2);
2680      }
2681  
2682      /**
# Line 2486 | Line 2714 | public class CompletableFutureTest exten
2714      }
2715  
2716      /**
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    /**
2717       * applyToEitherAsync result completes normally after normal
2718       * completion of sources
2719       */
# Line 2691 | Line 2790 | public class CompletableFutureTest exten
2790          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2791          f.complete(one);
2792          checkCompletedNormally(g, null);
2793 <        assertEquals(r.value, 2);
2793 >        assertEquals(r.value, (Integer) 2);
2794  
2795          r = new IncAction();
2796          f = new CompletableFuture<>();
# Line 2699 | Line 2798 | public class CompletableFutureTest exten
2798          f2 = new CompletableFuture<>();
2799          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2800          checkCompletedNormally(g, null);
2801 <        assertEquals(r.value, 2);
2801 >        assertEquals(r.value, (Integer) 2);
2802      }
2803  
2804      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines