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.60 by jsr166, Sun Sep 27 18:50:50 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 215 | Line 216 | public class ThreadPoolExecutorTest exte
216              new ThreadPoolExecutor(2, 2,
217                                     1000, MILLISECONDS,
218                                     new ArrayBlockingQueue<Runnable>(10));
219 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
219 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
220          joinPool(p);
221      }
222  
# Line 623 | Line 624 | public class ThreadPoolExecutorTest exte
624      }
625  
626      /**
627 <     * shutdownNow returns a list containing tasks that were not run
627 >     * shutdownNow returns a list containing tasks that were not run,
628 >     * and those tasks are drained from the queue
629       */
630      public void testShutdownNow() {
631          final ThreadPoolExecutor p =
# Line 641 | Line 643 | public class ThreadPoolExecutorTest exte
643              } catch (SecurityException ok) { return; }
644          }
645          assertTrue(p.isShutdown());
646 +        assertTrue(p.getQueue().isEmpty());
647          assertTrue(l.size() <= 4);
648      }
649  
# Line 990 | Line 993 | public class ThreadPoolExecutorTest exte
993      public void testInterruptedSubmit() throws InterruptedException {
994          final ThreadPoolExecutor p =
995              new ThreadPoolExecutor(1, 1,
996 <                                   60, TimeUnit.SECONDS,
996 >                                   60, SECONDS,
997                                     new ArrayBlockingQueue<Runnable>(10));
998  
999          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1264 | Line 1267 | public class ThreadPoolExecutorTest exte
1267       */
1268      public void testExecuteNull() {
1269          ThreadPoolExecutor p =
1270 <            new ThreadPoolExecutor(1, 2,
1268 <                                   LONG_DELAY_MS, MILLISECONDS,
1270 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1271                                     new ArrayBlockingQueue<Runnable>(10));
1272          try {
1273              p.execute(null);
# Line 1332 | Line 1334 | public class ThreadPoolExecutorTest exte
1334      }
1335  
1336      /**
1337 +     * Configuration changes that allow core pool size greater than
1338 +     * max pool size result in IllegalArgumentException.
1339 +     */
1340 +    public void testPoolSizeInvariants() {
1341 +        ThreadPoolExecutor p =
1342 +            new ThreadPoolExecutor(1, 1,
1343 +                                   LONG_DELAY_MS, MILLISECONDS,
1344 +                                   new ArrayBlockingQueue<Runnable>(10));
1345 +        for (int s = 1; s < 5; s++) {
1346 +            p.setMaximumPoolSize(s);
1347 +            p.setCorePoolSize(s);
1348 +            try {
1349 +                p.setMaximumPoolSize(s - 1);
1350 +                shouldThrow();
1351 +            } catch (IllegalArgumentException success) {}
1352 +            assertEquals(s, p.getCorePoolSize());
1353 +            assertEquals(s, p.getMaximumPoolSize());
1354 +            try {
1355 +                p.setCorePoolSize(s + 1);
1356 +                shouldThrow();
1357 +            } catch (IllegalArgumentException success) {}
1358 +            assertEquals(s, p.getCorePoolSize());
1359 +            assertEquals(s, p.getMaximumPoolSize());
1360 +        }
1361 +        joinPool(p);
1362 +    }
1363 +
1364 +    /**
1365       * setKeepAliveTime throws IllegalArgumentException
1366       * when given a negative value
1367       */
# Line 1865 | Line 1895 | public class ThreadPoolExecutorTest exte
1895                                     LONG_DELAY_MS, MILLISECONDS,
1896                                     new ArrayBlockingQueue<Runnable>(10));
1897          try {
1898 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1899 <            l.add(new StringTask());
1900 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1901 <            l.add(new StringTask());
1902 <            List<Future<String>> futures =
1903 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1904 <            assertEquals(l.size(), futures.size());
1905 <            for (Future future : futures)
1906 <                assertTrue(future.isDone());
1907 <            assertFalse(futures.get(0).isCancelled());
1908 <            assertTrue(futures.get(1).isCancelled());
1898 >            for (long timeout = timeoutMillis();;) {
1899 >                List<Callable<String>> tasks = new ArrayList<>();
1900 >                tasks.add(new StringTask("0"));
1901 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1902 >                tasks.add(new StringTask("2"));
1903 >                long startTime = System.nanoTime();
1904 >                List<Future<String>> futures =
1905 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1906 >                assertEquals(tasks.size(), futures.size());
1907 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1908 >                for (Future future : futures)
1909 >                    assertTrue(future.isDone());
1910 >                assertTrue(futures.get(1).isCancelled());
1911 >                try {
1912 >                    assertEquals("0", futures.get(0).get());
1913 >                    assertEquals("2", futures.get(2).get());
1914 >                    break;
1915 >                } catch (CancellationException retryWithLongerTimeout) {
1916 >                    timeout *= 2;
1917 >                    if (timeout >= LONG_DELAY_MS / 2)
1918 >                        fail("expected exactly one task to be cancelled");
1919 >                }
1920 >            }
1921          } finally {
1922              joinPool(e);
1923          }
# Line 1921 | Line 1963 | public class ThreadPoolExecutorTest exte
1963       * allowCoreThreadTimeOut(true) causes idle threads to time out
1964       */
1965      public void testAllowCoreThreadTimeOut_true() throws Exception {
1966 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1966 >        long keepAliveTime = timeoutMillis();
1967          final ThreadPoolExecutor p =
1968              new ThreadPoolExecutor(2, 10,
1969 <                                   coreThreadTimeOut, MILLISECONDS,
1969 >                                   keepAliveTime, MILLISECONDS,
1970                                     new ArrayBlockingQueue<Runnable>(10));
1971          final CountDownLatch threadStarted = new CountDownLatch(1);
1972          try {
# Line 1935 | Line 1977 | public class ThreadPoolExecutorTest exte
1977                      assertEquals(1, p.getPoolSize());
1978                  }});
1979              await(threadStarted);
1980 <            delay(coreThreadTimeOut);
1980 >            delay(keepAliveTime);
1981              long startTime = System.nanoTime();
1982              while (p.getPoolSize() > 0
1983                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1951 | Line 1993 | public class ThreadPoolExecutorTest exte
1993       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1994       */
1995      public void testAllowCoreThreadTimeOut_false() 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 1964 | Line 2006 | public class ThreadPoolExecutorTest exte
2006                      threadStarted.countDown();
2007                      assertTrue(p.getPoolSize() >= 1);
2008                  }});
2009 <            delay(2 * coreThreadTimeOut);
2009 >            delay(2 * keepAliveTime);
2010              assertTrue(p.getPoolSize() >= 1);
2011          } finally {
2012              joinPool(p);
# Line 1983 | Line 2025 | public class ThreadPoolExecutorTest exte
2025                  done.countDown();
2026              }};
2027          final ThreadPoolExecutor p =
2028 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2028 >            new ThreadPoolExecutor(1, 30,
2029 >                                   60, SECONDS,
2030                                     new ArrayBlockingQueue(30));
2031          try {
2032              for (int i = 0; i < nTasks; ++i) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines