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.117 by jsr166, Sat Mar 25 21:41:10 2017 UTC vs.
Revision 1.121 by jsr166, Sat Jul 15 18:28:08 2017 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
14 + import java.util.Collection;
15 + import java.util.Collections;
16   import java.util.List;
17   import java.util.concurrent.ArrayBlockingQueue;
18   import java.util.concurrent.BlockingQueue;
# Line 27 | Line 29 | import java.util.concurrent.RejectedExec
29   import java.util.concurrent.SynchronousQueue;
30   import java.util.concurrent.ThreadFactory;
31   import java.util.concurrent.ThreadPoolExecutor;
32 + import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
33 + import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
34 + import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
35 + import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
36   import java.util.concurrent.atomic.AtomicInteger;
37 + import java.util.concurrent.atomic.AtomicReference;
38  
39   import junit.framework.Test;
40   import junit.framework.TestSuite;
# Line 282 | Line 289 | public class ThreadPoolExecutorTest exte
289                                     LONG_DELAY_MS, MILLISECONDS,
290                                     new ArrayBlockingQueue<Runnable>(10));
291          try (PoolCleaner cleaner = cleaner(p)) {
292 <            assertTrue(p.getRejectedExecutionHandler()
286 <                       instanceof ThreadPoolExecutor.AbortPolicy);
292 >            assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
293          }
294      }
295  
# Line 470 | Line 476 | public class ThreadPoolExecutorTest exte
476              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
477              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
478              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
479 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
480 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
479 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
480 >                                           randomTimeUnit()));
481              long timeoutNanos = 999999L;
482              long startTime = System.nanoTime();
483              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 1145 | Line 1151 | public class ThreadPoolExecutorTest exte
1151                                     LONG_DELAY_MS,
1152                                     MILLISECONDS,
1153                                     new ArrayBlockingQueue<Runnable>(1),
1154 <                                   new ThreadPoolExecutor.CallerRunsPolicy());
1154 >                                   new CallerRunsPolicy());
1155          try (PoolCleaner cleaner = cleaner(p)) {
1156              final CountDownLatch done = new CountDownLatch(1);
1157              Runnable blocker = new CheckedRunnable() {
# Line 1177 | Line 1183 | public class ThreadPoolExecutorTest exte
1183              new ThreadPoolExecutor(1, 1,
1184                            LONG_DELAY_MS, MILLISECONDS,
1185                            new ArrayBlockingQueue<Runnable>(1),
1186 <                          new ThreadPoolExecutor.DiscardPolicy());
1186 >                          new DiscardPolicy());
1187          try (PoolCleaner cleaner = cleaner(p, done)) {
1188              p.execute(awaiter(done));
1189  
# Line 1203 | Line 1209 | public class ThreadPoolExecutorTest exte
1209              new ThreadPoolExecutor(1, 1,
1210                                     LONG_DELAY_MS, MILLISECONDS,
1211                                     new ArrayBlockingQueue<Runnable>(1),
1212 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1212 >                                   new DiscardOldestPolicy());
1213          try (PoolCleaner cleaner = cleaner(p, done)) {
1214              assertEquals(LatchAwaiter.NEW, r1.state);
1215              assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1241 | Line 1247 | public class ThreadPoolExecutorTest exte
1247       * execute using CallerRunsPolicy drops task on shutdown
1248       */
1249      public void testCallerRunsOnShutdown() {
1250 <        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1250 >        RejectedExecutionHandler h = new CallerRunsPolicy();
1251          final ThreadPoolExecutor p =
1252              new ThreadPoolExecutor(1, 1,
1253                                     LONG_DELAY_MS, MILLISECONDS,
# Line 1263 | Line 1269 | public class ThreadPoolExecutorTest exte
1269              new ThreadPoolExecutor(1, 1,
1270                                     LONG_DELAY_MS, MILLISECONDS,
1271                                     new ArrayBlockingQueue<Runnable>(1),
1272 <                                   new ThreadPoolExecutor.DiscardPolicy());
1272 >                                   new DiscardPolicy());
1273  
1274          try { p.shutdown(); } catch (SecurityException ok) { return; }
1275          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1281 | Line 1287 | public class ThreadPoolExecutorTest exte
1287              new ThreadPoolExecutor(1, 1,
1288                                     LONG_DELAY_MS, MILLISECONDS,
1289                                     new ArrayBlockingQueue<Runnable>(1),
1290 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1290 >                                   new DiscardOldestPolicy());
1291  
1292          try { p.shutdown(); } catch (SecurityException ok) { return; }
1293          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1495 | Line 1501 | public class ThreadPoolExecutorTest exte
1501      }
1502  
1503      /**
1504 <     * invokeAny(empty collection) throws IAE
1504 >     * invokeAny(empty collection) throws IllegalArgumentException
1505       */
1506      public void testInvokeAny2() throws Exception {
1507          final ExecutorService e =
# Line 1585 | Line 1591 | public class ThreadPoolExecutorTest exte
1591      }
1592  
1593      /**
1594 <     * invokeAll(empty collection) returns empty collection
1594 >     * invokeAll(empty collection) returns empty list
1595       */
1596      public void testInvokeAll2() throws InterruptedException {
1597          final ExecutorService e =
1598              new ThreadPoolExecutor(2, 2,
1599                                     LONG_DELAY_MS, MILLISECONDS,
1600                                     new ArrayBlockingQueue<Runnable>(10));
1601 +        final Collection<Callable<String>> emptyCollection
1602 +            = Collections.emptyList();
1603          try (PoolCleaner cleaner = cleaner(e)) {
1604 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1604 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1605              assertTrue(r.isEmpty());
1606          }
1607      }
# Line 1668 | Line 1676 | public class ThreadPoolExecutorTest exte
1676                                     new ArrayBlockingQueue<Runnable>(10));
1677          try (PoolCleaner cleaner = cleaner(e)) {
1678              try {
1679 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1679 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1680                  shouldThrow();
1681              } catch (NullPointerException success) {}
1682          }
# Line 1686 | Line 1694 | public class ThreadPoolExecutorTest exte
1694              List<Callable<String>> l = new ArrayList<>();
1695              l.add(new StringTask());
1696              try {
1697 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1697 >                e.invokeAny(l, randomTimeout(), null);
1698                  shouldThrow();
1699              } catch (NullPointerException success) {}
1700          }
1701      }
1702  
1703      /**
1704 <     * timed invokeAny(empty collection) throws IAE
1704 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1705       */
1706      public void testTimedInvokeAny2() throws Exception {
1707          final ExecutorService e =
# Line 1703 | Line 1711 | public class ThreadPoolExecutorTest exte
1711          try (PoolCleaner cleaner = cleaner(e)) {
1712              try {
1713                  e.invokeAny(new ArrayList<Callable<String>>(),
1714 <                            MEDIUM_DELAY_MS, MILLISECONDS);
1714 >                            randomTimeout(), randomTimeUnit());
1715                  shouldThrow();
1716              } catch (IllegalArgumentException success) {}
1717          }
1718      }
1719  
1720      /**
1721 <     * timed invokeAny(c) throws NPE if c has null elements
1721 >     * timed invokeAny(c) throws NullPointerException if c has null elements
1722       */
1723      public void testTimedInvokeAny3() throws Exception {
1724          final CountDownLatch latch = new CountDownLatch(1);
# Line 1723 | Line 1731 | public class ThreadPoolExecutorTest exte
1731              l.add(latchAwaitingStringTask(latch));
1732              l.add(null);
1733              try {
1734 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1734 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1735                  shouldThrow();
1736              } catch (NullPointerException success) {}
1737              latch.countDown();
# Line 1781 | Line 1789 | public class ThreadPoolExecutorTest exte
1789                                     new ArrayBlockingQueue<Runnable>(10));
1790          try (PoolCleaner cleaner = cleaner(e)) {
1791              try {
1792 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1792 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1793                  shouldThrow();
1794              } catch (NullPointerException success) {}
1795          }
# Line 1799 | Line 1807 | public class ThreadPoolExecutorTest exte
1807              List<Callable<String>> l = new ArrayList<>();
1808              l.add(new StringTask());
1809              try {
1810 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1810 >                e.invokeAll(l, randomTimeout(), null);
1811                  shouldThrow();
1812              } catch (NullPointerException success) {}
1813          }
1814      }
1815  
1816      /**
1817 <     * timed invokeAll(empty collection) returns empty collection
1817 >     * timed invokeAll(empty collection) returns empty list
1818       */
1819      public void testTimedInvokeAll2() throws InterruptedException {
1820          final ExecutorService e =
1821              new ThreadPoolExecutor(2, 2,
1822                                     LONG_DELAY_MS, MILLISECONDS,
1823                                     new ArrayBlockingQueue<Runnable>(10));
1824 +        final Collection<Callable<String>> emptyCollection
1825 +            = Collections.emptyList();
1826          try (PoolCleaner cleaner = cleaner(e)) {
1827 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1828 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1827 >            List<Future<String>> r =
1828 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1829              assertTrue(r.isEmpty());
1830          }
1831      }
# Line 1833 | Line 1843 | public class ThreadPoolExecutorTest exte
1843              l.add(new StringTask());
1844              l.add(null);
1845              try {
1846 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1846 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1847                  shouldThrow();
1848              } catch (NullPointerException success) {}
1849          }
# Line 2075 | Line 2085 | public class ThreadPoolExecutorTest exte
2085          }
2086      }
2087  
2088 +    public void testAbortPolicy() {
2089 +        final RejectedExecutionHandler handler = new AbortPolicy();
2090 +        final ThreadPoolExecutor p =
2091 +            new ThreadPoolExecutor(1, 1,
2092 +                                   LONG_DELAY_MS, MILLISECONDS,
2093 +                                   new ArrayBlockingQueue<Runnable>(10));
2094 +        final TrackedNoOpRunnable r = new TrackedNoOpRunnable();
2095 +        try {
2096 +            handler.rejectedExecution(r, p);
2097 +            shouldThrow();
2098 +        } catch (RejectedExecutionException success) {}
2099 +        assertFalse(r.done);
2100 +        assertEquals(0, p.getTaskCount());
2101 +        assertTrue(p.getQueue().isEmpty());
2102 +    }
2103 +
2104 +    public void testCallerRunsPolicy() {
2105 +        final RejectedExecutionHandler handler = new CallerRunsPolicy();
2106 +        final ThreadPoolExecutor p =
2107 +            new ThreadPoolExecutor(1, 1,
2108 +                                   LONG_DELAY_MS, MILLISECONDS,
2109 +                                   new ArrayBlockingQueue<Runnable>(10));
2110 +        final AtomicReference<Thread> thread = new AtomicReference<>();
2111 +        final Runnable r = new Runnable() { public void run() {
2112 +            thread.set(Thread.currentThread()); }};
2113 +        handler.rejectedExecution(r, p);
2114 +        assertSame(Thread.currentThread(), thread.get());
2115 +        assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
2116 +        assertEquals(0, p.getTaskCount());
2117 +        assertTrue(p.getQueue().isEmpty());
2118 +    }
2119 +
2120   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines