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.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
28 |
dl |
1.5 |
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.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
55 |
dl |
1.5 |
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.9 |
e.printStackTrace(); |
65 |
dl |
1.5 |
unexpectedException(); |
66 |
dl |
1.2 |
} |
67 |
dl |
1.1 |
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
dl |
1.6 |
* delayed schedule of runnable successfully executes after delay |
71 |
dl |
1.1 |
*/ |
72 |
dl |
1.5 |
public void testSchedule3() { |
73 |
|
|
try { |
74 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
75 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
76 |
dl |
1.5 |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
77 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
78 |
|
|
assertFalse(runnable.done); |
79 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
80 |
dl |
1.1 |
assertTrue(runnable.done); |
81 |
dl |
1.5 |
p1.shutdown(); |
82 |
|
|
joinPool(p1); |
83 |
dl |
1.1 |
} catch(Exception e){ |
84 |
dl |
1.5 |
unexpectedException(); |
85 |
dl |
1.1 |
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
dl |
1.6 |
* scheduleAtFixedRate executes runnable after given initial delay |
90 |
dl |
1.1 |
*/ |
91 |
dl |
1.5 |
public void testSchedule4() { |
92 |
|
|
try { |
93 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
94 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
95 |
|
|
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
96 |
dl |
1.6 |
assertFalse(runnable.done); |
97 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
98 |
|
|
assertTrue(runnable.done); |
99 |
|
|
h.cancel(true); |
100 |
|
|
p1.shutdown(); |
101 |
|
|
joinPool(p1); |
102 |
|
|
} catch(Exception e){ |
103 |
|
|
unexpectedException(); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/** |
108 |
|
|
* scheduleWithFixedDelay executes runnable after given initial delay |
109 |
|
|
*/ |
110 |
|
|
public void testSchedule5() { |
111 |
|
|
try { |
112 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
113 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
114 |
|
|
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
115 |
dl |
1.4 |
assertFalse(runnable.done); |
116 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
117 |
dl |
1.1 |
assertTrue(runnable.done); |
118 |
dl |
1.6 |
h.cancel(true); |
119 |
dl |
1.5 |
p1.shutdown(); |
120 |
|
|
joinPool(p1); |
121 |
dl |
1.1 |
} catch(Exception e){ |
122 |
dl |
1.5 |
unexpectedException(); |
123 |
dl |
1.1 |
} |
124 |
|
|
} |
125 |
|
|
|
126 |
dl |
1.6 |
/** |
127 |
|
|
* execute (null) throws NPE |
128 |
|
|
*/ |
129 |
|
|
public void testExecuteNull() { |
130 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = null; |
131 |
dl |
1.6 |
try { |
132 |
dl |
1.9 |
se = new ScheduledThreadPoolExecutor(1); |
133 |
dl |
1.6 |
se.execute(null); |
134 |
|
|
shouldThrow(); |
135 |
|
|
} catch(NullPointerException success){} |
136 |
|
|
catch(Exception e){ |
137 |
|
|
unexpectedException(); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
joinPool(se); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/** |
144 |
|
|
* schedule (null) throws NPE |
145 |
|
|
*/ |
146 |
|
|
public void testScheduleNull() { |
147 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
148 |
dl |
1.6 |
try { |
149 |
dl |
1.7 |
TrackedCallable callable = null; |
150 |
dl |
1.6 |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
|
|
shouldThrow(); |
152 |
|
|
} catch(NullPointerException success){} |
153 |
|
|
catch(Exception e){ |
154 |
|
|
unexpectedException(); |
155 |
|
|
} |
156 |
|
|
joinPool(se); |
157 |
|
|
} |
158 |
dl |
1.1 |
|
159 |
|
|
/** |
160 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
161 |
dl |
1.1 |
*/ |
162 |
dl |
1.5 |
public void testSchedule1_RejectedExecutionException() { |
163 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
164 |
dl |
1.5 |
try { |
165 |
dl |
1.1 |
se.shutdown(); |
166 |
dl |
1.4 |
se.schedule(new NoOpRunnable(), |
167 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
168 |
dl |
1.5 |
shouldThrow(); |
169 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
170 |
|
|
} |
171 |
|
|
joinPool(se); |
172 |
|
|
|
173 |
dl |
1.1 |
} |
174 |
|
|
|
175 |
|
|
/** |
176 |
dl |
1.6 |
* schedule throws RejectedExecutionException if shutdown |
177 |
dl |
1.1 |
*/ |
178 |
dl |
1.5 |
public void testSchedule2_RejectedExecutionException() { |
179 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
180 |
dl |
1.5 |
try { |
181 |
dl |
1.1 |
se.shutdown(); |
182 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
183 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
184 |
dl |
1.5 |
shouldThrow(); |
185 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
186 |
|
|
} |
187 |
|
|
joinPool(se); |
188 |
dl |
1.1 |
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
dl |
1.6 |
* schedule callable throws RejectedExecutionException if shutdown |
192 |
dl |
1.1 |
*/ |
193 |
dl |
1.5 |
public void testSchedule3_RejectedExecutionException() { |
194 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
195 |
dl |
1.5 |
try { |
196 |
dl |
1.1 |
se.shutdown(); |
197 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
198 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
199 |
dl |
1.5 |
shouldThrow(); |
200 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
201 |
|
|
} |
202 |
|
|
joinPool(se); |
203 |
dl |
1.1 |
} |
204 |
|
|
|
205 |
|
|
/** |
206 |
dl |
1.6 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
207 |
dl |
1.1 |
*/ |
208 |
dl |
1.5 |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
209 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
210 |
dl |
1.5 |
try { |
211 |
dl |
1.1 |
se.shutdown(); |
212 |
dl |
1.4 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
213 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
214 |
dl |
1.5 |
shouldThrow(); |
215 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
216 |
|
|
} |
217 |
|
|
joinPool(se); |
218 |
dl |
1.1 |
} |
219 |
|
|
|
220 |
|
|
/** |
221 |
dl |
1.6 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
222 |
dl |
1.1 |
*/ |
223 |
dl |
1.5 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
224 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
225 |
dl |
1.5 |
try { |
226 |
dl |
1.1 |
se.shutdown(); |
227 |
dl |
1.4 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
228 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
229 |
dl |
1.5 |
shouldThrow(); |
230 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
231 |
|
|
} |
232 |
|
|
joinPool(se); |
233 |
dl |
1.1 |
} |
234 |
|
|
|
235 |
|
|
/** |
236 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
237 |
|
|
* thread becomes active |
238 |
dl |
1.2 |
*/ |
239 |
dl |
1.5 |
public void testGetActiveCount() { |
240 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
241 |
dl |
1.5 |
assertEquals(0, p2.getActiveCount()); |
242 |
|
|
p2.execute(new SmallRunnable()); |
243 |
|
|
try { |
244 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
245 |
|
|
} catch(Exception e){ |
246 |
dl |
1.5 |
unexpectedException(); |
247 |
dl |
1.2 |
} |
248 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
249 |
|
|
joinPool(p2); |
250 |
dl |
1.2 |
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
254 |
|
|
* when tasks complete |
255 |
dl |
1.2 |
*/ |
256 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
257 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
258 |
dl |
1.5 |
assertEquals(0, p2.getCompletedTaskCount()); |
259 |
|
|
p2.execute(new SmallRunnable()); |
260 |
|
|
try { |
261 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
262 |
|
|
} catch(Exception e){ |
263 |
dl |
1.5 |
unexpectedException(); |
264 |
dl |
1.2 |
} |
265 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
266 |
|
|
joinPool(p2); |
267 |
dl |
1.2 |
} |
268 |
|
|
|
269 |
|
|
/** |
270 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
271 |
dl |
1.2 |
*/ |
272 |
dl |
1.5 |
public void testGetCorePoolSize() { |
273 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
274 |
dl |
1.5 |
assertEquals(1, p1.getCorePoolSize()); |
275 |
|
|
joinPool(p1); |
276 |
dl |
1.2 |
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
280 |
|
|
* multiple threads active |
281 |
dl |
1.2 |
*/ |
282 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
283 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
284 |
dl |
1.5 |
assertEquals(0, p2.getLargestPoolSize()); |
285 |
|
|
p2.execute(new SmallRunnable()); |
286 |
|
|
p2.execute(new SmallRunnable()); |
287 |
|
|
try { |
288 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
289 |
|
|
} catch(Exception e){ |
290 |
dl |
1.5 |
unexpectedException(); |
291 |
dl |
1.2 |
} |
292 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
293 |
|
|
joinPool(p2); |
294 |
dl |
1.2 |
} |
295 |
|
|
|
296 |
|
|
/** |
297 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
298 |
|
|
* become active |
299 |
dl |
1.2 |
*/ |
300 |
dl |
1.5 |
public void testGetPoolSize() { |
301 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
302 |
dl |
1.5 |
assertEquals(0, p1.getPoolSize()); |
303 |
|
|
p1.execute(new SmallRunnable()); |
304 |
|
|
assertEquals(1, p1.getPoolSize()); |
305 |
|
|
joinPool(p1); |
306 |
dl |
1.2 |
} |
307 |
|
|
|
308 |
|
|
/** |
309 |
dl |
1.6 |
* getTaskCount increases, but doesn't overestimate, when tasks |
310 |
|
|
* submitted |
311 |
dl |
1.2 |
*/ |
312 |
dl |
1.5 |
public void testGetTaskCount() { |
313 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
314 |
dl |
1.5 |
assertEquals(0, p1.getTaskCount()); |
315 |
dl |
1.4 |
for(int i = 0; i < 5; i++) |
316 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
317 |
|
|
try { |
318 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
319 |
|
|
} catch(Exception e){ |
320 |
dl |
1.5 |
unexpectedException(); |
321 |
dl |
1.2 |
} |
322 |
dl |
1.5 |
assertEquals(5, p1.getTaskCount()); |
323 |
|
|
joinPool(p1); |
324 |
dl |
1.2 |
} |
325 |
dl |
1.8 |
|
326 |
|
|
/** |
327 |
|
|
* getThreadFactory returns factory in constructor if not set |
328 |
|
|
*/ |
329 |
|
|
public void testGetThreadFactory() { |
330 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
331 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
332 |
dl |
1.8 |
assertSame(tf, p.getThreadFactory()); |
333 |
|
|
p.shutdown(); |
334 |
|
|
joinPool(p); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
/** |
338 |
|
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
339 |
|
|
*/ |
340 |
|
|
public void testSetThreadFactory() { |
341 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
342 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
343 |
dl |
1.8 |
p.setThreadFactory(tf); |
344 |
|
|
assertSame(tf, p.getThreadFactory()); |
345 |
|
|
p.shutdown(); |
346 |
|
|
joinPool(p); |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
/** |
350 |
|
|
* setThreadFactory(null) throws NPE |
351 |
|
|
*/ |
352 |
|
|
public void testSetThreadFactoryNull() { |
353 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
354 |
dl |
1.8 |
try { |
355 |
|
|
p.setThreadFactory(null); |
356 |
|
|
shouldThrow(); |
357 |
|
|
} catch (NullPointerException success) { |
358 |
|
|
} finally { |
359 |
|
|
joinPool(p); |
360 |
|
|
} |
361 |
|
|
} |
362 |
dl |
1.2 |
|
363 |
|
|
/** |
364 |
dl |
1.6 |
* is isShutDown is false before shutdown, true after |
365 |
dl |
1.2 |
*/ |
366 |
dl |
1.5 |
public void testIsShutdown() { |
367 |
dl |
1.2 |
|
368 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
369 |
dl |
1.2 |
try { |
370 |
dl |
1.5 |
assertFalse(p1.isShutdown()); |
371 |
dl |
1.2 |
} |
372 |
|
|
finally { |
373 |
dl |
1.5 |
p1.shutdown(); |
374 |
dl |
1.2 |
} |
375 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
376 |
dl |
1.2 |
} |
377 |
|
|
|
378 |
|
|
|
379 |
|
|
/** |
380 |
dl |
1.6 |
* isTerminated is false before termination, true after |
381 |
dl |
1.2 |
*/ |
382 |
dl |
1.5 |
public void testIsTerminated() { |
383 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
384 |
dl |
1.2 |
try { |
385 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
386 |
dl |
1.2 |
} finally { |
387 |
dl |
1.5 |
p1.shutdown(); |
388 |
dl |
1.2 |
} |
389 |
dl |
1.4 |
try { |
390 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
391 |
|
|
assertTrue(p1.isTerminated()); |
392 |
dl |
1.2 |
} catch(Exception e){ |
393 |
dl |
1.5 |
unexpectedException(); |
394 |
|
|
} |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
/** |
398 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
399 |
dl |
1.5 |
*/ |
400 |
|
|
public void testIsTerminating() { |
401 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
402 |
dl |
1.5 |
assertFalse(p1.isTerminating()); |
403 |
|
|
try { |
404 |
|
|
p1.execute(new SmallRunnable()); |
405 |
|
|
assertFalse(p1.isTerminating()); |
406 |
|
|
} finally { |
407 |
|
|
p1.shutdown(); |
408 |
|
|
} |
409 |
|
|
try { |
410 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
|
|
assertTrue(p1.isTerminated()); |
412 |
|
|
assertFalse(p1.isTerminating()); |
413 |
|
|
} catch(Exception e){ |
414 |
|
|
unexpectedException(); |
415 |
dl |
1.2 |
} |
416 |
|
|
} |
417 |
|
|
|
418 |
|
|
/** |
419 |
dl |
1.8 |
* getQueue returns the work queue, which contains queued tasks |
420 |
|
|
*/ |
421 |
|
|
public void testGetQueue() { |
422 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
423 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
424 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
425 |
|
|
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
426 |
|
|
} |
427 |
|
|
try { |
428 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
429 |
|
|
BlockingQueue<Runnable> q = p1.getQueue(); |
430 |
|
|
assertTrue(q.contains(tasks[4])); |
431 |
|
|
assertFalse(q.contains(tasks[0])); |
432 |
|
|
p1.shutdownNow(); |
433 |
|
|
} catch(Exception e) { |
434 |
|
|
unexpectedException(); |
435 |
|
|
} finally { |
436 |
|
|
joinPool(p1); |
437 |
|
|
} |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
/** |
441 |
|
|
* remove(task) removes queued task, and fails to remove active task |
442 |
|
|
*/ |
443 |
|
|
public void testRemove() { |
444 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
445 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
446 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
447 |
|
|
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
448 |
|
|
} |
449 |
|
|
try { |
450 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
451 |
|
|
BlockingQueue<Runnable> q = p1.getQueue(); |
452 |
|
|
assertFalse(p1.remove((Runnable)tasks[0])); |
453 |
|
|
assertTrue(q.contains((Runnable)tasks[4])); |
454 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
455 |
|
|
assertTrue(p1.remove((Runnable)tasks[4])); |
456 |
|
|
assertFalse(p1.remove((Runnable)tasks[4])); |
457 |
|
|
assertFalse(q.contains((Runnable)tasks[4])); |
458 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
459 |
|
|
assertTrue(p1.remove((Runnable)tasks[3])); |
460 |
|
|
assertFalse(q.contains((Runnable)tasks[3])); |
461 |
|
|
p1.shutdownNow(); |
462 |
|
|
} catch(Exception e) { |
463 |
|
|
unexpectedException(); |
464 |
|
|
} finally { |
465 |
|
|
joinPool(p1); |
466 |
|
|
} |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
/** |
470 |
|
|
* purge removes cancelled tasks from the queue |
471 |
dl |
1.2 |
*/ |
472 |
dl |
1.5 |
public void testPurge() { |
473 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
474 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
475 |
dl |
1.4 |
for(int i = 0; i < 5; i++){ |
476 |
dl |
1.8 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
477 |
dl |
1.2 |
} |
478 |
dl |
1.4 |
int max = 5; |
479 |
|
|
if (tasks[4].cancel(true)) --max; |
480 |
|
|
if (tasks[3].cancel(true)) --max; |
481 |
dl |
1.5 |
p1.purge(); |
482 |
|
|
long count = p1.getTaskCount(); |
483 |
dl |
1.4 |
assertTrue(count > 0 && count <= max); |
484 |
dl |
1.5 |
joinPool(p1); |
485 |
dl |
1.2 |
} |
486 |
|
|
|
487 |
|
|
/** |
488 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
489 |
dl |
1.2 |
*/ |
490 |
dl |
1.5 |
public void testShutDownNow() { |
491 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
492 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
493 |
dl |
1.8 |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
dl |
1.5 |
List l = p1.shutdownNow(); |
495 |
|
|
assertTrue(p1.isShutdown()); |
496 |
dl |
1.2 |
assertTrue(l.size() > 0 && l.size() <= 5); |
497 |
dl |
1.5 |
joinPool(p1); |
498 |
dl |
1.2 |
} |
499 |
|
|
|
500 |
dl |
1.5 |
/** |
501 |
dl |
1.6 |
* In default setting, shutdown cancels periodic but not delayed |
502 |
|
|
* tasks at shutdown |
503 |
dl |
1.5 |
*/ |
504 |
|
|
public void testShutDown1() { |
505 |
dl |
1.2 |
try { |
506 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
507 |
dl |
1.5 |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
508 |
|
|
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
509 |
dl |
1.2 |
|
510 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
511 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
512 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
513 |
|
|
p1.shutdown(); |
514 |
|
|
BlockingQueue q = p1.getQueue(); |
515 |
dl |
1.2 |
for (Iterator it = q.iterator(); it.hasNext();) { |
516 |
dl |
1.9 |
ScheduledFuture t = (ScheduledFuture)it.next(); |
517 |
dl |
1.2 |
assertFalse(t.isCancelled()); |
518 |
|
|
} |
519 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
520 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
521 |
dl |
1.2 |
for (int i = 0; i < 5; ++i) { |
522 |
|
|
assertTrue(tasks[i].isDone()); |
523 |
|
|
assertFalse(tasks[i].isCancelled()); |
524 |
|
|
} |
525 |
|
|
|
526 |
|
|
} |
527 |
|
|
catch(Exception ex) { |
528 |
dl |
1.5 |
unexpectedException(); |
529 |
dl |
1.2 |
} |
530 |
|
|
} |
531 |
|
|
|
532 |
|
|
|
533 |
dl |
1.5 |
/** |
534 |
dl |
1.6 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
535 |
|
|
* delayed tasks are cancelled at shutdown |
536 |
dl |
1.5 |
*/ |
537 |
|
|
public void testShutDown2() { |
538 |
dl |
1.2 |
try { |
539 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
540 |
dl |
1.5 |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
541 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
542 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
543 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
|
|
p1.shutdown(); |
545 |
|
|
assertTrue(p1.isShutdown()); |
546 |
|
|
BlockingQueue q = p1.getQueue(); |
547 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
548 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
549 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
550 |
dl |
1.2 |
} |
551 |
|
|
catch(Exception ex) { |
552 |
dl |
1.5 |
unexpectedException(); |
553 |
dl |
1.2 |
} |
554 |
|
|
} |
555 |
|
|
|
556 |
|
|
|
557 |
dl |
1.5 |
/** |
558 |
dl |
1.6 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
559 |
|
|
* periodic tasks are not cancelled at shutdown |
560 |
dl |
1.5 |
*/ |
561 |
|
|
public void testShutDown3() { |
562 |
dl |
1.2 |
try { |
563 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
564 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
565 |
dl |
1.9 |
ScheduledFuture task = |
566 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
567 |
|
|
p1.shutdown(); |
568 |
|
|
assertTrue(p1.isShutdown()); |
569 |
|
|
BlockingQueue q = p1.getQueue(); |
570 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
571 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
572 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
573 |
dl |
1.2 |
} |
574 |
|
|
catch(Exception ex) { |
575 |
dl |
1.5 |
unexpectedException(); |
576 |
dl |
1.2 |
} |
577 |
|
|
} |
578 |
|
|
|
579 |
dl |
1.5 |
/** |
580 |
dl |
1.6 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
581 |
|
|
* periodic tasks are cancelled at shutdown |
582 |
dl |
1.5 |
*/ |
583 |
|
|
public void testShutDown4() { |
584 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
585 |
dl |
1.2 |
try { |
586 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
587 |
dl |
1.9 |
ScheduledFuture task = |
588 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
589 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
590 |
dl |
1.5 |
p1.shutdown(); |
591 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
592 |
dl |
1.5 |
assertFalse(p1.isTerminated()); |
593 |
|
|
assertTrue(p1.isShutdown()); |
594 |
dl |
1.2 |
Thread.sleep(SHORT_DELAY_MS); |
595 |
|
|
assertFalse(task.isCancelled()); |
596 |
|
|
task.cancel(true); |
597 |
|
|
assertTrue(task.isCancelled()); |
598 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
599 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
600 |
dl |
1.2 |
} |
601 |
|
|
catch(Exception ex) { |
602 |
dl |
1.5 |
unexpectedException(); |
603 |
dl |
1.2 |
} |
604 |
|
|
finally { |
605 |
dl |
1.5 |
p1.shutdownNow(); |
606 |
dl |
1.2 |
} |
607 |
dl |
1.1 |
} |
608 |
|
|
|
609 |
|
|
} |