601 |
|
* getQueue returns the work queue, which contains queued tasks |
602 |
|
*/ |
603 |
|
public void testGetQueue() throws InterruptedException { |
604 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
605 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
606 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
606 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
607 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
607 |
– |
final CountDownLatch done = new CountDownLatch(1); |
608 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
609 |
|
for (int i = 0; i < tasks.length; i++) { |
610 |
|
Runnable r = new CheckedRunnable() { |
618 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
619 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
620 |
|
assertFalse(q.contains(tasks[0])); |
621 |
– |
done.countDown(); |
621 |
|
} |
622 |
|
} |
623 |
|
|
625 |
|
* remove(task) removes queued task, and fails to remove active task |
626 |
|
*/ |
627 |
|
public void testRemove() throws InterruptedException { |
628 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
629 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
630 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
630 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
631 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
632 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
633 |
– |
final CountDownLatch done = new CountDownLatch(1); |
633 |
|
for (int i = 0; i < tasks.length; i++) { |
634 |
|
Runnable r = new CheckedRunnable() { |
635 |
|
public void realRun() throws InterruptedException { |
649 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
650 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
651 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
653 |
– |
done.countDown(); |
652 |
|
} |
653 |
|
} |
654 |
|
|
1218 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1219 |
|
*/ |
1220 |
|
public void testTimedInvokeAll6() throws Exception { |
1221 |
< |
final ExecutorService e = new CustomExecutor(2); |
1222 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1223 |
< |
for (long timeout = timeoutMillis();;) { |
1221 |
> |
for (long timeout = timeoutMillis();;) { |
1222 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1223 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1224 |
> |
public String realCall() { |
1225 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1226 |
> |
catch (InterruptedException ok) {} |
1227 |
> |
return "1"; }}; |
1228 |
> |
final ExecutorService p = new CustomExecutor(2); |
1229 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1230 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1231 |
|
tasks.add(new StringTask("0")); |
1232 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1232 |
> |
tasks.add(waiter); |
1233 |
|
tasks.add(new StringTask("2")); |
1234 |
|
long startTime = System.nanoTime(); |
1235 |
|
List<Future<String>> futures = |
1236 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1236 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1237 |
|
assertEquals(tasks.size(), futures.size()); |
1238 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1239 |
|
for (Future future : futures) |