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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.11 by dl, Thu Dec 4 20:54:46 2003 UTC vs.
Revision 1.16 by dl, Sun Dec 28 21:56:18 2003 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.*;
10   import junit.framework.*;
11 < import java.util.List;
11 > import java.util.*;
12  
13   public class ThreadPoolExecutorTest extends JSR166TestCase {
14      public static void main(String[] args) {
# Line 1025 | Line 1026 | public class ThreadPoolExecutorTest exte
1026              joinPool(tpe);
1027          }
1028      }
1029 +
1030 +    /**
1031 +     * completed submit of callable returns result
1032 +     */
1033 +    public void testSubmitCallable() {
1034 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1035 +        try {
1036 +            Future<String> future = e.submit(new StringTask());
1037 +            String result = future.get();
1038 +            assertSame(TEST_STRING, result);
1039 +        }
1040 +        catch (ExecutionException ex) {
1041 +            unexpectedException();
1042 +        }
1043 +        catch (InterruptedException ex) {
1044 +            unexpectedException();
1045 +        } finally {
1046 +            joinPool(e);
1047 +        }
1048 +    }
1049 +
1050 +    /**
1051 +     * completed submit of runnable returns successfully
1052 +     */
1053 +    public void testSubmitRunnable() {
1054 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1055 +        try {
1056 +            Future<?> future = e.submit(new NoOpRunnable());
1057 +            future.get();
1058 +            assertTrue(future.isDone());
1059 +        }
1060 +        catch (ExecutionException ex) {
1061 +            unexpectedException();
1062 +        }
1063 +        catch (InterruptedException ex) {
1064 +            unexpectedException();
1065 +        } finally {
1066 +            joinPool(e);
1067 +        }
1068 +    }
1069 +
1070 +    /**
1071 +     * completed submit of (runnable, result) returns result
1072 +     */
1073 +    public void testSubmitRunnable2() {
1074 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1075 +        try {
1076 +            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1077 +            String result = future.get();
1078 +            assertSame(TEST_STRING, result);
1079 +        }
1080 +        catch (ExecutionException ex) {
1081 +            unexpectedException();
1082 +        }
1083 +        catch (InterruptedException ex) {
1084 +            unexpectedException();
1085 +        } finally {
1086 +            joinPool(e);
1087 +        }
1088 +    }
1089 +
1090 +
1091 +
1092 +
1093 +
1094 +    /**
1095 +     * invokeAny(null) throws NPE
1096 +     */
1097 +    public void testInvokeAny1() {
1098 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1099 +        try {
1100 +            e.invokeAny(null);
1101 +        } catch (NullPointerException success) {
1102 +        } catch(Exception ex) {
1103 +            unexpectedException();
1104 +        } finally {
1105 +            joinPool(e);
1106 +        }
1107 +    }
1108 +
1109 +    /**
1110 +     * invokeAny(empty collection) throws IAE
1111 +     */
1112 +    public void testInvokeAny2() {
1113 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1114 +        try {
1115 +            e.invokeAny(new ArrayList<Callable<String>>());
1116 +        } catch (IllegalArgumentException success) {
1117 +        } catch(Exception ex) {
1118 +            unexpectedException();
1119 +        } finally {
1120 +            joinPool(e);
1121 +        }
1122 +    }
1123 +
1124 +    /**
1125 +     * invokeAny(c) throws NPE if c has null elements
1126 +     */
1127 +    public void testInvokeAny3() {
1128 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1129 +        try {
1130 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1131 +            l.add(new StringTask());
1132 +            l.add(null);
1133 +            e.invokeAny(l);
1134 +        } catch (NullPointerException success) {
1135 +        } catch(Exception ex) {
1136 +            unexpectedException();
1137 +        } finally {
1138 +            joinPool(e);
1139 +        }
1140 +    }
1141 +
1142 +    /**
1143 +     * invokeAny(c) throws ExecutionException if no task completes
1144 +     */
1145 +    public void testInvokeAny4() {
1146 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1147 +        try {
1148 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1149 +            l.add(new NPETask());
1150 +            e.invokeAny(l);
1151 +        } catch (ExecutionException success) {
1152 +        } catch(Exception ex) {
1153 +            unexpectedException();
1154 +        } finally {
1155 +            joinPool(e);
1156 +        }
1157 +    }
1158 +
1159 +    /**
1160 +     * invokeAny(c) returns result of some task
1161 +     */
1162 +    public void testInvokeAny5() {
1163 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1164 +        try {
1165 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1166 +            l.add(new StringTask());
1167 +            l.add(new StringTask());
1168 +            String result = e.invokeAny(l);
1169 +            assertSame(TEST_STRING, result);
1170 +        } catch (ExecutionException success) {
1171 +        } catch(Exception ex) {
1172 +            unexpectedException();
1173 +        } finally {
1174 +            joinPool(e);
1175 +        }
1176 +    }
1177 +
1178 +    /**
1179 +     * invokeAll(null) throws NPE
1180 +     */
1181 +    public void testInvokeAll1() {
1182 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1183 +        try {
1184 +            e.invokeAll(null);
1185 +        } catch (NullPointerException success) {
1186 +        } catch(Exception ex) {
1187 +            unexpectedException();
1188 +        } finally {
1189 +            joinPool(e);
1190 +        }
1191 +    }
1192 +
1193 +    /**
1194 +     * invokeAll(empty collection) returns empty collection
1195 +     */
1196 +    public void testInvokeAll2() {
1197 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1198 +        try {
1199 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1200 +            assertTrue(r.isEmpty());
1201 +        } catch(Exception ex) {
1202 +            unexpectedException();
1203 +        } finally {
1204 +            joinPool(e);
1205 +        }
1206 +    }
1207 +
1208 +    /**
1209 +     * invokeAll(c) throws NPE if c has null elements
1210 +     */
1211 +    public void testInvokeAll3() {
1212 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1213 +        try {
1214 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1215 +            l.add(new StringTask());
1216 +            l.add(null);
1217 +            e.invokeAll(l);
1218 +        } catch (NullPointerException success) {
1219 +        } catch(Exception ex) {
1220 +            unexpectedException();
1221 +        } finally {
1222 +            joinPool(e);
1223 +        }
1224 +    }
1225 +
1226 +    /**
1227 +     * get of element of invokeAll(c) throws exception on failed task
1228 +     */
1229 +    public void testInvokeAll4() {
1230 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1231 +        try {
1232 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1233 +            l.add(new NPETask());
1234 +            List<Future<String>> result = e.invokeAll(l);
1235 +            assertEquals(1, result.size());
1236 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1237 +                it.next().get();
1238 +        } catch(ExecutionException success) {
1239 +        } catch(Exception ex) {
1240 +            unexpectedException();
1241 +        } finally {
1242 +            joinPool(e);
1243 +        }
1244 +    }
1245 +
1246 +    /**
1247 +     * invokeAll(c) returns results of all completed tasks
1248 +     */
1249 +    public void testInvokeAll5() {
1250 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1251 +        try {
1252 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1253 +            l.add(new StringTask());
1254 +            l.add(new StringTask());
1255 +            List<Future<String>> result = e.invokeAll(l);
1256 +            assertEquals(2, result.size());
1257 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1258 +                assertSame(TEST_STRING, it.next().get());
1259 +        } catch (ExecutionException success) {
1260 +        } catch(Exception ex) {
1261 +            unexpectedException();
1262 +        } finally {
1263 +            joinPool(e);
1264 +        }
1265 +    }
1266 +
1267 +
1268 +
1269 +    /**
1270 +     * timed invokeAny(null) throws NPE
1271 +     */
1272 +    public void testTimedInvokeAny1() {
1273 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1274 +        try {
1275 +            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1276 +        } catch (NullPointerException success) {
1277 +        } catch(Exception ex) {
1278 +            unexpectedException();
1279 +        } finally {
1280 +            joinPool(e);
1281 +        }
1282 +    }
1283 +
1284 +    /**
1285 +     * timed invokeAny(,,null) throws NPE
1286 +     */
1287 +    public void testTimedInvokeAnyNullTimeUnit() {
1288 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1289 +        try {
1290 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1291 +            l.add(new StringTask());
1292 +            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1293 +        } catch (NullPointerException success) {
1294 +        } catch(Exception ex) {
1295 +            unexpectedException();
1296 +        } finally {
1297 +            joinPool(e);
1298 +        }
1299 +    }
1300 +
1301 +    /**
1302 +     * timed invokeAny(empty collection) throws IAE
1303 +     */
1304 +    public void testTimedInvokeAny2() {
1305 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1306 +        try {
1307 +            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1308 +        } catch (IllegalArgumentException success) {
1309 +        } catch(Exception ex) {
1310 +            unexpectedException();
1311 +        } finally {
1312 +            joinPool(e);
1313 +        }
1314 +    }
1315 +
1316 +    /**
1317 +     * timed invokeAny(c) throws NPE if c has null elements
1318 +     */
1319 +    public void testTimedInvokeAny3() {
1320 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1321 +        try {
1322 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1323 +            l.add(new StringTask());
1324 +            l.add(null);
1325 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1326 +        } catch (NullPointerException success) {
1327 +        } catch(Exception ex) {
1328 +            ex.printStackTrace();
1329 +            unexpectedException();
1330 +        } finally {
1331 +            joinPool(e);
1332 +        }
1333 +    }
1334 +
1335 +    /**
1336 +     * timed invokeAny(c) throws ExecutionException if no task completes
1337 +     */
1338 +    public void testTimedInvokeAny4() {
1339 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1340 +        try {
1341 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1342 +            l.add(new NPETask());
1343 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1344 +        } catch(ExecutionException success) {
1345 +        } catch(Exception ex) {
1346 +            unexpectedException();
1347 +        } finally {
1348 +            joinPool(e);
1349 +        }
1350 +    }
1351 +
1352 +    /**
1353 +     * timed invokeAny(c) returns result of some task
1354 +     */
1355 +    public void testTimedInvokeAny5() {
1356 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1357 +        try {
1358 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1359 +            l.add(new StringTask());
1360 +            l.add(new StringTask());
1361 +            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1362 +            assertSame(TEST_STRING, result);
1363 +        } catch (ExecutionException success) {
1364 +        } catch(Exception ex) {
1365 +            unexpectedException();
1366 +        } finally {
1367 +            joinPool(e);
1368 +        }
1369 +    }
1370 +
1371 +    /**
1372 +     * timed invokeAll(null) throws NPE
1373 +     */
1374 +    public void testTimedInvokeAll1() {
1375 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376 +        try {
1377 +            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1378 +        } catch (NullPointerException success) {
1379 +        } catch(Exception ex) {
1380 +            unexpectedException();
1381 +        } finally {
1382 +            joinPool(e);
1383 +        }
1384 +    }
1385 +
1386 +    /**
1387 +     * timed invokeAll(,,null) throws NPE
1388 +     */
1389 +    public void testTimedInvokeAllNullTimeUnit() {
1390 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1391 +        try {
1392 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1393 +            l.add(new StringTask());
1394 +            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1395 +        } catch (NullPointerException success) {
1396 +        } catch(Exception ex) {
1397 +            unexpectedException();
1398 +        } finally {
1399 +            joinPool(e);
1400 +        }
1401 +    }
1402 +
1403 +    /**
1404 +     * timed invokeAll(empty collection) returns empty collection
1405 +     */
1406 +    public void testTimedInvokeAll2() {
1407 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1408 +        try {
1409 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1410 +            assertTrue(r.isEmpty());
1411 +        } catch(Exception ex) {
1412 +            unexpectedException();
1413 +        } finally {
1414 +            joinPool(e);
1415 +        }
1416 +    }
1417 +
1418 +    /**
1419 +     * timed invokeAll(c) throws NPE if c has null elements
1420 +     */
1421 +    public void testTimedInvokeAll3() {
1422 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1423 +        try {
1424 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1425 +            l.add(new StringTask());
1426 +            l.add(null);
1427 +            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1428 +        } catch (NullPointerException success) {
1429 +        } catch(Exception ex) {
1430 +            unexpectedException();
1431 +        } finally {
1432 +            joinPool(e);
1433 +        }
1434 +    }
1435 +
1436 +    /**
1437 +     * get of element of invokeAll(c) throws exception on failed task
1438 +     */
1439 +    public void testTimedInvokeAll4() {
1440 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1441 +        try {
1442 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1443 +            l.add(new NPETask());
1444 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1445 +            assertEquals(1, result.size());
1446 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1447 +                it.next().get();
1448 +        } catch(ExecutionException success) {
1449 +        } catch(Exception ex) {
1450 +            unexpectedException();
1451 +        } finally {
1452 +            joinPool(e);
1453 +        }
1454 +    }
1455 +
1456 +    /**
1457 +     * timed invokeAll(c) returns results of all completed tasks
1458 +     */
1459 +    public void testTimedInvokeAll5() {
1460 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1461 +        try {
1462 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1463 +            l.add(new StringTask());
1464 +            l.add(new StringTask());
1465 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1466 +            assertEquals(2, result.size());
1467 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1468 +                assertSame(TEST_STRING, it.next().get());
1469 +        } catch (ExecutionException success) {
1470 +        } catch(Exception ex) {
1471 +            unexpectedException();
1472 +        } finally {
1473 +            joinPool(e);
1474 +        }
1475 +    }
1476 +
1477 +    /**
1478 +     * timed invokeAll(c) cancels tasks not completed by timeout
1479 +     */
1480 +    public void testTimedInvokeAll6() {
1481 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1482 +        try {
1483 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1484 +            l.add(new StringTask());
1485 +            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1486 +            l.add(new StringTask());
1487 +            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1488 +            assertEquals(3, result.size());
1489 +            Iterator<Future<String>> it = result.iterator();
1490 +            Future<String> f1 = it.next();
1491 +            Future<String> f2 = it.next();
1492 +            Future<String> f3 = it.next();
1493 +            assertTrue(f1.isDone());
1494 +            assertTrue(f2.isDone());
1495 +            assertTrue(f3.isDone());
1496 +            assertFalse(f1.isCancelled());
1497 +            assertTrue(f2.isCancelled());
1498 +        } catch(Exception ex) {
1499 +            unexpectedException();
1500 +        } finally {
1501 +            joinPool(e);
1502 +        }
1503 +    }
1504 +
1505 +
1506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines