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.28 by jsr166, Sat May 7 19:49:37 2011 UTC vs.
Revision 1.58 by jsr166, Sun Oct 4 01:52:43 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 301 | Line 354 | public class ThreadPoolExecutorSubclassT
354       * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p.getCorePoolSize());
359 <        joinPool(p);
357 >        ThreadPoolExecutor p =
358 >            new CustomTPE(1, 1,
359 >                          LONG_DELAY_MS, MILLISECONDS,
360 >                          new ArrayBlockingQueue<Runnable>(10));
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
367       * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
372 <        joinPool(p);
370 >        ThreadPoolExecutor p =
371 >            new CustomTPE(2, 2,
372 >                          1000, MILLISECONDS,
373 >                          new ArrayBlockingQueue<Runnable>(10));
374 >        try (PoolCleaner cleaner = cleaner(p)) {
375 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
376 >        }
377      }
378  
318
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        ThreadPoolExecutor p =
385 >            new CustomTPE(1, 2,
386 >                          LONG_DELAY_MS, MILLISECONDS,
387 >                          new ArrayBlockingQueue<Runnable>(10),
388 >                          threadFactory,
389 >                          new NoOpREHandler());
390 >        try (PoolCleaner cleaner = cleaner(p)) {
391 >            assertSame(threadFactory, p.getThreadFactory());
392 >        }
393      }
394  
395      /**
396       * setThreadFactory sets the thread factory returned by getThreadFactory
397       */
398      public void testSetThreadFactory() {
399 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        ThreadFactory tf = new SimpleThreadFactory();
401 <        p.setThreadFactory(tf);
402 <        assertSame(tf, p.getThreadFactory());
403 <        joinPool(p);
399 >        ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
340
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415 <        try {
416 <            p.setThreadFactory(null);
417 <            shouldThrow();
418 <        } catch (NullPointerException success) {
419 <        } finally {
420 <            joinPool(p);
414 >        ThreadPoolExecutor p =
415 >            new CustomTPE(1, 2,
416 >                          LONG_DELAY_MS, MILLISECONDS,
417 >                          new ArrayBlockingQueue<Runnable>(10));
418 >        try (PoolCleaner cleaner = cleaner(p)) {
419 >            try {
420 >                p.setThreadFactory(null);
421 >                shouldThrow();
422 >            } catch (NullPointerException success) {}
423          }
424      }
425  
# Line 374 | Line 445 | public class ThreadPoolExecutorSubclassT
445          joinPool(p);
446      }
447  
377
448      /**
449       * setRejectedExecutionHandler(null) throws NPE
450       */
# Line 389 | Line 459 | public class ThreadPoolExecutorSubclassT
459          }
460      }
461  
392
462      /**
463       * getLargestPoolSize increases, but doesn't overestimate, when
464       * multiple threads active
# Line 495 | Line 564 | public class ThreadPoolExecutorSubclassT
564          joinPool(p);
565      }
566  
498
567      /**
568       * isTerminated is false before termination, true after
569       */
# Line 669 | Line 737 | public class ThreadPoolExecutorSubclassT
737      }
738  
739      /**
740 <     * shutdownNow returns a list containing tasks that were not run
740 >     * shutdownNow returns a list containing tasks that were not run,
741 >     * and those tasks are drained from the queue
742       */
743 <    public void testShutdownNow() {
744 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
745 <        List l;
746 <        try {
747 <            for (int i = 0; i < 5; i++)
748 <                p.execute(new MediumPossiblyInterruptedRunnable());
749 <        }
750 <        finally {
743 >    public void testShutdownNow() throws InterruptedException {
744 >        final int poolSize = 2;
745 >        final int count = 5;
746 >        final AtomicInteger ran = new AtomicInteger(0);
747 >        ThreadPoolExecutor p =
748 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
749 >                          new ArrayBlockingQueue<Runnable>(10));
750 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
751 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
752 >            threadsStarted.countDown();
753              try {
754 <                l = p.shutdownNow();
755 <            } catch (SecurityException ok) { return; }
754 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
755 >            } catch (InterruptedException success) {}
756 >            ran.getAndIncrement();
757 >        }};
758 >        for (int i = 0; i < count; i++)
759 >            p.execute(waiter);
760 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
761 >        assertEquals(poolSize, p.getActiveCount());
762 >        assertEquals(0, p.getCompletedTaskCount());
763 >        final List<Runnable> queuedTasks;
764 >        try {
765 >            queuedTasks = p.shutdownNow();
766 >        } catch (SecurityException ok) {
767 >            return; // Allowed in case test doesn't have privs
768          }
769          assertTrue(p.isShutdown());
770 <        assertTrue(l.size() <= 4);
770 >        assertTrue(p.getQueue().isEmpty());
771 >        assertEquals(count - poolSize, queuedTasks.size());
772 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
773 >        assertTrue(p.isTerminated());
774 >        assertEquals(poolSize, ran.get());
775 >        assertEquals(poolSize, p.getCompletedTaskCount());
776      }
777  
778      // Exception Tests
779  
692
780      /**
781       * Constructor throws if corePoolSize argument is less than zero
782       */
783      public void testConstructor1() {
784          try {
785 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
785 >            new CustomTPE(-1, 1, 1L, SECONDS,
786 >                          new ArrayBlockingQueue<Runnable>(10));
787              shouldThrow();
788          } catch (IllegalArgumentException success) {}
789      }
# Line 705 | Line 793 | public class ThreadPoolExecutorSubclassT
793       */
794      public void testConstructor2() {
795          try {
796 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
796 >            new CustomTPE(1, -1, 1L, SECONDS,
797 >                          new ArrayBlockingQueue<Runnable>(10));
798              shouldThrow();
799          } catch (IllegalArgumentException success) {}
800      }
# Line 715 | Line 804 | public class ThreadPoolExecutorSubclassT
804       */
805      public void testConstructor3() {
806          try {
807 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
807 >            new CustomTPE(1, 0, 1L, SECONDS,
808 >                          new ArrayBlockingQueue<Runnable>(10));
809              shouldThrow();
810          } catch (IllegalArgumentException success) {}
811      }
# Line 725 | Line 815 | public class ThreadPoolExecutorSubclassT
815       */
816      public void testConstructor4() {
817          try {
818 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
818 >            new CustomTPE(1, 2, -1L, SECONDS,
819 >                          new ArrayBlockingQueue<Runnable>(10));
820              shouldThrow();
821          } catch (IllegalArgumentException success) {}
822      }
# Line 735 | Line 826 | public class ThreadPoolExecutorSubclassT
826       */
827      public void testConstructor5() {
828          try {
829 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
829 >            new CustomTPE(2, 1, 1L, SECONDS,
830 >                          new ArrayBlockingQueue<Runnable>(10));
831              shouldThrow();
832          } catch (IllegalArgumentException success) {}
833      }
# Line 745 | Line 837 | public class ThreadPoolExecutorSubclassT
837       */
838      public void testConstructorNullPointerException() {
839          try {
840 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
840 >            new CustomTPE(1, 2, 1L, SECONDS, null);
841              shouldThrow();
842          } catch (NullPointerException success) {}
843      }
844  
753
754
845      /**
846       * Constructor throws if corePoolSize argument is less than zero
847       */
848      public void testConstructor6() {
849          try {
850 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
850 >            new CustomTPE(-1, 1, 1L, SECONDS,
851 >                          new ArrayBlockingQueue<Runnable>(10),
852 >                          new SimpleThreadFactory());
853              shouldThrow();
854          } catch (IllegalArgumentException success) {}
855      }
# Line 767 | Line 859 | public class ThreadPoolExecutorSubclassT
859       */
860      public void testConstructor7() {
861          try {
862 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
862 >            new CustomTPE(1,-1, 1L, SECONDS,
863 >                          new ArrayBlockingQueue<Runnable>(10),
864 >                          new SimpleThreadFactory());
865              shouldThrow();
866          } catch (IllegalArgumentException success) {}
867      }
# Line 777 | Line 871 | public class ThreadPoolExecutorSubclassT
871       */
872      public void testConstructor8() {
873          try {
874 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
874 >            new CustomTPE(1, 0, 1L, SECONDS,
875 >                          new ArrayBlockingQueue<Runnable>(10),
876 >                          new SimpleThreadFactory());
877              shouldThrow();
878          } catch (IllegalArgumentException success) {}
879      }
# Line 787 | Line 883 | public class ThreadPoolExecutorSubclassT
883       */
884      public void testConstructor9() {
885          try {
886 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
886 >            new CustomTPE(1, 2, -1L, SECONDS,
887 >                          new ArrayBlockingQueue<Runnable>(10),
888 >                          new SimpleThreadFactory());
889              shouldThrow();
890          } catch (IllegalArgumentException success) {}
891      }
# Line 797 | Line 895 | public class ThreadPoolExecutorSubclassT
895       */
896      public void testConstructor10() {
897          try {
898 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
898 >            new CustomTPE(2, 1, 1L, SECONDS,
899 >                          new ArrayBlockingQueue<Runnable>(10),
900 >                          new SimpleThreadFactory());
901              shouldThrow();
902          } catch (IllegalArgumentException success) {}
903      }
# Line 807 | Line 907 | public class ThreadPoolExecutorSubclassT
907       */
908      public void testConstructorNullPointerException2() {
909          try {
910 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
910 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
911              shouldThrow();
912          } catch (NullPointerException success) {}
913      }
# Line 817 | Line 917 | public class ThreadPoolExecutorSubclassT
917       */
918      public void testConstructorNullPointerException3() {
919          try {
920 <            ThreadFactory f = null;
921 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
920 >            new CustomTPE(1, 2, 1L, SECONDS,
921 >                          new ArrayBlockingQueue<Runnable>(10),
922 >                          (ThreadFactory) null);
923              shouldThrow();
924          } catch (NullPointerException success) {}
925      }
926  
826
927      /**
928       * Constructor throws if corePoolSize argument is less than zero
929       */
930      public void testConstructor11() {
931          try {
932 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
932 >            new CustomTPE(-1, 1, 1L, SECONDS,
933 >                          new ArrayBlockingQueue<Runnable>(10),
934 >                          new NoOpREHandler());
935              shouldThrow();
936          } catch (IllegalArgumentException success) {}
937      }
# Line 839 | Line 941 | public class ThreadPoolExecutorSubclassT
941       */
942      public void testConstructor12() {
943          try {
944 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
944 >            new CustomTPE(1, -1, 1L, SECONDS,
945 >                          new ArrayBlockingQueue<Runnable>(10),
946 >                          new NoOpREHandler());
947              shouldThrow();
948          } catch (IllegalArgumentException success) {}
949      }
# Line 849 | Line 953 | public class ThreadPoolExecutorSubclassT
953       */
954      public void testConstructor13() {
955          try {
956 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
956 >            new CustomTPE(1, 0, 1L, SECONDS,
957 >                          new ArrayBlockingQueue<Runnable>(10),
958 >                          new NoOpREHandler());
959              shouldThrow();
960          } catch (IllegalArgumentException success) {}
961      }
# Line 859 | Line 965 | public class ThreadPoolExecutorSubclassT
965       */
966      public void testConstructor14() {
967          try {
968 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
968 >            new CustomTPE(1, 2, -1L, SECONDS,
969 >                          new ArrayBlockingQueue<Runnable>(10),
970 >                          new NoOpREHandler());
971              shouldThrow();
972          } catch (IllegalArgumentException success) {}
973      }
# Line 869 | Line 977 | public class ThreadPoolExecutorSubclassT
977       */
978      public void testConstructor15() {
979          try {
980 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
980 >            new CustomTPE(2, 1, 1L, SECONDS,
981 >                          new ArrayBlockingQueue<Runnable>(10),
982 >                          new NoOpREHandler());
983              shouldThrow();
984          } catch (IllegalArgumentException success) {}
985      }
# Line 879 | Line 989 | public class ThreadPoolExecutorSubclassT
989       */
990      public void testConstructorNullPointerException4() {
991          try {
992 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
992 >            new CustomTPE(1, 2, 1L, SECONDS,
993 >                          null,
994 >                          new NoOpREHandler());
995              shouldThrow();
996          } catch (NullPointerException success) {}
997      }
# Line 889 | Line 1001 | public class ThreadPoolExecutorSubclassT
1001       */
1002      public void testConstructorNullPointerException5() {
1003          try {
1004 <            RejectedExecutionHandler r = null;
1005 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1004 >            new CustomTPE(1, 2, 1L, SECONDS,
1005 >                          new ArrayBlockingQueue<Runnable>(10),
1006 >                          (RejectedExecutionHandler) null);
1007              shouldThrow();
1008          } catch (NullPointerException success) {}
1009      }
1010  
898
1011      /**
1012       * Constructor throws if corePoolSize argument is less than zero
1013       */
1014      public void testConstructor16() {
1015          try {
1016 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1016 >            new CustomTPE(-1, 1, 1L, SECONDS,
1017 >                          new ArrayBlockingQueue<Runnable>(10),
1018 >                          new SimpleThreadFactory(),
1019 >                          new NoOpREHandler());
1020              shouldThrow();
1021          } catch (IllegalArgumentException success) {}
1022      }
# Line 911 | Line 1026 | public class ThreadPoolExecutorSubclassT
1026       */
1027      public void testConstructor17() {
1028          try {
1029 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1029 >            new CustomTPE(1, -1, 1L, SECONDS,
1030 >                          new ArrayBlockingQueue<Runnable>(10),
1031 >                          new SimpleThreadFactory(),
1032 >                          new NoOpREHandler());
1033              shouldThrow();
1034          } catch (IllegalArgumentException success) {}
1035      }
# Line 921 | Line 1039 | public class ThreadPoolExecutorSubclassT
1039       */
1040      public void testConstructor18() {
1041          try {
1042 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1042 >            new CustomTPE(1, 0, 1L, SECONDS,
1043 >                          new ArrayBlockingQueue<Runnable>(10),
1044 >                          new SimpleThreadFactory(),
1045 >                          new NoOpREHandler());
1046              shouldThrow();
1047          } catch (IllegalArgumentException success) {}
1048      }
# Line 931 | Line 1052 | public class ThreadPoolExecutorSubclassT
1052       */
1053      public void testConstructor19() {
1054          try {
1055 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1055 >            new CustomTPE(1, 2, -1L, SECONDS,
1056 >                          new ArrayBlockingQueue<Runnable>(10),
1057 >                          new SimpleThreadFactory(),
1058 >                          new NoOpREHandler());
1059              shouldThrow();
1060          } catch (IllegalArgumentException success) {}
1061      }
# Line 941 | Line 1065 | public class ThreadPoolExecutorSubclassT
1065       */
1066      public void testConstructor20() {
1067          try {
1068 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1068 >            new CustomTPE(2, 1, 1L, SECONDS,
1069 >                          new ArrayBlockingQueue<Runnable>(10),
1070 >                          new SimpleThreadFactory(),
1071 >                          new NoOpREHandler());
1072              shouldThrow();
1073          } catch (IllegalArgumentException success) {}
1074      }
# Line 951 | Line 1078 | public class ThreadPoolExecutorSubclassT
1078       */
1079      public void testConstructorNullPointerException6() {
1080          try {
1081 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1081 >            new CustomTPE(1, 2, 1L, SECONDS,
1082 >                          null,
1083 >                          new SimpleThreadFactory(),
1084 >                          new NoOpREHandler());
1085              shouldThrow();
1086          } catch (NullPointerException success) {}
1087      }
# Line 961 | Line 1091 | public class ThreadPoolExecutorSubclassT
1091       */
1092      public void testConstructorNullPointerException7() {
1093          try {
1094 <            RejectedExecutionHandler r = null;
1095 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1094 >            new CustomTPE(1, 2, 1L, SECONDS,
1095 >                          new ArrayBlockingQueue<Runnable>(10),
1096 >                          new SimpleThreadFactory(),
1097 >                          (RejectedExecutionHandler) null);
1098              shouldThrow();
1099          } catch (NullPointerException success) {}
1100      }
# Line 972 | Line 1104 | public class ThreadPoolExecutorSubclassT
1104       */
1105      public void testConstructorNullPointerException8() {
1106          try {
1107 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1107 >            new CustomTPE(1, 2, 1L, SECONDS,
1108                            new ArrayBlockingQueue<Runnable>(10),
1109                            (ThreadFactory) null,
1110                            new NoOpREHandler());
# Line 981 | Line 1112 | public class ThreadPoolExecutorSubclassT
1112          } catch (NullPointerException success) {}
1113      }
1114  
984
1115      /**
1116       * execute throws RejectedExecutionException if saturated.
1117       */
# Line 1131 | Line 1261 | public class ThreadPoolExecutorSubclassT
1261          }
1262      }
1263  
1134
1264      /**
1265       * execute using DiscardOldestPolicy drops task on shutdown
1266       */
# Line 1149 | Line 1278 | public class ThreadPoolExecutorSubclassT
1278          }
1279      }
1280  
1152
1281      /**
1282       * execute(null) throws NPE
1283       */
1284      public void testExecuteNull() {
1285 <        ThreadPoolExecutor p = null;
1285 >        ThreadPoolExecutor p =
1286 >            new CustomTPE(1, 2, 1L, SECONDS,
1287 >                          new ArrayBlockingQueue<Runnable>(10));
1288          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1289              p.execute(null);
1290              shouldThrow();
1291          } catch (NullPointerException success) {}
# Line 1214 | Line 1343 | public class ThreadPoolExecutorSubclassT
1343          joinPool(p);
1344      }
1345  
1217
1346      /**
1347       * setKeepAliveTime throws IllegalArgumentException
1348       * when given a negative value
# Line 1239 | Line 1367 | public class ThreadPoolExecutorSubclassT
1367      public void testTerminated() {
1368          CustomTPE p = new CustomTPE();
1369          try { p.shutdown(); } catch (SecurityException ok) { return; }
1370 <        assertTrue(p.terminatedCalled);
1370 >        assertTrue(p.terminatedCalled());
1371          joinPool(p);
1372      }
1373  
# Line 1249 | Line 1377 | public class ThreadPoolExecutorSubclassT
1377      public void testBeforeAfter() throws InterruptedException {
1378          CustomTPE p = new CustomTPE();
1379          try {
1380 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1381 <            p.execute(r);
1382 <            delay(SHORT_DELAY_MS);
1383 <            assertTrue(r.done);
1384 <            assertTrue(p.beforeCalled);
1385 <            assertTrue(p.afterCalled);
1380 >            final CountDownLatch done = new CountDownLatch(1);
1381 >            p.execute(new CheckedRunnable() {
1382 >                public void realRun() {
1383 >                    done.countDown();
1384 >                }});
1385 >            await(p.afterCalled);
1386 >            assertEquals(0, done.getCount());
1387 >            assertTrue(p.afterCalled());
1388 >            assertTrue(p.beforeCalled());
1389              try { p.shutdown(); } catch (SecurityException ok) { return; }
1390          } finally {
1391              joinPool(p);
# Line 1303 | Line 1434 | public class ThreadPoolExecutorSubclassT
1434          }
1435      }
1436  
1306
1437      /**
1438       * invokeAny(null) throws NPE
1439       */
# Line 1465 | Line 1595 | public class ThreadPoolExecutorSubclassT
1595          }
1596      }
1597  
1468
1469
1598      /**
1599       * timed invokeAny(null) throws NPE
1600       */
# Line 1653 | Line 1781 | public class ThreadPoolExecutorSubclassT
1781              l.add(new StringTask());
1782              l.add(new StringTask());
1783              List<Future<String>> futures =
1784 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1784 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1785              assertEquals(2, futures.size());
1786              for (Future<String> future : futures)
1787                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1796 | public class ThreadPoolExecutorSubclassT
1796      public void testTimedInvokeAll6() throws Exception {
1797          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1798          try {
1799 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1800 <            l.add(new StringTask());
1801 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1802 <            l.add(new StringTask());
1803 <            List<Future<String>> futures =
1804 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1805 <            assertEquals(3, futures.size());
1806 <            Iterator<Future<String>> it = futures.iterator();
1807 <            Future<String> f1 = it.next();
1808 <            Future<String> f2 = it.next();
1809 <            Future<String> f3 = it.next();
1810 <            assertTrue(f1.isDone());
1811 <            assertTrue(f2.isDone());
1812 <            assertTrue(f3.isDone());
1813 <            assertFalse(f1.isCancelled());
1814 <            assertTrue(f2.isCancelled());
1799 >            for (long timeout = timeoutMillis();;) {
1800 >                List<Callable<String>> tasks = new ArrayList<>();
1801 >                tasks.add(new StringTask("0"));
1802 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1803 >                tasks.add(new StringTask("2"));
1804 >                long startTime = System.nanoTime();
1805 >                List<Future<String>> futures =
1806 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1807 >                assertEquals(tasks.size(), futures.size());
1808 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1809 >                for (Future future : futures)
1810 >                    assertTrue(future.isDone());
1811 >                assertTrue(futures.get(1).isCancelled());
1812 >                try {
1813 >                    assertEquals("0", futures.get(0).get());
1814 >                    assertEquals("2", futures.get(2).get());
1815 >                    break;
1816 >                } catch (CancellationException retryWithLongerTimeout) {
1817 >                    timeout *= 2;
1818 >                    if (timeout >= LONG_DELAY_MS / 2)
1819 >                        fail("expected exactly one task to be cancelled");
1820 >                }
1821 >            }
1822          } finally {
1823              joinPool(e);
1824          }
# Line 1726 | Line 1861 | public class ThreadPoolExecutorSubclassT
1861       * allowCoreThreadTimeOut(true) causes idle threads to time out
1862       */
1863      public void testAllowCoreThreadTimeOut_true() throws Exception {
1864 +        long keepAliveTime = timeoutMillis();
1865          final ThreadPoolExecutor p =
1866              new CustomTPE(2, 10,
1867 <                          SHORT_DELAY_MS, MILLISECONDS,
1867 >                          keepAliveTime, MILLISECONDS,
1868                            new ArrayBlockingQueue<Runnable>(10));
1869          final CountDownLatch threadStarted = new CountDownLatch(1);
1870          try {
1871              p.allowCoreThreadTimeOut(true);
1872              p.execute(new CheckedRunnable() {
1873 <                public void realRun() throws InterruptedException {
1873 >                public void realRun() {
1874                      threadStarted.countDown();
1875                      assertEquals(1, p.getPoolSize());
1876                  }});
1877 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1878 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1879 <                if (p.getPoolSize() == 0)
1880 <                    break;
1881 <                delay(10);
1882 <            }
1877 >            await(threadStarted);
1878 >            delay(keepAliveTime);
1879 >            long startTime = System.nanoTime();
1880 >            while (p.getPoolSize() > 0
1881 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1882 >                Thread.yield();
1883 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1884              assertEquals(0, p.getPoolSize());
1885          } finally {
1886              joinPool(p);
# Line 1754 | Line 1891 | public class ThreadPoolExecutorSubclassT
1891       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1892       */
1893      public void testAllowCoreThreadTimeOut_false() throws Exception {
1894 +        long keepAliveTime = timeoutMillis();
1895          final ThreadPoolExecutor p =
1896              new CustomTPE(2, 10,
1897 <                          SHORT_DELAY_MS, MILLISECONDS,
1897 >                          keepAliveTime, MILLISECONDS,
1898                            new ArrayBlockingQueue<Runnable>(10));
1899          final CountDownLatch threadStarted = new CountDownLatch(1);
1900          try {
# Line 1766 | Line 1904 | public class ThreadPoolExecutorSubclassT
1904                      threadStarted.countDown();
1905                      assertTrue(p.getPoolSize() >= 1);
1906                  }});
1907 <            delay(SMALL_DELAY_MS);
1907 >            delay(2 * keepAliveTime);
1908              assertTrue(p.getPoolSize() >= 1);
1909          } finally {
1910              joinPool(p);
1911          }
1912      }
1913  
1914 +    /**
1915 +     * get(cancelled task) throws CancellationException
1916 +     * (in part, a test of CustomTPE itself)
1917 +     */
1918 +    public void testGet_cancelled() throws Exception {
1919 +        final ExecutorService e =
1920 +            new CustomTPE(1, 1,
1921 +                          LONG_DELAY_MS, MILLISECONDS,
1922 +                          new LinkedBlockingQueue<Runnable>());
1923 +        try {
1924 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1925 +            final CountDownLatch done = new CountDownLatch(1);
1926 +            final List<Future<?>> futures = new ArrayList<>();
1927 +            for (int i = 0; i < 2; i++) {
1928 +                Runnable r = new CheckedRunnable() { public void realRun()
1929 +                                                         throws Throwable {
1930 +                    blockerStarted.countDown();
1931 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1932 +                }};
1933 +                futures.add(e.submit(r));
1934 +            }
1935 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1936 +            for (Future<?> future : futures) future.cancel(false);
1937 +            for (Future<?> future : futures) {
1938 +                try {
1939 +                    future.get();
1940 +                    shouldThrow();
1941 +                } catch (CancellationException success) {}
1942 +                try {
1943 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1944 +                    shouldThrow();
1945 +                } catch (CancellationException success) {}
1946 +                assertTrue(future.isCancelled());
1947 +                assertTrue(future.isDone());
1948 +            }
1949 +            done.countDown();
1950 +        } finally {
1951 +            joinPool(e);
1952 +        }
1953 +    }
1954 +
1955   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines