ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.22
Committed: Mon Nov 2 20:28:32 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +36 -36 lines
Log Message:
whitespace

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