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.41 by jsr166, Mon Sep 28 02:41:29 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.atomic.AtomicInteger;
34 > import java.util.concurrent.locks.Condition;
35 > import java.util.concurrent.locks.ReentrantLock;
36 >
37 > import junit.framework.Test;
38 > import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 115 | Line 139 | public class ThreadPoolExecutorSubclassT
139          }
140      }
141  
118
142      static class CustomTPE extends ThreadPoolExecutor {
143          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
144              return new CustomTask<V>(c);
# Line 162 | Line 185 | public class ThreadPoolExecutorSubclassT
185                workQueue, threadFactory, handler);
186          }
187  
188 <        volatile boolean beforeCalled = false;
189 <        volatile boolean afterCalled = false;
190 <        volatile boolean terminatedCalled = false;
188 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
189 >        final CountDownLatch afterCalled = new CountDownLatch(1);
190 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
191 >
192          public CustomTPE() {
193              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
194          }
195          protected void beforeExecute(Thread t, Runnable r) {
196 <            beforeCalled = true;
196 >            beforeCalled.countDown();
197          }
198          protected void afterExecute(Runnable r, Throwable t) {
199 <            afterCalled = true;
199 >            afterCalled.countDown();
200          }
201          protected void terminated() {
202 <            terminatedCalled = true;
202 >            terminatedCalled.countDown();
203          }
204  
205 +        public boolean beforeCalled() {
206 +            return beforeCalled.getCount() == 0;
207 +        }
208 +        public boolean afterCalled() {
209 +            return afterCalled.getCount() == 0;
210 +        }
211 +        public boolean terminatedCalled() {
212 +            return terminatedCalled.getCount() == 0;
213 +        }
214      }
215  
216      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 221 | public class ThreadPoolExecutorSubclassT
221          }
222      }
223  
191
224      /**
225       * execute successfully executes a runnable
226       */
# Line 286 | Line 318 | public class ThreadPoolExecutorSubclassT
318                      threadProceed.await();
319                      threadDone.countDown();
320                  }});
321 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
321 >            await(threadStarted);
322              assertEquals(0, p.getCompletedTaskCount());
323              threadProceed.countDown();
324              threadDone.await();
325 <            Thread.sleep(SHORT_DELAY_MS);
326 <            assertEquals(1, p.getCompletedTaskCount());
325 >            long startTime = System.nanoTime();
326 >            while (p.getCompletedTaskCount() != 1) {
327 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
328 >                    fail("timed out");
329 >                Thread.yield();
330 >            }
331          } finally {
332              joinPool(p);
333          }
# Line 311 | Line 347 | public class ThreadPoolExecutorSubclassT
347       */
348      public void testGetKeepAliveTime() {
349          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
350 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
350 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
351          joinPool(p);
352      }
353  
318
354      /**
355       * getThreadFactory returns factory in constructor if not set
356       */
# Line 337 | Line 372 | public class ThreadPoolExecutorSubclassT
372          joinPool(p);
373      }
374  
340
375      /**
376       * setThreadFactory(null) throws NPE
377       */
# Line 374 | Line 408 | public class ThreadPoolExecutorSubclassT
408          joinPool(p);
409      }
410  
377
411      /**
412       * setRejectedExecutionHandler(null) throws NPE
413       */
# Line 389 | Line 422 | public class ThreadPoolExecutorSubclassT
422          }
423      }
424  
392
425      /**
426       * getLargestPoolSize increases, but doesn't overestimate, when
427       * multiple threads active
# Line 484 | Line 516 | public class ThreadPoolExecutorSubclassT
516      }
517  
518      /**
519 <     * isShutDown is false before shutdown, true after
519 >     * isShutdown is false before shutdown, true after
520       */
521      public void testIsShutdown() {
522  
# Line 495 | Line 527 | public class ThreadPoolExecutorSubclassT
527          joinPool(p);
528      }
529  
498
530      /**
531       * isTerminated is false before termination, true after
532       */
# Line 510 | Line 541 | public class ThreadPoolExecutorSubclassT
541              assertFalse(p.isTerminating());
542              p.execute(new CheckedRunnable() {
543                  public void realRun() throws InterruptedException {
513                    threadStarted.countDown();
544                      assertFalse(p.isTerminating());
545 +                    threadStarted.countDown();
546                      done.await();
547                  }});
548              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 539 | Line 570 | public class ThreadPoolExecutorSubclassT
570              assertFalse(p.isTerminating());
571              p.execute(new CheckedRunnable() {
572                  public void realRun() throws InterruptedException {
542                    threadStarted.countDown();
573                      assertFalse(p.isTerminating());
574 +                    threadStarted.countDown();
575                      done.await();
576                  }});
577              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 669 | Line 700 | public class ThreadPoolExecutorSubclassT
700      }
701  
702      /**
703 <     * shutDownNow returns a list containing tasks that were not run
703 >     * shutdownNow returns a list containing tasks that were not run,
704 >     * and those tasks are drained from the queue
705       */
706 <    public void testShutDownNow() {
707 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
708 <        List l;
709 <        try {
710 <            for (int i = 0; i < 5; i++)
711 <                p.execute(new MediumPossiblyInterruptedRunnable());
712 <        }
713 <        finally {
706 >    public void testShutdownNow() throws InterruptedException {
707 >        final int poolSize = 2;
708 >        final int count = 5;
709 >        final AtomicInteger ran = new AtomicInteger(0);
710 >        ThreadPoolExecutor p =
711 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
712 >                          new ArrayBlockingQueue<Runnable>(10));
713 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
714 >        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
715 >            threadsStarted.countDown();
716              try {
717 <                l = p.shutdownNow();
718 <            } catch (SecurityException ok) { return; }
717 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
718 >            } catch (InterruptedException success) {}
719 >            ran.getAndIncrement();
720 >        }};
721 >        for (int i = 0; i < count; i++)
722 >            p.execute(waiter);
723 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
724 >        final List<Runnable> queuedTasks;
725 >        try {
726 >            queuedTasks = p.shutdownNow();
727 >        } catch (SecurityException ok) {
728 >            return; // Allowed in case test doesn't have privs
729          }
730          assertTrue(p.isShutdown());
731 <        assertTrue(l.size() <= 4);
731 >        assertTrue(p.getQueue().isEmpty());
732 >        assertEquals(count - poolSize, queuedTasks.size());
733 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
734 >        assertTrue(p.isTerminated());
735 >        assertEquals(poolSize, ran.get());
736      }
737  
738      // Exception Tests
739  
692
740      /**
741       * Constructor throws if corePoolSize argument is less than zero
742       */
743      public void testConstructor1() {
744          try {
745 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
745 >            new CustomTPE(-1, 1, 1L, SECONDS,
746 >                          new ArrayBlockingQueue<Runnable>(10));
747              shouldThrow();
748          } catch (IllegalArgumentException success) {}
749      }
# Line 705 | Line 753 | public class ThreadPoolExecutorSubclassT
753       */
754      public void testConstructor2() {
755          try {
756 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
756 >            new CustomTPE(1, -1, 1L, SECONDS,
757 >                          new ArrayBlockingQueue<Runnable>(10));
758              shouldThrow();
759          } catch (IllegalArgumentException success) {}
760      }
# Line 715 | Line 764 | public class ThreadPoolExecutorSubclassT
764       */
765      public void testConstructor3() {
766          try {
767 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
767 >            new CustomTPE(1, 0, 1L, SECONDS,
768 >                          new ArrayBlockingQueue<Runnable>(10));
769              shouldThrow();
770          } catch (IllegalArgumentException success) {}
771      }
# Line 725 | Line 775 | public class ThreadPoolExecutorSubclassT
775       */
776      public void testConstructor4() {
777          try {
778 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
778 >            new CustomTPE(1, 2, -1L, SECONDS,
779 >                          new ArrayBlockingQueue<Runnable>(10));
780              shouldThrow();
781          } catch (IllegalArgumentException success) {}
782      }
# Line 735 | Line 786 | public class ThreadPoolExecutorSubclassT
786       */
787      public void testConstructor5() {
788          try {
789 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
789 >            new CustomTPE(2, 1, 1L, SECONDS,
790 >                          new ArrayBlockingQueue<Runnable>(10));
791              shouldThrow();
792          } catch (IllegalArgumentException success) {}
793      }
# Line 745 | Line 797 | public class ThreadPoolExecutorSubclassT
797       */
798      public void testConstructorNullPointerException() {
799          try {
800 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
800 >            new CustomTPE(1, 2, 1L, SECONDS, null);
801              shouldThrow();
802          } catch (NullPointerException success) {}
803      }
804  
753
754
805      /**
806       * Constructor throws if corePoolSize argument is less than zero
807       */
808      public void testConstructor6() {
809          try {
810 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
810 >            new CustomTPE(-1, 1, 1L, SECONDS,
811 >                          new ArrayBlockingQueue<Runnable>(10),
812 >                          new SimpleThreadFactory());
813              shouldThrow();
814          } catch (IllegalArgumentException success) {}
815      }
# Line 767 | Line 819 | public class ThreadPoolExecutorSubclassT
819       */
820      public void testConstructor7() {
821          try {
822 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
822 >            new CustomTPE(1,-1, 1L, SECONDS,
823 >                          new ArrayBlockingQueue<Runnable>(10),
824 >                          new SimpleThreadFactory());
825              shouldThrow();
826          } catch (IllegalArgumentException success) {}
827      }
# Line 777 | Line 831 | public class ThreadPoolExecutorSubclassT
831       */
832      public void testConstructor8() {
833          try {
834 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
834 >            new CustomTPE(1, 0, 1L, SECONDS,
835 >                          new ArrayBlockingQueue<Runnable>(10),
836 >                          new SimpleThreadFactory());
837              shouldThrow();
838          } catch (IllegalArgumentException success) {}
839      }
# Line 787 | Line 843 | public class ThreadPoolExecutorSubclassT
843       */
844      public void testConstructor9() {
845          try {
846 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
846 >            new CustomTPE(1, 2, -1L, SECONDS,
847 >                          new ArrayBlockingQueue<Runnable>(10),
848 >                          new SimpleThreadFactory());
849              shouldThrow();
850          } catch (IllegalArgumentException success) {}
851      }
# Line 797 | Line 855 | public class ThreadPoolExecutorSubclassT
855       */
856      public void testConstructor10() {
857          try {
858 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
858 >            new CustomTPE(2, 1, 1L, SECONDS,
859 >                          new ArrayBlockingQueue<Runnable>(10),
860 >                          new SimpleThreadFactory());
861              shouldThrow();
862          } catch (IllegalArgumentException success) {}
863      }
# Line 807 | Line 867 | public class ThreadPoolExecutorSubclassT
867       */
868      public void testConstructorNullPointerException2() {
869          try {
870 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
870 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
871              shouldThrow();
872          } catch (NullPointerException success) {}
873      }
# Line 817 | Line 877 | public class ThreadPoolExecutorSubclassT
877       */
878      public void testConstructorNullPointerException3() {
879          try {
880 <            ThreadFactory f = null;
881 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
880 >            new CustomTPE(1, 2, 1L, SECONDS,
881 >                          new ArrayBlockingQueue<Runnable>(10),
882 >                          (ThreadFactory) null);
883              shouldThrow();
884          } catch (NullPointerException success) {}
885      }
886  
826
887      /**
888       * Constructor throws if corePoolSize argument is less than zero
889       */
890      public void testConstructor11() {
891          try {
892 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
892 >            new CustomTPE(-1, 1, 1L, SECONDS,
893 >                          new ArrayBlockingQueue<Runnable>(10),
894 >                          new NoOpREHandler());
895              shouldThrow();
896          } catch (IllegalArgumentException success) {}
897      }
# Line 839 | Line 901 | public class ThreadPoolExecutorSubclassT
901       */
902      public void testConstructor12() {
903          try {
904 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
904 >            new CustomTPE(1, -1, 1L, SECONDS,
905 >                          new ArrayBlockingQueue<Runnable>(10),
906 >                          new NoOpREHandler());
907              shouldThrow();
908          } catch (IllegalArgumentException success) {}
909      }
# Line 849 | Line 913 | public class ThreadPoolExecutorSubclassT
913       */
914      public void testConstructor13() {
915          try {
916 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
916 >            new CustomTPE(1, 0, 1L, SECONDS,
917 >                          new ArrayBlockingQueue<Runnable>(10),
918 >                          new NoOpREHandler());
919              shouldThrow();
920          } catch (IllegalArgumentException success) {}
921      }
# Line 859 | Line 925 | public class ThreadPoolExecutorSubclassT
925       */
926      public void testConstructor14() {
927          try {
928 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
928 >            new CustomTPE(1, 2, -1L, SECONDS,
929 >                          new ArrayBlockingQueue<Runnable>(10),
930 >                          new NoOpREHandler());
931              shouldThrow();
932          } catch (IllegalArgumentException success) {}
933      }
# Line 869 | Line 937 | public class ThreadPoolExecutorSubclassT
937       */
938      public void testConstructor15() {
939          try {
940 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
940 >            new CustomTPE(2, 1, 1L, SECONDS,
941 >                          new ArrayBlockingQueue<Runnable>(10),
942 >                          new NoOpREHandler());
943              shouldThrow();
944          } catch (IllegalArgumentException success) {}
945      }
# Line 879 | Line 949 | public class ThreadPoolExecutorSubclassT
949       */
950      public void testConstructorNullPointerException4() {
951          try {
952 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
952 >            new CustomTPE(1, 2, 1L, SECONDS,
953 >                          null,
954 >                          new NoOpREHandler());
955              shouldThrow();
956          } catch (NullPointerException success) {}
957      }
# Line 889 | Line 961 | public class ThreadPoolExecutorSubclassT
961       */
962      public void testConstructorNullPointerException5() {
963          try {
964 <            RejectedExecutionHandler r = null;
965 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
964 >            new CustomTPE(1, 2, 1L, SECONDS,
965 >                          new ArrayBlockingQueue<Runnable>(10),
966 >                          (RejectedExecutionHandler) null);
967              shouldThrow();
968          } catch (NullPointerException success) {}
969      }
970  
898
971      /**
972       * Constructor throws if corePoolSize argument is less than zero
973       */
974      public void testConstructor16() {
975          try {
976 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
976 >            new CustomTPE(-1, 1, 1L, SECONDS,
977 >                          new ArrayBlockingQueue<Runnable>(10),
978 >                          new SimpleThreadFactory(),
979 >                          new NoOpREHandler());
980              shouldThrow();
981          } catch (IllegalArgumentException success) {}
982      }
# Line 911 | Line 986 | public class ThreadPoolExecutorSubclassT
986       */
987      public void testConstructor17() {
988          try {
989 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
989 >            new CustomTPE(1, -1, 1L, SECONDS,
990 >                          new ArrayBlockingQueue<Runnable>(10),
991 >                          new SimpleThreadFactory(),
992 >                          new NoOpREHandler());
993              shouldThrow();
994          } catch (IllegalArgumentException success) {}
995      }
# Line 921 | Line 999 | public class ThreadPoolExecutorSubclassT
999       */
1000      public void testConstructor18() {
1001          try {
1002 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1002 >            new CustomTPE(1, 0, 1L, SECONDS,
1003 >                          new ArrayBlockingQueue<Runnable>(10),
1004 >                          new SimpleThreadFactory(),
1005 >                          new NoOpREHandler());
1006              shouldThrow();
1007          } catch (IllegalArgumentException success) {}
1008      }
# Line 931 | Line 1012 | public class ThreadPoolExecutorSubclassT
1012       */
1013      public void testConstructor19() {
1014          try {
1015 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1015 >            new CustomTPE(1, 2, -1L, SECONDS,
1016 >                          new ArrayBlockingQueue<Runnable>(10),
1017 >                          new SimpleThreadFactory(),
1018 >                          new NoOpREHandler());
1019              shouldThrow();
1020          } catch (IllegalArgumentException success) {}
1021      }
# Line 941 | Line 1025 | public class ThreadPoolExecutorSubclassT
1025       */
1026      public void testConstructor20() {
1027          try {
1028 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1028 >            new CustomTPE(2, 1, 1L, SECONDS,
1029 >                          new ArrayBlockingQueue<Runnable>(10),
1030 >                          new SimpleThreadFactory(),
1031 >                          new NoOpREHandler());
1032              shouldThrow();
1033          } catch (IllegalArgumentException success) {}
1034      }
# Line 951 | Line 1038 | public class ThreadPoolExecutorSubclassT
1038       */
1039      public void testConstructorNullPointerException6() {
1040          try {
1041 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1041 >            new CustomTPE(1, 2, 1L, SECONDS,
1042 >                          null,
1043 >                          new SimpleThreadFactory(),
1044 >                          new NoOpREHandler());
1045              shouldThrow();
1046          } catch (NullPointerException success) {}
1047      }
# Line 961 | Line 1051 | public class ThreadPoolExecutorSubclassT
1051       */
1052      public void testConstructorNullPointerException7() {
1053          try {
1054 <            RejectedExecutionHandler r = null;
1055 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1054 >            new CustomTPE(1, 2, 1L, SECONDS,
1055 >                          new ArrayBlockingQueue<Runnable>(10),
1056 >                          new SimpleThreadFactory(),
1057 >                          (RejectedExecutionHandler) null);
1058              shouldThrow();
1059          } catch (NullPointerException success) {}
1060      }
# Line 972 | Line 1064 | public class ThreadPoolExecutorSubclassT
1064       */
1065      public void testConstructorNullPointerException8() {
1066          try {
1067 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1067 >            new CustomTPE(1, 2, 1L, SECONDS,
1068                            new ArrayBlockingQueue<Runnable>(10),
1069                            (ThreadFactory) null,
1070                            new NoOpREHandler());
# Line 981 | Line 1072 | public class ThreadPoolExecutorSubclassT
1072          } catch (NullPointerException success) {}
1073      }
1074  
984
1075      /**
1076       * execute throws RejectedExecutionException if saturated.
1077       */
# Line 1131 | Line 1221 | public class ThreadPoolExecutorSubclassT
1221          }
1222      }
1223  
1134
1224      /**
1225       * execute using DiscardOldestPolicy drops task on shutdown
1226       */
# Line 1149 | Line 1238 | public class ThreadPoolExecutorSubclassT
1238          }
1239      }
1240  
1152
1241      /**
1242       * execute(null) throws NPE
1243       */
1244      public void testExecuteNull() {
1245 <        ThreadPoolExecutor p = null;
1245 >        ThreadPoolExecutor p =
1246 >            new CustomTPE(1, 2, 1L, SECONDS,
1247 >                          new ArrayBlockingQueue<Runnable>(10));
1248          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1249              p.execute(null);
1250              shouldThrow();
1251          } catch (NullPointerException success) {}
# Line 1214 | Line 1303 | public class ThreadPoolExecutorSubclassT
1303          joinPool(p);
1304      }
1305  
1217
1306      /**
1307       * setKeepAliveTime throws IllegalArgumentException
1308       * when given a negative value
# Line 1239 | Line 1327 | public class ThreadPoolExecutorSubclassT
1327      public void testTerminated() {
1328          CustomTPE p = new CustomTPE();
1329          try { p.shutdown(); } catch (SecurityException ok) { return; }
1330 <        assertTrue(p.terminatedCalled);
1330 >        assertTrue(p.terminatedCalled());
1331          joinPool(p);
1332      }
1333  
# Line 1249 | Line 1337 | public class ThreadPoolExecutorSubclassT
1337      public void testBeforeAfter() throws InterruptedException {
1338          CustomTPE p = new CustomTPE();
1339          try {
1340 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1341 <            p.execute(r);
1342 <            Thread.sleep(SHORT_DELAY_MS);
1343 <            assertTrue(r.done);
1344 <            assertTrue(p.beforeCalled);
1345 <            assertTrue(p.afterCalled);
1340 >            final CountDownLatch done = new CountDownLatch(1);
1341 >            p.execute(new CheckedRunnable() {
1342 >                public void realRun() {
1343 >                    done.countDown();
1344 >                }});
1345 >            await(p.afterCalled);
1346 >            assertEquals(0, done.getCount());
1347 >            assertTrue(p.afterCalled());
1348 >            assertTrue(p.beforeCalled());
1349              try { p.shutdown(); } catch (SecurityException ok) { return; }
1350          } finally {
1351              joinPool(p);
# Line 1303 | Line 1394 | public class ThreadPoolExecutorSubclassT
1394          }
1395      }
1396  
1306
1397      /**
1398       * invokeAny(null) throws NPE
1399       */
# Line 1465 | Line 1555 | public class ThreadPoolExecutorSubclassT
1555          }
1556      }
1557  
1468
1469
1558      /**
1559       * timed invokeAny(null) throws NPE
1560       */
# Line 1668 | Line 1756 | public class ThreadPoolExecutorSubclassT
1756      public void testTimedInvokeAll6() throws Exception {
1757          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758          try {
1759 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1760 <            l.add(new StringTask());
1761 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1762 <            l.add(new StringTask());
1763 <            List<Future<String>> futures =
1764 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1765 <            assertEquals(3, futures.size());
1766 <            Iterator<Future<String>> it = futures.iterator();
1767 <            Future<String> f1 = it.next();
1768 <            Future<String> f2 = it.next();
1769 <            Future<String> f3 = it.next();
1770 <            assertTrue(f1.isDone());
1771 <            assertTrue(f2.isDone());
1772 <            assertTrue(f3.isDone());
1773 <            assertFalse(f1.isCancelled());
1774 <            assertTrue(f2.isCancelled());
1759 >            for (long timeout = timeoutMillis();;) {
1760 >                List<Callable<String>> tasks = new ArrayList<>();
1761 >                tasks.add(new StringTask("0"));
1762 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1763 >                tasks.add(new StringTask("2"));
1764 >                long startTime = System.nanoTime();
1765 >                List<Future<String>> futures =
1766 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1767 >                assertEquals(tasks.size(), futures.size());
1768 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1769 >                for (Future future : futures)
1770 >                    assertTrue(future.isDone());
1771 >                assertTrue(futures.get(1).isCancelled());
1772 >                try {
1773 >                    assertEquals("0", futures.get(0).get());
1774 >                    assertEquals("2", futures.get(2).get());
1775 >                    break;
1776 >                } catch (CancellationException retryWithLongerTimeout) {
1777 >                    timeout *= 2;
1778 >                    if (timeout >= LONG_DELAY_MS / 2)
1779 >                        fail("expected exactly one task to be cancelled");
1780 >                }
1781 >            }
1782          } finally {
1783              joinPool(e);
1784          }
# Line 1726 | Line 1821 | public class ThreadPoolExecutorSubclassT
1821       * allowCoreThreadTimeOut(true) causes idle threads to time out
1822       */
1823      public void testAllowCoreThreadTimeOut_true() throws Exception {
1824 +        long keepAliveTime = timeoutMillis();
1825          final ThreadPoolExecutor p =
1826              new CustomTPE(2, 10,
1827 <                          SHORT_DELAY_MS, MILLISECONDS,
1827 >                          keepAliveTime, MILLISECONDS,
1828                            new ArrayBlockingQueue<Runnable>(10));
1829          final CountDownLatch threadStarted = new CountDownLatch(1);
1830          try {
1831              p.allowCoreThreadTimeOut(true);
1832              p.execute(new CheckedRunnable() {
1833 <                public void realRun() throws InterruptedException {
1833 >                public void realRun() {
1834                      threadStarted.countDown();
1835                      assertEquals(1, p.getPoolSize());
1836                  }});
1837 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1838 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1839 <                if (p.getPoolSize() == 0)
1840 <                    break;
1841 <                Thread.sleep(10);
1842 <            }
1837 >            await(threadStarted);
1838 >            delay(keepAliveTime);
1839 >            long startTime = System.nanoTime();
1840 >            while (p.getPoolSize() > 0
1841 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1842 >                Thread.yield();
1843 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1844              assertEquals(0, p.getPoolSize());
1845          } finally {
1846              joinPool(p);
# Line 1754 | Line 1851 | public class ThreadPoolExecutorSubclassT
1851       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1852       */
1853      public void testAllowCoreThreadTimeOut_false() throws Exception {
1854 +        long keepAliveTime = timeoutMillis();
1855          final ThreadPoolExecutor p =
1856              new CustomTPE(2, 10,
1857 <                          SHORT_DELAY_MS, MILLISECONDS,
1857 >                          keepAliveTime, MILLISECONDS,
1858                            new ArrayBlockingQueue<Runnable>(10));
1859          final CountDownLatch threadStarted = new CountDownLatch(1);
1860          try {
# Line 1766 | Line 1864 | public class ThreadPoolExecutorSubclassT
1864                      threadStarted.countDown();
1865                      assertTrue(p.getPoolSize() >= 1);
1866                  }});
1867 <            Thread.sleep(SMALL_DELAY_MS);
1867 >            delay(2 * keepAliveTime);
1868              assertTrue(p.getPoolSize() >= 1);
1869          } finally {
1870              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines