ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.26
Committed: Sat Nov 21 02:07:27 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +20 -20 lines
Log Message:
untabify

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.13 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.22 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.25 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.18 import java.util.concurrent.atomic.*;
14 dl 1.1
15 dl 1.4 public class ScheduledExecutorTest extends JSR166TestCase {
16 dl 1.1 public static void main(String[] args) {
17 jsr166 1.26 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
19     public static Test suite() {
20 jsr166 1.26 return new TestSuite(ScheduledExecutorTest.class);
21 dl 1.1 }
22    
23    
24 dl 1.5 /**
25 dl 1.6 * execute successfully executes a runnable
26 dl 1.5 */
27 jsr166 1.25 public void testExecute() throws InterruptedException {
28     TrackedShortRunnable runnable =new TrackedShortRunnable();
29     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
30     p1.execute(runnable);
31     assertFalse(runnable.done);
32     Thread.sleep(SHORT_DELAY_MS);
33     try { p1.shutdown(); } catch (SecurityException ok) { return; }
34     Thread.sleep(MEDIUM_DELAY_MS);
35     assertTrue(runnable.done);
36     try { p1.shutdown(); } catch (SecurityException ok) { return; }
37     joinPool(p1);
38 dl 1.1 }
39    
40 dl 1.6
41 dl 1.5 /**
42 dl 1.6 * delayed schedule of callable successfully executes after delay
43 dl 1.5 */
44 jsr166 1.25 public void testSchedule1() throws Exception {
45     TrackedCallable callable = new TrackedCallable();
46     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
47     Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
48     assertFalse(callable.done);
49     Thread.sleep(MEDIUM_DELAY_MS);
50     assertTrue(callable.done);
51     assertEquals(Boolean.TRUE, f.get());
52     try { p1.shutdown(); } catch (SecurityException ok) { return; }
53     joinPool(p1);
54 dl 1.1 }
55    
56     /**
57 dl 1.6 * delayed schedule of runnable successfully executes after delay
58 dl 1.1 */
59 jsr166 1.25 public void testSchedule3() throws InterruptedException {
60     TrackedShortRunnable runnable = new TrackedShortRunnable();
61     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
62     p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
63     Thread.sleep(SHORT_DELAY_MS);
64     assertFalse(runnable.done);
65     Thread.sleep(MEDIUM_DELAY_MS);
66     assertTrue(runnable.done);
67     try { p1.shutdown(); } catch (SecurityException ok) { return; }
68     joinPool(p1);
69 dl 1.1 }
70 jsr166 1.22
71 dl 1.1 /**
72 dl 1.6 * scheduleAtFixedRate executes runnable after given initial delay
73 dl 1.1 */
74 jsr166 1.25 public void testSchedule4() throws InterruptedException {
75     TrackedShortRunnable runnable = new TrackedShortRunnable();
76     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
77     ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
78     assertFalse(runnable.done);
79     Thread.sleep(MEDIUM_DELAY_MS);
80     assertTrue(runnable.done);
81     h.cancel(true);
82     joinPool(p1);
83 dl 1.6 }
84    
85 dl 1.18 static class RunnableCounter implements Runnable {
86     AtomicInteger count = new AtomicInteger(0);
87     public void run() { count.getAndIncrement(); }
88     }
89    
90 dl 1.6 /**
91     * scheduleWithFixedDelay executes runnable after given initial delay
92     */
93 jsr166 1.25 public void testSchedule5() throws InterruptedException {
94     TrackedShortRunnable runnable = new TrackedShortRunnable();
95     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
96     ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
97     assertFalse(runnable.done);
98     Thread.sleep(MEDIUM_DELAY_MS);
99     assertTrue(runnable.done);
100     h.cancel(true);
101     joinPool(p1);
102 dl 1.1 }
103 jsr166 1.22
104 dl 1.6 /**
105 dl 1.18 * scheduleAtFixedRate executes series of tasks at given rate
106     */
107 jsr166 1.25 public void testFixedRateSequence() throws InterruptedException {
108     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
109     RunnableCounter counter = new RunnableCounter();
110     ScheduledFuture h =
111     p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
112     Thread.sleep(SMALL_DELAY_MS);
113     h.cancel(true);
114     int c = counter.count.get();
115     // By time scaling conventions, we must have at least
116     // an execution per SHORT delay, but no more than one SHORT more
117     assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
118     assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
119     joinPool(p1);
120 dl 1.18 }
121    
122     /**
123     * scheduleWithFixedDelay executes series of tasks with given period
124     */
125 jsr166 1.25 public void testFixedDelaySequence() throws InterruptedException {
126     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
127     RunnableCounter counter = new RunnableCounter();
128     ScheduledFuture h =
129     p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
130     Thread.sleep(SMALL_DELAY_MS);
131     h.cancel(true);
132     int c = counter.count.get();
133     assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
134     assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
135     joinPool(p1);
136 dl 1.18 }
137    
138    
139     /**
140 dl 1.6 * execute (null) throws NPE
141     */
142 jsr166 1.25 public void testExecuteNull() throws InterruptedException {
143 dl 1.9 ScheduledThreadPoolExecutor se = null;
144 dl 1.6 try {
145 jsr166 1.26 se = new ScheduledThreadPoolExecutor(1);
146     se.execute(null);
147 dl 1.6 shouldThrow();
148 jsr166 1.26 } catch (NullPointerException success) {}
149 jsr166 1.22
150 jsr166 1.26 joinPool(se);
151 dl 1.6 }
152    
153     /**
154     * schedule (null) throws NPE
155     */
156 jsr166 1.25 public void testScheduleNull() throws InterruptedException {
157 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
158 jsr166 1.26 try {
159 dl 1.7 TrackedCallable callable = null;
160 jsr166 1.26 Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
161 dl 1.6 shouldThrow();
162 jsr166 1.26 } catch (NullPointerException success) {}
163     joinPool(se);
164 dl 1.6 }
165 jsr166 1.22
166 dl 1.1 /**
167 dl 1.6 * execute throws RejectedExecutionException if shutdown
168 dl 1.1 */
169 jsr166 1.25 public void testSchedule1_RejectedExecutionException() throws InterruptedException {
170 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
171 dl 1.5 try {
172 dl 1.1 se.shutdown();
173 dl 1.4 se.schedule(new NoOpRunnable(),
174 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
175 dl 1.5 shouldThrow();
176 jsr166 1.24 } catch (RejectedExecutionException success) {
177 dl 1.17 } catch (SecurityException ok) {
178 dl 1.4 }
179 jsr166 1.22
180 dl 1.4 joinPool(se);
181    
182 dl 1.1 }
183    
184     /**
185 dl 1.6 * schedule throws RejectedExecutionException if shutdown
186 dl 1.1 */
187 jsr166 1.25 public void testSchedule2_RejectedExecutionException() throws InterruptedException {
188 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
189 dl 1.5 try {
190 dl 1.1 se.shutdown();
191 dl 1.4 se.schedule(new NoOpCallable(),
192 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
193 dl 1.5 shouldThrow();
194 jsr166 1.24 } catch (RejectedExecutionException success) {
195 dl 1.17 } catch (SecurityException ok) {
196 dl 1.4 }
197     joinPool(se);
198 dl 1.1 }
199    
200     /**
201 dl 1.6 * schedule callable throws RejectedExecutionException if shutdown
202 dl 1.1 */
203 jsr166 1.25 public void testSchedule3_RejectedExecutionException() throws InterruptedException {
204 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
205 dl 1.5 try {
206 dl 1.1 se.shutdown();
207 dl 1.4 se.schedule(new NoOpCallable(),
208 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
209 dl 1.5 shouldThrow();
210 jsr166 1.24 } catch (RejectedExecutionException success) {
211 dl 1.17 } catch (SecurityException ok) {
212     }
213 dl 1.4 joinPool(se);
214 dl 1.1 }
215    
216     /**
217 dl 1.6 * scheduleAtFixedRate throws RejectedExecutionException if shutdown
218 dl 1.1 */
219 jsr166 1.25 public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
220 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
221 dl 1.5 try {
222 dl 1.1 se.shutdown();
223 dl 1.4 se.scheduleAtFixedRate(new NoOpRunnable(),
224 jsr166 1.25 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
225 dl 1.5 shouldThrow();
226 jsr166 1.24 } catch (RejectedExecutionException success) {
227 dl 1.17 } catch (SecurityException ok) {
228 jsr166 1.22 }
229 dl 1.4 joinPool(se);
230 dl 1.1 }
231 jsr166 1.22
232 dl 1.1 /**
233 dl 1.6 * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
234 dl 1.1 */
235 jsr166 1.25 public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
236 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
237 dl 1.5 try {
238 dl 1.1 se.shutdown();
239 dl 1.4 se.scheduleWithFixedDelay(new NoOpRunnable(),
240 jsr166 1.25 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
241 dl 1.5 shouldThrow();
242 jsr166 1.24 } catch (RejectedExecutionException success) {
243 dl 1.17 } catch (SecurityException ok) {
244 jsr166 1.22 }
245 dl 1.4 joinPool(se);
246 dl 1.1 }
247    
248     /**
249 dl 1.6 * getActiveCount increases but doesn't overestimate, when a
250     * thread becomes active
251 dl 1.2 */
252 jsr166 1.25 public void testGetActiveCount() throws InterruptedException {
253 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
254 dl 1.5 assertEquals(0, p2.getActiveCount());
255     p2.execute(new SmallRunnable());
256 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
257 dl 1.5 assertEquals(1, p2.getActiveCount());
258     joinPool(p2);
259 dl 1.2 }
260 jsr166 1.22
261 dl 1.2 /**
262 dl 1.6 * getCompletedTaskCount increases, but doesn't overestimate,
263     * when tasks complete
264 dl 1.2 */
265 jsr166 1.25 public void testGetCompletedTaskCount() throws InterruptedException {
266 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
267 dl 1.5 assertEquals(0, p2.getCompletedTaskCount());
268     p2.execute(new SmallRunnable());
269 jsr166 1.25 Thread.sleep(MEDIUM_DELAY_MS);
270 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
271     joinPool(p2);
272 dl 1.2 }
273 jsr166 1.22
274 dl 1.2 /**
275 jsr166 1.22 * getCorePoolSize returns size given in constructor if not otherwise set
276 dl 1.2 */
277 jsr166 1.25 public void testGetCorePoolSize() throws InterruptedException {
278 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
279 dl 1.5 assertEquals(1, p1.getCorePoolSize());
280     joinPool(p1);
281 dl 1.2 }
282 jsr166 1.22
283 dl 1.2 /**
284 dl 1.6 * getLargestPoolSize increases, but doesn't overestimate, when
285     * multiple threads active
286 dl 1.2 */
287 jsr166 1.25 public void testGetLargestPoolSize() throws InterruptedException {
288 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
289 dl 1.5 assertEquals(0, p2.getLargestPoolSize());
290     p2.execute(new SmallRunnable());
291     p2.execute(new SmallRunnable());
292 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
293 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
294     joinPool(p2);
295 dl 1.2 }
296 jsr166 1.22
297 dl 1.2 /**
298 dl 1.6 * getPoolSize increases, but doesn't overestimate, when threads
299     * become active
300 dl 1.2 */
301 jsr166 1.25 public void testGetPoolSize() throws InterruptedException {
302 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
303 dl 1.5 assertEquals(0, p1.getPoolSize());
304     p1.execute(new SmallRunnable());
305     assertEquals(1, p1.getPoolSize());
306     joinPool(p1);
307 dl 1.2 }
308 jsr166 1.22
309 dl 1.2 /**
310 dl 1.6 * getTaskCount increases, but doesn't overestimate, when tasks
311     * submitted
312 dl 1.2 */
313 jsr166 1.25 public void testGetTaskCount() throws InterruptedException {
314 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
315 dl 1.5 assertEquals(0, p1.getTaskCount());
316 jsr166 1.23 for (int i = 0; i < 5; i++)
317 dl 1.5 p1.execute(new SmallRunnable());
318 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
319 dl 1.5 assertEquals(5, p1.getTaskCount());
320     joinPool(p1);
321 dl 1.2 }
322 dl 1.8
323 jsr166 1.22 /**
324 dl 1.8 * getThreadFactory returns factory in constructor if not set
325     */
326 jsr166 1.25 public void testGetThreadFactory() throws InterruptedException {
327 dl 1.8 ThreadFactory tf = new SimpleThreadFactory();
328 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
329 dl 1.8 assertSame(tf, p.getThreadFactory());
330     joinPool(p);
331     }
332    
333 jsr166 1.22 /**
334 dl 1.8 * setThreadFactory sets the thread factory returned by getThreadFactory
335     */
336 jsr166 1.25 public void testSetThreadFactory() throws InterruptedException {
337 dl 1.8 ThreadFactory tf = new SimpleThreadFactory();
338 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
339 dl 1.8 p.setThreadFactory(tf);
340     assertSame(tf, p.getThreadFactory());
341     joinPool(p);
342     }
343    
344 jsr166 1.22 /**
345 dl 1.8 * setThreadFactory(null) throws NPE
346     */
347 jsr166 1.25 public void testSetThreadFactoryNull() throws InterruptedException {
348 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
349 dl 1.8 try {
350     p.setThreadFactory(null);
351     shouldThrow();
352     } catch (NullPointerException success) {
353     } finally {
354     joinPool(p);
355     }
356     }
357 jsr166 1.22
358 dl 1.2 /**
359 dl 1.6 * is isShutDown is false before shutdown, true after
360 dl 1.2 */
361 dl 1.5 public void testIsShutdown() {
362 jsr166 1.22
363 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
364 dl 1.2 try {
365 dl 1.5 assertFalse(p1.isShutdown());
366 dl 1.2 }
367     finally {
368 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
369 dl 1.2 }
370 jsr166 1.26 assertTrue(p1.isShutdown());
371 dl 1.2 }
372    
373 jsr166 1.22
374 dl 1.2 /**
375 dl 1.6 * isTerminated is false before termination, true after
376 dl 1.2 */
377 jsr166 1.25 public void testIsTerminated() throws InterruptedException {
378 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
379 dl 1.2 try {
380 dl 1.5 p1.execute(new SmallRunnable());
381 dl 1.2 } finally {
382 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
383 dl 1.2 }
384 jsr166 1.25 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
385     assertTrue(p1.isTerminated());
386 dl 1.5 }
387    
388     /**
389 dl 1.6 * isTerminating is not true when running or when terminated
390 dl 1.5 */
391 jsr166 1.25 public void testIsTerminating() throws InterruptedException {
392 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
393 dl 1.5 assertFalse(p1.isTerminating());
394     try {
395     p1.execute(new SmallRunnable());
396     assertFalse(p1.isTerminating());
397     } finally {
398 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
399 dl 1.5 }
400 jsr166 1.25
401     assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
402     assertTrue(p1.isTerminated());
403     assertFalse(p1.isTerminating());
404 dl 1.2 }
405    
406     /**
407 dl 1.8 * getQueue returns the work queue, which contains queued tasks
408     */
409 jsr166 1.25 public void testGetQueue() throws InterruptedException {
410 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
411     ScheduledFuture[] tasks = new ScheduledFuture[5];
412 jsr166 1.24 for (int i = 0; i < 5; i++) {
413 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
414 dl 1.8 }
415     try {
416     Thread.sleep(SHORT_DELAY_MS);
417     BlockingQueue<Runnable> q = p1.getQueue();
418     assertTrue(q.contains(tasks[4]));
419     assertFalse(q.contains(tasks[0]));
420     } finally {
421     joinPool(p1);
422     }
423     }
424    
425     /**
426     * remove(task) removes queued task, and fails to remove active task
427     */
428 jsr166 1.25 public void testRemove() throws InterruptedException {
429 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
430     ScheduledFuture[] tasks = new ScheduledFuture[5];
431 jsr166 1.24 for (int i = 0; i < 5; i++) {
432 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
433 dl 1.8 }
434     try {
435     Thread.sleep(SHORT_DELAY_MS);
436     BlockingQueue<Runnable> q = p1.getQueue();
437     assertFalse(p1.remove((Runnable)tasks[0]));
438     assertTrue(q.contains((Runnable)tasks[4]));
439     assertTrue(q.contains((Runnable)tasks[3]));
440     assertTrue(p1.remove((Runnable)tasks[4]));
441     assertFalse(p1.remove((Runnable)tasks[4]));
442     assertFalse(q.contains((Runnable)tasks[4]));
443     assertTrue(q.contains((Runnable)tasks[3]));
444     assertTrue(p1.remove((Runnable)tasks[3]));
445     assertFalse(q.contains((Runnable)tasks[3]));
446     } finally {
447     joinPool(p1);
448     }
449     }
450    
451     /**
452     * purge removes cancelled tasks from the queue
453 dl 1.2 */
454 jsr166 1.25 public void testPurge() throws InterruptedException {
455 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
456     ScheduledFuture[] tasks = new ScheduledFuture[5];
457 jsr166 1.24 for (int i = 0; i < 5; i++) {
458 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
459 dl 1.19 }
460     try {
461     int max = 5;
462     if (tasks[4].cancel(true)) --max;
463     if (tasks[3].cancel(true)) --max;
464     // There must eventually be an interference-free point at
465     // which purge will not fail. (At worst, when queue is empty.)
466     int k;
467     for (k = 0; k < SMALL_DELAY_MS; ++k) {
468     p1.purge();
469     long count = p1.getTaskCount();
470     if (count >= 0 && count <= max)
471     break;
472     Thread.sleep(1);
473     }
474     assertTrue(k < SMALL_DELAY_MS);
475     } finally {
476     joinPool(p1);
477 dl 1.2 }
478     }
479    
480     /**
481 dl 1.6 * shutDownNow returns a list containing tasks that were not run
482 dl 1.2 */
483 jsr166 1.25 public void testShutDownNow() throws InterruptedException {
484 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
485 jsr166 1.23 for (int i = 0; i < 5; i++)
486 jsr166 1.25 p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
487 dl 1.17 List l;
488     try {
489     l = p1.shutdownNow();
490 jsr166 1.22 } catch (SecurityException ok) {
491 dl 1.17 return;
492     }
493 jsr166 1.26 assertTrue(p1.isShutdown());
494     assertTrue(l.size() > 0 && l.size() <= 5);
495 dl 1.5 joinPool(p1);
496 dl 1.2 }
497    
498 dl 1.5 /**
499 dl 1.6 * In default setting, shutdown cancels periodic but not delayed
500     * tasks at shutdown
501 dl 1.5 */
502 jsr166 1.25 public void testShutDown1() throws InterruptedException {
503     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
504     assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
505     assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
506 jsr166 1.22
507 jsr166 1.25 ScheduledFuture[] tasks = new ScheduledFuture[5];
508     for (int i = 0; i < 5; i++)
509     tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
510     try { p1.shutdown(); } catch (SecurityException ok) { return; }
511     BlockingQueue q = p1.getQueue();
512     for (Iterator it = q.iterator(); it.hasNext();) {
513     ScheduledFuture t = (ScheduledFuture)it.next();
514     assertFalse(t.isCancelled());
515     }
516     assertTrue(p1.isShutdown());
517     Thread.sleep(SMALL_DELAY_MS);
518     for (int i = 0; i < 5; ++i) {
519     assertTrue(tasks[i].isDone());
520     assertFalse(tasks[i].isCancelled());
521 dl 1.2 }
522     }
523    
524    
525 dl 1.5 /**
526 dl 1.6 * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
527     * delayed tasks are cancelled at shutdown
528 dl 1.5 */
529 jsr166 1.25 public void testShutDown2() throws InterruptedException {
530     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
531     p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
532     ScheduledFuture[] tasks = new ScheduledFuture[5];
533     for (int i = 0; i < 5; i++)
534     tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
535     try { p1.shutdown(); } catch (SecurityException ok) { return; }
536     assertTrue(p1.isShutdown());
537     BlockingQueue q = p1.getQueue();
538     assertTrue(q.isEmpty());
539     Thread.sleep(SMALL_DELAY_MS);
540     assertTrue(p1.isTerminated());
541 dl 1.2 }
542    
543    
544 dl 1.5 /**
545 dl 1.6 * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
546     * periodic tasks are not cancelled at shutdown
547 dl 1.5 */
548 jsr166 1.25 public void testShutDown3() throws InterruptedException {
549     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
550     p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
551     ScheduledFuture task =
552     p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
553     try { p1.shutdown(); } catch (SecurityException ok) { return; }
554     assertTrue(p1.isShutdown());
555     BlockingQueue q = p1.getQueue();
556     assertTrue(q.isEmpty());
557     Thread.sleep(SHORT_DELAY_MS);
558     assertTrue(p1.isTerminated());
559 dl 1.2 }
560    
561 dl 1.5 /**
562 dl 1.6 * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
563     * periodic tasks are cancelled at shutdown
564 dl 1.5 */
565 jsr166 1.25 public void testShutDown4() throws InterruptedException {
566 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
567 dl 1.2 try {
568 dl 1.5 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
569 dl 1.9 ScheduledFuture task =
570 jsr166 1.25 p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
571 dl 1.2 assertFalse(task.isCancelled());
572 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
573 dl 1.2 assertFalse(task.isCancelled());
574 dl 1.5 assertFalse(p1.isTerminated());
575     assertTrue(p1.isShutdown());
576 dl 1.2 Thread.sleep(SHORT_DELAY_MS);
577     assertFalse(task.isCancelled());
578 dl 1.16 assertTrue(task.cancel(true));
579 dl 1.15 assertTrue(task.isDone());
580 dl 1.2 Thread.sleep(SHORT_DELAY_MS);
581 dl 1.5 assertTrue(p1.isTerminated());
582 dl 1.2 }
583 jsr166 1.22 finally {
584 dl 1.17 joinPool(p1);
585 dl 1.2 }
586 dl 1.1 }
587    
588 dl 1.10 /**
589     * completed submit of callable returns result
590     */
591 jsr166 1.25 public void testSubmitCallable() throws Exception {
592 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
593     try {
594     Future<String> future = e.submit(new StringTask());
595     String result = future.get();
596     assertSame(TEST_STRING, result);
597     } finally {
598     joinPool(e);
599     }
600     }
601    
602     /**
603     * completed submit of runnable returns successfully
604     */
605 jsr166 1.25 public void testSubmitRunnable() throws Exception {
606 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
607     try {
608     Future<?> future = e.submit(new NoOpRunnable());
609     future.get();
610     assertTrue(future.isDone());
611     } finally {
612     joinPool(e);
613     }
614     }
615    
616     /**
617     * completed submit of (runnable, result) returns result
618     */
619 jsr166 1.25 public void testSubmitRunnable2() throws Exception {
620 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
621     try {
622     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
623     String result = future.get();
624     assertSame(TEST_STRING, result);
625     } finally {
626     joinPool(e);
627     }
628     }
629    
630     /**
631     * invokeAny(null) throws NPE
632     */
633 jsr166 1.25 public void testInvokeAny1() throws Exception {
634 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
635     try {
636     e.invokeAny(null);
637 jsr166 1.25 shouldThrow();
638 dl 1.10 } catch (NullPointerException success) {
639     } finally {
640     joinPool(e);
641     }
642     }
643    
644     /**
645     * invokeAny(empty collection) throws IAE
646     */
647 jsr166 1.25 public void testInvokeAny2() throws Exception {
648 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
649     try {
650     e.invokeAny(new ArrayList<Callable<String>>());
651 jsr166 1.25 shouldThrow();
652 dl 1.10 } catch (IllegalArgumentException success) {
653     } finally {
654     joinPool(e);
655     }
656     }
657    
658     /**
659     * invokeAny(c) throws NPE if c has null elements
660     */
661 jsr166 1.25 public void testInvokeAny3() throws Exception {
662     final CountDownLatch latch = new CountDownLatch(1);
663 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
664     try {
665     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
666 jsr166 1.25 l.add(new Callable<String>() {
667     public String call() {
668     try {
669     latch.await();
670     } catch (InterruptedException ok) {}
671     return TEST_STRING;
672     }});
673 dl 1.10 l.add(null);
674     e.invokeAny(l);
675 jsr166 1.25 shouldThrow();
676 dl 1.10 } catch (NullPointerException success) {
677     } finally {
678 jsr166 1.25 latch.countDown();
679 dl 1.10 joinPool(e);
680     }
681     }
682    
683     /**
684     * invokeAny(c) throws ExecutionException if no task completes
685     */
686 jsr166 1.25 public void testInvokeAny4() throws Exception {
687 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
688     try {
689     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
690     l.add(new NPETask());
691     e.invokeAny(l);
692 jsr166 1.25 shouldThrow();
693 dl 1.10 } catch (ExecutionException success) {
694 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
695 dl 1.10 } finally {
696     joinPool(e);
697     }
698     }
699    
700     /**
701     * invokeAny(c) returns result of some task
702     */
703 jsr166 1.25 public void testInvokeAny5() throws Exception {
704 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
705     try {
706     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
707     l.add(new StringTask());
708     l.add(new StringTask());
709     String result = e.invokeAny(l);
710     assertSame(TEST_STRING, result);
711     } finally {
712     joinPool(e);
713     }
714     }
715    
716     /**
717     * invokeAll(null) throws NPE
718     */
719 jsr166 1.25 public void testInvokeAll1() throws Exception {
720 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
721     try {
722     e.invokeAll(null);
723 jsr166 1.25 shouldThrow();
724 dl 1.10 } catch (NullPointerException success) {
725     } finally {
726     joinPool(e);
727     }
728     }
729    
730     /**
731     * invokeAll(empty collection) returns empty collection
732     */
733 jsr166 1.25 public void testInvokeAll2() throws Exception {
734 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
735     try {
736     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
737     assertTrue(r.isEmpty());
738     } finally {
739     joinPool(e);
740     }
741     }
742    
743     /**
744     * invokeAll(c) throws NPE if c has null elements
745     */
746 jsr166 1.25 public void testInvokeAll3() throws Exception {
747 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
748     try {
749     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
750     l.add(new StringTask());
751     l.add(null);
752     e.invokeAll(l);
753 jsr166 1.25 shouldThrow();
754 dl 1.10 } catch (NullPointerException success) {
755     } finally {
756     joinPool(e);
757     }
758     }
759    
760     /**
761     * get of invokeAll(c) throws exception on failed task
762     */
763 jsr166 1.25 public void testInvokeAll4() throws Exception {
764 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
765     try {
766     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
767     l.add(new NPETask());
768     List<Future<String>> result = e.invokeAll(l);
769     assertEquals(1, result.size());
770 jsr166 1.25 for (Future<String> future : result)
771     future.get();
772     shouldThrow();
773 jsr166 1.23 } catch (ExecutionException success) {
774 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
775 dl 1.10 } finally {
776     joinPool(e);
777     }
778     }
779    
780     /**
781     * invokeAll(c) returns results of all completed tasks
782     */
783 jsr166 1.25 public void testInvokeAll5() throws Exception {
784 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
785     try {
786     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
787     l.add(new StringTask());
788     l.add(new StringTask());
789     List<Future<String>> result = e.invokeAll(l);
790     assertEquals(2, result.size());
791 jsr166 1.25 for (Future<String> future : result)
792     assertSame(TEST_STRING, future.get());
793 dl 1.10 } finally {
794     joinPool(e);
795     }
796     }
797    
798 dl 1.11 /**
799     * timed invokeAny(null) throws NPE
800     */
801 jsr166 1.25 public void testTimedInvokeAny1() throws Exception {
802 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
803     try {
804 jsr166 1.25 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
805     shouldThrow();
806 dl 1.11 } catch (NullPointerException success) {
807     } finally {
808     joinPool(e);
809     }
810     }
811    
812     /**
813     * timed invokeAny(,,null) throws NPE
814     */
815 jsr166 1.25 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
816 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
817     try {
818     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
819     l.add(new StringTask());
820     e.invokeAny(l, MEDIUM_DELAY_MS, null);
821 jsr166 1.25 shouldThrow();
822 dl 1.11 } catch (NullPointerException success) {
823     } finally {
824     joinPool(e);
825     }
826     }
827    
828     /**
829     * timed invokeAny(empty collection) throws IAE
830     */
831 jsr166 1.25 public void testTimedInvokeAny2() throws Exception {
832 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
833     try {
834 jsr166 1.25 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
835     shouldThrow();
836 dl 1.11 } catch (IllegalArgumentException success) {
837     } finally {
838     joinPool(e);
839     }
840     }
841    
842     /**
843     * timed invokeAny(c) throws NPE if c has null elements
844     */
845 jsr166 1.25 public void testTimedInvokeAny3() throws Exception {
846     final CountDownLatch latch = new CountDownLatch(1);
847 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
848     try {
849     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
850 jsr166 1.25 l.add(new Callable<String>() {
851     public String call() {
852     try {
853     latch.await();
854     } catch (InterruptedException ok) {}
855     return TEST_STRING;
856     }});
857 dl 1.11 l.add(null);
858 jsr166 1.25 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
859     shouldThrow();
860 dl 1.11 } catch (NullPointerException success) {
861     } finally {
862 jsr166 1.25 latch.countDown();
863 dl 1.11 joinPool(e);
864     }
865     }
866    
867     /**
868     * timed invokeAny(c) throws ExecutionException if no task completes
869     */
870 jsr166 1.25 public void testTimedInvokeAny4() throws Exception {
871 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
872     try {
873     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
874     l.add(new NPETask());
875 jsr166 1.25 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
876     shouldThrow();
877 jsr166 1.23 } catch (ExecutionException success) {
878 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
879 dl 1.11 } finally {
880     joinPool(e);
881     }
882     }
883    
884     /**
885     * timed invokeAny(c) returns result of some task
886     */
887 jsr166 1.25 public void testTimedInvokeAny5() throws Exception {
888 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
889     try {
890     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
891     l.add(new StringTask());
892     l.add(new StringTask());
893 jsr166 1.25 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
894 dl 1.11 assertSame(TEST_STRING, result);
895     } finally {
896     joinPool(e);
897     }
898     }
899    
900     /**
901     * timed invokeAll(null) throws NPE
902     */
903 jsr166 1.25 public void testTimedInvokeAll1() throws Exception {
904 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
905     try {
906 jsr166 1.25 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
907     shouldThrow();
908 dl 1.11 } catch (NullPointerException success) {
909     } finally {
910     joinPool(e);
911     }
912     }
913    
914     /**
915     * timed invokeAll(,,null) throws NPE
916     */
917 jsr166 1.25 public void testTimedInvokeAllNullTimeUnit() throws Exception {
918 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
919     try {
920     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
921     l.add(new StringTask());
922     e.invokeAll(l, MEDIUM_DELAY_MS, null);
923 jsr166 1.25 shouldThrow();
924 dl 1.11 } catch (NullPointerException success) {
925     } finally {
926     joinPool(e);
927     }
928     }
929    
930     /**
931     * timed invokeAll(empty collection) returns empty collection
932     */
933 jsr166 1.25 public void testTimedInvokeAll2() throws Exception {
934 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
935     try {
936 jsr166 1.25 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
937 dl 1.11 assertTrue(r.isEmpty());
938     } finally {
939     joinPool(e);
940     }
941     }
942    
943     /**
944     * timed invokeAll(c) throws NPE if c has null elements
945     */
946 jsr166 1.25 public void testTimedInvokeAll3() throws Exception {
947 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
948     try {
949     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
950     l.add(new StringTask());
951     l.add(null);
952 jsr166 1.25 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
953     shouldThrow();
954 dl 1.11 } catch (NullPointerException success) {
955     } finally {
956     joinPool(e);
957     }
958     }
959    
960     /**
961     * get of element of invokeAll(c) throws exception on failed task
962     */
963 jsr166 1.25 public void testTimedInvokeAll4() throws Exception {
964 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
965     try {
966     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
967     l.add(new NPETask());
968 jsr166 1.25 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
969 dl 1.11 assertEquals(1, result.size());
970 jsr166 1.25 for (Future<String> future : result)
971     future.get();
972     shouldThrow();
973 jsr166 1.23 } catch (ExecutionException success) {
974 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
975 dl 1.11 } finally {
976     joinPool(e);
977     }
978     }
979    
980     /**
981     * timed invokeAll(c) returns results of all completed tasks
982     */
983 jsr166 1.25 public void testTimedInvokeAll5() throws Exception {
984 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
985     try {
986     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
987     l.add(new StringTask());
988     l.add(new StringTask());
989 jsr166 1.25 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
990 dl 1.11 assertEquals(2, result.size());
991 jsr166 1.25 for (Future<String> future : result)
992     assertSame(TEST_STRING, future.get());
993 dl 1.11 } finally {
994     joinPool(e);
995     }
996     }
997    
998     /**
999     * timed invokeAll(c) cancels tasks not completed by timeout
1000     */
1001 jsr166 1.25 public void testTimedInvokeAll6() throws Exception {
1002 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1003     try {
1004     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1005     l.add(new StringTask());
1006 dl 1.12 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1007 dl 1.14 l.add(new StringTask());
1008 jsr166 1.25 List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1009 dl 1.14 assertEquals(3, result.size());
1010 jsr166 1.22 Iterator<Future<String>> it = result.iterator();
1011 dl 1.11 Future<String> f1 = it.next();
1012     Future<String> f2 = it.next();
1013 dl 1.14 Future<String> f3 = it.next();
1014 dl 1.11 assertTrue(f1.isDone());
1015 dl 1.14 assertTrue(f2.isDone());
1016     assertTrue(f3.isDone());
1017 dl 1.11 assertFalse(f1.isCancelled());
1018     assertTrue(f2.isCancelled());
1019     } finally {
1020     joinPool(e);
1021     }
1022     }
1023    
1024 dl 1.10
1025 dl 1.1 }