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.52 by jsr166, Fri May 15 17:07:27 2015 UTC vs.
Revision 1.69 by jsr166, Sun Oct 4 01:29:09 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 148 | 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 168 | 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 190 | Line 206 | public class ThreadPoolExecutorTest exte
206                      fail("timed out");
207                  Thread.yield();
208              }
193        } finally {
194            joinPool(p);
209          }
210      }
211  
# Line 203 | 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 215 | 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));
233 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
234          joinPool(p);
235      }
236  
# Line 623 | Line 638 | public class ThreadPoolExecutorTest exte
638      }
639  
640      /**
641 <     * shutdownNow returns a list containing tasks that were not run
641 >     * shutdownNow returns a list containing tasks that were not run,
642 >     * and those tasks are drained from the queue
643       */
644 <    public void testShutdownNow() {
644 >    public void testShutdownNow() throws InterruptedException {
645 >        final int poolSize = 2;
646 >        final int count = 5;
647 >        final AtomicInteger ran = new AtomicInteger(0);
648          final ThreadPoolExecutor p =
649 <            new ThreadPoolExecutor(1, 1,
649 >            new ThreadPoolExecutor(poolSize, poolSize,
650                                     LONG_DELAY_MS, MILLISECONDS,
651                                     new ArrayBlockingQueue<Runnable>(10));
652 <        List l;
653 <        try {
654 <            for (int i = 0; i < 5; i++)
636 <                p.execute(new MediumPossiblyInterruptedRunnable());
637 <        }
638 <        finally {
652 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
653 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
654 >            threadsStarted.countDown();
655              try {
656 <                l = p.shutdownNow();
657 <            } catch (SecurityException ok) { return; }
656 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
657 >            } catch (InterruptedException success) {}
658 >            ran.getAndIncrement();
659 >        }};
660 >        for (int i = 0; i < count; i++)
661 >            p.execute(waiter);
662 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
663 >        assertEquals(poolSize, p.getActiveCount());
664 >        assertEquals(0, p.getCompletedTaskCount());
665 >        final List<Runnable> queuedTasks;
666 >        try {
667 >            queuedTasks = p.shutdownNow();
668 >        } catch (SecurityException ok) {
669 >            return; // Allowed in case test doesn't have privs
670          }
671          assertTrue(p.isShutdown());
672 <        assertTrue(l.size() <= 4);
672 >        assertTrue(p.getQueue().isEmpty());
673 >        assertEquals(count - poolSize, queuedTasks.size());
674 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
675 >        assertTrue(p.isTerminated());
676 >        assertEquals(poolSize, ran.get());
677 >        assertEquals(poolSize, p.getCompletedTaskCount());
678      }
679  
680      // Exception Tests
# Line 990 | Line 1023 | public class ThreadPoolExecutorTest exte
1023      public void testInterruptedSubmit() throws InterruptedException {
1024          final ThreadPoolExecutor p =
1025              new ThreadPoolExecutor(1, 1,
1026 <                                   60, TimeUnit.SECONDS,
1026 >                                   60, SECONDS,
1027                                     new ArrayBlockingQueue<Runnable>(10));
1028  
1029          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1264 | Line 1297 | public class ThreadPoolExecutorTest exte
1297       */
1298      public void testExecuteNull() {
1299          ThreadPoolExecutor p =
1300 <            new ThreadPoolExecutor(1, 2,
1268 <                                   LONG_DELAY_MS, MILLISECONDS,
1300 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1301                                     new ArrayBlockingQueue<Runnable>(10));
1302          try {
1303              p.execute(null);
# Line 1332 | Line 1364 | public class ThreadPoolExecutorTest exte
1364      }
1365  
1366      /**
1367 +     * Configuration changes that allow core pool size greater than
1368 +     * max pool size result in IllegalArgumentException.
1369 +     */
1370 +    public void testPoolSizeInvariants() {
1371 +        ThreadPoolExecutor p =
1372 +            new ThreadPoolExecutor(1, 1,
1373 +                                   LONG_DELAY_MS, MILLISECONDS,
1374 +                                   new ArrayBlockingQueue<Runnable>(10));
1375 +        for (int s = 1; s < 5; s++) {
1376 +            p.setMaximumPoolSize(s);
1377 +            p.setCorePoolSize(s);
1378 +            try {
1379 +                p.setMaximumPoolSize(s - 1);
1380 +                shouldThrow();
1381 +            } catch (IllegalArgumentException success) {}
1382 +            assertEquals(s, p.getCorePoolSize());
1383 +            assertEquals(s, p.getMaximumPoolSize());
1384 +            try {
1385 +                p.setCorePoolSize(s + 1);
1386 +                shouldThrow();
1387 +            } catch (IllegalArgumentException success) {}
1388 +            assertEquals(s, p.getCorePoolSize());
1389 +            assertEquals(s, p.getMaximumPoolSize());
1390 +        }
1391 +        joinPool(p);
1392 +    }
1393 +
1394 +    /**
1395       * setKeepAliveTime throws IllegalArgumentException
1396       * when given a negative value
1397       */
# Line 1847 | Line 1907 | public class ThreadPoolExecutorTest exte
1907              l.add(new StringTask());
1908              l.add(new StringTask());
1909              List<Future<String>> futures =
1910 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1910 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1911              assertEquals(2, futures.size());
1912              for (Future<String> future : futures)
1913                  assertSame(TEST_STRING, future.get());
# Line 1865 | Line 1925 | public class ThreadPoolExecutorTest exte
1925                                     LONG_DELAY_MS, MILLISECONDS,
1926                                     new ArrayBlockingQueue<Runnable>(10));
1927          try {
1928 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1929 <            l.add(new StringTask());
1930 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1931 <            l.add(new StringTask());
1932 <            List<Future<String>> futures =
1933 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1934 <            assertEquals(l.size(), futures.size());
1935 <            for (Future future : futures)
1936 <                assertTrue(future.isDone());
1937 <            assertFalse(futures.get(0).isCancelled());
1938 <            assertTrue(futures.get(1).isCancelled());
1928 >            for (long timeout = timeoutMillis();;) {
1929 >                List<Callable<String>> tasks = new ArrayList<>();
1930 >                tasks.add(new StringTask("0"));
1931 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1932 >                tasks.add(new StringTask("2"));
1933 >                long startTime = System.nanoTime();
1934 >                List<Future<String>> futures =
1935 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1936 >                assertEquals(tasks.size(), futures.size());
1937 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1938 >                for (Future future : futures)
1939 >                    assertTrue(future.isDone());
1940 >                assertTrue(futures.get(1).isCancelled());
1941 >                try {
1942 >                    assertEquals("0", futures.get(0).get());
1943 >                    assertEquals("2", futures.get(2).get());
1944 >                    break;
1945 >                } catch (CancellationException retryWithLongerTimeout) {
1946 >                    timeout *= 2;
1947 >                    if (timeout >= LONG_DELAY_MS / 2)
1948 >                        fail("expected exactly one task to be cancelled");
1949 >                }
1950 >            }
1951          } finally {
1952              joinPool(e);
1953          }
# Line 1921 | Line 1993 | public class ThreadPoolExecutorTest exte
1993       * allowCoreThreadTimeOut(true) causes idle threads to time out
1994       */
1995      public void testAllowCoreThreadTimeOut_true() throws Exception {
1996 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1996 >        long keepAliveTime = timeoutMillis();
1997          final ThreadPoolExecutor p =
1998              new ThreadPoolExecutor(2, 10,
1999 <                                   coreThreadTimeOut, MILLISECONDS,
1999 >                                   keepAliveTime, MILLISECONDS,
2000                                     new ArrayBlockingQueue<Runnable>(10));
2001          final CountDownLatch threadStarted = new CountDownLatch(1);
2002          try {
# Line 1935 | Line 2007 | public class ThreadPoolExecutorTest exte
2007                      assertEquals(1, p.getPoolSize());
2008                  }});
2009              await(threadStarted);
2010 <            delay(coreThreadTimeOut);
2010 >            delay(keepAliveTime);
2011              long startTime = System.nanoTime();
2012              while (p.getPoolSize() > 0
2013                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1951 | Line 2023 | public class ThreadPoolExecutorTest exte
2023       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2024       */
2025      public void testAllowCoreThreadTimeOut_false() throws Exception {
2026 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2026 >        long keepAliveTime = timeoutMillis();
2027          final ThreadPoolExecutor p =
2028              new ThreadPoolExecutor(2, 10,
2029 <                                   coreThreadTimeOut, MILLISECONDS,
2029 >                                   keepAliveTime, MILLISECONDS,
2030                                     new ArrayBlockingQueue<Runnable>(10));
2031          final CountDownLatch threadStarted = new CountDownLatch(1);
2032          try {
# Line 1964 | Line 2036 | public class ThreadPoolExecutorTest exte
2036                      threadStarted.countDown();
2037                      assertTrue(p.getPoolSize() >= 1);
2038                  }});
2039 <            delay(2 * coreThreadTimeOut);
2039 >            delay(2 * keepAliveTime);
2040              assertTrue(p.getPoolSize() >= 1);
2041          } finally {
2042              joinPool(p);
# Line 1983 | Line 2055 | public class ThreadPoolExecutorTest exte
2055                  done.countDown();
2056              }};
2057          final ThreadPoolExecutor p =
2058 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2058 >            new ThreadPoolExecutor(1, 30,
2059 >                                   60, SECONDS,
2060                                     new ArrayBlockingQueue(30));
2061          try {
2062              for (int i = 0; i < nTasks; ++i) {
# Line 2002 | Line 2075 | public class ThreadPoolExecutorTest exte
2075          }
2076      }
2077  
2078 +    /**
2079 +     * get(cancelled task) throws CancellationException
2080 +     */
2081 +    public void testGet_cancelled() throws Exception {
2082 +        final ExecutorService e =
2083 +            new ThreadPoolExecutor(1, 1,
2084 +                                   LONG_DELAY_MS, MILLISECONDS,
2085 +                                   new LinkedBlockingQueue<Runnable>());
2086 +        try {
2087 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
2088 +            final CountDownLatch done = new CountDownLatch(1);
2089 +            final List<Future<?>> futures = new ArrayList<>();
2090 +            for (int i = 0; i < 2; i++) {
2091 +                Runnable r = new CheckedRunnable() { public void realRun()
2092 +                                                         throws Throwable {
2093 +                    blockerStarted.countDown();
2094 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2095 +                }};
2096 +                futures.add(e.submit(r));
2097 +            }
2098 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2099 +            for (Future<?> future : futures) future.cancel(false);
2100 +            for (Future<?> future : futures) {
2101 +                try {
2102 +                    future.get();
2103 +                    shouldThrow();
2104 +                } catch (CancellationException success) {}
2105 +                try {
2106 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
2107 +                    shouldThrow();
2108 +                } catch (CancellationException success) {}
2109 +                assertTrue(future.isCancelled());
2110 +                assertTrue(future.isDone());
2111 +            }
2112 +            done.countDown();
2113 +        } finally {
2114 +            joinPool(e);
2115 +        }
2116 +    }
2117 +
2118   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines