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.15 by dl, Wed Jan 7 01:13:50 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);
# 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);
# Line 83 | Line 62 | public class ScheduledExecutorTest exten
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);
# 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);
# Line 130 | Line 110 | public class ScheduledExecutorTest exten
110       */
111      public void testSchedule5() {
112          try {
113 <            MyRunnable runnable = new MyRunnable();
114 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
115 <            ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
113 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
114 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
115 >            ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
116              assertFalse(runnable.done);
117              Thread.sleep(MEDIUM_DELAY_MS);
118              assertTrue(runnable.done);
# Line 148 | Line 128 | public class ScheduledExecutorTest exten
128       *  execute (null) throws NPE
129       */
130      public void testExecuteNull() {
131 <        ScheduledExecutor se = null;
131 >        ScheduledThreadPoolExecutor se = null;
132          try {
133 <            se = new ScheduledExecutor(1);
133 >            se = new ScheduledThreadPoolExecutor(1);
134              se.execute(null);
135              shouldThrow();
136          } catch(NullPointerException success){}
# Line 165 | Line 145 | public class ScheduledExecutorTest exten
145       * schedule (null) throws NPE
146       */
147      public void testScheduleNull() {
148 <        ScheduledExecutor se = new ScheduledExecutor(1);
148 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
149          try {
150 <            MyCallable callable = null;
150 >            TrackedCallable callable = null;
151              Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
152              shouldThrow();
153          } catch(NullPointerException success){}
# Line 181 | Line 161 | public class ScheduledExecutorTest exten
161       * execute throws RejectedExecutionException if shutdown
162       */
163      public void testSchedule1_RejectedExecutionException() {
164 <        ScheduledExecutor se = new ScheduledExecutor(1);
164 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
165          try {
166              se.shutdown();
167              se.schedule(new NoOpRunnable(),
# 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(),
# Line 212 | Line 192 | public class ScheduledExecutorTest exten
192       * schedule callable throws RejectedExecutionException if shutdown
193       */
194       public void testSchedule3_RejectedExecutionException() {
195 <         ScheduledExecutor se = new ScheduledExecutor(1);
195 >         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
196           try {
197              se.shutdown();
198              se.schedule(new NoOpCallable(),
# Line 227 | Line 207 | public class ScheduledExecutorTest exten
207       *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
208       */
209      public void testScheduleAtFixedRate1_RejectedExecutionException() {
210 <        ScheduledExecutor se = new ScheduledExecutor(1);
210 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
211          try {
212              se.shutdown();
213              se.scheduleAtFixedRate(new NoOpRunnable(),
# Line 242 | Line 222 | public class ScheduledExecutorTest exten
222       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
223       */
224      public void testScheduleWithFixedDelay1_RejectedExecutionException() {
225 <        ScheduledExecutor se = new ScheduledExecutor(1);
225 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
226          try {
227              se.shutdown();
228              se.scheduleWithFixedDelay(new NoOpRunnable(),
# Line 258 | Line 238 | public class ScheduledExecutorTest exten
238       *  thread becomes active
239       */
240      public void testGetActiveCount() {
241 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
241 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
242          assertEquals(0, p2.getActiveCount());
243          p2.execute(new SmallRunnable());
244          try {
# Line 275 | Line 255 | public class ScheduledExecutorTest exten
255       *   when tasks complete
256       */
257      public void testGetCompletedTaskCount() {
258 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
258 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
259          assertEquals(0, p2.getCompletedTaskCount());
260          p2.execute(new SmallRunnable());
261          try {
# Line 291 | Line 271 | public class ScheduledExecutorTest exten
271       *  getCorePoolSize returns size given in constructor if not otherwise set
272       */
273      public void testGetCorePoolSize() {
274 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
274 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
275          assertEquals(1, p1.getCorePoolSize());
276          joinPool(p1);
277      }
# Line 301 | Line 281 | public class ScheduledExecutorTest exten
281       *   multiple threads active
282       */
283      public void testGetLargestPoolSize() {
284 <        ScheduledExecutor p2 = new ScheduledExecutor(2);
284 >        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
285          assertEquals(0, p2.getLargestPoolSize());
286          p2.execute(new SmallRunnable());
287          p2.execute(new SmallRunnable());
# Line 319 | Line 299 | public class ScheduledExecutorTest exten
299       *   become active
300       */
301      public void testGetPoolSize() {
302 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
302 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
303          assertEquals(0, p1.getPoolSize());
304          p1.execute(new SmallRunnable());
305          assertEquals(1, p1.getPoolSize());
# Line 331 | Line 311 | public class ScheduledExecutorTest exten
311       *    submitted
312       */
313      public void testGetTaskCount() {
314 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
314 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
315          assertEquals(0, p1.getTaskCount());
316          for(int i = 0; i < 5; i++)
317              p1.execute(new SmallRunnable());
# Line 343 | Line 323 | public class ScheduledExecutorTest exten
323          assertEquals(5, p1.getTaskCount());
324          joinPool(p1);
325      }
326 +
327 +    /**
328 +     * getThreadFactory returns factory in constructor if not set
329 +     */
330 +    public void testGetThreadFactory() {
331 +        ThreadFactory tf = new SimpleThreadFactory();
332 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
333 +        assertSame(tf, p.getThreadFactory());
334 +        p.shutdown();
335 +        joinPool(p);
336 +    }
337 +
338 +    /**
339 +     * setThreadFactory sets the thread factory returned by getThreadFactory
340 +     */
341 +    public void testSetThreadFactory() {
342 +        ThreadFactory tf = new SimpleThreadFactory();
343 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
344 +        p.setThreadFactory(tf);
345 +        assertSame(tf, p.getThreadFactory());
346 +        p.shutdown();
347 +        joinPool(p);
348 +    }
349 +
350 +    /**
351 +     * setThreadFactory(null) throws NPE
352 +     */
353 +    public void testSetThreadFactoryNull() {
354 +        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
355 +        try {
356 +            p.setThreadFactory(null);
357 +            shouldThrow();
358 +        } catch (NullPointerException success) {
359 +        } finally {
360 +            joinPool(p);
361 +        }
362 +    }
363      
364      /**
365       *   is isShutDown is false before shutdown, true after
366       */
367      public void testIsShutdown() {
368          
369 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
369 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
370          try {
371              assertFalse(p1.isShutdown());
372          }
# Line 364 | Line 381 | public class ScheduledExecutorTest exten
381       *   isTerminated is false before termination, true after
382       */
383      public void testIsTerminated() {
384 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
384 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
385          try {
386              p1.execute(new SmallRunnable());
387          } finally {
# Line 382 | Line 399 | public class ScheduledExecutorTest exten
399       *  isTerminating is not true when running or when terminated
400       */
401      public void testIsTerminating() {
402 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
402 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
403          assertFalse(p1.isTerminating());
404          try {
405              p1.execute(new SmallRunnable());
# Line 400 | Line 417 | public class ScheduledExecutorTest exten
417      }
418  
419      /**
420 <     *   purge removes cancelled tasks from the queue
420 >     * getQueue returns the work queue, which contains queued tasks
421 >     */
422 >    public void testGetQueue() {
423 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
424 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
425 >        for(int i = 0; i < 5; i++){
426 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
427 >        }
428 >        try {
429 >            Thread.sleep(SHORT_DELAY_MS);
430 >            BlockingQueue<Runnable> q = p1.getQueue();
431 >            assertTrue(q.contains(tasks[4]));
432 >            assertFalse(q.contains(tasks[0]));
433 >            p1.shutdownNow();
434 >        } catch(Exception e) {
435 >            unexpectedException();
436 >        } finally {
437 >            joinPool(p1);
438 >        }
439 >    }
440 >
441 >    /**
442 >     * remove(task) removes queued task, and fails to remove active task
443 >     */
444 >    public void testRemove() {
445 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
446 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
447 >        for(int i = 0; i < 5; i++){
448 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
449 >        }
450 >        try {
451 >            Thread.sleep(SHORT_DELAY_MS);
452 >            BlockingQueue<Runnable> q = p1.getQueue();
453 >            assertFalse(p1.remove((Runnable)tasks[0]));
454 >            assertTrue(q.contains((Runnable)tasks[4]));
455 >            assertTrue(q.contains((Runnable)tasks[3]));
456 >            assertTrue(p1.remove((Runnable)tasks[4]));
457 >            assertFalse(p1.remove((Runnable)tasks[4]));
458 >            assertFalse(q.contains((Runnable)tasks[4]));
459 >            assertTrue(q.contains((Runnable)tasks[3]));
460 >            assertTrue(p1.remove((Runnable)tasks[3]));
461 >            assertFalse(q.contains((Runnable)tasks[3]));
462 >            p1.shutdownNow();
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);
494 >            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
495          List l = p1.shutdownNow();
496          assertTrue(p1.isShutdown());
497          assertTrue(l.size() > 0 && l.size() <= 5);
# Line 436 | Line 504 | public class ScheduledExecutorTest exten
504       */
505      public void testShutDown1() {
506          try {
507 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
507 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
508              assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
509              assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
510  
511 <            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
511 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
512              for(int i = 0; i < 5; i++)
513                  tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
514              p1.shutdown();
515              BlockingQueue q = p1.getQueue();
516              for (Iterator it = q.iterator(); it.hasNext();) {
517 <                ScheduledCancellable t = (ScheduledCancellable)it.next();
517 >                ScheduledFuture t = (ScheduledFuture)it.next();
518                  assertFalse(t.isCancelled());
519              }
520              assertTrue(p1.isShutdown());
# Line 469 | Line 537 | public class ScheduledExecutorTest exten
537       */
538      public void testShutDown2() {
539          try {
540 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
540 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
541              p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
542 <            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
542 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
543              for(int i = 0; i < 5; i++)
544                  tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
545              p1.shutdown();
# Line 493 | Line 561 | public class ScheduledExecutorTest exten
561       */
562      public void testShutDown3() {
563          try {
564 <            ScheduledExecutor p1 = new ScheduledExecutor(1);
564 >            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
565              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
566 <            ScheduledCancellable task =
566 >            ScheduledFuture task =
567                  p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
568              p1.shutdown();
569              assertTrue(p1.isShutdown());
# Line 514 | Line 582 | public class ScheduledExecutorTest exten
582       * periodic tasks are cancelled at shutdown
583       */
584      public void testShutDown4() {
585 <        ScheduledExecutor p1 = new ScheduledExecutor(1);
585 >        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
586          try {
587              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
588 <            ScheduledCancellable task =
588 >            ScheduledFuture task =
589                  p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
590              assertFalse(task.isCancelled());
591              p1.shutdown();
# Line 527 | Line 595 | public class ScheduledExecutorTest exten
595              Thread.sleep(SHORT_DELAY_MS);
596              assertFalse(task.isCancelled());
597              task.cancel(true);
598 <            assertTrue(task.isCancelled());
598 >            assertTrue(task.isDone());
599              Thread.sleep(SHORT_DELAY_MS);
600              assertTrue(p1.isTerminated());
601          }
# Line 539 | Line 607 | public class ScheduledExecutorTest exten
607          }
608      }
609  
610 +    /**
611 +     * completed submit of callable returns result
612 +     */
613 +    public void testSubmitCallable() {
614 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
615 +        try {
616 +            Future<String> future = e.submit(new StringTask());
617 +            String result = future.get();
618 +            assertSame(TEST_STRING, result);
619 +        }
620 +        catch (ExecutionException ex) {
621 +            unexpectedException();
622 +        }
623 +        catch (InterruptedException ex) {
624 +            unexpectedException();
625 +        } finally {
626 +            joinPool(e);
627 +        }
628 +    }
629 +
630 +    /**
631 +     * completed submit of runnable returns successfully
632 +     */
633 +    public void testSubmitRunnable() {
634 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
635 +        try {
636 +            Future<?> future = e.submit(new NoOpRunnable());
637 +            future.get();
638 +            assertTrue(future.isDone());
639 +        }
640 +        catch (ExecutionException ex) {
641 +            unexpectedException();
642 +        }
643 +        catch (InterruptedException ex) {
644 +            unexpectedException();
645 +        } finally {
646 +            joinPool(e);
647 +        }
648 +    }
649 +
650 +    /**
651 +     * completed submit of (runnable, result) returns result
652 +     */
653 +    public void testSubmitRunnable2() {
654 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
655 +        try {
656 +            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
657 +            String result = future.get();
658 +            assertSame(TEST_STRING, result);
659 +        }
660 +        catch (ExecutionException ex) {
661 +            unexpectedException();
662 +        }
663 +        catch (InterruptedException ex) {
664 +            unexpectedException();
665 +        } finally {
666 +            joinPool(e);
667 +        }
668 +    }
669 +
670 +
671 +
672 +
673 +
674 +    /**
675 +     * invokeAny(null) throws NPE
676 +     */
677 +    public void testInvokeAny1() {
678 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
679 +        try {
680 +            e.invokeAny(null);
681 +        } catch (NullPointerException success) {
682 +        } catch(Exception ex) {
683 +            unexpectedException();
684 +        } finally {
685 +            joinPool(e);
686 +        }
687 +    }
688 +
689 +    /**
690 +     * invokeAny(empty collection) throws IAE
691 +     */
692 +    public void testInvokeAny2() {
693 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
694 +        try {
695 +            e.invokeAny(new ArrayList<Callable<String>>());
696 +        } catch (IllegalArgumentException success) {
697 +        } catch(Exception ex) {
698 +            unexpectedException();
699 +        } finally {
700 +            joinPool(e);
701 +        }
702 +    }
703 +
704 +    /**
705 +     * invokeAny(c) throws NPE if c has null elements
706 +     */
707 +    public void testInvokeAny3() {
708 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
709 +        try {
710 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
711 +            l.add(new StringTask());
712 +            l.add(null);
713 +            e.invokeAny(l);
714 +        } catch (NullPointerException success) {
715 +        } catch(Exception ex) {
716 +            unexpectedException();
717 +        } finally {
718 +            joinPool(e);
719 +        }
720 +    }
721 +
722 +    /**
723 +     * invokeAny(c) throws ExecutionException if no task completes
724 +     */
725 +    public void testInvokeAny4() {
726 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
727 +        try {
728 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
729 +            l.add(new NPETask());
730 +            e.invokeAny(l);
731 +        } catch (ExecutionException success) {
732 +        } catch(Exception ex) {
733 +            unexpectedException();
734 +        } finally {
735 +            joinPool(e);
736 +        }
737 +    }
738 +
739 +    /**
740 +     * invokeAny(c) returns result of some task
741 +     */
742 +    public void testInvokeAny5() {
743 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
744 +        try {
745 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
746 +            l.add(new StringTask());
747 +            l.add(new StringTask());
748 +            String result = e.invokeAny(l);
749 +            assertSame(TEST_STRING, result);
750 +        } catch (ExecutionException success) {
751 +        } catch(Exception ex) {
752 +            unexpectedException();
753 +        } finally {
754 +            joinPool(e);
755 +        }
756 +    }
757 +
758 +    /**
759 +     * invokeAll(null) throws NPE
760 +     */
761 +    public void testInvokeAll1() {
762 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
763 +        try {
764 +            e.invokeAll(null);
765 +        } catch (NullPointerException success) {
766 +        } catch(Exception ex) {
767 +            unexpectedException();
768 +        } finally {
769 +            joinPool(e);
770 +        }
771 +    }
772 +
773 +    /**
774 +     * invokeAll(empty collection) returns empty collection
775 +     */
776 +    public void testInvokeAll2() {
777 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
778 +        try {
779 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
780 +            assertTrue(r.isEmpty());
781 +        } catch(Exception ex) {
782 +            unexpectedException();
783 +        } finally {
784 +            joinPool(e);
785 +        }
786 +    }
787 +
788 +    /**
789 +     * invokeAll(c) throws NPE if c has null elements
790 +     */
791 +    public void testInvokeAll3() {
792 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
793 +        try {
794 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
795 +            l.add(new StringTask());
796 +            l.add(null);
797 +            e.invokeAll(l);
798 +        } catch (NullPointerException success) {
799 +        } catch(Exception ex) {
800 +            unexpectedException();
801 +        } finally {
802 +            joinPool(e);
803 +        }
804 +    }
805 +
806 +    /**
807 +     * get of invokeAll(c) throws exception on failed task
808 +     */
809 +    public void testInvokeAll4() {
810 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
811 +        try {
812 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
813 +            l.add(new NPETask());
814 +            List<Future<String>> result = e.invokeAll(l);
815 +            assertEquals(1, result.size());
816 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
817 +                it.next().get();
818 +        } catch(ExecutionException success) {
819 +        } catch(Exception ex) {
820 +            unexpectedException();
821 +        } finally {
822 +            joinPool(e);
823 +        }
824 +    }
825 +
826 +    /**
827 +     * invokeAll(c) returns results of all completed tasks
828 +     */
829 +    public void testInvokeAll5() {
830 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
831 +        try {
832 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
833 +            l.add(new StringTask());
834 +            l.add(new StringTask());
835 +            List<Future<String>> result = e.invokeAll(l);
836 +            assertEquals(2, result.size());
837 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
838 +                assertSame(TEST_STRING, it.next().get());
839 +        } catch (ExecutionException success) {
840 +        } catch(Exception ex) {
841 +            unexpectedException();
842 +        } finally {
843 +            joinPool(e);
844 +        }
845 +    }
846 +
847 +    /**
848 +     * timed invokeAny(null) throws NPE
849 +     */
850 +    public void testTimedInvokeAny1() {
851 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
852 +        try {
853 +            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
854 +        } catch (NullPointerException success) {
855 +        } catch(Exception ex) {
856 +            unexpectedException();
857 +        } finally {
858 +            joinPool(e);
859 +        }
860 +    }
861 +
862 +    /**
863 +     * timed invokeAny(,,null) throws NPE
864 +     */
865 +    public void testTimedInvokeAnyNullTimeUnit() {
866 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
867 +        try {
868 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
869 +            l.add(new StringTask());
870 +            e.invokeAny(l, MEDIUM_DELAY_MS, null);
871 +        } catch (NullPointerException success) {
872 +        } catch(Exception ex) {
873 +            unexpectedException();
874 +        } finally {
875 +            joinPool(e);
876 +        }
877 +    }
878 +
879 +    /**
880 +     * timed invokeAny(empty collection) throws IAE
881 +     */
882 +    public void testTimedInvokeAny2() {
883 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
884 +        try {
885 +            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
886 +        } catch (IllegalArgumentException success) {
887 +        } catch(Exception ex) {
888 +            unexpectedException();
889 +        } finally {
890 +            joinPool(e);
891 +        }
892 +    }
893 +
894 +    /**
895 +     * timed invokeAny(c) throws NPE if c has null elements
896 +     */
897 +    public void testTimedInvokeAny3() {
898 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
899 +        try {
900 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
901 +            l.add(new StringTask());
902 +            l.add(null);
903 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
904 +        } catch (NullPointerException success) {
905 +        } catch(Exception ex) {
906 +            ex.printStackTrace();
907 +            unexpectedException();
908 +        } finally {
909 +            joinPool(e);
910 +        }
911 +    }
912 +
913 +    /**
914 +     * timed invokeAny(c) throws ExecutionException if no task completes
915 +     */
916 +    public void testTimedInvokeAny4() {
917 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
918 +        try {
919 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
920 +            l.add(new NPETask());
921 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
922 +        } catch(ExecutionException success) {
923 +        } catch(Exception ex) {
924 +            unexpectedException();
925 +        } finally {
926 +            joinPool(e);
927 +        }
928 +    }
929 +
930 +    /**
931 +     * timed invokeAny(c) returns result of some task
932 +     */
933 +    public void testTimedInvokeAny5() {
934 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
935 +        try {
936 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
937 +            l.add(new StringTask());
938 +            l.add(new StringTask());
939 +            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
940 +            assertSame(TEST_STRING, result);
941 +        } catch (ExecutionException success) {
942 +        } catch(Exception ex) {
943 +            unexpectedException();
944 +        } finally {
945 +            joinPool(e);
946 +        }
947 +    }
948 +
949 +    /**
950 +     * timed invokeAll(null) throws NPE
951 +     */
952 +    public void testTimedInvokeAll1() {
953 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
954 +        try {
955 +            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
956 +        } catch (NullPointerException success) {
957 +        } catch(Exception ex) {
958 +            unexpectedException();
959 +        } finally {
960 +            joinPool(e);
961 +        }
962 +    }
963 +
964 +    /**
965 +     * timed invokeAll(,,null) throws NPE
966 +     */
967 +    public void testTimedInvokeAllNullTimeUnit() {
968 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
969 +        try {
970 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
971 +            l.add(new StringTask());
972 +            e.invokeAll(l, MEDIUM_DELAY_MS, null);
973 +        } catch (NullPointerException success) {
974 +        } catch(Exception ex) {
975 +            unexpectedException();
976 +        } finally {
977 +            joinPool(e);
978 +        }
979 +    }
980 +
981 +    /**
982 +     * timed invokeAll(empty collection) returns empty collection
983 +     */
984 +    public void testTimedInvokeAll2() {
985 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
986 +        try {
987 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
988 +            assertTrue(r.isEmpty());
989 +        } catch(Exception ex) {
990 +            unexpectedException();
991 +        } finally {
992 +            joinPool(e);
993 +        }
994 +    }
995 +
996 +    /**
997 +     * timed invokeAll(c) throws NPE if c has null elements
998 +     */
999 +    public void testTimedInvokeAll3() {
1000 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1001 +        try {
1002 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1003 +            l.add(new StringTask());
1004 +            l.add(null);
1005 +            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1006 +        } catch (NullPointerException success) {
1007 +        } catch(Exception ex) {
1008 +            unexpectedException();
1009 +        } finally {
1010 +            joinPool(e);
1011 +        }
1012 +    }
1013 +
1014 +    /**
1015 +     * get of element of invokeAll(c) throws exception on failed task
1016 +     */
1017 +    public void testTimedInvokeAll4() {
1018 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1019 +        try {
1020 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1021 +            l.add(new NPETask());
1022 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1023 +            assertEquals(1, result.size());
1024 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1025 +                it.next().get();
1026 +        } catch(ExecutionException success) {
1027 +        } catch(Exception ex) {
1028 +            unexpectedException();
1029 +        } finally {
1030 +            joinPool(e);
1031 +        }
1032 +    }
1033 +
1034 +    /**
1035 +     * timed invokeAll(c) returns results of all completed tasks
1036 +     */
1037 +    public void testTimedInvokeAll5() {
1038 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1039 +        try {
1040 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1041 +            l.add(new StringTask());
1042 +            l.add(new StringTask());
1043 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1044 +            assertEquals(2, result.size());
1045 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1046 +                assertSame(TEST_STRING, it.next().get());
1047 +        } catch (ExecutionException success) {
1048 +        } catch(Exception ex) {
1049 +            unexpectedException();
1050 +        } finally {
1051 +            joinPool(e);
1052 +        }
1053 +    }
1054 +
1055 +    /**
1056 +     * timed invokeAll(c) cancels tasks not completed by timeout
1057 +     */
1058 +    public void testTimedInvokeAll6() {
1059 +        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1060 +        try {
1061 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1062 +            l.add(new StringTask());
1063 +            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1064 +            l.add(new StringTask());
1065 +            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1066 +            assertEquals(3, result.size());
1067 +            Iterator<Future<String>> it = result.iterator();
1068 +            Future<String> f1 = it.next();
1069 +            Future<String> f2 = it.next();
1070 +            Future<String> f3 = it.next();
1071 +            assertTrue(f1.isDone());
1072 +            assertTrue(f2.isDone());
1073 +            assertTrue(f3.isDone());
1074 +            assertFalse(f1.isCancelled());
1075 +            assertTrue(f2.isCancelled());
1076 +        } catch(Exception ex) {
1077 +            unexpectedException();
1078 +        } finally {
1079 +            joinPool(e);
1080 +        }
1081 +    }
1082 +
1083 +
1084   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines