ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.41 by dl, Fri May 6 11:22:08 2011 UTC vs.
Revision 1.75 by jsr166, Sun Oct 4 02:00:19 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.atomic.*;
11 < import junit.framework.*;
12 < import java.util.*;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 > import static java.util.concurrent.TimeUnit.SECONDS;
12 >
13 > import java.util.ArrayList;
14 > import java.util.List;
15 > import java.util.concurrent.ArrayBlockingQueue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.Callable;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.ExecutionException;
21 > import java.util.concurrent.Executors;
22 > import java.util.concurrent.ExecutorService;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.FutureTask;
25 > import java.util.concurrent.LinkedBlockingQueue;
26 > import java.util.concurrent.RejectedExecutionException;
27 > import java.util.concurrent.RejectedExecutionHandler;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeUnit;
32 > import java.util.concurrent.atomic.AtomicInteger;
33 >
34 > import junit.framework.Test;
35 > import junit.framework.TestSuite;
36  
37   public class ThreadPoolExecutorTest extends JSR166TestCase {
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(ThreadPoolExecutorTest.class);
43      }
44  
45      static class ExtendedTPE extends ThreadPoolExecutor {
46 <        volatile boolean beforeCalled = false;
47 <        volatile boolean afterCalled = false;
48 <        volatile boolean terminatedCalled = false;
46 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
47 >        final CountDownLatch afterCalled = new CountDownLatch(1);
48 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
49 >
50          public ExtendedTPE() {
51              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
52          }
53          protected void beforeExecute(Thread t, Runnable r) {
54 <            beforeCalled = true;
54 >            beforeCalled.countDown();
55          }
56          protected void afterExecute(Runnable r, Throwable t) {
57 <            afterCalled = true;
57 >            afterCalled.countDown();
58          }
59          protected void terminated() {
60 <            terminatedCalled = true;
60 >            terminatedCalled.countDown();
61 >        }
62 >
63 >        public boolean beforeCalled() {
64 >            return beforeCalled.getCount() == 0;
65 >        }
66 >        public boolean afterCalled() {
67 >            return afterCalled.getCount() == 0;
68 >        }
69 >        public boolean terminatedCalled() {
70 >            return terminatedCalled.getCount() == 0;
71          }
72      }
73  
# Line 46 | Line 79 | public class ThreadPoolExecutorTest exte
79          }
80      }
81  
49
82      /**
83       * execute successfully executes a runnable
84       */
# Line 100 | Line 132 | public class ThreadPoolExecutorTest exte
132       */
133      public void testPrestartCoreThread() {
134          final ThreadPoolExecutor p =
135 <            new ThreadPoolExecutor(2, 2,
135 >            new ThreadPoolExecutor(2, 6,
136                                     LONG_DELAY_MS, MILLISECONDS,
137                                     new ArrayBlockingQueue<Runnable>(10));
138 <        assertEquals(0, p.getPoolSize());
139 <        assertTrue(p.prestartCoreThread());
140 <        assertEquals(1, p.getPoolSize());
141 <        assertTrue(p.prestartCoreThread());
142 <        assertEquals(2, p.getPoolSize());
143 <        assertFalse(p.prestartCoreThread());
144 <        assertEquals(2, p.getPoolSize());
145 <        joinPool(p);
138 >        try (PoolCleaner cleaner = cleaner(p)) {
139 >            assertEquals(0, p.getPoolSize());
140 >            assertTrue(p.prestartCoreThread());
141 >            assertEquals(1, p.getPoolSize());
142 >            assertTrue(p.prestartCoreThread());
143 >            assertEquals(2, p.getPoolSize());
144 >            assertFalse(p.prestartCoreThread());
145 >            assertEquals(2, p.getPoolSize());
146 >            p.setCorePoolSize(4);
147 >            assertTrue(p.prestartCoreThread());
148 >            assertEquals(3, p.getPoolSize());
149 >            assertTrue(p.prestartCoreThread());
150 >            assertEquals(4, p.getPoolSize());
151 >            assertFalse(p.prestartCoreThread());
152 >            assertEquals(4, p.getPoolSize());
153 >        }
154      }
155  
156      /**
# Line 118 | Line 158 | public class ThreadPoolExecutorTest exte
158       */
159      public void testPrestartAllCoreThreads() {
160          final ThreadPoolExecutor p =
161 <            new ThreadPoolExecutor(2, 2,
161 >            new ThreadPoolExecutor(2, 6,
162                                     LONG_DELAY_MS, MILLISECONDS,
163                                     new ArrayBlockingQueue<Runnable>(10));
164 <        assertEquals(0, p.getPoolSize());
165 <        p.prestartAllCoreThreads();
166 <        assertEquals(2, p.getPoolSize());
167 <        p.prestartAllCoreThreads();
168 <        assertEquals(2, p.getPoolSize());
169 <        joinPool(p);
164 >        try (PoolCleaner cleaner = cleaner(p)) {
165 >            assertEquals(0, p.getPoolSize());
166 >            p.prestartAllCoreThreads();
167 >            assertEquals(2, p.getPoolSize());
168 >            p.prestartAllCoreThreads();
169 >            assertEquals(2, p.getPoolSize());
170 >            p.setCorePoolSize(4);
171 >            p.prestartAllCoreThreads();
172 >            assertEquals(4, p.getPoolSize());
173 >            p.prestartAllCoreThreads();
174 >            assertEquals(4, p.getPoolSize());
175 >        }
176      }
177  
178      /**
# Line 138 | Line 184 | public class ThreadPoolExecutorTest exte
184              new ThreadPoolExecutor(2, 2,
185                                     LONG_DELAY_MS, MILLISECONDS,
186                                     new ArrayBlockingQueue<Runnable>(10));
187 <        final CountDownLatch threadStarted = new CountDownLatch(1);
188 <        final CountDownLatch threadProceed = new CountDownLatch(1);
189 <        final CountDownLatch threadDone = new CountDownLatch(1);
190 <        try {
187 >        try (PoolCleaner cleaner = cleaner(p)) {
188 >            final CountDownLatch threadStarted = new CountDownLatch(1);
189 >            final CountDownLatch threadProceed = new CountDownLatch(1);
190 >            final CountDownLatch threadDone = new CountDownLatch(1);
191              assertEquals(0, p.getCompletedTaskCount());
192              p.execute(new CheckedRunnable() {
193                  public void realRun() throws InterruptedException {
# Line 150 | Line 196 | public class ThreadPoolExecutorTest exte
196                      threadProceed.await();
197                      threadDone.countDown();
198                  }});
199 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
199 >            await(threadStarted);
200              assertEquals(0, p.getCompletedTaskCount());
201              threadProceed.countDown();
202              threadDone.await();
203 <            delay(SHORT_DELAY_MS);
204 <            assertEquals(1, p.getCompletedTaskCount());
205 <        } finally {
206 <            joinPool(p);
203 >            long startTime = System.nanoTime();
204 >            while (p.getCompletedTaskCount() != 1) {
205 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
206 >                    fail("timed out");
207 >                Thread.yield();
208 >            }
209          }
210      }
211  
# Line 169 | Line 217 | public class ThreadPoolExecutorTest exte
217              new ThreadPoolExecutor(1, 1,
218                                     LONG_DELAY_MS, MILLISECONDS,
219                                     new ArrayBlockingQueue<Runnable>(10));
220 <        assertEquals(1, p.getCorePoolSize());
221 <        joinPool(p);
220 >        try (PoolCleaner cleaner = cleaner(p)) {
221 >            assertEquals(1, p.getCorePoolSize());
222 >        }
223      }
224  
225      /**
# Line 181 | Line 230 | public class ThreadPoolExecutorTest exte
230              new ThreadPoolExecutor(2, 2,
231                                     1000, MILLISECONDS,
232                                     new ArrayBlockingQueue<Runnable>(10));
233 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
234 <        joinPool(p);
233 >        try (PoolCleaner cleaner = cleaner(p)) {
234 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
235 >        }
236      }
237  
188
238      /**
239       * getThreadFactory returns factory in constructor if not set
240       */
241      public void testGetThreadFactory() {
242 <        ThreadFactory tf = new SimpleThreadFactory();
242 >        ThreadFactory threadFactory = new SimpleThreadFactory();
243          final ThreadPoolExecutor p =
244              new ThreadPoolExecutor(1, 2,
245                                     LONG_DELAY_MS, MILLISECONDS,
246                                     new ArrayBlockingQueue<Runnable>(10),
247 <                                   tf,
247 >                                   threadFactory,
248                                     new NoOpREHandler());
249 <        assertSame(tf, p.getThreadFactory());
250 <        joinPool(p);
249 >        try (PoolCleaner cleaner = cleaner(p)) {
250 >            assertSame(threadFactory, p.getThreadFactory());
251 >        }
252      }
253  
254      /**
# Line 209 | Line 259 | public class ThreadPoolExecutorTest exte
259              new ThreadPoolExecutor(1, 2,
260                                     LONG_DELAY_MS, MILLISECONDS,
261                                     new ArrayBlockingQueue<Runnable>(10));
262 <        ThreadFactory tf = new SimpleThreadFactory();
263 <        p.setThreadFactory(tf);
264 <        assertSame(tf, p.getThreadFactory());
265 <        joinPool(p);
262 >        try (PoolCleaner cleaner = cleaner(p)) {
263 >            ThreadFactory threadFactory = new SimpleThreadFactory();
264 >            p.setThreadFactory(threadFactory);
265 >            assertSame(threadFactory, p.getThreadFactory());
266 >        }
267      }
268  
218
269      /**
270       * setThreadFactory(null) throws NPE
271       */
# Line 224 | Line 274 | public class ThreadPoolExecutorTest exte
274              new ThreadPoolExecutor(1, 2,
275                                     LONG_DELAY_MS, MILLISECONDS,
276                                     new ArrayBlockingQueue<Runnable>(10));
277 <        try {
278 <            p.setThreadFactory(null);
279 <            shouldThrow();
280 <        } catch (NullPointerException success) {
281 <        } finally {
232 <            joinPool(p);
277 >        try (PoolCleaner cleaner = cleaner(p)) {
278 >            try {
279 >                p.setThreadFactory(null);
280 >                shouldThrow();
281 >            } catch (NullPointerException success) {}
282          }
283      }
284  
# Line 237 | Line 286 | public class ThreadPoolExecutorTest exte
286       * getRejectedExecutionHandler returns handler in constructor if not set
287       */
288      public void testGetRejectedExecutionHandler() {
289 <        final RejectedExecutionHandler h = new NoOpREHandler();
289 >        final RejectedExecutionHandler handler = new NoOpREHandler();
290          final ThreadPoolExecutor p =
291              new ThreadPoolExecutor(1, 2,
292                                     LONG_DELAY_MS, MILLISECONDS,
293                                     new ArrayBlockingQueue<Runnable>(10),
294 <                                   h);
295 <        assertSame(h, p.getRejectedExecutionHandler());
296 <        joinPool(p);
294 >                                   handler);
295 >        try (PoolCleaner cleaner = cleaner(p)) {
296 >            assertSame(handler, p.getRejectedExecutionHandler());
297 >        }
298      }
299  
300      /**
# Line 256 | Line 306 | public class ThreadPoolExecutorTest exte
306              new ThreadPoolExecutor(1, 2,
307                                     LONG_DELAY_MS, MILLISECONDS,
308                                     new ArrayBlockingQueue<Runnable>(10));
309 <        RejectedExecutionHandler h = new NoOpREHandler();
310 <        p.setRejectedExecutionHandler(h);
311 <        assertSame(h, p.getRejectedExecutionHandler());
312 <        joinPool(p);
309 >        try (PoolCleaner cleaner = cleaner(p)) {
310 >            RejectedExecutionHandler handler = new NoOpREHandler();
311 >            p.setRejectedExecutionHandler(handler);
312 >            assertSame(handler, p.getRejectedExecutionHandler());
313 >        }
314      }
315  
265
316      /**
317       * setRejectedExecutionHandler(null) throws NPE
318       */
# Line 271 | Line 321 | public class ThreadPoolExecutorTest exte
321              new ThreadPoolExecutor(1, 2,
322                                     LONG_DELAY_MS, MILLISECONDS,
323                                     new ArrayBlockingQueue<Runnable>(10));
324 <        try {
325 <            p.setRejectedExecutionHandler(null);
326 <            shouldThrow();
327 <        } catch (NullPointerException success) {
328 <        } finally {
279 <            joinPool(p);
324 >        try (PoolCleaner cleaner = cleaner(p)) {
325 >            try {
326 >                p.setRejectedExecutionHandler(null);
327 >                shouldThrow();
328 >            } catch (NullPointerException success) {}
329          }
330      }
331  
283
332      /**
333       * getLargestPoolSize increases, but doesn't overestimate, when
334       * multiple threads active
# Line 378 | Line 426 | public class ThreadPoolExecutorTest exte
426      }
427  
428      /**
429 <     * isShutDown is false before shutdown, true after
429 >     * isShutdown is false before shutdown, true after
430       */
431      public void testIsShutdown() {
432          final ThreadPoolExecutor p =
# Line 391 | Line 439 | public class ThreadPoolExecutorTest exte
439          joinPool(p);
440      }
441  
442 +    /**
443 +     * awaitTermination on a non-shutdown pool times out
444 +     */
445 +    public void testAwaitTermination_timesOut() throws InterruptedException {
446 +        final ThreadPoolExecutor p =
447 +            new ThreadPoolExecutor(1, 1,
448 +                                   LONG_DELAY_MS, MILLISECONDS,
449 +                                   new ArrayBlockingQueue<Runnable>(10));
450 +        assertFalse(p.isTerminated());
451 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
452 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
453 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
454 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
455 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
456 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
457 +        long timeoutNanos = 999999L;
458 +        long startTime = System.nanoTime();
459 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
460 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
461 +        assertFalse(p.isTerminated());
462 +        startTime = System.nanoTime();
463 +        long timeoutMillis = timeoutMillis();
464 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
465 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
466 +        assertFalse(p.isTerminated());
467 +        p.shutdown();
468 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
469 +        assertTrue(p.isTerminated());
470 +    }
471  
472      /**
473       * isTerminated is false before termination, true after
# Line 564 | Line 641 | public class ThreadPoolExecutorTest exte
641      }
642  
643      /**
644 <     * shutDownNow returns a list containing tasks that were not run
644 >     * shutdownNow returns a list containing tasks that were not run,
645 >     * and those tasks are drained from the queue
646       */
647 <    public void testShutDownNow() {
647 >    public void testShutdownNow() throws InterruptedException {
648 >        final int poolSize = 2;
649 >        final int count = 5;
650 >        final AtomicInteger ran = new AtomicInteger(0);
651          final ThreadPoolExecutor p =
652 <            new ThreadPoolExecutor(1, 1,
652 >            new ThreadPoolExecutor(poolSize, poolSize,
653                                     LONG_DELAY_MS, MILLISECONDS,
654                                     new ArrayBlockingQueue<Runnable>(10));
655 <        List l;
656 <        try {
657 <            for (int i = 0; i < 5; i++)
577 <                p.execute(new MediumPossiblyInterruptedRunnable());
578 <        }
579 <        finally {
655 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
656 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
657 >            threadsStarted.countDown();
658              try {
659 <                l = p.shutdownNow();
660 <            } catch (SecurityException ok) { return; }
659 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
660 >            } catch (InterruptedException success) {}
661 >            ran.getAndIncrement();
662 >        }};
663 >        for (int i = 0; i < count; i++)
664 >            p.execute(waiter);
665 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
666 >        assertEquals(poolSize, p.getActiveCount());
667 >        assertEquals(0, p.getCompletedTaskCount());
668 >        final List<Runnable> queuedTasks;
669 >        try {
670 >            queuedTasks = p.shutdownNow();
671 >        } catch (SecurityException ok) {
672 >            return; // Allowed in case test doesn't have privs
673          }
674          assertTrue(p.isShutdown());
675 <        assertTrue(l.size() <= 4);
675 >        assertTrue(p.getQueue().isEmpty());
676 >        assertEquals(count - poolSize, queuedTasks.size());
677 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
678 >        assertTrue(p.isTerminated());
679 >        assertEquals(poolSize, ran.get());
680 >        assertEquals(poolSize, p.getCompletedTaskCount());
681      }
682  
683      // Exception Tests
684  
590
685      /**
686       * Constructor throws if corePoolSize argument is less than zero
687       */
688      public void testConstructor1() {
689          try {
690 <            new ThreadPoolExecutor(-1, 1,
597 <                                   LONG_DELAY_MS, MILLISECONDS,
690 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
691                                     new ArrayBlockingQueue<Runnable>(10));
692              shouldThrow();
693          } catch (IllegalArgumentException success) {}
# Line 605 | Line 698 | public class ThreadPoolExecutorTest exte
698       */
699      public void testConstructor2() {
700          try {
701 <            new ThreadPoolExecutor(1, -1,
609 <                                   LONG_DELAY_MS, MILLISECONDS,
701 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
702                                     new ArrayBlockingQueue<Runnable>(10));
703              shouldThrow();
704          } catch (IllegalArgumentException success) {}
# Line 617 | Line 709 | public class ThreadPoolExecutorTest exte
709       */
710      public void testConstructor3() {
711          try {
712 <            new ThreadPoolExecutor(1, 0,
621 <                                   LONG_DELAY_MS, MILLISECONDS,
712 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
713                                     new ArrayBlockingQueue<Runnable>(10));
714              shouldThrow();
715          } catch (IllegalArgumentException success) {}
# Line 629 | Line 720 | public class ThreadPoolExecutorTest exte
720       */
721      public void testConstructor4() {
722          try {
723 <            new ThreadPoolExecutor(1, 2,
633 <                                   -1L, MILLISECONDS,
723 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
724                                     new ArrayBlockingQueue<Runnable>(10));
725              shouldThrow();
726          } catch (IllegalArgumentException success) {}
# Line 641 | Line 731 | public class ThreadPoolExecutorTest exte
731       */
732      public void testConstructor5() {
733          try {
734 <            new ThreadPoolExecutor(2, 1,
645 <                                   LONG_DELAY_MS, MILLISECONDS,
734 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
735                                     new ArrayBlockingQueue<Runnable>(10));
736              shouldThrow();
737          } catch (IllegalArgumentException success) {}
# Line 653 | Line 742 | public class ThreadPoolExecutorTest exte
742       */
743      public void testConstructorNullPointerException() {
744          try {
745 <            new ThreadPoolExecutor(1, 2,
657 <                                   LONG_DELAY_MS, MILLISECONDS,
745 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
746                                     (BlockingQueue) null);
747              shouldThrow();
748          } catch (NullPointerException success) {}
749      }
750  
663
664
751      /**
752       * Constructor throws if corePoolSize argument is less than zero
753       */
754      public void testConstructor6() {
755          try {
756 <            new ThreadPoolExecutor(-1, 1,
671 <                                   LONG_DELAY_MS, MILLISECONDS,
756 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
757                                     new ArrayBlockingQueue<Runnable>(10),
758                                     new SimpleThreadFactory());
759              shouldThrow();
# Line 680 | Line 765 | public class ThreadPoolExecutorTest exte
765       */
766      public void testConstructor7() {
767          try {
768 <            new ThreadPoolExecutor(1, -1,
684 <                                   LONG_DELAY_MS, MILLISECONDS,
768 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
769                                     new ArrayBlockingQueue<Runnable>(10),
770                                     new SimpleThreadFactory());
771              shouldThrow();
# Line 693 | Line 777 | public class ThreadPoolExecutorTest exte
777       */
778      public void testConstructor8() {
779          try {
780 <            new ThreadPoolExecutor(1, 0,
697 <                                   LONG_DELAY_MS, MILLISECONDS,
780 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
781                                     new ArrayBlockingQueue<Runnable>(10),
782                                     new SimpleThreadFactory());
783              shouldThrow();
# Line 706 | Line 789 | public class ThreadPoolExecutorTest exte
789       */
790      public void testConstructor9() {
791          try {
792 <            new ThreadPoolExecutor(1, 2,
710 <                                   -1L, MILLISECONDS,
792 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
793                                     new ArrayBlockingQueue<Runnable>(10),
794                                     new SimpleThreadFactory());
795              shouldThrow();
# Line 719 | Line 801 | public class ThreadPoolExecutorTest exte
801       */
802      public void testConstructor10() {
803          try {
804 <            new ThreadPoolExecutor(2, 1,
723 <                                   LONG_DELAY_MS, MILLISECONDS,
804 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
805                                     new ArrayBlockingQueue<Runnable>(10),
806                                     new SimpleThreadFactory());
807              shouldThrow();
# Line 732 | Line 813 | public class ThreadPoolExecutorTest exte
813       */
814      public void testConstructorNullPointerException2() {
815          try {
816 <            new ThreadPoolExecutor(1, 2,
736 <                                   LONG_DELAY_MS, MILLISECONDS,
816 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
817                                     (BlockingQueue) null,
818                                     new SimpleThreadFactory());
819              shouldThrow();
# Line 745 | Line 825 | public class ThreadPoolExecutorTest exte
825       */
826      public void testConstructorNullPointerException3() {
827          try {
828 <            new ThreadPoolExecutor(1, 2,
749 <                                   LONG_DELAY_MS, MILLISECONDS,
828 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
829                                     new ArrayBlockingQueue<Runnable>(10),
830                                     (ThreadFactory) null);
831              shouldThrow();
832          } catch (NullPointerException success) {}
833      }
834  
756
835      /**
836       * Constructor throws if corePoolSize argument is less than zero
837       */
838      public void testConstructor11() {
839          try {
840 <            new ThreadPoolExecutor(-1, 1,
763 <                                   LONG_DELAY_MS, MILLISECONDS,
840 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
841                                     new ArrayBlockingQueue<Runnable>(10),
842                                     new NoOpREHandler());
843              shouldThrow();
# Line 772 | Line 849 | public class ThreadPoolExecutorTest exte
849       */
850      public void testConstructor12() {
851          try {
852 <            new ThreadPoolExecutor(1, -1,
776 <                                   LONG_DELAY_MS, MILLISECONDS,
852 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
853                                     new ArrayBlockingQueue<Runnable>(10),
854                                     new NoOpREHandler());
855              shouldThrow();
# Line 785 | Line 861 | public class ThreadPoolExecutorTest exte
861       */
862      public void testConstructor13() {
863          try {
864 <            new ThreadPoolExecutor(1, 0,
789 <                                   LONG_DELAY_MS, MILLISECONDS,
864 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
865                                     new ArrayBlockingQueue<Runnable>(10),
866                                     new NoOpREHandler());
867              shouldThrow();
# Line 798 | Line 873 | public class ThreadPoolExecutorTest exte
873       */
874      public void testConstructor14() {
875          try {
876 <            new ThreadPoolExecutor(1, 2,
802 <                                   -1L, MILLISECONDS,
876 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
877                                     new ArrayBlockingQueue<Runnable>(10),
878                                     new NoOpREHandler());
879              shouldThrow();
# Line 811 | Line 885 | public class ThreadPoolExecutorTest exte
885       */
886      public void testConstructor15() {
887          try {
888 <            new ThreadPoolExecutor(2, 1,
815 <                                   LONG_DELAY_MS, MILLISECONDS,
888 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
889                                     new ArrayBlockingQueue<Runnable>(10),
890                                     new NoOpREHandler());
891              shouldThrow();
# Line 824 | Line 897 | public class ThreadPoolExecutorTest exte
897       */
898      public void testConstructorNullPointerException4() {
899          try {
900 <            new ThreadPoolExecutor(1, 2,
828 <                                   LONG_DELAY_MS, MILLISECONDS,
900 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
901                                     (BlockingQueue) null,
902                                     new NoOpREHandler());
903              shouldThrow();
# Line 837 | Line 909 | public class ThreadPoolExecutorTest exte
909       */
910      public void testConstructorNullPointerException5() {
911          try {
912 <            new ThreadPoolExecutor(1, 2,
841 <                                   LONG_DELAY_MS, MILLISECONDS,
912 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
913                                     new ArrayBlockingQueue<Runnable>(10),
914                                     (RejectedExecutionHandler) null);
915              shouldThrow();
916          } catch (NullPointerException success) {}
917      }
918  
848
919      /**
920       * Constructor throws if corePoolSize argument is less than zero
921       */
922      public void testConstructor16() {
923          try {
924 <            new ThreadPoolExecutor(-1, 1,
855 <                                   LONG_DELAY_MS, MILLISECONDS,
924 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
925                                     new ArrayBlockingQueue<Runnable>(10),
926                                     new SimpleThreadFactory(),
927                                     new NoOpREHandler());
# Line 865 | Line 934 | public class ThreadPoolExecutorTest exte
934       */
935      public void testConstructor17() {
936          try {
937 <            new ThreadPoolExecutor(1, -1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
937 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
938                                     new ArrayBlockingQueue<Runnable>(10),
939                                     new SimpleThreadFactory(),
940                                     new NoOpREHandler());
# Line 879 | Line 947 | public class ThreadPoolExecutorTest exte
947       */
948      public void testConstructor18() {
949          try {
950 <            new ThreadPoolExecutor(1, 0,
883 <                                   LONG_DELAY_MS, MILLISECONDS,
950 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
951                                     new ArrayBlockingQueue<Runnable>(10),
952                                     new SimpleThreadFactory(),
953                                     new NoOpREHandler());
# Line 893 | Line 960 | public class ThreadPoolExecutorTest exte
960       */
961      public void testConstructor19() {
962          try {
963 <            new ThreadPoolExecutor(1, 2,
897 <                                   -1L, MILLISECONDS,
963 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
964                                     new ArrayBlockingQueue<Runnable>(10),
965                                     new SimpleThreadFactory(),
966                                     new NoOpREHandler());
# Line 907 | Line 973 | public class ThreadPoolExecutorTest exte
973       */
974      public void testConstructor20() {
975          try {
976 <            new ThreadPoolExecutor(2, 1,
911 <                                   LONG_DELAY_MS, MILLISECONDS,
976 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
977                                     new ArrayBlockingQueue<Runnable>(10),
978                                     new SimpleThreadFactory(),
979                                     new NoOpREHandler());
# Line 921 | Line 986 | public class ThreadPoolExecutorTest exte
986       */
987      public void testConstructorNullPointerException6() {
988          try {
989 <            new ThreadPoolExecutor(1, 2,
925 <                                   LONG_DELAY_MS, MILLISECONDS,
989 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
990                                     (BlockingQueue) null,
991                                     new SimpleThreadFactory(),
992                                     new NoOpREHandler());
# Line 935 | Line 999 | public class ThreadPoolExecutorTest exte
999       */
1000      public void testConstructorNullPointerException7() {
1001          try {
1002 <            new ThreadPoolExecutor(1, 2,
939 <                                   LONG_DELAY_MS, MILLISECONDS,
1002 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1003                                     new ArrayBlockingQueue<Runnable>(10),
1004                                     new SimpleThreadFactory(),
1005                                     (RejectedExecutionHandler) null);
# Line 949 | Line 1012 | public class ThreadPoolExecutorTest exte
1012       */
1013      public void testConstructorNullPointerException8() {
1014          try {
1015 <            new ThreadPoolExecutor(1, 2,
953 <                                   LONG_DELAY_MS, MILLISECONDS,
1015 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1016                                     new ArrayBlockingQueue<Runnable>(10),
1017                                     (ThreadFactory) null,
1018                                     new NoOpREHandler());
# Line 964 | Line 1026 | public class ThreadPoolExecutorTest exte
1026      public void testInterruptedSubmit() throws InterruptedException {
1027          final ThreadPoolExecutor p =
1028              new ThreadPoolExecutor(1, 1,
1029 <                                   60, TimeUnit.SECONDS,
1029 >                                   60, SECONDS,
1030                                     new ArrayBlockingQueue<Runnable>(10));
1031  
1032          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1212 | Line 1274 | public class ThreadPoolExecutorTest exte
1274          }
1275      }
1276  
1215
1277      /**
1278       * execute using DiscardOldestPolicy drops task on shutdown
1279       */
# Line 1234 | Line 1295 | public class ThreadPoolExecutorTest exte
1295          }
1296      }
1297  
1237
1298      /**
1299       * execute(null) throws NPE
1300       */
1301      public void testExecuteNull() {
1302          ThreadPoolExecutor p =
1303 <            new ThreadPoolExecutor(1, 2,
1244 <                                   LONG_DELAY_MS, MILLISECONDS,
1303 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1304                                     new ArrayBlockingQueue<Runnable>(10));
1305          try {
1306              p.execute(null);
# Line 1307 | Line 1366 | public class ThreadPoolExecutorTest exte
1366          joinPool(p);
1367      }
1368  
1369 +    /**
1370 +     * Configuration changes that allow core pool size greater than
1371 +     * max pool size result in IllegalArgumentException.
1372 +     */
1373 +    public void testPoolSizeInvariants() {
1374 +        ThreadPoolExecutor p =
1375 +            new ThreadPoolExecutor(1, 1,
1376 +                                   LONG_DELAY_MS, MILLISECONDS,
1377 +                                   new ArrayBlockingQueue<Runnable>(10));
1378 +        for (int s = 1; s < 5; s++) {
1379 +            p.setMaximumPoolSize(s);
1380 +            p.setCorePoolSize(s);
1381 +            try {
1382 +                p.setMaximumPoolSize(s - 1);
1383 +                shouldThrow();
1384 +            } catch (IllegalArgumentException success) {}
1385 +            assertEquals(s, p.getCorePoolSize());
1386 +            assertEquals(s, p.getMaximumPoolSize());
1387 +            try {
1388 +                p.setCorePoolSize(s + 1);
1389 +                shouldThrow();
1390 +            } catch (IllegalArgumentException success) {}
1391 +            assertEquals(s, p.getCorePoolSize());
1392 +            assertEquals(s, p.getMaximumPoolSize());
1393 +        }
1394 +        joinPool(p);
1395 +    }
1396  
1397      /**
1398       * setKeepAliveTime throws IllegalArgumentException
# Line 1333 | Line 1419 | public class ThreadPoolExecutorTest exte
1419      public void testTerminated() {
1420          ExtendedTPE p = new ExtendedTPE();
1421          try { p.shutdown(); } catch (SecurityException ok) { return; }
1422 <        assertTrue(p.terminatedCalled);
1422 >        assertTrue(p.terminatedCalled());
1423          joinPool(p);
1424      }
1425  
# Line 1343 | Line 1429 | public class ThreadPoolExecutorTest exte
1429      public void testBeforeAfter() throws InterruptedException {
1430          ExtendedTPE p = new ExtendedTPE();
1431          try {
1432 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1433 <            p.execute(r);
1434 <            delay(SHORT_DELAY_MS);
1435 <            assertTrue(r.done);
1436 <            assertTrue(p.beforeCalled);
1437 <            assertTrue(p.afterCalled);
1432 >            final CountDownLatch done = new CountDownLatch(1);
1433 >            p.execute(new CheckedRunnable() {
1434 >                public void realRun() {
1435 >                    done.countDown();
1436 >                }});
1437 >            await(p.afterCalled);
1438 >            assertEquals(0, done.getCount());
1439 >            assertTrue(p.afterCalled());
1440 >            assertTrue(p.beforeCalled());
1441              try { p.shutdown(); } catch (SecurityException ok) { return; }
1442          } finally {
1443              joinPool(p);
# Line 1406 | Line 1495 | public class ThreadPoolExecutorTest exte
1495          }
1496      }
1497  
1409
1498      /**
1499       * invokeAny(null) throws NPE
1500       */
# Line 1600 | Line 1688 | public class ThreadPoolExecutorTest exte
1688          }
1689      }
1690  
1603
1604
1691      /**
1692       * timed invokeAny(null) throws NPE
1693       */
# Line 1824 | Line 1910 | public class ThreadPoolExecutorTest exte
1910              l.add(new StringTask());
1911              l.add(new StringTask());
1912              List<Future<String>> futures =
1913 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1913 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1914              assertEquals(2, futures.size());
1915              for (Future<String> future : futures)
1916                  assertSame(TEST_STRING, future.get());
# Line 1842 | Line 1928 | public class ThreadPoolExecutorTest exte
1928                                     LONG_DELAY_MS, MILLISECONDS,
1929                                     new ArrayBlockingQueue<Runnable>(10));
1930          try {
1931 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1932 <            l.add(new StringTask());
1933 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1934 <            l.add(new StringTask());
1935 <            List<Future<String>> futures =
1936 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1937 <            assertEquals(3, futures.size());
1938 <            Iterator<Future<String>> it = futures.iterator();
1939 <            Future<String> f1 = it.next();
1940 <            Future<String> f2 = it.next();
1941 <            Future<String> f3 = it.next();
1942 <            assertTrue(f1.isDone());
1943 <            assertTrue(f2.isDone());
1944 <            assertTrue(f3.isDone());
1945 <            assertFalse(f1.isCancelled());
1946 <            assertTrue(f2.isCancelled());
1931 >            for (long timeout = timeoutMillis();;) {
1932 >                List<Callable<String>> tasks = new ArrayList<>();
1933 >                tasks.add(new StringTask("0"));
1934 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1935 >                tasks.add(new StringTask("2"));
1936 >                long startTime = System.nanoTime();
1937 >                List<Future<String>> futures =
1938 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1939 >                assertEquals(tasks.size(), futures.size());
1940 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1941 >                for (Future future : futures)
1942 >                    assertTrue(future.isDone());
1943 >                assertTrue(futures.get(1).isCancelled());
1944 >                try {
1945 >                    assertEquals("0", futures.get(0).get());
1946 >                    assertEquals("2", futures.get(2).get());
1947 >                    break;
1948 >                } catch (CancellationException retryWithLongerTimeout) {
1949 >                    timeout *= 2;
1950 >                    if (timeout >= LONG_DELAY_MS / 2)
1951 >                        fail("expected exactly one task to be cancelled");
1952 >                }
1953 >            }
1954          } finally {
1955              joinPool(e);
1956          }
# Line 1903 | Line 1996 | public class ThreadPoolExecutorTest exte
1996       * allowCoreThreadTimeOut(true) causes idle threads to time out
1997       */
1998      public void testAllowCoreThreadTimeOut_true() throws Exception {
1999 +        long keepAliveTime = timeoutMillis();
2000          final ThreadPoolExecutor p =
2001              new ThreadPoolExecutor(2, 10,
2002 <                                   SHORT_DELAY_MS, MILLISECONDS,
2002 >                                   keepAliveTime, MILLISECONDS,
2003                                     new ArrayBlockingQueue<Runnable>(10));
2004          final CountDownLatch threadStarted = new CountDownLatch(1);
2005          try {
2006              p.allowCoreThreadTimeOut(true);
2007              p.execute(new CheckedRunnable() {
2008 <                public void realRun() throws InterruptedException {
2008 >                public void realRun() {
2009                      threadStarted.countDown();
2010                      assertEquals(1, p.getPoolSize());
2011                  }});
2012 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
2013 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
2014 <                if (p.getPoolSize() == 0)
2015 <                    break;
2016 <                delay(10);
2017 <            }
2012 >            await(threadStarted);
2013 >            delay(keepAliveTime);
2014 >            long startTime = System.nanoTime();
2015 >            while (p.getPoolSize() > 0
2016 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
2017 >                Thread.yield();
2018 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2019              assertEquals(0, p.getPoolSize());
2020          } finally {
2021              joinPool(p);
# Line 1931 | Line 2026 | public class ThreadPoolExecutorTest exte
2026       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2027       */
2028      public void testAllowCoreThreadTimeOut_false() throws Exception {
2029 +        long keepAliveTime = timeoutMillis();
2030          final ThreadPoolExecutor p =
2031              new ThreadPoolExecutor(2, 10,
2032 <                                   SHORT_DELAY_MS, MILLISECONDS,
2032 >                                   keepAliveTime, MILLISECONDS,
2033                                     new ArrayBlockingQueue<Runnable>(10));
2034          final CountDownLatch threadStarted = new CountDownLatch(1);
2035          try {
# Line 1943 | Line 2039 | public class ThreadPoolExecutorTest exte
2039                      threadStarted.countDown();
2040                      assertTrue(p.getPoolSize() >= 1);
2041                  }});
2042 <            delay(SMALL_DELAY_MS);
2042 >            delay(2 * keepAliveTime);
2043              assertTrue(p.getPoolSize() >= 1);
2044          } finally {
2045              joinPool(p);
# Line 1962 | Line 2058 | public class ThreadPoolExecutorTest exte
2058                  done.countDown();
2059              }};
2060          final ThreadPoolExecutor p =
2061 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2061 >            new ThreadPoolExecutor(1, 30,
2062 >                                   60, SECONDS,
2063                                     new ArrayBlockingQueue(30));
2064          try {
2065              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2074 | public class ThreadPoolExecutorTest exte
2074              // enough time to run all tasks
2075              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2076          } finally {
2077 <            p.shutdown();
2077 >            joinPool(p);
2078 >        }
2079 >    }
2080 >
2081 >    /**
2082 >     * get(cancelled task) throws CancellationException
2083 >     */
2084 >    public void testGet_cancelled() throws Exception {
2085 >        final ExecutorService e =
2086 >            new ThreadPoolExecutor(1, 1,
2087 >                                   LONG_DELAY_MS, MILLISECONDS,
2088 >                                   new LinkedBlockingQueue<Runnable>());
2089 >        try {
2090 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2091 >            final CountDownLatch done = new CountDownLatch(1);
2092 >            final List<Future<?>> futures = new ArrayList<>();
2093 >            for (int i = 0; i < 2; i++) {
2094 >                Runnable r = new CheckedRunnable() { public void realRun()
2095 >                                                         throws Throwable {
2096 >                    blockerStarted.countDown();
2097 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2098 >                }};
2099 >                futures.add(e.submit(r));
2100 >            }
2101 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2102 >            for (Future<?> future : futures) future.cancel(false);
2103 >            for (Future<?> future : futures) {
2104 >                try {
2105 >                    future.get();
2106 >                    shouldThrow();
2107 >                } catch (CancellationException success) {}
2108 >                try {
2109 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2110 >                    shouldThrow();
2111 >                } catch (CancellationException success) {}
2112 >                assertTrue(future.isCancelled());
2113 >                assertTrue(future.isDone());
2114 >            }
2115 >            done.countDown();
2116 >        } finally {
2117 >            joinPool(e);
2118          }
2119      }
2120  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines