17 |
|
return new TestSuite(ScheduledExecutorTest.class); |
18 |
|
} |
19 |
|
|
20 |
– |
static class MyRunnable implements Runnable { |
21 |
– |
volatile boolean done = false; |
22 |
– |
public void run() { |
23 |
– |
try { |
24 |
– |
Thread.sleep(SMALL_DELAY_MS); |
25 |
– |
done = true; |
26 |
– |
} catch(Exception e){ |
27 |
– |
} |
28 |
– |
} |
29 |
– |
} |
30 |
– |
|
31 |
– |
static class MyCallable implements Callable { |
32 |
– |
volatile boolean done = false; |
33 |
– |
public Object call() { |
34 |
– |
try { |
35 |
– |
Thread.sleep(SMALL_DELAY_MS); |
36 |
– |
done = true; |
37 |
– |
} catch(Exception e){ |
38 |
– |
} |
39 |
– |
return Boolean.TRUE; |
40 |
– |
} |
41 |
– |
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* |
22 |
> |
* execute successfully executes a runnable |
23 |
|
*/ |
24 |
|
public void testExecute() { |
25 |
|
try { |
26 |
< |
MyRunnable runnable =new MyRunnable(); |
26 |
> |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
27 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
28 |
|
p1.execute(runnable); |
29 |
|
assertFalse(runnable.done); |
44 |
|
|
45 |
|
} |
46 |
|
|
47 |
+ |
|
48 |
|
/** |
49 |
< |
* |
49 |
> |
* delayed schedule of callable successfully executes after delay |
50 |
|
*/ |
51 |
|
public void testSchedule1() { |
52 |
|
try { |
53 |
< |
MyCallable callable = new MyCallable(); |
53 |
> |
TrackedCallable callable = new TrackedCallable(); |
54 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
55 |
|
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
56 |
|
assertFalse(callable.done); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* |
69 |
> |
* delayed schedule of runnable successfully executes after delay |
70 |
|
*/ |
71 |
|
public void testSchedule3() { |
72 |
|
try { |
73 |
< |
MyRunnable runnable = new MyRunnable(); |
73 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
74 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
75 |
|
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
76 |
|
Thread.sleep(SHORT_DELAY_MS); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* |
88 |
> |
* scheduleAtFixedRate executes runnable after given initial delay |
89 |
|
*/ |
90 |
|
public void testSchedule4() { |
91 |
|
try { |
92 |
< |
MyRunnable runnable = new MyRunnable(); |
92 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
93 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
94 |
> |
ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
95 |
> |
assertFalse(runnable.done); |
96 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
97 |
> |
assertTrue(runnable.done); |
98 |
> |
h.cancel(true); |
99 |
> |
p1.shutdown(); |
100 |
> |
joinPool(p1); |
101 |
> |
} catch(Exception e){ |
102 |
> |
unexpectedException(); |
103 |
> |
} |
104 |
> |
} |
105 |
> |
|
106 |
> |
/** |
107 |
> |
* scheduleWithFixedDelay executes runnable after given initial delay |
108 |
> |
*/ |
109 |
> |
public void testSchedule5() { |
110 |
> |
try { |
111 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
112 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
113 |
< |
p1.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
113 |
> |
ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
|
assertFalse(runnable.done); |
115 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
116 |
|
assertTrue(runnable.done); |
117 |
+ |
h.cancel(true); |
118 |
|
p1.shutdown(); |
119 |
|
joinPool(p1); |
120 |
|
} catch(Exception e){ |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
< |
|
126 |
< |
// exception tests |
125 |
> |
/** |
126 |
> |
* execute (null) throws NPE |
127 |
> |
*/ |
128 |
> |
public void testExecuteNull() { |
129 |
> |
ScheduledExecutor se = null; |
130 |
> |
try { |
131 |
> |
se = new ScheduledExecutor(1); |
132 |
> |
se.execute(null); |
133 |
> |
shouldThrow(); |
134 |
> |
} catch(NullPointerException success){} |
135 |
> |
catch(Exception e){ |
136 |
> |
unexpectedException(); |
137 |
> |
} |
138 |
> |
|
139 |
> |
joinPool(se); |
140 |
> |
} |
141 |
|
|
142 |
|
/** |
143 |
< |
* schedule(Runnable, long) throws RejectedExecutionException |
144 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
143 |
> |
* schedule (null) throws NPE |
144 |
> |
*/ |
145 |
> |
public void testScheduleNull() { |
146 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
147 |
> |
try { |
148 |
> |
TrackedCallable callable = null; |
149 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
150 |
> |
shouldThrow(); |
151 |
> |
} catch(NullPointerException success){} |
152 |
> |
catch(Exception e){ |
153 |
> |
unexpectedException(); |
154 |
> |
} |
155 |
> |
joinPool(se); |
156 |
> |
} |
157 |
> |
|
158 |
> |
/** |
159 |
> |
* execute throws RejectedExecutionException if shutdown |
160 |
|
*/ |
161 |
|
public void testSchedule1_RejectedExecutionException() { |
162 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
148 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
175 |
> |
* schedule throws RejectedExecutionException if shutdown |
176 |
|
*/ |
177 |
|
public void testSchedule2_RejectedExecutionException() { |
178 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* schedule(Callable, long) throws RejectedExecutionException |
164 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
190 |
> |
* schedule callable throws RejectedExecutionException if shutdown |
191 |
|
*/ |
192 |
|
public void testSchedule3_RejectedExecutionException() { |
193 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
180 |
< |
* RejectedExecutionException. |
181 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
205 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
206 |
|
*/ |
207 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
208 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
197 |
< |
* RejectedExecutionException. |
198 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
199 |
< |
*/ |
200 |
< |
public void testScheduleAtFixedRate2_RejectedExecutionException() { |
201 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
202 |
< |
try { |
203 |
< |
se.shutdown(); |
204 |
< |
se.scheduleAtFixedRate(new NoOpRunnable(), |
205 |
< |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
206 |
< |
shouldThrow(); |
207 |
< |
} catch(RejectedExecutionException success){ |
208 |
< |
} |
209 |
< |
joinPool(se); |
210 |
< |
} |
211 |
< |
|
212 |
< |
/** |
213 |
< |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
214 |
< |
* RejectedExecutionException. |
215 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
220 |
> |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
221 |
|
*/ |
222 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
223 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
236 |
< |
* RejectedExecutionException. |
232 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
233 |
< |
*/ |
234 |
< |
public void testScheduleWithFixedDelay2_RejectedExecutionException() { |
235 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
236 |
< |
try { |
237 |
< |
se.shutdown(); |
238 |
< |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
239 |
< |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
240 |
< |
shouldThrow(); |
241 |
< |
} catch(RejectedExecutionException success){ |
242 |
< |
} |
243 |
< |
joinPool(se); |
244 |
< |
} |
245 |
< |
|
246 |
< |
/** |
247 |
< |
* execute throws RejectedExecutionException |
248 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
249 |
< |
*/ |
250 |
< |
public void testExecute_RejectedExecutionException() { |
251 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
252 |
< |
try { |
253 |
< |
se.shutdown(); |
254 |
< |
se.execute(new NoOpRunnable()); |
255 |
< |
shouldThrow(); |
256 |
< |
} catch(RejectedExecutionException success){ |
257 |
< |
} |
258 |
< |
joinPool(se); |
259 |
< |
} |
260 |
< |
|
261 |
< |
/** |
262 |
< |
* getActiveCount gives correct values |
235 |
> |
* getActiveCount increases but doesn't overestimate, when a |
236 |
> |
* thread becomes active |
237 |
|
*/ |
238 |
|
public void testGetActiveCount() { |
239 |
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
< |
* getCompleteTaskCount gives correct values |
252 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
253 |
> |
* when tasks complete |
254 |
|
*/ |
255 |
|
public void testGetCompletedTaskCount() { |
256 |
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
< |
* getCorePoolSize gives correct values |
269 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
270 |
|
*/ |
271 |
|
public void testGetCorePoolSize() { |
272 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
< |
* getLargestPoolSize gives correct values |
278 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
279 |
> |
* multiple threads active |
280 |
|
*/ |
281 |
|
public void testGetLargestPoolSize() { |
282 |
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* getPoolSize gives correct values |
296 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
297 |
> |
* become active |
298 |
|
*/ |
299 |
|
public void testGetPoolSize() { |
300 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
< |
* getTaskCount gives correct values |
308 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
309 |
> |
* submitted |
310 |
|
*/ |
311 |
|
public void testGetTaskCount() { |
312 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
321 |
|
assertEquals(5, p1.getTaskCount()); |
322 |
|
joinPool(p1); |
323 |
|
} |
324 |
+ |
|
325 |
+ |
/** |
326 |
+ |
* getThreadFactory returns factory in constructor if not set |
327 |
+ |
*/ |
328 |
+ |
public void testGetThreadFactory() { |
329 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
330 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1, tf); |
331 |
+ |
assertSame(tf, p.getThreadFactory()); |
332 |
+ |
p.shutdown(); |
333 |
+ |
joinPool(p); |
334 |
+ |
} |
335 |
+ |
|
336 |
+ |
/** |
337 |
+ |
* setThreadFactory sets the thread factory returned by getThreadFactory |
338 |
+ |
*/ |
339 |
+ |
public void testSetThreadFactory() { |
340 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
341 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1); |
342 |
+ |
p.setThreadFactory(tf); |
343 |
+ |
assertSame(tf, p.getThreadFactory()); |
344 |
+ |
p.shutdown(); |
345 |
+ |
joinPool(p); |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
/** |
349 |
+ |
* setThreadFactory(null) throws NPE |
350 |
+ |
*/ |
351 |
+ |
public void testSetThreadFactoryNull() { |
352 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1); |
353 |
+ |
try { |
354 |
+ |
p.setThreadFactory(null); |
355 |
+ |
shouldThrow(); |
356 |
+ |
} catch (NullPointerException success) { |
357 |
+ |
} finally { |
358 |
+ |
joinPool(p); |
359 |
+ |
} |
360 |
+ |
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* isShutDown gives correct values |
363 |
> |
* is isShutDown is false before shutdown, true after |
364 |
|
*/ |
365 |
|
public void testIsShutdown() { |
366 |
|
|
376 |
|
|
377 |
|
|
378 |
|
/** |
379 |
< |
* isTerminated gives correct values |
379 |
> |
* isTerminated is false before termination, true after |
380 |
|
*/ |
381 |
|
public void testIsTerminated() { |
382 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
< |
* isTerminating gives correct values |
397 |
> |
* isTerminating is not true when running or when terminated |
398 |
|
*/ |
399 |
|
public void testIsTerminating() { |
400 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* that purge correctly removes cancelled tasks |
419 |
< |
* from the queue |
418 |
> |
* getQueue returns the work queue, which contains queued tasks |
419 |
> |
*/ |
420 |
> |
public void testGetQueue() { |
421 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
422 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
423 |
> |
for(int i = 0; i < 5; i++){ |
424 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
425 |
> |
} |
426 |
> |
try { |
427 |
> |
Thread.sleep(SHORT_DELAY_MS); |
428 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
429 |
> |
assertTrue(q.contains(tasks[4])); |
430 |
> |
assertFalse(q.contains(tasks[0])); |
431 |
> |
p1.shutdownNow(); |
432 |
> |
} catch(Exception e) { |
433 |
> |
unexpectedException(); |
434 |
> |
} finally { |
435 |
> |
joinPool(p1); |
436 |
> |
} |
437 |
> |
} |
438 |
> |
|
439 |
> |
/** |
440 |
> |
* remove(task) removes queued task, and fails to remove active task |
441 |
> |
*/ |
442 |
> |
public void testRemove() { |
443 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
444 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
445 |
> |
for(int i = 0; i < 5; i++){ |
446 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
447 |
> |
} |
448 |
> |
try { |
449 |
> |
Thread.sleep(SHORT_DELAY_MS); |
450 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
451 |
> |
assertFalse(p1.remove((Runnable)tasks[0])); |
452 |
> |
assertTrue(q.contains((Runnable)tasks[4])); |
453 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
454 |
> |
assertTrue(p1.remove((Runnable)tasks[4])); |
455 |
> |
assertFalse(p1.remove((Runnable)tasks[4])); |
456 |
> |
assertFalse(q.contains((Runnable)tasks[4])); |
457 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
458 |
> |
assertTrue(p1.remove((Runnable)tasks[3])); |
459 |
> |
assertFalse(q.contains((Runnable)tasks[3])); |
460 |
> |
p1.shutdownNow(); |
461 |
> |
} catch(Exception e) { |
462 |
> |
unexpectedException(); |
463 |
> |
} finally { |
464 |
> |
joinPool(p1); |
465 |
> |
} |
466 |
> |
} |
467 |
> |
|
468 |
> |
/** |
469 |
> |
* purge removes cancelled tasks from the queue |
470 |
|
*/ |
471 |
|
public void testPurge() { |
472 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
473 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
474 |
|
for(int i = 0; i < 5; i++){ |
475 |
< |
tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
475 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
476 |
|
} |
477 |
|
int max = 5; |
478 |
|
if (tasks[4].cancel(true)) --max; |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
< |
* shutDownNow returns a list |
423 |
< |
* containing the correct number of elements |
487 |
> |
* shutDownNow returns a list containing tasks that were not run |
488 |
|
*/ |
489 |
|
public void testShutDownNow() { |
490 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
491 |
|
for(int i = 0; i < 5; i++) |
492 |
< |
p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
492 |
> |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
|
List l = p1.shutdownNow(); |
494 |
|
assertTrue(p1.isShutdown()); |
495 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
< |
* |
500 |
> |
* In default setting, shutdown cancels periodic but not delayed |
501 |
> |
* tasks at shutdown |
502 |
|
*/ |
503 |
|
public void testShutDown1() { |
504 |
|
try { |
530 |
|
|
531 |
|
|
532 |
|
/** |
533 |
< |
* |
533 |
> |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
534 |
> |
* delayed tasks are cancelled at shutdown |
535 |
|
*/ |
536 |
|
public void testShutDown2() { |
537 |
|
try { |
554 |
|
|
555 |
|
|
556 |
|
/** |
557 |
< |
* |
557 |
> |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
558 |
> |
* periodic tasks are not cancelled at shutdown |
559 |
|
*/ |
560 |
|
public void testShutDown3() { |
561 |
|
try { |
576 |
|
} |
577 |
|
|
578 |
|
/** |
579 |
< |
* |
579 |
> |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
580 |
> |
* periodic tasks are cancelled at shutdown |
581 |
|
*/ |
582 |
|
public void testShutDown4() { |
583 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |