ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.10
Committed: Mon Dec 22 00:48:56 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.9: +238 -0 lines
Log Message:
Add and adjust tests reflecting API changes

File Contents

# User Rev Content
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 dl 1.10 /**
610     * completed submit of callable returns result
611     */
612     public void testSubmitCallable() {
613     ExecutorService e = new ScheduledThreadPoolExecutor(2);
614     try {
615     Future<String> future = e.submit(new StringTask());
616     String result = future.get();
617     assertSame(TEST_STRING, result);
618     }
619     catch (ExecutionException ex) {
620     unexpectedException();
621     }
622     catch (InterruptedException ex) {
623     unexpectedException();
624     } finally {
625     joinPool(e);
626     }
627     }
628    
629     /**
630     * completed submit of runnable returns successfully
631     */
632     public void testSubmitRunnable() {
633     ExecutorService e = new ScheduledThreadPoolExecutor(2);
634     try {
635     Future<?> future = e.submit(new NoOpRunnable());
636     future.get();
637     assertTrue(future.isDone());
638     }
639     catch (ExecutionException ex) {
640     unexpectedException();
641     }
642     catch (InterruptedException ex) {
643     unexpectedException();
644     } finally {
645     joinPool(e);
646     }
647     }
648    
649     /**
650     * completed submit of (runnable, result) returns result
651     */
652     public void testSubmitRunnable2() {
653     ExecutorService e = new ScheduledThreadPoolExecutor(2);
654     try {
655     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
656     String result = future.get();
657     assertSame(TEST_STRING, result);
658     }
659     catch (ExecutionException ex) {
660     unexpectedException();
661     }
662     catch (InterruptedException ex) {
663     unexpectedException();
664     } finally {
665     joinPool(e);
666     }
667     }
668    
669    
670    
671    
672    
673     /**
674     * invokeAny(null) throws NPE
675     */
676     public void testInvokeAny1() {
677     ExecutorService e = new ScheduledThreadPoolExecutor(2);
678     try {
679     e.invokeAny(null);
680     } catch (NullPointerException success) {
681     } catch(Exception ex) {
682     unexpectedException();
683     } finally {
684     joinPool(e);
685     }
686     }
687    
688     /**
689     * invokeAny(empty collection) throws IAE
690     */
691     public void testInvokeAny2() {
692     ExecutorService e = new ScheduledThreadPoolExecutor(2);
693     try {
694     e.invokeAny(new ArrayList<Callable<String>>());
695     } catch (IllegalArgumentException success) {
696     } catch(Exception ex) {
697     unexpectedException();
698     } finally {
699     joinPool(e);
700     }
701     }
702    
703     /**
704     * invokeAny(c) throws NPE if c has null elements
705     */
706     public void testInvokeAny3() {
707     ExecutorService e = new ScheduledThreadPoolExecutor(2);
708     try {
709     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
710     l.add(new StringTask());
711     l.add(null);
712     e.invokeAny(l);
713     } catch (NullPointerException success) {
714     } catch(Exception ex) {
715     unexpectedException();
716     } finally {
717     joinPool(e);
718     }
719     }
720    
721     /**
722     * invokeAny(c) throws ExecutionException if no task completes
723     */
724     public void testInvokeAny4() {
725     ExecutorService e = new ScheduledThreadPoolExecutor(2);
726     try {
727     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
728     l.add(new NPETask());
729     e.invokeAny(l);
730     } catch (ExecutionException success) {
731     } catch(Exception ex) {
732     unexpectedException();
733     } finally {
734     joinPool(e);
735     }
736     }
737    
738     /**
739     * invokeAny(c) returns result of some task
740     */
741     public void testInvokeAny5() {
742     ExecutorService e = new ScheduledThreadPoolExecutor(2);
743     try {
744     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
745     l.add(new StringTask());
746     l.add(new StringTask());
747     String result = e.invokeAny(l);
748     assertSame(TEST_STRING, result);
749     } catch (ExecutionException success) {
750     } catch(Exception ex) {
751     unexpectedException();
752     } finally {
753     joinPool(e);
754     }
755     }
756    
757     /**
758     * invokeAll(null) throws NPE
759     */
760     public void testInvokeAll1() {
761     ExecutorService e = new ScheduledThreadPoolExecutor(2);
762     try {
763     e.invokeAll(null);
764     } catch (NullPointerException success) {
765     } catch(Exception ex) {
766     unexpectedException();
767     } finally {
768     joinPool(e);
769     }
770     }
771    
772     /**
773     * invokeAll(empty collection) returns empty collection
774     */
775     public void testInvokeAll2() {
776     ExecutorService e = new ScheduledThreadPoolExecutor(2);
777     try {
778     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
779     assertTrue(r.isEmpty());
780     } catch(Exception ex) {
781     unexpectedException();
782     } finally {
783     joinPool(e);
784     }
785     }
786    
787     /**
788     * invokeAll(c) throws NPE if c has null elements
789     */
790     public void testInvokeAll3() {
791     ExecutorService e = new ScheduledThreadPoolExecutor(2);
792     try {
793     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
794     l.add(new StringTask());
795     l.add(null);
796     e.invokeAll(l);
797     } catch (NullPointerException success) {
798     } catch(Exception ex) {
799     unexpectedException();
800     } finally {
801     joinPool(e);
802     }
803     }
804    
805     /**
806     * get of invokeAll(c) throws exception on failed task
807     */
808     public void testInvokeAll4() {
809     ExecutorService e = new ScheduledThreadPoolExecutor(2);
810     try {
811     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
812     l.add(new NPETask());
813     List<Future<String>> result = e.invokeAll(l);
814     assertEquals(1, result.size());
815     for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
816     it.next().get();
817     } catch(ExecutionException success) {
818     } catch(Exception ex) {
819     unexpectedException();
820     } finally {
821     joinPool(e);
822     }
823     }
824    
825     /**
826     * invokeAll(c) returns results of all completed tasks
827     */
828     public void testInvokeAll5() {
829     ExecutorService e = new ScheduledThreadPoolExecutor(2);
830     try {
831     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
832     l.add(new StringTask());
833     l.add(new StringTask());
834     List<Future<String>> result = e.invokeAll(l);
835     assertEquals(2, result.size());
836     for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
837     assertSame(TEST_STRING, it.next().get());
838     } catch (ExecutionException success) {
839     } catch(Exception ex) {
840     unexpectedException();
841     } finally {
842     joinPool(e);
843     }
844     }
845    
846    
847 dl 1.1 }