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.36 by jsr166, Fri May 15 17:07:27 2015 UTC vs.
Revision 1.41 by jsr166, Mon Sep 28 02:41:29 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 345 | Line 347 | public class ThreadPoolExecutorSubclassT
347       */
348      public void testGetKeepAliveTime() {
349          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
350 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
350 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
351          joinPool(p);
352      }
353  
# 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 >        CheckedRunnable 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 >        final List<Runnable> queuedTasks;
725 >        try {
726 >            queuedTasks = p.shutdownNow();
727 >        } catch (SecurityException ok) {
728 >            return; // Allowed in case test doesn't have privs
729          }
730          assertTrue(p.isShutdown());
731 <        assertTrue(l.size() <= 4);
731 >        assertTrue(p.getQueue().isEmpty());
732 >        assertEquals(count - poolSize, queuedTasks.size());
733 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
734 >        assertTrue(p.isTerminated());
735 >        assertEquals(poolSize, ran.get());
736      }
737  
738      // Exception Tests
# Line 1223 | Line 1242 | public class ThreadPoolExecutorSubclassT
1242       * execute(null) throws NPE
1243       */
1244      public void testExecuteNull() {
1245 <        ThreadPoolExecutor p = null;
1245 >        ThreadPoolExecutor p =
1246 >            new CustomTPE(1, 2, 1L, SECONDS,
1247 >                          new ArrayBlockingQueue<Runnable>(10));
1248          try {
1228            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1249              p.execute(null);
1250              shouldThrow();
1251          } catch (NullPointerException success) {}
# Line 1736 | Line 1756 | public class ThreadPoolExecutorSubclassT
1756      public void testTimedInvokeAll6() throws Exception {
1757          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758          try {
1759 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1760 <            l.add(new StringTask());
1761 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1762 <            l.add(new StringTask());
1763 <            List<Future<String>> futures =
1764 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1765 <            assertEquals(l.size(), futures.size());
1766 <            for (Future future : futures)
1767 <                assertTrue(future.isDone());
1768 <            assertFalse(futures.get(0).isCancelled());
1769 <            assertTrue(futures.get(1).isCancelled());
1759 >            for (long timeout = timeoutMillis();;) {
1760 >                List<Callable<String>> tasks = new ArrayList<>();
1761 >                tasks.add(new StringTask("0"));
1762 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1763 >                tasks.add(new StringTask("2"));
1764 >                long startTime = System.nanoTime();
1765 >                List<Future<String>> futures =
1766 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1767 >                assertEquals(tasks.size(), futures.size());
1768 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1769 >                for (Future future : futures)
1770 >                    assertTrue(future.isDone());
1771 >                assertTrue(futures.get(1).isCancelled());
1772 >                try {
1773 >                    assertEquals("0", futures.get(0).get());
1774 >                    assertEquals("2", futures.get(2).get());
1775 >                    break;
1776 >                } catch (CancellationException retryWithLongerTimeout) {
1777 >                    timeout *= 2;
1778 >                    if (timeout >= LONG_DELAY_MS / 2)
1779 >                        fail("expected exactly one task to be cancelled");
1780 >                }
1781 >            }
1782          } finally {
1783              joinPool(e);
1784          }
# Line 1789 | Line 1821 | public class ThreadPoolExecutorSubclassT
1821       * allowCoreThreadTimeOut(true) causes idle threads to time out
1822       */
1823      public void testAllowCoreThreadTimeOut_true() throws Exception {
1824 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1824 >        long keepAliveTime = timeoutMillis();
1825          final ThreadPoolExecutor p =
1826              new CustomTPE(2, 10,
1827 <                          coreThreadTimeOut, MILLISECONDS,
1827 >                          keepAliveTime, MILLISECONDS,
1828                            new ArrayBlockingQueue<Runnable>(10));
1829          final CountDownLatch threadStarted = new CountDownLatch(1);
1830          try {
1831              p.allowCoreThreadTimeOut(true);
1832              p.execute(new CheckedRunnable() {
1833 <                public void realRun() throws InterruptedException {
1833 >                public void realRun() {
1834                      threadStarted.countDown();
1835                      assertEquals(1, p.getPoolSize());
1836                  }});
1837              await(threadStarted);
1838 <            delay(coreThreadTimeOut);
1838 >            delay(keepAliveTime);
1839              long startTime = System.nanoTime();
1840              while (p.getPoolSize() > 0
1841                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1819 | Line 1851 | public class ThreadPoolExecutorSubclassT
1851       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1852       */
1853      public void testAllowCoreThreadTimeOut_false() throws Exception {
1854 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1854 >        long keepAliveTime = timeoutMillis();
1855          final ThreadPoolExecutor p =
1856              new CustomTPE(2, 10,
1857 <                          coreThreadTimeOut, MILLISECONDS,
1857 >                          keepAliveTime, MILLISECONDS,
1858                            new ArrayBlockingQueue<Runnable>(10));
1859          final CountDownLatch threadStarted = new CountDownLatch(1);
1860          try {
# Line 1832 | Line 1864 | public class ThreadPoolExecutorSubclassT
1864                      threadStarted.countDown();
1865                      assertTrue(p.getPoolSize() >= 1);
1866                  }});
1867 <            delay(2 * coreThreadTimeOut);
1867 >            delay(2 * keepAliveTime);
1868              assertTrue(p.getPoolSize() >= 1);
1869          } finally {
1870              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines