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.56 by jsr166, Mon Sep 14 00:33:41 2015 UTC vs.
Revision 1.70 by jsr166, Sun Oct 4 01:30:27 2015 UTC

# Line 14 | Line 14 | 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.CancellationException;
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 29 | 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 131 | 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 149 | 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 169 | 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 191 | Line 206 | public class ThreadPoolExecutorTest exte
206                      fail("timed out");
207                  Thread.yield();
208              }
194        } finally {
195            joinPool(p);
209          }
210      }
211  
# Line 204 | 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 216 | Line 230 | public class ThreadPoolExecutorTest exte
230              new ThreadPoolExecutor(2, 2,
231                                     1000, MILLISECONDS,
232                                     new ArrayBlockingQueue<Runnable>(10));
233 <        assertEquals(1, p.getKeepAliveTime(SECONDS));
234 <        joinPool(p);
233 >        try (PoolCleaner cleaner = cleaner(p)) {
234 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
235 >        }
236      }
237  
238      /**
# Line 624 | 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++)
637 <                p.execute(new MediumPossiblyInterruptedRunnable());
638 <        }
639 <        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 1875 | 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 1895 | Line 1928 | public class ThreadPoolExecutorTest exte
1928          try {
1929              for (long timeout = timeoutMillis();;) {
1930                  List<Callable<String>> tasks = new ArrayList<>();
1931 <                tasks.add(new StringTask());
1931 >                tasks.add(new StringTask("0"));
1932                  tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1933 <                tasks.add(new StringTask());
1933 >                tasks.add(new StringTask("2"));
1934                  long startTime = System.nanoTime();
1935                  List<Future<String>> futures =
1936                      e.invokeAll(tasks, timeout, MILLISECONDS);
# Line 1907 | Line 1940 | public class ThreadPoolExecutorTest exte
1940                      assertTrue(future.isDone());
1941                  assertTrue(futures.get(1).isCancelled());
1942                  try {
1943 <                    assertEquals(TEST_STRING, futures.get(0).get());
1944 <                    assertEquals(TEST_STRING, futures.get(2).get());
1943 >                    assertEquals("0", futures.get(0).get());
1944 >                    assertEquals("2", futures.get(2).get());
1945                      break;
1946                  } catch (CancellationException retryWithLongerTimeout) {
1947                      timeout *= 2;
# Line 1961 | 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 1975 | 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 1991 | 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 2004 | 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 2043 | 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