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.45 by jsr166, Sun May 29 07:01:17 2011 UTC vs.
Revision 1.74 by jsr166, Sun Oct 4 01:58:38 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);
# 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 402 | Line 441 | public class ThreadPoolExecutorTest exte
441      }
442  
443      /**
444 +     * awaitTermination on a non-shutdown pool times out
445 +     */
446 +    public void testAwaitTermination_timesOut() throws InterruptedException {
447 +        final ThreadPoolExecutor p =
448 +            new ThreadPoolExecutor(1, 1,
449 +                                   LONG_DELAY_MS, MILLISECONDS,
450 +                                   new ArrayBlockingQueue<Runnable>(10));
451 +        assertFalse(p.isTerminated());
452 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
453 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
454 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
455 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
456 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
457 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
458 +        long timeoutNanos = 999999L;
459 +        long startTime = System.nanoTime();
460 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
461 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
462 +        assertFalse(p.isTerminated());
463 +        startTime = System.nanoTime();
464 +        long timeoutMillis = timeoutMillis();
465 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
466 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
467 +        assertFalse(p.isTerminated());
468 +        p.shutdown();
469 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
470 +        assertTrue(p.isTerminated());
471 +    }
472 +
473 +    /**
474       * isTerminated is false before termination, true after
475       */
476      public void testIsTerminated() throws InterruptedException {
# Line 573 | Line 642 | public class ThreadPoolExecutorTest exte
642      }
643  
644      /**
645 <     * shutdownNow returns a list containing tasks that were not run
645 >     * shutdownNow returns a list containing tasks that were not run,
646 >     * and those tasks are drained from the queue
647       */
648 <    public void testShutdownNow() {
648 >    public void testShutdownNow() throws InterruptedException {
649 >        final int poolSize = 2;
650 >        final int count = 5;
651 >        final AtomicInteger ran = new AtomicInteger(0);
652          final ThreadPoolExecutor p =
653 <            new ThreadPoolExecutor(1, 1,
653 >            new ThreadPoolExecutor(poolSize, poolSize,
654                                     LONG_DELAY_MS, MILLISECONDS,
655                                     new ArrayBlockingQueue<Runnable>(10));
656 <        List l;
657 <        try {
658 <            for (int i = 0; i < 5; i++)
586 <                p.execute(new MediumPossiblyInterruptedRunnable());
587 <        }
588 <        finally {
656 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
657 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
658 >            threadsStarted.countDown();
659              try {
660 <                l = p.shutdownNow();
661 <            } catch (SecurityException ok) { return; }
660 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
661 >            } catch (InterruptedException success) {}
662 >            ran.getAndIncrement();
663 >        }};
664 >        for (int i = 0; i < count; i++)
665 >            p.execute(waiter);
666 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
667 >        assertEquals(poolSize, p.getActiveCount());
668 >        assertEquals(0, p.getCompletedTaskCount());
669 >        final List<Runnable> queuedTasks;
670 >        try {
671 >            queuedTasks = p.shutdownNow();
672 >        } catch (SecurityException ok) {
673 >            return; // Allowed in case test doesn't have privs
674          }
675          assertTrue(p.isShutdown());
676 <        assertTrue(l.size() <= 4);
676 >        assertTrue(p.getQueue().isEmpty());
677 >        assertEquals(count - poolSize, queuedTasks.size());
678 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
679 >        assertTrue(p.isTerminated());
680 >        assertEquals(poolSize, ran.get());
681 >        assertEquals(poolSize, p.getCompletedTaskCount());
682      }
683  
684      // Exception Tests
# Line 601 | Line 688 | public class ThreadPoolExecutorTest exte
688       */
689      public void testConstructor1() {
690          try {
691 <            new ThreadPoolExecutor(-1, 1,
605 <                                   LONG_DELAY_MS, MILLISECONDS,
691 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
692                                     new ArrayBlockingQueue<Runnable>(10));
693              shouldThrow();
694          } catch (IllegalArgumentException success) {}
# Line 613 | Line 699 | public class ThreadPoolExecutorTest exte
699       */
700      public void testConstructor2() {
701          try {
702 <            new ThreadPoolExecutor(1, -1,
617 <                                   LONG_DELAY_MS, MILLISECONDS,
702 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
703                                     new ArrayBlockingQueue<Runnable>(10));
704              shouldThrow();
705          } catch (IllegalArgumentException success) {}
# Line 625 | Line 710 | public class ThreadPoolExecutorTest exte
710       */
711      public void testConstructor3() {
712          try {
713 <            new ThreadPoolExecutor(1, 0,
629 <                                   LONG_DELAY_MS, MILLISECONDS,
713 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
714                                     new ArrayBlockingQueue<Runnable>(10));
715              shouldThrow();
716          } catch (IllegalArgumentException success) {}
# Line 637 | Line 721 | public class ThreadPoolExecutorTest exte
721       */
722      public void testConstructor4() {
723          try {
724 <            new ThreadPoolExecutor(1, 2,
641 <                                   -1L, MILLISECONDS,
724 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
725                                     new ArrayBlockingQueue<Runnable>(10));
726              shouldThrow();
727          } catch (IllegalArgumentException success) {}
# Line 649 | Line 732 | public class ThreadPoolExecutorTest exte
732       */
733      public void testConstructor5() {
734          try {
735 <            new ThreadPoolExecutor(2, 1,
653 <                                   LONG_DELAY_MS, MILLISECONDS,
735 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
736                                     new ArrayBlockingQueue<Runnable>(10));
737              shouldThrow();
738          } catch (IllegalArgumentException success) {}
# Line 661 | Line 743 | public class ThreadPoolExecutorTest exte
743       */
744      public void testConstructorNullPointerException() {
745          try {
746 <            new ThreadPoolExecutor(1, 2,
665 <                                   LONG_DELAY_MS, MILLISECONDS,
746 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
747                                     (BlockingQueue) null);
748              shouldThrow();
749          } catch (NullPointerException success) {}
# Line 673 | Line 754 | public class ThreadPoolExecutorTest exte
754       */
755      public void testConstructor6() {
756          try {
757 <            new ThreadPoolExecutor(-1, 1,
677 <                                   LONG_DELAY_MS, MILLISECONDS,
757 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
758                                     new ArrayBlockingQueue<Runnable>(10),
759                                     new SimpleThreadFactory());
760              shouldThrow();
# Line 686 | Line 766 | public class ThreadPoolExecutorTest exte
766       */
767      public void testConstructor7() {
768          try {
769 <            new ThreadPoolExecutor(1, -1,
690 <                                   LONG_DELAY_MS, MILLISECONDS,
769 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
770                                     new ArrayBlockingQueue<Runnable>(10),
771                                     new SimpleThreadFactory());
772              shouldThrow();
# Line 699 | Line 778 | public class ThreadPoolExecutorTest exte
778       */
779      public void testConstructor8() {
780          try {
781 <            new ThreadPoolExecutor(1, 0,
703 <                                   LONG_DELAY_MS, MILLISECONDS,
781 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
782                                     new ArrayBlockingQueue<Runnable>(10),
783                                     new SimpleThreadFactory());
784              shouldThrow();
# Line 712 | Line 790 | public class ThreadPoolExecutorTest exte
790       */
791      public void testConstructor9() {
792          try {
793 <            new ThreadPoolExecutor(1, 2,
716 <                                   -1L, MILLISECONDS,
793 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
794                                     new ArrayBlockingQueue<Runnable>(10),
795                                     new SimpleThreadFactory());
796              shouldThrow();
# Line 725 | Line 802 | public class ThreadPoolExecutorTest exte
802       */
803      public void testConstructor10() {
804          try {
805 <            new ThreadPoolExecutor(2, 1,
729 <                                   LONG_DELAY_MS, MILLISECONDS,
805 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
806                                     new ArrayBlockingQueue<Runnable>(10),
807                                     new SimpleThreadFactory());
808              shouldThrow();
# Line 738 | Line 814 | public class ThreadPoolExecutorTest exte
814       */
815      public void testConstructorNullPointerException2() {
816          try {
817 <            new ThreadPoolExecutor(1, 2,
742 <                                   LONG_DELAY_MS, MILLISECONDS,
817 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
818                                     (BlockingQueue) null,
819                                     new SimpleThreadFactory());
820              shouldThrow();
# Line 751 | Line 826 | public class ThreadPoolExecutorTest exte
826       */
827      public void testConstructorNullPointerException3() {
828          try {
829 <            new ThreadPoolExecutor(1, 2,
755 <                                   LONG_DELAY_MS, MILLISECONDS,
829 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
830                                     new ArrayBlockingQueue<Runnable>(10),
831                                     (ThreadFactory) null);
832              shouldThrow();
# Line 764 | Line 838 | public class ThreadPoolExecutorTest exte
838       */
839      public void testConstructor11() {
840          try {
841 <            new ThreadPoolExecutor(-1, 1,
768 <                                   LONG_DELAY_MS, MILLISECONDS,
841 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
842                                     new ArrayBlockingQueue<Runnable>(10),
843                                     new NoOpREHandler());
844              shouldThrow();
# Line 777 | Line 850 | public class ThreadPoolExecutorTest exte
850       */
851      public void testConstructor12() {
852          try {
853 <            new ThreadPoolExecutor(1, -1,
781 <                                   LONG_DELAY_MS, MILLISECONDS,
853 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
854                                     new ArrayBlockingQueue<Runnable>(10),
855                                     new NoOpREHandler());
856              shouldThrow();
# Line 790 | Line 862 | public class ThreadPoolExecutorTest exte
862       */
863      public void testConstructor13() {
864          try {
865 <            new ThreadPoolExecutor(1, 0,
794 <                                   LONG_DELAY_MS, MILLISECONDS,
865 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
866                                     new ArrayBlockingQueue<Runnable>(10),
867                                     new NoOpREHandler());
868              shouldThrow();
# Line 803 | Line 874 | public class ThreadPoolExecutorTest exte
874       */
875      public void testConstructor14() {
876          try {
877 <            new ThreadPoolExecutor(1, 2,
807 <                                   -1L, MILLISECONDS,
877 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
878                                     new ArrayBlockingQueue<Runnable>(10),
879                                     new NoOpREHandler());
880              shouldThrow();
# Line 816 | Line 886 | public class ThreadPoolExecutorTest exte
886       */
887      public void testConstructor15() {
888          try {
889 <            new ThreadPoolExecutor(2, 1,
820 <                                   LONG_DELAY_MS, MILLISECONDS,
889 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
890                                     new ArrayBlockingQueue<Runnable>(10),
891                                     new NoOpREHandler());
892              shouldThrow();
# Line 829 | Line 898 | public class ThreadPoolExecutorTest exte
898       */
899      public void testConstructorNullPointerException4() {
900          try {
901 <            new ThreadPoolExecutor(1, 2,
833 <                                   LONG_DELAY_MS, MILLISECONDS,
901 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
902                                     (BlockingQueue) null,
903                                     new NoOpREHandler());
904              shouldThrow();
# Line 842 | Line 910 | public class ThreadPoolExecutorTest exte
910       */
911      public void testConstructorNullPointerException5() {
912          try {
913 <            new ThreadPoolExecutor(1, 2,
846 <                                   LONG_DELAY_MS, MILLISECONDS,
913 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
914                                     new ArrayBlockingQueue<Runnable>(10),
915                                     (RejectedExecutionHandler) null);
916              shouldThrow();
# Line 855 | Line 922 | public class ThreadPoolExecutorTest exte
922       */
923      public void testConstructor16() {
924          try {
925 <            new ThreadPoolExecutor(-1, 1,
859 <                                   LONG_DELAY_MS, MILLISECONDS,
925 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
926                                     new ArrayBlockingQueue<Runnable>(10),
927                                     new SimpleThreadFactory(),
928                                     new NoOpREHandler());
# Line 869 | Line 935 | public class ThreadPoolExecutorTest exte
935       */
936      public void testConstructor17() {
937          try {
938 <            new ThreadPoolExecutor(1, -1,
873 <                                   LONG_DELAY_MS, MILLISECONDS,
938 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
939                                     new ArrayBlockingQueue<Runnable>(10),
940                                     new SimpleThreadFactory(),
941                                     new NoOpREHandler());
# Line 883 | Line 948 | public class ThreadPoolExecutorTest exte
948       */
949      public void testConstructor18() {
950          try {
951 <            new ThreadPoolExecutor(1, 0,
887 <                                   LONG_DELAY_MS, MILLISECONDS,
951 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
952                                     new ArrayBlockingQueue<Runnable>(10),
953                                     new SimpleThreadFactory(),
954                                     new NoOpREHandler());
# Line 897 | Line 961 | public class ThreadPoolExecutorTest exte
961       */
962      public void testConstructor19() {
963          try {
964 <            new ThreadPoolExecutor(1, 2,
901 <                                   -1L, MILLISECONDS,
964 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
965                                     new ArrayBlockingQueue<Runnable>(10),
966                                     new SimpleThreadFactory(),
967                                     new NoOpREHandler());
# Line 911 | Line 974 | public class ThreadPoolExecutorTest exte
974       */
975      public void testConstructor20() {
976          try {
977 <            new ThreadPoolExecutor(2, 1,
915 <                                   LONG_DELAY_MS, MILLISECONDS,
977 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
978                                     new ArrayBlockingQueue<Runnable>(10),
979                                     new SimpleThreadFactory(),
980                                     new NoOpREHandler());
# Line 925 | Line 987 | public class ThreadPoolExecutorTest exte
987       */
988      public void testConstructorNullPointerException6() {
989          try {
990 <            new ThreadPoolExecutor(1, 2,
929 <                                   LONG_DELAY_MS, MILLISECONDS,
990 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
991                                     (BlockingQueue) null,
992                                     new SimpleThreadFactory(),
993                                     new NoOpREHandler());
# Line 939 | Line 1000 | public class ThreadPoolExecutorTest exte
1000       */
1001      public void testConstructorNullPointerException7() {
1002          try {
1003 <            new ThreadPoolExecutor(1, 2,
943 <                                   LONG_DELAY_MS, MILLISECONDS,
1003 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1004                                     new ArrayBlockingQueue<Runnable>(10),
1005                                     new SimpleThreadFactory(),
1006                                     (RejectedExecutionHandler) null);
# Line 953 | Line 1013 | public class ThreadPoolExecutorTest exte
1013       */
1014      public void testConstructorNullPointerException8() {
1015          try {
1016 <            new ThreadPoolExecutor(1, 2,
957 <                                   LONG_DELAY_MS, MILLISECONDS,
1016 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1017                                     new ArrayBlockingQueue<Runnable>(10),
1018                                     (ThreadFactory) null,
1019                                     new NoOpREHandler());
# Line 968 | Line 1027 | public class ThreadPoolExecutorTest exte
1027      public void testInterruptedSubmit() throws InterruptedException {
1028          final ThreadPoolExecutor p =
1029              new ThreadPoolExecutor(1, 1,
1030 <                                   60, TimeUnit.SECONDS,
1030 >                                   60, SECONDS,
1031                                     new ArrayBlockingQueue<Runnable>(10));
1032  
1033          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1242 | Line 1301 | public class ThreadPoolExecutorTest exte
1301       */
1302      public void testExecuteNull() {
1303          ThreadPoolExecutor p =
1304 <            new ThreadPoolExecutor(1, 2,
1246 <                                   LONG_DELAY_MS, MILLISECONDS,
1304 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1305                                     new ArrayBlockingQueue<Runnable>(10));
1306          try {
1307              p.execute(null);
# Line 1310 | Line 1368 | public class ThreadPoolExecutorTest exte
1368      }
1369  
1370      /**
1371 +     * Configuration changes that allow core pool size greater than
1372 +     * max pool size result in IllegalArgumentException.
1373 +     */
1374 +    public void testPoolSizeInvariants() {
1375 +        ThreadPoolExecutor p =
1376 +            new ThreadPoolExecutor(1, 1,
1377 +                                   LONG_DELAY_MS, MILLISECONDS,
1378 +                                   new ArrayBlockingQueue<Runnable>(10));
1379 +        for (int s = 1; s < 5; s++) {
1380 +            p.setMaximumPoolSize(s);
1381 +            p.setCorePoolSize(s);
1382 +            try {
1383 +                p.setMaximumPoolSize(s - 1);
1384 +                shouldThrow();
1385 +            } catch (IllegalArgumentException success) {}
1386 +            assertEquals(s, p.getCorePoolSize());
1387 +            assertEquals(s, p.getMaximumPoolSize());
1388 +            try {
1389 +                p.setCorePoolSize(s + 1);
1390 +                shouldThrow();
1391 +            } catch (IllegalArgumentException success) {}
1392 +            assertEquals(s, p.getCorePoolSize());
1393 +            assertEquals(s, p.getMaximumPoolSize());
1394 +        }
1395 +        joinPool(p);
1396 +    }
1397 +
1398 +    /**
1399       * setKeepAliveTime throws IllegalArgumentException
1400       * when given a negative value
1401       */
# Line 1345 | Line 1431 | public class ThreadPoolExecutorTest exte
1431          ExtendedTPE p = new ExtendedTPE();
1432          try {
1433              final CountDownLatch done = new CountDownLatch(1);
1434 <            final CheckedRunnable task = new CheckedRunnable() {
1434 >            p.execute(new CheckedRunnable() {
1435                  public void realRun() {
1436                      done.countDown();
1437 <                }};
1352 <            p.execute(task);
1437 >                }});
1438              await(p.afterCalled);
1439              assertEquals(0, done.getCount());
1440              assertTrue(p.afterCalled());
# Line 1826 | Line 1911 | public class ThreadPoolExecutorTest exte
1911              l.add(new StringTask());
1912              l.add(new StringTask());
1913              List<Future<String>> futures =
1914 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1914 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1915              assertEquals(2, futures.size());
1916              for (Future<String> future : futures)
1917                  assertSame(TEST_STRING, future.get());
# Line 1844 | Line 1929 | public class ThreadPoolExecutorTest exte
1929                                     LONG_DELAY_MS, MILLISECONDS,
1930                                     new ArrayBlockingQueue<Runnable>(10));
1931          try {
1932 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1933 <            l.add(new StringTask());
1934 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1935 <            l.add(new StringTask());
1936 <            List<Future<String>> futures =
1937 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1938 <            assertEquals(l.size(), futures.size());
1939 <            for (Future future : futures)
1940 <                assertTrue(future.isDone());
1941 <            assertFalse(futures.get(0).isCancelled());
1942 <            assertTrue(futures.get(1).isCancelled());
1932 >            for (long timeout = timeoutMillis();;) {
1933 >                List<Callable<String>> tasks = new ArrayList<>();
1934 >                tasks.add(new StringTask("0"));
1935 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1936 >                tasks.add(new StringTask("2"));
1937 >                long startTime = System.nanoTime();
1938 >                List<Future<String>> futures =
1939 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1940 >                assertEquals(tasks.size(), futures.size());
1941 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1942 >                for (Future future : futures)
1943 >                    assertTrue(future.isDone());
1944 >                assertTrue(futures.get(1).isCancelled());
1945 >                try {
1946 >                    assertEquals("0", futures.get(0).get());
1947 >                    assertEquals("2", futures.get(2).get());
1948 >                    break;
1949 >                } catch (CancellationException retryWithLongerTimeout) {
1950 >                    timeout *= 2;
1951 >                    if (timeout >= LONG_DELAY_MS / 2)
1952 >                        fail("expected exactly one task to be cancelled");
1953 >                }
1954 >            }
1955          } finally {
1956              joinPool(e);
1957          }
# Line 1900 | Line 1997 | public class ThreadPoolExecutorTest exte
1997       * allowCoreThreadTimeOut(true) causes idle threads to time out
1998       */
1999      public void testAllowCoreThreadTimeOut_true() throws Exception {
2000 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2000 >        long keepAliveTime = timeoutMillis();
2001          final ThreadPoolExecutor p =
2002              new ThreadPoolExecutor(2, 10,
2003 <                                   coreThreadTimeOut, MILLISECONDS,
2003 >                                   keepAliveTime, MILLISECONDS,
2004                                     new ArrayBlockingQueue<Runnable>(10));
2005          final CountDownLatch threadStarted = new CountDownLatch(1);
2006          try {
# Line 1914 | Line 2011 | public class ThreadPoolExecutorTest exte
2011                      assertEquals(1, p.getPoolSize());
2012                  }});
2013              await(threadStarted);
2014 <            delay(coreThreadTimeOut);
2014 >            delay(keepAliveTime);
2015              long startTime = System.nanoTime();
2016              while (p.getPoolSize() > 0
2017                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1930 | Line 2027 | public class ThreadPoolExecutorTest exte
2027       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2028       */
2029      public void testAllowCoreThreadTimeOut_false() throws Exception {
2030 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2030 >        long keepAliveTime = timeoutMillis();
2031          final ThreadPoolExecutor p =
2032              new ThreadPoolExecutor(2, 10,
2033 <                                   coreThreadTimeOut, MILLISECONDS,
2033 >                                   keepAliveTime, MILLISECONDS,
2034                                     new ArrayBlockingQueue<Runnable>(10));
2035          final CountDownLatch threadStarted = new CountDownLatch(1);
2036          try {
# Line 1943 | Line 2040 | public class ThreadPoolExecutorTest exte
2040                      threadStarted.countDown();
2041                      assertTrue(p.getPoolSize() >= 1);
2042                  }});
2043 <            delay(2 * coreThreadTimeOut);
2043 >            delay(2 * keepAliveTime);
2044              assertTrue(p.getPoolSize() >= 1);
2045          } finally {
2046              joinPool(p);
# Line 1962 | Line 2059 | public class ThreadPoolExecutorTest exte
2059                  done.countDown();
2060              }};
2061          final ThreadPoolExecutor p =
2062 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2062 >            new ThreadPoolExecutor(1, 30,
2063 >                                   60, SECONDS,
2064                                     new ArrayBlockingQueue(30));
2065          try {
2066              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2075 | public class ThreadPoolExecutorTest exte
2075              // enough time to run all tasks
2076              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2077          } finally {
2078 <            p.shutdown();
2078 >            joinPool(p);
2079 >        }
2080 >    }
2081 >
2082 >    /**
2083 >     * get(cancelled task) throws CancellationException
2084 >     */
2085 >    public void testGet_cancelled() throws Exception {
2086 >        final ExecutorService e =
2087 >            new ThreadPoolExecutor(1, 1,
2088 >                                   LONG_DELAY_MS, MILLISECONDS,
2089 >                                   new LinkedBlockingQueue<Runnable>());
2090 >        try {
2091 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2092 >            final CountDownLatch done = new CountDownLatch(1);
2093 >            final List<Future<?>> futures = new ArrayList<>();
2094 >            for (int i = 0; i < 2; i++) {
2095 >                Runnable r = new CheckedRunnable() { public void realRun()
2096 >                                                         throws Throwable {
2097 >                    blockerStarted.countDown();
2098 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2099 >                }};
2100 >                futures.add(e.submit(r));
2101 >            }
2102 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2103 >            for (Future<?> future : futures) future.cancel(false);
2104 >            for (Future<?> future : futures) {
2105 >                try {
2106 >                    future.get();
2107 >                    shouldThrow();
2108 >                } catch (CancellationException success) {}
2109 >                try {
2110 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2111 >                    shouldThrow();
2112 >                } catch (CancellationException success) {}
2113 >                assertTrue(future.isCancelled());
2114 >                assertTrue(future.isDone());
2115 >            }
2116 >            done.countDown();
2117 >        } finally {
2118 >            joinPool(e);
2119          }
2120      }
2121  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines