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.10 by dl, Mon Dec 22 00:48:56 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 28 | Line 29 | public class ScheduledExecutorTest exten
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 57 | Line 58 | public class ScheduledExecutorTest exten
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){
# Line 78 | Line 79 | public class ScheduledExecutorTest exten
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 97 | Line 98 | public class ScheduledExecutorTest exten
98              Thread.sleep(MEDIUM_DELAY_MS);
99              assertTrue(runnable.done);
100              h.cancel(true);
100            p1.shutdown();
101              joinPool(p1);
102          } catch(Exception e){
103              unexpectedException();
# Line 116 | Line 116 | public class ScheduledExecutorTest exten
116              Thread.sleep(MEDIUM_DELAY_MS);
117              assertTrue(runnable.done);
118              h.cancel(true);
119            p1.shutdown();
119              joinPool(p1);
120          } catch(Exception e){
121              unexpectedException();
# Line 167 | Line 166 | public class ScheduledExecutorTest exten
166                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
167              shouldThrow();
168          } catch(RejectedExecutionException success){
169 +        } catch (SecurityException ok) {
170          }
171 +        
172          joinPool(se);
173  
174      }
# Line 183 | Line 184 | public class ScheduledExecutorTest exten
184                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
185              shouldThrow();
186          } catch(RejectedExecutionException success){
187 +        } catch (SecurityException ok) {
188          }
189          joinPool(se);
190      }
# Line 198 | Line 200 | public class ScheduledExecutorTest exten
200                          MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
201              shouldThrow();
202          } catch(RejectedExecutionException success){
203 <        }
203 >        } catch (SecurityException ok) {
204 >        }
205           joinPool(se);
206      }
207  
# Line 213 | Line 216 | public class ScheduledExecutorTest exten
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 228 | Line 232 | public class ScheduledExecutorTest exten
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 330 | Line 335 | public class ScheduledExecutorTest exten
335          ThreadFactory tf = new SimpleThreadFactory();
336          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
337          assertSame(tf, p.getThreadFactory());
333        p.shutdown();
338          joinPool(p);
339      }
340  
# Line 342 | Line 346 | public class ScheduledExecutorTest exten
346          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
347          p.setThreadFactory(tf);
348          assertSame(tf, p.getThreadFactory());
345        p.shutdown();
349          joinPool(p);
350      }
351  
# Line 370 | Line 373 | public class ScheduledExecutorTest exten
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 384 | Line 387 | public class ScheduledExecutorTest exten
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 404 | Line 407 | public class ScheduledExecutorTest exten
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 429 | Line 432 | public class ScheduledExecutorTest exten
432              BlockingQueue<Runnable> q = p1.getQueue();
433              assertTrue(q.contains(tasks[4]));
434              assertFalse(q.contains(tasks[0]));
432            p1.shutdownNow();
435          } catch(Exception e) {
436              unexpectedException();
437          } finally {
# Line 458 | Line 460 | public class ScheduledExecutorTest exten
460              assertTrue(q.contains((Runnable)tasks[3]));
461              assertTrue(p1.remove((Runnable)tasks[3]));
462              assertFalse(q.contains((Runnable)tasks[3]));
461            p1.shutdownNow();
463          } catch(Exception e) {
464              unexpectedException();
465          } finally {
# Line 491 | Line 492 | public class ScheduledExecutorTest exten
492          ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
493          for(int i = 0; i < 5; i++)
494              p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
495 <        List l = p1.shutdownNow();
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 510 | Line 516 | public class ScheduledExecutorTest exten
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                  ScheduledFuture t = (ScheduledFuture)it.next();
# Line 541 | Line 547 | public class ScheduledExecutorTest exten
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 564 | Line 570 | public class ScheduledExecutorTest exten
570              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
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 585 | Line 591 | public class ScheduledExecutorTest exten
591          try {
592              p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
593              ScheduledFuture task =
594 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
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  
# Line 839 | Line 845 | public class ScheduledExecutorTest exten
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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines