1 |
dl |
1.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 |
dl |
1.4 |
public class ScheduledExecutorTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ScheduledExecutorTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
static class MyRunnable implements Runnable { |
21 |
|
|
volatile boolean done = false; |
22 |
dl |
1.5 |
public void run() { |
23 |
|
|
try { |
24 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
25 |
dl |
1.1 |
done = true; |
26 |
dl |
1.2 |
} catch(Exception e){ |
27 |
|
|
} |
28 |
dl |
1.1 |
} |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
static class MyCallable implements Callable { |
32 |
|
|
volatile boolean done = false; |
33 |
dl |
1.5 |
public Object call() { |
34 |
|
|
try { |
35 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
36 |
dl |
1.1 |
done = true; |
37 |
dl |
1.5 |
} catch(Exception e){ |
38 |
|
|
} |
39 |
dl |
1.1 |
return Boolean.TRUE; |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
|
|
43 |
dl |
1.5 |
/** |
44 |
dl |
1.6 |
* execute successfully executes a runnable |
45 |
dl |
1.5 |
*/ |
46 |
|
|
public void testExecute() { |
47 |
|
|
try { |
48 |
dl |
1.1 |
MyRunnable runnable =new MyRunnable(); |
49 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
50 |
|
|
p1.execute(runnable); |
51 |
dl |
1.4 |
assertFalse(runnable.done); |
52 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
53 |
dl |
1.5 |
p1.shutdown(); |
54 |
|
|
try { |
55 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
56 |
|
|
} catch(InterruptedException e){ |
57 |
dl |
1.5 |
unexpectedException(); |
58 |
dl |
1.2 |
} |
59 |
dl |
1.1 |
assertTrue(runnable.done); |
60 |
dl |
1.5 |
p1.shutdown(); |
61 |
|
|
joinPool(p1); |
62 |
dl |
1.1 |
} |
63 |
|
|
catch(Exception e){ |
64 |
dl |
1.5 |
unexpectedException(); |
65 |
dl |
1.1 |
} |
66 |
dl |
1.4 |
|
67 |
dl |
1.1 |
} |
68 |
|
|
|
69 |
dl |
1.6 |
|
70 |
dl |
1.5 |
/** |
71 |
dl |
1.6 |
* delayed schedule of callable successfully executes after delay |
72 |
dl |
1.5 |
*/ |
73 |
|
|
public void testSchedule1() { |
74 |
|
|
try { |
75 |
dl |
1.1 |
MyCallable callable = new MyCallable(); |
76 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
77 |
|
|
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
78 |
dl |
1.4 |
assertFalse(callable.done); |
79 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
80 |
dl |
1.1 |
assertTrue(callable.done); |
81 |
|
|
assertEquals(Boolean.TRUE, f.get()); |
82 |
dl |
1.5 |
p1.shutdown(); |
83 |
|
|
joinPool(p1); |
84 |
|
|
} catch(RejectedExecutionException e){} |
85 |
dl |
1.2 |
catch(Exception e){ |
86 |
dl |
1.5 |
unexpectedException(); |
87 |
dl |
1.2 |
} |
88 |
dl |
1.1 |
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
dl |
1.6 |
* delayed schedule of runnable successfully executes after delay |
92 |
dl |
1.1 |
*/ |
93 |
dl |
1.5 |
public void testSchedule3() { |
94 |
|
|
try { |
95 |
dl |
1.1 |
MyRunnable runnable = new MyRunnable(); |
96 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
97 |
|
|
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
99 |
|
|
assertFalse(runnable.done); |
100 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
101 |
dl |
1.1 |
assertTrue(runnable.done); |
102 |
dl |
1.5 |
p1.shutdown(); |
103 |
|
|
joinPool(p1); |
104 |
dl |
1.1 |
} catch(Exception e){ |
105 |
dl |
1.5 |
unexpectedException(); |
106 |
dl |
1.1 |
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/** |
110 |
dl |
1.6 |
* scheduleAtFixedRate executes runnable after given initial delay |
111 |
dl |
1.1 |
*/ |
112 |
dl |
1.5 |
public void testSchedule4() { |
113 |
|
|
try { |
114 |
dl |
1.1 |
MyRunnable runnable = new MyRunnable(); |
115 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
116 |
dl |
1.6 |
ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
117 |
|
|
assertFalse(runnable.done); |
118 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
119 |
|
|
assertTrue(runnable.done); |
120 |
|
|
h.cancel(true); |
121 |
|
|
p1.shutdown(); |
122 |
|
|
joinPool(p1); |
123 |
|
|
} catch(Exception e){ |
124 |
|
|
unexpectedException(); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* scheduleWithFixedDelay executes runnable after given initial delay |
130 |
|
|
*/ |
131 |
|
|
public void testSchedule5() { |
132 |
|
|
try { |
133 |
|
|
MyRunnable runnable = new MyRunnable(); |
134 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
135 |
|
|
ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
136 |
dl |
1.4 |
assertFalse(runnable.done); |
137 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
138 |
dl |
1.1 |
assertTrue(runnable.done); |
139 |
dl |
1.6 |
h.cancel(true); |
140 |
dl |
1.5 |
p1.shutdown(); |
141 |
|
|
joinPool(p1); |
142 |
dl |
1.1 |
} catch(Exception e){ |
143 |
dl |
1.5 |
unexpectedException(); |
144 |
dl |
1.1 |
} |
145 |
|
|
} |
146 |
|
|
|
147 |
dl |
1.6 |
/** |
148 |
|
|
* execute (null) throws NPE |
149 |
|
|
*/ |
150 |
|
|
public void testExecuteNull() { |
151 |
|
|
ScheduledExecutor se = null; |
152 |
|
|
try { |
153 |
|
|
se = new ScheduledExecutor(1); |
154 |
|
|
se.execute(null); |
155 |
|
|
shouldThrow(); |
156 |
|
|
} catch(NullPointerException success){} |
157 |
|
|
catch(Exception e){ |
158 |
|
|
unexpectedException(); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
joinPool(se); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
|
|
* schedule (null) throws NPE |
166 |
|
|
*/ |
167 |
|
|
public void testScheduleNull() { |
168 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
169 |
|
|
try { |
170 |
|
|
MyCallable callable = null; |
171 |
|
|
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
172 |
|
|
shouldThrow(); |
173 |
|
|
} catch(NullPointerException success){} |
174 |
|
|
catch(Exception e){ |
175 |
|
|
unexpectedException(); |
176 |
|
|
} |
177 |
|
|
joinPool(se); |
178 |
|
|
} |
179 |
dl |
1.1 |
|
180 |
|
|
/** |
181 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
182 |
dl |
1.1 |
*/ |
183 |
dl |
1.5 |
public void testSchedule1_RejectedExecutionException() { |
184 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
185 |
dl |
1.5 |
try { |
186 |
dl |
1.1 |
se.shutdown(); |
187 |
dl |
1.4 |
se.schedule(new NoOpRunnable(), |
188 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
189 |
dl |
1.5 |
shouldThrow(); |
190 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
191 |
|
|
} |
192 |
|
|
joinPool(se); |
193 |
|
|
|
194 |
dl |
1.1 |
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
dl |
1.6 |
* schedule throws RejectedExecutionException if shutdown |
198 |
dl |
1.1 |
*/ |
199 |
dl |
1.5 |
public void testSchedule2_RejectedExecutionException() { |
200 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
201 |
dl |
1.5 |
try { |
202 |
dl |
1.1 |
se.shutdown(); |
203 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
204 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
205 |
dl |
1.5 |
shouldThrow(); |
206 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
207 |
|
|
} |
208 |
|
|
joinPool(se); |
209 |
dl |
1.1 |
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
dl |
1.6 |
* schedule callable throws RejectedExecutionException if shutdown |
213 |
dl |
1.1 |
*/ |
214 |
dl |
1.5 |
public void testSchedule3_RejectedExecutionException() { |
215 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
216 |
dl |
1.5 |
try { |
217 |
dl |
1.1 |
se.shutdown(); |
218 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
219 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
220 |
dl |
1.5 |
shouldThrow(); |
221 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
222 |
|
|
} |
223 |
|
|
joinPool(se); |
224 |
dl |
1.1 |
} |
225 |
|
|
|
226 |
|
|
/** |
227 |
dl |
1.6 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
228 |
dl |
1.1 |
*/ |
229 |
dl |
1.5 |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
230 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
231 |
dl |
1.5 |
try { |
232 |
dl |
1.1 |
se.shutdown(); |
233 |
dl |
1.4 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
234 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
235 |
dl |
1.5 |
shouldThrow(); |
236 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
237 |
|
|
} |
238 |
|
|
joinPool(se); |
239 |
dl |
1.1 |
} |
240 |
|
|
|
241 |
|
|
/** |
242 |
dl |
1.6 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
243 |
dl |
1.1 |
*/ |
244 |
dl |
1.5 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
245 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
dl |
1.5 |
try { |
247 |
dl |
1.1 |
se.shutdown(); |
248 |
dl |
1.4 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
249 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
250 |
dl |
1.5 |
shouldThrow(); |
251 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
252 |
|
|
} |
253 |
|
|
joinPool(se); |
254 |
dl |
1.1 |
} |
255 |
|
|
|
256 |
|
|
/** |
257 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
258 |
|
|
* thread becomes active |
259 |
dl |
1.2 |
*/ |
260 |
dl |
1.5 |
public void testGetActiveCount() { |
261 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
262 |
|
|
assertEquals(0, p2.getActiveCount()); |
263 |
|
|
p2.execute(new SmallRunnable()); |
264 |
|
|
try { |
265 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
266 |
|
|
} catch(Exception e){ |
267 |
dl |
1.5 |
unexpectedException(); |
268 |
dl |
1.2 |
} |
269 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
270 |
|
|
joinPool(p2); |
271 |
dl |
1.2 |
} |
272 |
|
|
|
273 |
|
|
/** |
274 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
275 |
|
|
* when tasks complete |
276 |
dl |
1.2 |
*/ |
277 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
278 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
279 |
|
|
assertEquals(0, p2.getCompletedTaskCount()); |
280 |
|
|
p2.execute(new SmallRunnable()); |
281 |
|
|
try { |
282 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
283 |
|
|
} catch(Exception e){ |
284 |
dl |
1.5 |
unexpectedException(); |
285 |
dl |
1.2 |
} |
286 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
287 |
|
|
joinPool(p2); |
288 |
dl |
1.2 |
} |
289 |
|
|
|
290 |
|
|
/** |
291 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
292 |
dl |
1.2 |
*/ |
293 |
dl |
1.5 |
public void testGetCorePoolSize() { |
294 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
295 |
|
|
assertEquals(1, p1.getCorePoolSize()); |
296 |
|
|
joinPool(p1); |
297 |
dl |
1.2 |
} |
298 |
|
|
|
299 |
|
|
/** |
300 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
301 |
|
|
* multiple threads active |
302 |
dl |
1.2 |
*/ |
303 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
304 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
305 |
|
|
assertEquals(0, p2.getLargestPoolSize()); |
306 |
|
|
p2.execute(new SmallRunnable()); |
307 |
|
|
p2.execute(new SmallRunnable()); |
308 |
|
|
try { |
309 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
310 |
|
|
} catch(Exception e){ |
311 |
dl |
1.5 |
unexpectedException(); |
312 |
dl |
1.2 |
} |
313 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
314 |
|
|
joinPool(p2); |
315 |
dl |
1.2 |
} |
316 |
|
|
|
317 |
|
|
/** |
318 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
319 |
|
|
* become active |
320 |
dl |
1.2 |
*/ |
321 |
dl |
1.5 |
public void testGetPoolSize() { |
322 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
323 |
|
|
assertEquals(0, p1.getPoolSize()); |
324 |
|
|
p1.execute(new SmallRunnable()); |
325 |
|
|
assertEquals(1, p1.getPoolSize()); |
326 |
|
|
joinPool(p1); |
327 |
dl |
1.2 |
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
dl |
1.6 |
* getTaskCount increases, but doesn't overestimate, when tasks |
331 |
|
|
* submitted |
332 |
dl |
1.2 |
*/ |
333 |
dl |
1.5 |
public void testGetTaskCount() { |
334 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
335 |
|
|
assertEquals(0, p1.getTaskCount()); |
336 |
dl |
1.4 |
for(int i = 0; i < 5; i++) |
337 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
338 |
|
|
try { |
339 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
|
} catch(Exception e){ |
341 |
dl |
1.5 |
unexpectedException(); |
342 |
dl |
1.2 |
} |
343 |
dl |
1.5 |
assertEquals(5, p1.getTaskCount()); |
344 |
|
|
joinPool(p1); |
345 |
dl |
1.2 |
} |
346 |
|
|
|
347 |
|
|
/** |
348 |
dl |
1.6 |
* is isShutDown is false before shutdown, true after |
349 |
dl |
1.2 |
*/ |
350 |
dl |
1.5 |
public void testIsShutdown() { |
351 |
dl |
1.2 |
|
352 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
353 |
dl |
1.2 |
try { |
354 |
dl |
1.5 |
assertFalse(p1.isShutdown()); |
355 |
dl |
1.2 |
} |
356 |
|
|
finally { |
357 |
dl |
1.5 |
p1.shutdown(); |
358 |
dl |
1.2 |
} |
359 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
360 |
dl |
1.2 |
} |
361 |
|
|
|
362 |
|
|
|
363 |
|
|
/** |
364 |
dl |
1.6 |
* isTerminated is false before termination, true after |
365 |
dl |
1.2 |
*/ |
366 |
dl |
1.5 |
public void testIsTerminated() { |
367 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
368 |
dl |
1.2 |
try { |
369 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
370 |
dl |
1.2 |
} finally { |
371 |
dl |
1.5 |
p1.shutdown(); |
372 |
dl |
1.2 |
} |
373 |
dl |
1.4 |
try { |
374 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
|
|
assertTrue(p1.isTerminated()); |
376 |
dl |
1.2 |
} catch(Exception e){ |
377 |
dl |
1.5 |
unexpectedException(); |
378 |
|
|
} |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
/** |
382 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
383 |
dl |
1.5 |
*/ |
384 |
|
|
public void testIsTerminating() { |
385 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
386 |
|
|
assertFalse(p1.isTerminating()); |
387 |
|
|
try { |
388 |
|
|
p1.execute(new SmallRunnable()); |
389 |
|
|
assertFalse(p1.isTerminating()); |
390 |
|
|
} finally { |
391 |
|
|
p1.shutdown(); |
392 |
|
|
} |
393 |
|
|
try { |
394 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
395 |
|
|
assertTrue(p1.isTerminated()); |
396 |
|
|
assertFalse(p1.isTerminating()); |
397 |
|
|
} catch(Exception e){ |
398 |
|
|
unexpectedException(); |
399 |
dl |
1.2 |
} |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
/** |
403 |
dl |
1.6 |
* purge removes cancelled tasks from the queue |
404 |
dl |
1.2 |
*/ |
405 |
dl |
1.5 |
public void testPurge() { |
406 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
407 |
dl |
1.4 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
408 |
|
|
for(int i = 0; i < 5; i++){ |
409 |
dl |
1.5 |
tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
410 |
dl |
1.2 |
} |
411 |
dl |
1.4 |
int max = 5; |
412 |
|
|
if (tasks[4].cancel(true)) --max; |
413 |
|
|
if (tasks[3].cancel(true)) --max; |
414 |
dl |
1.5 |
p1.purge(); |
415 |
|
|
long count = p1.getTaskCount(); |
416 |
dl |
1.4 |
assertTrue(count > 0 && count <= max); |
417 |
dl |
1.5 |
joinPool(p1); |
418 |
dl |
1.2 |
} |
419 |
|
|
|
420 |
|
|
/** |
421 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
422 |
dl |
1.2 |
*/ |
423 |
dl |
1.5 |
public void testShutDownNow() { |
424 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
425 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
426 |
dl |
1.5 |
p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
427 |
|
|
List l = p1.shutdownNow(); |
428 |
|
|
assertTrue(p1.isShutdown()); |
429 |
dl |
1.2 |
assertTrue(l.size() > 0 && l.size() <= 5); |
430 |
dl |
1.5 |
joinPool(p1); |
431 |
dl |
1.2 |
} |
432 |
|
|
|
433 |
dl |
1.5 |
/** |
434 |
dl |
1.6 |
* In default setting, shutdown cancels periodic but not delayed |
435 |
|
|
* tasks at shutdown |
436 |
dl |
1.5 |
*/ |
437 |
|
|
public void testShutDown1() { |
438 |
dl |
1.2 |
try { |
439 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
440 |
|
|
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
441 |
|
|
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
442 |
dl |
1.2 |
|
443 |
|
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
444 |
|
|
for(int i = 0; i < 5; i++) |
445 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
446 |
|
|
p1.shutdown(); |
447 |
|
|
BlockingQueue q = p1.getQueue(); |
448 |
dl |
1.2 |
for (Iterator it = q.iterator(); it.hasNext();) { |
449 |
|
|
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
450 |
|
|
assertFalse(t.isCancelled()); |
451 |
|
|
} |
452 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
453 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
454 |
dl |
1.2 |
for (int i = 0; i < 5; ++i) { |
455 |
|
|
assertTrue(tasks[i].isDone()); |
456 |
|
|
assertFalse(tasks[i].isCancelled()); |
457 |
|
|
} |
458 |
|
|
|
459 |
|
|
} |
460 |
|
|
catch(Exception ex) { |
461 |
dl |
1.5 |
unexpectedException(); |
462 |
dl |
1.2 |
} |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
|
466 |
dl |
1.5 |
/** |
467 |
dl |
1.6 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
468 |
|
|
* delayed tasks are cancelled at shutdown |
469 |
dl |
1.5 |
*/ |
470 |
|
|
public void testShutDown2() { |
471 |
dl |
1.2 |
try { |
472 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
473 |
|
|
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
474 |
dl |
1.2 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
475 |
|
|
for(int i = 0; i < 5; i++) |
476 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
477 |
|
|
p1.shutdown(); |
478 |
|
|
assertTrue(p1.isShutdown()); |
479 |
|
|
BlockingQueue q = p1.getQueue(); |
480 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
481 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
482 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
483 |
dl |
1.2 |
} |
484 |
|
|
catch(Exception ex) { |
485 |
dl |
1.5 |
unexpectedException(); |
486 |
dl |
1.2 |
} |
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
|
490 |
dl |
1.5 |
/** |
491 |
dl |
1.6 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
492 |
|
|
* periodic tasks are not cancelled at shutdown |
493 |
dl |
1.5 |
*/ |
494 |
|
|
public void testShutDown3() { |
495 |
dl |
1.2 |
try { |
496 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
497 |
|
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
498 |
dl |
1.2 |
ScheduledCancellable task = |
499 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
500 |
|
|
p1.shutdown(); |
501 |
|
|
assertTrue(p1.isShutdown()); |
502 |
|
|
BlockingQueue q = p1.getQueue(); |
503 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
504 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
505 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
506 |
dl |
1.2 |
} |
507 |
|
|
catch(Exception ex) { |
508 |
dl |
1.5 |
unexpectedException(); |
509 |
dl |
1.2 |
} |
510 |
|
|
} |
511 |
|
|
|
512 |
dl |
1.5 |
/** |
513 |
dl |
1.6 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
514 |
|
|
* periodic tasks are cancelled at shutdown |
515 |
dl |
1.5 |
*/ |
516 |
|
|
public void testShutDown4() { |
517 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
518 |
dl |
1.2 |
try { |
519 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
520 |
dl |
1.2 |
ScheduledCancellable task = |
521 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
522 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
523 |
dl |
1.5 |
p1.shutdown(); |
524 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
525 |
dl |
1.5 |
assertFalse(p1.isTerminated()); |
526 |
|
|
assertTrue(p1.isShutdown()); |
527 |
dl |
1.2 |
Thread.sleep(SHORT_DELAY_MS); |
528 |
|
|
assertFalse(task.isCancelled()); |
529 |
|
|
task.cancel(true); |
530 |
|
|
assertTrue(task.isCancelled()); |
531 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
532 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
533 |
dl |
1.2 |
} |
534 |
|
|
catch(Exception ex) { |
535 |
dl |
1.5 |
unexpectedException(); |
536 |
dl |
1.2 |
} |
537 |
|
|
finally { |
538 |
dl |
1.5 |
p1.shutdownNow(); |
539 |
dl |
1.2 |
} |
540 |
dl |
1.1 |
} |
541 |
|
|
|
542 |
|
|
} |