20 |
|
return new TestSuite(ScheduledExecutorTest.class); |
21 |
|
} |
22 |
|
|
23 |
– |
|
23 |
|
/** |
24 |
|
* execute successfully executes a runnable |
25 |
|
*/ |
38 |
|
} |
39 |
|
} |
40 |
|
|
42 |
– |
|
41 |
|
/** |
42 |
|
* delayed schedule of callable successfully executes after delay |
43 |
|
*/ |
44 |
|
public void testSchedule1() throws Exception { |
45 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
< |
final long t0 = System.nanoTime(); |
49 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
46 |
> |
final long startTime = System.nanoTime(); |
47 |
|
final CountDownLatch done = new CountDownLatch(1); |
48 |
|
try { |
49 |
|
Callable task = new CheckedCallable<Boolean>() { |
50 |
|
public Boolean realCall() { |
51 |
|
done.countDown(); |
52 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
52 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
53 |
|
return Boolean.TRUE; |
54 |
|
}}; |
55 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
56 |
< |
assertEquals(Boolean.TRUE, f.get()); |
57 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
55 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
56 |
> |
assertSame(Boolean.TRUE, f.get()); |
57 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
58 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
59 |
|
} finally { |
60 |
|
joinPool(p); |
66 |
|
*/ |
67 |
|
public void testSchedule3() throws Exception { |
68 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
69 |
< |
final long t0 = System.nanoTime(); |
73 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
69 |
> |
final long startTime = System.nanoTime(); |
70 |
|
final CountDownLatch done = new CountDownLatch(1); |
71 |
|
try { |
72 |
|
Runnable task = new CheckedRunnable() { |
73 |
|
public void realRun() { |
74 |
|
done.countDown(); |
75 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
75 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
|
}}; |
77 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
78 |
< |
assertNull(f.get()); |
79 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
80 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
78 |
> |
await(done); |
79 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
80 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
81 |
|
} finally { |
82 |
|
joinPool(p); |
83 |
|
} |
88 |
|
*/ |
89 |
|
public void testSchedule4() throws Exception { |
90 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
91 |
< |
final long t0 = System.nanoTime(); |
96 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
91 |
> |
final long startTime = System.nanoTime(); |
92 |
|
final CountDownLatch done = new CountDownLatch(1); |
93 |
|
try { |
94 |
|
Runnable task = new CheckedRunnable() { |
95 |
|
public void realRun() { |
96 |
|
done.countDown(); |
97 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
97 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
98 |
|
}}; |
99 |
|
ScheduledFuture f = |
100 |
< |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
101 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
102 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
103 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
100 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
101 |
> |
LONG_DELAY_MS, MILLISECONDS); |
102 |
> |
await(done); |
103 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
104 |
|
f.cancel(true); |
105 |
|
} finally { |
106 |
|
joinPool(p); |
110 |
|
/** |
111 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
112 |
|
*/ |
113 |
< |
public void testSchedule5() throws InterruptedException { |
113 |
> |
public void testSchedule5() throws Exception { |
114 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
115 |
< |
final long t0 = System.nanoTime(); |
121 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
115 |
> |
final long startTime = System.nanoTime(); |
116 |
|
final CountDownLatch done = new CountDownLatch(1); |
117 |
|
try { |
118 |
|
Runnable task = new CheckedRunnable() { |
119 |
|
public void realRun() { |
120 |
|
done.countDown(); |
121 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
121 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
122 |
|
}}; |
123 |
|
ScheduledFuture f = |
124 |
< |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
125 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
126 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
127 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
124 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
125 |
> |
LONG_DELAY_MS, MILLISECONDS); |
126 |
> |
await(done); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
|
f.cancel(true); |
129 |
|
} finally { |
130 |
|
joinPool(p); |
170 |
|
joinPool(p); |
171 |
|
} |
172 |
|
|
179 |
– |
|
173 |
|
/** |
174 |
|
* execute(null) throws NPE |
175 |
|
*/ |
320 |
|
threadProceed.await(); |
321 |
|
threadDone.countDown(); |
322 |
|
}}); |
323 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
323 |
> |
await(threadStarted); |
324 |
|
assertEquals(0, p.getCompletedTaskCount()); |
325 |
|
threadProceed.countDown(); |
326 |
|
threadDone.await(); |
327 |
< |
delay(SHORT_DELAY_MS); |
328 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
327 |
> |
long startTime = System.nanoTime(); |
328 |
> |
while (p.getCompletedTaskCount() != 1) { |
329 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
330 |
> |
fail("timed out"); |
331 |
> |
Thread.yield(); |
332 |
> |
} |
333 |
|
} finally { |
334 |
|
joinPool(p); |
335 |
|
} |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* isShutDown is false before shutdown, true after |
459 |
> |
* isShutdown is false before shutdown, true after |
460 |
|
*/ |
461 |
|
public void testIsShutdown() { |
462 |
|
|
470 |
|
assertTrue(p.isShutdown()); |
471 |
|
} |
472 |
|
|
476 |
– |
|
473 |
|
/** |
474 |
|
* isTerminated is false before termination, true after |
475 |
|
*/ |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
< |
* purge removes cancelled tasks from the queue |
586 |
> |
* purge eventually removes cancelled tasks from the queue |
587 |
|
*/ |
588 |
|
public void testPurge() throws InterruptedException { |
589 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
590 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
591 |
< |
for (int i = 0; i < tasks.length; i++) { |
592 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
593 |
< |
} |
591 |
> |
for (int i = 0; i < tasks.length; i++) |
592 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
593 |
> |
LONG_DELAY_MS, MILLISECONDS); |
594 |
|
try { |
595 |
|
int max = tasks.length; |
596 |
|
if (tasks[4].cancel(true)) --max; |
597 |
|
if (tasks[3].cancel(true)) --max; |
598 |
|
// There must eventually be an interference-free point at |
599 |
|
// which purge will not fail. (At worst, when queue is empty.) |
600 |
< |
int k; |
601 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
600 |
> |
long startTime = System.nanoTime(); |
601 |
> |
do { |
602 |
|
p.purge(); |
603 |
|
long count = p.getTaskCount(); |
604 |
< |
if (count >= 0 && count <= max) |
605 |
< |
break; |
606 |
< |
delay(1); |
607 |
< |
} |
612 |
< |
assertTrue(k < SMALL_DELAY_MS); |
604 |
> |
if (count == max) |
605 |
> |
return; |
606 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
607 |
> |
fail("Purge failed to remove cancelled tasks"); |
608 |
|
} finally { |
609 |
|
for (ScheduledFuture task : tasks) |
610 |
|
task.cancel(true); |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
< |
* shutDownNow returns a list containing tasks that were not run |
616 |
> |
* shutdownNow returns a list containing tasks that were not run |
617 |
|
*/ |
618 |
< |
public void testShutDownNow() throws InterruptedException { |
618 |
> |
public void testShutdownNow() { |
619 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
620 |
|
for (int i = 0; i < 5; i++) |
621 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
622 |
< |
List l; |
621 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
622 |
> |
LONG_DELAY_MS, MILLISECONDS); |
623 |
|
try { |
624 |
< |
l = p.shutdownNow(); |
624 |
> |
List<Runnable> l = p.shutdownNow(); |
625 |
> |
assertTrue(p.isShutdown()); |
626 |
> |
assertEquals(5, l.size()); |
627 |
|
} catch (SecurityException ok) { |
628 |
< |
return; |
628 |
> |
// Allowed in case test doesn't have privs |
629 |
> |
} finally { |
630 |
> |
joinPool(p); |
631 |
|
} |
633 |
– |
assertTrue(p.isShutdown()); |
634 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
635 |
– |
joinPool(p); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
|
* In default setting, shutdown cancels periodic but not delayed |
636 |
|
* tasks at shutdown |
637 |
|
*/ |
638 |
< |
public void testShutDown1() throws InterruptedException { |
638 |
> |
public void testShutdown1() throws InterruptedException { |
639 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
640 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
641 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
660 |
|
} |
661 |
|
} |
662 |
|
|
667 |
– |
|
663 |
|
/** |
664 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
665 |
|
* delayed tasks are cancelled at shutdown |
666 |
|
*/ |
667 |
< |
public void testShutDown2() throws InterruptedException { |
667 |
> |
public void testShutdown2() throws InterruptedException { |
668 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
669 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
670 |
|
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
690 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
691 |
|
* periodic tasks are cancelled at shutdown |
692 |
|
*/ |
693 |
< |
public void testShutDown3() throws InterruptedException { |
693 |
> |
public void testShutdown3() throws InterruptedException { |
694 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
695 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
696 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
697 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
698 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
699 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
700 |
+ |
long initialDelay = LONG_DELAY_MS; |
701 |
|
ScheduledFuture task = |
702 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
702 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
703 |
> |
5, MILLISECONDS); |
704 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
705 |
|
assertTrue(p.isShutdown()); |
709 |
– |
BlockingQueue q = p.getQueue(); |
706 |
|
assertTrue(p.getQueue().isEmpty()); |
707 |
|
assertTrue(task.isDone()); |
708 |
|
assertTrue(task.isCancelled()); |
709 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
714 |
< |
assertTrue(p.isTerminated()); |
709 |
> |
joinPool(p); |
710 |
|
} |
711 |
|
|
712 |
|
/** |
713 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
714 |
|
* periodic tasks are not cancelled at shutdown |
715 |
|
*/ |
716 |
< |
public void testShutDown4() throws InterruptedException { |
716 |
> |
public void testShutdown4() throws InterruptedException { |
717 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
718 |
|
final CountDownLatch counter = new CountDownLatch(2); |
719 |
|
try { |
1155 |
|
l.add(new StringTask()); |
1156 |
|
List<Future<String>> futures = |
1157 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1158 |
< |
assertEquals(3, futures.size()); |
1159 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1160 |
< |
Future<String> f1 = it.next(); |
1161 |
< |
Future<String> f2 = it.next(); |
1162 |
< |
Future<String> f3 = it.next(); |
1168 |
< |
assertTrue(f1.isDone()); |
1169 |
< |
assertTrue(f2.isDone()); |
1170 |
< |
assertTrue(f3.isDone()); |
1171 |
< |
assertFalse(f1.isCancelled()); |
1172 |
< |
assertTrue(f2.isCancelled()); |
1158 |
> |
assertEquals(l.size(), futures.size()); |
1159 |
> |
for (Future future : futures) |
1160 |
> |
assertTrue(future.isDone()); |
1161 |
> |
assertFalse(futures.get(0).isCancelled()); |
1162 |
> |
assertTrue(futures.get(1).isCancelled()); |
1163 |
|
} finally { |
1164 |
|
joinPool(e); |
1165 |
|
} |
1166 |
|
} |
1167 |
|
|
1178 |
– |
|
1168 |
|
} |