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.27 by jsr166, Sat May 7 19:34:51 2011 UTC vs.
Revision 1.54 by jsr166, Sun Oct 4 01:27:32 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 274 | Line 325 | public class ThreadPoolExecutorSubclassT
325              new CustomTPE(2, 2,
326                            LONG_DELAY_MS, MILLISECONDS,
327                            new ArrayBlockingQueue<Runnable>(10));
328 <        final CountDownLatch threadStarted = new CountDownLatch(1);
329 <        final CountDownLatch threadProceed = new CountDownLatch(1);
330 <        final CountDownLatch threadDone = new CountDownLatch(1);
331 <        try {
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332              assertEquals(0, p.getCompletedTaskCount());
333              p.execute(new CheckedRunnable() {
334                  public void realRun() throws InterruptedException {
# 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 <            delay(SHORT_DELAY_MS);
345 <            assertEquals(1, p.getCompletedTaskCount());
346 <        } finally {
347 <            joinPool(p);
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          }
351      }
352  
# Line 311 | Line 364 | public class ThreadPoolExecutorSubclassT
364       */
365      public void testGetKeepAliveTime() {
366          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
367 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
367 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
368          joinPool(p);
369      }
370  
318
371      /**
372       * getThreadFactory returns factory in constructor if not set
373       */
# Line 337 | Line 389 | public class ThreadPoolExecutorSubclassT
389          joinPool(p);
390      }
391  
340
392      /**
393       * setThreadFactory(null) throws NPE
394       */
# Line 374 | Line 425 | public class ThreadPoolExecutorSubclassT
425          joinPool(p);
426      }
427  
377
428      /**
429       * setRejectedExecutionHandler(null) throws NPE
430       */
# Line 389 | Line 439 | public class ThreadPoolExecutorSubclassT
439          }
440      }
441  
392
442      /**
443       * getLargestPoolSize increases, but doesn't overestimate, when
444       * multiple threads active
# Line 484 | Line 533 | public class ThreadPoolExecutorSubclassT
533      }
534  
535      /**
536 <     * isShutDown is false before shutdown, true after
536 >     * isShutdown is false before shutdown, true after
537       */
538      public void testIsShutdown() {
539  
# Line 495 | Line 544 | public class ThreadPoolExecutorSubclassT
544          joinPool(p);
545      }
546  
498
547      /**
548       * isTerminated is false before termination, true after
549       */
# Line 669 | Line 717 | public class ThreadPoolExecutorSubclassT
717      }
718  
719      /**
720 <     * shutDownNow returns a list containing tasks that were not run
720 >     * shutdownNow returns a list containing tasks that were not run,
721 >     * and those tasks are drained from the queue
722       */
723 <    public void testShutdownNow() {
724 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
725 <        List l;
726 <        try {
727 <            for (int i = 0; i < 5; i++)
728 <                p.execute(new MediumPossiblyInterruptedRunnable());
729 <        }
730 <        finally {
723 >    public void testShutdownNow() throws InterruptedException {
724 >        final int poolSize = 2;
725 >        final int count = 5;
726 >        final AtomicInteger ran = new AtomicInteger(0);
727 >        ThreadPoolExecutor p =
728 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
729 >                          new ArrayBlockingQueue<Runnable>(10));
730 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
731 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
732 >            threadsStarted.countDown();
733              try {
734 <                l = p.shutdownNow();
735 <            } catch (SecurityException ok) { return; }
734 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
735 >            } catch (InterruptedException success) {}
736 >            ran.getAndIncrement();
737 >        }};
738 >        for (int i = 0; i < count; i++)
739 >            p.execute(waiter);
740 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
741 >        assertEquals(poolSize, p.getActiveCount());
742 >        assertEquals(0, p.getCompletedTaskCount());
743 >        final List<Runnable> queuedTasks;
744 >        try {
745 >            queuedTasks = p.shutdownNow();
746 >        } catch (SecurityException ok) {
747 >            return; // Allowed in case test doesn't have privs
748          }
749          assertTrue(p.isShutdown());
750 <        assertTrue(l.size() <= 4);
750 >        assertTrue(p.getQueue().isEmpty());
751 >        assertEquals(count - poolSize, queuedTasks.size());
752 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
753 >        assertTrue(p.isTerminated());
754 >        assertEquals(poolSize, ran.get());
755 >        assertEquals(poolSize, p.getCompletedTaskCount());
756      }
757  
758      // Exception Tests
759  
692
760      /**
761       * Constructor throws if corePoolSize argument is less than zero
762       */
763      public void testConstructor1() {
764          try {
765 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
765 >            new CustomTPE(-1, 1, 1L, SECONDS,
766 >                          new ArrayBlockingQueue<Runnable>(10));
767              shouldThrow();
768          } catch (IllegalArgumentException success) {}
769      }
# Line 705 | Line 773 | public class ThreadPoolExecutorSubclassT
773       */
774      public void testConstructor2() {
775          try {
776 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
776 >            new CustomTPE(1, -1, 1L, SECONDS,
777 >                          new ArrayBlockingQueue<Runnable>(10));
778              shouldThrow();
779          } catch (IllegalArgumentException success) {}
780      }
# Line 715 | Line 784 | public class ThreadPoolExecutorSubclassT
784       */
785      public void testConstructor3() {
786          try {
787 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
787 >            new CustomTPE(1, 0, 1L, SECONDS,
788 >                          new ArrayBlockingQueue<Runnable>(10));
789              shouldThrow();
790          } catch (IllegalArgumentException success) {}
791      }
# Line 725 | Line 795 | public class ThreadPoolExecutorSubclassT
795       */
796      public void testConstructor4() {
797          try {
798 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
798 >            new CustomTPE(1, 2, -1L, SECONDS,
799 >                          new ArrayBlockingQueue<Runnable>(10));
800              shouldThrow();
801          } catch (IllegalArgumentException success) {}
802      }
# Line 735 | Line 806 | public class ThreadPoolExecutorSubclassT
806       */
807      public void testConstructor5() {
808          try {
809 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
809 >            new CustomTPE(2, 1, 1L, SECONDS,
810 >                          new ArrayBlockingQueue<Runnable>(10));
811              shouldThrow();
812          } catch (IllegalArgumentException success) {}
813      }
# Line 745 | Line 817 | public class ThreadPoolExecutorSubclassT
817       */
818      public void testConstructorNullPointerException() {
819          try {
820 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
820 >            new CustomTPE(1, 2, 1L, SECONDS, null);
821              shouldThrow();
822          } catch (NullPointerException success) {}
823      }
824  
753
754
825      /**
826       * Constructor throws if corePoolSize argument is less than zero
827       */
828      public void testConstructor6() {
829          try {
830 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
830 >            new CustomTPE(-1, 1, 1L, SECONDS,
831 >                          new ArrayBlockingQueue<Runnable>(10),
832 >                          new SimpleThreadFactory());
833              shouldThrow();
834          } catch (IllegalArgumentException success) {}
835      }
# Line 767 | Line 839 | public class ThreadPoolExecutorSubclassT
839       */
840      public void testConstructor7() {
841          try {
842 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
842 >            new CustomTPE(1,-1, 1L, SECONDS,
843 >                          new ArrayBlockingQueue<Runnable>(10),
844 >                          new SimpleThreadFactory());
845              shouldThrow();
846          } catch (IllegalArgumentException success) {}
847      }
# Line 777 | Line 851 | public class ThreadPoolExecutorSubclassT
851       */
852      public void testConstructor8() {
853          try {
854 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
854 >            new CustomTPE(1, 0, 1L, SECONDS,
855 >                          new ArrayBlockingQueue<Runnable>(10),
856 >                          new SimpleThreadFactory());
857              shouldThrow();
858          } catch (IllegalArgumentException success) {}
859      }
# Line 787 | Line 863 | public class ThreadPoolExecutorSubclassT
863       */
864      public void testConstructor9() {
865          try {
866 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
866 >            new CustomTPE(1, 2, -1L, SECONDS,
867 >                          new ArrayBlockingQueue<Runnable>(10),
868 >                          new SimpleThreadFactory());
869              shouldThrow();
870          } catch (IllegalArgumentException success) {}
871      }
# Line 797 | Line 875 | public class ThreadPoolExecutorSubclassT
875       */
876      public void testConstructor10() {
877          try {
878 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
878 >            new CustomTPE(2, 1, 1L, SECONDS,
879 >                          new ArrayBlockingQueue<Runnable>(10),
880 >                          new SimpleThreadFactory());
881              shouldThrow();
882          } catch (IllegalArgumentException success) {}
883      }
# Line 807 | Line 887 | public class ThreadPoolExecutorSubclassT
887       */
888      public void testConstructorNullPointerException2() {
889          try {
890 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
890 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
891              shouldThrow();
892          } catch (NullPointerException success) {}
893      }
# Line 817 | Line 897 | public class ThreadPoolExecutorSubclassT
897       */
898      public void testConstructorNullPointerException3() {
899          try {
900 <            ThreadFactory f = null;
901 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
900 >            new CustomTPE(1, 2, 1L, SECONDS,
901 >                          new ArrayBlockingQueue<Runnable>(10),
902 >                          (ThreadFactory) null);
903              shouldThrow();
904          } catch (NullPointerException success) {}
905      }
906  
826
907      /**
908       * Constructor throws if corePoolSize argument is less than zero
909       */
910      public void testConstructor11() {
911          try {
912 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
912 >            new CustomTPE(-1, 1, 1L, SECONDS,
913 >                          new ArrayBlockingQueue<Runnable>(10),
914 >                          new NoOpREHandler());
915              shouldThrow();
916          } catch (IllegalArgumentException success) {}
917      }
# Line 839 | Line 921 | public class ThreadPoolExecutorSubclassT
921       */
922      public void testConstructor12() {
923          try {
924 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
924 >            new CustomTPE(1, -1, 1L, SECONDS,
925 >                          new ArrayBlockingQueue<Runnable>(10),
926 >                          new NoOpREHandler());
927              shouldThrow();
928          } catch (IllegalArgumentException success) {}
929      }
# Line 849 | Line 933 | public class ThreadPoolExecutorSubclassT
933       */
934      public void testConstructor13() {
935          try {
936 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
936 >            new CustomTPE(1, 0, 1L, SECONDS,
937 >                          new ArrayBlockingQueue<Runnable>(10),
938 >                          new NoOpREHandler());
939              shouldThrow();
940          } catch (IllegalArgumentException success) {}
941      }
# Line 859 | Line 945 | public class ThreadPoolExecutorSubclassT
945       */
946      public void testConstructor14() {
947          try {
948 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
948 >            new CustomTPE(1, 2, -1L, SECONDS,
949 >                          new ArrayBlockingQueue<Runnable>(10),
950 >                          new NoOpREHandler());
951              shouldThrow();
952          } catch (IllegalArgumentException success) {}
953      }
# Line 869 | Line 957 | public class ThreadPoolExecutorSubclassT
957       */
958      public void testConstructor15() {
959          try {
960 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
960 >            new CustomTPE(2, 1, 1L, SECONDS,
961 >                          new ArrayBlockingQueue<Runnable>(10),
962 >                          new NoOpREHandler());
963              shouldThrow();
964          } catch (IllegalArgumentException success) {}
965      }
# Line 879 | Line 969 | public class ThreadPoolExecutorSubclassT
969       */
970      public void testConstructorNullPointerException4() {
971          try {
972 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
972 >            new CustomTPE(1, 2, 1L, SECONDS,
973 >                          null,
974 >                          new NoOpREHandler());
975              shouldThrow();
976          } catch (NullPointerException success) {}
977      }
# Line 889 | Line 981 | public class ThreadPoolExecutorSubclassT
981       */
982      public void testConstructorNullPointerException5() {
983          try {
984 <            RejectedExecutionHandler r = null;
985 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
984 >            new CustomTPE(1, 2, 1L, SECONDS,
985 >                          new ArrayBlockingQueue<Runnable>(10),
986 >                          (RejectedExecutionHandler) null);
987              shouldThrow();
988          } catch (NullPointerException success) {}
989      }
990  
898
991      /**
992       * Constructor throws if corePoolSize argument is less than zero
993       */
994      public void testConstructor16() {
995          try {
996 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
996 >            new CustomTPE(-1, 1, 1L, SECONDS,
997 >                          new ArrayBlockingQueue<Runnable>(10),
998 >                          new SimpleThreadFactory(),
999 >                          new NoOpREHandler());
1000              shouldThrow();
1001          } catch (IllegalArgumentException success) {}
1002      }
# Line 911 | Line 1006 | public class ThreadPoolExecutorSubclassT
1006       */
1007      public void testConstructor17() {
1008          try {
1009 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1009 >            new CustomTPE(1, -1, 1L, SECONDS,
1010 >                          new ArrayBlockingQueue<Runnable>(10),
1011 >                          new SimpleThreadFactory(),
1012 >                          new NoOpREHandler());
1013              shouldThrow();
1014          } catch (IllegalArgumentException success) {}
1015      }
# Line 921 | Line 1019 | public class ThreadPoolExecutorSubclassT
1019       */
1020      public void testConstructor18() {
1021          try {
1022 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1022 >            new CustomTPE(1, 0, 1L, SECONDS,
1023 >                          new ArrayBlockingQueue<Runnable>(10),
1024 >                          new SimpleThreadFactory(),
1025 >                          new NoOpREHandler());
1026              shouldThrow();
1027          } catch (IllegalArgumentException success) {}
1028      }
# Line 931 | Line 1032 | public class ThreadPoolExecutorSubclassT
1032       */
1033      public void testConstructor19() {
1034          try {
1035 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1035 >            new CustomTPE(1, 2, -1L, SECONDS,
1036 >                          new ArrayBlockingQueue<Runnable>(10),
1037 >                          new SimpleThreadFactory(),
1038 >                          new NoOpREHandler());
1039              shouldThrow();
1040          } catch (IllegalArgumentException success) {}
1041      }
# Line 941 | Line 1045 | public class ThreadPoolExecutorSubclassT
1045       */
1046      public void testConstructor20() {
1047          try {
1048 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1048 >            new CustomTPE(2, 1, 1L, SECONDS,
1049 >                          new ArrayBlockingQueue<Runnable>(10),
1050 >                          new SimpleThreadFactory(),
1051 >                          new NoOpREHandler());
1052              shouldThrow();
1053          } catch (IllegalArgumentException success) {}
1054      }
# Line 951 | Line 1058 | public class ThreadPoolExecutorSubclassT
1058       */
1059      public void testConstructorNullPointerException6() {
1060          try {
1061 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1061 >            new CustomTPE(1, 2, 1L, SECONDS,
1062 >                          null,
1063 >                          new SimpleThreadFactory(),
1064 >                          new NoOpREHandler());
1065              shouldThrow();
1066          } catch (NullPointerException success) {}
1067      }
# Line 961 | Line 1071 | public class ThreadPoolExecutorSubclassT
1071       */
1072      public void testConstructorNullPointerException7() {
1073          try {
1074 <            RejectedExecutionHandler r = null;
1075 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1074 >            new CustomTPE(1, 2, 1L, SECONDS,
1075 >                          new ArrayBlockingQueue<Runnable>(10),
1076 >                          new SimpleThreadFactory(),
1077 >                          (RejectedExecutionHandler) null);
1078              shouldThrow();
1079          } catch (NullPointerException success) {}
1080      }
# Line 972 | Line 1084 | public class ThreadPoolExecutorSubclassT
1084       */
1085      public void testConstructorNullPointerException8() {
1086          try {
1087 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1087 >            new CustomTPE(1, 2, 1L, SECONDS,
1088                            new ArrayBlockingQueue<Runnable>(10),
1089                            (ThreadFactory) null,
1090                            new NoOpREHandler());
# Line 981 | Line 1092 | public class ThreadPoolExecutorSubclassT
1092          } catch (NullPointerException success) {}
1093      }
1094  
984
1095      /**
1096       * execute throws RejectedExecutionException if saturated.
1097       */
# Line 1131 | Line 1241 | public class ThreadPoolExecutorSubclassT
1241          }
1242      }
1243  
1134
1244      /**
1245       * execute using DiscardOldestPolicy drops task on shutdown
1246       */
# Line 1149 | Line 1258 | public class ThreadPoolExecutorSubclassT
1258          }
1259      }
1260  
1152
1261      /**
1262       * execute(null) throws NPE
1263       */
1264      public void testExecuteNull() {
1265 <        ThreadPoolExecutor p = null;
1265 >        ThreadPoolExecutor p =
1266 >            new CustomTPE(1, 2, 1L, SECONDS,
1267 >                          new ArrayBlockingQueue<Runnable>(10));
1268          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1269              p.execute(null);
1270              shouldThrow();
1271          } catch (NullPointerException success) {}
# Line 1214 | Line 1323 | public class ThreadPoolExecutorSubclassT
1323          joinPool(p);
1324      }
1325  
1217
1326      /**
1327       * setKeepAliveTime throws IllegalArgumentException
1328       * when given a negative value
# Line 1239 | Line 1347 | public class ThreadPoolExecutorSubclassT
1347      public void testTerminated() {
1348          CustomTPE p = new CustomTPE();
1349          try { p.shutdown(); } catch (SecurityException ok) { return; }
1350 <        assertTrue(p.terminatedCalled);
1350 >        assertTrue(p.terminatedCalled());
1351          joinPool(p);
1352      }
1353  
# Line 1249 | Line 1357 | public class ThreadPoolExecutorSubclassT
1357      public void testBeforeAfter() throws InterruptedException {
1358          CustomTPE p = new CustomTPE();
1359          try {
1360 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1361 <            p.execute(r);
1362 <            delay(SHORT_DELAY_MS);
1363 <            assertTrue(r.done);
1364 <            assertTrue(p.beforeCalled);
1365 <            assertTrue(p.afterCalled);
1360 >            final CountDownLatch done = new CountDownLatch(1);
1361 >            p.execute(new CheckedRunnable() {
1362 >                public void realRun() {
1363 >                    done.countDown();
1364 >                }});
1365 >            await(p.afterCalled);
1366 >            assertEquals(0, done.getCount());
1367 >            assertTrue(p.afterCalled());
1368 >            assertTrue(p.beforeCalled());
1369              try { p.shutdown(); } catch (SecurityException ok) { return; }
1370          } finally {
1371              joinPool(p);
# Line 1303 | Line 1414 | public class ThreadPoolExecutorSubclassT
1414          }
1415      }
1416  
1306
1417      /**
1418       * invokeAny(null) throws NPE
1419       */
# Line 1465 | Line 1575 | public class ThreadPoolExecutorSubclassT
1575          }
1576      }
1577  
1468
1469
1578      /**
1579       * timed invokeAny(null) throws NPE
1580       */
# Line 1653 | Line 1761 | public class ThreadPoolExecutorSubclassT
1761              l.add(new StringTask());
1762              l.add(new StringTask());
1763              List<Future<String>> futures =
1764 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1764 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1765              assertEquals(2, futures.size());
1766              for (Future<String> future : futures)
1767                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1776 | public class ThreadPoolExecutorSubclassT
1776      public void testTimedInvokeAll6() throws Exception {
1777          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1778          try {
1779 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1780 <            l.add(new StringTask());
1781 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1782 <            l.add(new StringTask());
1783 <            List<Future<String>> futures =
1784 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1785 <            assertEquals(3, futures.size());
1786 <            Iterator<Future<String>> it = futures.iterator();
1787 <            Future<String> f1 = it.next();
1788 <            Future<String> f2 = it.next();
1789 <            Future<String> f3 = it.next();
1790 <            assertTrue(f1.isDone());
1791 <            assertTrue(f2.isDone());
1792 <            assertTrue(f3.isDone());
1793 <            assertFalse(f1.isCancelled());
1794 <            assertTrue(f2.isCancelled());
1779 >            for (long timeout = timeoutMillis();;) {
1780 >                List<Callable<String>> tasks = new ArrayList<>();
1781 >                tasks.add(new StringTask("0"));
1782 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1783 >                tasks.add(new StringTask("2"));
1784 >                long startTime = System.nanoTime();
1785 >                List<Future<String>> futures =
1786 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1787 >                assertEquals(tasks.size(), futures.size());
1788 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1789 >                for (Future future : futures)
1790 >                    assertTrue(future.isDone());
1791 >                assertTrue(futures.get(1).isCancelled());
1792 >                try {
1793 >                    assertEquals("0", futures.get(0).get());
1794 >                    assertEquals("2", futures.get(2).get());
1795 >                    break;
1796 >                } catch (CancellationException retryWithLongerTimeout) {
1797 >                    timeout *= 2;
1798 >                    if (timeout >= LONG_DELAY_MS / 2)
1799 >                        fail("expected exactly one task to be cancelled");
1800 >                }
1801 >            }
1802          } finally {
1803              joinPool(e);
1804          }
# Line 1726 | Line 1841 | public class ThreadPoolExecutorSubclassT
1841       * allowCoreThreadTimeOut(true) causes idle threads to time out
1842       */
1843      public void testAllowCoreThreadTimeOut_true() throws Exception {
1844 +        long keepAliveTime = timeoutMillis();
1845          final ThreadPoolExecutor p =
1846              new CustomTPE(2, 10,
1847 <                          SHORT_DELAY_MS, MILLISECONDS,
1847 >                          keepAliveTime, MILLISECONDS,
1848                            new ArrayBlockingQueue<Runnable>(10));
1849          final CountDownLatch threadStarted = new CountDownLatch(1);
1850          try {
1851              p.allowCoreThreadTimeOut(true);
1852              p.execute(new CheckedRunnable() {
1853 <                public void realRun() throws InterruptedException {
1853 >                public void realRun() {
1854                      threadStarted.countDown();
1855                      assertEquals(1, p.getPoolSize());
1856                  }});
1857 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1858 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1859 <                if (p.getPoolSize() == 0)
1860 <                    break;
1861 <                delay(10);
1862 <            }
1857 >            await(threadStarted);
1858 >            delay(keepAliveTime);
1859 >            long startTime = System.nanoTime();
1860 >            while (p.getPoolSize() > 0
1861 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1862 >                Thread.yield();
1863 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1864              assertEquals(0, p.getPoolSize());
1865          } finally {
1866              joinPool(p);
# Line 1754 | Line 1871 | public class ThreadPoolExecutorSubclassT
1871       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1872       */
1873      public void testAllowCoreThreadTimeOut_false() throws Exception {
1874 +        long keepAliveTime = timeoutMillis();
1875          final ThreadPoolExecutor p =
1876              new CustomTPE(2, 10,
1877 <                          SHORT_DELAY_MS, MILLISECONDS,
1877 >                          keepAliveTime, MILLISECONDS,
1878                            new ArrayBlockingQueue<Runnable>(10));
1879          final CountDownLatch threadStarted = new CountDownLatch(1);
1880          try {
# Line 1766 | Line 1884 | public class ThreadPoolExecutorSubclassT
1884                      threadStarted.countDown();
1885                      assertTrue(p.getPoolSize() >= 1);
1886                  }});
1887 <            delay(SMALL_DELAY_MS);
1887 >            delay(2 * keepAliveTime);
1888              assertTrue(p.getPoolSize() >= 1);
1889          } finally {
1890              joinPool(p);
1891          }
1892      }
1893  
1894 +    /**
1895 +     * get(cancelled task) throws CancellationException
1896 +     * (in part, a test of CustomTPE itself)
1897 +     */
1898 +    public void testGet_cancelled() throws Exception {
1899 +        final ExecutorService e =
1900 +            new CustomTPE(1, 1,
1901 +                          LONG_DELAY_MS, MILLISECONDS,
1902 +                          new LinkedBlockingQueue<Runnable>());
1903 +        try {
1904 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1905 +            final CountDownLatch done = new CountDownLatch(1);
1906 +            final List<Future<?>> futures = new ArrayList<>();
1907 +            for (int i = 0; i < 2; i++) {
1908 +                Runnable r = new CheckedRunnable() { public void realRun()
1909 +                                                         throws Throwable {
1910 +                    blockerStarted.countDown();
1911 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1912 +                }};
1913 +                futures.add(e.submit(r));
1914 +            }
1915 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1916 +            for (Future<?> future : futures) future.cancel(false);
1917 +            for (Future<?> future : futures) {
1918 +                try {
1919 +                    future.get();
1920 +                    shouldThrow();
1921 +                } catch (CancellationException success) {}
1922 +                try {
1923 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1924 +                    shouldThrow();
1925 +                } catch (CancellationException success) {}
1926 +                assertTrue(future.isCancelled());
1927 +                assertTrue(future.isDone());
1928 +            }
1929 +            done.countDown();
1930 +        } finally {
1931 +            joinPool(e);
1932 +        }
1933 +    }
1934 +
1935   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines