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.57 by jsr166, Mon Sep 14 00:42:48 2015 UTC vs.
Revision 1.66 by jsr166, Sun Oct 4 01:18:25 2015 UTC

# Line 14 | Line 14 | import java.util.ArrayList;
14   import java.util.List;
15   import java.util.concurrent.ArrayBlockingQueue;
16   import java.util.concurrent.BlockingQueue;
17 import java.util.concurrent.CancellationException;
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 29 | 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 131 | 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 624 | Line 633 | public class ThreadPoolExecutorTest exte
633      }
634  
635      /**
636 <     * shutdownNow returns a list containing tasks that were not run
636 >     * shutdownNow returns a list containing tasks that were not run,
637 >     * and those tasks are drained from the queue
638       */
639 <    public void testShutdownNow() {
639 >    public void testShutdownNow() throws InterruptedException {
640 >        final int poolSize = 2;
641 >        final int count = 5;
642 >        final AtomicInteger ran = new AtomicInteger(0);
643          final ThreadPoolExecutor p =
644 <            new ThreadPoolExecutor(1, 1,
644 >            new ThreadPoolExecutor(poolSize, poolSize,
645                                     LONG_DELAY_MS, MILLISECONDS,
646                                     new ArrayBlockingQueue<Runnable>(10));
647 <        List l;
648 <        try {
649 <            for (int i = 0; i < 5; i++)
637 <                p.execute(new MediumPossiblyInterruptedRunnable());
638 <        }
639 <        finally {
647 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
648 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
649 >            threadsStarted.countDown();
650              try {
651 <                l = p.shutdownNow();
652 <            } catch (SecurityException ok) { return; }
651 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
652 >            } catch (InterruptedException success) {}
653 >            ran.getAndIncrement();
654 >        }};
655 >        for (int i = 0; i < count; i++)
656 >            p.execute(waiter);
657 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
658 >        assertEquals(poolSize, p.getActiveCount());
659 >        assertEquals(0, p.getCompletedTaskCount());
660 >        final List<Runnable> queuedTasks;
661 >        try {
662 >            queuedTasks = p.shutdownNow();
663 >        } catch (SecurityException ok) {
664 >            return; // Allowed in case test doesn't have privs
665          }
666          assertTrue(p.isShutdown());
667 <        assertTrue(l.size() <= 4);
667 >        assertTrue(p.getQueue().isEmpty());
668 >        assertEquals(count - poolSize, queuedTasks.size());
669 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
670 >        assertTrue(p.isTerminated());
671 >        assertEquals(poolSize, ran.get());
672 >        assertEquals(poolSize, p.getCompletedTaskCount());
673      }
674  
675      // Exception Tests
# Line 1875 | Line 1902 | public class ThreadPoolExecutorTest exte
1902              l.add(new StringTask());
1903              l.add(new StringTask());
1904              List<Future<String>> futures =
1905 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1905 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1906              assertEquals(2, futures.size());
1907              for (Future<String> future : futures)
1908                  assertSame(TEST_STRING, future.get());
# Line 1895 | Line 1922 | public class ThreadPoolExecutorTest exte
1922          try {
1923              for (long timeout = timeoutMillis();;) {
1924                  List<Callable<String>> tasks = new ArrayList<>();
1925 <                tasks.add(new StringTask());
1925 >                tasks.add(new StringTask("0"));
1926                  tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1927 <                tasks.add(new StringTask());
1927 >                tasks.add(new StringTask("2"));
1928                  long startTime = System.nanoTime();
1929                  List<Future<String>> futures =
1930                      e.invokeAll(tasks, timeout, MILLISECONDS);
# Line 1907 | Line 1934 | public class ThreadPoolExecutorTest exte
1934                      assertTrue(future.isDone());
1935                  assertTrue(futures.get(1).isCancelled());
1936                  try {
1937 <                    assertEquals(TEST_STRING, futures.get(0).get());
1938 <                    assertEquals(TEST_STRING, futures.get(2).get());
1937 >                    assertEquals("0", futures.get(0).get());
1938 >                    assertEquals("2", futures.get(2).get());
1939                      break;
1940                  } catch (CancellationException retryWithLongerTimeout) {
1941                      timeout *= 2;
# Line 2043 | Line 2070 | public class ThreadPoolExecutorTest exte
2070          }
2071      }
2072  
2073 +    /**
2074 +     * get(cancelled task) throws CancellationException
2075 +     */
2076 +    public void testGet_cancelled() throws Exception {
2077 +        final ExecutorService e =
2078 +            new ThreadPoolExecutor(1, 1,
2079 +                                   LONG_DELAY_MS, MILLISECONDS,
2080 +                                   new LinkedBlockingQueue<Runnable>());
2081 +        try {
2082 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
2083 +            final CountDownLatch done = new CountDownLatch(1);
2084 +            final List<Future<?>> futures = new ArrayList<>();
2085 +            for (int i = 0; i < 2; i++) {
2086 +                Runnable r = new CheckedRunnable() { public void realRun()
2087 +                                                         throws Throwable {
2088 +                    blockerStarted.countDown();
2089 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2090 +                }};
2091 +                futures.add(e.submit(r));
2092 +            }
2093 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2094 +            for (Future<?> future : futures) future.cancel(false);
2095 +            for (Future<?> future : futures) {
2096 +                try {
2097 +                    future.get();
2098 +                    shouldThrow();
2099 +                } catch (CancellationException success) {}
2100 +                try {
2101 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
2102 +                    shouldThrow();
2103 +                } catch (CancellationException success) {}
2104 +                assertTrue(future.isCancelled());
2105 +                assertTrue(future.isDone());
2106 +            }
2107 +            done.countDown();
2108 +        } finally {
2109 +            joinPool(e);
2110 +        }
2111 +    }
2112 +
2113   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines