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.13 by dl, Tue Dec 23 19:40:24 2003 UTC vs.
Revision 1.19 by dl, Sat Apr 10 14:27:17 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 java.util.concurrent.*;
# Line 35 | 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) throw new NullPointerException();
43 +            return new Thread(r);
44 +        }  
45 +    }
46 +    
47 +
48      /**
49       *  execute successfully executes a runnable
50       */
# Line 116 | 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 146 | 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());
149        p.shutdown();
159          joinPool(p);
160      }
161  
# Line 158 | Line 167 | public class ThreadPoolExecutorTest exte
167          ThreadFactory tf = new SimpleThreadFactory();
168          p.setThreadFactory(tf);
169          assertSame(tf, p.getThreadFactory());
161        p.shutdown();
170          joinPool(p);
171      }
172  
# Line 184 | 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());
187        p.shutdown();
195          joinPool(p);
196      }
197  
# Line 197 | Line 204 | public class ThreadPoolExecutorTest exte
204          RejectedExecutionHandler h = new NoOpREHandler();
205          p.setRejectedExecutionHandler(h);
206          assertSame(h, p.getRejectedExecutionHandler());
200        p.shutdown();
207          joinPool(p);
208      }
209  
# Line 280 | 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 295 | 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 315 | 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 343 | Line 349 | public class ThreadPoolExecutorTest exte
349              assertSame(q, wq);
350              assertFalse(wq.contains(tasks[0]));
351              assertTrue(wq.contains(tasks[4]));
346            p1.shutdownNow();
352          } catch(Exception e) {
353              unexpectedException();
354          } finally {
# Line 373 | Line 378 | public class ThreadPoolExecutorTest exte
378              assertTrue(q.contains(tasks[3]));
379              assertTrue(p1.remove(tasks[3]));
380              assertFalse(q.contains(tasks[3]));
376            p1.shutdownNow();
381          } catch(Exception e) {
382              unexpectedException();
383          } finally {
# Line 396 | Line 400 | public class ThreadPoolExecutorTest exte
400          p1.purge();
401          long count = p1.getTaskCount();
402          assertTrue(count >= 2 && count < 5);
399        p1.shutdownNow();
403          joinPool(p1);
404      }
405  
# Line 411 | 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 772 | 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 799 | 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 822 | 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 836 | 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 852 | 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 871 | 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 891 | 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 931 | 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 950 | 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 969 | 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 990 | 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 1000 | 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 1017 | 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 1481 | Line 1487 | public class ThreadPoolExecutorTest exte
1487          try {
1488              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1489              l.add(new StringTask());
1490 <            l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING));
1490 >            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1491 >            l.add(new StringTask());
1492              List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1493 <            assertEquals(2, result.size());
1493 >            assertEquals(3, result.size());
1494              Iterator<Future<String>> it = result.iterator();
1495              Future<String> f1 = it.next();
1496              Future<String> f2 = it.next();
1497 +            Future<String> f3 = it.next();
1498              assertTrue(f1.isDone());
1491            assertFalse(f1.isCancelled());
1499              assertTrue(f2.isDone());
1500 +            assertTrue(f3.isDone());
1501 +            assertFalse(f1.isCancelled());
1502              assertTrue(f2.isCancelled());
1503          } catch(Exception ex) {
1504              unexpectedException();
# Line 1498 | 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