1264 |
|
} |
1265 |
|
|
1266 |
|
|
1267 |
+ |
|
1268 |
+ |
/** |
1269 |
+ |
* timed invokeAny(null) throws NPE |
1270 |
+ |
*/ |
1271 |
+ |
public void testTimedInvokeAny1() { |
1272 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1273 |
+ |
try { |
1274 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1275 |
+ |
} catch (NullPointerException success) { |
1276 |
+ |
} catch(Exception ex) { |
1277 |
+ |
unexpectedException(); |
1278 |
+ |
} finally { |
1279 |
+ |
joinPool(e); |
1280 |
+ |
} |
1281 |
+ |
} |
1282 |
+ |
|
1283 |
+ |
/** |
1284 |
+ |
* timed invokeAny(,,null) throws NPE |
1285 |
+ |
*/ |
1286 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
1287 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1288 |
+ |
try { |
1289 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1290 |
+ |
l.add(new StringTask()); |
1291 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1292 |
+ |
} catch (NullPointerException success) { |
1293 |
+ |
} catch(Exception ex) { |
1294 |
+ |
unexpectedException(); |
1295 |
+ |
} finally { |
1296 |
+ |
joinPool(e); |
1297 |
+ |
} |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
/** |
1301 |
+ |
* timed invokeAny(empty collection) throws IAE |
1302 |
+ |
*/ |
1303 |
+ |
public void testTimedInvokeAny2() { |
1304 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1305 |
+ |
try { |
1306 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1307 |
+ |
} catch (IllegalArgumentException success) { |
1308 |
+ |
} catch(Exception ex) { |
1309 |
+ |
unexpectedException(); |
1310 |
+ |
} finally { |
1311 |
+ |
joinPool(e); |
1312 |
+ |
} |
1313 |
+ |
} |
1314 |
+ |
|
1315 |
+ |
/** |
1316 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1317 |
+ |
*/ |
1318 |
+ |
public void testTimedInvokeAny3() { |
1319 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1320 |
+ |
try { |
1321 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1322 |
+ |
l.add(new StringTask()); |
1323 |
+ |
l.add(null); |
1324 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1325 |
+ |
} catch (NullPointerException success) { |
1326 |
+ |
} catch(Exception ex) { |
1327 |
+ |
ex.printStackTrace(); |
1328 |
+ |
unexpectedException(); |
1329 |
+ |
} finally { |
1330 |
+ |
joinPool(e); |
1331 |
+ |
} |
1332 |
+ |
} |
1333 |
+ |
|
1334 |
+ |
/** |
1335 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
1336 |
+ |
*/ |
1337 |
+ |
public void testTimedInvokeAny4() { |
1338 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1339 |
+ |
try { |
1340 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1341 |
+ |
l.add(new NPETask()); |
1342 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1343 |
+ |
} catch(ExecutionException success) { |
1344 |
+ |
} catch(Exception ex) { |
1345 |
+ |
unexpectedException(); |
1346 |
+ |
} finally { |
1347 |
+ |
joinPool(e); |
1348 |
+ |
} |
1349 |
+ |
} |
1350 |
+ |
|
1351 |
+ |
/** |
1352 |
+ |
* timed invokeAny(c) returns result of some task |
1353 |
+ |
*/ |
1354 |
+ |
public void testTimedInvokeAny5() { |
1355 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1356 |
+ |
try { |
1357 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1358 |
+ |
l.add(new StringTask()); |
1359 |
+ |
l.add(new StringTask()); |
1360 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1361 |
+ |
assertSame(TEST_STRING, result); |
1362 |
+ |
} catch (ExecutionException success) { |
1363 |
+ |
} catch(Exception ex) { |
1364 |
+ |
unexpectedException(); |
1365 |
+ |
} finally { |
1366 |
+ |
joinPool(e); |
1367 |
+ |
} |
1368 |
+ |
} |
1369 |
+ |
|
1370 |
+ |
/** |
1371 |
+ |
* timed invokeAll(null) throws NPE |
1372 |
+ |
*/ |
1373 |
+ |
public void testTimedInvokeAll1() { |
1374 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1375 |
+ |
try { |
1376 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1377 |
+ |
} catch (NullPointerException success) { |
1378 |
+ |
} catch(Exception ex) { |
1379 |
+ |
unexpectedException(); |
1380 |
+ |
} finally { |
1381 |
+ |
joinPool(e); |
1382 |
+ |
} |
1383 |
+ |
} |
1384 |
+ |
|
1385 |
+ |
/** |
1386 |
+ |
* timed invokeAll(,,null) throws NPE |
1387 |
+ |
*/ |
1388 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1389 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1390 |
+ |
try { |
1391 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1392 |
+ |
l.add(new StringTask()); |
1393 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1394 |
+ |
} catch (NullPointerException success) { |
1395 |
+ |
} catch(Exception ex) { |
1396 |
+ |
unexpectedException(); |
1397 |
+ |
} finally { |
1398 |
+ |
joinPool(e); |
1399 |
+ |
} |
1400 |
+ |
} |
1401 |
+ |
|
1402 |
+ |
/** |
1403 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1404 |
+ |
*/ |
1405 |
+ |
public void testTimedInvokeAll2() { |
1406 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1407 |
+ |
try { |
1408 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1409 |
+ |
assertTrue(r.isEmpty()); |
1410 |
+ |
} catch(Exception ex) { |
1411 |
+ |
unexpectedException(); |
1412 |
+ |
} finally { |
1413 |
+ |
joinPool(e); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
+ |
|
1417 |
+ |
/** |
1418 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1419 |
+ |
*/ |
1420 |
+ |
public void testTimedInvokeAll3() { |
1421 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1422 |
+ |
try { |
1423 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1424 |
+ |
l.add(new StringTask()); |
1425 |
+ |
l.add(null); |
1426 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1427 |
+ |
} catch (NullPointerException success) { |
1428 |
+ |
} catch(Exception ex) { |
1429 |
+ |
unexpectedException(); |
1430 |
+ |
} finally { |
1431 |
+ |
joinPool(e); |
1432 |
+ |
} |
1433 |
+ |
} |
1434 |
+ |
|
1435 |
+ |
/** |
1436 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1437 |
+ |
*/ |
1438 |
+ |
public void testTimedInvokeAll4() { |
1439 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1440 |
+ |
try { |
1441 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1442 |
+ |
l.add(new NPETask()); |
1443 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1444 |
+ |
assertEquals(1, result.size()); |
1445 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1446 |
+ |
it.next().get(); |
1447 |
+ |
} catch(ExecutionException success) { |
1448 |
+ |
} catch(Exception ex) { |
1449 |
+ |
unexpectedException(); |
1450 |
+ |
} finally { |
1451 |
+ |
joinPool(e); |
1452 |
+ |
} |
1453 |
+ |
} |
1454 |
+ |
|
1455 |
+ |
/** |
1456 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1457 |
+ |
*/ |
1458 |
+ |
public void testTimedInvokeAll5() { |
1459 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1460 |
+ |
try { |
1461 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1462 |
+ |
l.add(new StringTask()); |
1463 |
+ |
l.add(new StringTask()); |
1464 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1465 |
+ |
assertEquals(2, result.size()); |
1466 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1467 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1468 |
+ |
} catch (ExecutionException success) { |
1469 |
+ |
} catch(Exception ex) { |
1470 |
+ |
unexpectedException(); |
1471 |
+ |
} finally { |
1472 |
+ |
joinPool(e); |
1473 |
+ |
} |
1474 |
+ |
} |
1475 |
+ |
|
1476 |
+ |
/** |
1477 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1478 |
+ |
*/ |
1479 |
+ |
public void testTimedInvokeAll6() { |
1480 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1481 |
+ |
try { |
1482 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1483 |
+ |
l.add(new StringTask()); |
1484 |
+ |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
1485 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1486 |
+ |
assertEquals(2, result.size()); |
1487 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1488 |
+ |
Future<String> f1 = it.next(); |
1489 |
+ |
Future<String> f2 = it.next(); |
1490 |
+ |
assertTrue(f1.isDone()); |
1491 |
+ |
assertFalse(f1.isCancelled()); |
1492 |
+ |
assertTrue(f2.isDone()); |
1493 |
+ |
assertTrue(f2.isCancelled()); |
1494 |
+ |
} catch(Exception ex) { |
1495 |
+ |
unexpectedException(); |
1496 |
+ |
} finally { |
1497 |
+ |
joinPool(e); |
1498 |
+ |
} |
1499 |
+ |
} |
1500 |
+ |
|
1501 |
+ |
|
1502 |
|
} |