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.53 by jsr166, Sun Oct 4 01:23:41 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.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 77 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 90 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 101 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
106 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 115 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
118
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 162 | Line 190 | public class ThreadPoolExecutorSubclassT
190                workQueue, threadFactory, handler);
191          }
192  
193 <        volatile boolean beforeCalled = false;
194 <        volatile boolean afterCalled = false;
195 <        volatile boolean terminatedCalled = false;
193 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
194 >        final CountDownLatch afterCalled = new CountDownLatch(1);
195 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 >
197          public CustomTPE() {
198              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
191
229      /**
230       * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
204 <            }};
205 <        try {
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
208 <        } finally {
209 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 221 | Line 254 | public class ThreadPoolExecutorSubclassT
254                            new ArrayBlockingQueue<Runnable>(10));
255          final CountDownLatch threadStarted = new CountDownLatch(1);
256          final CountDownLatch done = new CountDownLatch(1);
257 <        try {
257 >        try (PoolCleaner cleaner = cleaner(p)) {
258              assertEquals(0, p.getActiveCount());
259              p.execute(new CheckedRunnable() {
260                  public void realRun() throws InterruptedException {
# Line 231 | Line 264 | public class ThreadPoolExecutorSubclassT
264                  }});
265              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
234        } finally {
267              done.countDown();
236            joinPool(p);
268          }
269      }
270  
# Line 241 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p.getPoolSize());
277 <        assertTrue(p.prestartCoreThread());
278 <        assertEquals(1, p.getPoolSize());
279 <        assertTrue(p.prestartCoreThread());
280 <        assertEquals(2, p.getPoolSize());
281 <        assertFalse(p.prestartCoreThread());
282 <        assertEquals(2, p.getPoolSize());
283 <        joinPool(p);
275 >        ThreadPoolExecutor p =
276 >            new CustomTPE(2, 6,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        try (PoolCleaner cleaner = cleaner(p)) {
280 >            assertEquals(0, p.getPoolSize());
281 >            assertTrue(p.prestartCoreThread());
282 >            assertEquals(1, p.getPoolSize());
283 >            assertTrue(p.prestartCoreThread());
284 >            assertEquals(2, p.getPoolSize());
285 >            assertFalse(p.prestartCoreThread());
286 >            assertEquals(2, p.getPoolSize());
287 >            p.setCorePoolSize(4);
288 >            assertTrue(p.prestartCoreThread());
289 >            assertEquals(3, p.getPoolSize());
290 >            assertTrue(p.prestartCoreThread());
291 >            assertEquals(4, p.getPoolSize());
292 >            assertFalse(p.prestartCoreThread());
293 >            assertEquals(4, p.getPoolSize());
294 >        }
295      }
296  
297      /**
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p.getPoolSize());
303 <        p.prestartAllCoreThreads();
304 <        assertEquals(2, p.getPoolSize());
305 <        p.prestartAllCoreThreads();
306 <        assertEquals(2, p.getPoolSize());
307 <        joinPool(p);
301 >        ThreadPoolExecutor p =
302 >            new CustomTPE(2, 6,
303 >                          LONG_DELAY_MS, MILLISECONDS,
304 >                          new ArrayBlockingQueue<Runnable>(10));
305 >        try (PoolCleaner cleaner = cleaner(p)) {
306 >            assertEquals(0, p.getPoolSize());
307 >            p.prestartAllCoreThreads();
308 >            assertEquals(2, p.getPoolSize());
309 >            p.prestartAllCoreThreads();
310 >            assertEquals(2, p.getPoolSize());
311 >            p.setCorePoolSize(4);
312 >            p.prestartAllCoreThreads();
313 >            assertEquals(4, p.getPoolSize());
314 >            p.prestartAllCoreThreads();
315 >            assertEquals(4, p.getPoolSize());
316 >        }
317      }
318  
319      /**
# Line 286 | Line 337 | public class ThreadPoolExecutorSubclassT
337                      threadProceed.await();
338                      threadDone.countDown();
339                  }});
340 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
340 >            await(threadStarted);
341              assertEquals(0, p.getCompletedTaskCount());
342              threadProceed.countDown();
343              threadDone.await();
344 <            Thread.sleep(SHORT_DELAY_MS);
345 <            assertEquals(1, p.getCompletedTaskCount());
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350          } finally {
351              joinPool(p);
352          }
# Line 311 | Line 366 | public class ThreadPoolExecutorSubclassT
366       */
367      public void testGetKeepAliveTime() {
368          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
369 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
370          joinPool(p);
371      }
372  
318
373      /**
374       * getThreadFactory returns factory in constructor if not set
375       */
# Line 337 | Line 391 | public class ThreadPoolExecutorSubclassT
391          joinPool(p);
392      }
393  
340
394      /**
395       * setThreadFactory(null) throws NPE
396       */
# Line 374 | Line 427 | public class ThreadPoolExecutorSubclassT
427          joinPool(p);
428      }
429  
377
430      /**
431       * setRejectedExecutionHandler(null) throws NPE
432       */
# Line 389 | Line 441 | public class ThreadPoolExecutorSubclassT
441          }
442      }
443  
392
444      /**
445       * getLargestPoolSize increases, but doesn't overestimate, when
446       * multiple threads active
# Line 484 | Line 535 | public class ThreadPoolExecutorSubclassT
535      }
536  
537      /**
538 <     * isShutDown is false before shutdown, true after
538 >     * isShutdown is false before shutdown, true after
539       */
540      public void testIsShutdown() {
541  
# Line 495 | Line 546 | public class ThreadPoolExecutorSubclassT
546          joinPool(p);
547      }
548  
498
549      /**
550       * isTerminated is false before termination, true after
551       */
# Line 669 | Line 719 | public class ThreadPoolExecutorSubclassT
719      }
720  
721      /**
722 <     * shutDownNow returns a list containing tasks that were not run
722 >     * shutdownNow returns a list containing tasks that were not run,
723 >     * and those tasks are drained from the queue
724       */
725 <    public void testShutDownNow() {
726 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
727 <        List l;
728 <        try {
729 <            for (int i = 0; i < 5; i++)
730 <                p.execute(new MediumPossiblyInterruptedRunnable());
731 <        }
732 <        finally {
725 >    public void testShutdownNow() throws InterruptedException {
726 >        final int poolSize = 2;
727 >        final int count = 5;
728 >        final AtomicInteger ran = new AtomicInteger(0);
729 >        ThreadPoolExecutor p =
730 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
731 >                          new ArrayBlockingQueue<Runnable>(10));
732 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
733 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
734 >            threadsStarted.countDown();
735              try {
736 <                l = p.shutdownNow();
737 <            } catch (SecurityException ok) { return; }
736 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
737 >            } catch (InterruptedException success) {}
738 >            ran.getAndIncrement();
739 >        }};
740 >        for (int i = 0; i < count; i++)
741 >            p.execute(waiter);
742 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
743 >        assertEquals(poolSize, p.getActiveCount());
744 >        assertEquals(0, p.getCompletedTaskCount());
745 >        final List<Runnable> queuedTasks;
746 >        try {
747 >            queuedTasks = p.shutdownNow();
748 >        } catch (SecurityException ok) {
749 >            return; // Allowed in case test doesn't have privs
750          }
751          assertTrue(p.isShutdown());
752 <        assertTrue(l.size() <= 4);
752 >        assertTrue(p.getQueue().isEmpty());
753 >        assertEquals(count - poolSize, queuedTasks.size());
754 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
755 >        assertTrue(p.isTerminated());
756 >        assertEquals(poolSize, ran.get());
757 >        assertEquals(poolSize, p.getCompletedTaskCount());
758      }
759  
760      // Exception Tests
761  
692
762      /**
763       * Constructor throws if corePoolSize argument is less than zero
764       */
765      public void testConstructor1() {
766          try {
767 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
767 >            new CustomTPE(-1, 1, 1L, SECONDS,
768 >                          new ArrayBlockingQueue<Runnable>(10));
769              shouldThrow();
770          } catch (IllegalArgumentException success) {}
771      }
# Line 705 | Line 775 | public class ThreadPoolExecutorSubclassT
775       */
776      public void testConstructor2() {
777          try {
778 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
778 >            new CustomTPE(1, -1, 1L, SECONDS,
779 >                          new ArrayBlockingQueue<Runnable>(10));
780              shouldThrow();
781          } catch (IllegalArgumentException success) {}
782      }
# Line 715 | Line 786 | public class ThreadPoolExecutorSubclassT
786       */
787      public void testConstructor3() {
788          try {
789 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
789 >            new CustomTPE(1, 0, 1L, SECONDS,
790 >                          new ArrayBlockingQueue<Runnable>(10));
791              shouldThrow();
792          } catch (IllegalArgumentException success) {}
793      }
# Line 725 | Line 797 | public class ThreadPoolExecutorSubclassT
797       */
798      public void testConstructor4() {
799          try {
800 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
800 >            new CustomTPE(1, 2, -1L, SECONDS,
801 >                          new ArrayBlockingQueue<Runnable>(10));
802              shouldThrow();
803          } catch (IllegalArgumentException success) {}
804      }
# Line 735 | Line 808 | public class ThreadPoolExecutorSubclassT
808       */
809      public void testConstructor5() {
810          try {
811 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
811 >            new CustomTPE(2, 1, 1L, SECONDS,
812 >                          new ArrayBlockingQueue<Runnable>(10));
813              shouldThrow();
814          } catch (IllegalArgumentException success) {}
815      }
# Line 745 | Line 819 | public class ThreadPoolExecutorSubclassT
819       */
820      public void testConstructorNullPointerException() {
821          try {
822 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
822 >            new CustomTPE(1, 2, 1L, SECONDS, null);
823              shouldThrow();
824          } catch (NullPointerException success) {}
825      }
826  
753
754
827      /**
828       * Constructor throws if corePoolSize argument is less than zero
829       */
830      public void testConstructor6() {
831          try {
832 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
832 >            new CustomTPE(-1, 1, 1L, SECONDS,
833 >                          new ArrayBlockingQueue<Runnable>(10),
834 >                          new SimpleThreadFactory());
835              shouldThrow();
836          } catch (IllegalArgumentException success) {}
837      }
# Line 767 | Line 841 | public class ThreadPoolExecutorSubclassT
841       */
842      public void testConstructor7() {
843          try {
844 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
844 >            new CustomTPE(1,-1, 1L, SECONDS,
845 >                          new ArrayBlockingQueue<Runnable>(10),
846 >                          new SimpleThreadFactory());
847              shouldThrow();
848          } catch (IllegalArgumentException success) {}
849      }
# Line 777 | Line 853 | public class ThreadPoolExecutorSubclassT
853       */
854      public void testConstructor8() {
855          try {
856 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
856 >            new CustomTPE(1, 0, 1L, SECONDS,
857 >                          new ArrayBlockingQueue<Runnable>(10),
858 >                          new SimpleThreadFactory());
859              shouldThrow();
860          } catch (IllegalArgumentException success) {}
861      }
# Line 787 | Line 865 | public class ThreadPoolExecutorSubclassT
865       */
866      public void testConstructor9() {
867          try {
868 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
868 >            new CustomTPE(1, 2, -1L, SECONDS,
869 >                          new ArrayBlockingQueue<Runnable>(10),
870 >                          new SimpleThreadFactory());
871              shouldThrow();
872          } catch (IllegalArgumentException success) {}
873      }
# Line 797 | Line 877 | public class ThreadPoolExecutorSubclassT
877       */
878      public void testConstructor10() {
879          try {
880 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
880 >            new CustomTPE(2, 1, 1L, SECONDS,
881 >                          new ArrayBlockingQueue<Runnable>(10),
882 >                          new SimpleThreadFactory());
883              shouldThrow();
884          } catch (IllegalArgumentException success) {}
885      }
# Line 807 | Line 889 | public class ThreadPoolExecutorSubclassT
889       */
890      public void testConstructorNullPointerException2() {
891          try {
892 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
892 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
893              shouldThrow();
894          } catch (NullPointerException success) {}
895      }
# Line 817 | Line 899 | public class ThreadPoolExecutorSubclassT
899       */
900      public void testConstructorNullPointerException3() {
901          try {
902 <            ThreadFactory f = null;
903 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
902 >            new CustomTPE(1, 2, 1L, SECONDS,
903 >                          new ArrayBlockingQueue<Runnable>(10),
904 >                          (ThreadFactory) null);
905              shouldThrow();
906          } catch (NullPointerException success) {}
907      }
908  
826
909      /**
910       * Constructor throws if corePoolSize argument is less than zero
911       */
912      public void testConstructor11() {
913          try {
914 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
914 >            new CustomTPE(-1, 1, 1L, SECONDS,
915 >                          new ArrayBlockingQueue<Runnable>(10),
916 >                          new NoOpREHandler());
917              shouldThrow();
918          } catch (IllegalArgumentException success) {}
919      }
# Line 839 | Line 923 | public class ThreadPoolExecutorSubclassT
923       */
924      public void testConstructor12() {
925          try {
926 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
926 >            new CustomTPE(1, -1, 1L, SECONDS,
927 >                          new ArrayBlockingQueue<Runnable>(10),
928 >                          new NoOpREHandler());
929              shouldThrow();
930          } catch (IllegalArgumentException success) {}
931      }
# Line 849 | Line 935 | public class ThreadPoolExecutorSubclassT
935       */
936      public void testConstructor13() {
937          try {
938 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
938 >            new CustomTPE(1, 0, 1L, SECONDS,
939 >                          new ArrayBlockingQueue<Runnable>(10),
940 >                          new NoOpREHandler());
941              shouldThrow();
942          } catch (IllegalArgumentException success) {}
943      }
# Line 859 | Line 947 | public class ThreadPoolExecutorSubclassT
947       */
948      public void testConstructor14() {
949          try {
950 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
950 >            new CustomTPE(1, 2, -1L, SECONDS,
951 >                          new ArrayBlockingQueue<Runnable>(10),
952 >                          new NoOpREHandler());
953              shouldThrow();
954          } catch (IllegalArgumentException success) {}
955      }
# Line 869 | Line 959 | public class ThreadPoolExecutorSubclassT
959       */
960      public void testConstructor15() {
961          try {
962 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
962 >            new CustomTPE(2, 1, 1L, SECONDS,
963 >                          new ArrayBlockingQueue<Runnable>(10),
964 >                          new NoOpREHandler());
965              shouldThrow();
966          } catch (IllegalArgumentException success) {}
967      }
# Line 879 | Line 971 | public class ThreadPoolExecutorSubclassT
971       */
972      public void testConstructorNullPointerException4() {
973          try {
974 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
974 >            new CustomTPE(1, 2, 1L, SECONDS,
975 >                          null,
976 >                          new NoOpREHandler());
977              shouldThrow();
978          } catch (NullPointerException success) {}
979      }
# Line 889 | Line 983 | public class ThreadPoolExecutorSubclassT
983       */
984      public void testConstructorNullPointerException5() {
985          try {
986 <            RejectedExecutionHandler r = null;
987 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
986 >            new CustomTPE(1, 2, 1L, SECONDS,
987 >                          new ArrayBlockingQueue<Runnable>(10),
988 >                          (RejectedExecutionHandler) null);
989              shouldThrow();
990          } catch (NullPointerException success) {}
991      }
992  
898
993      /**
994       * Constructor throws if corePoolSize argument is less than zero
995       */
996      public void testConstructor16() {
997          try {
998 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
998 >            new CustomTPE(-1, 1, 1L, SECONDS,
999 >                          new ArrayBlockingQueue<Runnable>(10),
1000 >                          new SimpleThreadFactory(),
1001 >                          new NoOpREHandler());
1002              shouldThrow();
1003          } catch (IllegalArgumentException success) {}
1004      }
# Line 911 | Line 1008 | public class ThreadPoolExecutorSubclassT
1008       */
1009      public void testConstructor17() {
1010          try {
1011 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1011 >            new CustomTPE(1, -1, 1L, SECONDS,
1012 >                          new ArrayBlockingQueue<Runnable>(10),
1013 >                          new SimpleThreadFactory(),
1014 >                          new NoOpREHandler());
1015              shouldThrow();
1016          } catch (IllegalArgumentException success) {}
1017      }
# Line 921 | Line 1021 | public class ThreadPoolExecutorSubclassT
1021       */
1022      public void testConstructor18() {
1023          try {
1024 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1024 >            new CustomTPE(1, 0, 1L, SECONDS,
1025 >                          new ArrayBlockingQueue<Runnable>(10),
1026 >                          new SimpleThreadFactory(),
1027 >                          new NoOpREHandler());
1028              shouldThrow();
1029          } catch (IllegalArgumentException success) {}
1030      }
# Line 931 | Line 1034 | public class ThreadPoolExecutorSubclassT
1034       */
1035      public void testConstructor19() {
1036          try {
1037 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1037 >            new CustomTPE(1, 2, -1L, SECONDS,
1038 >                          new ArrayBlockingQueue<Runnable>(10),
1039 >                          new SimpleThreadFactory(),
1040 >                          new NoOpREHandler());
1041              shouldThrow();
1042          } catch (IllegalArgumentException success) {}
1043      }
# Line 941 | Line 1047 | public class ThreadPoolExecutorSubclassT
1047       */
1048      public void testConstructor20() {
1049          try {
1050 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1050 >            new CustomTPE(2, 1, 1L, SECONDS,
1051 >                          new ArrayBlockingQueue<Runnable>(10),
1052 >                          new SimpleThreadFactory(),
1053 >                          new NoOpREHandler());
1054              shouldThrow();
1055          } catch (IllegalArgumentException success) {}
1056      }
# Line 951 | Line 1060 | public class ThreadPoolExecutorSubclassT
1060       */
1061      public void testConstructorNullPointerException6() {
1062          try {
1063 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1063 >            new CustomTPE(1, 2, 1L, SECONDS,
1064 >                          null,
1065 >                          new SimpleThreadFactory(),
1066 >                          new NoOpREHandler());
1067              shouldThrow();
1068          } catch (NullPointerException success) {}
1069      }
# Line 961 | Line 1073 | public class ThreadPoolExecutorSubclassT
1073       */
1074      public void testConstructorNullPointerException7() {
1075          try {
1076 <            RejectedExecutionHandler r = null;
1077 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1076 >            new CustomTPE(1, 2, 1L, SECONDS,
1077 >                          new ArrayBlockingQueue<Runnable>(10),
1078 >                          new SimpleThreadFactory(),
1079 >                          (RejectedExecutionHandler) null);
1080              shouldThrow();
1081          } catch (NullPointerException success) {}
1082      }
# Line 972 | Line 1086 | public class ThreadPoolExecutorSubclassT
1086       */
1087      public void testConstructorNullPointerException8() {
1088          try {
1089 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1089 >            new CustomTPE(1, 2, 1L, SECONDS,
1090                            new ArrayBlockingQueue<Runnable>(10),
1091                            (ThreadFactory) null,
1092                            new NoOpREHandler());
# Line 981 | Line 1094 | public class ThreadPoolExecutorSubclassT
1094          } catch (NullPointerException success) {}
1095      }
1096  
984
1097      /**
1098       * execute throws RejectedExecutionException if saturated.
1099       */
# Line 1131 | Line 1243 | public class ThreadPoolExecutorSubclassT
1243          }
1244      }
1245  
1134
1246      /**
1247       * execute using DiscardOldestPolicy drops task on shutdown
1248       */
# Line 1149 | Line 1260 | public class ThreadPoolExecutorSubclassT
1260          }
1261      }
1262  
1152
1263      /**
1264       * execute(null) throws NPE
1265       */
1266      public void testExecuteNull() {
1267 <        ThreadPoolExecutor p = null;
1267 >        ThreadPoolExecutor p =
1268 >            new CustomTPE(1, 2, 1L, SECONDS,
1269 >                          new ArrayBlockingQueue<Runnable>(10));
1270          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1271              p.execute(null);
1272              shouldThrow();
1273          } catch (NullPointerException success) {}
# Line 1214 | Line 1325 | public class ThreadPoolExecutorSubclassT
1325          joinPool(p);
1326      }
1327  
1217
1328      /**
1329       * setKeepAliveTime throws IllegalArgumentException
1330       * when given a negative value
# Line 1239 | Line 1349 | public class ThreadPoolExecutorSubclassT
1349      public void testTerminated() {
1350          CustomTPE p = new CustomTPE();
1351          try { p.shutdown(); } catch (SecurityException ok) { return; }
1352 <        assertTrue(p.terminatedCalled);
1352 >        assertTrue(p.terminatedCalled());
1353          joinPool(p);
1354      }
1355  
# Line 1249 | Line 1359 | public class ThreadPoolExecutorSubclassT
1359      public void testBeforeAfter() throws InterruptedException {
1360          CustomTPE p = new CustomTPE();
1361          try {
1362 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1363 <            p.execute(r);
1364 <            Thread.sleep(SHORT_DELAY_MS);
1365 <            assertTrue(r.done);
1366 <            assertTrue(p.beforeCalled);
1367 <            assertTrue(p.afterCalled);
1362 >            final CountDownLatch done = new CountDownLatch(1);
1363 >            p.execute(new CheckedRunnable() {
1364 >                public void realRun() {
1365 >                    done.countDown();
1366 >                }});
1367 >            await(p.afterCalled);
1368 >            assertEquals(0, done.getCount());
1369 >            assertTrue(p.afterCalled());
1370 >            assertTrue(p.beforeCalled());
1371              try { p.shutdown(); } catch (SecurityException ok) { return; }
1372          } finally {
1373              joinPool(p);
# Line 1303 | Line 1416 | public class ThreadPoolExecutorSubclassT
1416          }
1417      }
1418  
1306
1419      /**
1420       * invokeAny(null) throws NPE
1421       */
# Line 1465 | Line 1577 | public class ThreadPoolExecutorSubclassT
1577          }
1578      }
1579  
1468
1469
1580      /**
1581       * timed invokeAny(null) throws NPE
1582       */
# Line 1653 | Line 1763 | public class ThreadPoolExecutorSubclassT
1763              l.add(new StringTask());
1764              l.add(new StringTask());
1765              List<Future<String>> futures =
1766 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1766 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1767              assertEquals(2, futures.size());
1768              for (Future<String> future : futures)
1769                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1778 | public class ThreadPoolExecutorSubclassT
1778      public void testTimedInvokeAll6() throws Exception {
1779          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1780          try {
1781 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1782 <            l.add(new StringTask());
1783 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1784 <            l.add(new StringTask());
1785 <            List<Future<String>> futures =
1786 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1787 <            assertEquals(3, futures.size());
1788 <            Iterator<Future<String>> it = futures.iterator();
1789 <            Future<String> f1 = it.next();
1790 <            Future<String> f2 = it.next();
1791 <            Future<String> f3 = it.next();
1792 <            assertTrue(f1.isDone());
1793 <            assertTrue(f2.isDone());
1794 <            assertTrue(f3.isDone());
1795 <            assertFalse(f1.isCancelled());
1796 <            assertTrue(f2.isCancelled());
1781 >            for (long timeout = timeoutMillis();;) {
1782 >                List<Callable<String>> tasks = new ArrayList<>();
1783 >                tasks.add(new StringTask("0"));
1784 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1785 >                tasks.add(new StringTask("2"));
1786 >                long startTime = System.nanoTime();
1787 >                List<Future<String>> futures =
1788 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1789 >                assertEquals(tasks.size(), futures.size());
1790 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1791 >                for (Future future : futures)
1792 >                    assertTrue(future.isDone());
1793 >                assertTrue(futures.get(1).isCancelled());
1794 >                try {
1795 >                    assertEquals("0", futures.get(0).get());
1796 >                    assertEquals("2", futures.get(2).get());
1797 >                    break;
1798 >                } catch (CancellationException retryWithLongerTimeout) {
1799 >                    timeout *= 2;
1800 >                    if (timeout >= LONG_DELAY_MS / 2)
1801 >                        fail("expected exactly one task to be cancelled");
1802 >                }
1803 >            }
1804          } finally {
1805              joinPool(e);
1806          }
# Line 1726 | Line 1843 | public class ThreadPoolExecutorSubclassT
1843       * allowCoreThreadTimeOut(true) causes idle threads to time out
1844       */
1845      public void testAllowCoreThreadTimeOut_true() throws Exception {
1846 +        long keepAliveTime = timeoutMillis();
1847          final ThreadPoolExecutor p =
1848              new CustomTPE(2, 10,
1849 <                          SHORT_DELAY_MS, MILLISECONDS,
1849 >                          keepAliveTime, MILLISECONDS,
1850                            new ArrayBlockingQueue<Runnable>(10));
1851          final CountDownLatch threadStarted = new CountDownLatch(1);
1852          try {
1853              p.allowCoreThreadTimeOut(true);
1854              p.execute(new CheckedRunnable() {
1855 <                public void realRun() throws InterruptedException {
1855 >                public void realRun() {
1856                      threadStarted.countDown();
1857                      assertEquals(1, p.getPoolSize());
1858                  }});
1859 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1860 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1861 <                if (p.getPoolSize() == 0)
1862 <                    break;
1863 <                Thread.sleep(10);
1864 <            }
1859 >            await(threadStarted);
1860 >            delay(keepAliveTime);
1861 >            long startTime = System.nanoTime();
1862 >            while (p.getPoolSize() > 0
1863 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1864 >                Thread.yield();
1865 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1866              assertEquals(0, p.getPoolSize());
1867          } finally {
1868              joinPool(p);
# Line 1754 | Line 1873 | public class ThreadPoolExecutorSubclassT
1873       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1874       */
1875      public void testAllowCoreThreadTimeOut_false() throws Exception {
1876 +        long keepAliveTime = timeoutMillis();
1877          final ThreadPoolExecutor p =
1878              new CustomTPE(2, 10,
1879 <                          SHORT_DELAY_MS, MILLISECONDS,
1879 >                          keepAliveTime, MILLISECONDS,
1880                            new ArrayBlockingQueue<Runnable>(10));
1881          final CountDownLatch threadStarted = new CountDownLatch(1);
1882          try {
# Line 1766 | Line 1886 | public class ThreadPoolExecutorSubclassT
1886                      threadStarted.countDown();
1887                      assertTrue(p.getPoolSize() >= 1);
1888                  }});
1889 <            Thread.sleep(SMALL_DELAY_MS);
1889 >            delay(2 * keepAliveTime);
1890              assertTrue(p.getPoolSize() >= 1);
1891          } finally {
1892              joinPool(p);
1893          }
1894      }
1895  
1896 +    /**
1897 +     * get(cancelled task) throws CancellationException
1898 +     * (in part, a test of CustomTPE itself)
1899 +     */
1900 +    public void testGet_cancelled() throws Exception {
1901 +        final ExecutorService e =
1902 +            new CustomTPE(1, 1,
1903 +                          LONG_DELAY_MS, MILLISECONDS,
1904 +                          new LinkedBlockingQueue<Runnable>());
1905 +        try {
1906 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1907 +            final CountDownLatch done = new CountDownLatch(1);
1908 +            final List<Future<?>> futures = new ArrayList<>();
1909 +            for (int i = 0; i < 2; i++) {
1910 +                Runnable r = new CheckedRunnable() { public void realRun()
1911 +                                                         throws Throwable {
1912 +                    blockerStarted.countDown();
1913 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1914 +                }};
1915 +                futures.add(e.submit(r));
1916 +            }
1917 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1918 +            for (Future<?> future : futures) future.cancel(false);
1919 +            for (Future<?> future : futures) {
1920 +                try {
1921 +                    future.get();
1922 +                    shouldThrow();
1923 +                } catch (CancellationException success) {}
1924 +                try {
1925 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1926 +                    shouldThrow();
1927 +                } catch (CancellationException success) {}
1928 +                assertTrue(future.isCancelled());
1929 +                assertTrue(future.isDone());
1930 +            }
1931 +            done.countDown();
1932 +        } finally {
1933 +            joinPool(e);
1934 +        }
1935 +    }
1936 +
1937   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines