10 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
+ |
import java.util.Collection; |
14 |
+ |
import java.util.Collections; |
15 |
|
import java.util.List; |
16 |
|
import java.util.concurrent.ArrayBlockingQueue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
19 |
|
import java.util.concurrent.CancellationException; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
22 |
|
import java.util.concurrent.ExecutorService; |
23 |
|
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.FutureTask; |
240 |
|
final Runnable task = new CheckedRunnable() { |
241 |
|
public void realRun() { done.countDown(); }}; |
242 |
|
p.execute(task); |
243 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
> |
await(done); |
244 |
|
} |
245 |
|
} |
246 |
|
|
334 |
|
public void realRun() throws InterruptedException { |
335 |
|
threadStarted.countDown(); |
336 |
|
assertEquals(0, p.getCompletedTaskCount()); |
337 |
< |
threadProceed.await(); |
337 |
> |
await(threadProceed); |
338 |
|
threadDone.countDown(); |
339 |
|
}}); |
340 |
|
await(threadStarted); |
341 |
|
assertEquals(0, p.getCompletedTaskCount()); |
342 |
|
threadProceed.countDown(); |
343 |
< |
threadDone.await(); |
343 |
> |
await(threadDone); |
344 |
|
long startTime = System.nanoTime(); |
345 |
|
while (p.getCompletedTaskCount() != 1) { |
346 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
653 |
|
*/ |
654 |
|
public void testGetQueue() throws InterruptedException { |
655 |
|
final CountDownLatch done = new CountDownLatch(1); |
656 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
656 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
657 |
|
final ThreadPoolExecutor p = |
658 |
|
new CustomTPE(1, 1, |
659 |
|
LONG_DELAY_MS, MILLISECONDS, |
662 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
663 |
|
FutureTask[] tasks = new FutureTask[5]; |
664 |
|
for (int i = 0; i < tasks.length; i++) { |
665 |
< |
Callable task = new CheckedCallable<Boolean>() { |
665 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
666 |
|
public Boolean realCall() throws InterruptedException { |
667 |
|
threadStarted.countDown(); |
668 |
|
assertSame(q, p.getQueue()); |
685 |
|
*/ |
686 |
|
public void testRemove() throws InterruptedException { |
687 |
|
final CountDownLatch done = new CountDownLatch(1); |
688 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
688 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
689 |
|
final ThreadPoolExecutor p = |
690 |
|
new CustomTPE(1, 1, |
691 |
|
LONG_DELAY_MS, MILLISECONDS, |
720 |
|
public void testPurge() throws InterruptedException { |
721 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
722 |
|
final CountDownLatch done = new CountDownLatch(1); |
723 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
723 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
724 |
|
final ThreadPoolExecutor p = |
725 |
|
new CustomTPE(1, 1, |
726 |
|
LONG_DELAY_MS, MILLISECONDS, |
728 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
729 |
|
FutureTask[] tasks = new FutureTask[5]; |
730 |
|
for (int i = 0; i < tasks.length; i++) { |
731 |
< |
Callable task = new CheckedCallable<Boolean>() { |
731 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
732 |
|
public Boolean realCall() throws InterruptedException { |
733 |
|
threadStarted.countDown(); |
734 |
|
await(done); |
1466 |
|
} |
1467 |
|
|
1468 |
|
/** |
1469 |
< |
* invokeAny(null) throws NPE |
1469 |
> |
* invokeAny(null) throws NullPointerException |
1470 |
|
*/ |
1471 |
|
public void testInvokeAny1() throws Exception { |
1472 |
|
final ExecutorService e = |
1482 |
|
} |
1483 |
|
|
1484 |
|
/** |
1485 |
< |
* invokeAny(empty collection) throws IAE |
1485 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1486 |
|
*/ |
1487 |
|
public void testInvokeAny2() throws Exception { |
1488 |
|
final ExecutorService e = |
1507 |
|
LONG_DELAY_MS, MILLISECONDS, |
1508 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1509 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1510 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1510 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1511 |
|
l.add(latchAwaitingStringTask(latch)); |
1512 |
|
l.add(null); |
1513 |
|
try { |
1527 |
|
LONG_DELAY_MS, MILLISECONDS, |
1528 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1530 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1531 |
|
l.add(new NPETask()); |
1532 |
|
try { |
1533 |
|
e.invokeAny(l); |
1547 |
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1550 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
l.add(new StringTask()); |
1553 |
|
String result = e.invokeAny(l); |
1572 |
|
} |
1573 |
|
|
1574 |
|
/** |
1575 |
< |
* invokeAll(empty collection) returns empty collection |
1575 |
> |
* invokeAll(empty collection) returns empty list |
1576 |
|
*/ |
1577 |
|
public void testInvokeAll2() throws Exception { |
1578 |
|
final ExecutorService e = |
1579 |
|
new CustomTPE(2, 2, |
1580 |
|
LONG_DELAY_MS, MILLISECONDS, |
1581 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1582 |
+ |
final Collection<Callable<String>> emptyCollection |
1583 |
+ |
= Collections.emptyList(); |
1584 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1585 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1585 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1586 |
|
assertTrue(r.isEmpty()); |
1587 |
|
} |
1588 |
|
} |
1596 |
|
LONG_DELAY_MS, MILLISECONDS, |
1597 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1598 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1599 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1599 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1600 |
|
l.add(new StringTask()); |
1601 |
|
l.add(null); |
1602 |
|
try { |
1615 |
|
LONG_DELAY_MS, MILLISECONDS, |
1616 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1617 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1618 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1618 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1619 |
|
l.add(new NPETask()); |
1620 |
|
List<Future<String>> futures = e.invokeAll(l); |
1621 |
|
assertEquals(1, futures.size()); |
1637 |
|
LONG_DELAY_MS, MILLISECONDS, |
1638 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1639 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1640 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1640 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1641 |
|
l.add(new StringTask()); |
1642 |
|
l.add(new StringTask()); |
1643 |
|
List<Future<String>> futures = e.invokeAll(l); |
1657 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1658 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1659 |
|
try { |
1660 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1660 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1661 |
|
shouldThrow(); |
1662 |
|
} catch (NullPointerException success) {} |
1663 |
|
} |
1672 |
|
LONG_DELAY_MS, MILLISECONDS, |
1673 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1674 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1675 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1675 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1676 |
|
l.add(new StringTask()); |
1677 |
|
try { |
1678 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1678 |
> |
e.invokeAny(l, randomTimeout(), null); |
1679 |
|
shouldThrow(); |
1680 |
|
} catch (NullPointerException success) {} |
1681 |
|
} |
1682 |
|
} |
1683 |
|
|
1684 |
|
/** |
1685 |
< |
* timed invokeAny(empty collection) throws IAE |
1685 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1686 |
|
*/ |
1687 |
|
public void testTimedInvokeAny2() throws Exception { |
1688 |
|
final ExecutorService e = |
1689 |
|
new CustomTPE(2, 2, |
1690 |
|
LONG_DELAY_MS, MILLISECONDS, |
1691 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1692 |
+ |
final Collection<Callable<String>> emptyCollection |
1693 |
+ |
= Collections.emptyList(); |
1694 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1695 |
|
try { |
1696 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
1692 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1696 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1697 |
|
shouldThrow(); |
1698 |
|
} catch (IllegalArgumentException success) {} |
1699 |
|
} |
1709 |
|
LONG_DELAY_MS, MILLISECONDS, |
1710 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1711 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1712 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1712 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1713 |
|
l.add(latchAwaitingStringTask(latch)); |
1714 |
|
l.add(null); |
1715 |
|
try { |
1716 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1716 |
> |
e.invokeAny(l, randomTimeout(), MILLISECONDS); |
1717 |
|
shouldThrow(); |
1718 |
|
} catch (NullPointerException success) {} |
1719 |
|
latch.countDown(); |
1729 |
|
LONG_DELAY_MS, MILLISECONDS, |
1730 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1731 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1732 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1732 |
> |
long startTime = System.nanoTime(); |
1733 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1734 |
|
l.add(new NPETask()); |
1735 |
|
try { |
1736 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1736 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1737 |
|
shouldThrow(); |
1738 |
|
} catch (ExecutionException success) { |
1739 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1740 |
|
} |
1741 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1742 |
|
} |
1743 |
|
} |
1744 |
|
|
1751 |
|
LONG_DELAY_MS, MILLISECONDS, |
1752 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1753 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1754 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1754 |
> |
long startTime = System.nanoTime(); |
1755 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1756 |
|
l.add(new StringTask()); |
1757 |
|
l.add(new StringTask()); |
1758 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1758 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1759 |
|
assertSame(TEST_STRING, result); |
1760 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1761 |
|
} |
1762 |
|
} |
1763 |
|
|
1771 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1772 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1773 |
|
try { |
1774 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1774 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1775 |
|
shouldThrow(); |
1776 |
|
} catch (NullPointerException success) {} |
1777 |
|
} |
1786 |
|
LONG_DELAY_MS, MILLISECONDS, |
1787 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1788 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1789 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1789 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1790 |
|
l.add(new StringTask()); |
1791 |
|
try { |
1792 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1792 |
> |
e.invokeAll(l, randomTimeout(), null); |
1793 |
|
shouldThrow(); |
1794 |
|
} catch (NullPointerException success) {} |
1795 |
|
} |
1796 |
|
} |
1797 |
|
|
1798 |
|
/** |
1799 |
< |
* timed invokeAll(empty collection) returns empty collection |
1799 |
> |
* timed invokeAll(empty collection) returns empty list |
1800 |
|
*/ |
1801 |
|
public void testTimedInvokeAll2() throws Exception { |
1802 |
|
final ExecutorService e = |
1803 |
|
new CustomTPE(2, 2, |
1804 |
|
LONG_DELAY_MS, MILLISECONDS, |
1805 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1806 |
+ |
final Collection<Callable<String>> emptyCollection |
1807 |
+ |
= Collections.emptyList(); |
1808 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1809 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1810 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1809 |
> |
List<Future<String>> r = |
1810 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1811 |
|
assertTrue(r.isEmpty()); |
1812 |
|
} |
1813 |
|
} |
1821 |
|
LONG_DELAY_MS, MILLISECONDS, |
1822 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1823 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1824 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1824 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1825 |
|
l.add(new StringTask()); |
1826 |
|
l.add(null); |
1827 |
|
try { |
1828 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1828 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1829 |
|
shouldThrow(); |
1830 |
|
} catch (NullPointerException success) {} |
1831 |
|
} |
1840 |
|
LONG_DELAY_MS, MILLISECONDS, |
1841 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1842 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1843 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1843 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1844 |
|
l.add(new NPETask()); |
1845 |
|
List<Future<String>> futures = |
1846 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1863 |
|
LONG_DELAY_MS, MILLISECONDS, |
1864 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1865 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1866 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1866 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1867 |
|
l.add(new StringTask()); |
1868 |
|
l.add(new StringTask()); |
1869 |
|
List<Future<String>> futures = |
1878 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1879 |
|
*/ |
1880 |
|
public void testTimedInvokeAll6() throws Exception { |
1881 |
< |
final ExecutorService e = |
1882 |
< |
new CustomTPE(2, 2, |
1883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1884 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1886 |
< |
for (long timeout = timeoutMillis();;) { |
1881 |
> |
for (long timeout = timeoutMillis();;) { |
1882 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1883 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1884 |
> |
public String realCall() { |
1885 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1886 |
> |
catch (InterruptedException ok) {} |
1887 |
> |
return "1"; }}; |
1888 |
> |
final ExecutorService p = |
1889 |
> |
new CustomTPE(2, 2, |
1890 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1891 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1892 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1893 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1894 |
|
tasks.add(new StringTask("0")); |
1895 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1895 |
> |
tasks.add(waiter); |
1896 |
|
tasks.add(new StringTask("2")); |
1897 |
|
long startTime = System.nanoTime(); |
1898 |
|
List<Future<String>> futures = |
1899 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1899 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1900 |
|
assertEquals(tasks.size(), futures.size()); |
1901 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1902 |
|
for (Future future : futures) |
1933 |
|
public void realRun() { |
1934 |
|
done.countDown(); |
1935 |
|
}}); |
1936 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1936 |
> |
await(done); |
1937 |
|
} |
1938 |
|
} |
1939 |
|
|