1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class ScheduledExecutorTest extends TestCase{ |
13 |
|
14 |
boolean flag = false; |
15 |
|
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run (suite()); |
18 |
} |
19 |
|
20 |
|
21 |
public static Test suite() { |
22 |
return new TestSuite(ScheduledExecutorTest.class); |
23 |
} |
24 |
|
25 |
private static long SHORT_DELAY_MS = 100; |
26 |
private static long MEDIUM_DELAY_MS = 1000; |
27 |
private static long LONG_DELAY_MS = 10000; |
28 |
|
29 |
static class MyRunnable implements Runnable { |
30 |
volatile boolean waiting = true; |
31 |
volatile boolean done = false; |
32 |
public void run(){ |
33 |
try{ |
34 |
Thread.sleep(SHORT_DELAY_MS); |
35 |
waiting = false; |
36 |
done = true; |
37 |
} catch(Exception e){ |
38 |
} |
39 |
} |
40 |
} |
41 |
|
42 |
static class MyCallable implements Callable { |
43 |
volatile boolean waiting = true; |
44 |
volatile boolean done = false; |
45 |
public Object call(){ |
46 |
try{ |
47 |
Thread.sleep(SHORT_DELAY_MS); |
48 |
waiting = false; |
49 |
done = true; |
50 |
}catch(Exception e){} |
51 |
return Boolean.TRUE; |
52 |
} |
53 |
} |
54 |
|
55 |
public Runnable newRunnable(){ |
56 |
return new Runnable(){ |
57 |
public void run(){ |
58 |
try{Thread.sleep(SHORT_DELAY_MS); |
59 |
} catch(Exception e){ |
60 |
} |
61 |
} |
62 |
}; |
63 |
} |
64 |
|
65 |
public Runnable newNoopRunnable() { |
66 |
return new Runnable(){ |
67 |
public void run(){ |
68 |
} |
69 |
}; |
70 |
} |
71 |
|
72 |
/** |
73 |
* Test to verify execute successfully runs the given Runnable |
74 |
*/ |
75 |
public void testExecute(){ |
76 |
try{ |
77 |
MyRunnable runnable =new MyRunnable(); |
78 |
ScheduledExecutor one = new ScheduledExecutor(1); |
79 |
one.execute(runnable); |
80 |
Thread.sleep(SHORT_DELAY_MS/2); |
81 |
assertTrue(runnable.waiting); |
82 |
one.shutdown(); |
83 |
try{ |
84 |
Thread.sleep(MEDIUM_DELAY_MS); |
85 |
} catch(InterruptedException e){ |
86 |
fail("unexpected exception"); |
87 |
} |
88 |
assertFalse(runnable.waiting); |
89 |
assertTrue(runnable.done); |
90 |
one.shutdown(); |
91 |
} |
92 |
catch(Exception e){ |
93 |
fail("unexpected exception"); |
94 |
} |
95 |
} |
96 |
|
97 |
/** |
98 |
* Test to verify schedule successfully runs the given Callable. |
99 |
* The waiting flag shows that the Callable is not started until |
100 |
* immediately. |
101 |
*/ |
102 |
public void testSchedule1(){ |
103 |
try{ |
104 |
MyCallable callable = new MyCallable(); |
105 |
ScheduledExecutor one = new ScheduledExecutor(1); |
106 |
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
107 |
assertTrue(callable.waiting); |
108 |
Thread.sleep(MEDIUM_DELAY_MS); |
109 |
assertTrue(callable.done); |
110 |
assertEquals(Boolean.TRUE, f.get()); |
111 |
one.shutdown(); |
112 |
}catch(RejectedExecutionException e){} |
113 |
catch(Exception e){ |
114 |
fail("unexpected exception"); |
115 |
} |
116 |
} |
117 |
|
118 |
/** |
119 |
* Another version of schedule, only using Runnable instead of Callable |
120 |
*/ |
121 |
public void testSchedule3(){ |
122 |
try{ |
123 |
MyRunnable runnable = new MyRunnable(); |
124 |
ScheduledExecutor one = new ScheduledExecutor(1); |
125 |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
126 |
Thread.sleep(SHORT_DELAY_MS/2); |
127 |
assertTrue(runnable.waiting); |
128 |
Thread.sleep(MEDIUM_DELAY_MS); |
129 |
assertTrue(runnable.done); |
130 |
one.shutdown(); |
131 |
} catch(Exception e){ |
132 |
fail("unexpected exception"); |
133 |
} |
134 |
} |
135 |
|
136 |
/** |
137 |
* The final version of schedule, using both long, TimeUnit and Runnable |
138 |
*/ |
139 |
public void testSchedule4(){ |
140 |
try{ |
141 |
MyRunnable runnable = new MyRunnable(); |
142 |
ScheduledExecutor one = new ScheduledExecutor(1); |
143 |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
144 |
// Thread.sleep(505); |
145 |
assertTrue(runnable.waiting); |
146 |
Thread.sleep(MEDIUM_DELAY_MS); |
147 |
assertTrue(runnable.done); |
148 |
one.shutdown(); |
149 |
} catch(Exception e){ |
150 |
fail("unexpected exception"); |
151 |
} |
152 |
} |
153 |
|
154 |
|
155 |
// exception tests |
156 |
|
157 |
/** |
158 |
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
159 |
* This occurs on an attempt to schedule a task on a shutdown executor |
160 |
*/ |
161 |
public void testSchedule1_RejectedExecutionException(){ |
162 |
try{ |
163 |
ScheduledExecutor se = new ScheduledExecutor(1); |
164 |
se.shutdown(); |
165 |
se.schedule(new Runnable(){ |
166 |
public void run(){} |
167 |
}, 10000, TimeUnit.MILLISECONDS); |
168 |
fail("shoud throw"); |
169 |
}catch(RejectedExecutionException e){} |
170 |
} |
171 |
|
172 |
/** |
173 |
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
174 |
* This occurs on an attempt to schedule a task on a shutdown executor |
175 |
*/ |
176 |
public void testSchedule2_RejectedExecutionException(){ |
177 |
try{ |
178 |
ScheduledExecutor se = new ScheduledExecutor(1); |
179 |
se.shutdown(); |
180 |
se.schedule(new Callable(){ |
181 |
public Object call(){ |
182 |
return Boolean.TRUE; |
183 |
} |
184 |
}, (long)100, TimeUnit.SECONDS); |
185 |
fail("should throw"); |
186 |
}catch(RejectedExecutionException e){} |
187 |
} |
188 |
|
189 |
/** |
190 |
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
191 |
* This occurs on an attempt to schedule a task on a shutdown executor |
192 |
*/ |
193 |
public void testSchedule3_RejectedExecutionException(){ |
194 |
try{ |
195 |
ScheduledExecutor se = new ScheduledExecutor(1); |
196 |
se.shutdown(); |
197 |
se.schedule(new Callable(){ |
198 |
public Object call(){ |
199 |
return Boolean.TRUE; |
200 |
} |
201 |
}, 10000, TimeUnit.MILLISECONDS); |
202 |
fail("should throw"); |
203 |
}catch(RejectedExecutionException e){} |
204 |
} |
205 |
|
206 |
/** |
207 |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
208 |
* RejectedExecutionException. |
209 |
* This occurs on an attempt to schedule a task on a shutdown executor |
210 |
*/ |
211 |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
212 |
try{ |
213 |
ScheduledExecutor se = new ScheduledExecutor(1); |
214 |
se.shutdown(); |
215 |
se.scheduleAtFixedRate(new Runnable(){ |
216 |
public void run(){} |
217 |
}, 100, 100, TimeUnit.SECONDS); |
218 |
fail("should throw"); |
219 |
}catch(RejectedExecutionException e){} |
220 |
} |
221 |
|
222 |
/** |
223 |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
224 |
* RejectedExecutionException. |
225 |
* This occurs on an attempt to schedule a task on a shutdown executor |
226 |
*/ |
227 |
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
228 |
try{ |
229 |
ScheduledExecutor se = new ScheduledExecutor(1); |
230 |
se.shutdown(); |
231 |
se.scheduleAtFixedRate(new Runnable(){ |
232 |
public void run(){} |
233 |
}, 1, 100, TimeUnit.SECONDS); |
234 |
fail("should throw"); |
235 |
}catch(RejectedExecutionException e){} |
236 |
} |
237 |
|
238 |
/** |
239 |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
240 |
* RejectedExecutionException. |
241 |
* This occurs on an attempt to schedule a task on a shutdown executor |
242 |
*/ |
243 |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
244 |
try{ |
245 |
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
se.shutdown(); |
247 |
se.scheduleWithFixedDelay(new Runnable(){ |
248 |
public void run(){} |
249 |
}, 100, 100, TimeUnit.SECONDS); |
250 |
fail("should throw"); |
251 |
}catch(RejectedExecutionException e){} |
252 |
} |
253 |
|
254 |
/** |
255 |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
256 |
* RejectedExecutionException. |
257 |
* This occurs on an attempt to schedule a task on a shutdown executor |
258 |
*/ |
259 |
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
260 |
try{ |
261 |
ScheduledExecutor se = new ScheduledExecutor(1); |
262 |
se.shutdown(); |
263 |
se.scheduleWithFixedDelay(new Runnable(){ |
264 |
public void run(){} |
265 |
}, 1, 100, TimeUnit.SECONDS); |
266 |
fail("should throw"); |
267 |
}catch(RejectedExecutionException e){} |
268 |
} |
269 |
|
270 |
/** |
271 |
* Test to verify execute throws RejectedExecutionException |
272 |
* This occurs on an attempt to schedule a task on a shutdown executor |
273 |
*/ |
274 |
public void testExecute_RejectedExecutionException(){ |
275 |
try{ |
276 |
ScheduledExecutor se = new ScheduledExecutor(1); |
277 |
se.shutdown(); |
278 |
se.execute(new Runnable(){ |
279 |
public void run(){} |
280 |
}); |
281 |
fail("should throw"); |
282 |
}catch(RejectedExecutionException e){} |
283 |
} |
284 |
|
285 |
|
286 |
|
287 |
/** |
288 |
* Test to verify getActiveCount gives correct values |
289 |
*/ |
290 |
public void testGetActiveCount(){ |
291 |
ScheduledExecutor two = new ScheduledExecutor(2); |
292 |
try { |
293 |
assertEquals(0, two.getActiveCount()); |
294 |
two.execute(newRunnable()); |
295 |
try{ |
296 |
Thread.sleep(SHORT_DELAY_MS/2); |
297 |
} catch(Exception e){ |
298 |
fail("unexpected exception"); |
299 |
} |
300 |
assertEquals(1, two.getActiveCount()); |
301 |
} finally { |
302 |
two.shutdown(); |
303 |
} |
304 |
} |
305 |
|
306 |
/** |
307 |
* Test to verify getCompleteTaskCount gives correct values |
308 |
*/ |
309 |
public void testGetCompletedTaskCount(){ |
310 |
ScheduledExecutor two = new ScheduledExecutor(2); |
311 |
try { |
312 |
assertEquals(0, two.getCompletedTaskCount()); |
313 |
two.execute(newRunnable()); |
314 |
try{ |
315 |
Thread.sleep(MEDIUM_DELAY_MS); |
316 |
} catch(Exception e){ |
317 |
fail("unexpected exception"); |
318 |
} |
319 |
assertEquals(1, two.getCompletedTaskCount()); |
320 |
} finally { |
321 |
two.shutdown(); |
322 |
} |
323 |
} |
324 |
|
325 |
/** |
326 |
* Test to verify getCorePoolSize gives correct values |
327 |
*/ |
328 |
public void testGetCorePoolSize(){ |
329 |
ScheduledExecutor one = new ScheduledExecutor(1); |
330 |
try { |
331 |
assertEquals(1, one.getCorePoolSize()); |
332 |
} finally { |
333 |
one.shutdown(); |
334 |
} |
335 |
} |
336 |
|
337 |
/** |
338 |
* Test to verify getLargestPoolSize gives correct values |
339 |
*/ |
340 |
public void testGetLargestPoolSize(){ |
341 |
ScheduledExecutor two = new ScheduledExecutor(2); |
342 |
try { |
343 |
assertEquals(0, two.getLargestPoolSize()); |
344 |
two.execute(newRunnable()); |
345 |
two.execute(newRunnable()); |
346 |
try{ |
347 |
Thread.sleep(SHORT_DELAY_MS); |
348 |
} catch(Exception e){ |
349 |
fail("unexpected exception"); |
350 |
} |
351 |
assertEquals(2, two.getLargestPoolSize()); |
352 |
} finally { |
353 |
two.shutdown(); |
354 |
} |
355 |
} |
356 |
|
357 |
/** |
358 |
* Test to verify getPoolSize gives correct values |
359 |
*/ |
360 |
public void testGetPoolSize(){ |
361 |
ScheduledExecutor one = new ScheduledExecutor(1); |
362 |
try { |
363 |
assertEquals(0, one.getPoolSize()); |
364 |
one.execute(newRunnable()); |
365 |
assertEquals(1, one.getPoolSize()); |
366 |
} finally { |
367 |
one.shutdown(); |
368 |
} |
369 |
} |
370 |
|
371 |
/** |
372 |
* Test to verify getTaskCount gives correct values |
373 |
*/ |
374 |
public void testGetTaskCount(){ |
375 |
ScheduledExecutor one = new ScheduledExecutor(1); |
376 |
try { |
377 |
assertEquals(0, one.getTaskCount()); |
378 |
for(int i = 0; i < 5; i++) |
379 |
one.execute(newRunnable()); |
380 |
try{ |
381 |
Thread.sleep(SHORT_DELAY_MS); |
382 |
} catch(Exception e){ |
383 |
fail("unexpected exception"); |
384 |
} |
385 |
assertEquals(5, one.getTaskCount()); |
386 |
} finally { |
387 |
one.shutdown(); |
388 |
} |
389 |
} |
390 |
|
391 |
/** |
392 |
* Test to verify isShutDown gives correct values |
393 |
*/ |
394 |
public void testIsShutdown(){ |
395 |
|
396 |
ScheduledExecutor one = new ScheduledExecutor(1); |
397 |
try { |
398 |
assertFalse(one.isShutdown()); |
399 |
} |
400 |
finally { |
401 |
one.shutdown(); |
402 |
} |
403 |
assertTrue(one.isShutdown()); |
404 |
} |
405 |
|
406 |
|
407 |
/** |
408 |
* Test to verify isTerminated gives correct values |
409 |
* Makes sure termination does not take an innapropriate |
410 |
* amount of time |
411 |
*/ |
412 |
public void testIsTerminated(){ |
413 |
ScheduledExecutor one = new ScheduledExecutor(1); |
414 |
try { |
415 |
one.execute(newRunnable()); |
416 |
} finally { |
417 |
one.shutdown(); |
418 |
} |
419 |
boolean flag = false; |
420 |
try{ |
421 |
flag = one.awaitTermination(10, TimeUnit.SECONDS); |
422 |
} catch(Exception e){ |
423 |
fail("unexpected exception"); |
424 |
} |
425 |
assertTrue(one.isTerminated()); |
426 |
if(!flag) |
427 |
fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); |
428 |
} |
429 |
|
430 |
/** |
431 |
* Test to verify that purge correctly removes cancelled tasks |
432 |
* from the queue |
433 |
*/ |
434 |
public void testPurge(){ |
435 |
ScheduledExecutor one = new ScheduledExecutor(1); |
436 |
try { |
437 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
438 |
for(int i = 0; i < 5; i++){ |
439 |
tasks[i] = one.schedule(newRunnable(), 1, TimeUnit.MILLISECONDS); |
440 |
} |
441 |
int max = 5; |
442 |
if (tasks[4].cancel(true)) --max; |
443 |
if (tasks[3].cancel(true)) --max; |
444 |
one.purge(); |
445 |
long count = one.getTaskCount(); |
446 |
assertTrue(count > 0 && count <= max); |
447 |
} finally { |
448 |
one.shutdown(); |
449 |
} |
450 |
} |
451 |
|
452 |
/** |
453 |
* Test to verify shutDownNow returns a list |
454 |
* containing the correct number of elements |
455 |
*/ |
456 |
public void testShutDownNow(){ |
457 |
ScheduledExecutor one = new ScheduledExecutor(1); |
458 |
for(int i = 0; i < 5; i++) |
459 |
one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
460 |
List l = one.shutdownNow(); |
461 |
assertTrue(one.isShutdown()); |
462 |
assertTrue(l.size() > 0 && l.size() <= 5); |
463 |
} |
464 |
|
465 |
public void testShutDown1(){ |
466 |
try { |
467 |
ScheduledExecutor one = new ScheduledExecutor(1); |
468 |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
469 |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
470 |
|
471 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
472 |
for(int i = 0; i < 5; i++) |
473 |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
474 |
one.shutdown(); |
475 |
BlockingQueue q = one.getQueue(); |
476 |
for (Iterator it = q.iterator(); it.hasNext();) { |
477 |
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
478 |
assertFalse(t.isCancelled()); |
479 |
} |
480 |
assertTrue(one.isShutdown()); |
481 |
Thread.sleep(SHORT_DELAY_MS); |
482 |
for (int i = 0; i < 5; ++i) { |
483 |
assertTrue(tasks[i].isDone()); |
484 |
assertFalse(tasks[i].isCancelled()); |
485 |
} |
486 |
|
487 |
} |
488 |
catch(Exception ex) { |
489 |
fail("unexpected exception"); |
490 |
} |
491 |
} |
492 |
|
493 |
|
494 |
public void testShutDown2(){ |
495 |
try { |
496 |
ScheduledExecutor one = new ScheduledExecutor(1); |
497 |
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
498 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
499 |
for(int i = 0; i < 5; i++) |
500 |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
501 |
one.shutdown(); |
502 |
assertTrue(one.isShutdown()); |
503 |
BlockingQueue q = one.getQueue(); |
504 |
assertTrue(q.isEmpty()); |
505 |
Thread.sleep(SHORT_DELAY_MS); |
506 |
assertTrue(one.isTerminated()); |
507 |
} |
508 |
catch(Exception ex) { |
509 |
fail("unexpected exception"); |
510 |
} |
511 |
} |
512 |
|
513 |
|
514 |
public void testShutDown3(){ |
515 |
try { |
516 |
ScheduledExecutor one = new ScheduledExecutor(1); |
517 |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
518 |
ScheduledCancellable task = |
519 |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
520 |
one.shutdown(); |
521 |
assertTrue(one.isShutdown()); |
522 |
BlockingQueue q = one.getQueue(); |
523 |
assertTrue(q.isEmpty()); |
524 |
Thread.sleep(SHORT_DELAY_MS); |
525 |
assertTrue(one.isTerminated()); |
526 |
} |
527 |
catch(Exception ex) { |
528 |
fail("unexpected exception"); |
529 |
} |
530 |
} |
531 |
|
532 |
public void testShutDown4(){ |
533 |
ScheduledExecutor one = new ScheduledExecutor(1); |
534 |
try { |
535 |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
536 |
ScheduledCancellable task = |
537 |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
538 |
assertFalse(task.isCancelled()); |
539 |
one.shutdown(); |
540 |
assertFalse(task.isCancelled()); |
541 |
assertFalse(one.isTerminated()); |
542 |
assertTrue(one.isShutdown()); |
543 |
Thread.sleep(SHORT_DELAY_MS); |
544 |
assertFalse(task.isCancelled()); |
545 |
task.cancel(true); |
546 |
assertTrue(task.isCancelled()); |
547 |
Thread.sleep(SHORT_DELAY_MS); |
548 |
assertTrue(one.isTerminated()); |
549 |
} |
550 |
catch(Exception ex) { |
551 |
fail("unexpected exception"); |
552 |
} |
553 |
finally { |
554 |
one.shutdownNow(); |
555 |
} |
556 |
} |
557 |
|
558 |
} |