ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.17
Committed: Tue Jan 20 20:20:56 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.16: +26 -21 lines
Log Message:
Don't fail if test harness doesn't have sufficient permissions

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