ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.6 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.17 by dl, Tue Jan 20 20:20:56 2004 UTC

# Line 1 | Line 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.
2 > * 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   */
8  
9   import junit.framework.*;
# Line 17 | Line 18 | public class ScheduledExecutorTest exten
18          return new TestSuite(ScheduledExecutorTest.class);
19      }
20  
20    static class MyRunnable implements Runnable {
21        volatile boolean done = false;
22        public void run() {
23            try {
24                Thread.sleep(SMALL_DELAY_MS);
25                done = true;
26            } catch(Exception e){
27            }
28        }
29    }
30
31    static class MyCallable implements Callable {
32        volatile boolean done = false;
33        public Object call() {
34            try {
35                Thread.sleep(SMALL_DELAY_MS);
36                done = true;
37            } catch(Exception e){
38            }
39            return Boolean.TRUE;
40        }
41    }
21  
22      /**
23       * execute successfully executes a runnable
24       */
25      public void testExecute() {
26          try {
27 <            MyRunnable runnable =new MyRunnable();
28 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
27 >            TrackedShortRunnable runnable =new TrackedShortRunnable();
28 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
29              p1.execute(runnable);
30              assertFalse(runnable.done);
31              Thread.sleep(SHORT_DELAY_MS);
32 <            p1.shutdown();
32 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
33              try {
34                  Thread.sleep(MEDIUM_DELAY_MS);
35              } catch(InterruptedException e){
36                  unexpectedException();
37              }
38              assertTrue(runnable.done);
39 <            p1.shutdown();
39 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
40              joinPool(p1);
41          }
42          catch(Exception e){
# Line 72 | Line 51 | public class ScheduledExecutorTest exten
51       */
52      public void testSchedule1() {
53          try {
54 <            MyCallable callable = new MyCallable();
55 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
54 >            TrackedCallable callable = new TrackedCallable();
55 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
56              Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
57              assertFalse(callable.done);
58              Thread.sleep(MEDIUM_DELAY_MS);
59              assertTrue(callable.done);
60              assertEquals(Boolean.TRUE, f.get());
61 <            p1.shutdown();
61 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
62              joinPool(p1);
63          } catch(RejectedExecutionException e){}
64          catch(Exception e){
65 +            e.printStackTrace();
66              unexpectedException();
67          }
68      }
# Line 92 | Line 72 | public class ScheduledExecutorTest exten
72       */
73      public void testSchedule3() {
74          try {
75 <            MyRunnable runnable = new MyRunnable();
76 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
75 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
76 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
77              p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
78              Thread.sleep(SHORT_DELAY_MS);
79              assertFalse(runnable.done);
80              Thread.sleep(MEDIUM_DELAY_MS);
81              assertTrue(runnable.done);
82 <            p1.shutdown();
82 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
83              joinPool(p1);
84          } catch(Exception e){
85              unexpectedException();
# Line 111 | Line 91 | public class ScheduledExecutorTest exten
91       */
92      public void testSchedule4() {
93          try {
94 <            MyRunnable runnable = new MyRunnable();
95 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
96 <            ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
94 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
95 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
96 >            ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
97              assertFalse(runnable.done);
98              Thread.sleep(MEDIUM_DELAY_MS);
99              assertTrue(runnable.done);
100              h.cancel(true);
121            p1.shutdown();
101              joinPool(p1);
102          } catch(Exception e){
103              unexpectedException();
# Line 130 | Line 109 | public class ScheduledExecutorTest exten
109       */
110      public void testSchedule5() {
111          try {
112 <            MyRunnable runnable = new MyRunnable();
113 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
114 <            ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
112 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
113 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
114 >            ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
115              assertFalse(runnable.done);
116              Thread.sleep(MEDIUM_DELAY_MS);
117              assertTrue(runnable.done);
118              h.cancel(true);
140            p1.shutdown();
119              joinPool(p1);
120          } catch(Exception e){
121              unexpectedException();
# Line 148 | Line 126 | public class ScheduledExecutorTest exten
126       *  execute (null) throws NPE
127       */
128      public void testExecuteNull() {
129 <        ScheduledExecutor se = null;
129 >        ScheduledThreadPoolExecutor se = null;
130          try {
131 <            se = new ScheduledExecutor(1);
131 >            se = new ScheduledThreadPoolExecutor(1);
132              se.execute(null);
133              shouldThrow();
134          } catch(NullPointerException success){}
# Line 165 | Line 143 | public class ScheduledExecutorTest exten
143       * schedule (null) throws NPE
144       */
145      public void testScheduleNull() {
146 <        ScheduledExecutor se = new ScheduledExecutor(1);
146 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
147          try {
148 <            MyCallable callable = null;
148 >            TrackedCallable callable = null;
149              Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
150              shouldThrow();
151          } catch(NullPointerException success){}
# Line 181 | Line 159 | public class ScheduledExecutorTest exten
159       * execute throws RejectedExecutionException if shutdown
160       */
161      public void testSchedule1_RejectedExecutionException() {
162 <        ScheduledExecutor se = new ScheduledExecutor(1);
162 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
163          try {
164              se.shutdown();
165              se.schedule(new NoOpRunnable(),
166                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
167              shouldThrow();
168          } catch(RejectedExecutionException success){
169 +        } catch (SecurityException ok) {
170          }
171 +        
172          joinPool(se);
173  
174      }
# Line 197 | Line 177 | public class ScheduledExecutorTest exten
177       * schedule throws RejectedExecutionException if shutdown
178       */
179      public void testSchedule2_RejectedExecutionException() {
180 <        ScheduledExecutor se = new ScheduledExecutor(1);
180 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
181          try {
182              se.shutdown();
183              se.schedule(new NoOpCallable(),
184                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
185              shouldThrow();
186          } catch(RejectedExecutionException success){
187 +        } catch (SecurityException ok) {
188          }
189          joinPool(se);
190      }
# Line 212 | Line 193 | public class ScheduledExecutorTest exten
193       * schedule callable throws RejectedExecutionException if shutdown
194       */
195       public void testSchedule3_RejectedExecutionException() {
196 <         ScheduledExecutor se = new ScheduledExecutor(1);
196 >         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
197           try {
198              se.shutdown();
199              se.schedule(new NoOpCallable(),
200                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
201              shouldThrow();
202          } catch(RejectedExecutionException success){
203 <        }
203 >        } catch (SecurityException ok) {
204 >        }
205           joinPool(se);
206      }
207  
# Line 227 | Line 209 | public class ScheduledExecutorTest exten
209       *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
210       */
211      public void testScheduleAtFixedRate1_RejectedExecutionException() {
212 <        ScheduledExecutor se = new ScheduledExecutor(1);
212 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
213          try {
214              se.shutdown();
215              se.scheduleAtFixedRate(new NoOpRunnable(),
216                                     MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
217              shouldThrow();
218          } catch(RejectedExecutionException success){
219 +        } catch (SecurityException ok) {
220          }
221          joinPool(se);
222      }
# Line 242 | Line 225 | public class ScheduledExecutorTest exten
225       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
226       */
227      public void testScheduleWithFixedDelay1_RejectedExecutionException() {
228 <        ScheduledExecutor se = new ScheduledExecutor(1);
228 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
229          try {
230              se.shutdown();
231              se.scheduleWithFixedDelay(new NoOpRunnable(),
232                                        MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
233              shouldThrow();
234          } catch(RejectedExecutionException success){
235 +        } catch (SecurityException ok) {
236          }
237          joinPool(se);
238      }
# Line 258 | Line 242 | public class ScheduledExecutorTest exten
242       *  thread becomes active
243       */
244      public void testGetActiveCount() {
245 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
245 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
246          assertEquals(0, p2.getActiveCount());
247          p2.execute(new SmallRunnable());
248          try {
# Line 275 | Line 259 | public class ScheduledExecutorTest exten
259       *   when tasks complete
260       */
261      public void testGetCompletedTaskCount() {
262 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
262 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
263          assertEquals(0, p2.getCompletedTaskCount());
264          p2.execute(new SmallRunnable());
265          try {
# Line 291 | Line 275 | public class ScheduledExecutorTest exten
275       *  getCorePoolSize returns size given in constructor if not otherwise set
276       */
277      public void testGetCorePoolSize() {
278 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
278 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
279          assertEquals(1, p1.getCorePoolSize());
280          joinPool(p1);
281      }
# Line 301 | Line 285 | public class ScheduledExecutorTest exten
285       *   multiple threads active
286       */
287      public void testGetLargestPoolSize() {
288 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
288 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
289          assertEquals(0, p2.getLargestPoolSize());
290          p2.execute(new SmallRunnable());
291          p2.execute(new SmallRunnable());
# Line 319 | Line 303 | public class ScheduledExecutorTest exten
303       *   become active
304       */
305      public void testGetPoolSize() {
306 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
306 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
307          assertEquals(0, p1.getPoolSize());
308          p1.execute(new SmallRunnable());
309          assertEquals(1, p1.getPoolSize());
# Line 331 | Line 315 | public class ScheduledExecutorTest exten
315       *    submitted
316       */
317      public void testGetTaskCount() {
318 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
318 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
319          assertEquals(0, p1.getTaskCount());
320          for(int i = 0; i < 5; i++)
321              p1.execute(new SmallRunnable());
# Line 343 | Line 327 | public class ScheduledExecutorTest exten
327          assertEquals(5, p1.getTaskCount());
328          joinPool(p1);
329      }
330 +
331 +    /**
332 +     * getThreadFactory returns factory in constructor if not set
333 +     */
334 +    public void testGetThreadFactory() {
335 +        ThreadFactory tf = new SimpleThreadFactory();
336 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
337 +        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 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
347 +        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 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
357 +        try {
358 +            p.setThreadFactory(null);
359 +            shouldThrow();
360 +        } catch (NullPointerException success) {
361 +        } finally {
362 +            joinPool(p);
363 +        }
364 +    }
365      
366      /**
367       *   is isShutDown is false before shutdown, true after
368       */
369      public void testIsShutdown() {
370          
371 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
371 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
372          try {
373              assertFalse(p1.isShutdown());
374          }
375          finally {
376 <            p1.shutdown();
376 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
377          }
378          assertTrue(p1.isShutdown());
379      }
# Line 364 | Line 383 | public class ScheduledExecutorTest exten
383       *   isTerminated is false before termination, true after
384       */
385      public void testIsTerminated() {
386 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
386 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
387          try {
388              p1.execute(new SmallRunnable());
389          } finally {
390 <            p1.shutdown();
390 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
391          }
392          try {
393              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 382 | Line 401 | public class ScheduledExecutorTest exten
401       *  isTerminating is not true when running or when terminated
402       */
403      public void testIsTerminating() {
404 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
404 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
405          assertFalse(p1.isTerminating());
406          try {
407              p1.execute(new SmallRunnable());
408              assertFalse(p1.isTerminating());
409          } finally {
410 <            p1.shutdown();
410 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
411          }
412          try {
413              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 400 | Line 419 | public class ScheduledExecutorTest exten
419      }
420  
421      /**
422 <     *   purge removes cancelled tasks from the queue
422 >     * getQueue returns the work queue, which contains queued tasks
423 >     */
424 >    public void testGetQueue() {
425 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
426 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
427 >        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 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
447 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
448 >        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       */
473      public void testPurge() {
474 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
475 <        ScheduledCancellable[] tasks = new ScheduledCancellable[5];
474 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
475 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
476          for(int i = 0; i < 5; i++){
477 <            tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS);
477 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
478          }
479          int max = 5;
480          if (tasks[4].cancel(true)) --max;
# Line 421 | Line 489 | public class ScheduledExecutorTest exten
489       *  shutDownNow returns a list containing tasks that were not run
490       */
491      public void testShutDownNow() {
492 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
492 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
493          for(int i = 0; i < 5; i++)
494 <            p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
495 <        List l = p1.shutdownNow();
494 >            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
495 >        List l;
496 >        try {
497 >            l = p1.shutdownNow();
498 >        } catch (SecurityException ok) {
499 >            return;
500 >        }
501          assertTrue(p1.isShutdown());
502          assertTrue(l.size() > 0 && l.size() <= 5);
503          joinPool(p1);
# Line 436 | Line 509 | public class ScheduledExecutorTest exten
509       */
510      public void testShutDown1() {
511          try {
512 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
512 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
513              assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
514              assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
515  
516 <            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
516 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
517              for(int i = 0; i < 5; i++)
518                  tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
519 <            p1.shutdown();
519 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
520              BlockingQueue q = p1.getQueue();
521              for (Iterator it = q.iterator(); it.hasNext();) {
522 <                ScheduledCancellable t = (ScheduledCancellable)it.next();
522 >                ScheduledFuture t = (ScheduledFuture)it.next();
523                  assertFalse(t.isCancelled());
524              }
525              assertTrue(p1.isShutdown());
# Line 469 | Line 542 | public class ScheduledExecutorTest exten
542       */
543      public void testShutDown2() {
544          try {
545 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
545 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
546              p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
547 <            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
547 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
548              for(int i = 0; i < 5; i++)
549                  tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
550 <            p1.shutdown();
550 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
551              assertTrue(p1.isShutdown());
552              BlockingQueue q = p1.getQueue();
553              assertTrue(q.isEmpty());
# Line 493 | Line 566 | public class ScheduledExecutorTest exten
566       */
567      public void testShutDown3() {
568          try {
569 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
569 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
570              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
571 <            ScheduledCancellable task =
571 >            ScheduledFuture task =
572                  p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
573 <            p1.shutdown();
573 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
574              assertTrue(p1.isShutdown());
575              BlockingQueue q = p1.getQueue();
576              assertTrue(q.isEmpty());
# Line 514 | Line 587 | public class ScheduledExecutorTest exten
587       * periodic tasks are cancelled at shutdown
588       */
589      public void testShutDown4() {
590 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
590 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
591          try {
592              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
593 <            ScheduledCancellable task =
594 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
593 >            ScheduledFuture task =
594 >                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS);
595              assertFalse(task.isCancelled());
596 <            p1.shutdown();
596 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
597              assertFalse(task.isCancelled());
598              assertFalse(p1.isTerminated());
599              assertTrue(p1.isShutdown());
600              Thread.sleep(SHORT_DELAY_MS);
601              assertFalse(task.isCancelled());
602 <            task.cancel(true);
603 <            assertTrue(task.isCancelled());
602 >            assertTrue(task.cancel(true));
603 >            assertTrue(task.isDone());
604              Thread.sleep(SHORT_DELAY_MS);
605              assertTrue(p1.isTerminated());
606          }
607          catch(Exception ex) {
608              unexpectedException();
609          }
610 <        finally {
611 <            p1.shutdownNow();
610 >        finally {
611 >            joinPool(p1);
612 >        }
613 >    }
614 >
615 >    /**
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 +    /**
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 +            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1069 +            l.add(new StringTask());
1070 +            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1071 +            assertEquals(3, result.size());
1072 +            Iterator<Future<String>> it = result.iterator();
1073 +            Future<String> f1 = it.next();
1074 +            Future<String> f2 = it.next();
1075 +            Future<String> f3 = it.next();
1076 +            assertTrue(f1.isDone());
1077 +            assertTrue(f2.isDone());
1078 +            assertTrue(f3.isDone());
1079 +            assertFalse(f1.isCancelled());
1080 +            assertTrue(f2.isCancelled());
1081 +        } catch(Exception ex) {
1082 +            unexpectedException();
1083 +        } finally {
1084 +            joinPool(e);
1085 +        }
1086 +    }
1087 +
1088 +
1089   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines