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.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC vs.
Revision 1.48 by jsr166, Mon May 20 16:51:56 2013 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
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 <            Thread.sleep(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 406 | Line 445 | public class ThreadPoolExecutorTest exte
445          try {
446              p.execute(new CheckedRunnable() {
447                  public void realRun() throws InterruptedException {
409                    threadStarted.countDown();
448                      assertFalse(p.isTerminated());
449 +                    threadStarted.countDown();
450                      done.await();
451                  }});
452              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
453 +            assertFalse(p.isTerminating());
454              done.countDown();
455          } finally {
456              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 433 | Line 473 | public class ThreadPoolExecutorTest exte
473              assertFalse(p.isTerminating());
474              p.execute(new CheckedRunnable() {
475                  public void realRun() throws InterruptedException {
436                    threadStarted.countDown();
476                      assertFalse(p.isTerminating());
477 +                    threadStarted.countDown();
478                      done.await();
479                  }});
480              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 563 | 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 586 | Line 626 | public class ThreadPoolExecutorTest exte
626  
627      // Exception Tests
628  
589
629      /**
630       * Constructor throws if corePoolSize argument is less than zero
631       */
# Line 659 | Line 698 | public class ThreadPoolExecutorTest exte
698          } catch (NullPointerException success) {}
699      }
700  
662
663
701      /**
702       * Constructor throws if corePoolSize argument is less than zero
703       */
# Line 752 | Line 789 | public class ThreadPoolExecutorTest exte
789          } catch (NullPointerException success) {}
790      }
791  
755
792      /**
793       * Constructor throws if corePoolSize argument is less than zero
794       */
# Line 844 | Line 880 | public class ThreadPoolExecutorTest exte
880          } catch (NullPointerException success) {}
881      }
882  
847
883      /**
884       * Constructor throws if corePoolSize argument is less than zero
885       */
# Line 1211 | Line 1246 | public class ThreadPoolExecutorTest exte
1246          }
1247      }
1248  
1214
1249      /**
1250       * execute using DiscardOldestPolicy drops task on shutdown
1251       */
# Line 1233 | Line 1267 | public class ThreadPoolExecutorTest exte
1267          }
1268      }
1269  
1236
1270      /**
1271       * execute(null) throws NPE
1272       */
# Line 1306 | Line 1339 | public class ThreadPoolExecutorTest exte
1339          joinPool(p);
1340      }
1341  
1309
1342      /**
1343       * setKeepAliveTime throws IllegalArgumentException
1344       * when given a negative value
# Line 1332 | 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 1342 | 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 <            Thread.sleep(SHORT_DELAY_MS);
1380 <            assertTrue(r.done);
1381 <            assertTrue(p.beforeCalled);
1382 <            assertTrue(p.afterCalled);
1377 >            final CountDownLatch done = new CountDownLatch(1);
1378 >            final CheckedRunnable task = new CheckedRunnable() {
1379 >                public void realRun() {
1380 >                    done.countDown();
1381 >                }};
1382 >            p.execute(task);
1383 >            await(p.afterCalled);
1384 >            assertEquals(0, done.getCount());
1385 >            assertTrue(p.afterCalled());
1386 >            assertTrue(p.beforeCalled());
1387              try { p.shutdown(); } catch (SecurityException ok) { return; }
1388          } finally {
1389              joinPool(p);
# Line 1405 | Line 1441 | public class ThreadPoolExecutorTest exte
1441          }
1442      }
1443  
1408
1444      /**
1445       * invokeAny(null) throws NPE
1446       */
# Line 1599 | Line 1634 | public class ThreadPoolExecutorTest exte
1634          }
1635      }
1636  
1602
1603
1637      /**
1638       * timed invokeAny(null) throws NPE
1639       */
# Line 1847 | Line 1880 | public class ThreadPoolExecutorTest exte
1880              l.add(new StringTask());
1881              List<Future<String>> futures =
1882                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1883 <            assertEquals(3, futures.size());
1884 <            Iterator<Future<String>> it = futures.iterator();
1885 <            Future<String> f1 = it.next();
1886 <            Future<String> f2 = it.next();
1887 <            Future<String> f3 = it.next();
1855 <            assertTrue(f1.isDone());
1856 <            assertTrue(f2.isDone());
1857 <            assertTrue(f3.isDone());
1858 <            assertFalse(f1.isCancelled());
1859 <            assertTrue(f2.isCancelled());
1883 >            assertEquals(l.size(), futures.size());
1884 >            for (Future future : futures)
1885 >                assertTrue(future.isDone());
1886 >            assertFalse(futures.get(0).isCancelled());
1887 >            assertTrue(futures.get(1).isCancelled());
1888          } finally {
1889              joinPool(e);
1890          }
# Line 1902 | Line 1930 | public class ThreadPoolExecutorTest exte
1930       * allowCoreThreadTimeOut(true) causes idle threads to time out
1931       */
1932      public void testAllowCoreThreadTimeOut_true() throws Exception {
1933 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1934          final ThreadPoolExecutor p =
1935              new ThreadPoolExecutor(2, 10,
1936 <                                   SHORT_DELAY_MS, MILLISECONDS,
1936 >                                   coreThreadTimeOut, MILLISECONDS,
1937                                     new ArrayBlockingQueue<Runnable>(10));
1938          final CountDownLatch threadStarted = new CountDownLatch(1);
1939          try {
1940              p.allowCoreThreadTimeOut(true);
1941              p.execute(new CheckedRunnable() {
1942 <                public void realRun() throws InterruptedException {
1942 >                public void realRun() {
1943                      threadStarted.countDown();
1944                      assertEquals(1, p.getPoolSize());
1945                  }});
1946 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1947 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1948 <                if (p.getPoolSize() == 0)
1949 <                    break;
1950 <                Thread.sleep(10);
1951 <            }
1946 >            await(threadStarted);
1947 >            delay(coreThreadTimeOut);
1948 >            long startTime = System.nanoTime();
1949 >            while (p.getPoolSize() > 0
1950 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1951 >                Thread.yield();
1952 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1953              assertEquals(0, p.getPoolSize());
1954          } finally {
1955              joinPool(p);
# Line 1930 | Line 1960 | public class ThreadPoolExecutorTest exte
1960       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1961       */
1962      public void testAllowCoreThreadTimeOut_false() throws Exception {
1963 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1964          final ThreadPoolExecutor p =
1965              new ThreadPoolExecutor(2, 10,
1966 <                                   SHORT_DELAY_MS, MILLISECONDS,
1966 >                                   coreThreadTimeOut, MILLISECONDS,
1967                                     new ArrayBlockingQueue<Runnable>(10));
1968          final CountDownLatch threadStarted = new CountDownLatch(1);
1969          try {
# Line 1942 | Line 1973 | public class ThreadPoolExecutorTest exte
1973                      threadStarted.countDown();
1974                      assertTrue(p.getPoolSize() >= 1);
1975                  }});
1976 <            Thread.sleep(SMALL_DELAY_MS);
1976 >            delay(2 * coreThreadTimeOut);
1977              assertTrue(p.getPoolSize() >= 1);
1978          } finally {
1979              joinPool(p);
# Line 1976 | Line 2007 | public class ThreadPoolExecutorTest exte
2007              // enough time to run all tasks
2008              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2009          } finally {
2010 <            p.shutdown();
2010 >            joinPool(p);
2011          }
2012      }
2013  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines