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.50 by jsr166, Wed Dec 31 19:05:43 2014 UTC vs.
Revision 1.72 by jsr166, Sun Oct 4 01:52:43 2015 UTC

# Line 8 | Line 8
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
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;
# Line 27 | Line 29 | import java.util.concurrent.SynchronousQ
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 129 | 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 147 | 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 167 | 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 189 | Line 206 | public class ThreadPoolExecutorTest exte
206                      fail("timed out");
207                  Thread.yield();
208              }
192        } finally {
193            joinPool(p);
209          }
210      }
211  
# Line 202 | 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 214 | 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 241 | 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 255 | 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 {
263 <            joinPool(p);
277 >        try (PoolCleaner cleaner = cleaner(p)) {
278 >            try {
279 >                p.setThreadFactory(null);
280 >                shouldThrow();
281 >            } catch (NullPointerException success) {}
282          }
283      }
284  
# Line 622 | 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++)
635 <                p.execute(new MediumPossiblyInterruptedRunnable());
636 <        }
637 <        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
# Line 650 | Line 686 | public class ThreadPoolExecutorTest exte
686       */
687      public void testConstructor1() {
688          try {
689 <            new ThreadPoolExecutor(-1, 1,
654 <                                   LONG_DELAY_MS, MILLISECONDS,
689 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
690                                     new ArrayBlockingQueue<Runnable>(10));
691              shouldThrow();
692          } catch (IllegalArgumentException success) {}
# Line 662 | Line 697 | public class ThreadPoolExecutorTest exte
697       */
698      public void testConstructor2() {
699          try {
700 <            new ThreadPoolExecutor(1, -1,
666 <                                   LONG_DELAY_MS, MILLISECONDS,
700 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
701                                     new ArrayBlockingQueue<Runnable>(10));
702              shouldThrow();
703          } catch (IllegalArgumentException success) {}
# Line 674 | Line 708 | public class ThreadPoolExecutorTest exte
708       */
709      public void testConstructor3() {
710          try {
711 <            new ThreadPoolExecutor(1, 0,
678 <                                   LONG_DELAY_MS, MILLISECONDS,
711 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
712                                     new ArrayBlockingQueue<Runnable>(10));
713              shouldThrow();
714          } catch (IllegalArgumentException success) {}
# Line 686 | Line 719 | public class ThreadPoolExecutorTest exte
719       */
720      public void testConstructor4() {
721          try {
722 <            new ThreadPoolExecutor(1, 2,
690 <                                   -1L, MILLISECONDS,
722 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
723                                     new ArrayBlockingQueue<Runnable>(10));
724              shouldThrow();
725          } catch (IllegalArgumentException success) {}
# Line 698 | Line 730 | public class ThreadPoolExecutorTest exte
730       */
731      public void testConstructor5() {
732          try {
733 <            new ThreadPoolExecutor(2, 1,
702 <                                   LONG_DELAY_MS, MILLISECONDS,
733 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
734                                     new ArrayBlockingQueue<Runnable>(10));
735              shouldThrow();
736          } catch (IllegalArgumentException success) {}
# Line 710 | Line 741 | public class ThreadPoolExecutorTest exte
741       */
742      public void testConstructorNullPointerException() {
743          try {
744 <            new ThreadPoolExecutor(1, 2,
714 <                                   LONG_DELAY_MS, MILLISECONDS,
744 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
745                                     (BlockingQueue) null);
746              shouldThrow();
747          } catch (NullPointerException success) {}
# Line 722 | Line 752 | public class ThreadPoolExecutorTest exte
752       */
753      public void testConstructor6() {
754          try {
755 <            new ThreadPoolExecutor(-1, 1,
726 <                                   LONG_DELAY_MS, MILLISECONDS,
755 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
756                                     new ArrayBlockingQueue<Runnable>(10),
757                                     new SimpleThreadFactory());
758              shouldThrow();
# Line 735 | Line 764 | public class ThreadPoolExecutorTest exte
764       */
765      public void testConstructor7() {
766          try {
767 <            new ThreadPoolExecutor(1, -1,
739 <                                   LONG_DELAY_MS, MILLISECONDS,
767 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
768                                     new ArrayBlockingQueue<Runnable>(10),
769                                     new SimpleThreadFactory());
770              shouldThrow();
# Line 748 | Line 776 | public class ThreadPoolExecutorTest exte
776       */
777      public void testConstructor8() {
778          try {
779 <            new ThreadPoolExecutor(1, 0,
752 <                                   LONG_DELAY_MS, MILLISECONDS,
779 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
780                                     new ArrayBlockingQueue<Runnable>(10),
781                                     new SimpleThreadFactory());
782              shouldThrow();
# Line 761 | Line 788 | public class ThreadPoolExecutorTest exte
788       */
789      public void testConstructor9() {
790          try {
791 <            new ThreadPoolExecutor(1, 2,
765 <                                   -1L, MILLISECONDS,
791 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
792                                     new ArrayBlockingQueue<Runnable>(10),
793                                     new SimpleThreadFactory());
794              shouldThrow();
# Line 774 | Line 800 | public class ThreadPoolExecutorTest exte
800       */
801      public void testConstructor10() {
802          try {
803 <            new ThreadPoolExecutor(2, 1,
778 <                                   LONG_DELAY_MS, MILLISECONDS,
803 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
804                                     new ArrayBlockingQueue<Runnable>(10),
805                                     new SimpleThreadFactory());
806              shouldThrow();
# Line 787 | Line 812 | public class ThreadPoolExecutorTest exte
812       */
813      public void testConstructorNullPointerException2() {
814          try {
815 <            new ThreadPoolExecutor(1, 2,
791 <                                   LONG_DELAY_MS, MILLISECONDS,
815 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
816                                     (BlockingQueue) null,
817                                     new SimpleThreadFactory());
818              shouldThrow();
# Line 800 | Line 824 | public class ThreadPoolExecutorTest exte
824       */
825      public void testConstructorNullPointerException3() {
826          try {
827 <            new ThreadPoolExecutor(1, 2,
804 <                                   LONG_DELAY_MS, MILLISECONDS,
827 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
828                                     new ArrayBlockingQueue<Runnable>(10),
829                                     (ThreadFactory) null);
830              shouldThrow();
# Line 813 | Line 836 | public class ThreadPoolExecutorTest exte
836       */
837      public void testConstructor11() {
838          try {
839 <            new ThreadPoolExecutor(-1, 1,
817 <                                   LONG_DELAY_MS, MILLISECONDS,
839 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
840                                     new ArrayBlockingQueue<Runnable>(10),
841                                     new NoOpREHandler());
842              shouldThrow();
# Line 826 | Line 848 | public class ThreadPoolExecutorTest exte
848       */
849      public void testConstructor12() {
850          try {
851 <            new ThreadPoolExecutor(1, -1,
830 <                                   LONG_DELAY_MS, MILLISECONDS,
851 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
852                                     new ArrayBlockingQueue<Runnable>(10),
853                                     new NoOpREHandler());
854              shouldThrow();
# Line 839 | Line 860 | public class ThreadPoolExecutorTest exte
860       */
861      public void testConstructor13() {
862          try {
863 <            new ThreadPoolExecutor(1, 0,
843 <                                   LONG_DELAY_MS, MILLISECONDS,
863 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
864                                     new ArrayBlockingQueue<Runnable>(10),
865                                     new NoOpREHandler());
866              shouldThrow();
# Line 852 | Line 872 | public class ThreadPoolExecutorTest exte
872       */
873      public void testConstructor14() {
874          try {
875 <            new ThreadPoolExecutor(1, 2,
856 <                                   -1L, MILLISECONDS,
875 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
876                                     new ArrayBlockingQueue<Runnable>(10),
877                                     new NoOpREHandler());
878              shouldThrow();
# Line 865 | Line 884 | public class ThreadPoolExecutorTest exte
884       */
885      public void testConstructor15() {
886          try {
887 <            new ThreadPoolExecutor(2, 1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
887 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
888                                     new ArrayBlockingQueue<Runnable>(10),
889                                     new NoOpREHandler());
890              shouldThrow();
# Line 878 | Line 896 | public class ThreadPoolExecutorTest exte
896       */
897      public void testConstructorNullPointerException4() {
898          try {
899 <            new ThreadPoolExecutor(1, 2,
882 <                                   LONG_DELAY_MS, MILLISECONDS,
899 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
900                                     (BlockingQueue) null,
901                                     new NoOpREHandler());
902              shouldThrow();
# Line 891 | Line 908 | public class ThreadPoolExecutorTest exte
908       */
909      public void testConstructorNullPointerException5() {
910          try {
911 <            new ThreadPoolExecutor(1, 2,
895 <                                   LONG_DELAY_MS, MILLISECONDS,
911 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
912                                     new ArrayBlockingQueue<Runnable>(10),
913                                     (RejectedExecutionHandler) null);
914              shouldThrow();
# Line 904 | Line 920 | public class ThreadPoolExecutorTest exte
920       */
921      public void testConstructor16() {
922          try {
923 <            new ThreadPoolExecutor(-1, 1,
908 <                                   LONG_DELAY_MS, MILLISECONDS,
923 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
924                                     new ArrayBlockingQueue<Runnable>(10),
925                                     new SimpleThreadFactory(),
926                                     new NoOpREHandler());
# Line 918 | Line 933 | public class ThreadPoolExecutorTest exte
933       */
934      public void testConstructor17() {
935          try {
936 <            new ThreadPoolExecutor(1, -1,
922 <                                   LONG_DELAY_MS, MILLISECONDS,
936 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
937                                     new ArrayBlockingQueue<Runnable>(10),
938                                     new SimpleThreadFactory(),
939                                     new NoOpREHandler());
# Line 932 | Line 946 | public class ThreadPoolExecutorTest exte
946       */
947      public void testConstructor18() {
948          try {
949 <            new ThreadPoolExecutor(1, 0,
936 <                                   LONG_DELAY_MS, MILLISECONDS,
949 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
950                                     new ArrayBlockingQueue<Runnable>(10),
951                                     new SimpleThreadFactory(),
952                                     new NoOpREHandler());
# Line 946 | Line 959 | public class ThreadPoolExecutorTest exte
959       */
960      public void testConstructor19() {
961          try {
962 <            new ThreadPoolExecutor(1, 2,
950 <                                   -1L, MILLISECONDS,
962 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
963                                     new ArrayBlockingQueue<Runnable>(10),
964                                     new SimpleThreadFactory(),
965                                     new NoOpREHandler());
# Line 960 | Line 972 | public class ThreadPoolExecutorTest exte
972       */
973      public void testConstructor20() {
974          try {
975 <            new ThreadPoolExecutor(2, 1,
964 <                                   LONG_DELAY_MS, MILLISECONDS,
975 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
976                                     new ArrayBlockingQueue<Runnable>(10),
977                                     new SimpleThreadFactory(),
978                                     new NoOpREHandler());
# Line 974 | Line 985 | public class ThreadPoolExecutorTest exte
985       */
986      public void testConstructorNullPointerException6() {
987          try {
988 <            new ThreadPoolExecutor(1, 2,
978 <                                   LONG_DELAY_MS, MILLISECONDS,
988 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
989                                     (BlockingQueue) null,
990                                     new SimpleThreadFactory(),
991                                     new NoOpREHandler());
# Line 988 | Line 998 | public class ThreadPoolExecutorTest exte
998       */
999      public void testConstructorNullPointerException7() {
1000          try {
1001 <            new ThreadPoolExecutor(1, 2,
992 <                                   LONG_DELAY_MS, MILLISECONDS,
1001 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1002                                     new ArrayBlockingQueue<Runnable>(10),
1003                                     new SimpleThreadFactory(),
1004                                     (RejectedExecutionHandler) null);
# Line 1002 | Line 1011 | public class ThreadPoolExecutorTest exte
1011       */
1012      public void testConstructorNullPointerException8() {
1013          try {
1014 <            new ThreadPoolExecutor(1, 2,
1006 <                                   LONG_DELAY_MS, MILLISECONDS,
1014 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1015                                     new ArrayBlockingQueue<Runnable>(10),
1016                                     (ThreadFactory) null,
1017                                     new NoOpREHandler());
# Line 1017 | 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 1291 | Line 1299 | public class ThreadPoolExecutorTest exte
1299       */
1300      public void testExecuteNull() {
1301          ThreadPoolExecutor p =
1302 <            new ThreadPoolExecutor(1, 2,
1295 <                                   LONG_DELAY_MS, MILLISECONDS,
1302 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1303                                     new ArrayBlockingQueue<Runnable>(10));
1304          try {
1305              p.execute(null);
# Line 1359 | Line 1366 | public class ThreadPoolExecutorTest exte
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
1398       * when given a negative value
1399       */
# Line 1874 | 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 1892 | 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(l.size(), futures.size());
1937 <            for (Future future : futures)
1938 <                assertTrue(future.isDone());
1939 <            assertFalse(futures.get(0).isCancelled());
1940 <            assertTrue(futures.get(1).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 1948 | 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 coreThreadTimeOut = SHORT_DELAY_MS;
1998 >        long keepAliveTime = timeoutMillis();
1999          final ThreadPoolExecutor p =
2000              new ThreadPoolExecutor(2, 10,
2001 <                                   coreThreadTimeOut, MILLISECONDS,
2001 >                                   keepAliveTime, MILLISECONDS,
2002                                     new ArrayBlockingQueue<Runnable>(10));
2003          final CountDownLatch threadStarted = new CountDownLatch(1);
2004          try {
# Line 1962 | Line 2009 | public class ThreadPoolExecutorTest exte
2009                      assertEquals(1, p.getPoolSize());
2010                  }});
2011              await(threadStarted);
2012 <            delay(coreThreadTimeOut);
2012 >            delay(keepAliveTime);
2013              long startTime = System.nanoTime();
2014              while (p.getPoolSize() > 0
2015                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1978 | 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 coreThreadTimeOut = SHORT_DELAY_MS;
2028 >        long keepAliveTime = timeoutMillis();
2029          final ThreadPoolExecutor p =
2030              new ThreadPoolExecutor(2, 10,
2031 <                                   coreThreadTimeOut, MILLISECONDS,
2031 >                                   keepAliveTime, MILLISECONDS,
2032                                     new ArrayBlockingQueue<Runnable>(10));
2033          final CountDownLatch threadStarted = new CountDownLatch(1);
2034          try {
# Line 1991 | Line 2038 | public class ThreadPoolExecutorTest exte
2038                      threadStarted.countDown();
2039                      assertTrue(p.getPoolSize() >= 1);
2040                  }});
2041 <            delay(2 * coreThreadTimeOut);
2041 >            delay(2 * keepAliveTime);
2042              assertTrue(p.getPoolSize() >= 1);
2043          } finally {
2044              joinPool(p);
# Line 2010 | 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 2029 | Line 2077 | public class ThreadPoolExecutorTest exte
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 +
2120   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines