1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.*; |
11 |
import java.util.concurrent.*; |
12 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
import java.util.concurrent.atomic.*; |
14 |
|
15 |
public class ScheduledExecutorTest extends JSR166TestCase { |
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run (suite()); |
18 |
} |
19 |
public static Test suite() { |
20 |
return new TestSuite(ScheduledExecutorTest.class); |
21 |
} |
22 |
|
23 |
|
24 |
/** |
25 |
* execute successfully executes a runnable |
26 |
*/ |
27 |
public void testExecute() throws InterruptedException { |
28 |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
29 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
30 |
p1.execute(runnable); |
31 |
assertFalse(runnable.done); |
32 |
Thread.sleep(SHORT_DELAY_MS); |
33 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
34 |
Thread.sleep(MEDIUM_DELAY_MS); |
35 |
assertTrue(runnable.done); |
36 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
37 |
joinPool(p1); |
38 |
} |
39 |
|
40 |
|
41 |
/** |
42 |
* delayed schedule of callable successfully executes after delay |
43 |
*/ |
44 |
public void testSchedule1() throws Exception { |
45 |
TrackedCallable callable = new TrackedCallable(); |
46 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
47 |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
48 |
assertFalse(callable.done); |
49 |
Thread.sleep(MEDIUM_DELAY_MS); |
50 |
assertTrue(callable.done); |
51 |
assertEquals(Boolean.TRUE, f.get()); |
52 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
53 |
joinPool(p1); |
54 |
} |
55 |
|
56 |
/** |
57 |
* delayed schedule of runnable successfully executes after delay |
58 |
*/ |
59 |
public void testSchedule3() throws InterruptedException { |
60 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
61 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
62 |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
63 |
Thread.sleep(SHORT_DELAY_MS); |
64 |
assertFalse(runnable.done); |
65 |
Thread.sleep(MEDIUM_DELAY_MS); |
66 |
assertTrue(runnable.done); |
67 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
68 |
joinPool(p1); |
69 |
} |
70 |
|
71 |
/** |
72 |
* scheduleAtFixedRate executes runnable after given initial delay |
73 |
*/ |
74 |
public void testSchedule4() throws InterruptedException { |
75 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
76 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
77 |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
78 |
assertFalse(runnable.done); |
79 |
Thread.sleep(MEDIUM_DELAY_MS); |
80 |
assertTrue(runnable.done); |
81 |
h.cancel(true); |
82 |
joinPool(p1); |
83 |
} |
84 |
|
85 |
static class RunnableCounter implements Runnable { |
86 |
AtomicInteger count = new AtomicInteger(0); |
87 |
public void run() { count.getAndIncrement(); } |
88 |
} |
89 |
|
90 |
/** |
91 |
* scheduleWithFixedDelay executes runnable after given initial delay |
92 |
*/ |
93 |
public void testSchedule5() throws InterruptedException { |
94 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
95 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
96 |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
97 |
assertFalse(runnable.done); |
98 |
Thread.sleep(MEDIUM_DELAY_MS); |
99 |
assertTrue(runnable.done); |
100 |
h.cancel(true); |
101 |
joinPool(p1); |
102 |
} |
103 |
|
104 |
/** |
105 |
* scheduleAtFixedRate executes series of tasks at given rate |
106 |
*/ |
107 |
public void testFixedRateSequence() throws InterruptedException { |
108 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
109 |
RunnableCounter counter = new RunnableCounter(); |
110 |
ScheduledFuture h = |
111 |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
112 |
Thread.sleep(SMALL_DELAY_MS); |
113 |
h.cancel(true); |
114 |
int c = counter.count.get(); |
115 |
// By time scaling conventions, we must have at least |
116 |
// an execution per SHORT delay, but no more than one SHORT more |
117 |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
118 |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
119 |
joinPool(p1); |
120 |
} |
121 |
|
122 |
/** |
123 |
* scheduleWithFixedDelay executes series of tasks with given period |
124 |
*/ |
125 |
public void testFixedDelaySequence() throws InterruptedException { |
126 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
127 |
RunnableCounter counter = new RunnableCounter(); |
128 |
ScheduledFuture h = |
129 |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
130 |
Thread.sleep(SMALL_DELAY_MS); |
131 |
h.cancel(true); |
132 |
int c = counter.count.get(); |
133 |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
134 |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
135 |
joinPool(p1); |
136 |
} |
137 |
|
138 |
|
139 |
/** |
140 |
* execute (null) throws NPE |
141 |
*/ |
142 |
public void testExecuteNull() throws InterruptedException { |
143 |
ScheduledThreadPoolExecutor se = null; |
144 |
try { |
145 |
se = new ScheduledThreadPoolExecutor(1); |
146 |
se.execute(null); |
147 |
shouldThrow(); |
148 |
} catch (NullPointerException success) {} |
149 |
|
150 |
joinPool(se); |
151 |
} |
152 |
|
153 |
/** |
154 |
* schedule (null) throws NPE |
155 |
*/ |
156 |
public void testScheduleNull() throws InterruptedException { |
157 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
158 |
try { |
159 |
TrackedCallable callable = null; |
160 |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
161 |
shouldThrow(); |
162 |
} catch (NullPointerException success) {} |
163 |
joinPool(se); |
164 |
} |
165 |
|
166 |
/** |
167 |
* execute throws RejectedExecutionException if shutdown |
168 |
*/ |
169 |
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
170 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
171 |
try { |
172 |
se.shutdown(); |
173 |
se.schedule(new NoOpRunnable(), |
174 |
MEDIUM_DELAY_MS, MILLISECONDS); |
175 |
shouldThrow(); |
176 |
} catch (RejectedExecutionException success) { |
177 |
} catch (SecurityException ok) { |
178 |
} |
179 |
|
180 |
joinPool(se); |
181 |
|
182 |
} |
183 |
|
184 |
/** |
185 |
* schedule throws RejectedExecutionException if shutdown |
186 |
*/ |
187 |
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
188 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
189 |
try { |
190 |
se.shutdown(); |
191 |
se.schedule(new NoOpCallable(), |
192 |
MEDIUM_DELAY_MS, MILLISECONDS); |
193 |
shouldThrow(); |
194 |
} catch (RejectedExecutionException success) { |
195 |
} catch (SecurityException ok) { |
196 |
} |
197 |
joinPool(se); |
198 |
} |
199 |
|
200 |
/** |
201 |
* schedule callable throws RejectedExecutionException if shutdown |
202 |
*/ |
203 |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
204 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
205 |
try { |
206 |
se.shutdown(); |
207 |
se.schedule(new NoOpCallable(), |
208 |
MEDIUM_DELAY_MS, MILLISECONDS); |
209 |
shouldThrow(); |
210 |
} catch (RejectedExecutionException success) { |
211 |
} catch (SecurityException ok) { |
212 |
} |
213 |
joinPool(se); |
214 |
} |
215 |
|
216 |
/** |
217 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
218 |
*/ |
219 |
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
220 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
221 |
try { |
222 |
se.shutdown(); |
223 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
224 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
225 |
shouldThrow(); |
226 |
} catch (RejectedExecutionException success) { |
227 |
} catch (SecurityException ok) { |
228 |
} |
229 |
joinPool(se); |
230 |
} |
231 |
|
232 |
/** |
233 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
234 |
*/ |
235 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
236 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
237 |
try { |
238 |
se.shutdown(); |
239 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
240 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
241 |
shouldThrow(); |
242 |
} catch (RejectedExecutionException success) { |
243 |
} catch (SecurityException ok) { |
244 |
} |
245 |
joinPool(se); |
246 |
} |
247 |
|
248 |
/** |
249 |
* getActiveCount increases but doesn't overestimate, when a |
250 |
* thread becomes active |
251 |
*/ |
252 |
public void testGetActiveCount() throws InterruptedException { |
253 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
254 |
assertEquals(0, p2.getActiveCount()); |
255 |
p2.execute(new SmallRunnable()); |
256 |
Thread.sleep(SHORT_DELAY_MS); |
257 |
assertEquals(1, p2.getActiveCount()); |
258 |
joinPool(p2); |
259 |
} |
260 |
|
261 |
/** |
262 |
* getCompletedTaskCount increases, but doesn't overestimate, |
263 |
* when tasks complete |
264 |
*/ |
265 |
public void testGetCompletedTaskCount() throws InterruptedException { |
266 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
267 |
assertEquals(0, p2.getCompletedTaskCount()); |
268 |
p2.execute(new SmallRunnable()); |
269 |
Thread.sleep(MEDIUM_DELAY_MS); |
270 |
assertEquals(1, p2.getCompletedTaskCount()); |
271 |
joinPool(p2); |
272 |
} |
273 |
|
274 |
/** |
275 |
* getCorePoolSize returns size given in constructor if not otherwise set |
276 |
*/ |
277 |
public void testGetCorePoolSize() throws InterruptedException { |
278 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
279 |
assertEquals(1, p1.getCorePoolSize()); |
280 |
joinPool(p1); |
281 |
} |
282 |
|
283 |
/** |
284 |
* getLargestPoolSize increases, but doesn't overestimate, when |
285 |
* multiple threads active |
286 |
*/ |
287 |
public void testGetLargestPoolSize() throws InterruptedException { |
288 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
289 |
assertEquals(0, p2.getLargestPoolSize()); |
290 |
p2.execute(new SmallRunnable()); |
291 |
p2.execute(new SmallRunnable()); |
292 |
Thread.sleep(SHORT_DELAY_MS); |
293 |
assertEquals(2, p2.getLargestPoolSize()); |
294 |
joinPool(p2); |
295 |
} |
296 |
|
297 |
/** |
298 |
* getPoolSize increases, but doesn't overestimate, when threads |
299 |
* become active |
300 |
*/ |
301 |
public void testGetPoolSize() throws InterruptedException { |
302 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
303 |
assertEquals(0, p1.getPoolSize()); |
304 |
p1.execute(new SmallRunnable()); |
305 |
assertEquals(1, p1.getPoolSize()); |
306 |
joinPool(p1); |
307 |
} |
308 |
|
309 |
/** |
310 |
* getTaskCount increases, but doesn't overestimate, when tasks |
311 |
* submitted |
312 |
*/ |
313 |
public void testGetTaskCount() throws InterruptedException { |
314 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
315 |
assertEquals(0, p1.getTaskCount()); |
316 |
for (int i = 0; i < 5; i++) |
317 |
p1.execute(new SmallRunnable()); |
318 |
Thread.sleep(SHORT_DELAY_MS); |
319 |
assertEquals(5, p1.getTaskCount()); |
320 |
joinPool(p1); |
321 |
} |
322 |
|
323 |
/** |
324 |
* getThreadFactory returns factory in constructor if not set |
325 |
*/ |
326 |
public void testGetThreadFactory() throws InterruptedException { |
327 |
ThreadFactory tf = new SimpleThreadFactory(); |
328 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
329 |
assertSame(tf, p.getThreadFactory()); |
330 |
joinPool(p); |
331 |
} |
332 |
|
333 |
/** |
334 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
335 |
*/ |
336 |
public void testSetThreadFactory() throws InterruptedException { |
337 |
ThreadFactory tf = new SimpleThreadFactory(); |
338 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
339 |
p.setThreadFactory(tf); |
340 |
assertSame(tf, p.getThreadFactory()); |
341 |
joinPool(p); |
342 |
} |
343 |
|
344 |
/** |
345 |
* setThreadFactory(null) throws NPE |
346 |
*/ |
347 |
public void testSetThreadFactoryNull() throws InterruptedException { |
348 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
349 |
try { |
350 |
p.setThreadFactory(null); |
351 |
shouldThrow(); |
352 |
} catch (NullPointerException success) { |
353 |
} finally { |
354 |
joinPool(p); |
355 |
} |
356 |
} |
357 |
|
358 |
/** |
359 |
* is isShutDown is false before shutdown, true after |
360 |
*/ |
361 |
public void testIsShutdown() { |
362 |
|
363 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
364 |
try { |
365 |
assertFalse(p1.isShutdown()); |
366 |
} |
367 |
finally { |
368 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
369 |
} |
370 |
assertTrue(p1.isShutdown()); |
371 |
} |
372 |
|
373 |
|
374 |
/** |
375 |
* isTerminated is false before termination, true after |
376 |
*/ |
377 |
public void testIsTerminated() throws InterruptedException { |
378 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
379 |
try { |
380 |
p1.execute(new SmallRunnable()); |
381 |
} finally { |
382 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
383 |
} |
384 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
385 |
assertTrue(p1.isTerminated()); |
386 |
} |
387 |
|
388 |
/** |
389 |
* isTerminating is not true when running or when terminated |
390 |
*/ |
391 |
public void testIsTerminating() throws InterruptedException { |
392 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
393 |
assertFalse(p1.isTerminating()); |
394 |
try { |
395 |
p1.execute(new SmallRunnable()); |
396 |
assertFalse(p1.isTerminating()); |
397 |
} finally { |
398 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
399 |
} |
400 |
|
401 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
402 |
assertTrue(p1.isTerminated()); |
403 |
assertFalse(p1.isTerminating()); |
404 |
} |
405 |
|
406 |
/** |
407 |
* getQueue returns the work queue, which contains queued tasks |
408 |
*/ |
409 |
public void testGetQueue() throws InterruptedException { |
410 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
411 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
412 |
for (int i = 0; i < 5; i++) { |
413 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
414 |
} |
415 |
try { |
416 |
Thread.sleep(SHORT_DELAY_MS); |
417 |
BlockingQueue<Runnable> q = p1.getQueue(); |
418 |
assertTrue(q.contains(tasks[4])); |
419 |
assertFalse(q.contains(tasks[0])); |
420 |
} finally { |
421 |
joinPool(p1); |
422 |
} |
423 |
} |
424 |
|
425 |
/** |
426 |
* remove(task) removes queued task, and fails to remove active task |
427 |
*/ |
428 |
public void testRemove() throws InterruptedException { |
429 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
430 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
431 |
for (int i = 0; i < 5; i++) { |
432 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
433 |
} |
434 |
try { |
435 |
Thread.sleep(SHORT_DELAY_MS); |
436 |
BlockingQueue<Runnable> q = p1.getQueue(); |
437 |
assertFalse(p1.remove((Runnable)tasks[0])); |
438 |
assertTrue(q.contains((Runnable)tasks[4])); |
439 |
assertTrue(q.contains((Runnable)tasks[3])); |
440 |
assertTrue(p1.remove((Runnable)tasks[4])); |
441 |
assertFalse(p1.remove((Runnable)tasks[4])); |
442 |
assertFalse(q.contains((Runnable)tasks[4])); |
443 |
assertTrue(q.contains((Runnable)tasks[3])); |
444 |
assertTrue(p1.remove((Runnable)tasks[3])); |
445 |
assertFalse(q.contains((Runnable)tasks[3])); |
446 |
} finally { |
447 |
joinPool(p1); |
448 |
} |
449 |
} |
450 |
|
451 |
/** |
452 |
* purge removes cancelled tasks from the queue |
453 |
*/ |
454 |
public void testPurge() throws InterruptedException { |
455 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
456 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
457 |
for (int i = 0; i < 5; i++) { |
458 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
459 |
} |
460 |
try { |
461 |
int max = 5; |
462 |
if (tasks[4].cancel(true)) --max; |
463 |
if (tasks[3].cancel(true)) --max; |
464 |
// There must eventually be an interference-free point at |
465 |
// which purge will not fail. (At worst, when queue is empty.) |
466 |
int k; |
467 |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
468 |
p1.purge(); |
469 |
long count = p1.getTaskCount(); |
470 |
if (count >= 0 && count <= max) |
471 |
break; |
472 |
Thread.sleep(1); |
473 |
} |
474 |
assertTrue(k < SMALL_DELAY_MS); |
475 |
} finally { |
476 |
joinPool(p1); |
477 |
} |
478 |
} |
479 |
|
480 |
/** |
481 |
* shutDownNow returns a list containing tasks that were not run |
482 |
*/ |
483 |
public void testShutDownNow() throws InterruptedException { |
484 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
485 |
for (int i = 0; i < 5; i++) |
486 |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
487 |
List l; |
488 |
try { |
489 |
l = p1.shutdownNow(); |
490 |
} catch (SecurityException ok) { |
491 |
return; |
492 |
} |
493 |
assertTrue(p1.isShutdown()); |
494 |
assertTrue(l.size() > 0 && l.size() <= 5); |
495 |
joinPool(p1); |
496 |
} |
497 |
|
498 |
/** |
499 |
* In default setting, shutdown cancels periodic but not delayed |
500 |
* tasks at shutdown |
501 |
*/ |
502 |
public void testShutDown1() throws InterruptedException { |
503 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
504 |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
505 |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
506 |
|
507 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
508 |
for (int i = 0; i < 5; i++) |
509 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
510 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
511 |
BlockingQueue q = p1.getQueue(); |
512 |
for (Iterator it = q.iterator(); it.hasNext();) { |
513 |
ScheduledFuture t = (ScheduledFuture)it.next(); |
514 |
assertFalse(t.isCancelled()); |
515 |
} |
516 |
assertTrue(p1.isShutdown()); |
517 |
Thread.sleep(SMALL_DELAY_MS); |
518 |
for (int i = 0; i < 5; ++i) { |
519 |
assertTrue(tasks[i].isDone()); |
520 |
assertFalse(tasks[i].isCancelled()); |
521 |
} |
522 |
} |
523 |
|
524 |
|
525 |
/** |
526 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
527 |
* delayed tasks are cancelled at shutdown |
528 |
*/ |
529 |
public void testShutDown2() throws InterruptedException { |
530 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
531 |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
532 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
533 |
for (int i = 0; i < 5; i++) |
534 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
535 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
536 |
assertTrue(p1.isShutdown()); |
537 |
BlockingQueue q = p1.getQueue(); |
538 |
assertTrue(q.isEmpty()); |
539 |
Thread.sleep(SMALL_DELAY_MS); |
540 |
assertTrue(p1.isTerminated()); |
541 |
} |
542 |
|
543 |
|
544 |
/** |
545 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
546 |
* periodic tasks are not cancelled at shutdown |
547 |
*/ |
548 |
public void testShutDown3() throws InterruptedException { |
549 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
550 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
551 |
ScheduledFuture task = |
552 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
553 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
554 |
assertTrue(p1.isShutdown()); |
555 |
BlockingQueue q = p1.getQueue(); |
556 |
assertTrue(q.isEmpty()); |
557 |
Thread.sleep(SHORT_DELAY_MS); |
558 |
assertTrue(p1.isTerminated()); |
559 |
} |
560 |
|
561 |
/** |
562 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
563 |
* periodic tasks are cancelled at shutdown |
564 |
*/ |
565 |
public void testShutDown4() throws InterruptedException { |
566 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
567 |
try { |
568 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
569 |
ScheduledFuture task = |
570 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
571 |
assertFalse(task.isCancelled()); |
572 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
573 |
assertFalse(task.isCancelled()); |
574 |
assertFalse(p1.isTerminated()); |
575 |
assertTrue(p1.isShutdown()); |
576 |
Thread.sleep(SHORT_DELAY_MS); |
577 |
assertFalse(task.isCancelled()); |
578 |
assertTrue(task.cancel(true)); |
579 |
assertTrue(task.isDone()); |
580 |
Thread.sleep(SHORT_DELAY_MS); |
581 |
assertTrue(p1.isTerminated()); |
582 |
} |
583 |
finally { |
584 |
joinPool(p1); |
585 |
} |
586 |
} |
587 |
|
588 |
/** |
589 |
* completed submit of callable returns result |
590 |
*/ |
591 |
public void testSubmitCallable() throws Exception { |
592 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
593 |
try { |
594 |
Future<String> future = e.submit(new StringTask()); |
595 |
String result = future.get(); |
596 |
assertSame(TEST_STRING, result); |
597 |
} finally { |
598 |
joinPool(e); |
599 |
} |
600 |
} |
601 |
|
602 |
/** |
603 |
* completed submit of runnable returns successfully |
604 |
*/ |
605 |
public void testSubmitRunnable() throws Exception { |
606 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
607 |
try { |
608 |
Future<?> future = e.submit(new NoOpRunnable()); |
609 |
future.get(); |
610 |
assertTrue(future.isDone()); |
611 |
} finally { |
612 |
joinPool(e); |
613 |
} |
614 |
} |
615 |
|
616 |
/** |
617 |
* completed submit of (runnable, result) returns result |
618 |
*/ |
619 |
public void testSubmitRunnable2() throws Exception { |
620 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
621 |
try { |
622 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
623 |
String result = future.get(); |
624 |
assertSame(TEST_STRING, result); |
625 |
} finally { |
626 |
joinPool(e); |
627 |
} |
628 |
} |
629 |
|
630 |
/** |
631 |
* invokeAny(null) throws NPE |
632 |
*/ |
633 |
public void testInvokeAny1() throws Exception { |
634 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
635 |
try { |
636 |
e.invokeAny(null); |
637 |
shouldThrow(); |
638 |
} catch (NullPointerException success) { |
639 |
} finally { |
640 |
joinPool(e); |
641 |
} |
642 |
} |
643 |
|
644 |
/** |
645 |
* invokeAny(empty collection) throws IAE |
646 |
*/ |
647 |
public void testInvokeAny2() throws Exception { |
648 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
649 |
try { |
650 |
e.invokeAny(new ArrayList<Callable<String>>()); |
651 |
shouldThrow(); |
652 |
} catch (IllegalArgumentException success) { |
653 |
} finally { |
654 |
joinPool(e); |
655 |
} |
656 |
} |
657 |
|
658 |
/** |
659 |
* invokeAny(c) throws NPE if c has null elements |
660 |
*/ |
661 |
public void testInvokeAny3() throws Exception { |
662 |
final CountDownLatch latch = new CountDownLatch(1); |
663 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
664 |
try { |
665 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
666 |
l.add(new Callable<String>() { |
667 |
public String call() { |
668 |
try { |
669 |
latch.await(); |
670 |
} catch (InterruptedException quittingTime) {} |
671 |
return TEST_STRING; |
672 |
}}); |
673 |
l.add(null); |
674 |
e.invokeAny(l); |
675 |
shouldThrow(); |
676 |
} catch (NullPointerException success) { |
677 |
} finally { |
678 |
latch.countDown(); |
679 |
joinPool(e); |
680 |
} |
681 |
} |
682 |
|
683 |
/** |
684 |
* invokeAny(c) throws ExecutionException if no task completes |
685 |
*/ |
686 |
public void testInvokeAny4() throws Exception { |
687 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
688 |
try { |
689 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
690 |
l.add(new NPETask()); |
691 |
e.invokeAny(l); |
692 |
shouldThrow(); |
693 |
} catch (ExecutionException success) { |
694 |
assertTrue(success.getCause() instanceof NullPointerException); |
695 |
} finally { |
696 |
joinPool(e); |
697 |
} |
698 |
} |
699 |
|
700 |
/** |
701 |
* invokeAny(c) returns result of some task |
702 |
*/ |
703 |
public void testInvokeAny5() throws Exception { |
704 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
705 |
try { |
706 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
707 |
l.add(new StringTask()); |
708 |
l.add(new StringTask()); |
709 |
String result = e.invokeAny(l); |
710 |
assertSame(TEST_STRING, result); |
711 |
} finally { |
712 |
joinPool(e); |
713 |
} |
714 |
} |
715 |
|
716 |
/** |
717 |
* invokeAll(null) throws NPE |
718 |
*/ |
719 |
public void testInvokeAll1() throws Exception { |
720 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
721 |
try { |
722 |
e.invokeAll(null); |
723 |
shouldThrow(); |
724 |
} catch (NullPointerException success) { |
725 |
} finally { |
726 |
joinPool(e); |
727 |
} |
728 |
} |
729 |
|
730 |
/** |
731 |
* invokeAll(empty collection) returns empty collection |
732 |
*/ |
733 |
public void testInvokeAll2() throws Exception { |
734 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
735 |
try { |
736 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
737 |
assertTrue(r.isEmpty()); |
738 |
} finally { |
739 |
joinPool(e); |
740 |
} |
741 |
} |
742 |
|
743 |
/** |
744 |
* invokeAll(c) throws NPE if c has null elements |
745 |
*/ |
746 |
public void testInvokeAll3() throws Exception { |
747 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
748 |
try { |
749 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
750 |
l.add(new StringTask()); |
751 |
l.add(null); |
752 |
e.invokeAll(l); |
753 |
shouldThrow(); |
754 |
} catch (NullPointerException success) { |
755 |
} finally { |
756 |
joinPool(e); |
757 |
} |
758 |
} |
759 |
|
760 |
/** |
761 |
* get of invokeAll(c) throws exception on failed task |
762 |
*/ |
763 |
public void testInvokeAll4() throws Exception { |
764 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
765 |
try { |
766 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
767 |
l.add(new NPETask()); |
768 |
List<Future<String>> result = e.invokeAll(l); |
769 |
assertEquals(1, result.size()); |
770 |
for (Future<String> future : result) |
771 |
future.get(); |
772 |
shouldThrow(); |
773 |
} catch (ExecutionException success) { |
774 |
assertTrue(success.getCause() instanceof NullPointerException); |
775 |
} finally { |
776 |
joinPool(e); |
777 |
} |
778 |
} |
779 |
|
780 |
/** |
781 |
* invokeAll(c) returns results of all completed tasks |
782 |
*/ |
783 |
public void testInvokeAll5() throws Exception { |
784 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
785 |
try { |
786 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
787 |
l.add(new StringTask()); |
788 |
l.add(new StringTask()); |
789 |
List<Future<String>> result = e.invokeAll(l); |
790 |
assertEquals(2, result.size()); |
791 |
for (Future<String> future : result) |
792 |
assertSame(TEST_STRING, future.get()); |
793 |
} finally { |
794 |
joinPool(e); |
795 |
} |
796 |
} |
797 |
|
798 |
/** |
799 |
* timed invokeAny(null) throws NPE |
800 |
*/ |
801 |
public void testTimedInvokeAny1() throws Exception { |
802 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
803 |
try { |
804 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
805 |
shouldThrow(); |
806 |
} catch (NullPointerException success) { |
807 |
} finally { |
808 |
joinPool(e); |
809 |
} |
810 |
} |
811 |
|
812 |
/** |
813 |
* timed invokeAny(,,null) throws NPE |
814 |
*/ |
815 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
816 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
817 |
try { |
818 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
819 |
l.add(new StringTask()); |
820 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
821 |
shouldThrow(); |
822 |
} catch (NullPointerException success) { |
823 |
} finally { |
824 |
joinPool(e); |
825 |
} |
826 |
} |
827 |
|
828 |
/** |
829 |
* timed invokeAny(empty collection) throws IAE |
830 |
*/ |
831 |
public void testTimedInvokeAny2() throws Exception { |
832 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
833 |
try { |
834 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
835 |
shouldThrow(); |
836 |
} catch (IllegalArgumentException success) { |
837 |
} finally { |
838 |
joinPool(e); |
839 |
} |
840 |
} |
841 |
|
842 |
/** |
843 |
* timed invokeAny(c) throws NPE if c has null elements |
844 |
*/ |
845 |
public void testTimedInvokeAny3() throws Exception { |
846 |
final CountDownLatch latch = new CountDownLatch(1); |
847 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
848 |
try { |
849 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
850 |
l.add(new Callable<String>() { |
851 |
public String call() { |
852 |
try { |
853 |
latch.await(); |
854 |
} catch (InterruptedException quittingTime) {} |
855 |
return TEST_STRING; |
856 |
}}); |
857 |
l.add(null); |
858 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
859 |
shouldThrow(); |
860 |
} catch (NullPointerException success) { |
861 |
} finally { |
862 |
latch.countDown(); |
863 |
joinPool(e); |
864 |
} |
865 |
} |
866 |
|
867 |
/** |
868 |
* timed invokeAny(c) throws ExecutionException if no task completes |
869 |
*/ |
870 |
public void testTimedInvokeAny4() throws Exception { |
871 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
872 |
try { |
873 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
874 |
l.add(new NPETask()); |
875 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
876 |
shouldThrow(); |
877 |
} catch (ExecutionException success) { |
878 |
assertTrue(success.getCause() instanceof NullPointerException); |
879 |
} finally { |
880 |
joinPool(e); |
881 |
} |
882 |
} |
883 |
|
884 |
/** |
885 |
* timed invokeAny(c) returns result of some task |
886 |
*/ |
887 |
public void testTimedInvokeAny5() throws Exception { |
888 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
889 |
try { |
890 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
891 |
l.add(new StringTask()); |
892 |
l.add(new StringTask()); |
893 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
894 |
assertSame(TEST_STRING, result); |
895 |
} finally { |
896 |
joinPool(e); |
897 |
} |
898 |
} |
899 |
|
900 |
/** |
901 |
* timed invokeAll(null) throws NPE |
902 |
*/ |
903 |
public void testTimedInvokeAll1() throws Exception { |
904 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
905 |
try { |
906 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
907 |
shouldThrow(); |
908 |
} catch (NullPointerException success) { |
909 |
} finally { |
910 |
joinPool(e); |
911 |
} |
912 |
} |
913 |
|
914 |
/** |
915 |
* timed invokeAll(,,null) throws NPE |
916 |
*/ |
917 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
918 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
919 |
try { |
920 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
921 |
l.add(new StringTask()); |
922 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
923 |
shouldThrow(); |
924 |
} catch (NullPointerException success) { |
925 |
} finally { |
926 |
joinPool(e); |
927 |
} |
928 |
} |
929 |
|
930 |
/** |
931 |
* timed invokeAll(empty collection) returns empty collection |
932 |
*/ |
933 |
public void testTimedInvokeAll2() throws Exception { |
934 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
935 |
try { |
936 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
937 |
assertTrue(r.isEmpty()); |
938 |
} finally { |
939 |
joinPool(e); |
940 |
} |
941 |
} |
942 |
|
943 |
/** |
944 |
* timed invokeAll(c) throws NPE if c has null elements |
945 |
*/ |
946 |
public void testTimedInvokeAll3() throws Exception { |
947 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
948 |
try { |
949 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
950 |
l.add(new StringTask()); |
951 |
l.add(null); |
952 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
953 |
shouldThrow(); |
954 |
} catch (NullPointerException success) { |
955 |
} finally { |
956 |
joinPool(e); |
957 |
} |
958 |
} |
959 |
|
960 |
/** |
961 |
* get of element of invokeAll(c) throws exception on failed task |
962 |
*/ |
963 |
public void testTimedInvokeAll4() throws Exception { |
964 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
965 |
try { |
966 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
l.add(new NPETask()); |
968 |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
969 |
assertEquals(1, result.size()); |
970 |
for (Future<String> future : result) |
971 |
future.get(); |
972 |
shouldThrow(); |
973 |
} catch (ExecutionException success) { |
974 |
assertTrue(success.getCause() instanceof NullPointerException); |
975 |
} finally { |
976 |
joinPool(e); |
977 |
} |
978 |
} |
979 |
|
980 |
/** |
981 |
* timed invokeAll(c) returns results of all completed tasks |
982 |
*/ |
983 |
public void testTimedInvokeAll5() throws Exception { |
984 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
985 |
try { |
986 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
987 |
l.add(new StringTask()); |
988 |
l.add(new StringTask()); |
989 |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
990 |
assertEquals(2, result.size()); |
991 |
for (Future<String> future : result) |
992 |
assertSame(TEST_STRING, future.get()); |
993 |
} finally { |
994 |
joinPool(e); |
995 |
} |
996 |
} |
997 |
|
998 |
/** |
999 |
* timed invokeAll(c) cancels tasks not completed by timeout |
1000 |
*/ |
1001 |
public void testTimedInvokeAll6() throws Exception { |
1002 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1003 |
try { |
1004 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1005 |
l.add(new StringTask()); |
1006 |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1007 |
l.add(new StringTask()); |
1008 |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1009 |
assertEquals(3, result.size()); |
1010 |
Iterator<Future<String>> it = result.iterator(); |
1011 |
Future<String> f1 = it.next(); |
1012 |
Future<String> f2 = it.next(); |
1013 |
Future<String> f3 = it.next(); |
1014 |
assertTrue(f1.isDone()); |
1015 |
assertTrue(f2.isDone()); |
1016 |
assertTrue(f3.isDone()); |
1017 |
assertFalse(f1.isCancelled()); |
1018 |
assertTrue(f2.isCancelled()); |
1019 |
} finally { |
1020 |
joinPool(e); |
1021 |
} |
1022 |
} |
1023 |
|
1024 |
|
1025 |
} |