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.55 by jsr166, Fri Sep 4 20:08: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 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 1892 | 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 1948 | 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 1962 | 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 1978 | 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 1991 | 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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines