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.72 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.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 262 | Line 311 | public class ThreadPoolExecutorTest exte
311          joinPool(p);
312      }
313  
265
314      /**
315       * setRejectedExecutionHandler(null) throws NPE
316       */
# Line 280 | Line 328 | public class ThreadPoolExecutorTest exte
328          }
329      }
330  
283
331      /**
332       * getLargestPoolSize increases, but doesn't overestimate, when
333       * multiple threads active
# Line 378 | Line 425 | public class ThreadPoolExecutorTest exte
425      }
426  
427      /**
428 <     * isShutDown is false before shutdown, true after
428 >     * isShutdown is false before shutdown, true after
429       */
430      public void testIsShutdown() {
431          final ThreadPoolExecutor p =
# Line 391 | Line 438 | public class ThreadPoolExecutorTest exte
438          joinPool(p);
439      }
440  
441 +    /**
442 +     * awaitTermination on a non-shutdown pool times out
443 +     */
444 +    public void testAwaitTermination_timesOut() throws InterruptedException {
445 +        final ThreadPoolExecutor p =
446 +            new ThreadPoolExecutor(1, 1,
447 +                                   LONG_DELAY_MS, MILLISECONDS,
448 +                                   new ArrayBlockingQueue<Runnable>(10));
449 +        assertFalse(p.isTerminated());
450 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
451 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
452 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
453 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
454 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
455 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
456 +        long timeoutNanos = 999999L;
457 +        long startTime = System.nanoTime();
458 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
459 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
460 +        assertFalse(p.isTerminated());
461 +        startTime = System.nanoTime();
462 +        long timeoutMillis = timeoutMillis();
463 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
464 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
465 +        assertFalse(p.isTerminated());
466 +        p.shutdown();
467 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
468 +        assertTrue(p.isTerminated());
469 +    }
470  
471      /**
472       * isTerminated is false before termination, true after
# Line 564 | Line 640 | public class ThreadPoolExecutorTest exte
640      }
641  
642      /**
643 <     * shutDownNow returns a list containing tasks that were not run
643 >     * shutdownNow returns a list containing tasks that were not run,
644 >     * and those tasks are drained from the queue
645       */
646 <    public void testShutDownNow() {
646 >    public void testShutdownNow() throws InterruptedException {
647 >        final int poolSize = 2;
648 >        final int count = 5;
649 >        final AtomicInteger ran = new AtomicInteger(0);
650          final ThreadPoolExecutor p =
651 <            new ThreadPoolExecutor(1, 1,
651 >            new ThreadPoolExecutor(poolSize, poolSize,
652                                     LONG_DELAY_MS, MILLISECONDS,
653                                     new ArrayBlockingQueue<Runnable>(10));
654 <        List l;
655 <        try {
656 <            for (int i = 0; i < 5; i++)
577 <                p.execute(new MediumPossiblyInterruptedRunnable());
578 <        }
579 <        finally {
654 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
655 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
656 >            threadsStarted.countDown();
657              try {
658 <                l = p.shutdownNow();
659 <            } catch (SecurityException ok) { return; }
658 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
659 >            } catch (InterruptedException success) {}
660 >            ran.getAndIncrement();
661 >        }};
662 >        for (int i = 0; i < count; i++)
663 >            p.execute(waiter);
664 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
665 >        assertEquals(poolSize, p.getActiveCount());
666 >        assertEquals(0, p.getCompletedTaskCount());
667 >        final List<Runnable> queuedTasks;
668 >        try {
669 >            queuedTasks = p.shutdownNow();
670 >        } catch (SecurityException ok) {
671 >            return; // Allowed in case test doesn't have privs
672          }
673          assertTrue(p.isShutdown());
674 <        assertTrue(l.size() <= 4);
674 >        assertTrue(p.getQueue().isEmpty());
675 >        assertEquals(count - poolSize, queuedTasks.size());
676 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
677 >        assertTrue(p.isTerminated());
678 >        assertEquals(poolSize, ran.get());
679 >        assertEquals(poolSize, p.getCompletedTaskCount());
680      }
681  
682      // Exception Tests
683  
590
684      /**
685       * Constructor throws if corePoolSize argument is less than zero
686       */
687      public void testConstructor1() {
688          try {
689 <            new ThreadPoolExecutor(-1, 1,
597 <                                   LONG_DELAY_MS, MILLISECONDS,
689 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
690                                     new ArrayBlockingQueue<Runnable>(10));
691              shouldThrow();
692          } catch (IllegalArgumentException success) {}
# Line 605 | Line 697 | public class ThreadPoolExecutorTest exte
697       */
698      public void testConstructor2() {
699          try {
700 <            new ThreadPoolExecutor(1, -1,
609 <                                   LONG_DELAY_MS, MILLISECONDS,
700 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
701                                     new ArrayBlockingQueue<Runnable>(10));
702              shouldThrow();
703          } catch (IllegalArgumentException success) {}
# Line 617 | Line 708 | public class ThreadPoolExecutorTest exte
708       */
709      public void testConstructor3() {
710          try {
711 <            new ThreadPoolExecutor(1, 0,
621 <                                   LONG_DELAY_MS, MILLISECONDS,
711 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
712                                     new ArrayBlockingQueue<Runnable>(10));
713              shouldThrow();
714          } catch (IllegalArgumentException success) {}
# Line 629 | Line 719 | public class ThreadPoolExecutorTest exte
719       */
720      public void testConstructor4() {
721          try {
722 <            new ThreadPoolExecutor(1, 2,
633 <                                   -1L, MILLISECONDS,
722 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
723                                     new ArrayBlockingQueue<Runnable>(10));
724              shouldThrow();
725          } catch (IllegalArgumentException success) {}
# Line 641 | Line 730 | public class ThreadPoolExecutorTest exte
730       */
731      public void testConstructor5() {
732          try {
733 <            new ThreadPoolExecutor(2, 1,
645 <                                   LONG_DELAY_MS, MILLISECONDS,
733 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
734                                     new ArrayBlockingQueue<Runnable>(10));
735              shouldThrow();
736          } catch (IllegalArgumentException success) {}
# Line 653 | Line 741 | public class ThreadPoolExecutorTest exte
741       */
742      public void testConstructorNullPointerException() {
743          try {
744 <            new ThreadPoolExecutor(1, 2,
657 <                                   LONG_DELAY_MS, MILLISECONDS,
744 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
745                                     (BlockingQueue) null);
746              shouldThrow();
747          } catch (NullPointerException success) {}
748      }
749  
663
664
750      /**
751       * Constructor throws if corePoolSize argument is less than zero
752       */
753      public void testConstructor6() {
754          try {
755 <            new ThreadPoolExecutor(-1, 1,
671 <                                   LONG_DELAY_MS, MILLISECONDS,
755 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
756                                     new ArrayBlockingQueue<Runnable>(10),
757                                     new SimpleThreadFactory());
758              shouldThrow();
# Line 680 | Line 764 | public class ThreadPoolExecutorTest exte
764       */
765      public void testConstructor7() {
766          try {
767 <            new ThreadPoolExecutor(1, -1,
684 <                                   LONG_DELAY_MS, MILLISECONDS,
767 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
768                                     new ArrayBlockingQueue<Runnable>(10),
769                                     new SimpleThreadFactory());
770              shouldThrow();
# Line 693 | Line 776 | public class ThreadPoolExecutorTest exte
776       */
777      public void testConstructor8() {
778          try {
779 <            new ThreadPoolExecutor(1, 0,
697 <                                   LONG_DELAY_MS, MILLISECONDS,
779 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
780                                     new ArrayBlockingQueue<Runnable>(10),
781                                     new SimpleThreadFactory());
782              shouldThrow();
# Line 706 | Line 788 | public class ThreadPoolExecutorTest exte
788       */
789      public void testConstructor9() {
790          try {
791 <            new ThreadPoolExecutor(1, 2,
710 <                                   -1L, MILLISECONDS,
791 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
792                                     new ArrayBlockingQueue<Runnable>(10),
793                                     new SimpleThreadFactory());
794              shouldThrow();
# Line 719 | Line 800 | public class ThreadPoolExecutorTest exte
800       */
801      public void testConstructor10() {
802          try {
803 <            new ThreadPoolExecutor(2, 1,
723 <                                   LONG_DELAY_MS, MILLISECONDS,
803 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
804                                     new ArrayBlockingQueue<Runnable>(10),
805                                     new SimpleThreadFactory());
806              shouldThrow();
# Line 732 | Line 812 | public class ThreadPoolExecutorTest exte
812       */
813      public void testConstructorNullPointerException2() {
814          try {
815 <            new ThreadPoolExecutor(1, 2,
736 <                                   LONG_DELAY_MS, MILLISECONDS,
815 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
816                                     (BlockingQueue) null,
817                                     new SimpleThreadFactory());
818              shouldThrow();
# Line 745 | Line 824 | public class ThreadPoolExecutorTest exte
824       */
825      public void testConstructorNullPointerException3() {
826          try {
827 <            new ThreadPoolExecutor(1, 2,
749 <                                   LONG_DELAY_MS, MILLISECONDS,
827 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
828                                     new ArrayBlockingQueue<Runnable>(10),
829                                     (ThreadFactory) null);
830              shouldThrow();
831          } catch (NullPointerException success) {}
832      }
833  
756
834      /**
835       * Constructor throws if corePoolSize argument is less than zero
836       */
837      public void testConstructor11() {
838          try {
839 <            new ThreadPoolExecutor(-1, 1,
763 <                                   LONG_DELAY_MS, MILLISECONDS,
839 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
840                                     new ArrayBlockingQueue<Runnable>(10),
841                                     new NoOpREHandler());
842              shouldThrow();
# Line 772 | Line 848 | public class ThreadPoolExecutorTest exte
848       */
849      public void testConstructor12() {
850          try {
851 <            new ThreadPoolExecutor(1, -1,
776 <                                   LONG_DELAY_MS, MILLISECONDS,
851 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
852                                     new ArrayBlockingQueue<Runnable>(10),
853                                     new NoOpREHandler());
854              shouldThrow();
# Line 785 | Line 860 | public class ThreadPoolExecutorTest exte
860       */
861      public void testConstructor13() {
862          try {
863 <            new ThreadPoolExecutor(1, 0,
789 <                                   LONG_DELAY_MS, MILLISECONDS,
863 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
864                                     new ArrayBlockingQueue<Runnable>(10),
865                                     new NoOpREHandler());
866              shouldThrow();
# Line 798 | Line 872 | public class ThreadPoolExecutorTest exte
872       */
873      public void testConstructor14() {
874          try {
875 <            new ThreadPoolExecutor(1, 2,
802 <                                   -1L, MILLISECONDS,
875 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
876                                     new ArrayBlockingQueue<Runnable>(10),
877                                     new NoOpREHandler());
878              shouldThrow();
# Line 811 | Line 884 | public class ThreadPoolExecutorTest exte
884       */
885      public void testConstructor15() {
886          try {
887 <            new ThreadPoolExecutor(2, 1,
815 <                                   LONG_DELAY_MS, MILLISECONDS,
887 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
888                                     new ArrayBlockingQueue<Runnable>(10),
889                                     new NoOpREHandler());
890              shouldThrow();
# Line 824 | Line 896 | public class ThreadPoolExecutorTest exte
896       */
897      public void testConstructorNullPointerException4() {
898          try {
899 <            new ThreadPoolExecutor(1, 2,
828 <                                   LONG_DELAY_MS, MILLISECONDS,
899 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
900                                     (BlockingQueue) null,
901                                     new NoOpREHandler());
902              shouldThrow();
# Line 837 | Line 908 | public class ThreadPoolExecutorTest exte
908       */
909      public void testConstructorNullPointerException5() {
910          try {
911 <            new ThreadPoolExecutor(1, 2,
841 <                                   LONG_DELAY_MS, MILLISECONDS,
911 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
912                                     new ArrayBlockingQueue<Runnable>(10),
913                                     (RejectedExecutionHandler) null);
914              shouldThrow();
915          } catch (NullPointerException success) {}
916      }
917  
848
918      /**
919       * Constructor throws if corePoolSize argument is less than zero
920       */
921      public void testConstructor16() {
922          try {
923 <            new ThreadPoolExecutor(-1, 1,
855 <                                   LONG_DELAY_MS, MILLISECONDS,
923 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
924                                     new ArrayBlockingQueue<Runnable>(10),
925                                     new SimpleThreadFactory(),
926                                     new NoOpREHandler());
# Line 865 | Line 933 | public class ThreadPoolExecutorTest exte
933       */
934      public void testConstructor17() {
935          try {
936 <            new ThreadPoolExecutor(1, -1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
936 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
937                                     new ArrayBlockingQueue<Runnable>(10),
938                                     new SimpleThreadFactory(),
939                                     new NoOpREHandler());
# Line 879 | Line 946 | public class ThreadPoolExecutorTest exte
946       */
947      public void testConstructor18() {
948          try {
949 <            new ThreadPoolExecutor(1, 0,
883 <                                   LONG_DELAY_MS, MILLISECONDS,
949 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
950                                     new ArrayBlockingQueue<Runnable>(10),
951                                     new SimpleThreadFactory(),
952                                     new NoOpREHandler());
# Line 893 | Line 959 | public class ThreadPoolExecutorTest exte
959       */
960      public void testConstructor19() {
961          try {
962 <            new ThreadPoolExecutor(1, 2,
897 <                                   -1L, MILLISECONDS,
962 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
963                                     new ArrayBlockingQueue<Runnable>(10),
964                                     new SimpleThreadFactory(),
965                                     new NoOpREHandler());
# Line 907 | Line 972 | public class ThreadPoolExecutorTest exte
972       */
973      public void testConstructor20() {
974          try {
975 <            new ThreadPoolExecutor(2, 1,
911 <                                   LONG_DELAY_MS, MILLISECONDS,
975 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
976                                     new ArrayBlockingQueue<Runnable>(10),
977                                     new SimpleThreadFactory(),
978                                     new NoOpREHandler());
# Line 921 | Line 985 | public class ThreadPoolExecutorTest exte
985       */
986      public void testConstructorNullPointerException6() {
987          try {
988 <            new ThreadPoolExecutor(1, 2,
925 <                                   LONG_DELAY_MS, MILLISECONDS,
988 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
989                                     (BlockingQueue) null,
990                                     new SimpleThreadFactory(),
991                                     new NoOpREHandler());
# Line 935 | Line 998 | public class ThreadPoolExecutorTest exte
998       */
999      public void testConstructorNullPointerException7() {
1000          try {
1001 <            new ThreadPoolExecutor(1, 2,
939 <                                   LONG_DELAY_MS, MILLISECONDS,
1001 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1002                                     new ArrayBlockingQueue<Runnable>(10),
1003                                     new SimpleThreadFactory(),
1004                                     (RejectedExecutionHandler) null);
# Line 949 | Line 1011 | public class ThreadPoolExecutorTest exte
1011       */
1012      public void testConstructorNullPointerException8() {
1013          try {
1014 <            new ThreadPoolExecutor(1, 2,
953 <                                   LONG_DELAY_MS, MILLISECONDS,
1014 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1015                                     new ArrayBlockingQueue<Runnable>(10),
1016                                     (ThreadFactory) null,
1017                                     new NoOpREHandler());
# Line 964 | Line 1025 | public class ThreadPoolExecutorTest exte
1025      public void testInterruptedSubmit() throws InterruptedException {
1026          final ThreadPoolExecutor p =
1027              new ThreadPoolExecutor(1, 1,
1028 <                                   60, TimeUnit.SECONDS,
1028 >                                   60, SECONDS,
1029                                     new ArrayBlockingQueue<Runnable>(10));
1030  
1031          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1212 | Line 1273 | public class ThreadPoolExecutorTest exte
1273          }
1274      }
1275  
1215
1276      /**
1277       * execute using DiscardOldestPolicy drops task on shutdown
1278       */
# Line 1234 | Line 1294 | public class ThreadPoolExecutorTest exte
1294          }
1295      }
1296  
1237
1297      /**
1298       * execute(null) throws NPE
1299       */
1300      public void testExecuteNull() {
1301          ThreadPoolExecutor p =
1302 <            new ThreadPoolExecutor(1, 2,
1244 <                                   LONG_DELAY_MS, MILLISECONDS,
1302 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1303                                     new ArrayBlockingQueue<Runnable>(10));
1304          try {
1305              p.execute(null);
# Line 1307 | Line 1365 | public class ThreadPoolExecutorTest exte
1365          joinPool(p);
1366      }
1367  
1368 +    /**
1369 +     * Configuration changes that allow core pool size greater than
1370 +     * max pool size result in IllegalArgumentException.
1371 +     */
1372 +    public void testPoolSizeInvariants() {
1373 +        ThreadPoolExecutor p =
1374 +            new ThreadPoolExecutor(1, 1,
1375 +                                   LONG_DELAY_MS, MILLISECONDS,
1376 +                                   new ArrayBlockingQueue<Runnable>(10));
1377 +        for (int s = 1; s < 5; s++) {
1378 +            p.setMaximumPoolSize(s);
1379 +            p.setCorePoolSize(s);
1380 +            try {
1381 +                p.setMaximumPoolSize(s - 1);
1382 +                shouldThrow();
1383 +            } catch (IllegalArgumentException success) {}
1384 +            assertEquals(s, p.getCorePoolSize());
1385 +            assertEquals(s, p.getMaximumPoolSize());
1386 +            try {
1387 +                p.setCorePoolSize(s + 1);
1388 +                shouldThrow();
1389 +            } catch (IllegalArgumentException success) {}
1390 +            assertEquals(s, p.getCorePoolSize());
1391 +            assertEquals(s, p.getMaximumPoolSize());
1392 +        }
1393 +        joinPool(p);
1394 +    }
1395  
1396      /**
1397       * setKeepAliveTime throws IllegalArgumentException
# Line 1333 | Line 1418 | public class ThreadPoolExecutorTest exte
1418      public void testTerminated() {
1419          ExtendedTPE p = new ExtendedTPE();
1420          try { p.shutdown(); } catch (SecurityException ok) { return; }
1421 <        assertTrue(p.terminatedCalled);
1421 >        assertTrue(p.terminatedCalled());
1422          joinPool(p);
1423      }
1424  
# Line 1343 | Line 1428 | public class ThreadPoolExecutorTest exte
1428      public void testBeforeAfter() throws InterruptedException {
1429          ExtendedTPE p = new ExtendedTPE();
1430          try {
1431 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1432 <            p.execute(r);
1433 <            delay(SHORT_DELAY_MS);
1434 <            assertTrue(r.done);
1435 <            assertTrue(p.beforeCalled);
1436 <            assertTrue(p.afterCalled);
1431 >            final CountDownLatch done = new CountDownLatch(1);
1432 >            p.execute(new CheckedRunnable() {
1433 >                public void realRun() {
1434 >                    done.countDown();
1435 >                }});
1436 >            await(p.afterCalled);
1437 >            assertEquals(0, done.getCount());
1438 >            assertTrue(p.afterCalled());
1439 >            assertTrue(p.beforeCalled());
1440              try { p.shutdown(); } catch (SecurityException ok) { return; }
1441          } finally {
1442              joinPool(p);
# Line 1406 | Line 1494 | public class ThreadPoolExecutorTest exte
1494          }
1495      }
1496  
1409
1497      /**
1498       * invokeAny(null) throws NPE
1499       */
# Line 1600 | Line 1687 | public class ThreadPoolExecutorTest exte
1687          }
1688      }
1689  
1603
1604
1690      /**
1691       * timed invokeAny(null) throws NPE
1692       */
# Line 1824 | Line 1909 | public class ThreadPoolExecutorTest exte
1909              l.add(new StringTask());
1910              l.add(new StringTask());
1911              List<Future<String>> futures =
1912 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1912 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1913              assertEquals(2, futures.size());
1914              for (Future<String> future : futures)
1915                  assertSame(TEST_STRING, future.get());
# Line 1842 | Line 1927 | public class ThreadPoolExecutorTest exte
1927                                     LONG_DELAY_MS, MILLISECONDS,
1928                                     new ArrayBlockingQueue<Runnable>(10));
1929          try {
1930 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1931 <            l.add(new StringTask());
1932 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1933 <            l.add(new StringTask());
1934 <            List<Future<String>> futures =
1935 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1936 <            assertEquals(3, futures.size());
1937 <            Iterator<Future<String>> it = futures.iterator();
1938 <            Future<String> f1 = it.next();
1939 <            Future<String> f2 = it.next();
1940 <            Future<String> f3 = it.next();
1941 <            assertTrue(f1.isDone());
1942 <            assertTrue(f2.isDone());
1943 <            assertTrue(f3.isDone());
1944 <            assertFalse(f1.isCancelled());
1945 <            assertTrue(f2.isCancelled());
1930 >            for (long timeout = timeoutMillis();;) {
1931 >                List<Callable<String>> tasks = new ArrayList<>();
1932 >                tasks.add(new StringTask("0"));
1933 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1934 >                tasks.add(new StringTask("2"));
1935 >                long startTime = System.nanoTime();
1936 >                List<Future<String>> futures =
1937 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1938 >                assertEquals(tasks.size(), futures.size());
1939 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1940 >                for (Future future : futures)
1941 >                    assertTrue(future.isDone());
1942 >                assertTrue(futures.get(1).isCancelled());
1943 >                try {
1944 >                    assertEquals("0", futures.get(0).get());
1945 >                    assertEquals("2", futures.get(2).get());
1946 >                    break;
1947 >                } catch (CancellationException retryWithLongerTimeout) {
1948 >                    timeout *= 2;
1949 >                    if (timeout >= LONG_DELAY_MS / 2)
1950 >                        fail("expected exactly one task to be cancelled");
1951 >                }
1952 >            }
1953          } finally {
1954              joinPool(e);
1955          }
# Line 1903 | Line 1995 | public class ThreadPoolExecutorTest exte
1995       * allowCoreThreadTimeOut(true) causes idle threads to time out
1996       */
1997      public void testAllowCoreThreadTimeOut_true() throws Exception {
1998 +        long keepAliveTime = timeoutMillis();
1999          final ThreadPoolExecutor p =
2000              new ThreadPoolExecutor(2, 10,
2001 <                                   SHORT_DELAY_MS, MILLISECONDS,
2001 >                                   keepAliveTime, MILLISECONDS,
2002                                     new ArrayBlockingQueue<Runnable>(10));
2003          final CountDownLatch threadStarted = new CountDownLatch(1);
2004          try {
2005              p.allowCoreThreadTimeOut(true);
2006              p.execute(new CheckedRunnable() {
2007 <                public void realRun() throws InterruptedException {
2007 >                public void realRun() {
2008                      threadStarted.countDown();
2009                      assertEquals(1, p.getPoolSize());
2010                  }});
2011 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
2012 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
2013 <                if (p.getPoolSize() == 0)
2014 <                    break;
2015 <                delay(10);
2016 <            }
2011 >            await(threadStarted);
2012 >            delay(keepAliveTime);
2013 >            long startTime = System.nanoTime();
2014 >            while (p.getPoolSize() > 0
2015 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
2016 >                Thread.yield();
2017 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2018              assertEquals(0, p.getPoolSize());
2019          } finally {
2020              joinPool(p);
# Line 1931 | Line 2025 | public class ThreadPoolExecutorTest exte
2025       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2026       */
2027      public void testAllowCoreThreadTimeOut_false() throws Exception {
2028 +        long keepAliveTime = timeoutMillis();
2029          final ThreadPoolExecutor p =
2030              new ThreadPoolExecutor(2, 10,
2031 <                                   SHORT_DELAY_MS, MILLISECONDS,
2031 >                                   keepAliveTime, MILLISECONDS,
2032                                     new ArrayBlockingQueue<Runnable>(10));
2033          final CountDownLatch threadStarted = new CountDownLatch(1);
2034          try {
# Line 1943 | Line 2038 | public class ThreadPoolExecutorTest exte
2038                      threadStarted.countDown();
2039                      assertTrue(p.getPoolSize() >= 1);
2040                  }});
2041 <            delay(SMALL_DELAY_MS);
2041 >            delay(2 * keepAliveTime);
2042              assertTrue(p.getPoolSize() >= 1);
2043          } finally {
2044              joinPool(p);
# Line 1962 | Line 2057 | public class ThreadPoolExecutorTest exte
2057                  done.countDown();
2058              }};
2059          final ThreadPoolExecutor p =
2060 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2060 >            new ThreadPoolExecutor(1, 30,
2061 >                                   60, SECONDS,
2062                                     new ArrayBlockingQueue(30));
2063          try {
2064              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2073 | public class ThreadPoolExecutorTest exte
2073              // enough time to run all tasks
2074              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2075          } finally {
2076 <            p.shutdown();
2076 >            joinPool(p);
2077 >        }
2078 >    }
2079 >
2080 >    /**
2081 >     * get(cancelled task) throws CancellationException
2082 >     */
2083 >    public void testGet_cancelled() throws Exception {
2084 >        final ExecutorService e =
2085 >            new ThreadPoolExecutor(1, 1,
2086 >                                   LONG_DELAY_MS, MILLISECONDS,
2087 >                                   new LinkedBlockingQueue<Runnable>());
2088 >        try {
2089 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2090 >            final CountDownLatch done = new CountDownLatch(1);
2091 >            final List<Future<?>> futures = new ArrayList<>();
2092 >            for (int i = 0; i < 2; i++) {
2093 >                Runnable r = new CheckedRunnable() { public void realRun()
2094 >                                                         throws Throwable {
2095 >                    blockerStarted.countDown();
2096 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2097 >                }};
2098 >                futures.add(e.submit(r));
2099 >            }
2100 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2101 >            for (Future<?> future : futures) future.cancel(false);
2102 >            for (Future<?> future : futures) {
2103 >                try {
2104 >                    future.get();
2105 >                    shouldThrow();
2106 >                } catch (CancellationException success) {}
2107 >                try {
2108 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2109 >                    shouldThrow();
2110 >                } catch (CancellationException success) {}
2111 >                assertTrue(future.isCancelled());
2112 >                assertTrue(future.isDone());
2113 >            }
2114 >            done.countDown();
2115 >        } finally {
2116 >            joinPool(e);
2117          }
2118      }
2119  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines