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.41 by dl, Fri May 6 11:22:08 2011 UTC vs.
Revision 1.49 by jsr166, Wed Sep 25 07:39:17 2013 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 + import junit.framework.*;
10   import java.util.concurrent.*;
11   import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 < import java.util.concurrent.atomic.*;
12 < import junit.framework.*;
12 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
13   import java.util.*;
14  
15   public class ThreadPoolExecutorTest extends JSR166TestCase {
# Line 21 | Line 21 | public class ThreadPoolExecutorTest exte
21      }
22  
23      static class ExtendedTPE extends ThreadPoolExecutor {
24 <        volatile boolean beforeCalled = false;
25 <        volatile boolean afterCalled = false;
26 <        volatile boolean terminatedCalled = false;
24 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
25 >        final CountDownLatch afterCalled = new CountDownLatch(1);
26 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
27 >
28          public ExtendedTPE() {
29              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
30          }
31          protected void beforeExecute(Thread t, Runnable r) {
32 <            beforeCalled = true;
32 >            beforeCalled.countDown();
33          }
34          protected void afterExecute(Runnable r, Throwable t) {
35 <            afterCalled = true;
35 >            afterCalled.countDown();
36          }
37          protected void terminated() {
38 <            terminatedCalled = true;
38 >            terminatedCalled.countDown();
39 >        }
40 >
41 >        public boolean beforeCalled() {
42 >            return beforeCalled.getCount() == 0;
43 >        }
44 >        public boolean afterCalled() {
45 >            return afterCalled.getCount() == 0;
46 >        }
47 >        public boolean terminatedCalled() {
48 >            return terminatedCalled.getCount() == 0;
49          }
50      }
51  
# Line 46 | Line 57 | public class ThreadPoolExecutorTest exte
57          }
58      }
59  
49
60      /**
61       * execute successfully executes a runnable
62       */
# Line 150 | Line 160 | public class ThreadPoolExecutorTest exte
160                      threadProceed.await();
161                      threadDone.countDown();
162                  }});
163 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
163 >            await(threadStarted);
164              assertEquals(0, p.getCompletedTaskCount());
165              threadProceed.countDown();
166              threadDone.await();
167 <            delay(SHORT_DELAY_MS);
168 <            assertEquals(1, p.getCompletedTaskCount());
167 >            long startTime = System.nanoTime();
168 >            while (p.getCompletedTaskCount() != 1) {
169 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
170 >                    fail("timed out");
171 >                Thread.yield();
172 >            }
173          } finally {
174              joinPool(p);
175          }
# Line 185 | Line 199 | public class ThreadPoolExecutorTest exte
199          joinPool(p);
200      }
201  
188
202      /**
203       * getThreadFactory returns factory in constructor if not set
204       */
# Line 215 | Line 228 | public class ThreadPoolExecutorTest exte
228          joinPool(p);
229      }
230  
218
231      /**
232       * setThreadFactory(null) throws NPE
233       */
# Line 262 | Line 274 | public class ThreadPoolExecutorTest exte
274          joinPool(p);
275      }
276  
265
277      /**
278       * setRejectedExecutionHandler(null) throws NPE
279       */
# Line 280 | Line 291 | public class ThreadPoolExecutorTest exte
291          }
292      }
293  
283
294      /**
295       * getLargestPoolSize increases, but doesn't overestimate, when
296       * multiple threads active
# Line 378 | Line 388 | public class ThreadPoolExecutorTest exte
388      }
389  
390      /**
391 <     * isShutDown is false before shutdown, true after
391 >     * isShutdown is false before shutdown, true after
392       */
393      public void testIsShutdown() {
394          final ThreadPoolExecutor p =
# Line 391 | Line 401 | public class ThreadPoolExecutorTest exte
401          joinPool(p);
402      }
403  
404 +    /**
405 +     * awaitTermination on a non-shutdown pool times out
406 +     */
407 +    public void testAwaitTermination_timesOut() throws InterruptedException {
408 +        final ThreadPoolExecutor p =
409 +            new ThreadPoolExecutor(1, 1,
410 +                                   LONG_DELAY_MS, MILLISECONDS,
411 +                                   new ArrayBlockingQueue<Runnable>(10));
412 +        assertFalse(p.isTerminated());
413 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
414 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
415 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
416 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
417 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
418 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
419 +        long timeoutNanos = 999999L;
420 +        long startTime = System.nanoTime();
421 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
422 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
423 +        assertFalse(p.isTerminated());
424 +        startTime = System.nanoTime();
425 +        long timeoutMillis = timeoutMillis();
426 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
427 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
428 +        assertFalse(p.isTerminated());
429 +        p.shutdown();
430 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
431 +        assertTrue(p.isTerminated());
432 +    }
433  
434      /**
435       * isTerminated is false before termination, true after
# Line 564 | Line 603 | public class ThreadPoolExecutorTest exte
603      }
604  
605      /**
606 <     * shutDownNow returns a list containing tasks that were not run
606 >     * shutdownNow returns a list containing tasks that were not run
607       */
608 <    public void testShutDownNow() {
608 >    public void testShutdownNow() {
609          final ThreadPoolExecutor p =
610              new ThreadPoolExecutor(1, 1,
611                                     LONG_DELAY_MS, MILLISECONDS,
# Line 587 | Line 626 | public class ThreadPoolExecutorTest exte
626  
627      // Exception Tests
628  
590
629      /**
630       * Constructor throws if corePoolSize argument is less than zero
631       */
# Line 660 | Line 698 | public class ThreadPoolExecutorTest exte
698          } catch (NullPointerException success) {}
699      }
700  
663
664
701      /**
702       * Constructor throws if corePoolSize argument is less than zero
703       */
# Line 753 | Line 789 | public class ThreadPoolExecutorTest exte
789          } catch (NullPointerException success) {}
790      }
791  
756
792      /**
793       * Constructor throws if corePoolSize argument is less than zero
794       */
# Line 845 | Line 880 | public class ThreadPoolExecutorTest exte
880          } catch (NullPointerException success) {}
881      }
882  
848
883      /**
884       * Constructor throws if corePoolSize argument is less than zero
885       */
# Line 1212 | Line 1246 | public class ThreadPoolExecutorTest exte
1246          }
1247      }
1248  
1215
1249      /**
1250       * execute using DiscardOldestPolicy drops task on shutdown
1251       */
# Line 1234 | Line 1267 | public class ThreadPoolExecutorTest exte
1267          }
1268      }
1269  
1237
1270      /**
1271       * execute(null) throws NPE
1272       */
# Line 1307 | Line 1339 | public class ThreadPoolExecutorTest exte
1339          joinPool(p);
1340      }
1341  
1310
1342      /**
1343       * setKeepAliveTime throws IllegalArgumentException
1344       * when given a negative value
# Line 1333 | Line 1364 | public class ThreadPoolExecutorTest exte
1364      public void testTerminated() {
1365          ExtendedTPE p = new ExtendedTPE();
1366          try { p.shutdown(); } catch (SecurityException ok) { return; }
1367 <        assertTrue(p.terminatedCalled);
1367 >        assertTrue(p.terminatedCalled());
1368          joinPool(p);
1369      }
1370  
# Line 1343 | Line 1374 | public class ThreadPoolExecutorTest exte
1374      public void testBeforeAfter() throws InterruptedException {
1375          ExtendedTPE p = new ExtendedTPE();
1376          try {
1377 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1378 <            p.execute(r);
1379 <            delay(SHORT_DELAY_MS);
1380 <            assertTrue(r.done);
1381 <            assertTrue(p.beforeCalled);
1382 <            assertTrue(p.afterCalled);
1377 >            final CountDownLatch done = new CountDownLatch(1);
1378 >            p.execute(new CheckedRunnable() {
1379 >                public void realRun() {
1380 >                    done.countDown();
1381 >                }});
1382 >            await(p.afterCalled);
1383 >            assertEquals(0, done.getCount());
1384 >            assertTrue(p.afterCalled());
1385 >            assertTrue(p.beforeCalled());
1386              try { p.shutdown(); } catch (SecurityException ok) { return; }
1387          } finally {
1388              joinPool(p);
# Line 1406 | Line 1440 | public class ThreadPoolExecutorTest exte
1440          }
1441      }
1442  
1409
1443      /**
1444       * invokeAny(null) throws NPE
1445       */
# Line 1600 | Line 1633 | public class ThreadPoolExecutorTest exte
1633          }
1634      }
1635  
1603
1604
1636      /**
1637       * timed invokeAny(null) throws NPE
1638       */
# Line 1848 | Line 1879 | public class ThreadPoolExecutorTest exte
1879              l.add(new StringTask());
1880              List<Future<String>> futures =
1881                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1882 <            assertEquals(3, futures.size());
1883 <            Iterator<Future<String>> it = futures.iterator();
1884 <            Future<String> f1 = it.next();
1885 <            Future<String> f2 = it.next();
1886 <            Future<String> f3 = it.next();
1856 <            assertTrue(f1.isDone());
1857 <            assertTrue(f2.isDone());
1858 <            assertTrue(f3.isDone());
1859 <            assertFalse(f1.isCancelled());
1860 <            assertTrue(f2.isCancelled());
1882 >            assertEquals(l.size(), futures.size());
1883 >            for (Future future : futures)
1884 >                assertTrue(future.isDone());
1885 >            assertFalse(futures.get(0).isCancelled());
1886 >            assertTrue(futures.get(1).isCancelled());
1887          } finally {
1888              joinPool(e);
1889          }
# Line 1903 | Line 1929 | public class ThreadPoolExecutorTest exte
1929       * allowCoreThreadTimeOut(true) causes idle threads to time out
1930       */
1931      public void testAllowCoreThreadTimeOut_true() throws Exception {
1932 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1933          final ThreadPoolExecutor p =
1934              new ThreadPoolExecutor(2, 10,
1935 <                                   SHORT_DELAY_MS, MILLISECONDS,
1935 >                                   coreThreadTimeOut, MILLISECONDS,
1936                                     new ArrayBlockingQueue<Runnable>(10));
1937          final CountDownLatch threadStarted = new CountDownLatch(1);
1938          try {
1939              p.allowCoreThreadTimeOut(true);
1940              p.execute(new CheckedRunnable() {
1941 <                public void realRun() throws InterruptedException {
1941 >                public void realRun() {
1942                      threadStarted.countDown();
1943                      assertEquals(1, p.getPoolSize());
1944                  }});
1945 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1946 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1947 <                if (p.getPoolSize() == 0)
1948 <                    break;
1949 <                delay(10);
1950 <            }
1945 >            await(threadStarted);
1946 >            delay(coreThreadTimeOut);
1947 >            long startTime = System.nanoTime();
1948 >            while (p.getPoolSize() > 0
1949 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1950 >                Thread.yield();
1951 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1952              assertEquals(0, p.getPoolSize());
1953          } finally {
1954              joinPool(p);
# Line 1931 | Line 1959 | public class ThreadPoolExecutorTest exte
1959       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1960       */
1961      public void testAllowCoreThreadTimeOut_false() throws Exception {
1962 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1963          final ThreadPoolExecutor p =
1964              new ThreadPoolExecutor(2, 10,
1965 <                                   SHORT_DELAY_MS, MILLISECONDS,
1965 >                                   coreThreadTimeOut, MILLISECONDS,
1966                                     new ArrayBlockingQueue<Runnable>(10));
1967          final CountDownLatch threadStarted = new CountDownLatch(1);
1968          try {
# Line 1943 | Line 1972 | public class ThreadPoolExecutorTest exte
1972                      threadStarted.countDown();
1973                      assertTrue(p.getPoolSize() >= 1);
1974                  }});
1975 <            delay(SMALL_DELAY_MS);
1975 >            delay(2 * coreThreadTimeOut);
1976              assertTrue(p.getPoolSize() >= 1);
1977          } finally {
1978              joinPool(p);
# Line 1977 | Line 2006 | public class ThreadPoolExecutorTest exte
2006              // enough time to run all tasks
2007              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2008          } finally {
2009 <            p.shutdown();
2009 >            joinPool(p);
2010          }
2011      }
2012  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines