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)); |
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)); |
752 |
|
final int poolSize = 2; |
753 |
|
final int count = 5; |
754 |
|
final AtomicInteger ran = new AtomicInteger(0); |
755 |
< |
ThreadPoolExecutor p = |
755 |
> |
final ThreadPoolExecutor p = |
756 |
|
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
757 |
|
new ArrayBlockingQueue<Runnable>(10)); |
758 |
|
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
1321 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1322 |
|
*/ |
1323 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1324 |
< |
ThreadPoolExecutor p = |
1324 |
> |
final ThreadPoolExecutor p = |
1325 |
|
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1326 |
|
try { |
1327 |
|
p.setCorePoolSize(-1); |
1338 |
|
* if given a value less the core pool size |
1339 |
|
*/ |
1340 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1341 |
< |
ThreadPoolExecutor p = |
1341 |
> |
final ThreadPoolExecutor p = |
1342 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1343 |
|
try { |
1344 |
|
p.setMaximumPoolSize(1); |
1355 |
|
* if given a negative value |
1356 |
|
*/ |
1357 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1358 |
< |
ThreadPoolExecutor p = |
1358 |
> |
final ThreadPoolExecutor p = |
1359 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1360 |
|
try { |
1361 |
|
p.setMaximumPoolSize(-1); |
1372 |
|
* when given a negative value |
1373 |
|
*/ |
1374 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1375 |
< |
ThreadPoolExecutor p = |
1375 |
> |
final ThreadPoolExecutor p = |
1376 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1377 |
|
|
1378 |
|
try { |
1420 |
|
* completed submit of callable returns result |
1421 |
|
*/ |
1422 |
|
public void testSubmitCallable() throws Exception { |
1423 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1423 |
> |
final ExecutorService e = |
1424 |
> |
new CustomTPE(2, 2, |
1425 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1426 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1427 |
|
try { |
1428 |
|
Future<String> future = e.submit(new StringTask()); |
1429 |
|
String result = future.get(); |
1437 |
|
* completed submit of runnable returns successfully |
1438 |
|
*/ |
1439 |
|
public void testSubmitRunnable() throws Exception { |
1440 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1440 |
> |
final ExecutorService e = |
1441 |
> |
new CustomTPE(2, 2, |
1442 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1443 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1444 |
|
try { |
1445 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1446 |
|
future.get(); |
1454 |
|
* completed submit of (runnable, result) returns result |
1455 |
|
*/ |
1456 |
|
public void testSubmitRunnable2() throws Exception { |
1457 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1457 |
> |
final ExecutorService e = |
1458 |
> |
new CustomTPE(2, 2, |
1459 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1460 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1461 |
|
try { |
1462 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1463 |
|
String result = future.get(); |
1471 |
|
* invokeAny(null) throws NPE |
1472 |
|
*/ |
1473 |
|
public void testInvokeAny1() throws Exception { |
1474 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1474 |
> |
final ExecutorService e = |
1475 |
> |
new CustomTPE(2, 2, |
1476 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1477 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1478 |
|
try { |
1479 |
|
e.invokeAny(null); |
1480 |
|
shouldThrow(); |
1488 |
|
* invokeAny(empty collection) throws IAE |
1489 |
|
*/ |
1490 |
|
public void testInvokeAny2() throws Exception { |
1491 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1491 |
> |
final ExecutorService e = |
1492 |
> |
new CustomTPE(2, 2, |
1493 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1494 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1495 |
|
try { |
1496 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1497 |
|
shouldThrow(); |
1506 |
|
*/ |
1507 |
|
public void testInvokeAny3() throws Exception { |
1508 |
|
CountDownLatch latch = new CountDownLatch(1); |
1509 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1509 |
> |
final ExecutorService e = |
1510 |
> |
new CustomTPE(2, 2, |
1511 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1512 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1513 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1514 |
|
l.add(latchAwaitingStringTask(latch)); |
1515 |
|
l.add(null); |
1527 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1528 |
|
*/ |
1529 |
|
public void testInvokeAny4() throws Exception { |
1530 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1530 |
> |
final ExecutorService e = |
1531 |
> |
new CustomTPE(2, 2, |
1532 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1533 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1534 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1535 |
|
l.add(new NPETask()); |
1536 |
|
try { |
1547 |
|
* invokeAny(c) returns result of some task |
1548 |
|
*/ |
1549 |
|
public void testInvokeAny5() throws Exception { |
1550 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1550 |
> |
final ExecutorService e = |
1551 |
> |
new CustomTPE(2, 2, |
1552 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1553 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1554 |
|
try { |
1555 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1556 |
|
l.add(new StringTask()); |
1566 |
|
* invokeAll(null) throws NPE |
1567 |
|
*/ |
1568 |
|
public void testInvokeAll1() throws Exception { |
1569 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1569 |
> |
final ExecutorService e = |
1570 |
> |
new CustomTPE(2, 2, |
1571 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1572 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1573 |
|
try { |
1574 |
|
e.invokeAll(null); |
1575 |
|
shouldThrow(); |
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)); |
1586 |
> |
final ExecutorService e = |
1587 |
> |
new CustomTPE(2, 2, |
1588 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1589 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1590 |
|
try { |
1591 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1592 |
|
assertTrue(r.isEmpty()); |
1599 |
|
* invokeAll(c) throws NPE if c has null elements |
1600 |
|
*/ |
1601 |
|
public void testInvokeAll3() throws Exception { |
1602 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1602 |
> |
final ExecutorService e = |
1603 |
> |
new CustomTPE(2, 2, |
1604 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1605 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1606 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1607 |
|
l.add(new StringTask()); |
1608 |
|
l.add(null); |
1619 |
|
* get of element of invokeAll(c) throws exception on failed task |
1620 |
|
*/ |
1621 |
|
public void testInvokeAll4() throws Exception { |
1622 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1622 |
> |
final ExecutorService e = |
1623 |
> |
new CustomTPE(2, 2, |
1624 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1625 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1626 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1627 |
|
l.add(new NPETask()); |
1628 |
|
List<Future<String>> futures = e.invokeAll(l); |
1641 |
|
* invokeAll(c) returns results of all completed tasks |
1642 |
|
*/ |
1643 |
|
public void testInvokeAll5() throws Exception { |
1644 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1644 |
> |
final ExecutorService e = |
1645 |
> |
new CustomTPE(2, 2, |
1646 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1647 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1648 |
|
try { |
1649 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1650 |
|
l.add(new StringTask()); |
1662 |
|
* timed invokeAny(null) throws NPE |
1663 |
|
*/ |
1664 |
|
public void testTimedInvokeAny1() throws Exception { |
1665 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1665 |
> |
final ExecutorService e = |
1666 |
> |
new CustomTPE(2, 2, |
1667 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1668 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1669 |
|
try { |
1670 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1671 |
|
shouldThrow(); |
1679 |
|
* timed invokeAny(,,null) throws NPE |
1680 |
|
*/ |
1681 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1682 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1682 |
> |
final ExecutorService e = |
1683 |
> |
new CustomTPE(2, 2, |
1684 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1685 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1686 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1687 |
|
l.add(new StringTask()); |
1688 |
|
try { |
1698 |
|
* timed invokeAny(empty collection) throws IAE |
1699 |
|
*/ |
1700 |
|
public void testTimedInvokeAny2() throws Exception { |
1701 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1701 |
> |
final ExecutorService e = |
1702 |
> |
new CustomTPE(2, 2, |
1703 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1704 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1705 |
|
try { |
1706 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1707 |
|
shouldThrow(); |
1716 |
|
*/ |
1717 |
|
public void testTimedInvokeAny3() throws Exception { |
1718 |
|
CountDownLatch latch = new CountDownLatch(1); |
1719 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1719 |
> |
final ExecutorService e = |
1720 |
> |
new CustomTPE(2, 2, |
1721 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1722 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1723 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1724 |
|
l.add(latchAwaitingStringTask(latch)); |
1725 |
|
l.add(null); |
1737 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1738 |
|
*/ |
1739 |
|
public void testTimedInvokeAny4() throws Exception { |
1740 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1740 |
> |
final ExecutorService e = |
1741 |
> |
new CustomTPE(2, 2, |
1742 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1743 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1744 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1745 |
|
l.add(new NPETask()); |
1746 |
|
try { |
1757 |
|
* timed invokeAny(c) returns result of some task |
1758 |
|
*/ |
1759 |
|
public void testTimedInvokeAny5() throws Exception { |
1760 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1760 |
> |
final ExecutorService e = |
1761 |
> |
new CustomTPE(2, 2, |
1762 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1763 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1764 |
|
try { |
1765 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1766 |
|
l.add(new StringTask()); |
1776 |
|
* timed invokeAll(null) throws NPE |
1777 |
|
*/ |
1778 |
|
public void testTimedInvokeAll1() throws Exception { |
1779 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1779 |
> |
final ExecutorService e = |
1780 |
> |
new CustomTPE(2, 2, |
1781 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1782 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1783 |
|
try { |
1784 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1785 |
|
shouldThrow(); |
1793 |
|
* timed invokeAll(,,null) throws NPE |
1794 |
|
*/ |
1795 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1796 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1796 |
> |
final ExecutorService e = |
1797 |
> |
new CustomTPE(2, 2, |
1798 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1799 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1800 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1801 |
|
l.add(new StringTask()); |
1802 |
|
try { |
1812 |
|
* timed invokeAll(empty collection) returns empty collection |
1813 |
|
*/ |
1814 |
|
public void testTimedInvokeAll2() throws Exception { |
1815 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1815 |
> |
final ExecutorService e = |
1816 |
> |
new CustomTPE(2, 2, |
1817 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1818 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1819 |
|
try { |
1820 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1821 |
|
assertTrue(r.isEmpty()); |
1828 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1829 |
|
*/ |
1830 |
|
public void testTimedInvokeAll3() throws Exception { |
1831 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1831 |
> |
final ExecutorService e = |
1832 |
> |
new CustomTPE(2, 2, |
1833 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1834 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1835 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1836 |
|
l.add(new StringTask()); |
1837 |
|
l.add(null); |
1848 |
|
* get of element of invokeAll(c) throws exception on failed task |
1849 |
|
*/ |
1850 |
|
public void testTimedInvokeAll4() throws Exception { |
1851 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1851 |
> |
final ExecutorService e = |
1852 |
> |
new CustomTPE(2, 2, |
1853 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1854 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1855 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1856 |
|
l.add(new NPETask()); |
1857 |
|
List<Future<String>> futures = |
1871 |
|
* timed invokeAll(c) returns results of all completed tasks |
1872 |
|
*/ |
1873 |
|
public void testTimedInvokeAll5() throws Exception { |
1874 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1874 |
> |
final ExecutorService e = |
1875 |
> |
new CustomTPE(2, 2, |
1876 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1877 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1878 |
|
try { |
1879 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1880 |
|
l.add(new StringTask()); |
1893 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1894 |
|
*/ |
1895 |
|
public void testTimedInvokeAll6() throws Exception { |
1896 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1896 |
> |
final ExecutorService e = |
1897 |
> |
new CustomTPE(2, 2, |
1898 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1899 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1900 |
|
try { |
1901 |
|
for (long timeout = timeoutMillis();;) { |
1902 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1954 |
|
* allowsCoreThreadTimeOut is by default false. |
1955 |
|
*/ |
1956 |
|
public void testAllowsCoreThreadTimeOut() { |
1957 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1957 |
> |
final ThreadPoolExecutor p = |
1958 |
> |
new CustomTPE(2, 2, |
1959 |
> |
1000, MILLISECONDS, |
1960 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1961 |
|
assertFalse(p.allowsCoreThreadTimeOut()); |
1962 |
|
joinPool(p); |
1963 |
|
} |