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.61 by jsr166, Mon Sep 28 02:41:29 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 >        CheckedRunnable 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 >        final List<Runnable> queuedTasks;
651 >        try {
652 >            queuedTasks = p.shutdownNow();
653 >        } catch (SecurityException ok) {
654 >            return; // Allowed in case test doesn't have privs
655          }
656          assertTrue(p.isShutdown());
657 <        assertTrue(l.size() <= 4);
657 >        assertTrue(p.getQueue().isEmpty());
658 >        assertEquals(count - poolSize, queuedTasks.size());
659 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
660 >        assertTrue(p.isTerminated());
661 >        assertEquals(poolSize, ran.get());
662      }
663  
664      // Exception Tests
# Line 990 | Line 1007 | public class ThreadPoolExecutorTest exte
1007      public void testInterruptedSubmit() throws InterruptedException {
1008          final ThreadPoolExecutor p =
1009              new ThreadPoolExecutor(1, 1,
1010 <                                   60, TimeUnit.SECONDS,
1010 >                                   60, SECONDS,
1011                                     new ArrayBlockingQueue<Runnable>(10));
1012  
1013          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1264 | Line 1281 | public class ThreadPoolExecutorTest exte
1281       */
1282      public void testExecuteNull() {
1283          ThreadPoolExecutor p =
1284 <            new ThreadPoolExecutor(1, 2,
1268 <                                   LONG_DELAY_MS, MILLISECONDS,
1284 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1285                                     new ArrayBlockingQueue<Runnable>(10));
1286          try {
1287              p.execute(null);
# Line 1332 | Line 1348 | public class ThreadPoolExecutorTest exte
1348      }
1349  
1350      /**
1351 +     * Configuration changes that allow core pool size greater than
1352 +     * max pool size result in IllegalArgumentException.
1353 +     */
1354 +    public void testPoolSizeInvariants() {
1355 +        ThreadPoolExecutor p =
1356 +            new ThreadPoolExecutor(1, 1,
1357 +                                   LONG_DELAY_MS, MILLISECONDS,
1358 +                                   new ArrayBlockingQueue<Runnable>(10));
1359 +        for (int s = 1; s < 5; s++) {
1360 +            p.setMaximumPoolSize(s);
1361 +            p.setCorePoolSize(s);
1362 +            try {
1363 +                p.setMaximumPoolSize(s - 1);
1364 +                shouldThrow();
1365 +            } catch (IllegalArgumentException success) {}
1366 +            assertEquals(s, p.getCorePoolSize());
1367 +            assertEquals(s, p.getMaximumPoolSize());
1368 +            try {
1369 +                p.setCorePoolSize(s + 1);
1370 +                shouldThrow();
1371 +            } catch (IllegalArgumentException success) {}
1372 +            assertEquals(s, p.getCorePoolSize());
1373 +            assertEquals(s, p.getMaximumPoolSize());
1374 +        }
1375 +        joinPool(p);
1376 +    }
1377 +
1378 +    /**
1379       * setKeepAliveTime throws IllegalArgumentException
1380       * when given a negative value
1381       */
# Line 1865 | Line 1909 | public class ThreadPoolExecutorTest exte
1909                                     LONG_DELAY_MS, MILLISECONDS,
1910                                     new ArrayBlockingQueue<Runnable>(10));
1911          try {
1912 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1913 <            l.add(new StringTask());
1914 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1915 <            l.add(new StringTask());
1916 <            List<Future<String>> futures =
1917 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1918 <            assertEquals(l.size(), futures.size());
1919 <            for (Future future : futures)
1920 <                assertTrue(future.isDone());
1921 <            assertFalse(futures.get(0).isCancelled());
1922 <            assertTrue(futures.get(1).isCancelled());
1912 >            for (long timeout = timeoutMillis();;) {
1913 >                List<Callable<String>> tasks = new ArrayList<>();
1914 >                tasks.add(new StringTask("0"));
1915 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1916 >                tasks.add(new StringTask("2"));
1917 >                long startTime = System.nanoTime();
1918 >                List<Future<String>> futures =
1919 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1920 >                assertEquals(tasks.size(), futures.size());
1921 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1922 >                for (Future future : futures)
1923 >                    assertTrue(future.isDone());
1924 >                assertTrue(futures.get(1).isCancelled());
1925 >                try {
1926 >                    assertEquals("0", futures.get(0).get());
1927 >                    assertEquals("2", futures.get(2).get());
1928 >                    break;
1929 >                } catch (CancellationException retryWithLongerTimeout) {
1930 >                    timeout *= 2;
1931 >                    if (timeout >= LONG_DELAY_MS / 2)
1932 >                        fail("expected exactly one task to be cancelled");
1933 >                }
1934 >            }
1935          } finally {
1936              joinPool(e);
1937          }
# Line 1921 | Line 1977 | public class ThreadPoolExecutorTest exte
1977       * allowCoreThreadTimeOut(true) causes idle threads to time out
1978       */
1979      public void testAllowCoreThreadTimeOut_true() throws Exception {
1980 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1980 >        long keepAliveTime = timeoutMillis();
1981          final ThreadPoolExecutor p =
1982              new ThreadPoolExecutor(2, 10,
1983 <                                   coreThreadTimeOut, MILLISECONDS,
1983 >                                   keepAliveTime, MILLISECONDS,
1984                                     new ArrayBlockingQueue<Runnable>(10));
1985          final CountDownLatch threadStarted = new CountDownLatch(1);
1986          try {
# Line 1935 | Line 1991 | public class ThreadPoolExecutorTest exte
1991                      assertEquals(1, p.getPoolSize());
1992                  }});
1993              await(threadStarted);
1994 <            delay(coreThreadTimeOut);
1994 >            delay(keepAliveTime);
1995              long startTime = System.nanoTime();
1996              while (p.getPoolSize() > 0
1997                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1951 | Line 2007 | public class ThreadPoolExecutorTest exte
2007       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2008       */
2009      public void testAllowCoreThreadTimeOut_false() throws Exception {
2010 <        long coreThreadTimeOut = SHORT_DELAY_MS;
2010 >        long keepAliveTime = timeoutMillis();
2011          final ThreadPoolExecutor p =
2012              new ThreadPoolExecutor(2, 10,
2013 <                                   coreThreadTimeOut, MILLISECONDS,
2013 >                                   keepAliveTime, MILLISECONDS,
2014                                     new ArrayBlockingQueue<Runnable>(10));
2015          final CountDownLatch threadStarted = new CountDownLatch(1);
2016          try {
# Line 1964 | Line 2020 | public class ThreadPoolExecutorTest exte
2020                      threadStarted.countDown();
2021                      assertTrue(p.getPoolSize() >= 1);
2022                  }});
2023 <            delay(2 * coreThreadTimeOut);
2023 >            delay(2 * keepAliveTime);
2024              assertTrue(p.getPoolSize() >= 1);
2025          } finally {
2026              joinPool(p);
# Line 1983 | Line 2039 | public class ThreadPoolExecutorTest exte
2039                  done.countDown();
2040              }};
2041          final ThreadPoolExecutor p =
2042 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2042 >            new ThreadPoolExecutor(1, 30,
2043 >                                   60, SECONDS,
2044                                     new ArrayBlockingQueue(30));
2045          try {
2046              for (int i = 0; i < nTasks; ++i) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines