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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.16 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.20 by dl, Mon Apr 12 12:03:10 2004 UTC

# Line 36 | Line 36 | public class ThreadPoolExecutorTest exte
36          }
37      }
38  
39 +    static class FailingThreadFactory implements ThreadFactory{
40 +        int calls = 0;
41 +        public Thread newThread(Runnable r){
42 +            if (++calls > 1) return null;
43 +            return new Thread(r);
44 +        }  
45 +    }
46 +    
47 +
48      /**
49       *  execute successfully executes a runnable
50       */
# Line 117 | Line 126 | public class ThreadPoolExecutorTest exte
126              unexpectedException();
127          }
128          assertEquals(1, p2.getCompletedTaskCount());
129 <        p2.shutdown();
129 >        try { p2.shutdown(); } catch(SecurityException ok) { return; }
130          joinPool(p2);
131      }
132      
# Line 147 | Line 156 | public class ThreadPoolExecutorTest exte
156          ThreadFactory tf = new SimpleThreadFactory();
157          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
158          assertSame(tf, p.getThreadFactory());
150        p.shutdown();
159          joinPool(p);
160      }
161  
# Line 159 | Line 167 | public class ThreadPoolExecutorTest exte
167          ThreadFactory tf = new SimpleThreadFactory();
168          p.setThreadFactory(tf);
169          assertSame(tf, p.getThreadFactory());
162        p.shutdown();
170          joinPool(p);
171      }
172  
# Line 185 | Line 192 | public class ThreadPoolExecutorTest exte
192          RejectedExecutionHandler h = new NoOpREHandler();
193          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
194          assertSame(h, p.getRejectedExecutionHandler());
188        p.shutdown();
195          joinPool(p);
196      }
197  
# Line 198 | Line 204 | public class ThreadPoolExecutorTest exte
204          RejectedExecutionHandler h = new NoOpREHandler();
205          p.setRejectedExecutionHandler(h);
206          assertSame(h, p.getRejectedExecutionHandler());
201        p.shutdown();
207          joinPool(p);
208      }
209  
# Line 281 | Line 286 | public class ThreadPoolExecutorTest exte
286          
287          ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
288          assertFalse(p1.isShutdown());
289 <        p1.shutdown();
289 >        try { p1.shutdown(); } catch(SecurityException ok) { return; }
290          assertTrue(p1.isShutdown());
291          joinPool(p1);
292      }
# Line 296 | Line 301 | public class ThreadPoolExecutorTest exte
301          try {
302              p1.execute(new MediumRunnable());
303          } finally {
304 <            p1.shutdown();
304 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
305          }
306          try {
307              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 316 | Line 321 | public class ThreadPoolExecutorTest exte
321              p1.execute(new SmallRunnable());
322              assertFalse(p1.isTerminating());
323          } finally {
324 <            p1.shutdown();
324 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
325          }
326          try {
327              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 344 | Line 349 | public class ThreadPoolExecutorTest exte
349              assertSame(q, wq);
350              assertFalse(wq.contains(tasks[0]));
351              assertTrue(wq.contains(tasks[4]));
347            p1.shutdownNow();
352          } catch(Exception e) {
353              unexpectedException();
354          } finally {
# Line 374 | Line 378 | public class ThreadPoolExecutorTest exte
378              assertTrue(q.contains(tasks[3]));
379              assertTrue(p1.remove(tasks[3]));
380              assertFalse(q.contains(tasks[3]));
377            p1.shutdownNow();
381          } catch(Exception e) {
382              unexpectedException();
383          } finally {
# Line 397 | Line 400 | public class ThreadPoolExecutorTest exte
400          p1.purge();
401          long count = p1.getTaskCount();
402          assertTrue(count >= 2 && count < 5);
400        p1.shutdownNow();
403          joinPool(p1);
404      }
405  
# Line 412 | Line 414 | public class ThreadPoolExecutorTest exte
414                  p1.execute(new MediumPossiblyInterruptedRunnable());
415          }
416          finally {
417 <            l = p1.shutdownNow();
417 >            try {
418 >                l = p1.shutdownNow();
419 >            } catch (SecurityException ok) { return; }
420 >            
421          }
422          assertTrue(p1.isShutdown());
423          assertTrue(l.size() <= 4);
# Line 773 | Line 778 | public class ThreadPoolExecutorTest exte
778              for(int i = 1; i < 5; ++i) {
779                  assertTrue(tasks[i].done);
780              }
781 <            p.shutdownNow();
781 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
782          } catch(RejectedExecutionException ex){
783              unexpectedException();
784          } finally {
# Line 800 | Line 805 | public class ThreadPoolExecutorTest exte
805              for(int i = 0; i < 5; ++i){
806                  assertFalse(tasks[i].done);
807              }
808 <            p.shutdownNow();
808 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
809          } catch(RejectedExecutionException ex){
810              unexpectedException();
811          } finally {
# Line 823 | Line 828 | public class ThreadPoolExecutorTest exte
828              p.execute(r3);
829              assertFalse(p.getQueue().contains(r2));
830              assertTrue(p.getQueue().contains(r3));
831 <            p.shutdownNow();
831 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
832          } catch(RejectedExecutionException ex){
833              unexpectedException();
834          } finally {
# Line 837 | Line 842 | public class ThreadPoolExecutorTest exte
842      public void testRejectedExecutionExceptionOnShutdown() {
843          ThreadPoolExecutor tpe =
844              new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
845 <        tpe.shutdown();
845 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
846          try {
847              tpe.execute(new NoOpRunnable());
848              shouldThrow();
# Line 853 | Line 858 | public class ThreadPoolExecutorTest exte
858          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
859          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
860  
861 <        p.shutdown();
861 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
862          try {
863              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
864              p.execute(r);
# Line 872 | Line 877 | public class ThreadPoolExecutorTest exte
877          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
878          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
879  
880 <        p.shutdown();
880 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
881          try {
882              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
883              p.execute(r);
# Line 892 | Line 897 | public class ThreadPoolExecutorTest exte
897          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
898          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
899  
900 <        p.shutdown();
900 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
901          try {
902              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
903              p.execute(r);
# Line 932 | Line 937 | public class ThreadPoolExecutorTest exte
937              shouldThrow();
938          } catch(IllegalArgumentException success){
939          } finally {
940 <            tpe.shutdown();
940 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
941          }
942          joinPool(tpe);
943      }  
# Line 951 | Line 956 | public class ThreadPoolExecutorTest exte
956              shouldThrow();
957          } catch(IllegalArgumentException success){
958          } finally {
959 <            tpe.shutdown();
959 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
960          }
961          joinPool(tpe);
962      }
# Line 970 | Line 975 | public class ThreadPoolExecutorTest exte
975              shouldThrow();
976          } catch(IllegalArgumentException success){
977          } finally {
978 <            tpe.shutdown();
978 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
979          }
980          joinPool(tpe);
981      }
# Line 991 | Line 996 | public class ThreadPoolExecutorTest exte
996              shouldThrow();
997          } catch(IllegalArgumentException success){
998          } finally {
999 <            tpe.shutdown();
999 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1000          }
1001          joinPool(tpe);
1002      }
# Line 1001 | Line 1006 | public class ThreadPoolExecutorTest exte
1006       */
1007      public void testTerminated() {
1008          ExtendedTPE tpe = new ExtendedTPE();
1009 <        tpe.shutdown();
1009 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1010          assertTrue(tpe.terminatedCalled);
1011          joinPool(tpe);
1012      }
# Line 1018 | Line 1023 | public class ThreadPoolExecutorTest exte
1023              assertTrue(r.done);
1024              assertTrue(tpe.beforeCalled);
1025              assertTrue(tpe.afterCalled);
1026 <            tpe.shutdown();
1026 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1027          }
1028          catch(Exception ex) {
1029              unexpectedException();
# Line 1502 | Line 1507 | public class ThreadPoolExecutorTest exte
1507          }
1508      }
1509  
1510 <
1510 >    /**
1511 >     * Execution continues if there is at least one thread even if
1512 >     * thread factory fails to create more
1513 >     */
1514 >    public void testFailingThreadFactory() {
1515 >        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1516 >        try {
1517 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1518 >            for (int k = 0; k < 100; ++k) {
1519 >                e.execute(new NoOpRunnable());
1520 >            }
1521 >            Thread.sleep(LONG_DELAY_MS);
1522 >        } catch(Exception ex) {
1523 >            unexpectedException();
1524 >        } finally {
1525 >            joinPool(e);
1526 >        }
1527 >    }
1528   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines