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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.79 by jsr166, Sun Oct 4 03:58:22 2015 UTC vs.
Revision 1.85 by jsr166, Mon Oct 5 21:54:33 2015 UTC

# Line 272 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p =
275 >        final ThreadPoolExecutor p =
276              new CustomTPE(2, 6,
277                            LONG_DELAY_MS, MILLISECONDS,
278                            new ArrayBlockingQueue<Runnable>(10));
# Line 298 | Line 298 | public class ThreadPoolExecutorSubclassT
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p =
301 >        final ThreadPoolExecutor p =
302              new CustomTPE(2, 6,
303                            LONG_DELAY_MS, MILLISECONDS,
304                            new ArrayBlockingQueue<Runnable>(10));
# Line 545 | Line 545 | public class ThreadPoolExecutorSubclassT
545       * getTaskCount increases, but doesn't overestimate, when tasks submitted
546       */
547      public void testGetTaskCount() throws InterruptedException {
548 +        final int TASKS = 3;
549 +        final CountDownLatch done = new CountDownLatch(1);
550          final ThreadPoolExecutor p =
551              new CustomTPE(1, 1,
552                            LONG_DELAY_MS, MILLISECONDS,
553                            new ArrayBlockingQueue<Runnable>(10));
554 <        try (PoolCleaner cleaner = cleaner(p)) {
554 >        try (PoolCleaner cleaner = cleaner(p, done)) {
555              final CountDownLatch threadStarted = new CountDownLatch(1);
554            final CountDownLatch done = new CountDownLatch(1);
556              assertEquals(0, p.getTaskCount());
557 +            assertEquals(0, p.getCompletedTaskCount());
558              p.execute(new CheckedRunnable() {
559                  public void realRun() throws InterruptedException {
560                      threadStarted.countDown();
559                    assertEquals(1, p.getTaskCount());
561                      done.await();
562                  }});
563 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
563 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
564              assertEquals(1, p.getTaskCount());
565 <            done.countDown();
565 >            assertEquals(0, p.getCompletedTaskCount());
566 >            for (int i = 0; i < TASKS; i++) {
567 >                assertEquals(1 + i, p.getTaskCount());
568 >                p.execute(new CheckedRunnable() {
569 >                    public void realRun() throws InterruptedException {
570 >                        threadStarted.countDown();
571 >                        assertEquals(1 + TASKS, p.getTaskCount());
572 >                        done.await();
573 >                    }});
574 >            }
575 >            assertEquals(1 + TASKS, p.getTaskCount());
576 >            assertEquals(0, p.getCompletedTaskCount());
577          }
578 +        assertEquals(1 + TASKS, p.getTaskCount());
579 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
580      }
581  
582      /**
# Line 715 | Line 729 | public class ThreadPoolExecutorSubclassT
729              new CustomTPE(1, 1,
730                            LONG_DELAY_MS, MILLISECONDS,
731                            q);
732 <        try (PoolCleaner cleaner = cleaner(p)) {
732 >        try (PoolCleaner cleaner = cleaner(p, done)) {
733              FutureTask[] tasks = new FutureTask[5];
734              for (int i = 0; i < tasks.length; i++) {
735                  Callable task = new CheckedCallable<Boolean>() {
# Line 727 | Line 741 | public class ThreadPoolExecutorSubclassT
741                  tasks[i] = new FutureTask(task);
742                  p.execute(tasks[i]);
743              }
744 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
744 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
745              assertEquals(tasks.length, p.getTaskCount());
746              assertEquals(tasks.length - 1, q.size());
747              assertEquals(1L, p.getActiveCount());
# Line 740 | Line 754 | public class ThreadPoolExecutorSubclassT
754              p.purge();         // Nothing to do
755              assertEquals(tasks.length - 3, q.size());
756              assertEquals(tasks.length - 2, p.getTaskCount());
743            done.countDown();
757          }
758      }
759  
# Line 752 | Line 765 | public class ThreadPoolExecutorSubclassT
765          final int poolSize = 2;
766          final int count = 5;
767          final AtomicInteger ran = new AtomicInteger(0);
768 <        ThreadPoolExecutor p =
769 <            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
768 >        final ThreadPoolExecutor p =
769 >            new CustomTPE(poolSize, poolSize,
770 >                          LONG_DELAY_MS, MILLISECONDS,
771                            new ArrayBlockingQueue<Runnable>(10));
772 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
772 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
773          Runnable waiter = new CheckedRunnable() { public void realRun() {
774              threadsStarted.countDown();
775              try {
# Line 1321 | Line 1335 | public class ThreadPoolExecutorSubclassT
1335       * setCorePoolSize of negative value throws IllegalArgumentException
1336       */
1337      public void testCorePoolSizeIllegalArgumentException() {
1338 <        ThreadPoolExecutor p =
1339 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1340 <        try {
1341 <            p.setCorePoolSize(-1);
1342 <            shouldThrow();
1343 <        } catch (IllegalArgumentException success) {
1344 <        } finally {
1345 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1338 >        final ThreadPoolExecutor p =
1339 >            new CustomTPE(1, 2,
1340 >                          LONG_DELAY_MS, MILLISECONDS,
1341 >                          new ArrayBlockingQueue<Runnable>(10));
1342 >        try (PoolCleaner cleaner = cleaner(p)) {
1343 >            try {
1344 >                p.setCorePoolSize(-1);
1345 >                shouldThrow();
1346 >            } catch (IllegalArgumentException success) {}
1347          }
1333        joinPool(p);
1348      }
1349  
1350      /**
# Line 1338 | Line 1352 | public class ThreadPoolExecutorSubclassT
1352       * if given a value less the core pool size
1353       */
1354      public void testMaximumPoolSizeIllegalArgumentException() {
1355 <        ThreadPoolExecutor p =
1356 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1357 <        try {
1358 <            p.setMaximumPoolSize(1);
1359 <            shouldThrow();
1360 <        } catch (IllegalArgumentException success) {
1361 <        } finally {
1362 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1355 >        final ThreadPoolExecutor p =
1356 >            new CustomTPE(2, 3,
1357 >                          LONG_DELAY_MS, MILLISECONDS,
1358 >                          new ArrayBlockingQueue<Runnable>(10));
1359 >        try (PoolCleaner cleaner = cleaner(p)) {
1360 >            try {
1361 >                p.setMaximumPoolSize(1);
1362 >                shouldThrow();
1363 >            } catch (IllegalArgumentException success) {}
1364          }
1350        joinPool(p);
1365      }
1366  
1367      /**
# Line 1355 | Line 1369 | public class ThreadPoolExecutorSubclassT
1369       * if given a negative value
1370       */
1371      public void testMaximumPoolSizeIllegalArgumentException2() {
1372 <        ThreadPoolExecutor p =
1373 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1374 <        try {
1375 <            p.setMaximumPoolSize(-1);
1376 <            shouldThrow();
1377 <        } catch (IllegalArgumentException success) {
1378 <        } finally {
1379 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1372 >        final ThreadPoolExecutor p =
1373 >            new CustomTPE(2, 3,
1374 >                          LONG_DELAY_MS,
1375 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1376 >        try (PoolCleaner cleaner = cleaner(p)) {
1377 >            try {
1378 >                p.setMaximumPoolSize(-1);
1379 >                shouldThrow();
1380 >            } catch (IllegalArgumentException success) {}
1381          }
1367        joinPool(p);
1382      }
1383  
1384      /**
# Line 1372 | Line 1386 | public class ThreadPoolExecutorSubclassT
1386       * when given a negative value
1387       */
1388      public void testKeepAliveTimeIllegalArgumentException() {
1389 <        ThreadPoolExecutor p =
1390 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1391 <
1392 <        try {
1393 <            p.setKeepAliveTime(-1,MILLISECONDS);
1394 <            shouldThrow();
1395 <        } catch (IllegalArgumentException success) {
1396 <        } finally {
1397 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1389 >        final ThreadPoolExecutor p =
1390 >            new CustomTPE(2, 3,
1391 >                          LONG_DELAY_MS, MILLISECONDS,
1392 >                          new ArrayBlockingQueue<Runnable>(10));
1393 >        try (PoolCleaner cleaner = cleaner(p)) {
1394 >            try {
1395 >                p.setKeepAliveTime(-1, MILLISECONDS);
1396 >                shouldThrow();
1397 >            } catch (IllegalArgumentException success) {}
1398          }
1385        joinPool(p);
1399      }
1400  
1401      /**
# Line 1390 | Line 1403 | public class ThreadPoolExecutorSubclassT
1403       */
1404      public void testTerminated() {
1405          CustomTPE p = new CustomTPE();
1406 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1407 <        assertTrue(p.terminatedCalled());
1408 <        joinPool(p);
1406 >        try (PoolCleaner cleaner = cleaner(p)) {
1407 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1408 >            assertTrue(p.terminatedCalled());
1409 >            assertTrue(p.isShutdown());
1410 >        }
1411      }
1412  
1413      /**
# Line 1400 | Line 1415 | public class ThreadPoolExecutorSubclassT
1415       */
1416      public void testBeforeAfter() throws InterruptedException {
1417          CustomTPE p = new CustomTPE();
1418 <        try {
1418 >        try (PoolCleaner cleaner = cleaner(p)) {
1419              final CountDownLatch done = new CountDownLatch(1);
1420              p.execute(new CheckedRunnable() {
1421                  public void realRun() {
# Line 1410 | Line 1425 | public class ThreadPoolExecutorSubclassT
1425              assertEquals(0, done.getCount());
1426              assertTrue(p.afterCalled());
1427              assertTrue(p.beforeCalled());
1413            try { p.shutdown(); } catch (SecurityException ok) { return; }
1414        } finally {
1415            joinPool(p);
1428          }
1429      }
1430  
# Line 1420 | Line 1432 | public class ThreadPoolExecutorSubclassT
1432       * completed submit of callable returns result
1433       */
1434      public void testSubmitCallable() throws Exception {
1435 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1436 <        try {
1435 >        final ExecutorService e =
1436 >            new CustomTPE(2, 2,
1437 >                          LONG_DELAY_MS, MILLISECONDS,
1438 >                          new ArrayBlockingQueue<Runnable>(10));
1439 >        try (PoolCleaner cleaner = cleaner(e)) {
1440              Future<String> future = e.submit(new StringTask());
1441              String result = future.get();
1442              assertSame(TEST_STRING, result);
1428        } finally {
1429            joinPool(e);
1443          }
1444      }
1445  
# Line 1434 | Line 1447 | public class ThreadPoolExecutorSubclassT
1447       * completed submit of runnable returns successfully
1448       */
1449      public void testSubmitRunnable() throws Exception {
1450 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1451 <        try {
1450 >        final ExecutorService e =
1451 >            new CustomTPE(2, 2,
1452 >                          LONG_DELAY_MS, MILLISECONDS,
1453 >                          new ArrayBlockingQueue<Runnable>(10));
1454 >        try (PoolCleaner cleaner = cleaner(e)) {
1455              Future<?> future = e.submit(new NoOpRunnable());
1456              future.get();
1457              assertTrue(future.isDone());
1442        } finally {
1443            joinPool(e);
1458          }
1459      }
1460  
# Line 1448 | Line 1462 | public class ThreadPoolExecutorSubclassT
1462       * completed submit of (runnable, result) returns result
1463       */
1464      public void testSubmitRunnable2() throws Exception {
1465 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1466 <        try {
1465 >        final ExecutorService e =
1466 >            new CustomTPE(2, 2,
1467 >                          LONG_DELAY_MS, MILLISECONDS,
1468 >                          new ArrayBlockingQueue<Runnable>(10));
1469 >        try (PoolCleaner cleaner = cleaner(e)) {
1470              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1471              String result = future.get();
1472              assertSame(TEST_STRING, result);
1456        } finally {
1457            joinPool(e);
1473          }
1474      }
1475  
# Line 1462 | Line 1477 | public class ThreadPoolExecutorSubclassT
1477       * invokeAny(null) throws NPE
1478       */
1479      public void testInvokeAny1() throws Exception {
1480 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1481 <        try {
1482 <            e.invokeAny(null);
1483 <            shouldThrow();
1484 <        } catch (NullPointerException success) {
1485 <        } finally {
1486 <            joinPool(e);
1480 >        final ExecutorService e =
1481 >            new CustomTPE(2, 2,
1482 >                          LONG_DELAY_MS, MILLISECONDS,
1483 >                          new ArrayBlockingQueue<Runnable>(10));
1484 >        try (PoolCleaner cleaner = cleaner(e)) {
1485 >            try {
1486 >                e.invokeAny(null);
1487 >                shouldThrow();
1488 >            } catch (NullPointerException success) {}
1489          }
1490      }
1491  
# Line 1476 | Line 1493 | public class ThreadPoolExecutorSubclassT
1493       * invokeAny(empty collection) throws IAE
1494       */
1495      public void testInvokeAny2() throws Exception {
1496 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1497 <        try {
1498 <            e.invokeAny(new ArrayList<Callable<String>>());
1499 <            shouldThrow();
1500 <        } catch (IllegalArgumentException success) {
1501 <        } finally {
1502 <            joinPool(e);
1496 >        final ExecutorService e =
1497 >            new CustomTPE(2, 2,
1498 >                          LONG_DELAY_MS, MILLISECONDS,
1499 >                          new ArrayBlockingQueue<Runnable>(10));
1500 >        try (PoolCleaner cleaner = cleaner(e)) {
1501 >            try {
1502 >                e.invokeAny(new ArrayList<Callable<String>>());
1503 >                shouldThrow();
1504 >            } catch (IllegalArgumentException success) {}
1505          }
1506      }
1507  
# Line 1491 | Line 1510 | public class ThreadPoolExecutorSubclassT
1510       */
1511      public void testInvokeAny3() throws Exception {
1512          CountDownLatch latch = new CountDownLatch(1);
1513 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1514 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1515 <        l.add(latchAwaitingStringTask(latch));
1516 <        l.add(null);
1517 <        try {
1518 <            e.invokeAny(l);
1519 <            shouldThrow();
1520 <        } catch (NullPointerException success) {
1521 <        } finally {
1513 >        final ExecutorService e =
1514 >            new CustomTPE(2, 2,
1515 >                          LONG_DELAY_MS, MILLISECONDS,
1516 >                          new ArrayBlockingQueue<Runnable>(10));
1517 >        try (PoolCleaner cleaner = cleaner(e)) {
1518 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1519 >            l.add(latchAwaitingStringTask(latch));
1520 >            l.add(null);
1521 >            try {
1522 >                e.invokeAny(l);
1523 >                shouldThrow();
1524 >            } catch (NullPointerException success) {}
1525              latch.countDown();
1504            joinPool(e);
1526          }
1527      }
1528  
# Line 1509 | Line 1530 | public class ThreadPoolExecutorSubclassT
1530       * invokeAny(c) throws ExecutionException if no task completes
1531       */
1532      public void testInvokeAny4() throws Exception {
1533 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1534 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1535 <        l.add(new NPETask());
1536 <        try {
1537 <            e.invokeAny(l);
1538 <            shouldThrow();
1539 <        } catch (ExecutionException success) {
1540 <            assertTrue(success.getCause() instanceof NullPointerException);
1541 <        } finally {
1542 <            joinPool(e);
1533 >        final ExecutorService e =
1534 >            new CustomTPE(2, 2,
1535 >                          LONG_DELAY_MS, MILLISECONDS,
1536 >                          new ArrayBlockingQueue<Runnable>(10));
1537 >        try (PoolCleaner cleaner = cleaner(e)) {
1538 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1539 >            l.add(new NPETask());
1540 >            try {
1541 >                e.invokeAny(l);
1542 >                shouldThrow();
1543 >            } catch (ExecutionException success) {
1544 >                assertTrue(success.getCause() instanceof NullPointerException);
1545 >            }
1546          }
1547      }
1548  
# Line 1526 | Line 1550 | public class ThreadPoolExecutorSubclassT
1550       * invokeAny(c) returns result of some task
1551       */
1552      public void testInvokeAny5() throws Exception {
1553 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1554 <        try {
1553 >        final ExecutorService e =
1554 >            new CustomTPE(2, 2,
1555 >                          LONG_DELAY_MS, MILLISECONDS,
1556 >                          new ArrayBlockingQueue<Runnable>(10));
1557 >        try (PoolCleaner cleaner = cleaner(e)) {
1558              List<Callable<String>> l = new ArrayList<Callable<String>>();
1559              l.add(new StringTask());
1560              l.add(new StringTask());
1561              String result = e.invokeAny(l);
1562              assertSame(TEST_STRING, result);
1536        } finally {
1537            joinPool(e);
1563          }
1564      }
1565  
# Line 1542 | Line 1567 | public class ThreadPoolExecutorSubclassT
1567       * invokeAll(null) throws NPE
1568       */
1569      public void testInvokeAll1() throws Exception {
1570 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1571 <        try {
1572 <            e.invokeAll(null);
1573 <            shouldThrow();
1574 <        } catch (NullPointerException success) {
1575 <        } finally {
1576 <            joinPool(e);
1570 >        final ExecutorService e =
1571 >            new CustomTPE(2, 2,
1572 >                          LONG_DELAY_MS, MILLISECONDS,
1573 >                          new ArrayBlockingQueue<Runnable>(10));
1574 >        try (PoolCleaner cleaner = cleaner(e)) {
1575 >            try {
1576 >                e.invokeAll(null);
1577 >                shouldThrow();
1578 >            } catch (NullPointerException success) {}
1579          }
1580      }
1581  
# Line 1556 | Line 1583 | public class ThreadPoolExecutorSubclassT
1583       * invokeAll(empty collection) returns empty collection
1584       */
1585      public void testInvokeAll2() throws Exception {
1586 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1587 <        try {
1586 >        final ExecutorService e =
1587 >            new CustomTPE(2, 2,
1588 >                          LONG_DELAY_MS, MILLISECONDS,
1589 >                          new ArrayBlockingQueue<Runnable>(10));
1590 >        try (PoolCleaner cleaner = cleaner(e)) {
1591              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1592              assertTrue(r.isEmpty());
1563        } finally {
1564            joinPool(e);
1593          }
1594      }
1595  
# Line 1569 | Line 1597 | public class ThreadPoolExecutorSubclassT
1597       * invokeAll(c) throws NPE if c has null elements
1598       */
1599      public void testInvokeAll3() throws Exception {
1600 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1601 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1602 <        l.add(new StringTask());
1603 <        l.add(null);
1604 <        try {
1605 <            e.invokeAll(l);
1606 <            shouldThrow();
1607 <        } catch (NullPointerException success) {
1608 <        } finally {
1609 <            joinPool(e);
1600 >        final ExecutorService e =
1601 >            new CustomTPE(2, 2,
1602 >                          LONG_DELAY_MS, MILLISECONDS,
1603 >                          new ArrayBlockingQueue<Runnable>(10));
1604 >        try (PoolCleaner cleaner = cleaner(e)) {
1605 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1606 >            l.add(new StringTask());
1607 >            l.add(null);
1608 >            try {
1609 >                e.invokeAll(l);
1610 >                shouldThrow();
1611 >            } catch (NullPointerException success) {}
1612          }
1613      }
1614  
# Line 1586 | Line 1616 | public class ThreadPoolExecutorSubclassT
1616       * get of element of invokeAll(c) throws exception on failed task
1617       */
1618      public void testInvokeAll4() throws Exception {
1619 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1620 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1621 <        l.add(new NPETask());
1622 <        List<Future<String>> futures = e.invokeAll(l);
1623 <        assertEquals(1, futures.size());
1624 <        try {
1625 <            futures.get(0).get();
1626 <            shouldThrow();
1627 <        } catch (ExecutionException success) {
1628 <            assertTrue(success.getCause() instanceof NullPointerException);
1629 <        } finally {
1630 <            joinPool(e);
1619 >        final ExecutorService e =
1620 >            new CustomTPE(2, 2,
1621 >                          LONG_DELAY_MS, MILLISECONDS,
1622 >                          new ArrayBlockingQueue<Runnable>(10));
1623 >        try (PoolCleaner cleaner = cleaner(e)) {
1624 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1625 >            l.add(new NPETask());
1626 >            List<Future<String>> futures = e.invokeAll(l);
1627 >            assertEquals(1, futures.size());
1628 >            try {
1629 >                futures.get(0).get();
1630 >                shouldThrow();
1631 >            } catch (ExecutionException success) {
1632 >                assertTrue(success.getCause() instanceof NullPointerException);
1633 >            }
1634          }
1635      }
1636  
# Line 1605 | Line 1638 | public class ThreadPoolExecutorSubclassT
1638       * invokeAll(c) returns results of all completed tasks
1639       */
1640      public void testInvokeAll5() throws Exception {
1641 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1642 <        try {
1641 >        final ExecutorService e =
1642 >            new CustomTPE(2, 2,
1643 >                          LONG_DELAY_MS, MILLISECONDS,
1644 >                          new ArrayBlockingQueue<Runnable>(10));
1645 >        try (PoolCleaner cleaner = cleaner(e)) {
1646              List<Callable<String>> l = new ArrayList<Callable<String>>();
1647              l.add(new StringTask());
1648              l.add(new StringTask());
# Line 1614 | Line 1650 | public class ThreadPoolExecutorSubclassT
1650              assertEquals(2, futures.size());
1651              for (Future<String> future : futures)
1652                  assertSame(TEST_STRING, future.get());
1617        } finally {
1618            joinPool(e);
1653          }
1654      }
1655  
# Line 1623 | Line 1657 | public class ThreadPoolExecutorSubclassT
1657       * timed invokeAny(null) throws NPE
1658       */
1659      public void testTimedInvokeAny1() throws Exception {
1660 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1661 <        try {
1662 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1663 <            shouldThrow();
1664 <        } catch (NullPointerException success) {
1665 <        } finally {
1666 <            joinPool(e);
1660 >        final ExecutorService e =
1661 >            new CustomTPE(2, 2,
1662 >                          LONG_DELAY_MS, MILLISECONDS,
1663 >                          new ArrayBlockingQueue<Runnable>(10));
1664 >        try (PoolCleaner cleaner = cleaner(e)) {
1665 >            try {
1666 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1667 >                shouldThrow();
1668 >            } catch (NullPointerException success) {}
1669          }
1670      }
1671  
# Line 1637 | Line 1673 | public class ThreadPoolExecutorSubclassT
1673       * timed invokeAny(,,null) throws NPE
1674       */
1675      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1676 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1677 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1678 <        l.add(new StringTask());
1679 <        try {
1680 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1681 <            shouldThrow();
1682 <        } catch (NullPointerException success) {
1683 <        } finally {
1684 <            joinPool(e);
1676 >        final ExecutorService e =
1677 >            new CustomTPE(2, 2,
1678 >                          LONG_DELAY_MS, MILLISECONDS,
1679 >                          new ArrayBlockingQueue<Runnable>(10));
1680 >        try (PoolCleaner cleaner = cleaner(e)) {
1681 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1682 >            l.add(new StringTask());
1683 >            try {
1684 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1685 >                shouldThrow();
1686 >            } catch (NullPointerException success) {}
1687          }
1688      }
1689  
# Line 1653 | Line 1691 | public class ThreadPoolExecutorSubclassT
1691       * timed invokeAny(empty collection) throws IAE
1692       */
1693      public void testTimedInvokeAny2() throws Exception {
1694 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1695 <        try {
1696 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1697 <            shouldThrow();
1698 <        } catch (IllegalArgumentException success) {
1699 <        } finally {
1700 <            joinPool(e);
1694 >        final ExecutorService e =
1695 >            new CustomTPE(2, 2,
1696 >                          LONG_DELAY_MS, MILLISECONDS,
1697 >                          new ArrayBlockingQueue<Runnable>(10));
1698 >        try (PoolCleaner cleaner = cleaner(e)) {
1699 >            try {
1700 >                e.invokeAny(new ArrayList<Callable<String>>(),
1701 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1702 >                shouldThrow();
1703 >            } catch (IllegalArgumentException success) {}
1704          }
1705      }
1706  
# Line 1668 | Line 1709 | public class ThreadPoolExecutorSubclassT
1709       */
1710      public void testTimedInvokeAny3() throws Exception {
1711          CountDownLatch latch = new CountDownLatch(1);
1712 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1713 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1714 <        l.add(latchAwaitingStringTask(latch));
1715 <        l.add(null);
1716 <        try {
1717 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1718 <            shouldThrow();
1719 <        } catch (NullPointerException success) {
1720 <        } finally {
1712 >        final ExecutorService e =
1713 >            new CustomTPE(2, 2,
1714 >                          LONG_DELAY_MS, MILLISECONDS,
1715 >                          new ArrayBlockingQueue<Runnable>(10));
1716 >        try (PoolCleaner cleaner = cleaner(e)) {
1717 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1718 >            l.add(latchAwaitingStringTask(latch));
1719 >            l.add(null);
1720 >            try {
1721 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1722 >                shouldThrow();
1723 >            } catch (NullPointerException success) {}
1724              latch.countDown();
1681            joinPool(e);
1725          }
1726      }
1727  
# Line 1686 | Line 1729 | public class ThreadPoolExecutorSubclassT
1729       * timed invokeAny(c) throws ExecutionException if no task completes
1730       */
1731      public void testTimedInvokeAny4() throws Exception {
1732 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1733 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1734 <        l.add(new NPETask());
1735 <        try {
1736 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1737 <            shouldThrow();
1738 <        } catch (ExecutionException success) {
1739 <            assertTrue(success.getCause() instanceof NullPointerException);
1740 <        } finally {
1741 <            joinPool(e);
1732 >        final ExecutorService e =
1733 >            new CustomTPE(2, 2,
1734 >                          LONG_DELAY_MS, MILLISECONDS,
1735 >                          new ArrayBlockingQueue<Runnable>(10));
1736 >        try (PoolCleaner cleaner = cleaner(e)) {
1737 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1738 >            l.add(new NPETask());
1739 >            try {
1740 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1741 >                shouldThrow();
1742 >            } catch (ExecutionException success) {
1743 >                assertTrue(success.getCause() instanceof NullPointerException);
1744 >            }
1745          }
1746      }
1747  
# Line 1703 | Line 1749 | public class ThreadPoolExecutorSubclassT
1749       * timed invokeAny(c) returns result of some task
1750       */
1751      public void testTimedInvokeAny5() throws Exception {
1752 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1753 <        try {
1752 >        final ExecutorService e =
1753 >            new CustomTPE(2, 2,
1754 >                          LONG_DELAY_MS, MILLISECONDS,
1755 >                          new ArrayBlockingQueue<Runnable>(10));
1756 >        try (PoolCleaner cleaner = cleaner(e)) {
1757              List<Callable<String>> l = new ArrayList<Callable<String>>();
1758              l.add(new StringTask());
1759              l.add(new StringTask());
1760              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1761              assertSame(TEST_STRING, result);
1713        } finally {
1714            joinPool(e);
1762          }
1763      }
1764  
# Line 1719 | Line 1766 | public class ThreadPoolExecutorSubclassT
1766       * timed invokeAll(null) throws NPE
1767       */
1768      public void testTimedInvokeAll1() throws Exception {
1769 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1770 <        try {
1771 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1772 <            shouldThrow();
1773 <        } catch (NullPointerException success) {
1774 <        } finally {
1775 <            joinPool(e);
1769 >        final ExecutorService e =
1770 >            new CustomTPE(2, 2,
1771 >                          LONG_DELAY_MS, MILLISECONDS,
1772 >                          new ArrayBlockingQueue<Runnable>(10));
1773 >        try (PoolCleaner cleaner = cleaner(e)) {
1774 >            try {
1775 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1776 >                shouldThrow();
1777 >            } catch (NullPointerException success) {}
1778          }
1779      }
1780  
# Line 1733 | Line 1782 | public class ThreadPoolExecutorSubclassT
1782       * timed invokeAll(,,null) throws NPE
1783       */
1784      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1785 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1786 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1787 <        l.add(new StringTask());
1788 <        try {
1789 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1790 <            shouldThrow();
1791 <        } catch (NullPointerException success) {
1792 <        } finally {
1793 <            joinPool(e);
1785 >        final ExecutorService e =
1786 >            new CustomTPE(2, 2,
1787 >                          LONG_DELAY_MS, MILLISECONDS,
1788 >                          new ArrayBlockingQueue<Runnable>(10));
1789 >        try (PoolCleaner cleaner = cleaner(e)) {
1790 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1791 >            l.add(new StringTask());
1792 >            try {
1793 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1794 >                shouldThrow();
1795 >            } catch (NullPointerException success) {}
1796          }
1797      }
1798  
# Line 1749 | Line 1800 | public class ThreadPoolExecutorSubclassT
1800       * timed invokeAll(empty collection) returns empty collection
1801       */
1802      public void testTimedInvokeAll2() throws Exception {
1803 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1804 <        try {
1805 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1803 >        final ExecutorService e =
1804 >            new CustomTPE(2, 2,
1805 >                          LONG_DELAY_MS, MILLISECONDS,
1806 >                          new ArrayBlockingQueue<Runnable>(10));
1807 >        try (PoolCleaner cleaner = cleaner(e)) {
1808 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1809 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1810              assertTrue(r.isEmpty());
1756        } finally {
1757            joinPool(e);
1811          }
1812      }
1813  
# Line 1762 | Line 1815 | public class ThreadPoolExecutorSubclassT
1815       * timed invokeAll(c) throws NPE if c has null elements
1816       */
1817      public void testTimedInvokeAll3() throws Exception {
1818 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1819 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1820 <        l.add(new StringTask());
1821 <        l.add(null);
1822 <        try {
1823 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1824 <            shouldThrow();
1825 <        } catch (NullPointerException success) {
1826 <        } finally {
1827 <            joinPool(e);
1818 >        final ExecutorService e =
1819 >            new CustomTPE(2, 2,
1820 >                          LONG_DELAY_MS, MILLISECONDS,
1821 >                          new ArrayBlockingQueue<Runnable>(10));
1822 >        try (PoolCleaner cleaner = cleaner(e)) {
1823 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1824 >            l.add(new StringTask());
1825 >            l.add(null);
1826 >            try {
1827 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1828 >                shouldThrow();
1829 >            } catch (NullPointerException success) {}
1830          }
1831      }
1832  
# Line 1779 | Line 1834 | public class ThreadPoolExecutorSubclassT
1834       * get of element of invokeAll(c) throws exception on failed task
1835       */
1836      public void testTimedInvokeAll4() throws Exception {
1837 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1839 <        l.add(new NPETask());
1840 <        List<Future<String>> futures =
1841 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1842 <        assertEquals(1, futures.size());
1843 <        try {
1844 <            futures.get(0).get();
1845 <            shouldThrow();
1846 <        } catch (ExecutionException success) {
1847 <            assertTrue(success.getCause() instanceof NullPointerException);
1848 <        } finally {
1849 <            joinPool(e);
1837 >        final ExecutorService e =
1838 >            new CustomTPE(2, 2,
1839 >                          LONG_DELAY_MS, MILLISECONDS,
1840 >                          new ArrayBlockingQueue<Runnable>(10));
1841 >        try (PoolCleaner cleaner = cleaner(e)) {
1842 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1843 >            l.add(new NPETask());
1844 >            List<Future<String>> futures =
1845 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1846 >            assertEquals(1, futures.size());
1847 >            try {
1848 >                futures.get(0).get();
1849 >                shouldThrow();
1850 >            } catch (ExecutionException success) {
1851 >                assertTrue(success.getCause() instanceof NullPointerException);
1852 >            }
1853          }
1854      }
1855  
# Line 1799 | Line 1857 | public class ThreadPoolExecutorSubclassT
1857       * timed invokeAll(c) returns results of all completed tasks
1858       */
1859      public void testTimedInvokeAll5() throws Exception {
1860 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1861 <        try {
1860 >        final ExecutorService e =
1861 >            new CustomTPE(2, 2,
1862 >                          LONG_DELAY_MS, MILLISECONDS,
1863 >                          new ArrayBlockingQueue<Runnable>(10));
1864 >        try (PoolCleaner cleaner = cleaner(e)) {
1865              List<Callable<String>> l = new ArrayList<Callable<String>>();
1866              l.add(new StringTask());
1867              l.add(new StringTask());
# Line 1809 | Line 1870 | public class ThreadPoolExecutorSubclassT
1870              assertEquals(2, futures.size());
1871              for (Future<String> future : futures)
1872                  assertSame(TEST_STRING, future.get());
1812        } finally {
1813            joinPool(e);
1873          }
1874      }
1875  
# Line 1818 | Line 1877 | public class ThreadPoolExecutorSubclassT
1877       * timed invokeAll(c) cancels tasks not completed by timeout
1878       */
1879      public void testTimedInvokeAll6() throws Exception {
1880 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1881 <        try {
1880 >        final ExecutorService e =
1881 >            new CustomTPE(2, 2,
1882 >                          LONG_DELAY_MS, MILLISECONDS,
1883 >                          new ArrayBlockingQueue<Runnable>(10));
1884 >        try (PoolCleaner cleaner = cleaner(e)) {
1885              for (long timeout = timeoutMillis();;) {
1886                  List<Callable<String>> tasks = new ArrayList<>();
1887                  tasks.add(new StringTask("0"));
# Line 1843 | Line 1905 | public class ThreadPoolExecutorSubclassT
1905                          fail("expected exactly one task to be cancelled");
1906                  }
1907              }
1846        } finally {
1847            joinPool(e);
1908          }
1909      }
1910  
# Line 1858 | Line 1918 | public class ThreadPoolExecutorSubclassT
1918                            LONG_DELAY_MS, MILLISECONDS,
1919                            new LinkedBlockingQueue<Runnable>(),
1920                            new FailingThreadFactory());
1921 <        try {
1921 >        try (PoolCleaner cleaner = cleaner(e)) {
1922              final int TASKS = 100;
1923              final CountDownLatch done = new CountDownLatch(TASKS);
1924              for (int k = 0; k < TASKS; ++k)
# Line 1867 | Line 1927 | public class ThreadPoolExecutorSubclassT
1927                          done.countDown();
1928                      }});
1929              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1870        } finally {
1871            joinPool(e);
1930          }
1931      }
1932  
# Line 1876 | Line 1934 | public class ThreadPoolExecutorSubclassT
1934       * allowsCoreThreadTimeOut is by default false.
1935       */
1936      public void testAllowsCoreThreadTimeOut() {
1937 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1938 <        assertFalse(p.allowsCoreThreadTimeOut());
1939 <        joinPool(p);
1937 >        final ThreadPoolExecutor p =
1938 >            new CustomTPE(2, 2,
1939 >                          1000, MILLISECONDS,
1940 >                          new ArrayBlockingQueue<Runnable>(10));
1941 >        try (PoolCleaner cleaner = cleaner(p)) {
1942 >            assertFalse(p.allowsCoreThreadTimeOut());
1943 >        }
1944      }
1945  
1946      /**
# Line 1890 | Line 1952 | public class ThreadPoolExecutorSubclassT
1952              new CustomTPE(2, 10,
1953                            keepAliveTime, MILLISECONDS,
1954                            new ArrayBlockingQueue<Runnable>(10));
1955 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1956 <        try {
1955 >        try (PoolCleaner cleaner = cleaner(p)) {
1956 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1957              p.allowCoreThreadTimeOut(true);
1958              p.execute(new CheckedRunnable() {
1959                  public void realRun() {
# Line 1906 | Line 1968 | public class ThreadPoolExecutorSubclassT
1968                  Thread.yield();
1969              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1970              assertEquals(0, p.getPoolSize());
1909        } finally {
1910            joinPool(p);
1971          }
1972      }
1973  
# Line 1920 | Line 1980 | public class ThreadPoolExecutorSubclassT
1980              new CustomTPE(2, 10,
1981                            keepAliveTime, MILLISECONDS,
1982                            new ArrayBlockingQueue<Runnable>(10));
1983 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1984 <        try {
1983 >        try (PoolCleaner cleaner = cleaner(p)) {
1984 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1985              p.allowCoreThreadTimeOut(false);
1986              p.execute(new CheckedRunnable() {
1987                  public void realRun() throws InterruptedException {
# Line 1930 | Line 1990 | public class ThreadPoolExecutorSubclassT
1990                  }});
1991              delay(2 * keepAliveTime);
1992              assertTrue(p.getPoolSize() >= 1);
1933        } finally {
1934            joinPool(p);
1993          }
1994      }
1995  
# Line 1944 | Line 2002 | public class ThreadPoolExecutorSubclassT
2002              new CustomTPE(1, 1,
2003                            LONG_DELAY_MS, MILLISECONDS,
2004                            new LinkedBlockingQueue<Runnable>());
2005 <        try {
2005 >        try (PoolCleaner cleaner = cleaner(e)) {
2006              final CountDownLatch blockerStarted = new CountDownLatch(1);
2007              final CountDownLatch done = new CountDownLatch(1);
2008              final List<Future<?>> futures = new ArrayList<>();
# Line 1971 | Line 2029 | public class ThreadPoolExecutorSubclassT
2029                  assertTrue(future.isDone());
2030              }
2031              done.countDown();
1974        } finally {
1975            joinPool(e);
2032          }
2033      }
2034  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines