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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.29 by jsr166, Fri May 27 19:35:24 2011 UTC vs.
Revision 1.36 by jsr166, Fri May 15 17:07:27 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.ExecutionException;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.Future;
22 > import java.util.concurrent.FutureTask;
23 > import java.util.concurrent.LinkedBlockingQueue;
24 > import java.util.concurrent.RejectedExecutionException;
25 > import java.util.concurrent.RejectedExecutionHandler;
26 > import java.util.concurrent.RunnableFuture;
27 > import java.util.concurrent.SynchronousQueue;
28 > import java.util.concurrent.ThreadFactory;
29 > import java.util.concurrent.ThreadPoolExecutor;
30 > import java.util.concurrent.TimeoutException;
31 > import java.util.concurrent.TimeUnit;
32 > import java.util.concurrent.locks.Condition;
33 > import java.util.concurrent.locks.ReentrantLock;
34 >
35 > import junit.framework.Test;
36 > import junit.framework.TestSuite;
37  
38   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42      public static Test suite() {
43          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 59 | public class ThreadPoolExecutorSubclassT
59          CustomTask(final Runnable r, final V res) {
60              if (r == null) throw new NullPointerException();
61              callable = new Callable<V>() {
62 <            public V call() throws Exception { r.run(); return res; }};
62 >                public V call() throws Exception { r.run(); return res; }};
63          }
64          public boolean isDone() {
65              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 161 | Line 183 | public class ThreadPoolExecutorSubclassT
183                workQueue, threadFactory, handler);
184          }
185  
186 <        volatile boolean beforeCalled = false;
187 <        volatile boolean afterCalled = false;
188 <        volatile boolean terminatedCalled = false;
186 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
187 >        final CountDownLatch afterCalled = new CountDownLatch(1);
188 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
189 >
190          public CustomTPE() {
191              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
192          }
193          protected void beforeExecute(Thread t, Runnable r) {
194 <            beforeCalled = true;
194 >            beforeCalled.countDown();
195          }
196          protected void afterExecute(Runnable r, Throwable t) {
197 <            afterCalled = true;
197 >            afterCalled.countDown();
198          }
199          protected void terminated() {
200 <            terminatedCalled = true;
200 >            terminatedCalled.countDown();
201          }
202  
203 +        public boolean beforeCalled() {
204 +            return beforeCalled.getCount() == 0;
205 +        }
206 +        public boolean afterCalled() {
207 +            return afterCalled.getCount() == 0;
208 +        }
209 +        public boolean terminatedCalled() {
210 +            return terminatedCalled.getCount() == 0;
211 +        }
212      }
213  
214      static class FailingThreadFactory implements ThreadFactory {
# Line 284 | Line 316 | public class ThreadPoolExecutorSubclassT
316                      threadProceed.await();
317                      threadDone.countDown();
318                  }});
319 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
319 >            await(threadStarted);
320              assertEquals(0, p.getCompletedTaskCount());
321              threadProceed.countDown();
322              threadDone.await();
323 <            delay(SHORT_DELAY_MS);
324 <            assertEquals(1, p.getCompletedTaskCount());
323 >            long startTime = System.nanoTime();
324 >            while (p.getCompletedTaskCount() != 1) {
325 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
326 >                    fail("timed out");
327 >                Thread.yield();
328 >            }
329          } finally {
330              joinPool(p);
331          }
# Line 687 | Line 723 | public class ThreadPoolExecutorSubclassT
723       */
724      public void testConstructor1() {
725          try {
726 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
726 >            new CustomTPE(-1, 1, 1L, SECONDS,
727 >                          new ArrayBlockingQueue<Runnable>(10));
728              shouldThrow();
729          } catch (IllegalArgumentException success) {}
730      }
# Line 697 | Line 734 | public class ThreadPoolExecutorSubclassT
734       */
735      public void testConstructor2() {
736          try {
737 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
737 >            new CustomTPE(1, -1, 1L, SECONDS,
738 >                          new ArrayBlockingQueue<Runnable>(10));
739              shouldThrow();
740          } catch (IllegalArgumentException success) {}
741      }
# Line 707 | Line 745 | public class ThreadPoolExecutorSubclassT
745       */
746      public void testConstructor3() {
747          try {
748 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
748 >            new CustomTPE(1, 0, 1L, SECONDS,
749 >                          new ArrayBlockingQueue<Runnable>(10));
750              shouldThrow();
751          } catch (IllegalArgumentException success) {}
752      }
# Line 717 | Line 756 | public class ThreadPoolExecutorSubclassT
756       */
757      public void testConstructor4() {
758          try {
759 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
759 >            new CustomTPE(1, 2, -1L, SECONDS,
760 >                          new ArrayBlockingQueue<Runnable>(10));
761              shouldThrow();
762          } catch (IllegalArgumentException success) {}
763      }
# Line 727 | Line 767 | public class ThreadPoolExecutorSubclassT
767       */
768      public void testConstructor5() {
769          try {
770 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
770 >            new CustomTPE(2, 1, 1L, SECONDS,
771 >                          new ArrayBlockingQueue<Runnable>(10));
772              shouldThrow();
773          } catch (IllegalArgumentException success) {}
774      }
# Line 737 | Line 778 | public class ThreadPoolExecutorSubclassT
778       */
779      public void testConstructorNullPointerException() {
780          try {
781 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
781 >            new CustomTPE(1, 2, 1L, SECONDS, null);
782              shouldThrow();
783          } catch (NullPointerException success) {}
784      }
# Line 747 | Line 788 | public class ThreadPoolExecutorSubclassT
788       */
789      public void testConstructor6() {
790          try {
791 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
791 >            new CustomTPE(-1, 1, 1L, SECONDS,
792 >                          new ArrayBlockingQueue<Runnable>(10),
793 >                          new SimpleThreadFactory());
794              shouldThrow();
795          } catch (IllegalArgumentException success) {}
796      }
# Line 757 | Line 800 | public class ThreadPoolExecutorSubclassT
800       */
801      public void testConstructor7() {
802          try {
803 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
803 >            new CustomTPE(1,-1, 1L, SECONDS,
804 >                          new ArrayBlockingQueue<Runnable>(10),
805 >                          new SimpleThreadFactory());
806              shouldThrow();
807          } catch (IllegalArgumentException success) {}
808      }
# Line 767 | Line 812 | public class ThreadPoolExecutorSubclassT
812       */
813      public void testConstructor8() {
814          try {
815 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
815 >            new CustomTPE(1, 0, 1L, SECONDS,
816 >                          new ArrayBlockingQueue<Runnable>(10),
817 >                          new SimpleThreadFactory());
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 777 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor9() {
826          try {
827 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
827 >            new CustomTPE(1, 2, -1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10),
829 >                          new SimpleThreadFactory());
830              shouldThrow();
831          } catch (IllegalArgumentException success) {}
832      }
# Line 787 | Line 836 | public class ThreadPoolExecutorSubclassT
836       */
837      public void testConstructor10() {
838          try {
839 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
839 >            new CustomTPE(2, 1, 1L, SECONDS,
840 >                          new ArrayBlockingQueue<Runnable>(10),
841 >                          new SimpleThreadFactory());
842              shouldThrow();
843          } catch (IllegalArgumentException success) {}
844      }
# Line 797 | Line 848 | public class ThreadPoolExecutorSubclassT
848       */
849      public void testConstructorNullPointerException2() {
850          try {
851 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
851 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
852              shouldThrow();
853          } catch (NullPointerException success) {}
854      }
# Line 807 | Line 858 | public class ThreadPoolExecutorSubclassT
858       */
859      public void testConstructorNullPointerException3() {
860          try {
861 <            ThreadFactory f = null;
862 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
861 >            new CustomTPE(1, 2, 1L, SECONDS,
862 >                          new ArrayBlockingQueue<Runnable>(10),
863 >                          (ThreadFactory) null);
864              shouldThrow();
865          } catch (NullPointerException success) {}
866      }
# Line 818 | Line 870 | public class ThreadPoolExecutorSubclassT
870       */
871      public void testConstructor11() {
872          try {
873 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
873 >            new CustomTPE(-1, 1, 1L, SECONDS,
874 >                          new ArrayBlockingQueue<Runnable>(10),
875 >                          new NoOpREHandler());
876              shouldThrow();
877          } catch (IllegalArgumentException success) {}
878      }
# Line 828 | Line 882 | public class ThreadPoolExecutorSubclassT
882       */
883      public void testConstructor12() {
884          try {
885 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
885 >            new CustomTPE(1, -1, 1L, SECONDS,
886 >                          new ArrayBlockingQueue<Runnable>(10),
887 >                          new NoOpREHandler());
888              shouldThrow();
889          } catch (IllegalArgumentException success) {}
890      }
# Line 838 | Line 894 | public class ThreadPoolExecutorSubclassT
894       */
895      public void testConstructor13() {
896          try {
897 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
897 >            new CustomTPE(1, 0, 1L, SECONDS,
898 >                          new ArrayBlockingQueue<Runnable>(10),
899 >                          new NoOpREHandler());
900              shouldThrow();
901          } catch (IllegalArgumentException success) {}
902      }
# Line 848 | Line 906 | public class ThreadPoolExecutorSubclassT
906       */
907      public void testConstructor14() {
908          try {
909 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
909 >            new CustomTPE(1, 2, -1L, SECONDS,
910 >                          new ArrayBlockingQueue<Runnable>(10),
911 >                          new NoOpREHandler());
912              shouldThrow();
913          } catch (IllegalArgumentException success) {}
914      }
# Line 858 | Line 918 | public class ThreadPoolExecutorSubclassT
918       */
919      public void testConstructor15() {
920          try {
921 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
921 >            new CustomTPE(2, 1, 1L, SECONDS,
922 >                          new ArrayBlockingQueue<Runnable>(10),
923 >                          new NoOpREHandler());
924              shouldThrow();
925          } catch (IllegalArgumentException success) {}
926      }
# Line 868 | Line 930 | public class ThreadPoolExecutorSubclassT
930       */
931      public void testConstructorNullPointerException4() {
932          try {
933 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
933 >            new CustomTPE(1, 2, 1L, SECONDS,
934 >                          null,
935 >                          new NoOpREHandler());
936              shouldThrow();
937          } catch (NullPointerException success) {}
938      }
# Line 878 | Line 942 | public class ThreadPoolExecutorSubclassT
942       */
943      public void testConstructorNullPointerException5() {
944          try {
945 <            RejectedExecutionHandler r = null;
946 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
945 >            new CustomTPE(1, 2, 1L, SECONDS,
946 >                          new ArrayBlockingQueue<Runnable>(10),
947 >                          (RejectedExecutionHandler) null);
948              shouldThrow();
949          } catch (NullPointerException success) {}
950      }
# Line 889 | Line 954 | public class ThreadPoolExecutorSubclassT
954       */
955      public void testConstructor16() {
956          try {
957 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
957 >            new CustomTPE(-1, 1, 1L, SECONDS,
958 >                          new ArrayBlockingQueue<Runnable>(10),
959 >                          new SimpleThreadFactory(),
960 >                          new NoOpREHandler());
961              shouldThrow();
962          } catch (IllegalArgumentException success) {}
963      }
# Line 899 | Line 967 | public class ThreadPoolExecutorSubclassT
967       */
968      public void testConstructor17() {
969          try {
970 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
970 >            new CustomTPE(1, -1, 1L, SECONDS,
971 >                          new ArrayBlockingQueue<Runnable>(10),
972 >                          new SimpleThreadFactory(),
973 >                          new NoOpREHandler());
974              shouldThrow();
975          } catch (IllegalArgumentException success) {}
976      }
# Line 909 | Line 980 | public class ThreadPoolExecutorSubclassT
980       */
981      public void testConstructor18() {
982          try {
983 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
983 >            new CustomTPE(1, 0, 1L, SECONDS,
984 >                          new ArrayBlockingQueue<Runnable>(10),
985 >                          new SimpleThreadFactory(),
986 >                          new NoOpREHandler());
987              shouldThrow();
988          } catch (IllegalArgumentException success) {}
989      }
# Line 919 | Line 993 | public class ThreadPoolExecutorSubclassT
993       */
994      public void testConstructor19() {
995          try {
996 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
996 >            new CustomTPE(1, 2, -1L, SECONDS,
997 >                          new ArrayBlockingQueue<Runnable>(10),
998 >                          new SimpleThreadFactory(),
999 >                          new NoOpREHandler());
1000              shouldThrow();
1001          } catch (IllegalArgumentException success) {}
1002      }
# Line 929 | Line 1006 | public class ThreadPoolExecutorSubclassT
1006       */
1007      public void testConstructor20() {
1008          try {
1009 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1009 >            new CustomTPE(2, 1, 1L, SECONDS,
1010 >                          new ArrayBlockingQueue<Runnable>(10),
1011 >                          new SimpleThreadFactory(),
1012 >                          new NoOpREHandler());
1013              shouldThrow();
1014          } catch (IllegalArgumentException success) {}
1015      }
# Line 939 | Line 1019 | public class ThreadPoolExecutorSubclassT
1019       */
1020      public void testConstructorNullPointerException6() {
1021          try {
1022 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1022 >            new CustomTPE(1, 2, 1L, SECONDS,
1023 >                          null,
1024 >                          new SimpleThreadFactory(),
1025 >                          new NoOpREHandler());
1026              shouldThrow();
1027          } catch (NullPointerException success) {}
1028      }
# Line 949 | Line 1032 | public class ThreadPoolExecutorSubclassT
1032       */
1033      public void testConstructorNullPointerException7() {
1034          try {
1035 <            RejectedExecutionHandler r = null;
1036 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1035 >            new CustomTPE(1, 2, 1L, SECONDS,
1036 >                          new ArrayBlockingQueue<Runnable>(10),
1037 >                          new SimpleThreadFactory(),
1038 >                          (RejectedExecutionHandler) null);
1039              shouldThrow();
1040          } catch (NullPointerException success) {}
1041      }
# Line 960 | Line 1045 | public class ThreadPoolExecutorSubclassT
1045       */
1046      public void testConstructorNullPointerException8() {
1047          try {
1048 <            new CustomTPE(1, 2,
964 <                          LONG_DELAY_MS, MILLISECONDS,
1048 >            new CustomTPE(1, 2, 1L, SECONDS,
1049                            new ArrayBlockingQueue<Runnable>(10),
1050                            (ThreadFactory) null,
1051                            new NoOpREHandler());
# Line 1223 | Line 1307 | public class ThreadPoolExecutorSubclassT
1307      public void testTerminated() {
1308          CustomTPE p = new CustomTPE();
1309          try { p.shutdown(); } catch (SecurityException ok) { return; }
1310 <        assertTrue(p.terminatedCalled);
1310 >        assertTrue(p.terminatedCalled());
1311          joinPool(p);
1312      }
1313  
# Line 1233 | Line 1317 | public class ThreadPoolExecutorSubclassT
1317      public void testBeforeAfter() throws InterruptedException {
1318          CustomTPE p = new CustomTPE();
1319          try {
1320 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1321 <            p.execute(r);
1322 <            delay(SHORT_DELAY_MS);
1323 <            assertTrue(r.done);
1324 <            assertTrue(p.beforeCalled);
1325 <            assertTrue(p.afterCalled);
1320 >            final CountDownLatch done = new CountDownLatch(1);
1321 >            p.execute(new CheckedRunnable() {
1322 >                public void realRun() {
1323 >                    done.countDown();
1324 >                }});
1325 >            await(p.afterCalled);
1326 >            assertEquals(0, done.getCount());
1327 >            assertTrue(p.afterCalled());
1328 >            assertTrue(p.beforeCalled());
1329              try { p.shutdown(); } catch (SecurityException ok) { return; }
1330          } finally {
1331              joinPool(p);
# Line 1655 | Line 1742 | public class ThreadPoolExecutorSubclassT
1742              l.add(new StringTask());
1743              List<Future<String>> futures =
1744                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1745 <            assertEquals(3, futures.size());
1746 <            Iterator<Future<String>> it = futures.iterator();
1747 <            Future<String> f1 = it.next();
1748 <            Future<String> f2 = it.next();
1749 <            Future<String> f3 = it.next();
1663 <            assertTrue(f1.isDone());
1664 <            assertTrue(f2.isDone());
1665 <            assertTrue(f3.isDone());
1666 <            assertFalse(f1.isCancelled());
1667 <            assertTrue(f2.isCancelled());
1745 >            assertEquals(l.size(), futures.size());
1746 >            for (Future future : futures)
1747 >                assertTrue(future.isDone());
1748 >            assertFalse(futures.get(0).isCancelled());
1749 >            assertTrue(futures.get(1).isCancelled());
1750          } finally {
1751              joinPool(e);
1752          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines