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

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.19 by jsr166, Mon Sep 27 19:15:16 2010 UTC vs.
Revision 1.20 by dl, Sun Oct 24 13:41:17 2010 UTC

# Line 25 | Line 25 | public class ForkJoinTaskTest extends JS
25          return new TestSuite(ForkJoinTaskTest.class);
26      }
27  
28 +    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
29 +    static final int mainPoolSize =
30 +        Math.max(2, Runtime.getRuntime().availableProcessors());
31 +
32      private static ForkJoinPool mainPool() {
33 <        return new ForkJoinPool();
33 >        return new ForkJoinPool(mainPoolSize);
34      }
35  
36      private static ForkJoinPool singletonPool() {
# Line 965 | Line 969 | public class ForkJoinTaskTest extends JS
969              }};
970          testInvokeOnPool(asyncSingletonPool(), a);
971      }
972 +
973 +    // versions for singleton pools
974 +
975 +    /**
976 +     * invoke returns when task completes normally.
977 +     * isCompletedAbnormally and isCancelled return false for normally
978 +     * completed tasks. getRawResult of a RecursiveAction returns null;
979 +     */
980 +    public void testInvokeSingleton() {
981 +        RecursiveAction a = new CheckedRecursiveAction() {
982 +            public void realCompute() {
983 +                AsyncFib f = new AsyncFib(8);
984 +                assertNull(f.invoke());
985 +                assertEquals(21, f.number);
986 +                assertTrue(f.isDone());
987 +                assertFalse(f.isCancelled());
988 +                assertFalse(f.isCompletedAbnormally());
989 +                assertNull(f.getRawResult());
990 +            }};
991 +        testInvokeOnPool(singletonPool(), a);
992 +    }
993 +
994 +    /**
995 +     * quietlyInvoke task returns when task completes normally.
996 +     * isCompletedAbnormally and isCancelled return false for normally
997 +     * completed tasks
998 +     */
999 +    public void testQuietlyInvokeSingleton() {
1000 +        RecursiveAction a = new CheckedRecursiveAction() {
1001 +            public void realCompute() {
1002 +                AsyncFib f = new AsyncFib(8);
1003 +                f.quietlyInvoke();
1004 +                assertEquals(21, f.number);
1005 +                assertTrue(f.isDone());
1006 +                assertFalse(f.isCancelled());
1007 +                assertFalse(f.isCompletedAbnormally());
1008 +                assertNull(f.getRawResult());
1009 +            }};
1010 +        testInvokeOnPool(singletonPool(), a);
1011 +    }
1012 +
1013 +    /**
1014 +     * join of a forked task returns when task completes
1015 +     */
1016 +    public void testForkJoinSingleton() {
1017 +        RecursiveAction a = new CheckedRecursiveAction() {
1018 +            public void realCompute() {
1019 +                AsyncFib f = new AsyncFib(8);
1020 +                assertSame(f, f.fork());
1021 +                assertNull(f.join());
1022 +                assertEquals(21, f.number);
1023 +                assertTrue(f.isDone());
1024 +                assertNull(f.getRawResult());
1025 +            }};
1026 +        testInvokeOnPool(singletonPool(), a);
1027 +    }
1028 +
1029 +    /**
1030 +     * get of a forked task returns when task completes
1031 +     */
1032 +    public void testForkGetSingleton() {
1033 +        RecursiveAction a = new CheckedRecursiveAction() {
1034 +            public void realCompute() throws Exception {
1035 +                AsyncFib f = new AsyncFib(8);
1036 +                assertSame(f, f.fork());
1037 +                assertNull(f.get());
1038 +                assertEquals(21, f.number);
1039 +                assertTrue(f.isDone());
1040 +            }};
1041 +        testInvokeOnPool(singletonPool(), a);
1042 +    }
1043 +
1044 +    /**
1045 +     * timed get of a forked task returns when task completes
1046 +     */
1047 +    public void testForkTimedGetSingleton() {
1048 +        RecursiveAction a = new CheckedRecursiveAction() {
1049 +            public void realCompute() throws Exception {
1050 +                AsyncFib f = new AsyncFib(8);
1051 +                assertSame(f, f.fork());
1052 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1053 +                assertEquals(21, f.number);
1054 +                assertTrue(f.isDone());
1055 +            }};
1056 +        testInvokeOnPool(singletonPool(), a);
1057 +    }
1058 +
1059 +    /**
1060 +     * timed get with null time unit throws NPE
1061 +     */
1062 +    public void testForkTimedGetNPESingleton() {
1063 +        RecursiveAction a = new CheckedRecursiveAction() {
1064 +            public void realCompute() throws Exception {
1065 +                AsyncFib f = new AsyncFib(8);
1066 +                assertSame(f, f.fork());
1067 +                try {
1068 +                    f.get(5L, null);
1069 +                    shouldThrow();
1070 +                } catch (NullPointerException success) {}
1071 +            }};
1072 +        testInvokeOnPool(singletonPool(), a);
1073 +    }
1074 +
1075 +    /**
1076 +     * quietlyJoin of a forked task returns when task completes
1077 +     */
1078 +    public void testForkQuietlyJoinSingleton() {
1079 +        RecursiveAction a = new CheckedRecursiveAction() {
1080 +            public void realCompute() {
1081 +                AsyncFib f = new AsyncFib(8);
1082 +                assertSame(f, f.fork());
1083 +                f.quietlyJoin();
1084 +                assertEquals(21, f.number);
1085 +                assertTrue(f.isDone());
1086 +            }};
1087 +        testInvokeOnPool(singletonPool(), a);
1088 +    }
1089 +
1090 +
1091 +    /**
1092 +     * helpQuiesce returns when tasks are complete.
1093 +     * getQueuedTaskCount returns 0 when quiescent
1094 +     */
1095 +    public void testForkHelpQuiesceSingleton() {
1096 +        RecursiveAction a = new CheckedRecursiveAction() {
1097 +            public void realCompute() {
1098 +                AsyncFib f = new AsyncFib(8);
1099 +                assertSame(f, f.fork());
1100 +                f.helpQuiesce();
1101 +                assertEquals(21, f.number);
1102 +                assertTrue(f.isDone());
1103 +                assertEquals(0, getQueuedTaskCount());
1104 +            }};
1105 +        testInvokeOnPool(singletonPool(), a);
1106 +    }
1107 +
1108 +
1109 +    /**
1110 +     * invoke task throws exception when task completes abnormally
1111 +     */
1112 +    public void testAbnormalInvokeSingleton() {
1113 +        RecursiveAction a = new CheckedRecursiveAction() {
1114 +            public void realCompute() {
1115 +                FailingAsyncFib f = new FailingAsyncFib(8);
1116 +                try {
1117 +                    f.invoke();
1118 +                    shouldThrow();
1119 +                } catch (FJException success) {}
1120 +            }};
1121 +        testInvokeOnPool(singletonPool(), a);
1122 +    }
1123 +
1124 +    /**
1125 +     * quietlyInvoke task returns when task completes abnormally
1126 +     */
1127 +    public void testAbnormalQuietlyInvokeSingleton() {
1128 +        RecursiveAction a = new CheckedRecursiveAction() {
1129 +            public void realCompute() {
1130 +                FailingAsyncFib f = new FailingAsyncFib(8);
1131 +                f.quietlyInvoke();
1132 +                assertTrue(f.isDone());
1133 +            }};
1134 +        testInvokeOnPool(singletonPool(), a);
1135 +    }
1136 +
1137 +    /**
1138 +     * join of a forked task throws exception when task completes abnormally
1139 +     */
1140 +    public void testAbnormalForkJoinSingleton() {
1141 +        RecursiveAction a = new CheckedRecursiveAction() {
1142 +            public void realCompute() {
1143 +                FailingAsyncFib f = new FailingAsyncFib(8);
1144 +                assertSame(f, f.fork());
1145 +                try {
1146 +                    f.join();
1147 +                    shouldThrow();
1148 +                } catch (FJException success) {}
1149 +            }};
1150 +        testInvokeOnPool(singletonPool(), a);
1151 +    }
1152 +
1153 +    /**
1154 +     * get of a forked task throws exception when task completes abnormally
1155 +     */
1156 +    public void testAbnormalForkGetSingleton() {
1157 +        RecursiveAction a = new CheckedRecursiveAction() {
1158 +            public void realCompute() throws Exception {
1159 +                FailingAsyncFib f = new FailingAsyncFib(8);
1160 +                assertSame(f, f.fork());
1161 +                try {
1162 +                    f.get();
1163 +                    shouldThrow();
1164 +                } catch (ExecutionException success) {
1165 +                    Throwable cause = success.getCause();
1166 +                    assertTrue(cause instanceof FJException);
1167 +                    assertTrue(f.isDone());
1168 +                    assertTrue(f.isCompletedAbnormally());
1169 +                    assertSame(cause, f.getException());
1170 +                }
1171 +            }};
1172 +        testInvokeOnPool(singletonPool(), a);
1173 +    }
1174 +
1175 +    /**
1176 +     * timed get of a forked task throws exception when task completes abnormally
1177 +     */
1178 +    public void testAbnormalForkTimedGetSingleton() {
1179 +        RecursiveAction a = new CheckedRecursiveAction() {
1180 +            public void realCompute() throws Exception {
1181 +                FailingAsyncFib f = new FailingAsyncFib(8);
1182 +                assertSame(f, f.fork());
1183 +                try {
1184 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1185 +                    shouldThrow();
1186 +                } catch (ExecutionException success) {
1187 +                    Throwable cause = success.getCause();
1188 +                    assertTrue(cause instanceof FJException);
1189 +                    assertTrue(f.isDone());
1190 +                    assertTrue(f.isCompletedAbnormally());
1191 +                    assertSame(cause, f.getException());
1192 +                }
1193 +            }};
1194 +        testInvokeOnPool(singletonPool(), a);
1195 +    }
1196 +
1197 +    /**
1198 +     * quietlyJoin of a forked task returns when task completes abnormally
1199 +     */
1200 +    public void testAbnormalForkQuietlyJoinSingleton() {
1201 +        RecursiveAction a = new CheckedRecursiveAction() {
1202 +            public void realCompute() {
1203 +                FailingAsyncFib f = new FailingAsyncFib(8);
1204 +                assertSame(f, f.fork());
1205 +                f.quietlyJoin();
1206 +                assertTrue(f.isDone());
1207 +                assertTrue(f.isCompletedAbnormally());
1208 +                assertTrue(f.getException() instanceof FJException);
1209 +            }};
1210 +        testInvokeOnPool(singletonPool(), a);
1211 +    }
1212 +
1213 +    /**
1214 +     * invoke task throws exception when task cancelled
1215 +     */
1216 +    public void testCancelledInvokeSingleton() {
1217 +        RecursiveAction a = new CheckedRecursiveAction() {
1218 +            public void realCompute() {
1219 +                AsyncFib f = new AsyncFib(8);
1220 +                assertTrue(f.cancel(true));
1221 +                try {
1222 +                    f.invoke();
1223 +                    shouldThrow();
1224 +                } catch (CancellationException success) {
1225 +                    assertTrue(f.isDone());
1226 +                    assertTrue(f.isCancelled());
1227 +                    assertTrue(f.isCompletedAbnormally());
1228 +                    assertTrue(f.getException() instanceof CancellationException);
1229 +                }
1230 +            }};
1231 +        testInvokeOnPool(singletonPool(), a);
1232 +    }
1233 +
1234 +    /**
1235 +     * join of a forked task throws exception when task cancelled
1236 +     */
1237 +    public void testCancelledForkJoinSingleton() {
1238 +        RecursiveAction a = new CheckedRecursiveAction() {
1239 +            public void realCompute() {
1240 +                AsyncFib f = new AsyncFib(8);
1241 +                assertTrue(f.cancel(true));
1242 +                assertSame(f, f.fork());
1243 +                try {
1244 +                    f.join();
1245 +                    shouldThrow();
1246 +                } catch (CancellationException success) {
1247 +                    assertTrue(f.isDone());
1248 +                    assertTrue(f.isCancelled());
1249 +                    assertTrue(f.isCompletedAbnormally());
1250 +                    assertTrue(f.getException() instanceof CancellationException);
1251 +                }
1252 +            }};
1253 +        testInvokeOnPool(singletonPool(), a);
1254 +    }
1255 +
1256 +    /**
1257 +     * get of a forked task throws exception when task cancelled
1258 +     */
1259 +    public void testCancelledForkGetSingleton() {
1260 +        RecursiveAction a = new CheckedRecursiveAction() {
1261 +            public void realCompute() throws Exception {
1262 +                AsyncFib f = new AsyncFib(8);
1263 +                assertTrue(f.cancel(true));
1264 +                assertSame(f, f.fork());
1265 +                try {
1266 +                    f.get();
1267 +                    shouldThrow();
1268 +                } catch (CancellationException success) {
1269 +                    assertTrue(f.isDone());
1270 +                    assertTrue(f.isCancelled());
1271 +                    assertTrue(f.isCompletedAbnormally());
1272 +                    assertTrue(f.getException() instanceof CancellationException);
1273 +                }
1274 +            }};
1275 +        testInvokeOnPool(singletonPool(), a);
1276 +    }
1277 +
1278 +    /**
1279 +     * timed get of a forked task throws exception when task cancelled
1280 +     */
1281 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1282 +        RecursiveAction a = new CheckedRecursiveAction() {
1283 +            public void realCompute() throws Exception {
1284 +                AsyncFib f = new AsyncFib(8);
1285 +                assertTrue(f.cancel(true));
1286 +                assertSame(f, f.fork());
1287 +                try {
1288 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1289 +                    shouldThrow();
1290 +                } catch (CancellationException success) {
1291 +                    assertTrue(f.isDone());
1292 +                    assertTrue(f.isCancelled());
1293 +                    assertTrue(f.isCompletedAbnormally());
1294 +                    assertTrue(f.getException() instanceof CancellationException);
1295 +                }
1296 +            }};
1297 +        testInvokeOnPool(singletonPool(), a);
1298 +    }
1299 +
1300 +    /**
1301 +     * quietlyJoin of a forked task returns when task cancelled
1302 +     */
1303 +    public void testCancelledForkQuietlyJoinSingleton() {
1304 +        RecursiveAction a = new CheckedRecursiveAction() {
1305 +            public void realCompute() {
1306 +                AsyncFib f = new AsyncFib(8);
1307 +                assertTrue(f.cancel(true));
1308 +                assertSame(f, f.fork());
1309 +                f.quietlyJoin();
1310 +                assertTrue(f.isDone());
1311 +                assertTrue(f.isCompletedAbnormally());
1312 +                assertTrue(f.isCancelled());
1313 +                assertTrue(f.getException() instanceof CancellationException);
1314 +            }};
1315 +        testInvokeOnPool(singletonPool(), a);
1316 +    }
1317 +
1318 +    /**
1319 +     * invoke task throws exception after invoking completeExceptionally
1320 +     */
1321 +    public void testCompleteExceptionallySingleton() {
1322 +        RecursiveAction a = new CheckedRecursiveAction() {
1323 +            public void realCompute() {
1324 +                AsyncFib f = new AsyncFib(8);
1325 +                f.completeExceptionally(new FJException());
1326 +                try {
1327 +                    f.invoke();
1328 +                    shouldThrow();
1329 +                } catch (FJException success) {}
1330 +            }};
1331 +        testInvokeOnPool(singletonPool(), a);
1332 +    }
1333 +
1334 +    /**
1335 +     * invokeAll(t1, t2) invokes all task arguments
1336 +     */
1337 +    public void testInvokeAll2Singleton() {
1338 +        RecursiveAction a = new CheckedRecursiveAction() {
1339 +            public void realCompute() {
1340 +                AsyncFib f = new AsyncFib(8);
1341 +                AsyncFib g = new AsyncFib(9);
1342 +                invokeAll(f, g);
1343 +                assertTrue(f.isDone());
1344 +                assertEquals(21, f.number);
1345 +                assertTrue(g.isDone());
1346 +                assertEquals(34, g.number);
1347 +            }};
1348 +        testInvokeOnPool(singletonPool(), a);
1349 +    }
1350 +
1351 +    /**
1352 +     * invokeAll(tasks) with 1 argument invokes task
1353 +     */
1354 +    public void testInvokeAll1Singleton() {
1355 +        RecursiveAction a = new CheckedRecursiveAction() {
1356 +            public void realCompute() {
1357 +                AsyncFib f = new AsyncFib(8);
1358 +                invokeAll(f);
1359 +                assertTrue(f.isDone());
1360 +                assertEquals(21, f.number);
1361 +            }};
1362 +        testInvokeOnPool(singletonPool(), a);
1363 +    }
1364 +
1365 +    /**
1366 +     * invokeAll(tasks) with > 2 argument invokes tasks
1367 +     */
1368 +    public void testInvokeAll3Singleton() {
1369 +        RecursiveAction a = new CheckedRecursiveAction() {
1370 +            public void realCompute() {
1371 +                AsyncFib f = new AsyncFib(8);
1372 +                AsyncFib g = new AsyncFib(9);
1373 +                AsyncFib h = new AsyncFib(7);
1374 +                invokeAll(f, g, h);
1375 +                assertTrue(f.isDone());
1376 +                assertEquals(21, f.number);
1377 +                assertTrue(g.isDone());
1378 +                assertEquals(34, g.number);
1379 +                assertTrue(h.isDone());
1380 +                assertEquals(13, h.number);
1381 +            }};
1382 +        testInvokeOnPool(singletonPool(), a);
1383 +    }
1384 +
1385 +    /**
1386 +     * invokeAll(collection) invokes all tasks in the collection
1387 +     */
1388 +    public void testInvokeAllCollectionSingleton() {
1389 +        RecursiveAction a = new CheckedRecursiveAction() {
1390 +            public void realCompute() {
1391 +                AsyncFib f = new AsyncFib(8);
1392 +                AsyncFib g = new AsyncFib(9);
1393 +                AsyncFib h = new AsyncFib(7);
1394 +                HashSet set = new HashSet();
1395 +                set.add(f);
1396 +                set.add(g);
1397 +                set.add(h);
1398 +                invokeAll(set);
1399 +                assertTrue(f.isDone());
1400 +                assertEquals(21, f.number);
1401 +                assertTrue(g.isDone());
1402 +                assertEquals(34, g.number);
1403 +                assertTrue(h.isDone());
1404 +                assertEquals(13, h.number);
1405 +            }};
1406 +        testInvokeOnPool(singletonPool(), a);
1407 +    }
1408 +
1409 +
1410 +    /**
1411 +     * invokeAll(tasks) with any null task throws NPE
1412 +     */
1413 +    public void testInvokeAllNPESingleton() {
1414 +        RecursiveAction a = new CheckedRecursiveAction() {
1415 +            public void realCompute() {
1416 +                AsyncFib f = new AsyncFib(8);
1417 +                AsyncFib g = new AsyncFib(9);
1418 +                AsyncFib h = null;
1419 +                try {
1420 +                    invokeAll(f, g, h);
1421 +                    shouldThrow();
1422 +                } catch (NullPointerException success) {}
1423 +            }};
1424 +        testInvokeOnPool(singletonPool(), a);
1425 +    }
1426 +
1427 +    /**
1428 +     * invokeAll(t1, t2) throw exception if any task does
1429 +     */
1430 +    public void testAbnormalInvokeAll2Singleton() {
1431 +        RecursiveAction a = new CheckedRecursiveAction() {
1432 +            public void realCompute() {
1433 +                AsyncFib f = new AsyncFib(8);
1434 +                FailingAsyncFib g = new FailingAsyncFib(9);
1435 +                try {
1436 +                    invokeAll(f, g);
1437 +                    shouldThrow();
1438 +                } catch (FJException success) {}
1439 +            }};
1440 +        testInvokeOnPool(singletonPool(), a);
1441 +    }
1442 +
1443 +    /**
1444 +     * invokeAll(tasks) with 1 argument throws exception if task does
1445 +     */
1446 +    public void testAbnormalInvokeAll1Singleton() {
1447 +        RecursiveAction a = new CheckedRecursiveAction() {
1448 +            public void realCompute() {
1449 +                FailingAsyncFib g = new FailingAsyncFib(9);
1450 +                try {
1451 +                    invokeAll(g);
1452 +                    shouldThrow();
1453 +                } catch (FJException success) {}
1454 +            }};
1455 +        testInvokeOnPool(singletonPool(), a);
1456 +    }
1457 +
1458 +    /**
1459 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1460 +     */
1461 +    public void testAbnormalInvokeAll3Singleton() {
1462 +        RecursiveAction a = new CheckedRecursiveAction() {
1463 +            public void realCompute() {
1464 +                AsyncFib f = new AsyncFib(8);
1465 +                FailingAsyncFib g = new FailingAsyncFib(9);
1466 +                AsyncFib h = new AsyncFib(7);
1467 +                try {
1468 +                    invokeAll(f, g, h);
1469 +                    shouldThrow();
1470 +                } catch (FJException success) {}
1471 +            }};
1472 +        testInvokeOnPool(singletonPool(), a);
1473 +    }
1474 +
1475 +    /**
1476 +     * invokeAll(collection)  throws exception if any task does
1477 +     */
1478 +    public void testAbnormalInvokeAllCollectionSingleton() {
1479 +        RecursiveAction a = new CheckedRecursiveAction() {
1480 +            public void realCompute() {
1481 +                FailingAsyncFib f = new FailingAsyncFib(8);
1482 +                AsyncFib g = new AsyncFib(9);
1483 +                AsyncFib h = new AsyncFib(7);
1484 +                HashSet set = new HashSet();
1485 +                set.add(f);
1486 +                set.add(g);
1487 +                set.add(h);
1488 +                try {
1489 +                    invokeAll(set);
1490 +                    shouldThrow();
1491 +                } catch (FJException success) {}
1492 +            }};
1493 +        testInvokeOnPool(singletonPool(), a);
1494 +    }
1495 +
1496   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines