6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.ExecutionException; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.RejectedExecutionException; |
24 |
> |
import java.util.concurrent.ScheduledFuture; |
25 |
> |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.ThreadFactory; |
27 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
28 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
29 |
> |
|
30 |
> |
import junit.framework.Test; |
31 |
> |
import junit.framework.TestSuite; |
32 |
|
|
33 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
public static Test suite() { |
38 |
|
return new TestSuite(ScheduledExecutorTest.class); |
39 |
|
} |
40 |
|
|
23 |
– |
|
41 |
|
/** |
42 |
|
* execute successfully executes a runnable |
43 |
|
*/ |
56 |
|
} |
57 |
|
} |
58 |
|
|
42 |
– |
|
59 |
|
/** |
60 |
|
* delayed schedule of callable successfully executes after delay |
61 |
|
*/ |
62 |
|
public void testSchedule1() throws Exception { |
63 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
64 |
< |
final long t0 = System.nanoTime(); |
49 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
64 |
> |
final long startTime = System.nanoTime(); |
65 |
|
final CountDownLatch done = new CountDownLatch(1); |
66 |
|
try { |
67 |
|
Callable task = new CheckedCallable<Boolean>() { |
68 |
|
public Boolean realCall() { |
69 |
|
done.countDown(); |
70 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
70 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
71 |
|
return Boolean.TRUE; |
72 |
|
}}; |
73 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
74 |
< |
assertEquals(Boolean.TRUE, f.get()); |
75 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
73 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
74 |
> |
assertSame(Boolean.TRUE, f.get()); |
75 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
|
} finally { |
78 |
|
joinPool(p); |
84 |
|
*/ |
85 |
|
public void testSchedule3() throws Exception { |
86 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
87 |
< |
final long t0 = System.nanoTime(); |
73 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
87 |
> |
final long startTime = System.nanoTime(); |
88 |
|
final CountDownLatch done = new CountDownLatch(1); |
89 |
|
try { |
90 |
|
Runnable task = new CheckedRunnable() { |
91 |
|
public void realRun() { |
92 |
|
done.countDown(); |
93 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
93 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
94 |
|
}}; |
95 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
96 |
< |
assertNull(f.get()); |
97 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
98 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
95 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
96 |
> |
await(done); |
97 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
98 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
99 |
|
} finally { |
100 |
|
joinPool(p); |
101 |
|
} |
106 |
|
*/ |
107 |
|
public void testSchedule4() throws Exception { |
108 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
109 |
< |
final long t0 = System.nanoTime(); |
96 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
109 |
> |
final long startTime = System.nanoTime(); |
110 |
|
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
try { |
112 |
|
Runnable task = new CheckedRunnable() { |
113 |
|
public void realRun() { |
114 |
|
done.countDown(); |
115 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
115 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
116 |
|
}}; |
117 |
|
ScheduledFuture f = |
118 |
< |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
119 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
120 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
121 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
118 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
119 |
> |
LONG_DELAY_MS, MILLISECONDS); |
120 |
> |
await(done); |
121 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
122 |
|
f.cancel(true); |
123 |
|
} finally { |
124 |
|
joinPool(p); |
128 |
|
/** |
129 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
130 |
|
*/ |
131 |
< |
public void testSchedule5() throws InterruptedException { |
131 |
> |
public void testSchedule5() throws Exception { |
132 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
133 |
< |
final long t0 = System.nanoTime(); |
121 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
133 |
> |
final long startTime = System.nanoTime(); |
134 |
|
final CountDownLatch done = new CountDownLatch(1); |
135 |
|
try { |
136 |
|
Runnable task = new CheckedRunnable() { |
137 |
|
public void realRun() { |
138 |
|
done.countDown(); |
139 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
139 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
140 |
|
}}; |
141 |
|
ScheduledFuture f = |
142 |
< |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
143 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
144 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
145 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
142 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
143 |
> |
LONG_DELAY_MS, MILLISECONDS); |
144 |
> |
await(done); |
145 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
146 |
|
f.cancel(true); |
147 |
|
} finally { |
148 |
|
joinPool(p); |
159 |
|
*/ |
160 |
|
public void testFixedRateSequence() throws InterruptedException { |
161 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
162 |
< |
RunnableCounter counter = new RunnableCounter(); |
163 |
< |
ScheduledFuture h = |
164 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
165 |
< |
Thread.sleep(SMALL_DELAY_MS); |
166 |
< |
h.cancel(true); |
167 |
< |
int c = counter.count.get(); |
168 |
< |
// By time scaling conventions, we must have at least |
169 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
170 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
171 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
172 |
< |
joinPool(p); |
162 |
> |
try { |
163 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
164 |
> |
long startTime = System.nanoTime(); |
165 |
> |
int cycles = 10; |
166 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
167 |
> |
Runnable task = new CheckedRunnable() { |
168 |
> |
public void realRun() { done.countDown(); }}; |
169 |
> |
ScheduledFuture h = |
170 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
171 |
> |
done.await(); |
172 |
> |
h.cancel(true); |
173 |
> |
double normalizedTime = |
174 |
> |
(double) millisElapsedSince(startTime) / delay; |
175 |
> |
if (normalizedTime >= cycles - 1 && |
176 |
> |
normalizedTime <= cycles) |
177 |
> |
return; |
178 |
> |
} |
179 |
> |
throw new AssertionError("unexpected execution rate"); |
180 |
> |
} finally { |
181 |
> |
joinPool(p); |
182 |
> |
} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
187 |
|
*/ |
188 |
|
public void testFixedDelaySequence() throws InterruptedException { |
189 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
190 |
< |
RunnableCounter counter = new RunnableCounter(); |
191 |
< |
ScheduledFuture h = |
192 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
193 |
< |
Thread.sleep(SMALL_DELAY_MS); |
194 |
< |
h.cancel(true); |
195 |
< |
int c = counter.count.get(); |
196 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
197 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
198 |
< |
joinPool(p); |
190 |
> |
try { |
191 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
192 |
> |
long startTime = System.nanoTime(); |
193 |
> |
int cycles = 10; |
194 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
195 |
> |
Runnable task = new CheckedRunnable() { |
196 |
> |
public void realRun() { done.countDown(); }}; |
197 |
> |
ScheduledFuture h = |
198 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
199 |
> |
done.await(); |
200 |
> |
h.cancel(true); |
201 |
> |
double normalizedTime = |
202 |
> |
(double) millisElapsedSince(startTime) / delay; |
203 |
> |
if (normalizedTime >= cycles - 1 && |
204 |
> |
normalizedTime <= cycles) |
205 |
> |
return; |
206 |
> |
} |
207 |
> |
throw new AssertionError("unexpected execution rate"); |
208 |
> |
} finally { |
209 |
> |
joinPool(p); |
210 |
> |
} |
211 |
|
} |
212 |
|
|
179 |
– |
|
213 |
|
/** |
214 |
|
* execute(null) throws NPE |
215 |
|
*/ |
273 |
|
/** |
274 |
|
* schedule callable throws RejectedExecutionException if shutdown |
275 |
|
*/ |
276 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
277 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
278 |
< |
try { |
276 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
277 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
278 |
> |
try { |
279 |
|
se.shutdown(); |
280 |
|
se.schedule(new NoOpCallable(), |
281 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
283 |
|
} catch (RejectedExecutionException success) { |
284 |
|
} catch (SecurityException ok) { |
285 |
|
} |
286 |
< |
joinPool(se); |
286 |
> |
joinPool(se); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
334 |
|
assertEquals(1, p.getActiveCount()); |
335 |
|
done.await(); |
336 |
|
}}); |
337 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
337 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
338 |
|
assertEquals(1, p.getActiveCount()); |
339 |
|
} finally { |
340 |
|
done.countDown(); |
360 |
|
threadProceed.await(); |
361 |
|
threadDone.countDown(); |
362 |
|
}}); |
363 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
363 |
> |
await(threadStarted); |
364 |
|
assertEquals(0, p.getCompletedTaskCount()); |
365 |
|
threadProceed.countDown(); |
366 |
|
threadDone.await(); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
367 |
> |
long startTime = System.nanoTime(); |
368 |
> |
while (p.getCompletedTaskCount() != 1) { |
369 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
370 |
> |
fail("timed out"); |
371 |
> |
Thread.yield(); |
372 |
> |
} |
373 |
|
} finally { |
374 |
|
joinPool(p); |
375 |
|
} |
402 |
|
done.await(); |
403 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
404 |
|
}}); |
405 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
405 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
406 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
407 |
|
} finally { |
408 |
|
done.countDown(); |
427 |
|
assertEquals(1, p.getPoolSize()); |
428 |
|
done.await(); |
429 |
|
}}); |
430 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
430 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
431 |
|
assertEquals(1, p.getPoolSize()); |
432 |
|
} finally { |
433 |
|
done.countDown(); |
452 |
|
threadStarted.countDown(); |
453 |
|
done.await(); |
454 |
|
}}); |
455 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
455 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
456 |
|
assertEquals(TASKS, p.getTaskCount()); |
457 |
|
} finally { |
458 |
|
done.countDown(); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* isShutDown is false before shutdown, true after |
499 |
> |
* isShutdown is false before shutdown, true after |
500 |
|
*/ |
501 |
|
public void testIsShutdown() { |
502 |
|
|
510 |
|
assertTrue(p.isShutdown()); |
511 |
|
} |
512 |
|
|
476 |
– |
|
513 |
|
/** |
514 |
|
* isTerminated is false before termination, true after |
515 |
|
*/ |
525 |
|
threadStarted.countDown(); |
526 |
|
done.await(); |
527 |
|
}}); |
528 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
528 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
529 |
|
assertFalse(p.isTerminating()); |
530 |
|
done.countDown(); |
531 |
|
} finally { |
550 |
|
threadStarted.countDown(); |
551 |
|
done.await(); |
552 |
|
}}); |
553 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
553 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
554 |
|
assertFalse(p.isTerminating()); |
555 |
|
done.countDown(); |
556 |
|
} finally { |
578 |
|
}}; |
579 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
580 |
|
} |
581 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
581 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
582 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
583 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
584 |
|
assertFalse(q.contains(tasks[0])); |
605 |
|
}}; |
606 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
607 |
|
} |
608 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
608 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
609 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
610 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
611 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* purge removes cancelled tasks from the queue |
626 |
> |
* purge eventually removes cancelled tasks from the queue |
627 |
|
*/ |
628 |
|
public void testPurge() throws InterruptedException { |
629 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
630 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
631 |
< |
for (int i = 0; i < tasks.length; i++) { |
632 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
633 |
< |
} |
631 |
> |
for (int i = 0; i < tasks.length; i++) |
632 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
633 |
> |
LONG_DELAY_MS, MILLISECONDS); |
634 |
|
try { |
635 |
|
int max = tasks.length; |
636 |
|
if (tasks[4].cancel(true)) --max; |
637 |
|
if (tasks[3].cancel(true)) --max; |
638 |
|
// There must eventually be an interference-free point at |
639 |
|
// which purge will not fail. (At worst, when queue is empty.) |
640 |
< |
int k; |
641 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
640 |
> |
long startTime = System.nanoTime(); |
641 |
> |
do { |
642 |
|
p.purge(); |
643 |
|
long count = p.getTaskCount(); |
644 |
< |
if (count >= 0 && count <= max) |
645 |
< |
break; |
646 |
< |
Thread.sleep(1); |
647 |
< |
} |
612 |
< |
assertTrue(k < SMALL_DELAY_MS); |
644 |
> |
if (count == max) |
645 |
> |
return; |
646 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
647 |
> |
fail("Purge failed to remove cancelled tasks"); |
648 |
|
} finally { |
649 |
|
for (ScheduledFuture task : tasks) |
650 |
|
task.cancel(true); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* shutDownNow returns a list containing tasks that were not run |
656 |
> |
* shutdownNow returns a list containing tasks that were not run, |
657 |
> |
* and those tasks are drained from the queue |
658 |
|
*/ |
659 |
< |
public void testShutDownNow() throws InterruptedException { |
660 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
661 |
< |
for (int i = 0; i < 5; i++) |
662 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
663 |
< |
List l; |
659 |
> |
public void testShutdownNow() throws InterruptedException { |
660 |
> |
final int poolSize = 2; |
661 |
> |
final int count = 5; |
662 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
663 |
> |
final ScheduledThreadPoolExecutor p = |
664 |
> |
new ScheduledThreadPoolExecutor(poolSize); |
665 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
666 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
667 |
> |
threadsStarted.countDown(); |
668 |
> |
try { |
669 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
670 |
> |
} catch (InterruptedException success) {} |
671 |
> |
ran.getAndIncrement(); |
672 |
> |
}}; |
673 |
> |
for (int i = 0; i < count; i++) |
674 |
> |
p.execute(waiter); |
675 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
> |
assertEquals(poolSize, p.getActiveCount()); |
677 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
678 |
> |
final List<Runnable> queuedTasks; |
679 |
|
try { |
680 |
< |
l = p.shutdownNow(); |
680 |
> |
queuedTasks = p.shutdownNow(); |
681 |
|
} catch (SecurityException ok) { |
682 |
< |
return; |
682 |
> |
return; // Allowed in case test doesn't have privs |
683 |
|
} |
684 |
|
assertTrue(p.isShutdown()); |
685 |
< |
assertTrue(l.size() > 0 && l.size() <= 5); |
686 |
< |
joinPool(p); |
685 |
> |
assertTrue(p.getQueue().isEmpty()); |
686 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
687 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
688 |
> |
assertTrue(p.isTerminated()); |
689 |
> |
assertEquals(poolSize, ran.get()); |
690 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
691 |
|
} |
692 |
|
|
693 |
|
/** |
694 |
< |
* In default setting, shutdown cancels periodic but not delayed |
695 |
< |
* tasks at shutdown |
694 |
> |
* shutdownNow returns a list containing tasks that were not run, |
695 |
> |
* and those tasks are drained from the queue |
696 |
|
*/ |
697 |
< |
public void testShutDown1() throws InterruptedException { |
697 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
698 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
699 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
700 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
701 |
< |
|
702 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
703 |
< |
for (int i = 0; i < tasks.length; i++) |
704 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
705 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
706 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
707 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
708 |
< |
for (ScheduledFuture task : tasks) { |
709 |
< |
assertFalse(task.isDone()); |
710 |
< |
assertFalse(task.isCancelled()); |
711 |
< |
assertTrue(q.contains(task)); |
699 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
700 |
> |
for (int i = 0; i < 3; i++) { |
701 |
> |
Runnable r = new NoOpRunnable(); |
702 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
703 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
704 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
705 |
> |
} |
706 |
> |
if (testImplementationDetails) |
707 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
708 |
> |
final List<Runnable> queuedTasks; |
709 |
> |
try { |
710 |
> |
queuedTasks = p.shutdownNow(); |
711 |
> |
} catch (SecurityException ok) { |
712 |
> |
return; // Allowed in case test doesn't have privs |
713 |
|
} |
714 |
|
assertTrue(p.isShutdown()); |
715 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
716 |
< |
assertTrue(p.isTerminated()); |
715 |
> |
assertTrue(p.getQueue().isEmpty()); |
716 |
> |
if (testImplementationDetails) |
717 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
718 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
719 |
|
for (ScheduledFuture task : tasks) { |
720 |
< |
assertTrue(task.isDone()); |
720 |
> |
assertFalse(task.isDone()); |
721 |
|
assertFalse(task.isCancelled()); |
722 |
|
} |
723 |
< |
} |
666 |
< |
|
667 |
< |
|
668 |
< |
/** |
669 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
670 |
< |
* delayed tasks are cancelled at shutdown |
671 |
< |
*/ |
672 |
< |
public void testShutDown2() throws InterruptedException { |
673 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
674 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
675 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
676 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
677 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
678 |
< |
for (int i = 0; i < tasks.length; i++) |
679 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
680 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
681 |
< |
BlockingQueue q = p.getQueue(); |
682 |
< |
assertEquals(tasks.length, q.size()); |
683 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
684 |
< |
assertTrue(p.isShutdown()); |
685 |
< |
assertTrue(q.isEmpty()); |
686 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
723 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
724 |
|
assertTrue(p.isTerminated()); |
688 |
– |
for (ScheduledFuture task : tasks) { |
689 |
– |
assertTrue(task.isDone()); |
690 |
– |
assertTrue(task.isCancelled()); |
691 |
– |
} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
729 |
< |
* periodic tasks are cancelled at shutdown |
730 |
< |
*/ |
731 |
< |
public void testShutDown3() throws InterruptedException { |
732 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
733 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
736 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
737 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
738 |
< |
ScheduledFuture task = |
739 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
728 |
> |
* By default, periodic tasks are cancelled at shutdown. |
729 |
> |
* By default, delayed tasks keep running after shutdown. |
730 |
> |
* Check that changing the default values work: |
731 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
732 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
733 |
> |
*/ |
734 |
> |
public void testShutdown_cancellation() throws Exception { |
735 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
736 |
> |
for (Boolean policy : allBooleans) |
737 |
> |
{ |
738 |
> |
final int poolSize = 2; |
739 |
> |
final ScheduledThreadPoolExecutor p |
740 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
741 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
742 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
743 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
744 |
> |
if (policy != null) { |
745 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
746 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
747 |
> |
p.setRemoveOnCancelPolicy(policy); |
748 |
> |
} |
749 |
> |
assertEquals(effectiveDelayedPolicy, |
750 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
751 |
> |
assertEquals(effectivePeriodicPolicy, |
752 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
753 |
> |
assertEquals(effectiveRemovePolicy, |
754 |
> |
p.getRemoveOnCancelPolicy()); |
755 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
756 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
757 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
758 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
759 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
760 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
761 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
762 |
> |
throws InterruptedException { |
763 |
> |
poolBlocked.countDown(); |
764 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
765 |
> |
ran.getAndIncrement(); |
766 |
> |
}}; |
767 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
768 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
769 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
770 |
> |
for (int i = 0; i < poolSize; i++) |
771 |
> |
blockers.add(p.submit(task)); |
772 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
773 |
> |
|
774 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
775 |
> |
1, 1, MILLISECONDS)); |
776 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
777 |
> |
1, 1, MILLISECONDS)); |
778 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
779 |
> |
|
780 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
781 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
782 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
783 |
|
assertTrue(p.isShutdown()); |
784 |
< |
BlockingQueue q = p.getQueue(); |
785 |
< |
assertTrue(p.getQueue().isEmpty()); |
786 |
< |
assertTrue(task.isDone()); |
787 |
< |
assertTrue(task.isCancelled()); |
788 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
789 |
< |
assertTrue(p.isTerminated()); |
790 |
< |
} |
791 |
< |
|
792 |
< |
/** |
793 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
794 |
< |
* periodic tasks are not cancelled at shutdown |
795 |
< |
*/ |
796 |
< |
public void testShutDown4() throws InterruptedException { |
797 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
798 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
799 |
< |
try { |
800 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
801 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
802 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
803 |
< |
final Runnable r = new CheckedRunnable() { |
804 |
< |
public void realRun() { |
805 |
< |
counter.countDown(); |
731 |
< |
}}; |
732 |
< |
ScheduledFuture task = |
733 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
734 |
< |
assertFalse(task.isDone()); |
735 |
< |
assertFalse(task.isCancelled()); |
736 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
737 |
< |
assertFalse(task.isCancelled()); |
738 |
< |
assertFalse(p.isTerminated()); |
739 |
< |
assertTrue(p.isShutdown()); |
740 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
741 |
< |
assertFalse(task.isCancelled()); |
742 |
< |
assertTrue(task.cancel(false)); |
743 |
< |
assertTrue(task.isDone()); |
744 |
< |
assertTrue(task.isCancelled()); |
745 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
746 |
< |
assertTrue(p.isTerminated()); |
784 |
> |
assertFalse(p.isTerminated()); |
785 |
> |
for (Future<?> periodic : periodics) { |
786 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
787 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
788 |
> |
} |
789 |
> |
for (Future<?> delayed : delayeds) { |
790 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
791 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
792 |
> |
} |
793 |
> |
if (testImplementationDetails) { |
794 |
> |
assertEquals(effectivePeriodicPolicy, |
795 |
> |
p.getQueue().containsAll(periodics)); |
796 |
> |
assertEquals(effectiveDelayedPolicy, |
797 |
> |
p.getQueue().containsAll(delayeds)); |
798 |
> |
} |
799 |
> |
// Release all pool threads |
800 |
> |
unblock.countDown(); |
801 |
> |
|
802 |
> |
for (Future<?> delayed : delayeds) { |
803 |
> |
if (effectiveDelayedPolicy) { |
804 |
> |
assertNull(delayed.get()); |
805 |
> |
} |
806 |
|
} |
807 |
< |
finally { |
808 |
< |
joinPool(p); |
807 |
> |
if (effectivePeriodicPolicy) { |
808 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
809 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
810 |
> |
for (Future<?> periodic : periodics) { |
811 |
> |
assertTrue(periodic.cancel(false)); |
812 |
> |
assertTrue(periodic.isCancelled()); |
813 |
> |
assertTrue(periodic.isDone()); |
814 |
> |
} |
815 |
|
} |
816 |
< |
} |
816 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
817 |
> |
assertTrue(p.isTerminated()); |
818 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
819 |
> |
}} |
820 |
|
|
821 |
|
/** |
822 |
|
* completed submit of callable returns result |
1207 |
|
l.add(new StringTask()); |
1208 |
|
l.add(new StringTask()); |
1209 |
|
List<Future<String>> futures = |
1210 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1210 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1211 |
|
assertEquals(2, futures.size()); |
1212 |
|
for (Future<String> future : futures) |
1213 |
|
assertSame(TEST_STRING, future.get()); |
1222 |
|
public void testTimedInvokeAll6() throws Exception { |
1223 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1224 |
|
try { |
1225 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1226 |
< |
l.add(new StringTask()); |
1227 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1228 |
< |
l.add(new StringTask()); |
1229 |
< |
List<Future<String>> futures = |
1230 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1231 |
< |
assertEquals(3, futures.size()); |
1232 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1233 |
< |
Future<String> f1 = it.next(); |
1234 |
< |
Future<String> f2 = it.next(); |
1235 |
< |
Future<String> f3 = it.next(); |
1236 |
< |
assertTrue(f1.isDone()); |
1237 |
< |
assertTrue(f2.isDone()); |
1238 |
< |
assertTrue(f3.isDone()); |
1239 |
< |
assertFalse(f1.isCancelled()); |
1240 |
< |
assertTrue(f2.isCancelled()); |
1225 |
> |
for (long timeout = timeoutMillis();;) { |
1226 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1227 |
> |
tasks.add(new StringTask("0")); |
1228 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1229 |
> |
tasks.add(new StringTask("2")); |
1230 |
> |
long startTime = System.nanoTime(); |
1231 |
> |
List<Future<String>> futures = |
1232 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1233 |
> |
assertEquals(tasks.size(), futures.size()); |
1234 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1235 |
> |
for (Future future : futures) |
1236 |
> |
assertTrue(future.isDone()); |
1237 |
> |
assertTrue(futures.get(1).isCancelled()); |
1238 |
> |
try { |
1239 |
> |
assertEquals("0", futures.get(0).get()); |
1240 |
> |
assertEquals("2", futures.get(2).get()); |
1241 |
> |
break; |
1242 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1243 |
> |
timeout *= 2; |
1244 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1245 |
> |
fail("expected exactly one task to be cancelled"); |
1246 |
> |
} |
1247 |
> |
} |
1248 |
|
} finally { |
1249 |
|
joinPool(e); |
1250 |
|
} |
1251 |
|
} |
1252 |
|
|
1178 |
– |
|
1253 |
|
} |