ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.38 by jsr166, Fri Sep 4 20:08:27 2015 UTC vs.
Revision 1.43 by jsr166, Mon Sep 28 08:23:49 2015 UTC

# Line 14 | Line 14 | import java.util.List;
14   import java.util.concurrent.ArrayBlockingQueue;
15   import java.util.concurrent.BlockingQueue;
16   import java.util.concurrent.Callable;
17 + import java.util.concurrent.CancellationException;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.ExecutionException;
20   import java.util.concurrent.Executors;
# Line 29 | Line 30 | import java.util.concurrent.ThreadFactor
30   import java.util.concurrent.ThreadPoolExecutor;
31   import java.util.concurrent.TimeoutException;
32   import java.util.concurrent.TimeUnit;
33 + import java.util.concurrent.atomic.AtomicInteger;
34   import java.util.concurrent.locks.Condition;
35   import java.util.concurrent.locks.ReentrantLock;
36  
# Line 698 | Line 700 | public class ThreadPoolExecutorSubclassT
700      }
701  
702      /**
703 <     * shutdownNow returns a list containing tasks that were not run
703 >     * shutdownNow returns a list containing tasks that were not run,
704 >     * and those tasks are drained from the queue
705       */
706 <    public void testShutdownNow() {
707 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
708 <        List l;
709 <        try {
710 <            for (int i = 0; i < 5; i++)
711 <                p.execute(new MediumPossiblyInterruptedRunnable());
712 <        }
713 <        finally {
706 >    public void testShutdownNow() throws InterruptedException {
707 >        final int poolSize = 2;
708 >        final int count = 5;
709 >        final AtomicInteger ran = new AtomicInteger(0);
710 >        ThreadPoolExecutor p =
711 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
712 >                          new ArrayBlockingQueue<Runnable>(10));
713 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
714 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
715 >            threadsStarted.countDown();
716              try {
717 <                l = p.shutdownNow();
718 <            } catch (SecurityException ok) { return; }
717 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
718 >            } catch (InterruptedException success) {}
719 >            ran.getAndIncrement();
720 >        }};
721 >        for (int i = 0; i < count; i++)
722 >            p.execute(waiter);
723 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
724 >        assertEquals(poolSize, p.getActiveCount());
725 >        assertEquals(0, p.getCompletedTaskCount());
726 >        final List<Runnable> queuedTasks;
727 >        try {
728 >            queuedTasks = p.shutdownNow();
729 >        } catch (SecurityException ok) {
730 >            return; // Allowed in case test doesn't have privs
731          }
732          assertTrue(p.isShutdown());
733 <        assertTrue(l.size() <= 4);
733 >        assertTrue(p.getQueue().isEmpty());
734 >        assertEquals(count - poolSize, queuedTasks.size());
735 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
736 >        assertTrue(p.isTerminated());
737 >        assertEquals(poolSize, ran.get());
738 >        assertEquals(poolSize, p.getCompletedTaskCount());
739      }
740  
741      // Exception Tests
# Line 1737 | Line 1759 | public class ThreadPoolExecutorSubclassT
1759      public void testTimedInvokeAll6() throws Exception {
1760          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1761          try {
1762 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1763 <            l.add(new StringTask());
1764 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1765 <            l.add(new StringTask());
1766 <            List<Future<String>> futures =
1767 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1768 <            assertEquals(l.size(), futures.size());
1769 <            for (Future future : futures)
1770 <                assertTrue(future.isDone());
1771 <            assertFalse(futures.get(0).isCancelled());
1772 <            assertTrue(futures.get(1).isCancelled());
1762 >            for (long timeout = timeoutMillis();;) {
1763 >                List<Callable<String>> tasks = new ArrayList<>();
1764 >                tasks.add(new StringTask("0"));
1765 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1766 >                tasks.add(new StringTask("2"));
1767 >                long startTime = System.nanoTime();
1768 >                List<Future<String>> futures =
1769 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1770 >                assertEquals(tasks.size(), futures.size());
1771 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1772 >                for (Future future : futures)
1773 >                    assertTrue(future.isDone());
1774 >                assertTrue(futures.get(1).isCancelled());
1775 >                try {
1776 >                    assertEquals("0", futures.get(0).get());
1777 >                    assertEquals("2", futures.get(2).get());
1778 >                    break;
1779 >                } catch (CancellationException retryWithLongerTimeout) {
1780 >                    timeout *= 2;
1781 >                    if (timeout >= LONG_DELAY_MS / 2)
1782 >                        fail("expected exactly one task to be cancelled");
1783 >                }
1784 >            }
1785          } finally {
1786              joinPool(e);
1787          }
# Line 1790 | Line 1824 | public class ThreadPoolExecutorSubclassT
1824       * allowCoreThreadTimeOut(true) causes idle threads to time out
1825       */
1826      public void testAllowCoreThreadTimeOut_true() throws Exception {
1827 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1827 >        long keepAliveTime = timeoutMillis();
1828          final ThreadPoolExecutor p =
1829              new CustomTPE(2, 10,
1830 <                          coreThreadTimeOut, MILLISECONDS,
1830 >                          keepAliveTime, MILLISECONDS,
1831                            new ArrayBlockingQueue<Runnable>(10));
1832          final CountDownLatch threadStarted = new CountDownLatch(1);
1833          try {
1834              p.allowCoreThreadTimeOut(true);
1835              p.execute(new CheckedRunnable() {
1836 <                public void realRun() throws InterruptedException {
1836 >                public void realRun() {
1837                      threadStarted.countDown();
1838                      assertEquals(1, p.getPoolSize());
1839                  }});
1840              await(threadStarted);
1841 <            delay(coreThreadTimeOut);
1841 >            delay(keepAliveTime);
1842              long startTime = System.nanoTime();
1843              while (p.getPoolSize() > 0
1844                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1820 | Line 1854 | public class ThreadPoolExecutorSubclassT
1854       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1855       */
1856      public void testAllowCoreThreadTimeOut_false() throws Exception {
1857 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1857 >        long keepAliveTime = timeoutMillis();
1858          final ThreadPoolExecutor p =
1859              new CustomTPE(2, 10,
1860 <                          coreThreadTimeOut, MILLISECONDS,
1860 >                          keepAliveTime, MILLISECONDS,
1861                            new ArrayBlockingQueue<Runnable>(10));
1862          final CountDownLatch threadStarted = new CountDownLatch(1);
1863          try {
# Line 1833 | Line 1867 | public class ThreadPoolExecutorSubclassT
1867                      threadStarted.countDown();
1868                      assertTrue(p.getPoolSize() >= 1);
1869                  }});
1870 <            delay(2 * coreThreadTimeOut);
1870 >            delay(2 * keepAliveTime);
1871              assertTrue(p.getPoolSize() >= 1);
1872          } finally {
1873              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines