17 |
|
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.Future; |
22 |
|
import java.util.concurrent.FutureTask; |
1724 |
|
LONG_DELAY_MS, MILLISECONDS, |
1725 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1726 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1727 |
+ |
long startTime = System.nanoTime(); |
1728 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1729 |
|
l.add(new NPETask()); |
1730 |
|
try { |
1731 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1731 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1732 |
|
shouldThrow(); |
1733 |
|
} catch (ExecutionException success) { |
1734 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1735 |
|
} |
1736 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1737 |
|
} |
1738 |
|
} |
1739 |
|
|
1746 |
|
LONG_DELAY_MS, MILLISECONDS, |
1747 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1748 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1749 |
+ |
long startTime = System.nanoTime(); |
1750 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1751 |
|
l.add(new StringTask()); |
1752 |
|
l.add(new StringTask()); |
1753 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1753 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1754 |
|
assertSame(TEST_STRING, result); |
1755 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1756 |
|
} |
1757 |
|
} |
1758 |
|
|
1871 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1872 |
|
*/ |
1873 |
|
public void testTimedInvokeAll6() throws Exception { |
1874 |
< |
final ExecutorService e = |
1875 |
< |
new CustomTPE(2, 2, |
1876 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1877 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1878 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1879 |
< |
for (long timeout = timeoutMillis();;) { |
1874 |
> |
for (long timeout = timeoutMillis();;) { |
1875 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1876 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1877 |
> |
public String realCall() { |
1878 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1879 |
> |
catch (InterruptedException ok) {} |
1880 |
> |
return "1"; }}; |
1881 |
> |
final ExecutorService p = |
1882 |
> |
new CustomTPE(2, 2, |
1883 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1884 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1886 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1887 |
|
tasks.add(new StringTask("0")); |
1888 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1888 |
> |
tasks.add(waiter); |
1889 |
|
tasks.add(new StringTask("2")); |
1890 |
|
long startTime = System.nanoTime(); |
1891 |
|
List<Future<String>> futures = |
1892 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1892 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1893 |
|
assertEquals(tasks.size(), futures.size()); |
1894 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1895 |
|
for (Future future : futures) |