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.25 by jsr166, Sat Apr 20 23:28:35 2013 UTC vs.
Revision 1.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC

# Line 68 | Line 68 | public class CompletableFutureTest exten
68          } catch (Throwable fail) { threadUnexpectedException(fail); }
69          assertTrue(f.isDone());
70          assertFalse(f.isCancelled());
71 +        assertFalse(f.isCompletedExceptionally());
72          assertTrue(f.toString().contains("[Completed normally]"));
73      }
74  
# Line 101 | Line 102 | public class CompletableFutureTest exten
102          assertTrue(f.toString().contains("[Completed exceptionally]"));
103      }
104  
105 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
106 +                                              CFException ex) {
107 +        try {
108 +            f.get(LONG_DELAY_MS, MILLISECONDS);
109 +            shouldThrow();
110 +        } catch (ExecutionException success) {
111 +            assertSame(ex, success.getCause());
112 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
113 +        try {
114 +            f.join();
115 +            shouldThrow();
116 +        } catch (CompletionException success) {
117 +            assertSame(ex, success.getCause());
118 +        }
119 +        try {
120 +            f.getNow(null);
121 +            shouldThrow();
122 +        } catch (CompletionException success) {
123 +            assertSame(ex, success.getCause());
124 +        }
125 +        try {
126 +            f.get();
127 +            shouldThrow();
128 +        } catch (ExecutionException success) {
129 +            assertSame(ex, success.getCause());
130 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
131 +        assertTrue(f.isDone());
132 +        assertFalse(f.isCancelled());
133 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
134 +    }
135 +
136      void checkCancelled(CompletableFuture<?> f) {
137          try {
138              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 121 | Line 153 | public class CompletableFutureTest exten
153          } catch (CancellationException success) {
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155          assertTrue(f.isDone());
156 +        assertTrue(f.isCompletedExceptionally());
157          assertTrue(f.isCancelled());
158          assertTrue(f.toString().contains("[Completed exceptionally]"));
159      }
# Line 152 | Line 185 | public class CompletableFutureTest exten
185          } catch (Throwable fail) { threadUnexpectedException(fail); }
186          assertTrue(f.isDone());
187          assertFalse(f.isCancelled());
188 +        assertTrue(f.isCompletedExceptionally());
189          assertTrue(f.toString().contains("[Completed exceptionally]"));
190      }
191  
# Line 256 | Line 290 | public class CompletableFutureTest exten
290          assertEquals(g.getNumberOfDependents(), 0);
291      }
292  
259
293      /**
294       * toString indicates current completion state
295       */
# Line 371 | Line 404 | public class CompletableFutureTest exten
404          }
405      }
406  
374
407      /**
408       * exceptionally action completes with function value on source
409 <     * exception;  otherwise with source value
409 >     * exception; otherwise with source value
410       */
411      public void testExceptionally() {
412          CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 660 | Line 692 | public class CompletableFutureTest exten
692          checkCompletedWithWrappedCancellationException(g);
693      }
694  
663
695      /**
696       * thenCombine result completes normally after normal completion
697       * of sources
# Line 919 | Line 950 | public class CompletableFutureTest exten
950       * runAfterBoth result completes normally after normal
951       * completion of sources
952       */
953 <    public void testRunAfterBoth() {
954 <        CompletableFuture<Integer> f, g;
955 <        CompletableFuture<Void> h;
956 <        Noop r;
953 >    public void testRunAfterBoth_normalCompletion1() {
954 >        for (Integer v1 : new Integer[] { 1, null })
955 >        for (Integer v2 : new Integer[] { 2, null }) {
956 >
957 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
958 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
959 >        final Noop r = new Noop();
960 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
961  
962 <        f = new CompletableFuture<>();
928 <        g = new CompletableFuture<>();
929 <        h = f.runAfterBoth(g, r = new Noop());
930 <        f.complete(3);
962 >        f.complete(v1);
963          checkIncomplete(h);
964 <        g.complete(1);
964 >        assertFalse(r.ran);
965 >        g.complete(v2);
966 >
967          checkCompletedNormally(h, null);
968          assertTrue(r.ran);
969 +        checkCompletedNormally(f, v1);
970 +        checkCompletedNormally(g, v2);
971 +        }
972 +    }
973  
974 <        f = new CompletableFuture<>();
975 <        g = new CompletableFuture<>();
976 <        h = f.runAfterBoth(g, r = new Noop());
977 <        g.complete(1);
974 >    public void testRunAfterBoth_normalCompletion2() {
975 >        for (Integer v1 : new Integer[] { 1, null })
976 >        for (Integer v2 : new Integer[] { 2, null }) {
977 >
978 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
979 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
980 >        final Noop r = new Noop();
981 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
982 >
983 >        g.complete(v2);
984          checkIncomplete(h);
985 <        f.complete(3);
985 >        assertFalse(r.ran);
986 >        f.complete(v1);
987 >
988          checkCompletedNormally(h, null);
989          assertTrue(r.ran);
990 +        checkCompletedNormally(f, v1);
991 +        checkCompletedNormally(g, v2);
992 +        }
993 +    }
994 +
995 +    public void testRunAfterBoth_normalCompletion3() {
996 +        for (Integer v1 : new Integer[] { 1, null })
997 +        for (Integer v2 : new Integer[] { 2, null }) {
998 +
999 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1000 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1001 +        final Noop r = new Noop();
1002 +
1003 +        g.complete(v2);
1004 +        f.complete(v1);
1005 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1006  
945        f = new CompletableFuture<>();
946        g = new CompletableFuture<>();
947        g.complete(1);
948        f.complete(3);
949        h = f.runAfterBoth(g, r = new Noop());
1007          checkCompletedNormally(h, null);
1008          assertTrue(r.ran);
1009 +        checkCompletedNormally(f, v1);
1010 +        checkCompletedNormally(g, v2);
1011 +        }
1012 +    }
1013 +
1014 +    public void testRunAfterBoth_normalCompletion4() {
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 Noop r = new Noop();
1021 +
1022 +        f.complete(v1);
1023 +        g.complete(v2);
1024 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1025 +
1026 +        checkCompletedNormally(h, null);
1027 +        assertTrue(r.ran);
1028 +        checkCompletedNormally(f, v1);
1029 +        checkCompletedNormally(g, v2);
1030 +        }
1031      }
1032  
1033      /**
1034       * runAfterBoth result completes exceptionally after exceptional
1035       * completion of either source
1036       */
1037 <    public void testRunAfterBoth2() {
1038 <        CompletableFuture<Integer> f, g;
960 <        CompletableFuture<Void> h;
961 <        Noop r;
1037 >    public void testRunAfterBoth_exceptionalCompletion1() {
1038 >        for (Integer v1 : new Integer[] { 1, null }) {
1039  
1040 <        f = new CompletableFuture<>();
1041 <        g = new CompletableFuture<>();
1042 <        h = f.runAfterBoth(g, r = new Noop());
1043 <        f.completeExceptionally(new CFException());
1040 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1041 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1042 >        final Noop r = new Noop();
1043 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1044 >        final CFException ex = new CFException();
1045 >
1046 >        f.completeExceptionally(ex);
1047          checkIncomplete(h);
1048 <        g.complete(1);
1049 <        checkCompletedWithWrappedCFException(h);
1048 >        g.complete(v1);
1049 >
1050 >        checkCompletedWithWrappedCFException(h, ex);
1051 >        checkCompletedWithWrappedCFException(f, ex);
1052          assertFalse(r.ran);
1053 +        checkCompletedNormally(g, v1);
1054 +        }
1055 +    }
1056  
1057 <        f = new CompletableFuture<>();
1058 <        g = new CompletableFuture<>();
1059 <        h = f.runAfterBoth(g, r = new Noop());
1060 <        g.completeExceptionally(new CFException());
1057 >    public void testRunAfterBoth_exceptionalCompletion2() {
1058 >        for (Integer v1 : new Integer[] { 1, null }) {
1059 >
1060 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1061 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1062 >        final Noop r = new Noop();
1063 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1064 >        final CFException ex = new CFException();
1065 >
1066 >        g.completeExceptionally(ex);
1067          checkIncomplete(h);
1068 <        f.complete(3);
1069 <        checkCompletedWithWrappedCFException(h);
1068 >        f.complete(v1);
1069 >
1070 >        checkCompletedWithWrappedCFException(h, ex);
1071 >        checkCompletedWithWrappedCFException(g, ex);
1072          assertFalse(r.ran);
1073 +        checkCompletedNormally(f, v1);
1074 +        }
1075 +    }
1076  
1077 <        f = new CompletableFuture<>();
1078 <        g = new CompletableFuture<>();
1079 <        g.completeExceptionally(new CFException());
1080 <        f.complete(3);
1081 <        h = f.runAfterBoth(g, r = new Noop());
1082 <        checkCompletedWithWrappedCFException(h);
1077 >    public void testRunAfterBoth_exceptionalCompletion3() {
1078 >        for (Integer v1 : new Integer[] { 1, null }) {
1079 >
1080 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1081 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1082 >        final Noop r = new Noop();
1083 >        final CFException ex = new CFException();
1084 >
1085 >        g.completeExceptionally(ex);
1086 >        f.complete(v1);
1087 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1088 >
1089 >        checkCompletedWithWrappedCFException(h, ex);
1090 >        checkCompletedWithWrappedCFException(g, ex);
1091          assertFalse(r.ran);
1092 +        checkCompletedNormally(f, v1);
1093 +        }
1094 +    }
1095  
1096 <        f = new CompletableFuture<>();
1097 <        g = new CompletableFuture<>();
1098 <        f.completeExceptionally(new CFException());
1099 <        g.complete(1);
1100 <        h = f.runAfterBoth(g, r = new Noop());
1101 <        checkCompletedWithWrappedCFException(h);
1096 >    public void testRunAfterBoth_exceptionalCompletion4() {
1097 >        for (Integer v1 : new Integer[] { 1, null }) {
1098 >
1099 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1100 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1101 >        final Noop r = new Noop();
1102 >        final CFException ex = new CFException();
1103 >
1104 >        f.completeExceptionally(ex);
1105 >        g.complete(v1);
1106 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1107 >
1108 >        checkCompletedWithWrappedCFException(h, ex);
1109 >        checkCompletedWithWrappedCFException(f, ex);
1110          assertFalse(r.ran);
1111 +        checkCompletedNormally(g, v1);
1112 +        }
1113      }
1114  
1115      /**
1116       * runAfterBoth result completes exceptionally if action does
1117       */
1118 <    public void testRunAfterBoth3() {
1119 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1120 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1121 <        FailingNoop r = new FailingNoop();
1122 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1123 <        f.complete(one);
1124 <        checkIncomplete(g);
1125 <        f2.complete(two);
1126 <        checkCompletedWithWrappedCFException(g);
1118 >    public void testRunAfterBoth_actionFailed1() {
1119 >        for (Integer v1 : new Integer[] { 1, null })
1120 >        for (Integer v2 : new Integer[] { 2, null }) {
1121 >
1122 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1123 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1124 >        final FailingNoop r = new FailingNoop();
1125 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1126 >
1127 >        f.complete(v1);
1128 >        checkIncomplete(h);
1129 >        g.complete(v2);
1130 >
1131 >        checkCompletedWithWrappedCFException(h);
1132 >        checkCompletedNormally(f, v1);
1133 >        checkCompletedNormally(g, v2);
1134 >        }
1135 >    }
1136 >
1137 >    public void testRunAfterBoth_actionFailed2() {
1138 >        for (Integer v1 : new Integer[] { 1, null })
1139 >        for (Integer v2 : new Integer[] { 2, null }) {
1140 >
1141 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1142 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1143 >        final FailingNoop r = new FailingNoop();
1144 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1145 >
1146 >        g.complete(v2);
1147 >        checkIncomplete(h);
1148 >        f.complete(v1);
1149 >
1150 >        checkCompletedWithWrappedCFException(h);
1151 >        checkCompletedNormally(f, v1);
1152 >        checkCompletedNormally(g, v2);
1153 >        }
1154      }
1155  
1156      /**
1157       * runAfterBoth result completes exceptionally if either source cancelled
1158       */
1159 <    public void testRunAfterBoth4() {
1160 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1161 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1162 <        Noop r = new Noop();
1163 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1164 <        assertTrue(f.cancel(true));
1165 <        f2.complete(two);
1166 <        checkCompletedWithWrappedCancellationException(g);
1167 <        f = new CompletableFuture<>();
1168 <        f2 = new CompletableFuture<>();
1169 <        r = new Noop();
1170 <        g = f.runAfterBoth(f2, r);
1171 <        f.complete(one);
1172 <        assertTrue(f2.cancel(true));
1173 <        checkCompletedWithWrappedCancellationException(g);
1159 >    public void testRunAfterBoth_sourceCancelled1() {
1160 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1161 >        for (Integer v1 : new Integer[] { 1, null }) {
1162 >
1163 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1165 >        final Noop r = new Noop();
1166 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1167 >
1168 >        assertTrue(f.cancel(mayInterruptIfRunning));
1169 >        checkIncomplete(h);
1170 >        g.complete(v1);
1171 >
1172 >        checkCompletedWithWrappedCancellationException(h);
1173 >        checkCancelled(f);
1174 >        assertFalse(r.ran);
1175 >        checkCompletedNormally(g, v1);
1176 >        }
1177 >    }
1178 >
1179 >    public void testRunAfterBoth_sourceCancelled2() {
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 Noop r = new Noop();
1186 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1187 >
1188 >        assertTrue(g.cancel(mayInterruptIfRunning));
1189 >        checkIncomplete(h);
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 testRunAfterBoth_sourceCancelled3() {
1200 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1201 >        for (Integer v1 : new Integer[] { 1, null }) {
1202 >
1203 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1204 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1205 >        final Noop r = new Noop();
1206 >
1207 >        assertTrue(g.cancel(mayInterruptIfRunning));
1208 >        f.complete(v1);
1209 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1210 >
1211 >        checkCompletedWithWrappedCancellationException(h);
1212 >        checkCancelled(g);
1213 >        assertFalse(r.ran);
1214 >        checkCompletedNormally(f, v1);
1215 >        }
1216 >    }
1217 >
1218 >    public void testRunAfterBoth_sourceCancelled4() {
1219 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1220 >        for (Integer v1 : new Integer[] { 1, null }) {
1221 >
1222 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1224 >        final Noop r = new Noop();
1225 >
1226 >        assertTrue(f.cancel(mayInterruptIfRunning));
1227 >        g.complete(v1);
1228 >        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1229 >
1230 >        checkCompletedWithWrappedCancellationException(h);
1231 >        checkCancelled(f);
1232 >        assertFalse(r.ran);
1233 >        checkCompletedNormally(g, v1);
1234 >        }
1235      }
1236  
1237      /**
# Line 1168 | Line 1373 | public class CompletableFutureTest exten
1373          checkCompletedWithWrappedCancellationException(g);
1374      }
1375  
1171
1376      /**
1377       * runAfterEither result completes normally after normal completion
1378       * of either source
# Line 1317 | Line 1521 | public class CompletableFutureTest exten
1521          checkCompletedWithWrappedCancellationException(g);
1522      }
1523  
1320
1524      // asyncs
1525  
1526      /**
# Line 1350 | Line 1553 | public class CompletableFutureTest exten
1553          try {
1554              g.join();
1555              shouldThrow();
1556 <        } catch (Exception ok) {
1354 <        }
1556 >        } catch (CompletionException success) {}
1557          checkCompletedWithWrappedCFException(g);
1558      }
1559  
# Line 2085 | Line 2287 | public class CompletableFutureTest exten
2287          checkCompletedWithWrappedCancellationException(g);
2288      }
2289  
2088
2290      // async with explicit executors
2291  
2292      /**
# Line 2118 | Line 2319 | public class CompletableFutureTest exten
2319          try {
2320              g.join();
2321              shouldThrow();
2322 <        } catch (Exception ok) {
2122 <        }
2322 >        } catch (CompletionException success) {}
2323          checkCompletedWithWrappedCFException(g);
2324      }
2325  
# Line 2936 | Line 3136 | public class CompletableFutureTest exten
3136          ThreadExecutor exec = new ThreadExecutor();
3137  
3138          Runnable[] throwingActions = {
3139 <            () -> { CompletableFuture.supplyAsync(null); },
3140 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3141 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3142 <
3143 <            () -> { CompletableFuture.runAsync(null); },
3144 <            () -> { CompletableFuture.runAsync(null, exec); },
3145 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3146 <
3147 <            () -> { f.completeExceptionally(null); },
3148 <
3149 <            () -> { f.thenApply(null); },
3150 <            () -> { f.thenApplyAsync(null); },
3151 <            () -> { f.thenApplyAsync((x) -> x, null); },
3152 <            () -> { f.thenApplyAsync(null, exec); },
3153 <
3154 <            () -> { f.thenAccept(null); },
3155 <            () -> { f.thenAcceptAsync(null); },
3156 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3157 <            () -> { f.thenAcceptAsync(null, exec); },
3158 <
3159 <            () -> { f.thenRun(null); },
3160 <            () -> { f.thenRunAsync(null); },
3161 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3162 <            () -> { f.thenRunAsync(null, exec); },
3163 <
3164 <            () -> { f.thenCombine(g, null); },
3165 <            () -> { f.thenCombineAsync(g, null); },
3166 <            () -> { f.thenCombineAsync(g, null, exec); },
3167 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3168 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3169 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3170 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3171 <
3172 <            () -> { f.thenAcceptBoth(g, null); },
3173 <            () -> { f.thenAcceptBothAsync(g, null); },
3174 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3175 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3176 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3177 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3178 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3179 <
3180 <            () -> { f.runAfterBoth(g, null); },
3181 <            () -> { f.runAfterBothAsync(g, null); },
3182 <            () -> { f.runAfterBothAsync(g, null, exec); },
3183 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3184 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3185 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3186 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3187 <
3188 <            () -> { f.applyToEither(g, null); },
3189 <            () -> { f.applyToEitherAsync(g, null); },
3190 <            () -> { f.applyToEitherAsync(g, null, exec); },
3191 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3192 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3193 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3194 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3195 <
3196 <            () -> { f.acceptEither(g, null); },
3197 <            () -> { f.acceptEitherAsync(g, null); },
3198 <            () -> { f.acceptEitherAsync(g, null, exec); },
3199 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3200 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3201 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3202 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3203 <
3204 <            () -> { f.runAfterEither(g, null); },
3205 <            () -> { f.runAfterEitherAsync(g, null); },
3206 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3207 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3208 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3209 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3210 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3211 <
3212 <            () -> { f.thenCompose(null); },
3213 <            () -> { f.thenComposeAsync(null); },
3214 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3215 <            () -> { f.thenComposeAsync(null, exec); },
3216 <
3217 <            () -> { f.exceptionally(null); },
3218 <
3219 <            () -> { f.handle(null); },
3220 <
3221 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3222 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3223 <            () -> { CompletableFuture.allOf(f, null); },
3224 <            () -> { CompletableFuture.allOf(null, f); },
3225 <
3226 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3227 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3228 <            () -> { CompletableFuture.anyOf(f, null); },
3229 <            () -> { CompletableFuture.anyOf(null, f); },
3139 >            () -> CompletableFuture.supplyAsync(null),
3140 >            () -> CompletableFuture.supplyAsync(null, exec),
3141 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
3142 >
3143 >            () -> CompletableFuture.runAsync(null),
3144 >            () -> CompletableFuture.runAsync(null, exec),
3145 >            () -> CompletableFuture.runAsync(() -> {}, null),
3146 >
3147 >            () -> f.completeExceptionally(null),
3148 >
3149 >            () -> f.thenApply(null),
3150 >            () -> f.thenApplyAsync(null),
3151 >            () -> f.thenApplyAsync((x) -> x, null),
3152 >            () -> f.thenApplyAsync(null, exec),
3153 >
3154 >            () -> f.thenAccept(null),
3155 >            () -> f.thenAcceptAsync(null),
3156 >            () -> f.thenAcceptAsync((x) -> {} , null),
3157 >            () -> f.thenAcceptAsync(null, exec),
3158 >
3159 >            () -> f.thenRun(null),
3160 >            () -> f.thenRunAsync(null),
3161 >            () -> f.thenRunAsync(() -> {} , null),
3162 >            () -> f.thenRunAsync(null, exec),
3163 >
3164 >            () -> f.thenCombine(g, null),
3165 >            () -> f.thenCombineAsync(g, null),
3166 >            () -> f.thenCombineAsync(g, null, exec),
3167 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3168 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3169 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3170 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3171 >
3172 >            () -> f.thenAcceptBoth(g, null),
3173 >            () -> f.thenAcceptBothAsync(g, null),
3174 >            () -> f.thenAcceptBothAsync(g, null, exec),
3175 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3176 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3177 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3178 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3179 >
3180 >            () -> f.runAfterBoth(g, null),
3181 >            () -> f.runAfterBothAsync(g, null),
3182 >            () -> f.runAfterBothAsync(g, null, exec),
3183 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3184 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3185 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3186 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3187 >
3188 >            () -> f.applyToEither(g, null),
3189 >            () -> f.applyToEitherAsync(g, null),
3190 >            () -> f.applyToEitherAsync(g, null, exec),
3191 >            () -> f.applyToEither(nullFuture, (x) -> x),
3192 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3193 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3194 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3195 >
3196 >            () -> f.acceptEither(g, null),
3197 >            () -> f.acceptEitherAsync(g, null),
3198 >            () -> f.acceptEitherAsync(g, null, exec),
3199 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3200 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3201 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3202 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3203 >
3204 >            () -> f.runAfterEither(g, null),
3205 >            () -> f.runAfterEitherAsync(g, null),
3206 >            () -> f.runAfterEitherAsync(g, null, exec),
3207 >            () -> f.runAfterEither(nullFuture, () -> {}),
3208 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3209 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3210 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3211 >
3212 >            () -> f.thenCompose(null),
3213 >            () -> f.thenComposeAsync(null),
3214 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3215 >            () -> f.thenComposeAsync(null, exec),
3216 >
3217 >            () -> f.exceptionally(null),
3218 >
3219 >            () -> f.handle(null),
3220 >
3221 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3222 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3223 >            () -> CompletableFuture.allOf(f, null),
3224 >            () -> CompletableFuture.allOf(null, f),
3225 >
3226 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3227 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3228 >            () -> CompletableFuture.anyOf(f, null),
3229 >            () -> CompletableFuture.anyOf(null, f),
3230 >
3231 >            () -> f.obtrudeException(null),
3232          };
3233  
3234          assertThrows(NullPointerException.class, throwingActions);
3235          assertEquals(0, exec.count.get());
3236      }
3237  
3238 +    /**
3239 +     * toCompletableFuture returns this CompletableFuture.
3240 +     */
3241 +    public void testToCompletableFuture() {
3242 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3243 +        assertSame(f, f.toCompletableFuture());
3244 +    }
3245 +
3246 +    /**
3247 +     * whenComplete action executes on normal completion, propagating
3248 +     * source result.
3249 +     */
3250 +    public void testWhenComplete1() {
3251 +        final AtomicInteger a = new AtomicInteger();
3252 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3253 +        CompletableFuture<Integer> g =
3254 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3255 +        f.complete(three);
3256 +        checkCompletedNormally(f, three);
3257 +        checkCompletedNormally(g, three);
3258 +        assertEquals(a.get(), 1);
3259 +    }
3260 +
3261 +    /**
3262 +     * whenComplete action executes on exceptional completion, propagating
3263 +     * source result.
3264 +     */
3265 +    public void testWhenComplete2() {
3266 +        final AtomicInteger a = new AtomicInteger();
3267 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3268 +        CompletableFuture<Integer> g =
3269 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3270 +        f.completeExceptionally(new CFException());
3271 +        assertTrue(f.isCompletedExceptionally());
3272 +        assertTrue(g.isCompletedExceptionally());
3273 +        assertEquals(a.get(), 1);
3274 +    }
3275 +
3276 +    /**
3277 +     * If a whenComplete action throws an exception when triggered by
3278 +     * a normal completion, it completes exceptionally
3279 +     */
3280 +    public void testWhenComplete3() {
3281 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3282 +        CompletableFuture<Integer> g =
3283 +            f.whenComplete((Integer x, Throwable t) ->
3284 +                           { throw new CFException(); } );
3285 +        f.complete(three);
3286 +        checkCompletedNormally(f, three);
3287 +        assertTrue(g.isCompletedExceptionally());
3288 +        checkCompletedWithWrappedCFException(g);
3289 +    }
3290 +
3291 +    /**
3292 +     * whenCompleteAsync action executes on normal completion, propagating
3293 +     * source result.
3294 +     */
3295 +    public void testWhenCompleteAsync1() {
3296 +        final AtomicInteger a = new AtomicInteger();
3297 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3298 +        CompletableFuture<Integer> g =
3299 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3300 +        f.complete(three);
3301 +        checkCompletedNormally(f, three);
3302 +        checkCompletedNormally(g, three);
3303 +        assertEquals(a.get(), 1);
3304 +    }
3305 +
3306 +    /**
3307 +     * whenCompleteAsync action executes on exceptional completion, propagating
3308 +     * source result.
3309 +     */
3310 +    public void testWhenCompleteAsync2() {
3311 +        final AtomicInteger a = new AtomicInteger();
3312 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 +        CompletableFuture<Integer> g =
3314 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3315 +        f.completeExceptionally(new CFException());
3316 +        checkCompletedWithWrappedCFException(f);
3317 +        checkCompletedWithWrappedCFException(g);
3318 +    }
3319 +
3320 +    /**
3321 +     * If a whenCompleteAsync action throws an exception when
3322 +     * triggered by a normal completion, it completes exceptionally
3323 +     */
3324 +    public void testWhenCompleteAsync3() {
3325 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3326 +        CompletableFuture<Integer> g =
3327 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3328 +                           { throw new CFException(); } );
3329 +        f.complete(three);
3330 +        checkCompletedNormally(f, three);
3331 +        checkCompletedWithWrappedCFException(g);
3332 +    }
3333 +
3334 +    /**
3335 +     * whenCompleteAsync action executes on normal completion, propagating
3336 +     * source result.
3337 +     */
3338 +    public void testWhenCompleteAsync1e() {
3339 +        final AtomicInteger a = new AtomicInteger();
3340 +        ThreadExecutor exec = new ThreadExecutor();
3341 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3342 +        CompletableFuture<Integer> g =
3343 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3344 +                                exec);
3345 +        f.complete(three);
3346 +        checkCompletedNormally(f, three);
3347 +        checkCompletedNormally(g, three);
3348 +        assertEquals(a.get(), 1);
3349 +    }
3350 +
3351 +    /**
3352 +     * whenCompleteAsync action executes on exceptional completion, propagating
3353 +     * source result.
3354 +     */
3355 +    public void testWhenCompleteAsync2e() {
3356 +        final AtomicInteger a = new AtomicInteger();
3357 +        ThreadExecutor exec = new ThreadExecutor();
3358 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3359 +        CompletableFuture<Integer> g =
3360 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3361 +                                exec);
3362 +        f.completeExceptionally(new CFException());
3363 +        checkCompletedWithWrappedCFException(f);
3364 +        checkCompletedWithWrappedCFException(g);
3365 +    }
3366 +
3367 +    /**
3368 +     * If a whenCompleteAsync action throws an exception when triggered
3369 +     * by a normal completion, it completes exceptionally
3370 +     */
3371 +    public void testWhenCompleteAsync3e() {
3372 +        ThreadExecutor exec = new ThreadExecutor();
3373 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3374 +        CompletableFuture<Integer> g =
3375 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3376 +                                { throw new CFException(); },
3377 +                                exec);
3378 +        f.complete(three);
3379 +        checkCompletedNormally(f, three);
3380 +        checkCompletedWithWrappedCFException(g);
3381 +    }
3382 +
3383 +    /**
3384 +     * handleAsync action completes normally with function value on
3385 +     * either normal or exceptional completion of source
3386 +     */
3387 +    public void testHandleAsync() {
3388 +        CompletableFuture<Integer> f, g;
3389 +        IntegerHandler r;
3390 +
3391 +        f = new CompletableFuture<>();
3392 +        g = f.handleAsync(r = new IntegerHandler());
3393 +        assertFalse(r.ran);
3394 +        f.completeExceptionally(new CFException());
3395 +        checkCompletedWithWrappedCFException(f);
3396 +        checkCompletedNormally(g, three);
3397 +        assertTrue(r.ran);
3398 +
3399 +        f = new CompletableFuture<>();
3400 +        g = f.handleAsync(r = new IntegerHandler());
3401 +        assertFalse(r.ran);
3402 +        f.completeExceptionally(new CFException());
3403 +        checkCompletedWithWrappedCFException(f);
3404 +        checkCompletedNormally(g, three);
3405 +        assertTrue(r.ran);
3406 +
3407 +        f = new CompletableFuture<>();
3408 +        g = f.handleAsync(r = new IntegerHandler());
3409 +        assertFalse(r.ran);
3410 +        f.complete(one);
3411 +        checkCompletedNormally(f, one);
3412 +        checkCompletedNormally(g, two);
3413 +        assertTrue(r.ran);
3414 +
3415 +        f = new CompletableFuture<>();
3416 +        g = f.handleAsync(r = new IntegerHandler());
3417 +        assertFalse(r.ran);
3418 +        f.complete(one);
3419 +        checkCompletedNormally(f, one);
3420 +        checkCompletedNormally(g, two);
3421 +        assertTrue(r.ran);
3422 +    }
3423 +
3424 +    /**
3425 +     * handleAsync action with Executor completes normally with
3426 +     * function value on either normal or exceptional completion of
3427 +     * source
3428 +     */
3429 +    public void testHandleAsync2() {
3430 +        CompletableFuture<Integer> f, g;
3431 +        ThreadExecutor exec = new ThreadExecutor();
3432 +        IntegerHandler r;
3433 +
3434 +        f = new CompletableFuture<>();
3435 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3436 +        assertFalse(r.ran);
3437 +        f.completeExceptionally(new CFException());
3438 +        checkCompletedWithWrappedCFException(f);
3439 +        checkCompletedNormally(g, three);
3440 +        assertTrue(r.ran);
3441 +
3442 +        f = new CompletableFuture<>();
3443 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3444 +        assertFalse(r.ran);
3445 +        f.completeExceptionally(new CFException());
3446 +        checkCompletedWithWrappedCFException(f);
3447 +        checkCompletedNormally(g, three);
3448 +        assertTrue(r.ran);
3449 +
3450 +        f = new CompletableFuture<>();
3451 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3452 +        assertFalse(r.ran);
3453 +        f.complete(one);
3454 +        checkCompletedNormally(f, one);
3455 +        checkCompletedNormally(g, two);
3456 +        assertTrue(r.ran);
3457 +
3458 +        f = new CompletableFuture<>();
3459 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3460 +        assertFalse(r.ran);
3461 +        f.complete(one);
3462 +        checkCompletedNormally(f, one);
3463 +        checkCompletedNormally(g, two);
3464 +        assertTrue(r.ran);
3465 +    }
3466 +
3467   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines