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.63 by jsr166, Mon Sep 28 08:23:49 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 215 | Line 217 | public class ThreadPoolExecutorTest exte
217              new ThreadPoolExecutor(2, 2,
218                                     1000, MILLISECONDS,
219                                     new ArrayBlockingQueue<Runnable>(10));
220 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
220 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
221          joinPool(p);
222      }
223  
# Line 623 | Line 625 | public class ThreadPoolExecutorTest exte
625      }
626  
627      /**
628 <     * shutdownNow returns a list containing tasks that were not run
628 >     * shutdownNow returns a list containing tasks that were not run,
629 >     * and those tasks are drained from the queue
630       */
631 <    public void testShutdownNow() {
631 >    public void testShutdownNow() throws InterruptedException {
632 >        final int poolSize = 2;
633 >        final int count = 5;
634 >        final AtomicInteger ran = new AtomicInteger(0);
635          final ThreadPoolExecutor p =
636 <            new ThreadPoolExecutor(1, 1,
636 >            new ThreadPoolExecutor(poolSize, poolSize,
637                                     LONG_DELAY_MS, MILLISECONDS,
638                                     new ArrayBlockingQueue<Runnable>(10));
639 <        List l;
640 <        try {
641 <            for (int i = 0; i < 5; i++)
636 <                p.execute(new MediumPossiblyInterruptedRunnable());
637 <        }
638 <        finally {
639 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
640 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
641 >            threadsStarted.countDown();
642              try {
643 <                l = p.shutdownNow();
644 <            } catch (SecurityException ok) { return; }
643 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
644 >            } catch (InterruptedException success) {}
645 >            ran.getAndIncrement();
646 >        }};
647 >        for (int i = 0; i < count; i++)
648 >            p.execute(waiter);
649 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
650 >        assertEquals(poolSize, p.getActiveCount());
651 >        assertEquals(0, p.getCompletedTaskCount());
652 >        final List<Runnable> queuedTasks;
653 >        try {
654 >            queuedTasks = p.shutdownNow();
655 >        } catch (SecurityException ok) {
656 >            return; // Allowed in case test doesn't have privs
657          }
658          assertTrue(p.isShutdown());
659 <        assertTrue(l.size() <= 4);
659 >        assertTrue(p.getQueue().isEmpty());
660 >        assertEquals(count - poolSize, queuedTasks.size());
661 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
662 >        assertTrue(p.isTerminated());
663 >        assertEquals(poolSize, ran.get());
664 >        assertEquals(poolSize, p.getCompletedTaskCount());
665      }
666  
667      // Exception Tests
# Line 990 | Line 1010 | public class ThreadPoolExecutorTest exte
1010      public void testInterruptedSubmit() throws InterruptedException {
1011          final ThreadPoolExecutor p =
1012              new ThreadPoolExecutor(1, 1,
1013 <                                   60, TimeUnit.SECONDS,
1013 >                                   60, SECONDS,
1014                                     new ArrayBlockingQueue<Runnable>(10));
1015  
1016          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1264 | Line 1284 | public class ThreadPoolExecutorTest exte
1284       */
1285      public void testExecuteNull() {
1286          ThreadPoolExecutor p =
1287 <            new ThreadPoolExecutor(1, 2,
1268 <                                   LONG_DELAY_MS, MILLISECONDS,
1287 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1288                                     new ArrayBlockingQueue<Runnable>(10));
1289          try {
1290              p.execute(null);
# Line 1332 | Line 1351 | public class ThreadPoolExecutorTest exte
1351      }
1352  
1353      /**
1354 +     * Configuration changes that allow core pool size greater than
1355 +     * max pool size result in IllegalArgumentException.
1356 +     */
1357 +    public void testPoolSizeInvariants() {
1358 +        ThreadPoolExecutor p =
1359 +            new ThreadPoolExecutor(1, 1,
1360 +                                   LONG_DELAY_MS, MILLISECONDS,
1361 +                                   new ArrayBlockingQueue<Runnable>(10));
1362 +        for (int s = 1; s < 5; s++) {
1363 +            p.setMaximumPoolSize(s);
1364 +            p.setCorePoolSize(s);
1365 +            try {
1366 +                p.setMaximumPoolSize(s - 1);
1367 +                shouldThrow();
1368 +            } catch (IllegalArgumentException success) {}
1369 +            assertEquals(s, p.getCorePoolSize());
1370 +            assertEquals(s, p.getMaximumPoolSize());
1371 +            try {
1372 +                p.setCorePoolSize(s + 1);
1373 +                shouldThrow();
1374 +            } catch (IllegalArgumentException success) {}
1375 +            assertEquals(s, p.getCorePoolSize());
1376 +            assertEquals(s, p.getMaximumPoolSize());
1377 +        }
1378 +        joinPool(p);
1379 +    }
1380 +
1381 +    /**
1382       * setKeepAliveTime throws IllegalArgumentException
1383       * when given a negative value
1384       */
# Line 1865 | Line 1912 | public class ThreadPoolExecutorTest exte
1912                                     LONG_DELAY_MS, MILLISECONDS,
1913                                     new ArrayBlockingQueue<Runnable>(10));
1914          try {
1915 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1916 <            l.add(new StringTask());
1917 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1918 <            l.add(new StringTask());
1919 <            List<Future<String>> futures =
1920 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1921 <            assertEquals(l.size(), futures.size());
1922 <            for (Future future : futures)
1923 <                assertTrue(future.isDone());
1924 <            assertFalse(futures.get(0).isCancelled());
1925 <            assertTrue(futures.get(1).isCancelled());
1915 >            for (long timeout = timeoutMillis();;) {
1916 >                List<Callable<String>> tasks = new ArrayList<>();
1917 >                tasks.add(new StringTask("0"));
1918 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1919 >                tasks.add(new StringTask("2"));
1920 >                long startTime = System.nanoTime();
1921 >                List<Future<String>> futures =
1922 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1923 >                assertEquals(tasks.size(), futures.size());
1924 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1925 >                for (Future future : futures)
1926 >                    assertTrue(future.isDone());
1927 >                assertTrue(futures.get(1).isCancelled());
1928 >                try {
1929 >                    assertEquals("0", futures.get(0).get());
1930 >                    assertEquals("2", futures.get(2).get());
1931 >                    break;
1932 >                } catch (CancellationException retryWithLongerTimeout) {
1933 >                    timeout *= 2;
1934 >                    if (timeout >= LONG_DELAY_MS / 2)
1935 >                        fail("expected exactly one task to be cancelled");
1936 >                }
1937 >            }
1938          } finally {
1939              joinPool(e);
1940          }
# Line 1921 | Line 1980 | public class ThreadPoolExecutorTest exte
1980       * allowCoreThreadTimeOut(true) causes idle threads to time out
1981       */
1982      public void testAllowCoreThreadTimeOut_true() throws Exception {
1983 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1983 >        long keepAliveTime = timeoutMillis();
1984          final ThreadPoolExecutor p =
1985              new ThreadPoolExecutor(2, 10,
1986 <                                   coreThreadTimeOut, MILLISECONDS,
1986 >                                   keepAliveTime, MILLISECONDS,
1987                                     new ArrayBlockingQueue<Runnable>(10));
1988          final CountDownLatch threadStarted = new CountDownLatch(1);
1989          try {
# Line 1935 | Line 1994 | public class ThreadPoolExecutorTest exte
1994                      assertEquals(1, p.getPoolSize());
1995                  }});
1996              await(threadStarted);
1997 <            delay(coreThreadTimeOut);
1997 >            delay(keepAliveTime);
1998              long startTime = System.nanoTime();
1999              while (p.getPoolSize() > 0
2000                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1951 | Line 2010 | public class ThreadPoolExecutorTest exte
2010       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2011       */
2012      public void testAllowCoreThreadTimeOut_false() throws Exception {
2013 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2013 >        long keepAliveTime = timeoutMillis();
2014          final ThreadPoolExecutor p =
2015              new ThreadPoolExecutor(2, 10,
2016 <                                   coreThreadTimeOut, MILLISECONDS,
2016 >                                   keepAliveTime, MILLISECONDS,
2017                                     new ArrayBlockingQueue<Runnable>(10));
2018          final CountDownLatch threadStarted = new CountDownLatch(1);
2019          try {
# Line 1964 | Line 2023 | public class ThreadPoolExecutorTest exte
2023                      threadStarted.countDown();
2024                      assertTrue(p.getPoolSize() >= 1);
2025                  }});
2026 <            delay(2 * coreThreadTimeOut);
2026 >            delay(2 * keepAliveTime);
2027              assertTrue(p.getPoolSize() >= 1);
2028          } finally {
2029              joinPool(p);
# Line 1983 | Line 2042 | public class ThreadPoolExecutorTest exte
2042                  done.countDown();
2043              }};
2044          final ThreadPoolExecutor p =
2045 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2045 >            new ThreadPoolExecutor(1, 30,
2046 >                                   60, SECONDS,
2047                                     new ArrayBlockingQueue(30));
2048          try {
2049              for (int i = 0; i < nTasks; ++i) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines