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 |
|
|
|
21 |
dl |
1.5 |
/** |
22 |
dl |
1.6 |
* execute successfully executes a runnable |
23 |
dl |
1.5 |
*/ |
24 |
|
|
public void testExecute() { |
25 |
|
|
try { |
26 |
dl |
1.8 |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
27 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
28 |
|
|
p1.execute(runnable); |
29 |
dl |
1.4 |
assertFalse(runnable.done); |
30 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
31 |
dl |
1.5 |
p1.shutdown(); |
32 |
|
|
try { |
33 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
34 |
|
|
} catch(InterruptedException e){ |
35 |
dl |
1.5 |
unexpectedException(); |
36 |
dl |
1.2 |
} |
37 |
dl |
1.1 |
assertTrue(runnable.done); |
38 |
dl |
1.5 |
p1.shutdown(); |
39 |
|
|
joinPool(p1); |
40 |
dl |
1.1 |
} |
41 |
|
|
catch(Exception e){ |
42 |
dl |
1.5 |
unexpectedException(); |
43 |
dl |
1.1 |
} |
44 |
dl |
1.4 |
|
45 |
dl |
1.1 |
} |
46 |
|
|
|
47 |
dl |
1.6 |
|
48 |
dl |
1.5 |
/** |
49 |
dl |
1.6 |
* delayed schedule of callable successfully executes after delay |
50 |
dl |
1.5 |
*/ |
51 |
|
|
public void testSchedule1() { |
52 |
|
|
try { |
53 |
dl |
1.7 |
TrackedCallable callable = new TrackedCallable(); |
54 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
55 |
|
|
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
56 |
dl |
1.4 |
assertFalse(callable.done); |
57 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
58 |
dl |
1.1 |
assertTrue(callable.done); |
59 |
|
|
assertEquals(Boolean.TRUE, f.get()); |
60 |
dl |
1.5 |
p1.shutdown(); |
61 |
|
|
joinPool(p1); |
62 |
|
|
} catch(RejectedExecutionException e){} |
63 |
dl |
1.2 |
catch(Exception e){ |
64 |
dl |
1.5 |
unexpectedException(); |
65 |
dl |
1.2 |
} |
66 |
dl |
1.1 |
} |
67 |
|
|
|
68 |
|
|
/** |
69 |
dl |
1.6 |
* delayed schedule of runnable successfully executes after delay |
70 |
dl |
1.1 |
*/ |
71 |
dl |
1.5 |
public void testSchedule3() { |
72 |
|
|
try { |
73 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
74 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
75 |
|
|
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
76 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
77 |
|
|
assertFalse(runnable.done); |
78 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
79 |
dl |
1.1 |
assertTrue(runnable.done); |
80 |
dl |
1.5 |
p1.shutdown(); |
81 |
|
|
joinPool(p1); |
82 |
dl |
1.1 |
} catch(Exception e){ |
83 |
dl |
1.5 |
unexpectedException(); |
84 |
dl |
1.1 |
} |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/** |
88 |
dl |
1.6 |
* scheduleAtFixedRate executes runnable after given initial delay |
89 |
dl |
1.1 |
*/ |
90 |
dl |
1.5 |
public void testSchedule4() { |
91 |
|
|
try { |
92 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
93 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
94 |
dl |
1.6 |
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 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
112 |
dl |
1.6 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
113 |
|
|
ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
dl |
1.4 |
assertFalse(runnable.done); |
115 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
116 |
dl |
1.1 |
assertTrue(runnable.done); |
117 |
dl |
1.6 |
h.cancel(true); |
118 |
dl |
1.5 |
p1.shutdown(); |
119 |
|
|
joinPool(p1); |
120 |
dl |
1.1 |
} catch(Exception e){ |
121 |
dl |
1.5 |
unexpectedException(); |
122 |
dl |
1.1 |
} |
123 |
|
|
} |
124 |
|
|
|
125 |
dl |
1.6 |
/** |
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 (null) throws NPE |
144 |
|
|
*/ |
145 |
|
|
public void testScheduleNull() { |
146 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
147 |
|
|
try { |
148 |
dl |
1.7 |
TrackedCallable callable = null; |
149 |
dl |
1.6 |
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 |
dl |
1.1 |
|
158 |
|
|
/** |
159 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
160 |
dl |
1.1 |
*/ |
161 |
dl |
1.5 |
public void testSchedule1_RejectedExecutionException() { |
162 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
163 |
dl |
1.5 |
try { |
164 |
dl |
1.1 |
se.shutdown(); |
165 |
dl |
1.4 |
se.schedule(new NoOpRunnable(), |
166 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
167 |
dl |
1.5 |
shouldThrow(); |
168 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
169 |
|
|
} |
170 |
|
|
joinPool(se); |
171 |
|
|
|
172 |
dl |
1.1 |
} |
173 |
|
|
|
174 |
|
|
/** |
175 |
dl |
1.6 |
* schedule throws RejectedExecutionException if shutdown |
176 |
dl |
1.1 |
*/ |
177 |
dl |
1.5 |
public void testSchedule2_RejectedExecutionException() { |
178 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
179 |
dl |
1.5 |
try { |
180 |
dl |
1.1 |
se.shutdown(); |
181 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
182 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
183 |
dl |
1.5 |
shouldThrow(); |
184 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
185 |
|
|
} |
186 |
|
|
joinPool(se); |
187 |
dl |
1.1 |
} |
188 |
|
|
|
189 |
|
|
/** |
190 |
dl |
1.6 |
* schedule callable throws RejectedExecutionException if shutdown |
191 |
dl |
1.1 |
*/ |
192 |
dl |
1.5 |
public void testSchedule3_RejectedExecutionException() { |
193 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
194 |
dl |
1.5 |
try { |
195 |
dl |
1.1 |
se.shutdown(); |
196 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
197 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
198 |
dl |
1.5 |
shouldThrow(); |
199 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
200 |
|
|
} |
201 |
|
|
joinPool(se); |
202 |
dl |
1.1 |
} |
203 |
|
|
|
204 |
|
|
/** |
205 |
dl |
1.6 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
206 |
dl |
1.1 |
*/ |
207 |
dl |
1.5 |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
208 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
209 |
dl |
1.5 |
try { |
210 |
dl |
1.1 |
se.shutdown(); |
211 |
dl |
1.4 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
212 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
213 |
dl |
1.5 |
shouldThrow(); |
214 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
215 |
|
|
} |
216 |
|
|
joinPool(se); |
217 |
dl |
1.1 |
} |
218 |
|
|
|
219 |
|
|
/** |
220 |
dl |
1.6 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
221 |
dl |
1.1 |
*/ |
222 |
dl |
1.5 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
223 |
dl |
1.4 |
ScheduledExecutor se = new ScheduledExecutor(1); |
224 |
dl |
1.5 |
try { |
225 |
dl |
1.1 |
se.shutdown(); |
226 |
dl |
1.4 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
227 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
228 |
dl |
1.5 |
shouldThrow(); |
229 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
230 |
|
|
} |
231 |
|
|
joinPool(se); |
232 |
dl |
1.1 |
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
236 |
|
|
* thread becomes active |
237 |
dl |
1.2 |
*/ |
238 |
dl |
1.5 |
public void testGetActiveCount() { |
239 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
240 |
|
|
assertEquals(0, p2.getActiveCount()); |
241 |
|
|
p2.execute(new SmallRunnable()); |
242 |
|
|
try { |
243 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
244 |
|
|
} catch(Exception e){ |
245 |
dl |
1.5 |
unexpectedException(); |
246 |
dl |
1.2 |
} |
247 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
248 |
|
|
joinPool(p2); |
249 |
dl |
1.2 |
} |
250 |
|
|
|
251 |
|
|
/** |
252 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
253 |
|
|
* when tasks complete |
254 |
dl |
1.2 |
*/ |
255 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
256 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
257 |
|
|
assertEquals(0, p2.getCompletedTaskCount()); |
258 |
|
|
p2.execute(new SmallRunnable()); |
259 |
|
|
try { |
260 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
261 |
|
|
} catch(Exception e){ |
262 |
dl |
1.5 |
unexpectedException(); |
263 |
dl |
1.2 |
} |
264 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
265 |
|
|
joinPool(p2); |
266 |
dl |
1.2 |
} |
267 |
|
|
|
268 |
|
|
/** |
269 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
270 |
dl |
1.2 |
*/ |
271 |
dl |
1.5 |
public void testGetCorePoolSize() { |
272 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
273 |
|
|
assertEquals(1, p1.getCorePoolSize()); |
274 |
|
|
joinPool(p1); |
275 |
dl |
1.2 |
} |
276 |
|
|
|
277 |
|
|
/** |
278 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
279 |
|
|
* multiple threads active |
280 |
dl |
1.2 |
*/ |
281 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
282 |
|
|
ScheduledExecutor p2 = new ScheduledExecutor(2); |
283 |
|
|
assertEquals(0, p2.getLargestPoolSize()); |
284 |
|
|
p2.execute(new SmallRunnable()); |
285 |
|
|
p2.execute(new SmallRunnable()); |
286 |
|
|
try { |
287 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
288 |
|
|
} catch(Exception e){ |
289 |
dl |
1.5 |
unexpectedException(); |
290 |
dl |
1.2 |
} |
291 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
292 |
|
|
joinPool(p2); |
293 |
dl |
1.2 |
} |
294 |
|
|
|
295 |
|
|
/** |
296 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
297 |
|
|
* become active |
298 |
dl |
1.2 |
*/ |
299 |
dl |
1.5 |
public void testGetPoolSize() { |
300 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
301 |
|
|
assertEquals(0, p1.getPoolSize()); |
302 |
|
|
p1.execute(new SmallRunnable()); |
303 |
|
|
assertEquals(1, p1.getPoolSize()); |
304 |
|
|
joinPool(p1); |
305 |
dl |
1.2 |
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
dl |
1.6 |
* getTaskCount increases, but doesn't overestimate, when tasks |
309 |
|
|
* submitted |
310 |
dl |
1.2 |
*/ |
311 |
dl |
1.5 |
public void testGetTaskCount() { |
312 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
313 |
|
|
assertEquals(0, p1.getTaskCount()); |
314 |
dl |
1.4 |
for(int i = 0; i < 5; i++) |
315 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
316 |
|
|
try { |
317 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
318 |
|
|
} catch(Exception e){ |
319 |
dl |
1.5 |
unexpectedException(); |
320 |
dl |
1.2 |
} |
321 |
dl |
1.5 |
assertEquals(5, p1.getTaskCount()); |
322 |
|
|
joinPool(p1); |
323 |
dl |
1.2 |
} |
324 |
dl |
1.8 |
|
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 |
dl |
1.2 |
|
362 |
|
|
/** |
363 |
dl |
1.6 |
* is isShutDown is false before shutdown, true after |
364 |
dl |
1.2 |
*/ |
365 |
dl |
1.5 |
public void testIsShutdown() { |
366 |
dl |
1.2 |
|
367 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
368 |
dl |
1.2 |
try { |
369 |
dl |
1.5 |
assertFalse(p1.isShutdown()); |
370 |
dl |
1.2 |
} |
371 |
|
|
finally { |
372 |
dl |
1.5 |
p1.shutdown(); |
373 |
dl |
1.2 |
} |
374 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
375 |
dl |
1.2 |
} |
376 |
|
|
|
377 |
|
|
|
378 |
|
|
/** |
379 |
dl |
1.6 |
* isTerminated is false before termination, true after |
380 |
dl |
1.2 |
*/ |
381 |
dl |
1.5 |
public void testIsTerminated() { |
382 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
383 |
dl |
1.2 |
try { |
384 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
385 |
dl |
1.2 |
} finally { |
386 |
dl |
1.5 |
p1.shutdown(); |
387 |
dl |
1.2 |
} |
388 |
dl |
1.4 |
try { |
389 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
390 |
|
|
assertTrue(p1.isTerminated()); |
391 |
dl |
1.2 |
} catch(Exception e){ |
392 |
dl |
1.5 |
unexpectedException(); |
393 |
|
|
} |
394 |
|
|
} |
395 |
|
|
|
396 |
|
|
/** |
397 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
398 |
dl |
1.5 |
*/ |
399 |
|
|
public void testIsTerminating() { |
400 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
401 |
|
|
assertFalse(p1.isTerminating()); |
402 |
|
|
try { |
403 |
|
|
p1.execute(new SmallRunnable()); |
404 |
|
|
assertFalse(p1.isTerminating()); |
405 |
|
|
} finally { |
406 |
|
|
p1.shutdown(); |
407 |
|
|
} |
408 |
|
|
try { |
409 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
410 |
|
|
assertTrue(p1.isTerminated()); |
411 |
|
|
assertFalse(p1.isTerminating()); |
412 |
|
|
} catch(Exception e){ |
413 |
|
|
unexpectedException(); |
414 |
dl |
1.2 |
} |
415 |
|
|
} |
416 |
|
|
|
417 |
|
|
/** |
418 |
dl |
1.8 |
* 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 |
dl |
1.2 |
*/ |
471 |
dl |
1.5 |
public void testPurge() { |
472 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
473 |
dl |
1.4 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
474 |
|
|
for(int i = 0; i < 5; i++){ |
475 |
dl |
1.8 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
476 |
dl |
1.2 |
} |
477 |
dl |
1.4 |
int max = 5; |
478 |
|
|
if (tasks[4].cancel(true)) --max; |
479 |
|
|
if (tasks[3].cancel(true)) --max; |
480 |
dl |
1.5 |
p1.purge(); |
481 |
|
|
long count = p1.getTaskCount(); |
482 |
dl |
1.4 |
assertTrue(count > 0 && count <= max); |
483 |
dl |
1.5 |
joinPool(p1); |
484 |
dl |
1.2 |
} |
485 |
|
|
|
486 |
|
|
/** |
487 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
488 |
dl |
1.2 |
*/ |
489 |
dl |
1.5 |
public void testShutDownNow() { |
490 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
491 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
492 |
dl |
1.8 |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
dl |
1.5 |
List l = p1.shutdownNow(); |
494 |
|
|
assertTrue(p1.isShutdown()); |
495 |
dl |
1.2 |
assertTrue(l.size() > 0 && l.size() <= 5); |
496 |
dl |
1.5 |
joinPool(p1); |
497 |
dl |
1.2 |
} |
498 |
|
|
|
499 |
dl |
1.5 |
/** |
500 |
dl |
1.6 |
* In default setting, shutdown cancels periodic but not delayed |
501 |
|
|
* tasks at shutdown |
502 |
dl |
1.5 |
*/ |
503 |
|
|
public void testShutDown1() { |
504 |
dl |
1.2 |
try { |
505 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
506 |
|
|
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
507 |
|
|
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
508 |
dl |
1.2 |
|
509 |
|
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
510 |
|
|
for(int i = 0; i < 5; i++) |
511 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
512 |
|
|
p1.shutdown(); |
513 |
|
|
BlockingQueue q = p1.getQueue(); |
514 |
dl |
1.2 |
for (Iterator it = q.iterator(); it.hasNext();) { |
515 |
|
|
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
516 |
|
|
assertFalse(t.isCancelled()); |
517 |
|
|
} |
518 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
519 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
520 |
dl |
1.2 |
for (int i = 0; i < 5; ++i) { |
521 |
|
|
assertTrue(tasks[i].isDone()); |
522 |
|
|
assertFalse(tasks[i].isCancelled()); |
523 |
|
|
} |
524 |
|
|
|
525 |
|
|
} |
526 |
|
|
catch(Exception ex) { |
527 |
dl |
1.5 |
unexpectedException(); |
528 |
dl |
1.2 |
} |
529 |
|
|
} |
530 |
|
|
|
531 |
|
|
|
532 |
dl |
1.5 |
/** |
533 |
dl |
1.6 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
534 |
|
|
* delayed tasks are cancelled at shutdown |
535 |
dl |
1.5 |
*/ |
536 |
|
|
public void testShutDown2() { |
537 |
dl |
1.2 |
try { |
538 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
539 |
|
|
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
540 |
dl |
1.2 |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
541 |
|
|
for(int i = 0; i < 5; i++) |
542 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
|
|
p1.shutdown(); |
544 |
|
|
assertTrue(p1.isShutdown()); |
545 |
|
|
BlockingQueue q = p1.getQueue(); |
546 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
547 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
548 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
549 |
dl |
1.2 |
} |
550 |
|
|
catch(Exception ex) { |
551 |
dl |
1.5 |
unexpectedException(); |
552 |
dl |
1.2 |
} |
553 |
|
|
} |
554 |
|
|
|
555 |
|
|
|
556 |
dl |
1.5 |
/** |
557 |
dl |
1.6 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
558 |
|
|
* periodic tasks are not cancelled at shutdown |
559 |
dl |
1.5 |
*/ |
560 |
|
|
public void testShutDown3() { |
561 |
dl |
1.2 |
try { |
562 |
dl |
1.5 |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
563 |
|
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
564 |
dl |
1.2 |
ScheduledCancellable task = |
565 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
566 |
|
|
p1.shutdown(); |
567 |
|
|
assertTrue(p1.isShutdown()); |
568 |
|
|
BlockingQueue q = p1.getQueue(); |
569 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
570 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
571 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
572 |
dl |
1.2 |
} |
573 |
|
|
catch(Exception ex) { |
574 |
dl |
1.5 |
unexpectedException(); |
575 |
dl |
1.2 |
} |
576 |
|
|
} |
577 |
|
|
|
578 |
dl |
1.5 |
/** |
579 |
dl |
1.6 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
580 |
|
|
* periodic tasks are cancelled at shutdown |
581 |
dl |
1.5 |
*/ |
582 |
|
|
public void testShutDown4() { |
583 |
|
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
584 |
dl |
1.2 |
try { |
585 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
586 |
dl |
1.2 |
ScheduledCancellable task = |
587 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
588 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
589 |
dl |
1.5 |
p1.shutdown(); |
590 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
591 |
dl |
1.5 |
assertFalse(p1.isTerminated()); |
592 |
|
|
assertTrue(p1.isShutdown()); |
593 |
dl |
1.2 |
Thread.sleep(SHORT_DELAY_MS); |
594 |
|
|
assertFalse(task.isCancelled()); |
595 |
|
|
task.cancel(true); |
596 |
|
|
assertTrue(task.isCancelled()); |
597 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
598 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
599 |
dl |
1.2 |
} |
600 |
|
|
catch(Exception ex) { |
601 |
dl |
1.5 |
unexpectedException(); |
602 |
dl |
1.2 |
} |
603 |
|
|
finally { |
604 |
dl |
1.5 |
p1.shutdownNow(); |
605 |
dl |
1.2 |
} |
606 |
dl |
1.1 |
} |
607 |
|
|
|
608 |
|
|
} |