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.22 by jsr166, Mon Oct 11 08:28:05 2010 UTC vs.
Revision 1.39 by jsr166, Mon Sep 14 03:27:11 2015 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
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.CancellationException;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeoutException;
32 > import java.util.concurrent.TimeUnit;
33 > import java.util.concurrent.locks.Condition;
34 > import java.util.concurrent.locks.ReentrantLock;
35 >
36 > import junit.framework.Test;
37 > import junit.framework.TestSuite;
38  
39   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
40      public static void main(String[] args) {
41 <        junit.textui.TestRunner.run(suite());
41 >        main(suite(), args);
42      }
43      public static Test suite() {
44          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 60 | public class ThreadPoolExecutorSubclassT
60          CustomTask(final Runnable r, final V res) {
61              if (r == null) throw new NullPointerException();
62              callable = new Callable<V>() {
63 <            public V call() throws Exception { r.run(); return res; }};
63 >                public V call() throws Exception { r.run(); return res; }};
64          }
65          public boolean isDone() {
66              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 115 | Line 138 | public class ThreadPoolExecutorSubclassT
138          }
139      }
140  
118
141      static class CustomTPE extends ThreadPoolExecutor {
142          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
143              return new CustomTask<V>(c);
# Line 162 | Line 184 | public class ThreadPoolExecutorSubclassT
184                workQueue, threadFactory, handler);
185          }
186  
187 <        volatile boolean beforeCalled = false;
188 <        volatile boolean afterCalled = false;
189 <        volatile boolean terminatedCalled = false;
187 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
188 >        final CountDownLatch afterCalled = new CountDownLatch(1);
189 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
190 >
191          public CustomTPE() {
192              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
193          }
194          protected void beforeExecute(Thread t, Runnable r) {
195 <            beforeCalled = true;
195 >            beforeCalled.countDown();
196          }
197          protected void afterExecute(Runnable r, Throwable t) {
198 <            afterCalled = true;
198 >            afterCalled.countDown();
199          }
200          protected void terminated() {
201 <            terminatedCalled = true;
201 >            terminatedCalled.countDown();
202          }
203  
204 +        public boolean beforeCalled() {
205 +            return beforeCalled.getCount() == 0;
206 +        }
207 +        public boolean afterCalled() {
208 +            return afterCalled.getCount() == 0;
209 +        }
210 +        public boolean terminatedCalled() {
211 +            return terminatedCalled.getCount() == 0;
212 +        }
213      }
214  
215      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 220 | public class ThreadPoolExecutorSubclassT
220          }
221      }
222  
191
223      /**
224       * execute successfully executes a runnable
225       */
# Line 286 | Line 317 | public class ThreadPoolExecutorSubclassT
317                      threadProceed.await();
318                      threadDone.countDown();
319                  }});
320 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
320 >            await(threadStarted);
321              assertEquals(0, p.getCompletedTaskCount());
322              threadProceed.countDown();
323              threadDone.await();
324 <            Thread.sleep(SHORT_DELAY_MS);
325 <            assertEquals(1, p.getCompletedTaskCount());
324 >            long startTime = System.nanoTime();
325 >            while (p.getCompletedTaskCount() != 1) {
326 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
327 >                    fail("timed out");
328 >                Thread.yield();
329 >            }
330          } finally {
331              joinPool(p);
332          }
# Line 311 | Line 346 | public class ThreadPoolExecutorSubclassT
346       */
347      public void testGetKeepAliveTime() {
348          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
349 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
349 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
350          joinPool(p);
351      }
352  
318
353      /**
354       * getThreadFactory returns factory in constructor if not set
355       */
# Line 337 | Line 371 | public class ThreadPoolExecutorSubclassT
371          joinPool(p);
372      }
373  
340
374      /**
375       * setThreadFactory(null) throws NPE
376       */
# Line 374 | Line 407 | public class ThreadPoolExecutorSubclassT
407          joinPool(p);
408      }
409  
377
410      /**
411       * setRejectedExecutionHandler(null) throws NPE
412       */
# Line 389 | Line 421 | public class ThreadPoolExecutorSubclassT
421          }
422      }
423  
392
424      /**
425       * getLargestPoolSize increases, but doesn't overestimate, when
426       * multiple threads active
# Line 484 | Line 515 | public class ThreadPoolExecutorSubclassT
515      }
516  
517      /**
518 <     * isShutDown is false before shutdown, true after
518 >     * isShutdown is false before shutdown, true after
519       */
520      public void testIsShutdown() {
521  
# Line 495 | Line 526 | public class ThreadPoolExecutorSubclassT
526          joinPool(p);
527      }
528  
498
529      /**
530       * isTerminated is false before termination, true after
531       */
# Line 510 | Line 540 | public class ThreadPoolExecutorSubclassT
540              assertFalse(p.isTerminating());
541              p.execute(new CheckedRunnable() {
542                  public void realRun() throws InterruptedException {
513                    threadStarted.countDown();
543                      assertFalse(p.isTerminating());
544 +                    threadStarted.countDown();
545                      done.await();
546                  }});
547              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 539 | Line 569 | public class ThreadPoolExecutorSubclassT
569              assertFalse(p.isTerminating());
570              p.execute(new CheckedRunnable() {
571                  public void realRun() throws InterruptedException {
542                    threadStarted.countDown();
572                      assertFalse(p.isTerminating());
573 +                    threadStarted.countDown();
574                      done.await();
575                  }});
576              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 669 | Line 699 | public class ThreadPoolExecutorSubclassT
699      }
700  
701      /**
702 <     * shutDownNow returns a list containing tasks that were not run
702 >     * shutdownNow returns a list containing tasks that were not run
703       */
704 <    public void testShutDownNow() {
704 >    public void testShutdownNow() {
705          ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
706          List l;
707          try {
# Line 689 | Line 719 | public class ThreadPoolExecutorSubclassT
719  
720      // Exception Tests
721  
692
722      /**
723       * Constructor throws if corePoolSize argument is less than zero
724       */
725      public void testConstructor1() {
726          try {
727 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
727 >            new CustomTPE(-1, 1, 1L, SECONDS,
728 >                          new ArrayBlockingQueue<Runnable>(10));
729              shouldThrow();
730          } catch (IllegalArgumentException success) {}
731      }
# Line 705 | Line 735 | public class ThreadPoolExecutorSubclassT
735       */
736      public void testConstructor2() {
737          try {
738 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
738 >            new CustomTPE(1, -1, 1L, SECONDS,
739 >                          new ArrayBlockingQueue<Runnable>(10));
740              shouldThrow();
741          } catch (IllegalArgumentException success) {}
742      }
# Line 715 | Line 746 | public class ThreadPoolExecutorSubclassT
746       */
747      public void testConstructor3() {
748          try {
749 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
749 >            new CustomTPE(1, 0, 1L, SECONDS,
750 >                          new ArrayBlockingQueue<Runnable>(10));
751              shouldThrow();
752          } catch (IllegalArgumentException success) {}
753      }
# Line 725 | Line 757 | public class ThreadPoolExecutorSubclassT
757       */
758      public void testConstructor4() {
759          try {
760 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
760 >            new CustomTPE(1, 2, -1L, SECONDS,
761 >                          new ArrayBlockingQueue<Runnable>(10));
762              shouldThrow();
763          } catch (IllegalArgumentException success) {}
764      }
# Line 735 | Line 768 | public class ThreadPoolExecutorSubclassT
768       */
769      public void testConstructor5() {
770          try {
771 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
771 >            new CustomTPE(2, 1, 1L, SECONDS,
772 >                          new ArrayBlockingQueue<Runnable>(10));
773              shouldThrow();
774          } catch (IllegalArgumentException success) {}
775      }
# Line 745 | Line 779 | public class ThreadPoolExecutorSubclassT
779       */
780      public void testConstructorNullPointerException() {
781          try {
782 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
782 >            new CustomTPE(1, 2, 1L, SECONDS, null);
783              shouldThrow();
784          } catch (NullPointerException success) {}
785      }
786  
753
754
787      /**
788       * Constructor throws if corePoolSize argument is less than zero
789       */
790      public void testConstructor6() {
791          try {
792 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
792 >            new CustomTPE(-1, 1, 1L, SECONDS,
793 >                          new ArrayBlockingQueue<Runnable>(10),
794 >                          new SimpleThreadFactory());
795              shouldThrow();
796          } catch (IllegalArgumentException success) {}
797      }
# Line 767 | Line 801 | public class ThreadPoolExecutorSubclassT
801       */
802      public void testConstructor7() {
803          try {
804 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
804 >            new CustomTPE(1,-1, 1L, SECONDS,
805 >                          new ArrayBlockingQueue<Runnable>(10),
806 >                          new SimpleThreadFactory());
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 777 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor8() {
815          try {
816 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
816 >            new CustomTPE(1, 0, 1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10),
818 >                          new SimpleThreadFactory());
819              shouldThrow();
820          } catch (IllegalArgumentException success) {}
821      }
# Line 787 | Line 825 | public class ThreadPoolExecutorSubclassT
825       */
826      public void testConstructor9() {
827          try {
828 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
828 >            new CustomTPE(1, 2, -1L, SECONDS,
829 >                          new ArrayBlockingQueue<Runnable>(10),
830 >                          new SimpleThreadFactory());
831              shouldThrow();
832          } catch (IllegalArgumentException success) {}
833      }
# Line 797 | Line 837 | public class ThreadPoolExecutorSubclassT
837       */
838      public void testConstructor10() {
839          try {
840 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
840 >            new CustomTPE(2, 1, 1L, SECONDS,
841 >                          new ArrayBlockingQueue<Runnable>(10),
842 >                          new SimpleThreadFactory());
843              shouldThrow();
844          } catch (IllegalArgumentException success) {}
845      }
# Line 807 | Line 849 | public class ThreadPoolExecutorSubclassT
849       */
850      public void testConstructorNullPointerException2() {
851          try {
852 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
852 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
853              shouldThrow();
854          } catch (NullPointerException success) {}
855      }
# Line 817 | Line 859 | public class ThreadPoolExecutorSubclassT
859       */
860      public void testConstructorNullPointerException3() {
861          try {
862 <            ThreadFactory f = null;
863 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
862 >            new CustomTPE(1, 2, 1L, SECONDS,
863 >                          new ArrayBlockingQueue<Runnable>(10),
864 >                          (ThreadFactory) null);
865              shouldThrow();
866          } catch (NullPointerException success) {}
867      }
868  
826
869      /**
870       * Constructor throws if corePoolSize argument is less than zero
871       */
872      public void testConstructor11() {
873          try {
874 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
874 >            new CustomTPE(-1, 1, 1L, SECONDS,
875 >                          new ArrayBlockingQueue<Runnable>(10),
876 >                          new NoOpREHandler());
877              shouldThrow();
878          } catch (IllegalArgumentException success) {}
879      }
# Line 839 | Line 883 | public class ThreadPoolExecutorSubclassT
883       */
884      public void testConstructor12() {
885          try {
886 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
886 >            new CustomTPE(1, -1, 1L, SECONDS,
887 >                          new ArrayBlockingQueue<Runnable>(10),
888 >                          new NoOpREHandler());
889              shouldThrow();
890          } catch (IllegalArgumentException success) {}
891      }
# Line 849 | Line 895 | public class ThreadPoolExecutorSubclassT
895       */
896      public void testConstructor13() {
897          try {
898 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
898 >            new CustomTPE(1, 0, 1L, SECONDS,
899 >                          new ArrayBlockingQueue<Runnable>(10),
900 >                          new NoOpREHandler());
901              shouldThrow();
902          } catch (IllegalArgumentException success) {}
903      }
# Line 859 | Line 907 | public class ThreadPoolExecutorSubclassT
907       */
908      public void testConstructor14() {
909          try {
910 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
910 >            new CustomTPE(1, 2, -1L, SECONDS,
911 >                          new ArrayBlockingQueue<Runnable>(10),
912 >                          new NoOpREHandler());
913              shouldThrow();
914          } catch (IllegalArgumentException success) {}
915      }
# Line 869 | Line 919 | public class ThreadPoolExecutorSubclassT
919       */
920      public void testConstructor15() {
921          try {
922 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
922 >            new CustomTPE(2, 1, 1L, SECONDS,
923 >                          new ArrayBlockingQueue<Runnable>(10),
924 >                          new NoOpREHandler());
925              shouldThrow();
926          } catch (IllegalArgumentException success) {}
927      }
# Line 879 | Line 931 | public class ThreadPoolExecutorSubclassT
931       */
932      public void testConstructorNullPointerException4() {
933          try {
934 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
934 >            new CustomTPE(1, 2, 1L, SECONDS,
935 >                          null,
936 >                          new NoOpREHandler());
937              shouldThrow();
938          } catch (NullPointerException success) {}
939      }
# Line 889 | Line 943 | public class ThreadPoolExecutorSubclassT
943       */
944      public void testConstructorNullPointerException5() {
945          try {
946 <            RejectedExecutionHandler r = null;
947 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
946 >            new CustomTPE(1, 2, 1L, SECONDS,
947 >                          new ArrayBlockingQueue<Runnable>(10),
948 >                          (RejectedExecutionHandler) null);
949              shouldThrow();
950          } catch (NullPointerException success) {}
951      }
952  
898
953      /**
954       * Constructor throws if corePoolSize argument is less than zero
955       */
956      public void testConstructor16() {
957          try {
958 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
958 >            new CustomTPE(-1, 1, 1L, SECONDS,
959 >                          new ArrayBlockingQueue<Runnable>(10),
960 >                          new SimpleThreadFactory(),
961 >                          new NoOpREHandler());
962              shouldThrow();
963          } catch (IllegalArgumentException success) {}
964      }
# Line 911 | Line 968 | public class ThreadPoolExecutorSubclassT
968       */
969      public void testConstructor17() {
970          try {
971 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
971 >            new CustomTPE(1, -1, 1L, SECONDS,
972 >                          new ArrayBlockingQueue<Runnable>(10),
973 >                          new SimpleThreadFactory(),
974 >                          new NoOpREHandler());
975              shouldThrow();
976          } catch (IllegalArgumentException success) {}
977      }
# Line 921 | Line 981 | public class ThreadPoolExecutorSubclassT
981       */
982      public void testConstructor18() {
983          try {
984 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
984 >            new CustomTPE(1, 0, 1L, SECONDS,
985 >                          new ArrayBlockingQueue<Runnable>(10),
986 >                          new SimpleThreadFactory(),
987 >                          new NoOpREHandler());
988              shouldThrow();
989          } catch (IllegalArgumentException success) {}
990      }
# Line 931 | Line 994 | public class ThreadPoolExecutorSubclassT
994       */
995      public void testConstructor19() {
996          try {
997 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
997 >            new CustomTPE(1, 2, -1L, SECONDS,
998 >                          new ArrayBlockingQueue<Runnable>(10),
999 >                          new SimpleThreadFactory(),
1000 >                          new NoOpREHandler());
1001              shouldThrow();
1002          } catch (IllegalArgumentException success) {}
1003      }
# Line 941 | Line 1007 | public class ThreadPoolExecutorSubclassT
1007       */
1008      public void testConstructor20() {
1009          try {
1010 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1010 >            new CustomTPE(2, 1, 1L, SECONDS,
1011 >                          new ArrayBlockingQueue<Runnable>(10),
1012 >                          new SimpleThreadFactory(),
1013 >                          new NoOpREHandler());
1014              shouldThrow();
1015          } catch (IllegalArgumentException success) {}
1016      }
# Line 951 | Line 1020 | public class ThreadPoolExecutorSubclassT
1020       */
1021      public void testConstructorNullPointerException6() {
1022          try {
1023 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1023 >            new CustomTPE(1, 2, 1L, SECONDS,
1024 >                          null,
1025 >                          new SimpleThreadFactory(),
1026 >                          new NoOpREHandler());
1027              shouldThrow();
1028          } catch (NullPointerException success) {}
1029      }
# Line 961 | Line 1033 | public class ThreadPoolExecutorSubclassT
1033       */
1034      public void testConstructorNullPointerException7() {
1035          try {
1036 <            RejectedExecutionHandler r = null;
1037 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1036 >            new CustomTPE(1, 2, 1L, SECONDS,
1037 >                          new ArrayBlockingQueue<Runnable>(10),
1038 >                          new SimpleThreadFactory(),
1039 >                          (RejectedExecutionHandler) null);
1040              shouldThrow();
1041          } catch (NullPointerException success) {}
1042      }
# Line 972 | Line 1046 | public class ThreadPoolExecutorSubclassT
1046       */
1047      public void testConstructorNullPointerException8() {
1048          try {
1049 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1049 >            new CustomTPE(1, 2, 1L, SECONDS,
1050                            new ArrayBlockingQueue<Runnable>(10),
1051                            (ThreadFactory) null,
1052                            new NoOpREHandler());
# Line 981 | Line 1054 | public class ThreadPoolExecutorSubclassT
1054          } catch (NullPointerException success) {}
1055      }
1056  
984
1057      /**
1058       * execute throws RejectedExecutionException if saturated.
1059       */
# Line 1131 | Line 1203 | public class ThreadPoolExecutorSubclassT
1203          }
1204      }
1205  
1134
1206      /**
1207       * execute using DiscardOldestPolicy drops task on shutdown
1208       */
# Line 1149 | Line 1220 | public class ThreadPoolExecutorSubclassT
1220          }
1221      }
1222  
1152
1223      /**
1224       * execute(null) throws NPE
1225       */
1226      public void testExecuteNull() {
1227 <        ThreadPoolExecutor p = null;
1227 >        ThreadPoolExecutor p =
1228 >            new CustomTPE(1, 2, 1L, SECONDS,
1229 >                          new ArrayBlockingQueue<Runnable>(10));
1230          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1231              p.execute(null);
1232              shouldThrow();
1233          } catch (NullPointerException success) {}
# Line 1214 | Line 1285 | public class ThreadPoolExecutorSubclassT
1285          joinPool(p);
1286      }
1287  
1217
1288      /**
1289       * setKeepAliveTime throws IllegalArgumentException
1290       * when given a negative value
# Line 1239 | Line 1309 | public class ThreadPoolExecutorSubclassT
1309      public void testTerminated() {
1310          CustomTPE p = new CustomTPE();
1311          try { p.shutdown(); } catch (SecurityException ok) { return; }
1312 <        assertTrue(p.terminatedCalled);
1312 >        assertTrue(p.terminatedCalled());
1313          joinPool(p);
1314      }
1315  
# Line 1249 | Line 1319 | public class ThreadPoolExecutorSubclassT
1319      public void testBeforeAfter() throws InterruptedException {
1320          CustomTPE p = new CustomTPE();
1321          try {
1322 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1323 <            p.execute(r);
1324 <            Thread.sleep(SHORT_DELAY_MS);
1325 <            assertTrue(r.done);
1326 <            assertTrue(p.beforeCalled);
1327 <            assertTrue(p.afterCalled);
1322 >            final CountDownLatch done = new CountDownLatch(1);
1323 >            p.execute(new CheckedRunnable() {
1324 >                public void realRun() {
1325 >                    done.countDown();
1326 >                }});
1327 >            await(p.afterCalled);
1328 >            assertEquals(0, done.getCount());
1329 >            assertTrue(p.afterCalled());
1330 >            assertTrue(p.beforeCalled());
1331              try { p.shutdown(); } catch (SecurityException ok) { return; }
1332          } finally {
1333              joinPool(p);
# Line 1303 | Line 1376 | public class ThreadPoolExecutorSubclassT
1376          }
1377      }
1378  
1306
1379      /**
1380       * invokeAny(null) throws NPE
1381       */
# Line 1465 | Line 1537 | public class ThreadPoolExecutorSubclassT
1537          }
1538      }
1539  
1468
1469
1540      /**
1541       * timed invokeAny(null) throws NPE
1542       */
# Line 1668 | Line 1738 | public class ThreadPoolExecutorSubclassT
1738      public void testTimedInvokeAll6() throws Exception {
1739          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1740          try {
1741 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 <            l.add(new StringTask());
1743 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1744 <            l.add(new StringTask());
1745 <            List<Future<String>> futures =
1746 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1747 <            assertEquals(3, futures.size());
1748 <            Iterator<Future<String>> it = futures.iterator();
1749 <            Future<String> f1 = it.next();
1750 <            Future<String> f2 = it.next();
1751 <            Future<String> f3 = it.next();
1752 <            assertTrue(f1.isDone());
1753 <            assertTrue(f2.isDone());
1754 <            assertTrue(f3.isDone());
1755 <            assertFalse(f1.isCancelled());
1756 <            assertTrue(f2.isCancelled());
1741 >            for (long timeout = timeoutMillis();;) {
1742 >                List<Callable<String>> tasks = new ArrayList<>();
1743 >                tasks.add(new StringTask("0"));
1744 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1745 >                tasks.add(new StringTask("2"));
1746 >                long startTime = System.nanoTime();
1747 >                List<Future<String>> futures =
1748 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1749 >                assertEquals(tasks.size(), futures.size());
1750 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1751 >                for (Future future : futures)
1752 >                    assertTrue(future.isDone());
1753 >                assertTrue(futures.get(1).isCancelled());
1754 >                try {
1755 >                    assertEquals("0", futures.get(0).get());
1756 >                    assertEquals("2", futures.get(2).get());
1757 >                    break;
1758 >                } catch (CancellationException retryWithLongerTimeout) {
1759 >                    timeout *= 2;
1760 >                    if (timeout >= LONG_DELAY_MS / 2)
1761 >                        fail("expected exactly one task to be cancelled");
1762 >                }
1763 >            }
1764          } finally {
1765              joinPool(e);
1766          }
# Line 1726 | Line 1803 | public class ThreadPoolExecutorSubclassT
1803       * allowCoreThreadTimeOut(true) causes idle threads to time out
1804       */
1805      public void testAllowCoreThreadTimeOut_true() throws Exception {
1806 +        long keepAliveTime = timeoutMillis();
1807          final ThreadPoolExecutor p =
1808              new CustomTPE(2, 10,
1809 <                          SHORT_DELAY_MS, MILLISECONDS,
1809 >                          keepAliveTime, MILLISECONDS,
1810                            new ArrayBlockingQueue<Runnable>(10));
1811          final CountDownLatch threadStarted = new CountDownLatch(1);
1812          try {
1813              p.allowCoreThreadTimeOut(true);
1814              p.execute(new CheckedRunnable() {
1815 <                public void realRun() throws InterruptedException {
1815 >                public void realRun() {
1816                      threadStarted.countDown();
1817                      assertEquals(1, p.getPoolSize());
1818                  }});
1819 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1820 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1821 <                if (p.getPoolSize() == 0)
1822 <                    break;
1823 <                Thread.sleep(10);
1824 <            }
1819 >            await(threadStarted);
1820 >            delay(keepAliveTime);
1821 >            long startTime = System.nanoTime();
1822 >            while (p.getPoolSize() > 0
1823 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1824 >                Thread.yield();
1825 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1826              assertEquals(0, p.getPoolSize());
1827          } finally {
1828              joinPool(p);
# Line 1754 | Line 1833 | public class ThreadPoolExecutorSubclassT
1833       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1834       */
1835      public void testAllowCoreThreadTimeOut_false() throws Exception {
1836 +        long keepAliveTime = timeoutMillis();
1837          final ThreadPoolExecutor p =
1838              new CustomTPE(2, 10,
1839 <                          SHORT_DELAY_MS, MILLISECONDS,
1839 >                          keepAliveTime, MILLISECONDS,
1840                            new ArrayBlockingQueue<Runnable>(10));
1841          final CountDownLatch threadStarted = new CountDownLatch(1);
1842          try {
# Line 1766 | Line 1846 | public class ThreadPoolExecutorSubclassT
1846                      threadStarted.countDown();
1847                      assertTrue(p.getPoolSize() >= 1);
1848                  }});
1849 <            Thread.sleep(SMALL_DELAY_MS);
1849 >            delay(2 * keepAliveTime);
1850              assertTrue(p.getPoolSize() >= 1);
1851          } finally {
1852              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines