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.25 by jsr166, Tue Mar 15 19:47:07 2011 UTC vs.
Revision 1.38 by jsr166, Fri Sep 4 20:08: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 115 | Line 137 | public class ThreadPoolExecutorSubclassT
137          }
138      }
139  
118
140      static class CustomTPE extends ThreadPoolExecutor {
141          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
142              return new CustomTask<V>(c);
# Line 162 | 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 188 | Line 219 | public class ThreadPoolExecutorSubclassT
219          }
220      }
221  
191
222      /**
223       * execute successfully executes a runnable
224       */
# Line 286 | 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 <            Thread.sleep(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 311 | Line 345 | public class ThreadPoolExecutorSubclassT
345       */
346      public void testGetKeepAliveTime() {
347          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
348 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
348 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
349          joinPool(p);
350      }
351  
318
352      /**
353       * getThreadFactory returns factory in constructor if not set
354       */
# Line 337 | Line 370 | public class ThreadPoolExecutorSubclassT
370          joinPool(p);
371      }
372  
340
373      /**
374       * setThreadFactory(null) throws NPE
375       */
# Line 374 | Line 406 | public class ThreadPoolExecutorSubclassT
406          joinPool(p);
407      }
408  
377
409      /**
410       * setRejectedExecutionHandler(null) throws NPE
411       */
# Line 389 | Line 420 | public class ThreadPoolExecutorSubclassT
420          }
421      }
422  
392
423      /**
424       * getLargestPoolSize increases, but doesn't overestimate, when
425       * multiple threads active
# Line 484 | Line 514 | public class ThreadPoolExecutorSubclassT
514      }
515  
516      /**
517 <     * isShutDown is false before shutdown, true after
517 >     * isShutdown is false before shutdown, true after
518       */
519      public void testIsShutdown() {
520  
# Line 495 | Line 525 | public class ThreadPoolExecutorSubclassT
525          joinPool(p);
526      }
527  
498
528      /**
529       * isTerminated is false before termination, true after
530       */
# Line 669 | Line 698 | public class ThreadPoolExecutorSubclassT
698      }
699  
700      /**
701 <     * shutDownNow returns a list containing tasks that were not run
701 >     * shutdownNow returns a list containing tasks that were not run
702       */
703 <    public void testShutDownNow() {
703 >    public void testShutdownNow() {
704          ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
705          List l;
706          try {
# Line 689 | Line 718 | public class ThreadPoolExecutorSubclassT
718  
719      // Exception Tests
720  
692
721      /**
722       * Constructor throws if corePoolSize argument is less than zero
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 705 | 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 715 | 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 725 | 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 735 | 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 745 | 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      }
785  
753
754
786      /**
787       * Constructor throws if corePoolSize argument is less than zero
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 767 | 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 777 | 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 787 | 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 797 | 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 807 | 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 817 | 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      }
867  
826
868      /**
869       * Constructor throws if corePoolSize argument is less than zero
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 839 | 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 849 | 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 859 | 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 869 | 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 879 | 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 889 | 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      }
951  
898
952      /**
953       * Constructor throws if corePoolSize argument is less than zero
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 911 | 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 921 | 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 931 | 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 941 | 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 951 | 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 961 | 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 972 | Line 1045 | public class ThreadPoolExecutorSubclassT
1045       */
1046      public void testConstructorNullPointerException8() {
1047          try {
1048 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1048 >            new CustomTPE(1, 2, 1L, SECONDS,
1049                            new ArrayBlockingQueue<Runnable>(10),
1050                            (ThreadFactory) null,
1051                            new NoOpREHandler());
# Line 981 | Line 1053 | public class ThreadPoolExecutorSubclassT
1053          } catch (NullPointerException success) {}
1054      }
1055  
984
1056      /**
1057       * execute throws RejectedExecutionException if saturated.
1058       */
# Line 1131 | Line 1202 | public class ThreadPoolExecutorSubclassT
1202          }
1203      }
1204  
1134
1205      /**
1206       * execute using DiscardOldestPolicy drops task on shutdown
1207       */
# Line 1149 | Line 1219 | public class ThreadPoolExecutorSubclassT
1219          }
1220      }
1221  
1152
1222      /**
1223       * execute(null) throws NPE
1224       */
1225      public void testExecuteNull() {
1226 <        ThreadPoolExecutor p = null;
1226 >        ThreadPoolExecutor p =
1227 >            new CustomTPE(1, 2, 1L, SECONDS,
1228 >                          new ArrayBlockingQueue<Runnable>(10));
1229          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1230              p.execute(null);
1231              shouldThrow();
1232          } catch (NullPointerException success) {}
# Line 1214 | Line 1284 | public class ThreadPoolExecutorSubclassT
1284          joinPool(p);
1285      }
1286  
1217
1287      /**
1288       * setKeepAliveTime throws IllegalArgumentException
1289       * when given a negative value
# Line 1239 | Line 1308 | public class ThreadPoolExecutorSubclassT
1308      public void testTerminated() {
1309          CustomTPE p = new CustomTPE();
1310          try { p.shutdown(); } catch (SecurityException ok) { return; }
1311 <        assertTrue(p.terminatedCalled);
1311 >        assertTrue(p.terminatedCalled());
1312          joinPool(p);
1313      }
1314  
# Line 1249 | Line 1318 | public class ThreadPoolExecutorSubclassT
1318      public void testBeforeAfter() throws InterruptedException {
1319          CustomTPE p = new CustomTPE();
1320          try {
1321 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1322 <            p.execute(r);
1323 <            Thread.sleep(SHORT_DELAY_MS);
1324 <            assertTrue(r.done);
1325 <            assertTrue(p.beforeCalled);
1326 <            assertTrue(p.afterCalled);
1321 >            final CountDownLatch done = new CountDownLatch(1);
1322 >            p.execute(new CheckedRunnable() {
1323 >                public void realRun() {
1324 >                    done.countDown();
1325 >                }});
1326 >            await(p.afterCalled);
1327 >            assertEquals(0, done.getCount());
1328 >            assertTrue(p.afterCalled());
1329 >            assertTrue(p.beforeCalled());
1330              try { p.shutdown(); } catch (SecurityException ok) { return; }
1331          } finally {
1332              joinPool(p);
# Line 1303 | Line 1375 | public class ThreadPoolExecutorSubclassT
1375          }
1376      }
1377  
1306
1378      /**
1379       * invokeAny(null) throws NPE
1380       */
# Line 1465 | Line 1536 | public class ThreadPoolExecutorSubclassT
1536          }
1537      }
1538  
1468
1469
1539      /**
1540       * timed invokeAny(null) throws NPE
1541       */
# Line 1674 | Line 1743 | public class ThreadPoolExecutorSubclassT
1743              l.add(new StringTask());
1744              List<Future<String>> futures =
1745                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1746 <            assertEquals(3, futures.size());
1747 <            Iterator<Future<String>> it = futures.iterator();
1748 <            Future<String> f1 = it.next();
1749 <            Future<String> f2 = it.next();
1750 <            Future<String> f3 = it.next();
1682 <            assertTrue(f1.isDone());
1683 <            assertTrue(f2.isDone());
1684 <            assertTrue(f3.isDone());
1685 <            assertFalse(f1.isCancelled());
1686 <            assertTrue(f2.isCancelled());
1746 >            assertEquals(l.size(), futures.size());
1747 >            for (Future future : futures)
1748 >                assertTrue(future.isDone());
1749 >            assertFalse(futures.get(0).isCancelled());
1750 >            assertTrue(futures.get(1).isCancelled());
1751          } finally {
1752              joinPool(e);
1753          }
# Line 1726 | Line 1790 | public class ThreadPoolExecutorSubclassT
1790       * allowCoreThreadTimeOut(true) causes idle threads to time out
1791       */
1792      public void testAllowCoreThreadTimeOut_true() throws Exception {
1793 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1794          final ThreadPoolExecutor p =
1795              new CustomTPE(2, 10,
1796 <                          SHORT_DELAY_MS, MILLISECONDS,
1796 >                          coreThreadTimeOut, MILLISECONDS,
1797                            new ArrayBlockingQueue<Runnable>(10));
1798          final CountDownLatch threadStarted = new CountDownLatch(1);
1799          try {
# Line 1738 | Line 1803 | public class ThreadPoolExecutorSubclassT
1803                      threadStarted.countDown();
1804                      assertEquals(1, p.getPoolSize());
1805                  }});
1806 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1807 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1808 <                if (p.getPoolSize() == 0)
1809 <                    break;
1810 <                Thread.sleep(10);
1811 <            }
1806 >            await(threadStarted);
1807 >            delay(coreThreadTimeOut);
1808 >            long startTime = System.nanoTime();
1809 >            while (p.getPoolSize() > 0
1810 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1811 >                Thread.yield();
1812 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1813              assertEquals(0, p.getPoolSize());
1814          } finally {
1815              joinPool(p);
# Line 1754 | Line 1820 | public class ThreadPoolExecutorSubclassT
1820       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1821       */
1822      public void testAllowCoreThreadTimeOut_false() throws Exception {
1823 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1824          final ThreadPoolExecutor p =
1825              new CustomTPE(2, 10,
1826 <                          SHORT_DELAY_MS, MILLISECONDS,
1826 >                          coreThreadTimeOut, MILLISECONDS,
1827                            new ArrayBlockingQueue<Runnable>(10));
1828          final CountDownLatch threadStarted = new CountDownLatch(1);
1829          try {
# Line 1766 | Line 1833 | public class ThreadPoolExecutorSubclassT
1833                      threadStarted.countDown();
1834                      assertTrue(p.getPoolSize() >= 1);
1835                  }});
1836 <            Thread.sleep(SMALL_DELAY_MS);
1836 >            delay(2 * coreThreadTimeOut);
1837              assertTrue(p.getPoolSize() >= 1);
1838          } finally {
1839              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines