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.66 by jsr166, Sun Oct 4 01:18:25 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 214 | Line 225 | public class ThreadPoolExecutorTest exte
225              new ThreadPoolExecutor(2, 2,
226                                     1000, MILLISECONDS,
227                                     new ArrayBlockingQueue<Runnable>(10));
228 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
228 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
229          joinPool(p);
230      }
231  
# Line 622 | Line 633 | public class ThreadPoolExecutorTest exte
633      }
634  
635      /**
636 <     * shutdownNow returns a list containing tasks that were not run
636 >     * shutdownNow returns a list containing tasks that were not run,
637 >     * and those tasks are drained from the queue
638       */
639 <    public void testShutdownNow() {
639 >    public void testShutdownNow() throws InterruptedException {
640 >        final int poolSize = 2;
641 >        final int count = 5;
642 >        final AtomicInteger ran = new AtomicInteger(0);
643          final ThreadPoolExecutor p =
644 <            new ThreadPoolExecutor(1, 1,
644 >            new ThreadPoolExecutor(poolSize, poolSize,
645                                     LONG_DELAY_MS, MILLISECONDS,
646                                     new ArrayBlockingQueue<Runnable>(10));
647 <        List l;
648 <        try {
649 <            for (int i = 0; i < 5; i++)
635 <                p.execute(new MediumPossiblyInterruptedRunnable());
636 <        }
637 <        finally {
647 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
648 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
649 >            threadsStarted.countDown();
650              try {
651 <                l = p.shutdownNow();
652 <            } catch (SecurityException ok) { return; }
651 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
652 >            } catch (InterruptedException success) {}
653 >            ran.getAndIncrement();
654 >        }};
655 >        for (int i = 0; i < count; i++)
656 >            p.execute(waiter);
657 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
658 >        assertEquals(poolSize, p.getActiveCount());
659 >        assertEquals(0, p.getCompletedTaskCount());
660 >        final List<Runnable> queuedTasks;
661 >        try {
662 >            queuedTasks = p.shutdownNow();
663 >        } catch (SecurityException ok) {
664 >            return; // Allowed in case test doesn't have privs
665          }
666          assertTrue(p.isShutdown());
667 <        assertTrue(l.size() <= 4);
667 >        assertTrue(p.getQueue().isEmpty());
668 >        assertEquals(count - poolSize, queuedTasks.size());
669 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
670 >        assertTrue(p.isTerminated());
671 >        assertEquals(poolSize, ran.get());
672 >        assertEquals(poolSize, p.getCompletedTaskCount());
673      }
674  
675      // Exception Tests
# Line 650 | Line 679 | public class ThreadPoolExecutorTest exte
679       */
680      public void testConstructor1() {
681          try {
682 <            new ThreadPoolExecutor(-1, 1,
654 <                                   LONG_DELAY_MS, MILLISECONDS,
682 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
683                                     new ArrayBlockingQueue<Runnable>(10));
684              shouldThrow();
685          } catch (IllegalArgumentException success) {}
# Line 662 | Line 690 | public class ThreadPoolExecutorTest exte
690       */
691      public void testConstructor2() {
692          try {
693 <            new ThreadPoolExecutor(1, -1,
666 <                                   LONG_DELAY_MS, MILLISECONDS,
693 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
694                                     new ArrayBlockingQueue<Runnable>(10));
695              shouldThrow();
696          } catch (IllegalArgumentException success) {}
# Line 674 | Line 701 | public class ThreadPoolExecutorTest exte
701       */
702      public void testConstructor3() {
703          try {
704 <            new ThreadPoolExecutor(1, 0,
678 <                                   LONG_DELAY_MS, MILLISECONDS,
704 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
705                                     new ArrayBlockingQueue<Runnable>(10));
706              shouldThrow();
707          } catch (IllegalArgumentException success) {}
# Line 686 | Line 712 | public class ThreadPoolExecutorTest exte
712       */
713      public void testConstructor4() {
714          try {
715 <            new ThreadPoolExecutor(1, 2,
690 <                                   -1L, MILLISECONDS,
715 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
716                                     new ArrayBlockingQueue<Runnable>(10));
717              shouldThrow();
718          } catch (IllegalArgumentException success) {}
# Line 698 | Line 723 | public class ThreadPoolExecutorTest exte
723       */
724      public void testConstructor5() {
725          try {
726 <            new ThreadPoolExecutor(2, 1,
702 <                                   LONG_DELAY_MS, MILLISECONDS,
726 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
727                                     new ArrayBlockingQueue<Runnable>(10));
728              shouldThrow();
729          } catch (IllegalArgumentException success) {}
# Line 710 | Line 734 | public class ThreadPoolExecutorTest exte
734       */
735      public void testConstructorNullPointerException() {
736          try {
737 <            new ThreadPoolExecutor(1, 2,
714 <                                   LONG_DELAY_MS, MILLISECONDS,
737 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
738                                     (BlockingQueue) null);
739              shouldThrow();
740          } catch (NullPointerException success) {}
# Line 722 | Line 745 | public class ThreadPoolExecutorTest exte
745       */
746      public void testConstructor6() {
747          try {
748 <            new ThreadPoolExecutor(-1, 1,
726 <                                   LONG_DELAY_MS, MILLISECONDS,
748 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
749                                     new ArrayBlockingQueue<Runnable>(10),
750                                     new SimpleThreadFactory());
751              shouldThrow();
# Line 735 | Line 757 | public class ThreadPoolExecutorTest exte
757       */
758      public void testConstructor7() {
759          try {
760 <            new ThreadPoolExecutor(1, -1,
739 <                                   LONG_DELAY_MS, MILLISECONDS,
760 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
761                                     new ArrayBlockingQueue<Runnable>(10),
762                                     new SimpleThreadFactory());
763              shouldThrow();
# Line 748 | Line 769 | public class ThreadPoolExecutorTest exte
769       */
770      public void testConstructor8() {
771          try {
772 <            new ThreadPoolExecutor(1, 0,
752 <                                   LONG_DELAY_MS, MILLISECONDS,
772 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
773                                     new ArrayBlockingQueue<Runnable>(10),
774                                     new SimpleThreadFactory());
775              shouldThrow();
# Line 761 | Line 781 | public class ThreadPoolExecutorTest exte
781       */
782      public void testConstructor9() {
783          try {
784 <            new ThreadPoolExecutor(1, 2,
765 <                                   -1L, MILLISECONDS,
784 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
785                                     new ArrayBlockingQueue<Runnable>(10),
786                                     new SimpleThreadFactory());
787              shouldThrow();
# Line 774 | Line 793 | public class ThreadPoolExecutorTest exte
793       */
794      public void testConstructor10() {
795          try {
796 <            new ThreadPoolExecutor(2, 1,
778 <                                   LONG_DELAY_MS, MILLISECONDS,
796 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
797                                     new ArrayBlockingQueue<Runnable>(10),
798                                     new SimpleThreadFactory());
799              shouldThrow();
# Line 787 | Line 805 | public class ThreadPoolExecutorTest exte
805       */
806      public void testConstructorNullPointerException2() {
807          try {
808 <            new ThreadPoolExecutor(1, 2,
791 <                                   LONG_DELAY_MS, MILLISECONDS,
808 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
809                                     (BlockingQueue) null,
810                                     new SimpleThreadFactory());
811              shouldThrow();
# Line 800 | Line 817 | public class ThreadPoolExecutorTest exte
817       */
818      public void testConstructorNullPointerException3() {
819          try {
820 <            new ThreadPoolExecutor(1, 2,
804 <                                   LONG_DELAY_MS, MILLISECONDS,
820 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
821                                     new ArrayBlockingQueue<Runnable>(10),
822                                     (ThreadFactory) null);
823              shouldThrow();
# Line 813 | Line 829 | public class ThreadPoolExecutorTest exte
829       */
830      public void testConstructor11() {
831          try {
832 <            new ThreadPoolExecutor(-1, 1,
817 <                                   LONG_DELAY_MS, MILLISECONDS,
832 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
833                                     new ArrayBlockingQueue<Runnable>(10),
834                                     new NoOpREHandler());
835              shouldThrow();
# Line 826 | Line 841 | public class ThreadPoolExecutorTest exte
841       */
842      public void testConstructor12() {
843          try {
844 <            new ThreadPoolExecutor(1, -1,
830 <                                   LONG_DELAY_MS, MILLISECONDS,
844 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
845                                     new ArrayBlockingQueue<Runnable>(10),
846                                     new NoOpREHandler());
847              shouldThrow();
# Line 839 | Line 853 | public class ThreadPoolExecutorTest exte
853       */
854      public void testConstructor13() {
855          try {
856 <            new ThreadPoolExecutor(1, 0,
843 <                                   LONG_DELAY_MS, MILLISECONDS,
856 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
857                                     new ArrayBlockingQueue<Runnable>(10),
858                                     new NoOpREHandler());
859              shouldThrow();
# Line 852 | Line 865 | public class ThreadPoolExecutorTest exte
865       */
866      public void testConstructor14() {
867          try {
868 <            new ThreadPoolExecutor(1, 2,
856 <                                   -1L, MILLISECONDS,
868 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
869                                     new ArrayBlockingQueue<Runnable>(10),
870                                     new NoOpREHandler());
871              shouldThrow();
# Line 865 | Line 877 | public class ThreadPoolExecutorTest exte
877       */
878      public void testConstructor15() {
879          try {
880 <            new ThreadPoolExecutor(2, 1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
880 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
881                                     new ArrayBlockingQueue<Runnable>(10),
882                                     new NoOpREHandler());
883              shouldThrow();
# Line 878 | Line 889 | public class ThreadPoolExecutorTest exte
889       */
890      public void testConstructorNullPointerException4() {
891          try {
892 <            new ThreadPoolExecutor(1, 2,
882 <                                   LONG_DELAY_MS, MILLISECONDS,
892 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
893                                     (BlockingQueue) null,
894                                     new NoOpREHandler());
895              shouldThrow();
# Line 891 | Line 901 | public class ThreadPoolExecutorTest exte
901       */
902      public void testConstructorNullPointerException5() {
903          try {
904 <            new ThreadPoolExecutor(1, 2,
895 <                                   LONG_DELAY_MS, MILLISECONDS,
904 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
905                                     new ArrayBlockingQueue<Runnable>(10),
906                                     (RejectedExecutionHandler) null);
907              shouldThrow();
# Line 904 | Line 913 | public class ThreadPoolExecutorTest exte
913       */
914      public void testConstructor16() {
915          try {
916 <            new ThreadPoolExecutor(-1, 1,
908 <                                   LONG_DELAY_MS, MILLISECONDS,
916 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
917                                     new ArrayBlockingQueue<Runnable>(10),
918                                     new SimpleThreadFactory(),
919                                     new NoOpREHandler());
# Line 918 | Line 926 | public class ThreadPoolExecutorTest exte
926       */
927      public void testConstructor17() {
928          try {
929 <            new ThreadPoolExecutor(1, -1,
922 <                                   LONG_DELAY_MS, MILLISECONDS,
929 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
930                                     new ArrayBlockingQueue<Runnable>(10),
931                                     new SimpleThreadFactory(),
932                                     new NoOpREHandler());
# Line 932 | Line 939 | public class ThreadPoolExecutorTest exte
939       */
940      public void testConstructor18() {
941          try {
942 <            new ThreadPoolExecutor(1, 0,
936 <                                   LONG_DELAY_MS, MILLISECONDS,
942 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
943                                     new ArrayBlockingQueue<Runnable>(10),
944                                     new SimpleThreadFactory(),
945                                     new NoOpREHandler());
# Line 946 | Line 952 | public class ThreadPoolExecutorTest exte
952       */
953      public void testConstructor19() {
954          try {
955 <            new ThreadPoolExecutor(1, 2,
950 <                                   -1L, MILLISECONDS,
955 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
956                                     new ArrayBlockingQueue<Runnable>(10),
957                                     new SimpleThreadFactory(),
958                                     new NoOpREHandler());
# Line 960 | Line 965 | public class ThreadPoolExecutorTest exte
965       */
966      public void testConstructor20() {
967          try {
968 <            new ThreadPoolExecutor(2, 1,
964 <                                   LONG_DELAY_MS, MILLISECONDS,
968 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
969                                     new ArrayBlockingQueue<Runnable>(10),
970                                     new SimpleThreadFactory(),
971                                     new NoOpREHandler());
# Line 974 | Line 978 | public class ThreadPoolExecutorTest exte
978       */
979      public void testConstructorNullPointerException6() {
980          try {
981 <            new ThreadPoolExecutor(1, 2,
978 <                                   LONG_DELAY_MS, MILLISECONDS,
981 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
982                                     (BlockingQueue) null,
983                                     new SimpleThreadFactory(),
984                                     new NoOpREHandler());
# Line 988 | Line 991 | public class ThreadPoolExecutorTest exte
991       */
992      public void testConstructorNullPointerException7() {
993          try {
994 <            new ThreadPoolExecutor(1, 2,
992 <                                   LONG_DELAY_MS, MILLISECONDS,
994 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
995                                     new ArrayBlockingQueue<Runnable>(10),
996                                     new SimpleThreadFactory(),
997                                     (RejectedExecutionHandler) null);
# Line 1002 | Line 1004 | public class ThreadPoolExecutorTest exte
1004       */
1005      public void testConstructorNullPointerException8() {
1006          try {
1007 <            new ThreadPoolExecutor(1, 2,
1006 <                                   LONG_DELAY_MS, MILLISECONDS,
1007 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1008                                     new ArrayBlockingQueue<Runnable>(10),
1009                                     (ThreadFactory) null,
1010                                     new NoOpREHandler());
# Line 1017 | Line 1018 | public class ThreadPoolExecutorTest exte
1018      public void testInterruptedSubmit() throws InterruptedException {
1019          final ThreadPoolExecutor p =
1020              new ThreadPoolExecutor(1, 1,
1021 <                                   60, TimeUnit.SECONDS,
1021 >                                   60, SECONDS,
1022                                     new ArrayBlockingQueue<Runnable>(10));
1023  
1024          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1291 | Line 1292 | public class ThreadPoolExecutorTest exte
1292       */
1293      public void testExecuteNull() {
1294          ThreadPoolExecutor p =
1295 <            new ThreadPoolExecutor(1, 2,
1295 <                                   LONG_DELAY_MS, MILLISECONDS,
1295 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1296                                     new ArrayBlockingQueue<Runnable>(10));
1297          try {
1298              p.execute(null);
# Line 1359 | Line 1359 | public class ThreadPoolExecutorTest exte
1359      }
1360  
1361      /**
1362 +     * Configuration changes that allow core pool size greater than
1363 +     * max pool size result in IllegalArgumentException.
1364 +     */
1365 +    public void testPoolSizeInvariants() {
1366 +        ThreadPoolExecutor p =
1367 +            new ThreadPoolExecutor(1, 1,
1368 +                                   LONG_DELAY_MS, MILLISECONDS,
1369 +                                   new ArrayBlockingQueue<Runnable>(10));
1370 +        for (int s = 1; s < 5; s++) {
1371 +            p.setMaximumPoolSize(s);
1372 +            p.setCorePoolSize(s);
1373 +            try {
1374 +                p.setMaximumPoolSize(s - 1);
1375 +                shouldThrow();
1376 +            } catch (IllegalArgumentException success) {}
1377 +            assertEquals(s, p.getCorePoolSize());
1378 +            assertEquals(s, p.getMaximumPoolSize());
1379 +            try {
1380 +                p.setCorePoolSize(s + 1);
1381 +                shouldThrow();
1382 +            } catch (IllegalArgumentException success) {}
1383 +            assertEquals(s, p.getCorePoolSize());
1384 +            assertEquals(s, p.getMaximumPoolSize());
1385 +        }
1386 +        joinPool(p);
1387 +    }
1388 +
1389 +    /**
1390       * setKeepAliveTime throws IllegalArgumentException
1391       * when given a negative value
1392       */
# Line 1874 | Line 1902 | public class ThreadPoolExecutorTest exte
1902              l.add(new StringTask());
1903              l.add(new StringTask());
1904              List<Future<String>> futures =
1905 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1905 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1906              assertEquals(2, futures.size());
1907              for (Future<String> future : futures)
1908                  assertSame(TEST_STRING, future.get());
# Line 1892 | Line 1920 | public class ThreadPoolExecutorTest exte
1920                                     LONG_DELAY_MS, MILLISECONDS,
1921                                     new ArrayBlockingQueue<Runnable>(10));
1922          try {
1923 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1924 <            l.add(new StringTask());
1925 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1926 <            l.add(new StringTask());
1927 <            List<Future<String>> futures =
1928 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1929 <            assertEquals(l.size(), futures.size());
1930 <            for (Future future : futures)
1931 <                assertTrue(future.isDone());
1932 <            assertFalse(futures.get(0).isCancelled());
1933 <            assertTrue(futures.get(1).isCancelled());
1923 >            for (long timeout = timeoutMillis();;) {
1924 >                List<Callable<String>> tasks = new ArrayList<>();
1925 >                tasks.add(new StringTask("0"));
1926 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1927 >                tasks.add(new StringTask("2"));
1928 >                long startTime = System.nanoTime();
1929 >                List<Future<String>> futures =
1930 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1931 >                assertEquals(tasks.size(), futures.size());
1932 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1933 >                for (Future future : futures)
1934 >                    assertTrue(future.isDone());
1935 >                assertTrue(futures.get(1).isCancelled());
1936 >                try {
1937 >                    assertEquals("0", futures.get(0).get());
1938 >                    assertEquals("2", futures.get(2).get());
1939 >                    break;
1940 >                } catch (CancellationException retryWithLongerTimeout) {
1941 >                    timeout *= 2;
1942 >                    if (timeout >= LONG_DELAY_MS / 2)
1943 >                        fail("expected exactly one task to be cancelled");
1944 >                }
1945 >            }
1946          } finally {
1947              joinPool(e);
1948          }
# Line 1948 | Line 1988 | public class ThreadPoolExecutorTest exte
1988       * allowCoreThreadTimeOut(true) causes idle threads to time out
1989       */
1990      public void testAllowCoreThreadTimeOut_true() throws Exception {
1991 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1991 >        long keepAliveTime = timeoutMillis();
1992          final ThreadPoolExecutor p =
1993              new ThreadPoolExecutor(2, 10,
1994 <                                   coreThreadTimeOut, MILLISECONDS,
1994 >                                   keepAliveTime, MILLISECONDS,
1995                                     new ArrayBlockingQueue<Runnable>(10));
1996          final CountDownLatch threadStarted = new CountDownLatch(1);
1997          try {
# Line 1962 | Line 2002 | public class ThreadPoolExecutorTest exte
2002                      assertEquals(1, p.getPoolSize());
2003                  }});
2004              await(threadStarted);
2005 <            delay(coreThreadTimeOut);
2005 >            delay(keepAliveTime);
2006              long startTime = System.nanoTime();
2007              while (p.getPoolSize() > 0
2008                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1978 | Line 2018 | public class ThreadPoolExecutorTest exte
2018       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2019       */
2020      public void testAllowCoreThreadTimeOut_false() throws Exception {
2021 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2021 >        long keepAliveTime = timeoutMillis();
2022          final ThreadPoolExecutor p =
2023              new ThreadPoolExecutor(2, 10,
2024 <                                   coreThreadTimeOut, MILLISECONDS,
2024 >                                   keepAliveTime, MILLISECONDS,
2025                                     new ArrayBlockingQueue<Runnable>(10));
2026          final CountDownLatch threadStarted = new CountDownLatch(1);
2027          try {
# Line 1991 | Line 2031 | public class ThreadPoolExecutorTest exte
2031                      threadStarted.countDown();
2032                      assertTrue(p.getPoolSize() >= 1);
2033                  }});
2034 <            delay(2 * coreThreadTimeOut);
2034 >            delay(2 * keepAliveTime);
2035              assertTrue(p.getPoolSize() >= 1);
2036          } finally {
2037              joinPool(p);
# Line 2010 | Line 2050 | public class ThreadPoolExecutorTest exte
2050                  done.countDown();
2051              }};
2052          final ThreadPoolExecutor p =
2053 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2053 >            new ThreadPoolExecutor(1, 30,
2054 >                                   60, SECONDS,
2055                                     new ArrayBlockingQueue(30));
2056          try {
2057              for (int i = 0; i < nTasks; ++i) {
# Line 2029 | Line 2070 | public class ThreadPoolExecutorTest exte
2070          }
2071      }
2072  
2073 +    /**
2074 +     * get(cancelled task) throws CancellationException
2075 +     */
2076 +    public void testGet_cancelled() throws Exception {
2077 +        final ExecutorService e =
2078 +            new ThreadPoolExecutor(1, 1,
2079 +                                   LONG_DELAY_MS, MILLISECONDS,
2080 +                                   new LinkedBlockingQueue<Runnable>());
2081 +        try {
2082 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
2083 +            final CountDownLatch done = new CountDownLatch(1);
2084 +            final List<Future<?>> futures = new ArrayList<>();
2085 +            for (int i = 0; i < 2; i++) {
2086 +                Runnable r = new CheckedRunnable() { public void realRun()
2087 +                                                         throws Throwable {
2088 +                    blockerStarted.countDown();
2089 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2090 +                }};
2091 +                futures.add(e.submit(r));
2092 +            }
2093 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2094 +            for (Future<?> future : futures) future.cancel(false);
2095 +            for (Future<?> future : futures) {
2096 +                try {
2097 +                    future.get();
2098 +                    shouldThrow();
2099 +                } catch (CancellationException success) {}
2100 +                try {
2101 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
2102 +                    shouldThrow();
2103 +                } catch (CancellationException success) {}
2104 +                assertTrue(future.isCancelled());
2105 +                assertTrue(future.isDone());
2106 +            }
2107 +            done.countDown();
2108 +        } finally {
2109 +            joinPool(e);
2110 +        }
2111 +    }
2112 +
2113   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines