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.67 by jsr166, Sun Oct 4 01:23:41 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 214 | Line 231 | public class ThreadPoolExecutorTest exte
231              new ThreadPoolExecutor(2, 2,
232                                     1000, MILLISECONDS,
233                                     new ArrayBlockingQueue<Runnable>(10));
234 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
234 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
235          joinPool(p);
236      }
237  
# Line 622 | Line 639 | public class ThreadPoolExecutorTest exte
639      }
640  
641      /**
642 <     * shutdownNow returns a list containing tasks that were not run
642 >     * shutdownNow returns a list containing tasks that were not run,
643 >     * and those tasks are drained from the queue
644       */
645 <    public void testShutdownNow() {
645 >    public void testShutdownNow() throws InterruptedException {
646 >        final int poolSize = 2;
647 >        final int count = 5;
648 >        final AtomicInteger ran = new AtomicInteger(0);
649          final ThreadPoolExecutor p =
650 <            new ThreadPoolExecutor(1, 1,
650 >            new ThreadPoolExecutor(poolSize, poolSize,
651                                     LONG_DELAY_MS, MILLISECONDS,
652                                     new ArrayBlockingQueue<Runnable>(10));
653 <        List l;
654 <        try {
655 <            for (int i = 0; i < 5; i++)
635 <                p.execute(new MediumPossiblyInterruptedRunnable());
636 <        }
637 <        finally {
653 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
654 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
655 >            threadsStarted.countDown();
656              try {
657 <                l = p.shutdownNow();
658 <            } catch (SecurityException ok) { return; }
657 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
658 >            } catch (InterruptedException success) {}
659 >            ran.getAndIncrement();
660 >        }};
661 >        for (int i = 0; i < count; i++)
662 >            p.execute(waiter);
663 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
664 >        assertEquals(poolSize, p.getActiveCount());
665 >        assertEquals(0, p.getCompletedTaskCount());
666 >        final List<Runnable> queuedTasks;
667 >        try {
668 >            queuedTasks = p.shutdownNow();
669 >        } catch (SecurityException ok) {
670 >            return; // Allowed in case test doesn't have privs
671          }
672          assertTrue(p.isShutdown());
673 <        assertTrue(l.size() <= 4);
673 >        assertTrue(p.getQueue().isEmpty());
674 >        assertEquals(count - poolSize, queuedTasks.size());
675 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
676 >        assertTrue(p.isTerminated());
677 >        assertEquals(poolSize, ran.get());
678 >        assertEquals(poolSize, p.getCompletedTaskCount());
679      }
680  
681      // Exception Tests
# Line 650 | Line 685 | public class ThreadPoolExecutorTest exte
685       */
686      public void testConstructor1() {
687          try {
688 <            new ThreadPoolExecutor(-1, 1,
654 <                                   LONG_DELAY_MS, MILLISECONDS,
688 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
689                                     new ArrayBlockingQueue<Runnable>(10));
690              shouldThrow();
691          } catch (IllegalArgumentException success) {}
# Line 662 | Line 696 | public class ThreadPoolExecutorTest exte
696       */
697      public void testConstructor2() {
698          try {
699 <            new ThreadPoolExecutor(1, -1,
666 <                                   LONG_DELAY_MS, MILLISECONDS,
699 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
700                                     new ArrayBlockingQueue<Runnable>(10));
701              shouldThrow();
702          } catch (IllegalArgumentException success) {}
# Line 674 | Line 707 | public class ThreadPoolExecutorTest exte
707       */
708      public void testConstructor3() {
709          try {
710 <            new ThreadPoolExecutor(1, 0,
678 <                                   LONG_DELAY_MS, MILLISECONDS,
710 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
711                                     new ArrayBlockingQueue<Runnable>(10));
712              shouldThrow();
713          } catch (IllegalArgumentException success) {}
# Line 686 | Line 718 | public class ThreadPoolExecutorTest exte
718       */
719      public void testConstructor4() {
720          try {
721 <            new ThreadPoolExecutor(1, 2,
690 <                                   -1L, MILLISECONDS,
721 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
722                                     new ArrayBlockingQueue<Runnable>(10));
723              shouldThrow();
724          } catch (IllegalArgumentException success) {}
# Line 698 | Line 729 | public class ThreadPoolExecutorTest exte
729       */
730      public void testConstructor5() {
731          try {
732 <            new ThreadPoolExecutor(2, 1,
702 <                                   LONG_DELAY_MS, MILLISECONDS,
732 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
733                                     new ArrayBlockingQueue<Runnable>(10));
734              shouldThrow();
735          } catch (IllegalArgumentException success) {}
# Line 710 | Line 740 | public class ThreadPoolExecutorTest exte
740       */
741      public void testConstructorNullPointerException() {
742          try {
743 <            new ThreadPoolExecutor(1, 2,
714 <                                   LONG_DELAY_MS, MILLISECONDS,
743 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
744                                     (BlockingQueue) null);
745              shouldThrow();
746          } catch (NullPointerException success) {}
# Line 722 | Line 751 | public class ThreadPoolExecutorTest exte
751       */
752      public void testConstructor6() {
753          try {
754 <            new ThreadPoolExecutor(-1, 1,
726 <                                   LONG_DELAY_MS, MILLISECONDS,
754 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
755                                     new ArrayBlockingQueue<Runnable>(10),
756                                     new SimpleThreadFactory());
757              shouldThrow();
# Line 735 | Line 763 | public class ThreadPoolExecutorTest exte
763       */
764      public void testConstructor7() {
765          try {
766 <            new ThreadPoolExecutor(1, -1,
739 <                                   LONG_DELAY_MS, MILLISECONDS,
766 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
767                                     new ArrayBlockingQueue<Runnable>(10),
768                                     new SimpleThreadFactory());
769              shouldThrow();
# Line 748 | Line 775 | public class ThreadPoolExecutorTest exte
775       */
776      public void testConstructor8() {
777          try {
778 <            new ThreadPoolExecutor(1, 0,
752 <                                   LONG_DELAY_MS, MILLISECONDS,
778 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
779                                     new ArrayBlockingQueue<Runnable>(10),
780                                     new SimpleThreadFactory());
781              shouldThrow();
# Line 761 | Line 787 | public class ThreadPoolExecutorTest exte
787       */
788      public void testConstructor9() {
789          try {
790 <            new ThreadPoolExecutor(1, 2,
765 <                                   -1L, MILLISECONDS,
790 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
791                                     new ArrayBlockingQueue<Runnable>(10),
792                                     new SimpleThreadFactory());
793              shouldThrow();
# Line 774 | Line 799 | public class ThreadPoolExecutorTest exte
799       */
800      public void testConstructor10() {
801          try {
802 <            new ThreadPoolExecutor(2, 1,
778 <                                   LONG_DELAY_MS, MILLISECONDS,
802 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
803                                     new ArrayBlockingQueue<Runnable>(10),
804                                     new SimpleThreadFactory());
805              shouldThrow();
# Line 787 | Line 811 | public class ThreadPoolExecutorTest exte
811       */
812      public void testConstructorNullPointerException2() {
813          try {
814 <            new ThreadPoolExecutor(1, 2,
791 <                                   LONG_DELAY_MS, MILLISECONDS,
814 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
815                                     (BlockingQueue) null,
816                                     new SimpleThreadFactory());
817              shouldThrow();
# Line 800 | Line 823 | public class ThreadPoolExecutorTest exte
823       */
824      public void testConstructorNullPointerException3() {
825          try {
826 <            new ThreadPoolExecutor(1, 2,
804 <                                   LONG_DELAY_MS, MILLISECONDS,
826 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
827                                     new ArrayBlockingQueue<Runnable>(10),
828                                     (ThreadFactory) null);
829              shouldThrow();
# Line 813 | Line 835 | public class ThreadPoolExecutorTest exte
835       */
836      public void testConstructor11() {
837          try {
838 <            new ThreadPoolExecutor(-1, 1,
817 <                                   LONG_DELAY_MS, MILLISECONDS,
838 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
839                                     new ArrayBlockingQueue<Runnable>(10),
840                                     new NoOpREHandler());
841              shouldThrow();
# Line 826 | Line 847 | public class ThreadPoolExecutorTest exte
847       */
848      public void testConstructor12() {
849          try {
850 <            new ThreadPoolExecutor(1, -1,
830 <                                   LONG_DELAY_MS, MILLISECONDS,
850 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
851                                     new ArrayBlockingQueue<Runnable>(10),
852                                     new NoOpREHandler());
853              shouldThrow();
# Line 839 | Line 859 | public class ThreadPoolExecutorTest exte
859       */
860      public void testConstructor13() {
861          try {
862 <            new ThreadPoolExecutor(1, 0,
843 <                                   LONG_DELAY_MS, MILLISECONDS,
862 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
863                                     new ArrayBlockingQueue<Runnable>(10),
864                                     new NoOpREHandler());
865              shouldThrow();
# Line 852 | Line 871 | public class ThreadPoolExecutorTest exte
871       */
872      public void testConstructor14() {
873          try {
874 <            new ThreadPoolExecutor(1, 2,
856 <                                   -1L, MILLISECONDS,
874 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
875                                     new ArrayBlockingQueue<Runnable>(10),
876                                     new NoOpREHandler());
877              shouldThrow();
# Line 865 | Line 883 | public class ThreadPoolExecutorTest exte
883       */
884      public void testConstructor15() {
885          try {
886 <            new ThreadPoolExecutor(2, 1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
886 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
887                                     new ArrayBlockingQueue<Runnable>(10),
888                                     new NoOpREHandler());
889              shouldThrow();
# Line 878 | Line 895 | public class ThreadPoolExecutorTest exte
895       */
896      public void testConstructorNullPointerException4() {
897          try {
898 <            new ThreadPoolExecutor(1, 2,
882 <                                   LONG_DELAY_MS, MILLISECONDS,
898 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
899                                     (BlockingQueue) null,
900                                     new NoOpREHandler());
901              shouldThrow();
# Line 891 | Line 907 | public class ThreadPoolExecutorTest exte
907       */
908      public void testConstructorNullPointerException5() {
909          try {
910 <            new ThreadPoolExecutor(1, 2,
895 <                                   LONG_DELAY_MS, MILLISECONDS,
910 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
911                                     new ArrayBlockingQueue<Runnable>(10),
912                                     (RejectedExecutionHandler) null);
913              shouldThrow();
# Line 904 | Line 919 | public class ThreadPoolExecutorTest exte
919       */
920      public void testConstructor16() {
921          try {
922 <            new ThreadPoolExecutor(-1, 1,
908 <                                   LONG_DELAY_MS, MILLISECONDS,
922 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
923                                     new ArrayBlockingQueue<Runnable>(10),
924                                     new SimpleThreadFactory(),
925                                     new NoOpREHandler());
# Line 918 | Line 932 | public class ThreadPoolExecutorTest exte
932       */
933      public void testConstructor17() {
934          try {
935 <            new ThreadPoolExecutor(1, -1,
922 <                                   LONG_DELAY_MS, MILLISECONDS,
935 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
936                                     new ArrayBlockingQueue<Runnable>(10),
937                                     new SimpleThreadFactory(),
938                                     new NoOpREHandler());
# Line 932 | Line 945 | public class ThreadPoolExecutorTest exte
945       */
946      public void testConstructor18() {
947          try {
948 <            new ThreadPoolExecutor(1, 0,
936 <                                   LONG_DELAY_MS, MILLISECONDS,
948 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
949                                     new ArrayBlockingQueue<Runnable>(10),
950                                     new SimpleThreadFactory(),
951                                     new NoOpREHandler());
# Line 946 | Line 958 | public class ThreadPoolExecutorTest exte
958       */
959      public void testConstructor19() {
960          try {
961 <            new ThreadPoolExecutor(1, 2,
950 <                                   -1L, MILLISECONDS,
961 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
962                                     new ArrayBlockingQueue<Runnable>(10),
963                                     new SimpleThreadFactory(),
964                                     new NoOpREHandler());
# Line 960 | Line 971 | public class ThreadPoolExecutorTest exte
971       */
972      public void testConstructor20() {
973          try {
974 <            new ThreadPoolExecutor(2, 1,
964 <                                   LONG_DELAY_MS, MILLISECONDS,
974 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
975                                     new ArrayBlockingQueue<Runnable>(10),
976                                     new SimpleThreadFactory(),
977                                     new NoOpREHandler());
# Line 974 | Line 984 | public class ThreadPoolExecutorTest exte
984       */
985      public void testConstructorNullPointerException6() {
986          try {
987 <            new ThreadPoolExecutor(1, 2,
978 <                                   LONG_DELAY_MS, MILLISECONDS,
987 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
988                                     (BlockingQueue) null,
989                                     new SimpleThreadFactory(),
990                                     new NoOpREHandler());
# Line 988 | Line 997 | public class ThreadPoolExecutorTest exte
997       */
998      public void testConstructorNullPointerException7() {
999          try {
1000 <            new ThreadPoolExecutor(1, 2,
992 <                                   LONG_DELAY_MS, MILLISECONDS,
1000 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1001                                     new ArrayBlockingQueue<Runnable>(10),
1002                                     new SimpleThreadFactory(),
1003                                     (RejectedExecutionHandler) null);
# Line 1002 | Line 1010 | public class ThreadPoolExecutorTest exte
1010       */
1011      public void testConstructorNullPointerException8() {
1012          try {
1013 <            new ThreadPoolExecutor(1, 2,
1006 <                                   LONG_DELAY_MS, MILLISECONDS,
1013 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1014                                     new ArrayBlockingQueue<Runnable>(10),
1015                                     (ThreadFactory) null,
1016                                     new NoOpREHandler());
# Line 1017 | Line 1024 | public class ThreadPoolExecutorTest exte
1024      public void testInterruptedSubmit() throws InterruptedException {
1025          final ThreadPoolExecutor p =
1026              new ThreadPoolExecutor(1, 1,
1027 <                                   60, TimeUnit.SECONDS,
1027 >                                   60, SECONDS,
1028                                     new ArrayBlockingQueue<Runnable>(10));
1029  
1030          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1291 | Line 1298 | public class ThreadPoolExecutorTest exte
1298       */
1299      public void testExecuteNull() {
1300          ThreadPoolExecutor p =
1301 <            new ThreadPoolExecutor(1, 2,
1295 <                                   LONG_DELAY_MS, MILLISECONDS,
1301 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1302                                     new ArrayBlockingQueue<Runnable>(10));
1303          try {
1304              p.execute(null);
# Line 1359 | Line 1365 | public class ThreadPoolExecutorTest exte
1365      }
1366  
1367      /**
1368 +     * Configuration changes that allow core pool size greater than
1369 +     * max pool size result in IllegalArgumentException.
1370 +     */
1371 +    public void testPoolSizeInvariants() {
1372 +        ThreadPoolExecutor p =
1373 +            new ThreadPoolExecutor(1, 1,
1374 +                                   LONG_DELAY_MS, MILLISECONDS,
1375 +                                   new ArrayBlockingQueue<Runnable>(10));
1376 +        for (int s = 1; s < 5; s++) {
1377 +            p.setMaximumPoolSize(s);
1378 +            p.setCorePoolSize(s);
1379 +            try {
1380 +                p.setMaximumPoolSize(s - 1);
1381 +                shouldThrow();
1382 +            } catch (IllegalArgumentException success) {}
1383 +            assertEquals(s, p.getCorePoolSize());
1384 +            assertEquals(s, p.getMaximumPoolSize());
1385 +            try {
1386 +                p.setCorePoolSize(s + 1);
1387 +                shouldThrow();
1388 +            } catch (IllegalArgumentException success) {}
1389 +            assertEquals(s, p.getCorePoolSize());
1390 +            assertEquals(s, p.getMaximumPoolSize());
1391 +        }
1392 +        joinPool(p);
1393 +    }
1394 +
1395 +    /**
1396       * setKeepAliveTime throws IllegalArgumentException
1397       * when given a negative value
1398       */
# Line 1874 | Line 1908 | public class ThreadPoolExecutorTest exte
1908              l.add(new StringTask());
1909              l.add(new StringTask());
1910              List<Future<String>> futures =
1911 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1911 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1912              assertEquals(2, futures.size());
1913              for (Future<String> future : futures)
1914                  assertSame(TEST_STRING, future.get());
# Line 1892 | Line 1926 | public class ThreadPoolExecutorTest exte
1926                                     LONG_DELAY_MS, MILLISECONDS,
1927                                     new ArrayBlockingQueue<Runnable>(10));
1928          try {
1929 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1930 <            l.add(new StringTask());
1931 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1932 <            l.add(new StringTask());
1933 <            List<Future<String>> futures =
1934 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1935 <            assertEquals(l.size(), futures.size());
1936 <            for (Future future : futures)
1937 <                assertTrue(future.isDone());
1938 <            assertFalse(futures.get(0).isCancelled());
1939 <            assertTrue(futures.get(1).isCancelled());
1929 >            for (long timeout = timeoutMillis();;) {
1930 >                List<Callable<String>> tasks = new ArrayList<>();
1931 >                tasks.add(new StringTask("0"));
1932 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1933 >                tasks.add(new StringTask("2"));
1934 >                long startTime = System.nanoTime();
1935 >                List<Future<String>> futures =
1936 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1937 >                assertEquals(tasks.size(), futures.size());
1938 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1939 >                for (Future future : futures)
1940 >                    assertTrue(future.isDone());
1941 >                assertTrue(futures.get(1).isCancelled());
1942 >                try {
1943 >                    assertEquals("0", futures.get(0).get());
1944 >                    assertEquals("2", futures.get(2).get());
1945 >                    break;
1946 >                } catch (CancellationException retryWithLongerTimeout) {
1947 >                    timeout *= 2;
1948 >                    if (timeout >= LONG_DELAY_MS / 2)
1949 >                        fail("expected exactly one task to be cancelled");
1950 >                }
1951 >            }
1952          } finally {
1953              joinPool(e);
1954          }
# Line 1948 | Line 1994 | public class ThreadPoolExecutorTest exte
1994       * allowCoreThreadTimeOut(true) causes idle threads to time out
1995       */
1996      public void testAllowCoreThreadTimeOut_true() throws Exception {
1997 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1997 >        long keepAliveTime = timeoutMillis();
1998          final ThreadPoolExecutor p =
1999              new ThreadPoolExecutor(2, 10,
2000 <                                   coreThreadTimeOut, MILLISECONDS,
2000 >                                   keepAliveTime, MILLISECONDS,
2001                                     new ArrayBlockingQueue<Runnable>(10));
2002          final CountDownLatch threadStarted = new CountDownLatch(1);
2003          try {
# Line 1962 | Line 2008 | public class ThreadPoolExecutorTest exte
2008                      assertEquals(1, p.getPoolSize());
2009                  }});
2010              await(threadStarted);
2011 <            delay(coreThreadTimeOut);
2011 >            delay(keepAliveTime);
2012              long startTime = System.nanoTime();
2013              while (p.getPoolSize() > 0
2014                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1978 | Line 2024 | public class ThreadPoolExecutorTest exte
2024       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2025       */
2026      public void testAllowCoreThreadTimeOut_false() throws Exception {
2027 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2027 >        long keepAliveTime = timeoutMillis();
2028          final ThreadPoolExecutor p =
2029              new ThreadPoolExecutor(2, 10,
2030 <                                   coreThreadTimeOut, MILLISECONDS,
2030 >                                   keepAliveTime, MILLISECONDS,
2031                                     new ArrayBlockingQueue<Runnable>(10));
2032          final CountDownLatch threadStarted = new CountDownLatch(1);
2033          try {
# Line 1991 | Line 2037 | public class ThreadPoolExecutorTest exte
2037                      threadStarted.countDown();
2038                      assertTrue(p.getPoolSize() >= 1);
2039                  }});
2040 <            delay(2 * coreThreadTimeOut);
2040 >            delay(2 * keepAliveTime);
2041              assertTrue(p.getPoolSize() >= 1);
2042          } finally {
2043              joinPool(p);
# Line 2010 | Line 2056 | public class ThreadPoolExecutorTest exte
2056                  done.countDown();
2057              }};
2058          final ThreadPoolExecutor p =
2059 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2059 >            new ThreadPoolExecutor(1, 30,
2060 >                                   60, SECONDS,
2061                                     new ArrayBlockingQueue(30));
2062          try {
2063              for (int i = 0; i < nTasks; ++i) {
# Line 2029 | Line 2076 | public class ThreadPoolExecutorTest exte
2076          }
2077      }
2078  
2079 +    /**
2080 +     * get(cancelled task) throws CancellationException
2081 +     */
2082 +    public void testGet_cancelled() throws Exception {
2083 +        final ExecutorService e =
2084 +            new ThreadPoolExecutor(1, 1,
2085 +                                   LONG_DELAY_MS, MILLISECONDS,
2086 +                                   new LinkedBlockingQueue<Runnable>());
2087 +        try {
2088 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
2089 +            final CountDownLatch done = new CountDownLatch(1);
2090 +            final List<Future<?>> futures = new ArrayList<>();
2091 +            for (int i = 0; i < 2; i++) {
2092 +                Runnable r = new CheckedRunnable() { public void realRun()
2093 +                                                         throws Throwable {
2094 +                    blockerStarted.countDown();
2095 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2096 +                }};
2097 +                futures.add(e.submit(r));
2098 +            }
2099 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2100 +            for (Future<?> future : futures) future.cancel(false);
2101 +            for (Future<?> future : futures) {
2102 +                try {
2103 +                    future.get();
2104 +                    shouldThrow();
2105 +                } catch (CancellationException success) {}
2106 +                try {
2107 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
2108 +                    shouldThrow();
2109 +                } catch (CancellationException success) {}
2110 +                assertTrue(future.isCancelled());
2111 +                assertTrue(future.isDone());
2112 +            }
2113 +            done.countDown();
2114 +        } finally {
2115 +            joinPool(e);
2116 +        }
2117 +    }
2118 +
2119   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines