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.54 by jsr166, Fri Sep 4 19:35:46 2015 UTC vs.
Revision 1.66 by jsr166, Sun Oct 4 01:18:25 2015 UTC

# Line 15 | Line 15 | 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 28 | 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;
# Line 130 | 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 215 | 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 623 | 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++)
636 <                p.execute(new MediumPossiblyInterruptedRunnable());
637 <        }
638 <        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 990 | 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 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