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.109 by jsr166, Tue Oct 6 06:11:32 2015 UTC vs.
Revision 1.118 by jsr166, Mon May 29 19:15:03 2017 UTC

# Line 18 | Line 18 | 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;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.Future;
23   import java.util.concurrent.FutureTask;
# Line 28 | Line 27 | import java.util.concurrent.RejectedExec
27   import java.util.concurrent.SynchronousQueue;
28   import java.util.concurrent.ThreadFactory;
29   import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
30   import java.util.concurrent.atomic.AtomicInteger;
31  
32   import junit.framework.Test;
# Line 92 | Line 90 | public class ThreadPoolExecutorTest exte
90              final Runnable task = new CheckedRunnable() {
91                  public void realRun() { done.countDown(); }};
92              p.execute(task);
93 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
93 >            await(done);
94          }
95      }
96  
# Line 186 | Line 184 | public class ThreadPoolExecutorTest exte
184                  public void realRun() throws InterruptedException {
185                      threadStarted.countDown();
186                      assertEquals(0, p.getCompletedTaskCount());
187 <                    threadProceed.await();
187 >                    await(threadProceed);
188                      threadDone.countDown();
189                  }});
190              await(threadStarted);
191              assertEquals(0, p.getCompletedTaskCount());
192              threadProceed.countDown();
193 <            threadDone.await();
193 >            await(threadDone);
194              long startTime = System.nanoTime();
195              while (p.getCompletedTaskCount() != 1) {
196                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 276 | Line 274 | public class ThreadPoolExecutorTest exte
274      }
275  
276      /**
277 +     * The default rejected execution handler is AbortPolicy.
278 +     */
279 +    public void testDefaultRejectedExecutionHandler() {
280 +        final ThreadPoolExecutor p =
281 +            new ThreadPoolExecutor(1, 2,
282 +                                   LONG_DELAY_MS, MILLISECONDS,
283 +                                   new ArrayBlockingQueue<Runnable>(10));
284 +        try (PoolCleaner cleaner = cleaner(p)) {
285 +            assertTrue(p.getRejectedExecutionHandler()
286 +                       instanceof ThreadPoolExecutor.AbortPolicy);
287 +        }
288 +    }
289 +
290 +    /**
291       * getRejectedExecutionHandler returns handler in constructor if not set
292       */
293      public void testGetRejectedExecutionHandler() {
# Line 458 | Line 470 | public class ThreadPoolExecutorTest exte
470              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
471              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
472              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
473 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
474 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
473 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
474 >                                           randomTimeUnit()));
475              long timeoutNanos = 999999L;
476              long startTime = System.nanoTime();
477              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 537 | Line 549 | public class ThreadPoolExecutorTest exte
549       */
550      public void testGetQueue() throws InterruptedException {
551          final CountDownLatch done = new CountDownLatch(1);
552 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
552 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
553          final ThreadPoolExecutor p =
554              new ThreadPoolExecutor(1, 1,
555                                     LONG_DELAY_MS, MILLISECONDS,
# Line 569 | Line 581 | public class ThreadPoolExecutorTest exte
581       */
582      public void testRemove() throws InterruptedException {
583          final CountDownLatch done = new CountDownLatch(1);
584 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
584 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
585          final ThreadPoolExecutor p =
586              new ThreadPoolExecutor(1, 1,
587                                     LONG_DELAY_MS, MILLISECONDS,
# Line 604 | Line 616 | public class ThreadPoolExecutorTest exte
616      public void testPurge() throws InterruptedException {
617          final CountDownLatch threadStarted = new CountDownLatch(1);
618          final CountDownLatch done = new CountDownLatch(1);
619 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
619 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
620          final ThreadPoolExecutor p =
621              new ThreadPoolExecutor(1, 1,
622                                     LONG_DELAY_MS, MILLISECONDS,
# Line 1113 | Line 1125 | public class ThreadPoolExecutorTest exte
1125                      await(done);
1126                  }};
1127              for (int i = 0; i < 2; ++i)
1128 <                p.submit(Executors.callable(task));
1128 >                p.execute(task);
1129              for (int i = 0; i < 2; ++i) {
1130                  try {
1131                      p.execute(task);
# Line 1508 | Line 1520 | public class ThreadPoolExecutorTest exte
1520                                     LONG_DELAY_MS, MILLISECONDS,
1521                                     new ArrayBlockingQueue<Runnable>(10));
1522          try (PoolCleaner cleaner = cleaner(e)) {
1523 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1523 >            List<Callable<String>> l = new ArrayList<>();
1524              l.add(latchAwaitingStringTask(latch));
1525              l.add(null);
1526              try {
# Line 1528 | Line 1540 | public class ThreadPoolExecutorTest exte
1540                                     LONG_DELAY_MS, MILLISECONDS,
1541                                     new ArrayBlockingQueue<Runnable>(10));
1542          try (PoolCleaner cleaner = cleaner(e)) {
1543 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1543 >            List<Callable<String>> l = new ArrayList<>();
1544              l.add(new NPETask());
1545              try {
1546                  e.invokeAny(l);
# Line 1548 | Line 1560 | public class ThreadPoolExecutorTest exte
1560                                     LONG_DELAY_MS, MILLISECONDS,
1561                                     new ArrayBlockingQueue<Runnable>(10));
1562          try (PoolCleaner cleaner = cleaner(e)) {
1563 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1563 >            List<Callable<String>> l = new ArrayList<>();
1564              l.add(new StringTask());
1565              l.add(new StringTask());
1566              String result = e.invokeAny(l);
# Line 1595 | Line 1607 | public class ThreadPoolExecutorTest exte
1607                                     LONG_DELAY_MS, MILLISECONDS,
1608                                     new ArrayBlockingQueue<Runnable>(10));
1609          try (PoolCleaner cleaner = cleaner(e)) {
1610 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1610 >            List<Callable<String>> l = new ArrayList<>();
1611              l.add(new StringTask());
1612              l.add(null);
1613              try {
# Line 1614 | Line 1626 | public class ThreadPoolExecutorTest exte
1626                                     LONG_DELAY_MS, MILLISECONDS,
1627                                     new ArrayBlockingQueue<Runnable>(10));
1628          try (PoolCleaner cleaner = cleaner(e)) {
1629 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1629 >            List<Callable<String>> l = new ArrayList<>();
1630              l.add(new NPETask());
1631              List<Future<String>> futures = e.invokeAll(l);
1632              assertEquals(1, futures.size());
# Line 1636 | Line 1648 | public class ThreadPoolExecutorTest exte
1648                                     LONG_DELAY_MS, MILLISECONDS,
1649                                     new ArrayBlockingQueue<Runnable>(10));
1650          try (PoolCleaner cleaner = cleaner(e)) {
1651 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1651 >            List<Callable<String>> l = new ArrayList<>();
1652              l.add(new StringTask());
1653              l.add(new StringTask());
1654              List<Future<String>> futures = e.invokeAll(l);
# Line 1671 | Line 1683 | public class ThreadPoolExecutorTest exte
1683                                     LONG_DELAY_MS, MILLISECONDS,
1684                                     new ArrayBlockingQueue<Runnable>(10));
1685          try (PoolCleaner cleaner = cleaner(e)) {
1686 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1686 >            List<Callable<String>> l = new ArrayList<>();
1687              l.add(new StringTask());
1688              try {
1689                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1707 | Line 1719 | public class ThreadPoolExecutorTest exte
1719                                     LONG_DELAY_MS, MILLISECONDS,
1720                                     new ArrayBlockingQueue<Runnable>(10));
1721          try (PoolCleaner cleaner = cleaner(e)) {
1722 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1722 >            List<Callable<String>> l = new ArrayList<>();
1723              l.add(latchAwaitingStringTask(latch));
1724              l.add(null);
1725              try {
# Line 1727 | Line 1739 | public class ThreadPoolExecutorTest exte
1739                                     LONG_DELAY_MS, MILLISECONDS,
1740                                     new ArrayBlockingQueue<Runnable>(10));
1741          try (PoolCleaner cleaner = cleaner(e)) {
1742 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 >            long startTime = System.nanoTime();
1743 >            List<Callable<String>> l = new ArrayList<>();
1744              l.add(new NPETask());
1745              try {
1746 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1746 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1747                  shouldThrow();
1748              } catch (ExecutionException success) {
1749                  assertTrue(success.getCause() instanceof NullPointerException);
1750              }
1751 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1752          }
1753      }
1754  
# Line 1747 | Line 1761 | public class ThreadPoolExecutorTest exte
1761                                     LONG_DELAY_MS, MILLISECONDS,
1762                                     new ArrayBlockingQueue<Runnable>(10));
1763          try (PoolCleaner cleaner = cleaner(e)) {
1764 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1764 >            long startTime = System.nanoTime();
1765 >            List<Callable<String>> l = new ArrayList<>();
1766              l.add(new StringTask());
1767              l.add(new StringTask());
1768 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1768 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1769              assertSame(TEST_STRING, result);
1770 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1771          }
1772      }
1773  
# Line 1780 | Line 1796 | public class ThreadPoolExecutorTest exte
1796                                     LONG_DELAY_MS, MILLISECONDS,
1797                                     new ArrayBlockingQueue<Runnable>(10));
1798          try (PoolCleaner cleaner = cleaner(e)) {
1799 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1799 >            List<Callable<String>> l = new ArrayList<>();
1800              l.add(new StringTask());
1801              try {
1802                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1813 | Line 1829 | public class ThreadPoolExecutorTest exte
1829                                     LONG_DELAY_MS, MILLISECONDS,
1830                                     new ArrayBlockingQueue<Runnable>(10));
1831          try (PoolCleaner cleaner = cleaner(e)) {
1832 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1832 >            List<Callable<String>> l = new ArrayList<>();
1833              l.add(new StringTask());
1834              l.add(null);
1835              try {
# Line 1832 | Line 1848 | public class ThreadPoolExecutorTest exte
1848                                     LONG_DELAY_MS, MILLISECONDS,
1849                                     new ArrayBlockingQueue<Runnable>(10));
1850          try (PoolCleaner cleaner = cleaner(e)) {
1851 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1851 >            List<Callable<String>> l = new ArrayList<>();
1852              l.add(new NPETask());
1853              List<Future<String>> futures =
1854                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1855 | Line 1871 | public class ThreadPoolExecutorTest exte
1871                                     LONG_DELAY_MS, MILLISECONDS,
1872                                     new ArrayBlockingQueue<Runnable>(10));
1873          try (PoolCleaner cleaner = cleaner(e)) {
1874 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1874 >            List<Callable<String>> l = new ArrayList<>();
1875              l.add(new StringTask());
1876              l.add(new StringTask());
1877              List<Future<String>> futures =
# Line 1870 | Line 1886 | public class ThreadPoolExecutorTest exte
1886       * timed invokeAll(c) cancels tasks not completed by timeout
1887       */
1888      public void testTimedInvokeAll6() throws Exception {
1889 <        final ExecutorService e =
1890 <            new ThreadPoolExecutor(2, 2,
1891 <                                   LONG_DELAY_MS, MILLISECONDS,
1892 <                                   new ArrayBlockingQueue<Runnable>(10));
1893 <        try (PoolCleaner cleaner = cleaner(e)) {
1894 <            for (long timeout = timeoutMillis();;) {
1889 >        for (long timeout = timeoutMillis();;) {
1890 >            final CountDownLatch done = new CountDownLatch(1);
1891 >            final Callable<String> waiter = new CheckedCallable<String>() {
1892 >                public String realCall() {
1893 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1894 >                    catch (InterruptedException ok) {}
1895 >                    return "1"; }};
1896 >            final ExecutorService p =
1897 >                new ThreadPoolExecutor(2, 2,
1898 >                                       LONG_DELAY_MS, MILLISECONDS,
1899 >                                       new ArrayBlockingQueue<Runnable>(10));
1900 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1901                  List<Callable<String>> tasks = new ArrayList<>();
1902                  tasks.add(new StringTask("0"));
1903 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1903 >                tasks.add(waiter);
1904                  tasks.add(new StringTask("2"));
1905                  long startTime = System.nanoTime();
1906                  List<Future<String>> futures =
1907 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1907 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1908                  assertEquals(tasks.size(), futures.size());
1909                  assertTrue(millisElapsedSince(startTime) >= timeout);
1910                  for (Future future : futures)
# Line 1919 | Line 1941 | public class ThreadPoolExecutorTest exte
1941                      public void realRun() {
1942                          done.countDown();
1943                      }});
1944 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1944 >            await(done);
1945          }
1946      }
1947  
# Line 2012 | Line 2034 | public class ThreadPoolExecutorTest exte
2034                  }
2035              }
2036              // enough time to run all tasks
2037 <            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2037 >            await(done, nTasks * SHORT_DELAY_MS);
2038          }
2039      }
2040  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines