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.49 by jsr166, Mon Jun 2 19:20:51 2014 UTC vs.
Revision 1.53 by jsr166, Mon Jun 2 20:20:47 2014 UTC

# Line 416 | Line 416 | public class CompletableFutureTest exten
416              throw new CFException();
417          }
418      }
419 <    static final class FailingNoop implements Runnable {
419 >    static final class FailingRunnable implements Runnable {
420          int invocationCount = 0;
421          public void run() {
422              invocationCount++;
# Line 933 | Line 933 | public class CompletableFutureTest exten
933       * failing runAsync completes exceptionally after running Runnable
934       */
935      public void testRunAsync3() {
936 <        FailingNoop r = new FailingNoop();
936 >        FailingRunnable r = new FailingRunnable();
937          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
938          checkCompletedWithWrappedCFException(f);
939          assertEquals(1, r.invocationCount);
# Line 983 | Line 983 | public class CompletableFutureTest exten
983          final Noop r = new Noop();
984          if (!createIncomplete) f.complete(v1);
985          final CompletableFuture<Void> g = m.thenRun(f, r);
986 <        if (createIncomplete) f.complete(v1);
986 >        if (createIncomplete) {
987 >            checkIncomplete(g);
988 >            f.complete(v1);
989 >        }
990  
991          checkCompletedNormally(g, null);
992          checkCompletedNormally(f, v1);
# Line 1003 | Line 1006 | public class CompletableFutureTest exten
1006          final Noop r = new Noop();
1007          if (!createIncomplete) f.completeExceptionally(ex);
1008          final CompletableFuture<Void> g = m.thenRun(f, r);
1009 <        if (createIncomplete) f.completeExceptionally(ex);
1009 >        if (createIncomplete) {
1010 >            checkIncomplete(g);
1011 >            f.completeExceptionally(ex);
1012 >        }
1013  
1014          checkCompletedWithWrappedCFException(g, ex);
1015          checkCompletedWithWrappedCFException(f, ex);
# Line 1022 | Line 1028 | public class CompletableFutureTest exten
1028          final Noop r = new Noop();
1029          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1030          final CompletableFuture<Void> g = f.thenRun(r);
1031 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1031 >        if (createIncomplete) {
1032 >            checkIncomplete(g);
1033 >            assertTrue(f.cancel(mayInterruptIfRunning));
1034 >        }
1035  
1036          checkCompletedWithWrappedCancellationException(g);
1037          checkCancelled(f);
# Line 1038 | Line 1047 | public class CompletableFutureTest exten
1047          for (Integer v1 : new Integer[] { 1, null })
1048      {
1049          final CompletableFuture<Integer> f = new CompletableFuture<>();
1050 <        final FailingNoop r = new FailingNoop();
1050 >        final FailingRunnable r = new FailingRunnable();
1051          if (!createIncomplete) f.complete(v1);
1052          final CompletableFuture<Void> g = f.thenRun(r);
1053 <        if (createIncomplete) f.complete(v1);
1053 >        if (createIncomplete) {
1054 >            checkIncomplete(g);
1055 >            f.complete(v1);
1056 >        }
1057  
1058          checkCompletedWithWrappedCFException(g);
1059          checkCompletedNormally(f, v1);
# Line 1082 | Line 1094 | public class CompletableFutureTest exten
1094          final IncFunction r = new IncFunction();
1095          if (!createIncomplete) f.completeExceptionally(ex);
1096          final CompletableFuture<Integer> g = m.thenApply(f, r);
1097 <        if (createIncomplete) f.completeExceptionally(ex);
1097 >        if (createIncomplete) {
1098 >            checkIncomplete(g);
1099 >            f.completeExceptionally(ex);
1100 >        }
1101  
1102          checkCompletedWithWrappedCFException(g, ex);
1103          checkCompletedWithWrappedCFException(f, ex);
# Line 1101 | Line 1116 | public class CompletableFutureTest exten
1116          final IncFunction r = new IncFunction();
1117          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1118          final CompletableFuture<Integer> g = f.thenApply(r);
1119 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1119 >        if (createIncomplete) {
1120 >            checkIncomplete(g);
1121 >            assertTrue(f.cancel(mayInterruptIfRunning));
1122 >        }
1123  
1124          checkCompletedWithWrappedCancellationException(g);
1125          checkCancelled(f);
# Line 1120 | Line 1138 | public class CompletableFutureTest exten
1138          final FailingFunction r = new FailingFunction();
1139          if (!createIncomplete) f.complete(v1);
1140          final CompletableFuture<Integer> g = f.thenApply(r);
1141 <        if (createIncomplete) f.complete(v1);
1141 >        if (createIncomplete) {
1142 >            checkIncomplete(g);
1143 >            f.complete(v1);
1144 >        }
1145  
1146          checkCompletedWithWrappedCFException(g);
1147          checkCompletedNormally(f, v1);
# Line 1129 | Line 1150 | public class CompletableFutureTest exten
1150      /**
1151       * thenAccept result completes normally after normal completion of source
1152       */
1153 <    public void testThenAccept() {
1154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1155 <        IncAction r = new IncAction();
1156 <        CompletableFuture<Void> g = f.thenAccept(r);
1157 <        f.complete(one);
1153 >    public void testThenAccept_normalCompletion() {
1154 >        for (ExecutionMode m : ExecutionMode.values())
1155 >        for (boolean createIncomplete : new boolean[] { true, false })
1156 >        for (Integer v1 : new Integer[] { 1, null })
1157 >    {
1158 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1159 >        final IncAction r = new IncAction();
1160 >        if (!createIncomplete) f.complete(v1);
1161 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1162 >        if (createIncomplete) {
1163 >            checkIncomplete(g);
1164 >            f.complete(v1);
1165 >        }
1166 >
1167          checkCompletedNormally(g, null);
1168 <        assertEquals(r.value, (Integer) 2);
1169 <    }
1168 >        checkCompletedNormally(f, v1);
1169 >        assertEquals(1, r.invocationCount);
1170 >        assertEquals(inc(v1), r.value);
1171 >    }}
1172  
1173      /**
1174       * thenAccept result completes exceptionally after exceptional
1175       * completion of source
1176       */
1177 <    public void testThenAccept2() {
1178 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1179 <        IncAction r = new IncAction();
1180 <        CompletableFuture<Void> g = f.thenAccept(r);
1181 <        f.completeExceptionally(new CFException());
1182 <        checkCompletedWithWrappedCFException(g);
1183 <    }
1177 >    public void testThenAccept_exceptionalCompletion() {
1178 >        for (ExecutionMode m : ExecutionMode.values())
1179 >        for (boolean createIncomplete : new boolean[] { true, false })
1180 >    {
1181 >        final CFException ex = new CFException();
1182 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1183 >        final IncAction r = new IncAction();
1184 >        if (!createIncomplete) f.completeExceptionally(ex);
1185 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1186 >        if (createIncomplete) {
1187 >            checkIncomplete(g);
1188 >            f.completeExceptionally(ex);
1189 >        }
1190 >
1191 >        checkCompletedWithWrappedCFException(g, ex);
1192 >        checkCompletedWithWrappedCFException(f, ex);
1193 >        assertEquals(0, r.invocationCount);
1194 >    }}
1195  
1196      /**
1197       * thenAccept result completes exceptionally if action does
1198       */
1199 <    public void testThenAccept3() {
1200 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1201 <        FailingConsumer r = new FailingConsumer();
1202 <        CompletableFuture<Void> g = f.thenAccept(r);
1203 <        f.complete(one);
1199 >    public void testThenAccept_actionFailed() {
1200 >        for (ExecutionMode m : ExecutionMode.values())
1201 >        for (boolean createIncomplete : new boolean[] { true, false })
1202 >        for (Integer v1 : new Integer[] { 1, null })
1203 >    {
1204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 >        final FailingConsumer r = new FailingConsumer();
1206 >        if (!createIncomplete) f.complete(v1);
1207 >        final CompletableFuture<Void> g = f.thenAccept(r);
1208 >        if (createIncomplete) {
1209 >            checkIncomplete(g);
1210 >            f.complete(v1);
1211 >        }
1212 >
1213          checkCompletedWithWrappedCFException(g);
1214 <        assertEquals(1, r.invocationCount);
1215 <    }
1214 >        checkCompletedNormally(f, v1);
1215 >    }}
1216  
1217      /**
1218       * thenAccept result completes exceptionally if source cancelled
1219       */
1220 <    public void testThenAccept4() {
1221 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1222 <        IncAction r = new IncAction();
1223 <        CompletableFuture<Void> g = f.thenAccept(r);
1224 <        assertTrue(f.cancel(true));
1220 >    public void testThenAccept_sourceCancelled() {
1221 >        for (ExecutionMode m : ExecutionMode.values())
1222 >        for (boolean createIncomplete : new boolean[] { true, false })
1223 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1224 >    {
1225 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1226 >        final IncAction r = new IncAction();
1227 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1228 >        final CompletableFuture<Void> g = f.thenAccept(r);
1229 >        if (createIncomplete) {
1230 >            checkIncomplete(g);
1231 >            assertTrue(f.cancel(mayInterruptIfRunning));
1232 >        }
1233 >
1234          checkCompletedWithWrappedCancellationException(g);
1235 <    }
1235 >        checkCancelled(f);
1236 >        assertEquals(0, r.invocationCount);
1237 >    }}
1238  
1239      /**
1240       * thenCombine result completes normally after normal completion
1241       * of sources
1242       */
1243 <    public void testThenCombine_normalCompletion1() {
1243 >    public void testThenCombine_normalCompletion() {
1244 >        for (ExecutionMode m : ExecutionMode.values())
1245          for (boolean createIncomplete : new boolean[] { true, false })
1246          for (boolean fFirst : new boolean[] { true, false })
1183        for (ExecutionMode m : ExecutionMode.values())
1247          for (Integer v1 : new Integer[] { 1, null })
1248          for (Integer v2 : new Integer[] { 2, null })
1249      {
1250          final CompletableFuture<Integer> f = new CompletableFuture<>();
1251          final CompletableFuture<Integer> g = new CompletableFuture<>();
1252          final SubtractFunction r = new SubtractFunction();
1190        CompletableFuture<Integer> h = null;
1191        if (createIncomplete) h = m.thenCombine(f, g, r);
1253  
1254 <        if (fFirst)
1255 <            f.complete(v1);
1256 <        else
1257 <            g.complete(v2);
1258 <        if (createIncomplete) checkIncomplete(h);
1259 <        assertEquals(0, r.invocationCount);
1260 <        if (!fFirst)
1261 <            f.complete(v1);
1262 <        else
1202 <            g.complete(v2);
1203 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1254 >        if (fFirst) f.complete(v1); else g.complete(v2);
1255 >        if (!createIncomplete)
1256 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1257 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1258 >        if (createIncomplete) {
1259 >            checkIncomplete(h);
1260 >            assertEquals(0, r.invocationCount);
1261 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1262 >        }
1263  
1264          checkCompletedNormally(h, subtract(v1, v2));
1265          checkCompletedNormally(f, v1);
# Line 1212 | Line 1271 | public class CompletableFutureTest exten
1271       * thenCombine result completes exceptionally after exceptional
1272       * completion of either source
1273       */
1274 <    public void testThenCombine_exceptionalCompletion1() {
1216 <        for (ExecutionMode m : ExecutionMode.values())
1217 <        for (Integer v1 : new Integer[] { 1, null })
1218 <    {
1219 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1220 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1221 <        final SubtractFunction r = new SubtractFunction();
1222 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1223 <        final CFException ex = new CFException();
1224 <
1225 <        f.completeExceptionally(ex);
1226 <        checkIncomplete(h);
1227 <        g.complete(v1);
1228 <
1229 <        checkCompletedWithWrappedCFException(h, ex);
1230 <        checkCompletedWithWrappedCFException(f, ex);
1231 <        assertEquals(0, r.invocationCount);
1232 <        checkCompletedNormally(g, v1);
1233 <    }}
1234 <
1235 <    public void testThenCombine_exceptionalCompletion2() {
1236 <        for (ExecutionMode m : ExecutionMode.values())
1237 <        for (Integer v1 : new Integer[] { 1, null })
1238 <    {
1239 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1240 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1241 <        final SubtractFunction r = new SubtractFunction();
1242 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1243 <        final CFException ex = new CFException();
1244 <
1245 <        g.completeExceptionally(ex);
1246 <        checkIncomplete(h);
1247 <        f.complete(v1);
1248 <
1249 <        checkCompletedWithWrappedCFException(h, ex);
1250 <        checkCompletedWithWrappedCFException(g, ex);
1251 <        assertEquals(0, r.invocationCount);
1252 <        checkCompletedNormally(f, v1);
1253 <    }}
1254 <
1255 <    public void testThenCombine_exceptionalCompletion3() {
1274 >    public void testThenCombine_exceptionalCompletion() {
1275          for (ExecutionMode m : ExecutionMode.values())
1276 +        for (boolean createIncomplete : new boolean[] { true, false })
1277 +        for (boolean fFirst : new boolean[] { true, false })
1278          for (Integer v1 : new Integer[] { 1, null })
1279      {
1280          final CompletableFuture<Integer> f = new CompletableFuture<>();
1281          final CompletableFuture<Integer> g = new CompletableFuture<>();
1261        final SubtractFunction r = new SubtractFunction();
1282          final CFException ex = new CFException();
1263
1264        g.completeExceptionally(ex);
1265        f.complete(v1);
1266        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1267
1268        checkCompletedWithWrappedCFException(h, ex);
1269        checkCompletedWithWrappedCFException(g, ex);
1270        assertEquals(0, r.invocationCount);
1271        checkCompletedNormally(f, v1);
1272    }}
1273
1274    public void testThenCombine_exceptionalCompletion4() {
1275        for (ExecutionMode m : ExecutionMode.values())
1276        for (Integer v1 : new Integer[] { 1, null })
1277    {
1278        final CompletableFuture<Integer> f = new CompletableFuture<>();
1279        final CompletableFuture<Integer> g = new CompletableFuture<>();
1283          final SubtractFunction r = new SubtractFunction();
1281        final CFException ex = new CFException();
1284  
1285 <        f.completeExceptionally(ex);
1286 <        g.complete(v1);
1285 >        (fFirst ? f : g).complete(v1);
1286 >        if (!createIncomplete)
1287 >            (!fFirst ? f : g).completeExceptionally(ex);
1288          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1289 +        if (createIncomplete) {
1290 +            checkIncomplete(h);
1291 +            (!fFirst ? f : g).completeExceptionally(ex);
1292 +        }
1293  
1294          checkCompletedWithWrappedCFException(h, ex);
1288        checkCompletedWithWrappedCFException(f, ex);
1295          assertEquals(0, r.invocationCount);
1296 <        checkCompletedNormally(g, v1);
1296 >        checkCompletedNormally(fFirst ? f : g, v1);
1297 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1298      }}
1299  
1300      /**
1301       * thenCombine result completes exceptionally if action does
1302       */
1303 <    public void testThenCombine_actionFailed1() {
1297 <        for (ExecutionMode m : ExecutionMode.values())
1298 <        for (Integer v1 : new Integer[] { 1, null })
1299 <        for (Integer v2 : new Integer[] { 2, null })
1300 <    {
1301 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1302 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1303 <        final FailingBiFunction r = new FailingBiFunction();
1304 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1305 <
1306 <        f.complete(v1);
1307 <        checkIncomplete(h);
1308 <        g.complete(v2);
1309 <
1310 <        checkCompletedWithWrappedCFException(h);
1311 <        checkCompletedNormally(f, v1);
1312 <        checkCompletedNormally(g, v2);
1313 <    }}
1314 <
1315 <    public void testThenCombine_actionFailed2() {
1303 >    public void testThenCombine_actionFailed() {
1304          for (ExecutionMode m : ExecutionMode.values())
1305 +        for (boolean fFirst : new boolean[] { true, false })
1306          for (Integer v1 : new Integer[] { 1, null })
1307          for (Integer v2 : new Integer[] { 2, null })
1308      {
# Line 1322 | Line 1311 | public class CompletableFutureTest exten
1311          final FailingBiFunction r = new FailingBiFunction();
1312          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1313  
1314 <        g.complete(v2);
1315 <        checkIncomplete(h);
1316 <        f.complete(v1);
1314 >        if (fFirst) {
1315 >            f.complete(v1);
1316 >            g.complete(v2);
1317 >        } else {
1318 >            g.complete(v2);
1319 >            f.complete(v1);
1320 >        }
1321  
1322          checkCompletedWithWrappedCFException(h);
1323          checkCompletedNormally(f, v1);
# Line 1334 | Line 1327 | public class CompletableFutureTest exten
1327      /**
1328       * thenCombine result completes exceptionally if either source cancelled
1329       */
1330 <    public void testThenCombine_sourceCancelled1() {
1338 <        for (ExecutionMode m : ExecutionMode.values())
1339 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1340 <        for (Integer v1 : new Integer[] { 1, null })
1341 <    {
1342 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1343 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1344 <        final SubtractFunction r = new SubtractFunction();
1345 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1346 <
1347 <        assertTrue(f.cancel(mayInterruptIfRunning));
1348 <        checkIncomplete(h);
1349 <        g.complete(v1);
1350 <
1351 <        checkCompletedWithWrappedCancellationException(h);
1352 <        checkCancelled(f);
1353 <        assertEquals(0, r.invocationCount);
1354 <        checkCompletedNormally(g, v1);
1355 <    }}
1356 <
1357 <    public void testThenCombine_sourceCancelled2() {
1358 <        for (ExecutionMode m : ExecutionMode.values())
1359 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1360 <        for (Integer v1 : new Integer[] { 1, null })
1361 <    {
1362 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1363 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1364 <        final SubtractFunction r = new SubtractFunction();
1365 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1366 <
1367 <        assertTrue(g.cancel(mayInterruptIfRunning));
1368 <        checkIncomplete(h);
1369 <        f.complete(v1);
1370 <
1371 <        checkCompletedWithWrappedCancellationException(h);
1372 <        checkCancelled(g);
1373 <        assertEquals(0, r.invocationCount);
1374 <        checkCompletedNormally(f, v1);
1375 <    }}
1376 <
1377 <    public void testThenCombine_sourceCancelled3() {
1378 <        for (ExecutionMode m : ExecutionMode.values())
1379 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1380 <        for (Integer v1 : new Integer[] { 1, null })
1381 <    {
1382 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1383 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1384 <        final SubtractFunction r = new SubtractFunction();
1385 <
1386 <        assertTrue(g.cancel(mayInterruptIfRunning));
1387 <        f.complete(v1);
1388 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1389 <
1390 <        checkCompletedWithWrappedCancellationException(h);
1391 <        checkCancelled(g);
1392 <        assertEquals(0, r.invocationCount);
1393 <        checkCompletedNormally(f, v1);
1394 <    }}
1395 <
1396 <    public void testThenCombine_sourceCancelled4() {
1330 >    public void testThenCombine_sourceCancelled() {
1331          for (ExecutionMode m : ExecutionMode.values())
1332          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1333 +        for (boolean createIncomplete : new boolean[] { true, false })
1334 +        for (boolean fFirst : new boolean[] { true, false })
1335          for (Integer v1 : new Integer[] { 1, null })
1336      {
1337          final CompletableFuture<Integer> f = new CompletableFuture<>();
1338          final CompletableFuture<Integer> g = new CompletableFuture<>();
1339          final SubtractFunction r = new SubtractFunction();
1340  
1341 <        assertTrue(f.cancel(mayInterruptIfRunning));
1342 <        g.complete(v1);
1341 >        (fFirst ? f : g).complete(v1);
1342 >        if (!createIncomplete)
1343 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1344          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1345 +        if (createIncomplete) {
1346 +            checkIncomplete(h);
1347 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1348 +        }
1349  
1350          checkCompletedWithWrappedCancellationException(h);
1351 <        checkCancelled(f);
1351 >        checkCancelled(!fFirst ? f : g);
1352          assertEquals(0, r.invocationCount);
1353 <        checkCompletedNormally(g, v1);
1353 >        checkCompletedNormally(fFirst ? f : g, v1);
1354      }}
1355  
1356      /**
1357       * thenAcceptBoth result completes normally after normal
1358       * completion of sources
1359       */
1360 <    public void testThenAcceptBoth_normalCompletion1() {
1420 <        for (ExecutionMode m : ExecutionMode.values())
1421 <        for (Integer v1 : new Integer[] { 1, null })
1422 <        for (Integer v2 : new Integer[] { 2, null })
1423 <    {
1424 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1425 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1426 <        final SubtractAction r = new SubtractAction();
1427 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1428 <
1429 <        f.complete(v1);
1430 <        checkIncomplete(h);
1431 <        assertEquals(0, r.invocationCount);
1432 <        g.complete(v2);
1433 <
1434 <        checkCompletedNormally(h, null);
1435 <        assertEquals(subtract(v1, v2), r.value);
1436 <        checkCompletedNormally(f, v1);
1437 <        checkCompletedNormally(g, v2);
1438 <    }}
1439 <
1440 <    public void testThenAcceptBoth_normalCompletion2() {
1441 <        for (ExecutionMode m : ExecutionMode.values())
1442 <        for (Integer v1 : new Integer[] { 1, null })
1443 <        for (Integer v2 : new Integer[] { 2, null })
1444 <    {
1445 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1446 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1447 <        final SubtractAction r = new SubtractAction();
1448 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1449 <
1450 <        g.complete(v2);
1451 <        checkIncomplete(h);
1452 <        assertEquals(0, r.invocationCount);
1453 <        f.complete(v1);
1454 <
1455 <        checkCompletedNormally(h, null);
1456 <        assertEquals(subtract(v1, v2), r.value);
1457 <        checkCompletedNormally(f, v1);
1458 <        checkCompletedNormally(g, v2);
1459 <    }}
1460 <
1461 <    public void testThenAcceptBoth_normalCompletion3() {
1462 <        for (ExecutionMode m : ExecutionMode.values())
1463 <        for (Integer v1 : new Integer[] { 1, null })
1464 <        for (Integer v2 : new Integer[] { 2, null })
1465 <    {
1466 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1467 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1468 <        final SubtractAction r = new SubtractAction();
1469 <
1470 <        g.complete(v2);
1471 <        f.complete(v1);
1472 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1473 <
1474 <        checkCompletedNormally(h, null);
1475 <        assertEquals(subtract(v1, v2), r.value);
1476 <        checkCompletedNormally(f, v1);
1477 <        checkCompletedNormally(g, v2);
1478 <    }}
1479 <
1480 <    public void testThenAcceptBoth_normalCompletion4() {
1360 >    public void testThenAcceptBoth_normalCompletion() {
1361          for (ExecutionMode m : ExecutionMode.values())
1362 +        for (boolean createIncomplete : new boolean[] { true, false })
1363 +        for (boolean fFirst : new boolean[] { true, false })
1364          for (Integer v1 : new Integer[] { 1, null })
1365          for (Integer v2 : new Integer[] { 2, null })
1366      {
# Line 1486 | Line 1368 | public class CompletableFutureTest exten
1368          final CompletableFuture<Integer> g = new CompletableFuture<>();
1369          final SubtractAction r = new SubtractAction();
1370  
1371 <        f.complete(v1);
1372 <        g.complete(v2);
1371 >        if (fFirst) f.complete(v1); else g.complete(v2);
1372 >        if (!createIncomplete)
1373 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1374          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1375 +        if (createIncomplete) {
1376 +            checkIncomplete(h);
1377 +            assertEquals(0, r.invocationCount);
1378 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1379 +        }
1380  
1381          checkCompletedNormally(h, null);
1382          assertEquals(subtract(v1, v2), r.value);
# Line 1500 | Line 1388 | public class CompletableFutureTest exten
1388       * thenAcceptBoth result completes exceptionally after exceptional
1389       * completion of either source
1390       */
1391 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1504 <        for (ExecutionMode m : ExecutionMode.values())
1505 <        for (Integer v1 : new Integer[] { 1, null })
1506 <    {
1507 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1508 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1509 <        final SubtractAction r = new SubtractAction();
1510 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1511 <        final CFException ex = new CFException();
1512 <
1513 <        f.completeExceptionally(ex);
1514 <        checkIncomplete(h);
1515 <        g.complete(v1);
1516 <
1517 <        checkCompletedWithWrappedCFException(h, ex);
1518 <        checkCompletedWithWrappedCFException(f, ex);
1519 <        assertEquals(0, r.invocationCount);
1520 <        checkCompletedNormally(g, v1);
1521 <    }}
1522 <
1523 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1524 <        for (ExecutionMode m : ExecutionMode.values())
1525 <        for (Integer v1 : new Integer[] { 1, null })
1526 <    {
1527 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1528 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1529 <        final SubtractAction r = new SubtractAction();
1530 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1531 <        final CFException ex = new CFException();
1532 <
1533 <        g.completeExceptionally(ex);
1534 <        checkIncomplete(h);
1535 <        f.complete(v1);
1536 <
1537 <        checkCompletedWithWrappedCFException(h, ex);
1538 <        checkCompletedWithWrappedCFException(g, ex);
1539 <        assertEquals(0, r.invocationCount);
1540 <        checkCompletedNormally(f, v1);
1541 <    }}
1542 <
1543 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1391 >    public void testThenAcceptBoth_exceptionalCompletion() {
1392          for (ExecutionMode m : ExecutionMode.values())
1393 +        for (boolean createIncomplete : new boolean[] { true, false })
1394 +        for (boolean fFirst : new boolean[] { true, false })
1395          for (Integer v1 : new Integer[] { 1, null })
1396      {
1397          final CompletableFuture<Integer> f = new CompletableFuture<>();
1398          final CompletableFuture<Integer> g = new CompletableFuture<>();
1549        final SubtractAction r = new SubtractAction();
1399          final CFException ex = new CFException();
1551
1552        g.completeExceptionally(ex);
1553        f.complete(v1);
1554        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1555
1556        checkCompletedWithWrappedCFException(h, ex);
1557        checkCompletedWithWrappedCFException(g, ex);
1558        assertEquals(0, r.invocationCount);
1559        checkCompletedNormally(f, v1);
1560    }}
1561
1562    public void testThenAcceptBoth_exceptionalCompletion4() {
1563        for (ExecutionMode m : ExecutionMode.values())
1564        for (Integer v1 : new Integer[] { 1, null })
1565    {
1566        final CompletableFuture<Integer> f = new CompletableFuture<>();
1567        final CompletableFuture<Integer> g = new CompletableFuture<>();
1400          final SubtractAction r = new SubtractAction();
1569        final CFException ex = new CFException();
1401  
1402 <        f.completeExceptionally(ex);
1403 <        g.complete(v1);
1402 >        (fFirst ? f : g).complete(v1);
1403 >        if (!createIncomplete)
1404 >            (!fFirst ? f : g).completeExceptionally(ex);
1405          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1406 +        if (createIncomplete) {
1407 +            checkIncomplete(h);
1408 +            (!fFirst ? f : g).completeExceptionally(ex);
1409 +        }
1410  
1411          checkCompletedWithWrappedCFException(h, ex);
1576        checkCompletedWithWrappedCFException(f, ex);
1412          assertEquals(0, r.invocationCount);
1413 <        checkCompletedNormally(g, v1);
1413 >        checkCompletedNormally(fFirst ? f : g, v1);
1414 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1415      }}
1416  
1417      /**
1418       * thenAcceptBoth result completes exceptionally if action does
1419       */
1420 <    public void testThenAcceptBoth_actionFailed1() {
1585 <        for (ExecutionMode m : ExecutionMode.values())
1586 <        for (Integer v1 : new Integer[] { 1, null })
1587 <        for (Integer v2 : new Integer[] { 2, null })
1588 <    {
1589 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1590 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1591 <        final FailingBiConsumer r = new FailingBiConsumer();
1592 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1593 <
1594 <        f.complete(v1);
1595 <        checkIncomplete(h);
1596 <        g.complete(v2);
1597 <
1598 <        checkCompletedWithWrappedCFException(h);
1599 <        checkCompletedNormally(f, v1);
1600 <        checkCompletedNormally(g, v2);
1601 <    }}
1602 <
1603 <    public void testThenAcceptBoth_actionFailed2() {
1420 >    public void testThenAcceptBoth_actionFailed() {
1421          for (ExecutionMode m : ExecutionMode.values())
1422 +        for (boolean fFirst : new boolean[] { true, false })
1423          for (Integer v1 : new Integer[] { 1, null })
1424          for (Integer v2 : new Integer[] { 2, null })
1425      {
# Line 1610 | Line 1428 | public class CompletableFutureTest exten
1428          final FailingBiConsumer r = new FailingBiConsumer();
1429          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1430  
1431 <        g.complete(v2);
1432 <        checkIncomplete(h);
1433 <        f.complete(v1);
1431 >        if (fFirst) {
1432 >            f.complete(v1);
1433 >            g.complete(v2);
1434 >        } else {
1435 >            g.complete(v2);
1436 >            f.complete(v1);
1437 >        }
1438  
1439          checkCompletedWithWrappedCFException(h);
1440          checkCompletedNormally(f, v1);
# Line 1622 | Line 1444 | public class CompletableFutureTest exten
1444      /**
1445       * thenAcceptBoth result completes exceptionally if either source cancelled
1446       */
1447 <    public void testThenAcceptBoth_sourceCancelled1() {
1626 <        for (ExecutionMode m : ExecutionMode.values())
1627 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1628 <        for (Integer v1 : new Integer[] { 1, null })
1629 <    {
1630 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1631 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1632 <        final SubtractAction r = new SubtractAction();
1633 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1634 <
1635 <        assertTrue(f.cancel(mayInterruptIfRunning));
1636 <        checkIncomplete(h);
1637 <        g.complete(v1);
1638 <
1639 <        checkCompletedWithWrappedCancellationException(h);
1640 <        checkCancelled(f);
1641 <        assertEquals(0, r.invocationCount);
1642 <        checkCompletedNormally(g, v1);
1643 <    }}
1644 <
1645 <    public void testThenAcceptBoth_sourceCancelled2() {
1646 <        for (ExecutionMode m : ExecutionMode.values())
1647 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1648 <        for (Integer v1 : new Integer[] { 1, null })
1649 <    {
1650 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1651 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1652 <        final SubtractAction r = new SubtractAction();
1653 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1654 <
1655 <        assertTrue(g.cancel(mayInterruptIfRunning));
1656 <        checkIncomplete(h);
1657 <        f.complete(v1);
1658 <
1659 <        checkCompletedWithWrappedCancellationException(h);
1660 <        checkCancelled(g);
1661 <        assertEquals(0, r.invocationCount);
1662 <        checkCompletedNormally(f, v1);
1663 <    }}
1664 <
1665 <    public void testThenAcceptBoth_sourceCancelled3() {
1666 <        for (ExecutionMode m : ExecutionMode.values())
1667 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1668 <        for (Integer v1 : new Integer[] { 1, null })
1669 <    {
1670 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1671 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1672 <        final SubtractAction r = new SubtractAction();
1673 <
1674 <        assertTrue(g.cancel(mayInterruptIfRunning));
1675 <        f.complete(v1);
1676 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1677 <
1678 <        checkCompletedWithWrappedCancellationException(h);
1679 <        checkCancelled(g);
1680 <        assertEquals(0, r.invocationCount);
1681 <        checkCompletedNormally(f, v1);
1682 <    }}
1683 <
1684 <    public void testThenAcceptBoth_sourceCancelled4() {
1447 >    public void testThenAcceptBoth_sourceCancelled() {
1448          for (ExecutionMode m : ExecutionMode.values())
1449          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1450 +        for (boolean createIncomplete : new boolean[] { true, false })
1451 +        for (boolean fFirst : new boolean[] { true, false })
1452          for (Integer v1 : new Integer[] { 1, null })
1453      {
1454          final CompletableFuture<Integer> f = new CompletableFuture<>();
1455          final CompletableFuture<Integer> g = new CompletableFuture<>();
1456          final SubtractAction r = new SubtractAction();
1457  
1458 <        assertTrue(f.cancel(mayInterruptIfRunning));
1459 <        g.complete(v1);
1458 >        (fFirst ? f : g).complete(v1);
1459 >        if (!createIncomplete)
1460 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1461          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1462 +        if (createIncomplete) {
1463 +            checkIncomplete(h);
1464 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1465 +        }
1466  
1467          checkCompletedWithWrappedCancellationException(h);
1468 <        checkCancelled(f);
1468 >        checkCancelled(!fFirst ? f : g);
1469          assertEquals(0, r.invocationCount);
1470 <        checkCompletedNormally(g, v1);
1470 >        checkCompletedNormally(fFirst ? f : g, v1);
1471      }}
1472  
1473      /**
# Line 1876 | Line 1646 | public class CompletableFutureTest exten
1646      {
1647          final CompletableFuture<Integer> f = new CompletableFuture<>();
1648          final CompletableFuture<Integer> g = new CompletableFuture<>();
1649 <        final FailingNoop r = new FailingNoop();
1649 >        final FailingRunnable r = new FailingRunnable();
1650          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1651  
1652          f.complete(v1);
# Line 1895 | Line 1665 | public class CompletableFutureTest exten
1665      {
1666          final CompletableFuture<Integer> f = new CompletableFuture<>();
1667          final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final FailingNoop r = new FailingNoop();
1668 >        final FailingRunnable r = new FailingRunnable();
1669          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670  
1671          g.complete(v2);
# Line 2759 | Line 2529 | public class CompletableFutureTest exten
2529      {
2530          final CompletableFuture<Integer> f = new CompletableFuture<>();
2531          final CompletableFuture<Integer> g = new CompletableFuture<>();
2532 <        final FailingNoop r = new FailingNoop();
2532 >        final FailingRunnable r = new FailingRunnable();
2533          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2534  
2535          f.complete(v1);
# Line 2776 | Line 2546 | public class CompletableFutureTest exten
2546      {
2547          final CompletableFuture<Integer> f = new CompletableFuture<>();
2548          final CompletableFuture<Integer> g = new CompletableFuture<>();
2549 <        final FailingNoop r = new FailingNoop();
2549 >        final FailingRunnable r = new FailingRunnable();
2550          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2551  
2552          g.complete(v2);
# Line 2953 | Line 2723 | public class CompletableFutureTest exten
2723          final CompletableFutureInc r = new CompletableFutureInc();
2724          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2725          final CompletableFuture<Integer> g = f.thenCompose(r);
2726 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2726 >        if (createIncomplete) {
2727 >            checkIncomplete(g);
2728 >            assertTrue(f.cancel(mayInterruptIfRunning));
2729 >        }
2730  
2731          checkCompletedWithWrappedCancellationException(g);
2732          checkCancelled(f);
2733      }}
2734  
2962    // asyncs
2963
2964    /**
2965     * thenAcceptAsync result completes normally after normal
2966     * completion of source
2967     */
2968    public void testThenAcceptAsync() {
2969        CompletableFuture<Integer> f = new CompletableFuture<>();
2970        IncAction r = new IncAction();
2971        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2972        f.complete(one);
2973        checkCompletedNormally(g, null);
2974        assertEquals(r.value, (Integer) 2);
2975    }
2976
2977    /**
2978     * thenAcceptAsync result completes exceptionally after exceptional
2979     * completion of source
2980     */
2981    public void testThenAcceptAsync2() {
2982        CompletableFuture<Integer> f = new CompletableFuture<>();
2983        IncAction r = new IncAction();
2984        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2985        f.completeExceptionally(new CFException());
2986        checkCompletedWithWrappedCFException(g);
2987    }
2988
2989    /**
2990     * thenAcceptAsync result completes exceptionally if action does
2991     */
2992    public void testThenAcceptAsync3() {
2993        CompletableFuture<Integer> f = new CompletableFuture<>();
2994        FailingConsumer r = new FailingConsumer();
2995        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2996        f.complete(null);
2997        checkCompletedWithWrappedCFException(g);
2998    }
2999
3000    /**
3001     * thenAcceptAsync result completes exceptionally if source cancelled
3002     */
3003    public void testThenAcceptAsync4() {
3004        CompletableFuture<Integer> f = new CompletableFuture<>();
3005        IncAction r = new IncAction();
3006        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3007        assertTrue(f.cancel(true));
3008        checkCompletedWithWrappedCancellationException(g);
3009    }
3010
3011    // async with explicit executors
3012
3013    /**
3014     * thenAcceptAsync result completes normally after normal
3015     * completion of source
3016     */
3017    public void testThenAcceptAsyncE() {
3018        CompletableFuture<Integer> f = new CompletableFuture<>();
3019        IncAction r = new IncAction();
3020        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3021        f.complete(one);
3022        checkCompletedNormally(g, null);
3023        assertEquals(r.value, (Integer) 2);
3024    }
3025
3026    /**
3027     * thenAcceptAsync result completes exceptionally after exceptional
3028     * completion of source
3029     */
3030    public void testThenAcceptAsync2E() {
3031        CompletableFuture<Integer> f = new CompletableFuture<>();
3032        IncAction r = new IncAction();
3033        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3034        f.completeExceptionally(new CFException());
3035        checkCompletedWithWrappedCFException(g);
3036    }
3037
3038    /**
3039     * thenAcceptAsync result completes exceptionally if action does
3040     */
3041    public void testThenAcceptAsync3E() {
3042        CompletableFuture<Integer> f = new CompletableFuture<>();
3043        FailingConsumer r = new FailingConsumer();
3044        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3045        f.complete(null);
3046        checkCompletedWithWrappedCFException(g);
3047    }
3048
3049    /**
3050     * thenAcceptAsync result completes exceptionally if source cancelled
3051     */
3052    public void testThenAcceptAsync4E() {
3053        CompletableFuture<Integer> f = new CompletableFuture<>();
3054        IncAction r = new IncAction();
3055        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3056        assertTrue(f.cancel(true));
3057        checkCompletedWithWrappedCancellationException(g);
3058    }
3059
2735      // other static methods
2736  
2737      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines