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.49 by jsr166, Wed Sep 25 07:39:17 2013 UTC vs.
Revision 1.76 by jsr166, Sun Oct 4 02:04:56 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10   import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 < import java.util.*;
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);
# Line 110 | 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 128 | 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 148 | 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 170 | Line 206 | public class ThreadPoolExecutorTest exte
206                      fail("timed out");
207                  Thread.yield();
208              }
173        } finally {
174            joinPool(p);
209          }
210      }
211  
# Line 183 | 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 195 | 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  
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 222 | 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  
269      /**
# Line 236 | 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 {
244 <            joinPool(p);
277 >        try (PoolCleaner cleaner = cleaner(p)) {
278 >            try {
279 >                p.setThreadFactory(null);
280 >                shouldThrow();
281 >            } catch (NullPointerException success) {}
282          }
283      }
284  
# Line 249 | 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 268 | 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  
316      /**
# Line 282 | 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 {
290 <            joinPool(p);
324 >        try (PoolCleaner cleaner = cleaner(p)) {
325 >            try {
326 >                p.setRejectedExecutionHandler(null);
327 >                shouldThrow();
328 >            } catch (NullPointerException success) {}
329          }
330      }
331  
# Line 301 | Line 339 | public class ThreadPoolExecutorTest exte
339              new ThreadPoolExecutor(THREADS, THREADS,
340                                     LONG_DELAY_MS, MILLISECONDS,
341                                     new ArrayBlockingQueue<Runnable>(10));
342 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
343 <        final CountDownLatch done = new CountDownLatch(1);
344 <        try {
342 >        try (PoolCleaner cleaner = cleaner(p)) {
343 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
344 >            final CountDownLatch done = new CountDownLatch(1);
345              assertEquals(0, p.getLargestPoolSize());
346              for (int i = 0; i < THREADS; i++)
347                  p.execute(new CheckedRunnable() {
# Line 312 | Line 350 | public class ThreadPoolExecutorTest exte
350                          done.await();
351                          assertEquals(THREADS, p.getLargestPoolSize());
352                      }});
353 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
316 <            assertEquals(THREADS, p.getLargestPoolSize());
317 <        } finally {
318 <            done.countDown();
319 <            joinPool(p);
353 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
354              assertEquals(THREADS, p.getLargestPoolSize());
355 +            done.countDown();   // release pool
356          }
357 +        assertEquals(THREADS, p.getLargestPoolSize());
358      }
359  
360      /**
# Line 603 | Line 639 | public class ThreadPoolExecutorTest exte
639      }
640  
641      /**
642 <     * shutdownNow returns a list containing tasks that were not run
642 >     * shutdownNow returns a list containing tasks that were not run,
643 >     * and those tasks are drained from the queue
644       */
645 <    public void testShutdownNow() {
645 >    public void testShutdownNow() throws InterruptedException {
646 >        final int poolSize = 2;
647 >        final int count = 5;
648 >        final AtomicInteger ran = new AtomicInteger(0);
649          final ThreadPoolExecutor p =
650 <            new ThreadPoolExecutor(1, 1,
650 >            new ThreadPoolExecutor(poolSize, poolSize,
651                                     LONG_DELAY_MS, MILLISECONDS,
652                                     new ArrayBlockingQueue<Runnable>(10));
653 <        List l;
654 <        try {
655 <            for (int i = 0; i < 5; i++)
616 <                p.execute(new MediumPossiblyInterruptedRunnable());
617 <        }
618 <        finally {
653 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
654 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
655 >            threadsStarted.countDown();
656              try {
657 <                l = p.shutdownNow();
658 <            } catch (SecurityException ok) { return; }
657 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
658 >            } catch (InterruptedException success) {}
659 >            ran.getAndIncrement();
660 >        }};
661 >        for (int i = 0; i < count; i++)
662 >            p.execute(waiter);
663 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
664 >        assertEquals(poolSize, p.getActiveCount());
665 >        assertEquals(0, p.getCompletedTaskCount());
666 >        final List<Runnable> queuedTasks;
667 >        try {
668 >            queuedTasks = p.shutdownNow();
669 >        } catch (SecurityException ok) {
670 >            return; // Allowed in case test doesn't have privs
671          }
672          assertTrue(p.isShutdown());
673 <        assertTrue(l.size() <= 4);
673 >        assertTrue(p.getQueue().isEmpty());
674 >        assertEquals(count - poolSize, queuedTasks.size());
675 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
676 >        assertTrue(p.isTerminated());
677 >        assertEquals(poolSize, ran.get());
678 >        assertEquals(poolSize, p.getCompletedTaskCount());
679      }
680  
681      // Exception Tests
# Line 631 | Line 685 | public class ThreadPoolExecutorTest exte
685       */
686      public void testConstructor1() {
687          try {
688 <            new ThreadPoolExecutor(-1, 1,
635 <                                   LONG_DELAY_MS, MILLISECONDS,
688 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
689                                     new ArrayBlockingQueue<Runnable>(10));
690              shouldThrow();
691          } catch (IllegalArgumentException success) {}
# Line 643 | Line 696 | public class ThreadPoolExecutorTest exte
696       */
697      public void testConstructor2() {
698          try {
699 <            new ThreadPoolExecutor(1, -1,
647 <                                   LONG_DELAY_MS, MILLISECONDS,
699 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
700                                     new ArrayBlockingQueue<Runnable>(10));
701              shouldThrow();
702          } catch (IllegalArgumentException success) {}
# Line 655 | Line 707 | public class ThreadPoolExecutorTest exte
707       */
708      public void testConstructor3() {
709          try {
710 <            new ThreadPoolExecutor(1, 0,
659 <                                   LONG_DELAY_MS, MILLISECONDS,
710 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
711                                     new ArrayBlockingQueue<Runnable>(10));
712              shouldThrow();
713          } catch (IllegalArgumentException success) {}
# Line 667 | Line 718 | public class ThreadPoolExecutorTest exte
718       */
719      public void testConstructor4() {
720          try {
721 <            new ThreadPoolExecutor(1, 2,
671 <                                   -1L, MILLISECONDS,
721 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
722                                     new ArrayBlockingQueue<Runnable>(10));
723              shouldThrow();
724          } catch (IllegalArgumentException success) {}
# Line 679 | Line 729 | public class ThreadPoolExecutorTest exte
729       */
730      public void testConstructor5() {
731          try {
732 <            new ThreadPoolExecutor(2, 1,
683 <                                   LONG_DELAY_MS, MILLISECONDS,
732 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
733                                     new ArrayBlockingQueue<Runnable>(10));
734              shouldThrow();
735          } catch (IllegalArgumentException success) {}
# Line 691 | Line 740 | public class ThreadPoolExecutorTest exte
740       */
741      public void testConstructorNullPointerException() {
742          try {
743 <            new ThreadPoolExecutor(1, 2,
695 <                                   LONG_DELAY_MS, MILLISECONDS,
743 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
744                                     (BlockingQueue) null);
745              shouldThrow();
746          } catch (NullPointerException success) {}
# Line 703 | Line 751 | public class ThreadPoolExecutorTest exte
751       */
752      public void testConstructor6() {
753          try {
754 <            new ThreadPoolExecutor(-1, 1,
707 <                                   LONG_DELAY_MS, MILLISECONDS,
754 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
755                                     new ArrayBlockingQueue<Runnable>(10),
756                                     new SimpleThreadFactory());
757              shouldThrow();
# Line 716 | Line 763 | public class ThreadPoolExecutorTest exte
763       */
764      public void testConstructor7() {
765          try {
766 <            new ThreadPoolExecutor(1, -1,
720 <                                   LONG_DELAY_MS, MILLISECONDS,
766 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
767                                     new ArrayBlockingQueue<Runnable>(10),
768                                     new SimpleThreadFactory());
769              shouldThrow();
# Line 729 | Line 775 | public class ThreadPoolExecutorTest exte
775       */
776      public void testConstructor8() {
777          try {
778 <            new ThreadPoolExecutor(1, 0,
733 <                                   LONG_DELAY_MS, MILLISECONDS,
778 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
779                                     new ArrayBlockingQueue<Runnable>(10),
780                                     new SimpleThreadFactory());
781              shouldThrow();
# Line 742 | Line 787 | public class ThreadPoolExecutorTest exte
787       */
788      public void testConstructor9() {
789          try {
790 <            new ThreadPoolExecutor(1, 2,
746 <                                   -1L, MILLISECONDS,
790 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
791                                     new ArrayBlockingQueue<Runnable>(10),
792                                     new SimpleThreadFactory());
793              shouldThrow();
# Line 755 | Line 799 | public class ThreadPoolExecutorTest exte
799       */
800      public void testConstructor10() {
801          try {
802 <            new ThreadPoolExecutor(2, 1,
759 <                                   LONG_DELAY_MS, MILLISECONDS,
802 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
803                                     new ArrayBlockingQueue<Runnable>(10),
804                                     new SimpleThreadFactory());
805              shouldThrow();
# Line 768 | Line 811 | public class ThreadPoolExecutorTest exte
811       */
812      public void testConstructorNullPointerException2() {
813          try {
814 <            new ThreadPoolExecutor(1, 2,
772 <                                   LONG_DELAY_MS, MILLISECONDS,
814 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
815                                     (BlockingQueue) null,
816                                     new SimpleThreadFactory());
817              shouldThrow();
# Line 781 | Line 823 | public class ThreadPoolExecutorTest exte
823       */
824      public void testConstructorNullPointerException3() {
825          try {
826 <            new ThreadPoolExecutor(1, 2,
785 <                                   LONG_DELAY_MS, MILLISECONDS,
826 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
827                                     new ArrayBlockingQueue<Runnable>(10),
828                                     (ThreadFactory) null);
829              shouldThrow();
# Line 794 | Line 835 | public class ThreadPoolExecutorTest exte
835       */
836      public void testConstructor11() {
837          try {
838 <            new ThreadPoolExecutor(-1, 1,
798 <                                   LONG_DELAY_MS, MILLISECONDS,
838 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
839                                     new ArrayBlockingQueue<Runnable>(10),
840                                     new NoOpREHandler());
841              shouldThrow();
# Line 807 | Line 847 | public class ThreadPoolExecutorTest exte
847       */
848      public void testConstructor12() {
849          try {
850 <            new ThreadPoolExecutor(1, -1,
811 <                                   LONG_DELAY_MS, MILLISECONDS,
850 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
851                                     new ArrayBlockingQueue<Runnable>(10),
852                                     new NoOpREHandler());
853              shouldThrow();
# Line 820 | Line 859 | public class ThreadPoolExecutorTest exte
859       */
860      public void testConstructor13() {
861          try {
862 <            new ThreadPoolExecutor(1, 0,
824 <                                   LONG_DELAY_MS, MILLISECONDS,
862 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
863                                     new ArrayBlockingQueue<Runnable>(10),
864                                     new NoOpREHandler());
865              shouldThrow();
# Line 833 | Line 871 | public class ThreadPoolExecutorTest exte
871       */
872      public void testConstructor14() {
873          try {
874 <            new ThreadPoolExecutor(1, 2,
837 <                                   -1L, MILLISECONDS,
874 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
875                                     new ArrayBlockingQueue<Runnable>(10),
876                                     new NoOpREHandler());
877              shouldThrow();
# Line 846 | Line 883 | public class ThreadPoolExecutorTest exte
883       */
884      public void testConstructor15() {
885          try {
886 <            new ThreadPoolExecutor(2, 1,
850 <                                   LONG_DELAY_MS, MILLISECONDS,
886 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
887                                     new ArrayBlockingQueue<Runnable>(10),
888                                     new NoOpREHandler());
889              shouldThrow();
# Line 859 | Line 895 | public class ThreadPoolExecutorTest exte
895       */
896      public void testConstructorNullPointerException4() {
897          try {
898 <            new ThreadPoolExecutor(1, 2,
863 <                                   LONG_DELAY_MS, MILLISECONDS,
898 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
899                                     (BlockingQueue) null,
900                                     new NoOpREHandler());
901              shouldThrow();
# Line 872 | Line 907 | public class ThreadPoolExecutorTest exte
907       */
908      public void testConstructorNullPointerException5() {
909          try {
910 <            new ThreadPoolExecutor(1, 2,
876 <                                   LONG_DELAY_MS, MILLISECONDS,
910 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
911                                     new ArrayBlockingQueue<Runnable>(10),
912                                     (RejectedExecutionHandler) null);
913              shouldThrow();
# Line 885 | Line 919 | public class ThreadPoolExecutorTest exte
919       */
920      public void testConstructor16() {
921          try {
922 <            new ThreadPoolExecutor(-1, 1,
889 <                                   LONG_DELAY_MS, MILLISECONDS,
922 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
923                                     new ArrayBlockingQueue<Runnable>(10),
924                                     new SimpleThreadFactory(),
925                                     new NoOpREHandler());
# Line 899 | Line 932 | public class ThreadPoolExecutorTest exte
932       */
933      public void testConstructor17() {
934          try {
935 <            new ThreadPoolExecutor(1, -1,
903 <                                   LONG_DELAY_MS, MILLISECONDS,
935 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
936                                     new ArrayBlockingQueue<Runnable>(10),
937                                     new SimpleThreadFactory(),
938                                     new NoOpREHandler());
# Line 913 | Line 945 | public class ThreadPoolExecutorTest exte
945       */
946      public void testConstructor18() {
947          try {
948 <            new ThreadPoolExecutor(1, 0,
917 <                                   LONG_DELAY_MS, MILLISECONDS,
948 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
949                                     new ArrayBlockingQueue<Runnable>(10),
950                                     new SimpleThreadFactory(),
951                                     new NoOpREHandler());
# Line 927 | Line 958 | public class ThreadPoolExecutorTest exte
958       */
959      public void testConstructor19() {
960          try {
961 <            new ThreadPoolExecutor(1, 2,
931 <                                   -1L, MILLISECONDS,
961 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
962                                     new ArrayBlockingQueue<Runnable>(10),
963                                     new SimpleThreadFactory(),
964                                     new NoOpREHandler());
# Line 941 | Line 971 | public class ThreadPoolExecutorTest exte
971       */
972      public void testConstructor20() {
973          try {
974 <            new ThreadPoolExecutor(2, 1,
945 <                                   LONG_DELAY_MS, MILLISECONDS,
974 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
975                                     new ArrayBlockingQueue<Runnable>(10),
976                                     new SimpleThreadFactory(),
977                                     new NoOpREHandler());
# Line 955 | Line 984 | public class ThreadPoolExecutorTest exte
984       */
985      public void testConstructorNullPointerException6() {
986          try {
987 <            new ThreadPoolExecutor(1, 2,
959 <                                   LONG_DELAY_MS, MILLISECONDS,
987 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
988                                     (BlockingQueue) null,
989                                     new SimpleThreadFactory(),
990                                     new NoOpREHandler());
# Line 969 | Line 997 | public class ThreadPoolExecutorTest exte
997       */
998      public void testConstructorNullPointerException7() {
999          try {
1000 <            new ThreadPoolExecutor(1, 2,
973 <                                   LONG_DELAY_MS, MILLISECONDS,
1000 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1001                                     new ArrayBlockingQueue<Runnable>(10),
1002                                     new SimpleThreadFactory(),
1003                                     (RejectedExecutionHandler) null);
# Line 983 | Line 1010 | public class ThreadPoolExecutorTest exte
1010       */
1011      public void testConstructorNullPointerException8() {
1012          try {
1013 <            new ThreadPoolExecutor(1, 2,
987 <                                   LONG_DELAY_MS, MILLISECONDS,
1013 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1014                                     new ArrayBlockingQueue<Runnable>(10),
1015                                     (ThreadFactory) null,
1016                                     new NoOpREHandler());
# Line 998 | Line 1024 | public class ThreadPoolExecutorTest exte
1024      public void testInterruptedSubmit() throws InterruptedException {
1025          final ThreadPoolExecutor p =
1026              new ThreadPoolExecutor(1, 1,
1027 <                                   60, TimeUnit.SECONDS,
1027 >                                   60, SECONDS,
1028                                     new ArrayBlockingQueue<Runnable>(10));
1029  
1030          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1272 | Line 1298 | public class ThreadPoolExecutorTest exte
1298       */
1299      public void testExecuteNull() {
1300          ThreadPoolExecutor p =
1301 <            new ThreadPoolExecutor(1, 2,
1276 <                                   LONG_DELAY_MS, MILLISECONDS,
1301 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1302                                     new ArrayBlockingQueue<Runnable>(10));
1303          try {
1304              p.execute(null);
# Line 1340 | Line 1365 | public class ThreadPoolExecutorTest exte
1365      }
1366  
1367      /**
1368 +     * Configuration changes that allow core pool size greater than
1369 +     * max pool size result in IllegalArgumentException.
1370 +     */
1371 +    public void testPoolSizeInvariants() {
1372 +        ThreadPoolExecutor p =
1373 +            new ThreadPoolExecutor(1, 1,
1374 +                                   LONG_DELAY_MS, MILLISECONDS,
1375 +                                   new ArrayBlockingQueue<Runnable>(10));
1376 +        for (int s = 1; s < 5; s++) {
1377 +            p.setMaximumPoolSize(s);
1378 +            p.setCorePoolSize(s);
1379 +            try {
1380 +                p.setMaximumPoolSize(s - 1);
1381 +                shouldThrow();
1382 +            } catch (IllegalArgumentException success) {}
1383 +            assertEquals(s, p.getCorePoolSize());
1384 +            assertEquals(s, p.getMaximumPoolSize());
1385 +            try {
1386 +                p.setCorePoolSize(s + 1);
1387 +                shouldThrow();
1388 +            } catch (IllegalArgumentException success) {}
1389 +            assertEquals(s, p.getCorePoolSize());
1390 +            assertEquals(s, p.getMaximumPoolSize());
1391 +        }
1392 +        joinPool(p);
1393 +    }
1394 +
1395 +    /**
1396       * setKeepAliveTime throws IllegalArgumentException
1397       * when given a negative value
1398       */
# Line 1855 | Line 1908 | public class ThreadPoolExecutorTest exte
1908              l.add(new StringTask());
1909              l.add(new StringTask());
1910              List<Future<String>> futures =
1911 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1911 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1912              assertEquals(2, futures.size());
1913              for (Future<String> future : futures)
1914                  assertSame(TEST_STRING, future.get());
# Line 1873 | Line 1926 | public class ThreadPoolExecutorTest exte
1926                                     LONG_DELAY_MS, MILLISECONDS,
1927                                     new ArrayBlockingQueue<Runnable>(10));
1928          try {
1929 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1930 <            l.add(new StringTask());
1931 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1932 <            l.add(new StringTask());
1933 <            List<Future<String>> futures =
1934 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1935 <            assertEquals(l.size(), futures.size());
1936 <            for (Future future : futures)
1937 <                assertTrue(future.isDone());
1938 <            assertFalse(futures.get(0).isCancelled());
1939 <            assertTrue(futures.get(1).isCancelled());
1929 >            for (long timeout = timeoutMillis();;) {
1930 >                List<Callable<String>> tasks = new ArrayList<>();
1931 >                tasks.add(new StringTask("0"));
1932 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1933 >                tasks.add(new StringTask("2"));
1934 >                long startTime = System.nanoTime();
1935 >                List<Future<String>> futures =
1936 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1937 >                assertEquals(tasks.size(), futures.size());
1938 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1939 >                for (Future future : futures)
1940 >                    assertTrue(future.isDone());
1941 >                assertTrue(futures.get(1).isCancelled());
1942 >                try {
1943 >                    assertEquals("0", futures.get(0).get());
1944 >                    assertEquals("2", futures.get(2).get());
1945 >                    break;
1946 >                } catch (CancellationException retryWithLongerTimeout) {
1947 >                    timeout *= 2;
1948 >                    if (timeout >= LONG_DELAY_MS / 2)
1949 >                        fail("expected exactly one task to be cancelled");
1950 >                }
1951 >            }
1952          } finally {
1953              joinPool(e);
1954          }
# Line 1929 | Line 1994 | public class ThreadPoolExecutorTest exte
1994       * allowCoreThreadTimeOut(true) causes idle threads to time out
1995       */
1996      public void testAllowCoreThreadTimeOut_true() throws Exception {
1997 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1997 >        long keepAliveTime = timeoutMillis();
1998          final ThreadPoolExecutor p =
1999              new ThreadPoolExecutor(2, 10,
2000 <                                   coreThreadTimeOut, MILLISECONDS,
2000 >                                   keepAliveTime, MILLISECONDS,
2001                                     new ArrayBlockingQueue<Runnable>(10));
2002          final CountDownLatch threadStarted = new CountDownLatch(1);
2003          try {
# Line 1943 | Line 2008 | public class ThreadPoolExecutorTest exte
2008                      assertEquals(1, p.getPoolSize());
2009                  }});
2010              await(threadStarted);
2011 <            delay(coreThreadTimeOut);
2011 >            delay(keepAliveTime);
2012              long startTime = System.nanoTime();
2013              while (p.getPoolSize() > 0
2014                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1959 | Line 2024 | public class ThreadPoolExecutorTest exte
2024       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2025       */
2026      public void testAllowCoreThreadTimeOut_false() throws Exception {
2027 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2027 >        long keepAliveTime = timeoutMillis();
2028          final ThreadPoolExecutor p =
2029              new ThreadPoolExecutor(2, 10,
2030 <                                   coreThreadTimeOut, MILLISECONDS,
2030 >                                   keepAliveTime, MILLISECONDS,
2031                                     new ArrayBlockingQueue<Runnable>(10));
2032          final CountDownLatch threadStarted = new CountDownLatch(1);
2033          try {
# Line 1972 | Line 2037 | public class ThreadPoolExecutorTest exte
2037                      threadStarted.countDown();
2038                      assertTrue(p.getPoolSize() >= 1);
2039                  }});
2040 <            delay(2 * coreThreadTimeOut);
2040 >            delay(2 * keepAliveTime);
2041              assertTrue(p.getPoolSize() >= 1);
2042          } finally {
2043              joinPool(p);
# Line 1991 | Line 2056 | public class ThreadPoolExecutorTest exte
2056                  done.countDown();
2057              }};
2058          final ThreadPoolExecutor p =
2059 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2059 >            new ThreadPoolExecutor(1, 30,
2060 >                                   60, SECONDS,
2061                                     new ArrayBlockingQueue(30));
2062          try {
2063              for (int i = 0; i < nTasks; ++i) {
# Line 2010 | Line 2076 | public class ThreadPoolExecutorTest exte
2076          }
2077      }
2078  
2079 +    /**
2080 +     * get(cancelled task) throws CancellationException
2081 +     */
2082 +    public void testGet_cancelled() throws Exception {
2083 +        final ExecutorService e =
2084 +            new ThreadPoolExecutor(1, 1,
2085 +                                   LONG_DELAY_MS, MILLISECONDS,
2086 +                                   new LinkedBlockingQueue<Runnable>());
2087 +        try {
2088 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
2089 +            final CountDownLatch done = new CountDownLatch(1);
2090 +            final List<Future<?>> futures = new ArrayList<>();
2091 +            for (int i = 0; i < 2; i++) {
2092 +                Runnable r = new CheckedRunnable() { public void realRun()
2093 +                                                         throws Throwable {
2094 +                    blockerStarted.countDown();
2095 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2096 +                }};
2097 +                futures.add(e.submit(r));
2098 +            }
2099 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2100 +            for (Future<?> future : futures) future.cancel(false);
2101 +            for (Future<?> future : futures) {
2102 +                try {
2103 +                    future.get();
2104 +                    shouldThrow();
2105 +                } catch (CancellationException success) {}
2106 +                try {
2107 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
2108 +                    shouldThrow();
2109 +                } catch (CancellationException success) {}
2110 +                assertTrue(future.isCancelled());
2111 +                assertTrue(future.isDone());
2112 +            }
2113 +            done.countDown();
2114 +        } finally {
2115 +            joinPool(e);
2116 +        }
2117 +    }
2118 +
2119   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines