5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
9 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
|
|
11 |
|
import java.util.ArrayList; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
|
import java.util.concurrent.Delayed; |
19 |
|
import java.util.concurrent.ExecutionException; |
19 |
– |
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.Future; |
22 |
|
import java.util.concurrent.RejectedExecutionException; |
25 |
|
import java.util.concurrent.ScheduledFuture; |
26 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
|
import java.util.concurrent.ThreadFactory; |
28 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
29 |
|
import java.util.concurrent.ThreadPoolExecutor; |
30 |
|
import java.util.concurrent.TimeoutException; |
31 |
|
import java.util.concurrent.TimeUnit; |
32 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
33 |
|
import java.util.concurrent.atomic.AtomicInteger; |
34 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
35 |
+ |
import java.util.stream.Stream; |
36 |
|
|
37 |
|
import junit.framework.Test; |
38 |
|
import junit.framework.TestSuite; |
46 |
|
} |
47 |
|
|
48 |
|
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
49 |
< |
RunnableScheduledFuture<V> task; |
49 |
> |
private final RunnableScheduledFuture<V> task; |
50 |
|
volatile boolean ran; |
51 |
< |
CustomTask(RunnableScheduledFuture<V> t) { task = t; } |
51 |
> |
CustomTask(RunnableScheduledFuture<V> task) { this.task = task; } |
52 |
|
public boolean isPeriodic() { return task.isPeriodic(); } |
53 |
|
public void run() { |
54 |
|
ran = true; |
109 |
|
final Runnable task = new CheckedRunnable() { |
110 |
|
public void realRun() { done.countDown(); }}; |
111 |
|
p.execute(task); |
112 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
112 |
> |
await(done); |
113 |
|
} |
114 |
|
} |
115 |
|
|
117 |
|
* delayed schedule of callable successfully executes after delay |
118 |
|
*/ |
119 |
|
public void testSchedule1() throws Exception { |
120 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
121 |
|
final CustomExecutor p = new CustomExecutor(1); |
122 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
122 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
123 |
|
final long startTime = System.nanoTime(); |
119 |
– |
final CountDownLatch done = new CountDownLatch(1); |
124 |
|
Callable task = new CheckedCallable<Boolean>() { |
125 |
|
public Boolean realCall() { |
126 |
|
done.countDown(); |
130 |
|
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
131 |
|
assertSame(Boolean.TRUE, f.get()); |
132 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
129 |
– |
assertTrue(done.await(0L, MILLISECONDS)); |
133 |
|
} |
134 |
|
} |
135 |
|
|
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
206 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
207 |
> |
* Eventually, it must hold that: |
208 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
209 |
|
*/ |
210 |
|
public void testFixedRateSequence() throws InterruptedException { |
211 |
|
final CustomExecutor p = new CustomExecutor(1); |
212 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
213 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
214 |
< |
long startTime = System.nanoTime(); |
215 |
< |
int cycles = 10; |
214 |
> |
final long startTime = System.nanoTime(); |
215 |
> |
final int cycles = 8; |
216 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
217 |
< |
Runnable task = new CheckedRunnable() { |
217 |
> |
final Runnable task = new CheckedRunnable() { |
218 |
|
public void realRun() { done.countDown(); }}; |
219 |
< |
ScheduledFuture h = |
219 |
> |
final ScheduledFuture periodicTask = |
220 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
221 |
< |
done.await(); |
222 |
< |
h.cancel(true); |
223 |
< |
double normalizedTime = |
224 |
< |
(double) millisElapsedSince(startTime) / delay; |
225 |
< |
if (normalizedTime >= cycles - 1 && |
226 |
< |
normalizedTime <= cycles) |
221 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
222 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
223 |
> |
periodicTask.cancel(true); |
224 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
225 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
226 |
> |
if (elapsedMillis <= cycles * delay) |
227 |
|
return; |
228 |
+ |
// else retry with longer delay |
229 |
|
} |
230 |
< |
throw new AssertionError("unexpected execution rate"); |
230 |
> |
fail("unexpected execution rate"); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
235 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
236 |
> |
* Eventually, it must hold that each task starts at least delay and at |
237 |
> |
* most 2 * delay after the termination of the previous task. |
238 |
|
*/ |
239 |
|
public void testFixedDelaySequence() throws InterruptedException { |
240 |
|
final CustomExecutor p = new CustomExecutor(1); |
241 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
242 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
243 |
< |
long startTime = System.nanoTime(); |
244 |
< |
int cycles = 10; |
243 |
> |
final long startTime = System.nanoTime(); |
244 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
245 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
246 |
> |
final int cycles = 8; |
247 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
248 |
< |
Runnable task = new CheckedRunnable() { |
249 |
< |
public void realRun() { done.countDown(); }}; |
250 |
< |
ScheduledFuture h = |
248 |
> |
final int d = delay; |
249 |
> |
final Runnable task = new CheckedRunnable() { |
250 |
> |
public void realRun() { |
251 |
> |
long now = System.nanoTime(); |
252 |
> |
long elapsedMillis |
253 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
254 |
> |
if (done.getCount() == cycles) { // first execution |
255 |
> |
if (elapsedMillis >= d) |
256 |
> |
tryLongerDelay.set(true); |
257 |
> |
} else { |
258 |
> |
assertTrue(elapsedMillis >= d); |
259 |
> |
if (elapsedMillis >= 2 * d) |
260 |
> |
tryLongerDelay.set(true); |
261 |
> |
} |
262 |
> |
previous.set(now); |
263 |
> |
done.countDown(); |
264 |
> |
}}; |
265 |
> |
final ScheduledFuture periodicTask = |
266 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
267 |
< |
done.await(); |
268 |
< |
h.cancel(true); |
269 |
< |
double normalizedTime = |
270 |
< |
(double) millisElapsedSince(startTime) / delay; |
271 |
< |
if (normalizedTime >= cycles - 1 && |
272 |
< |
normalizedTime <= cycles) |
267 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
268 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
269 |
> |
periodicTask.cancel(true); |
270 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
271 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
272 |
> |
if (!tryLongerDelay.get()) |
273 |
|
return; |
274 |
+ |
// else retry with longer delay |
275 |
|
} |
276 |
< |
throw new AssertionError("unexpected execution rate"); |
276 |
> |
fail("unexpected execution rate"); |
277 |
|
} |
278 |
|
} |
279 |
|
|
297 |
|
final CustomExecutor p = new CustomExecutor(1); |
298 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
299 |
|
try { |
300 |
< |
TrackedCallable callable = null; |
301 |
< |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
300 |
> |
Future f = p.schedule((Callable)null, |
301 |
> |
randomTimeout(), randomTimeUnit()); |
302 |
|
shouldThrow(); |
303 |
|
} catch (NullPointerException success) {} |
304 |
|
} |
389 |
|
* thread becomes active |
390 |
|
*/ |
391 |
|
public void testGetActiveCount() throws InterruptedException { |
392 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
393 |
|
final ThreadPoolExecutor p = new CustomExecutor(2); |
394 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
394 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
395 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
369 |
– |
final CountDownLatch done = new CountDownLatch(1); |
396 |
|
assertEquals(0, p.getActiveCount()); |
397 |
|
p.execute(new CheckedRunnable() { |
398 |
|
public void realRun() throws InterruptedException { |
399 |
|
threadStarted.countDown(); |
400 |
|
assertEquals(1, p.getActiveCount()); |
401 |
< |
done.await(); |
401 |
> |
await(done); |
402 |
|
}}); |
403 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
403 |
> |
await(threadStarted); |
404 |
|
assertEquals(1, p.getActiveCount()); |
379 |
– |
done.countDown(); |
405 |
|
} |
406 |
|
} |
407 |
|
|
420 |
|
public void realRun() throws InterruptedException { |
421 |
|
threadStarted.countDown(); |
422 |
|
assertEquals(0, p.getCompletedTaskCount()); |
423 |
< |
threadProceed.await(); |
423 |
> |
await(threadProceed); |
424 |
|
threadDone.countDown(); |
425 |
|
}}); |
426 |
|
await(threadStarted); |
427 |
|
assertEquals(0, p.getCompletedTaskCount()); |
428 |
|
threadProceed.countDown(); |
429 |
< |
threadDone.await(); |
429 |
> |
await(threadDone); |
430 |
|
long startTime = System.nanoTime(); |
431 |
|
while (p.getCompletedTaskCount() != 1) { |
432 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
452 |
|
*/ |
453 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
454 |
|
final int THREADS = 3; |
455 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
456 |
|
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
457 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
457 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
458 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
433 |
– |
final CountDownLatch done = new CountDownLatch(1); |
459 |
|
assertEquals(0, p.getLargestPoolSize()); |
460 |
|
for (int i = 0; i < THREADS; i++) |
461 |
|
p.execute(new CheckedRunnable() { |
462 |
|
public void realRun() throws InterruptedException { |
463 |
|
threadsStarted.countDown(); |
464 |
< |
done.await(); |
464 |
> |
await(done); |
465 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
466 |
|
}}); |
467 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
467 |
> |
await(threadsStarted); |
468 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
444 |
– |
done.countDown(); |
469 |
|
} |
470 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
471 |
|
} |
475 |
|
* become active |
476 |
|
*/ |
477 |
|
public void testGetPoolSize() throws InterruptedException { |
478 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
479 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
480 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
480 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
481 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
457 |
– |
final CountDownLatch done = new CountDownLatch(1); |
482 |
|
assertEquals(0, p.getPoolSize()); |
483 |
|
p.execute(new CheckedRunnable() { |
484 |
|
public void realRun() throws InterruptedException { |
485 |
|
threadStarted.countDown(); |
486 |
|
assertEquals(1, p.getPoolSize()); |
487 |
< |
done.await(); |
487 |
> |
await(done); |
488 |
|
}}); |
489 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
489 |
> |
await(threadStarted); |
490 |
|
assertEquals(1, p.getPoolSize()); |
467 |
– |
done.countDown(); |
491 |
|
} |
492 |
|
} |
493 |
|
|
496 |
|
* submitted |
497 |
|
*/ |
498 |
|
public void testGetTaskCount() throws InterruptedException { |
499 |
+ |
final int TASKS = 3; |
500 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
501 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
502 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
502 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
503 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
479 |
– |
final CountDownLatch done = new CountDownLatch(1); |
480 |
– |
final int TASKS = 5; |
504 |
|
assertEquals(0, p.getTaskCount()); |
505 |
< |
for (int i = 0; i < TASKS; i++) |
505 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
506 |
> |
p.execute(new CheckedRunnable() { |
507 |
> |
public void realRun() throws InterruptedException { |
508 |
> |
threadStarted.countDown(); |
509 |
> |
await(done); |
510 |
> |
}}); |
511 |
> |
await(threadStarted); |
512 |
> |
assertEquals(1, p.getTaskCount()); |
513 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
514 |
> |
for (int i = 0; i < TASKS; i++) { |
515 |
> |
assertEquals(1 + i, p.getTaskCount()); |
516 |
|
p.execute(new CheckedRunnable() { |
517 |
|
public void realRun() throws InterruptedException { |
518 |
|
threadStarted.countDown(); |
519 |
< |
done.await(); |
519 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
520 |
> |
await(done); |
521 |
|
}}); |
522 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
523 |
< |
assertEquals(TASKS, p.getTaskCount()); |
524 |
< |
done.countDown(); |
522 |
> |
} |
523 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
524 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
525 |
|
} |
526 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
527 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
579 |
|
* isTerminated is false before termination, true after |
580 |
|
*/ |
581 |
|
public void testIsTerminated() throws InterruptedException { |
582 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
583 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
584 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
585 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
549 |
– |
final CountDownLatch done = new CountDownLatch(1); |
550 |
– |
assertFalse(p.isTerminated()); |
586 |
|
p.execute(new CheckedRunnable() { |
587 |
|
public void realRun() throws InterruptedException { |
588 |
|
assertFalse(p.isTerminated()); |
589 |
|
threadStarted.countDown(); |
590 |
< |
done.await(); |
590 |
> |
await(done); |
591 |
|
}}); |
592 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
592 |
> |
await(threadStarted); |
593 |
> |
assertFalse(p.isTerminated()); |
594 |
|
assertFalse(p.isTerminating()); |
595 |
|
done.countDown(); |
596 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
603 |
|
* isTerminating is not true when running or when terminated |
604 |
|
*/ |
605 |
|
public void testIsTerminating() throws InterruptedException { |
606 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
607 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
608 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
609 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
573 |
– |
final CountDownLatch done = new CountDownLatch(1); |
610 |
|
assertFalse(p.isTerminating()); |
611 |
|
p.execute(new CheckedRunnable() { |
612 |
|
public void realRun() throws InterruptedException { |
613 |
|
assertFalse(p.isTerminating()); |
614 |
|
threadStarted.countDown(); |
615 |
< |
done.await(); |
615 |
> |
await(done); |
616 |
|
}}); |
617 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
617 |
> |
await(threadStarted); |
618 |
|
assertFalse(p.isTerminating()); |
619 |
|
done.countDown(); |
620 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
628 |
|
* getQueue returns the work queue, which contains queued tasks |
629 |
|
*/ |
630 |
|
public void testGetQueue() throws InterruptedException { |
631 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
632 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
633 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
633 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
634 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
598 |
– |
final CountDownLatch done = new CountDownLatch(1); |
635 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
636 |
|
for (int i = 0; i < tasks.length; i++) { |
637 |
|
Runnable r = new CheckedRunnable() { |
638 |
|
public void realRun() throws InterruptedException { |
639 |
|
threadStarted.countDown(); |
640 |
< |
done.await(); |
640 |
> |
await(done); |
641 |
|
}}; |
642 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
643 |
|
} |
644 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
644 |
> |
await(threadStarted); |
645 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
646 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
647 |
|
assertFalse(q.contains(tasks[0])); |
612 |
– |
done.countDown(); |
648 |
|
} |
649 |
|
} |
650 |
|
|
652 |
|
* remove(task) removes queued task, and fails to remove active task |
653 |
|
*/ |
654 |
|
public void testRemove() throws InterruptedException { |
655 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
656 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
657 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
657 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
658 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
659 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
624 |
– |
final CountDownLatch done = new CountDownLatch(1); |
660 |
|
for (int i = 0; i < tasks.length; i++) { |
661 |
|
Runnable r = new CheckedRunnable() { |
662 |
|
public void realRun() throws InterruptedException { |
663 |
|
threadStarted.countDown(); |
664 |
< |
done.await(); |
664 |
> |
await(done); |
665 |
|
}}; |
666 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
667 |
|
} |
668 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
668 |
> |
await(threadStarted); |
669 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
670 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
671 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
676 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
677 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
678 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
644 |
– |
done.countDown(); |
679 |
|
} |
680 |
|
} |
681 |
|
|
683 |
|
* purge removes cancelled tasks from the queue |
684 |
|
*/ |
685 |
|
public void testPurge() throws InterruptedException { |
686 |
+ |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
687 |
+ |
final Runnable releaser = new Runnable() { public void run() { |
688 |
+ |
for (ScheduledFuture task : tasks) |
689 |
+ |
if (task != null) task.cancel(true); }}; |
690 |
|
final CustomExecutor p = new CustomExecutor(1); |
691 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
692 |
< |
for (int i = 0; i < tasks.length; i++) |
693 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
694 |
< |
LONG_DELAY_MS, MILLISECONDS); |
657 |
< |
try { |
691 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
692 |
> |
for (int i = 0; i < tasks.length; i++) |
693 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
694 |
> |
LONG_DELAY_MS, MILLISECONDS); |
695 |
|
int max = tasks.length; |
696 |
|
if (tasks[4].cancel(true)) --max; |
697 |
|
if (tasks[3].cancel(true)) --max; |
703 |
|
long count = p.getTaskCount(); |
704 |
|
if (count == max) |
705 |
|
return; |
706 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
706 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
707 |
|
fail("Purge failed to remove cancelled tasks"); |
671 |
– |
} finally { |
672 |
– |
for (ScheduledFuture task : tasks) |
673 |
– |
task.cancel(true); |
674 |
– |
joinPool(p); |
708 |
|
} |
709 |
|
} |
710 |
|
|
727 |
|
}}; |
728 |
|
for (int i = 0; i < count; i++) |
729 |
|
p.execute(waiter); |
730 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
730 |
> |
await(threadsStarted); |
731 |
|
assertEquals(poolSize, p.getActiveCount()); |
732 |
|
assertEquals(0, p.getCompletedTaskCount()); |
733 |
|
final List<Runnable> queuedTasks; |
788 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
789 |
|
*/ |
790 |
|
public void testShutdown_cancellation() throws Exception { |
791 |
< |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
759 |
< |
for (Boolean policy : allBooleans) |
760 |
< |
{ |
761 |
< |
final int poolSize = 2; |
791 |
> |
final int poolSize = 4; |
792 |
|
final CustomExecutor p = new CustomExecutor(poolSize); |
793 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
794 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
795 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
796 |
< |
if (policy != null) { |
797 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
798 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
799 |
< |
p.setRemoveOnCancelPolicy(policy); |
800 |
< |
} |
793 |
> |
final BlockingQueue<Runnable> q = p.getQueue(); |
794 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
795 |
> |
final long delay = rnd.nextInt(2); |
796 |
> |
final int rounds = rnd.nextInt(1, 3); |
797 |
> |
final boolean effectiveDelayedPolicy; |
798 |
> |
final boolean effectivePeriodicPolicy; |
799 |
> |
final boolean effectiveRemovePolicy; |
800 |
> |
|
801 |
> |
if (rnd.nextBoolean()) |
802 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
803 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
804 |
> |
else |
805 |
> |
effectiveDelayedPolicy = true; |
806 |
|
assertEquals(effectiveDelayedPolicy, |
807 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
808 |
+ |
|
809 |
+ |
if (rnd.nextBoolean()) |
810 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
811 |
+ |
effectivePeriodicPolicy = rnd.nextBoolean()); |
812 |
+ |
else |
813 |
+ |
effectivePeriodicPolicy = false; |
814 |
|
assertEquals(effectivePeriodicPolicy, |
815 |
|
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
816 |
+ |
|
817 |
+ |
if (rnd.nextBoolean()) |
818 |
+ |
p.setRemoveOnCancelPolicy( |
819 |
+ |
effectiveRemovePolicy = rnd.nextBoolean()); |
820 |
+ |
else |
821 |
+ |
effectiveRemovePolicy = false; |
822 |
|
assertEquals(effectiveRemovePolicy, |
823 |
|
p.getRemoveOnCancelPolicy()); |
824 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
824 |
> |
|
825 |
> |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
826 |
> |
|
827 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
828 |
> |
// then add a second wave that waits in the queue until unblocked. |
829 |
|
final AtomicInteger ran = new AtomicInteger(0); |
830 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
831 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
832 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
782 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
783 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
784 |
< |
throws InterruptedException { |
785 |
< |
poolBlocked.countDown(); |
786 |
< |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
787 |
< |
ran.getAndIncrement(); |
788 |
< |
}}; |
789 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
790 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
791 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
792 |
< |
for (int i = 0; i < poolSize; i++) |
793 |
< |
blockers.add(p.submit(task)); |
794 |
< |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
795 |
< |
|
796 |
< |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
797 |
< |
1, 1, MILLISECONDS)); |
798 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
799 |
< |
1, 1, MILLISECONDS)); |
800 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
832 |
> |
final RuntimeException exception = new RuntimeException(); |
833 |
|
|
834 |
< |
assertTrue(p.getQueue().containsAll(periodics)); |
835 |
< |
assertTrue(p.getQueue().containsAll(delayeds)); |
836 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
837 |
< |
assertTrue(p.isShutdown()); |
838 |
< |
assertFalse(p.isTerminated()); |
839 |
< |
for (Future<?> periodic : periodics) { |
840 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
809 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
810 |
< |
} |
811 |
< |
for (Future<?> delayed : delayeds) { |
812 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
813 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
814 |
< |
} |
815 |
< |
if (testImplementationDetails) { |
816 |
< |
assertEquals(effectivePeriodicPolicy, |
817 |
< |
p.getQueue().containsAll(periodics)); |
818 |
< |
assertEquals(effectiveDelayedPolicy, |
819 |
< |
p.getQueue().containsAll(delayeds)); |
820 |
< |
} |
821 |
< |
// Release all pool threads |
822 |
< |
unblock.countDown(); |
823 |
< |
|
824 |
< |
for (Future<?> delayed : delayeds) { |
825 |
< |
if (effectiveDelayedPolicy) { |
826 |
< |
assertNull(delayed.get()); |
834 |
> |
class Task implements Runnable { |
835 |
> |
public void run() { |
836 |
> |
try { |
837 |
> |
ran.getAndIncrement(); |
838 |
> |
poolBlocked.countDown(); |
839 |
> |
await(unblock); |
840 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
841 |
|
} |
842 |
|
} |
843 |
< |
if (effectivePeriodicPolicy) { |
844 |
< |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
845 |
< |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
846 |
< |
for (Future<?> periodic : periodics) { |
847 |
< |
assertTrue(periodic.cancel(false)); |
848 |
< |
assertTrue(periodic.isCancelled()); |
849 |
< |
assertTrue(periodic.isDone()); |
843 |
> |
|
844 |
> |
class PeriodicTask extends Task { |
845 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
846 |
> |
int rounds; |
847 |
> |
public void run() { |
848 |
> |
if (--rounds == 0) super.run(); |
849 |
> |
// throw exception to surely terminate this periodic task, |
850 |
> |
// but in a separate execution and in a detectable way. |
851 |
> |
if (rounds == -1) throw exception; |
852 |
|
} |
853 |
|
} |
854 |
+ |
|
855 |
+ |
Runnable task = new Task(); |
856 |
+ |
|
857 |
+ |
List<Future<?>> immediates = new ArrayList<>(); |
858 |
+ |
List<Future<?>> delayeds = new ArrayList<>(); |
859 |
+ |
List<Future<?>> periodics = new ArrayList<>(); |
860 |
+ |
|
861 |
+ |
immediates.add(p.submit(task)); |
862 |
+ |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
863 |
+ |
periodics.add(p.scheduleAtFixedRate( |
864 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
865 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
866 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
867 |
+ |
|
868 |
+ |
await(poolBlocked); |
869 |
+ |
|
870 |
+ |
assertEquals(poolSize, ran.get()); |
871 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
872 |
+ |
assertTrue(q.isEmpty()); |
873 |
+ |
|
874 |
+ |
// Add second wave of tasks. |
875 |
+ |
immediates.add(p.submit(task)); |
876 |
+ |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
877 |
+ |
periodics.add(p.scheduleAtFixedRate( |
878 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
879 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
880 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
881 |
+ |
|
882 |
+ |
assertEquals(poolSize, q.size()); |
883 |
+ |
assertEquals(poolSize, ran.get()); |
884 |
+ |
|
885 |
+ |
immediates.forEach( |
886 |
+ |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
887 |
+ |
|
888 |
+ |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
889 |
+ |
.forEach(f -> assertFalse(f.isDone())); |
890 |
+ |
|
891 |
+ |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
892 |
+ |
assertTrue(p.isShutdown()); |
893 |
+ |
assertTrue(p.isTerminating()); |
894 |
+ |
assertFalse(p.isTerminated()); |
895 |
+ |
|
896 |
+ |
if (rnd.nextBoolean()) |
897 |
+ |
assertThrows( |
898 |
+ |
RejectedExecutionException.class, |
899 |
+ |
() -> p.submit(task), |
900 |
+ |
() -> p.schedule(task, 1, SECONDS), |
901 |
+ |
() -> p.scheduleAtFixedRate( |
902 |
+ |
new PeriodicTask(1), 1, 1, SECONDS), |
903 |
+ |
() -> p.scheduleWithFixedDelay( |
904 |
+ |
new PeriodicTask(2), 1, 1, SECONDS)); |
905 |
+ |
|
906 |
+ |
assertTrue(q.contains(immediates.get(1))); |
907 |
+ |
assertTrue(!effectiveDelayedPolicy |
908 |
+ |
^ q.contains(delayeds.get(1))); |
909 |
+ |
assertTrue(!effectivePeriodicPolicy |
910 |
+ |
^ q.containsAll(periodics.subList(2, 4))); |
911 |
+ |
|
912 |
+ |
immediates.forEach(f -> assertFalse(f.isDone())); |
913 |
+ |
|
914 |
+ |
assertFalse(delayeds.get(0).isDone()); |
915 |
+ |
if (effectiveDelayedPolicy) |
916 |
+ |
assertFalse(delayeds.get(1).isDone()); |
917 |
+ |
else |
918 |
+ |
assertTrue(delayeds.get(1).isCancelled()); |
919 |
+ |
|
920 |
+ |
if (effectivePeriodicPolicy) |
921 |
+ |
periodics.forEach( |
922 |
+ |
f -> { |
923 |
+ |
assertFalse(f.isDone()); |
924 |
+ |
if (!periodicTasksContinue) { |
925 |
+ |
assertTrue(f.cancel(false)); |
926 |
+ |
assertTrue(f.isCancelled()); |
927 |
+ |
} |
928 |
+ |
}); |
929 |
+ |
else { |
930 |
+ |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
931 |
+ |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
932 |
+ |
} |
933 |
+ |
|
934 |
+ |
unblock.countDown(); // Release all pool threads |
935 |
+ |
|
936 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
937 |
+ |
assertFalse(p.isTerminating()); |
938 |
|
assertTrue(p.isTerminated()); |
939 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
940 |
< |
}} |
939 |
> |
|
940 |
> |
assertTrue(q.isEmpty()); |
941 |
> |
|
942 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
943 |
> |
.forEach(f -> assertTrue(f.isDone())); |
944 |
> |
|
945 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
946 |
> |
|
947 |
> |
assertNull(delayeds.get(0).get()); |
948 |
> |
if (effectiveDelayedPolicy) |
949 |
> |
assertNull(delayeds.get(1).get()); |
950 |
> |
else |
951 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
952 |
> |
|
953 |
> |
if (periodicTasksContinue) |
954 |
> |
periodics.forEach( |
955 |
> |
f -> { |
956 |
> |
try { f.get(); } |
957 |
> |
catch (ExecutionException success) { |
958 |
> |
assertSame(exception, success.getCause()); |
959 |
> |
} |
960 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
961 |
> |
}); |
962 |
> |
else |
963 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
964 |
> |
|
965 |
> |
assertEquals(poolSize + 1 |
966 |
> |
+ (effectiveDelayedPolicy ? 1 : 0) |
967 |
> |
+ (periodicTasksContinue ? 2 : 0), |
968 |
> |
ran.get()); |
969 |
> |
} |
970 |
|
|
971 |
|
/** |
972 |
|
* completed submit of callable returns result |
1037 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1038 |
|
final ExecutorService e = new CustomExecutor(2); |
1039 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1040 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1041 |
|
l.add(latchAwaitingStringTask(latch)); |
1042 |
|
l.add(null); |
1043 |
|
try { |
1054 |
|
public void testInvokeAny4() throws Exception { |
1055 |
|
final ExecutorService e = new CustomExecutor(2); |
1056 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1057 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1057 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1058 |
|
l.add(new NPETask()); |
1059 |
|
try { |
1060 |
|
e.invokeAny(l); |
1071 |
|
public void testInvokeAny5() throws Exception { |
1072 |
|
final ExecutorService e = new CustomExecutor(2); |
1073 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1074 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1074 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1075 |
|
l.add(new StringTask()); |
1076 |
|
l.add(new StringTask()); |
1077 |
|
String result = e.invokeAny(l); |
1109 |
|
public void testInvokeAll3() throws Exception { |
1110 |
|
final ExecutorService e = new CustomExecutor(2); |
1111 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1112 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1112 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1113 |
|
l.add(new StringTask()); |
1114 |
|
l.add(null); |
1115 |
|
try { |
1125 |
|
public void testInvokeAll4() throws Exception { |
1126 |
|
final ExecutorService e = new CustomExecutor(2); |
1127 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1128 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1128 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1129 |
|
l.add(new NPETask()); |
1130 |
|
List<Future<String>> futures = e.invokeAll(l); |
1131 |
|
assertEquals(1, futures.size()); |
1144 |
|
public void testInvokeAll5() throws Exception { |
1145 |
|
final ExecutorService e = new CustomExecutor(2); |
1146 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1147 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1147 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1148 |
|
l.add(new StringTask()); |
1149 |
|
l.add(new StringTask()); |
1150 |
|
List<Future<String>> futures = e.invokeAll(l); |
1173 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1174 |
|
final ExecutorService e = new CustomExecutor(2); |
1175 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1176 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1176 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1177 |
|
l.add(new StringTask()); |
1178 |
|
try { |
1179 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1202 |
|
CountDownLatch latch = new CountDownLatch(1); |
1203 |
|
final ExecutorService e = new CustomExecutor(2); |
1204 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1205 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1205 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1206 |
|
l.add(latchAwaitingStringTask(latch)); |
1207 |
|
l.add(null); |
1208 |
|
try { |
1219 |
|
public void testTimedInvokeAny4() throws Exception { |
1220 |
|
final ExecutorService e = new CustomExecutor(2); |
1221 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1222 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1222 |
> |
long startTime = System.nanoTime(); |
1223 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1224 |
|
l.add(new NPETask()); |
1225 |
|
try { |
1226 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1226 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1227 |
|
shouldThrow(); |
1228 |
|
} catch (ExecutionException success) { |
1229 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1230 |
|
} |
1231 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1232 |
|
} |
1233 |
|
} |
1234 |
|
|
1238 |
|
public void testTimedInvokeAny5() throws Exception { |
1239 |
|
final ExecutorService e = new CustomExecutor(2); |
1240 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1241 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1241 |
> |
long startTime = System.nanoTime(); |
1242 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1243 |
|
l.add(new StringTask()); |
1244 |
|
l.add(new StringTask()); |
1245 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1245 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1246 |
|
assertSame(TEST_STRING, result); |
1247 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1248 |
|
} |
1249 |
|
} |
1250 |
|
|
1267 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1268 |
|
final ExecutorService e = new CustomExecutor(2); |
1269 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1270 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1270 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1271 |
|
l.add(new StringTask()); |
1272 |
|
try { |
1273 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1293 |
|
public void testTimedInvokeAll3() throws Exception { |
1294 |
|
final ExecutorService e = new CustomExecutor(2); |
1295 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1296 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1296 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1297 |
|
l.add(new StringTask()); |
1298 |
|
l.add(null); |
1299 |
|
try { |
1309 |
|
public void testTimedInvokeAll4() throws Exception { |
1310 |
|
final ExecutorService e = new CustomExecutor(2); |
1311 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1312 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1312 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1313 |
|
l.add(new NPETask()); |
1314 |
|
List<Future<String>> futures = |
1315 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1329 |
|
public void testTimedInvokeAll5() throws Exception { |
1330 |
|
final ExecutorService e = new CustomExecutor(2); |
1331 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1332 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1332 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1333 |
|
l.add(new StringTask()); |
1334 |
|
l.add(new StringTask()); |
1335 |
|
List<Future<String>> futures = |
1344 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1345 |
|
*/ |
1346 |
|
public void testTimedInvokeAll6() throws Exception { |
1347 |
< |
final ExecutorService e = new CustomExecutor(2); |
1348 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1349 |
< |
for (long timeout = timeoutMillis();;) { |
1347 |
> |
for (long timeout = timeoutMillis();;) { |
1348 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1349 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1350 |
> |
public String realCall() { |
1351 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1352 |
> |
catch (InterruptedException ok) {} |
1353 |
> |
return "1"; }}; |
1354 |
> |
final ExecutorService p = new CustomExecutor(2); |
1355 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1356 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1357 |
|
tasks.add(new StringTask("0")); |
1358 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1358 |
> |
tasks.add(waiter); |
1359 |
|
tasks.add(new StringTask("2")); |
1360 |
|
long startTime = System.nanoTime(); |
1361 |
|
List<Future<String>> futures = |
1362 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1362 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1363 |
|
assertEquals(tasks.size(), futures.size()); |
1364 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1365 |
|
for (Future future : futures) |