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.22 by jsr166, Mon Oct 11 08:28:05 2010 UTC vs.
Revision 1.30 by jsr166, Sun May 29 07:01:17 2011 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   */
# Line 115 | Line 115 | public class ThreadPoolExecutorSubclassT
115          }
116      }
117  
118
118      static class CustomTPE extends ThreadPoolExecutor {
119          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
120              return new CustomTask<V>(c);
# Line 162 | Line 161 | public class ThreadPoolExecutorSubclassT
161                workQueue, threadFactory, handler);
162          }
163  
164 <        volatile boolean beforeCalled = false;
165 <        volatile boolean afterCalled = false;
166 <        volatile boolean terminatedCalled = false;
164 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
165 >        final CountDownLatch afterCalled = new CountDownLatch(1);
166 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
167 >
168          public CustomTPE() {
169              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
170          }
171          protected void beforeExecute(Thread t, Runnable r) {
172 <            beforeCalled = true;
172 >            beforeCalled.countDown();
173          }
174          protected void afterExecute(Runnable r, Throwable t) {
175 <            afterCalled = true;
175 >            afterCalled.countDown();
176          }
177          protected void terminated() {
178 <            terminatedCalled = true;
178 >            terminatedCalled.countDown();
179          }
180  
181 +        public boolean beforeCalled() {
182 +            return beforeCalled.getCount() == 0;
183 +        }
184 +        public boolean afterCalled() {
185 +            return afterCalled.getCount() == 0;
186 +        }
187 +        public boolean terminatedCalled() {
188 +            return terminatedCalled.getCount() == 0;
189 +        }
190      }
191  
192      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 197 | public class ThreadPoolExecutorSubclassT
197          }
198      }
199  
191
200      /**
201       * execute successfully executes a runnable
202       */
# Line 286 | Line 294 | public class ThreadPoolExecutorSubclassT
294                      threadProceed.await();
295                      threadDone.countDown();
296                  }});
297 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
297 >            await(threadStarted);
298              assertEquals(0, p.getCompletedTaskCount());
299              threadProceed.countDown();
300              threadDone.await();
301 <            Thread.sleep(SHORT_DELAY_MS);
302 <            assertEquals(1, p.getCompletedTaskCount());
301 >            long startTime = System.nanoTime();
302 >            while (p.getCompletedTaskCount() != 1) {
303 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
304 >                    fail("timed out");
305 >                Thread.yield();
306 >            }
307          } finally {
308              joinPool(p);
309          }
# Line 315 | Line 327 | public class ThreadPoolExecutorSubclassT
327          joinPool(p);
328      }
329  
318
330      /**
331       * getThreadFactory returns factory in constructor if not set
332       */
# Line 337 | Line 348 | public class ThreadPoolExecutorSubclassT
348          joinPool(p);
349      }
350  
340
351      /**
352       * setThreadFactory(null) throws NPE
353       */
# Line 374 | Line 384 | public class ThreadPoolExecutorSubclassT
384          joinPool(p);
385      }
386  
377
387      /**
388       * setRejectedExecutionHandler(null) throws NPE
389       */
# Line 389 | Line 398 | public class ThreadPoolExecutorSubclassT
398          }
399      }
400  
392
401      /**
402       * getLargestPoolSize increases, but doesn't overestimate, when
403       * multiple threads active
# Line 484 | Line 492 | public class ThreadPoolExecutorSubclassT
492      }
493  
494      /**
495 <     * isShutDown is false before shutdown, true after
495 >     * isShutdown is false before shutdown, true after
496       */
497      public void testIsShutdown() {
498  
# Line 495 | Line 503 | public class ThreadPoolExecutorSubclassT
503          joinPool(p);
504      }
505  
498
506      /**
507       * isTerminated is false before termination, true after
508       */
# Line 510 | Line 517 | public class ThreadPoolExecutorSubclassT
517              assertFalse(p.isTerminating());
518              p.execute(new CheckedRunnable() {
519                  public void realRun() throws InterruptedException {
513                    threadStarted.countDown();
520                      assertFalse(p.isTerminating());
521 +                    threadStarted.countDown();
522                      done.await();
523                  }});
524              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 539 | Line 546 | public class ThreadPoolExecutorSubclassT
546              assertFalse(p.isTerminating());
547              p.execute(new CheckedRunnable() {
548                  public void realRun() throws InterruptedException {
542                    threadStarted.countDown();
549                      assertFalse(p.isTerminating());
550 +                    threadStarted.countDown();
551                      done.await();
552                  }});
553              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 669 | Line 676 | public class ThreadPoolExecutorSubclassT
676      }
677  
678      /**
679 <     * shutDownNow returns a list containing tasks that were not run
679 >     * shutdownNow returns a list containing tasks that were not run
680       */
681 <    public void testShutDownNow() {
681 >    public void testShutdownNow() {
682          ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
683          List l;
684          try {
# Line 689 | Line 696 | public class ThreadPoolExecutorSubclassT
696  
697      // Exception Tests
698  
692
699      /**
700       * Constructor throws if corePoolSize argument is less than zero
701       */
# Line 750 | Line 756 | public class ThreadPoolExecutorSubclassT
756          } catch (NullPointerException success) {}
757      }
758  
753
754
759      /**
760       * Constructor throws if corePoolSize argument is less than zero
761       */
# Line 823 | Line 827 | public class ThreadPoolExecutorSubclassT
827          } catch (NullPointerException success) {}
828      }
829  
826
830      /**
831       * Constructor throws if corePoolSize argument is less than zero
832       */
# Line 895 | Line 898 | public class ThreadPoolExecutorSubclassT
898          } catch (NullPointerException success) {}
899      }
900  
898
901      /**
902       * Constructor throws if corePoolSize argument is less than zero
903       */
# Line 981 | Line 983 | public class ThreadPoolExecutorSubclassT
983          } catch (NullPointerException success) {}
984      }
985  
984
986      /**
987       * execute throws RejectedExecutionException if saturated.
988       */
# Line 1131 | Line 1132 | public class ThreadPoolExecutorSubclassT
1132          }
1133      }
1134  
1134
1135      /**
1136       * execute using DiscardOldestPolicy drops task on shutdown
1137       */
# Line 1149 | Line 1149 | public class ThreadPoolExecutorSubclassT
1149          }
1150      }
1151  
1152
1152      /**
1153       * execute(null) throws NPE
1154       */
# Line 1214 | Line 1213 | public class ThreadPoolExecutorSubclassT
1213          joinPool(p);
1214      }
1215  
1217
1216      /**
1217       * setKeepAliveTime throws IllegalArgumentException
1218       * when given a negative value
# Line 1239 | Line 1237 | public class ThreadPoolExecutorSubclassT
1237      public void testTerminated() {
1238          CustomTPE p = new CustomTPE();
1239          try { p.shutdown(); } catch (SecurityException ok) { return; }
1240 <        assertTrue(p.terminatedCalled);
1240 >        assertTrue(p.terminatedCalled());
1241          joinPool(p);
1242      }
1243  
# Line 1249 | Line 1247 | public class ThreadPoolExecutorSubclassT
1247      public void testBeforeAfter() throws InterruptedException {
1248          CustomTPE p = new CustomTPE();
1249          try {
1250 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1251 <            p.execute(r);
1252 <            Thread.sleep(SHORT_DELAY_MS);
1253 <            assertTrue(r.done);
1254 <            assertTrue(p.beforeCalled);
1255 <            assertTrue(p.afterCalled);
1250 >            final CountDownLatch done = new CountDownLatch(1);
1251 >            final CheckedRunnable task = new CheckedRunnable() {
1252 >                public void realRun() {
1253 >                    done.countDown();
1254 >                }};
1255 >            p.execute(task);
1256 >            await(p.afterCalled);
1257 >            assertEquals(0, done.getCount());
1258 >            assertTrue(p.afterCalled());
1259 >            assertTrue(p.beforeCalled());
1260              try { p.shutdown(); } catch (SecurityException ok) { return; }
1261          } finally {
1262              joinPool(p);
# Line 1303 | Line 1305 | public class ThreadPoolExecutorSubclassT
1305          }
1306      }
1307  
1306
1308      /**
1309       * invokeAny(null) throws NPE
1310       */
# Line 1465 | Line 1466 | public class ThreadPoolExecutorSubclassT
1466          }
1467      }
1468  
1468
1469
1469      /**
1470       * timed invokeAny(null) throws NPE
1471       */
# Line 1674 | Line 1673 | public class ThreadPoolExecutorSubclassT
1673              l.add(new StringTask());
1674              List<Future<String>> futures =
1675                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1676 <            assertEquals(3, futures.size());
1677 <            Iterator<Future<String>> it = futures.iterator();
1678 <            Future<String> f1 = it.next();
1679 <            Future<String> f2 = it.next();
1680 <            Future<String> f3 = it.next();
1682 <            assertTrue(f1.isDone());
1683 <            assertTrue(f2.isDone());
1684 <            assertTrue(f3.isDone());
1685 <            assertFalse(f1.isCancelled());
1686 <            assertTrue(f2.isCancelled());
1676 >            assertEquals(l.size(), futures.size());
1677 >            for (Future future : futures)
1678 >                assertTrue(future.isDone());
1679 >            assertFalse(futures.get(0).isCancelled());
1680 >            assertTrue(futures.get(1).isCancelled());
1681          } finally {
1682              joinPool(e);
1683          }
# Line 1726 | Line 1720 | public class ThreadPoolExecutorSubclassT
1720       * allowCoreThreadTimeOut(true) causes idle threads to time out
1721       */
1722      public void testAllowCoreThreadTimeOut_true() throws Exception {
1723 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1724          final ThreadPoolExecutor p =
1725              new CustomTPE(2, 10,
1726 <                          SHORT_DELAY_MS, MILLISECONDS,
1726 >                          coreThreadTimeOut, MILLISECONDS,
1727                            new ArrayBlockingQueue<Runnable>(10));
1728          final CountDownLatch threadStarted = new CountDownLatch(1);
1729          try {
# Line 1738 | Line 1733 | public class ThreadPoolExecutorSubclassT
1733                      threadStarted.countDown();
1734                      assertEquals(1, p.getPoolSize());
1735                  }});
1736 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1737 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1738 <                if (p.getPoolSize() == 0)
1739 <                    break;
1740 <                Thread.sleep(10);
1741 <            }
1736 >            await(threadStarted);
1737 >            delay(coreThreadTimeOut);
1738 >            long startTime = System.nanoTime();
1739 >            while (p.getPoolSize() > 0
1740 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1741 >                Thread.yield();
1742 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1743              assertEquals(0, p.getPoolSize());
1744          } finally {
1745              joinPool(p);
# Line 1754 | Line 1750 | public class ThreadPoolExecutorSubclassT
1750       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1751       */
1752      public void testAllowCoreThreadTimeOut_false() throws Exception {
1753 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1754          final ThreadPoolExecutor p =
1755              new CustomTPE(2, 10,
1756 <                          SHORT_DELAY_MS, MILLISECONDS,
1756 >                          coreThreadTimeOut, MILLISECONDS,
1757                            new ArrayBlockingQueue<Runnable>(10));
1758          final CountDownLatch threadStarted = new CountDownLatch(1);
1759          try {
# Line 1766 | Line 1763 | public class ThreadPoolExecutorSubclassT
1763                      threadStarted.countDown();
1764                      assertTrue(p.getPoolSize() >= 1);
1765                  }});
1766 <            Thread.sleep(SMALL_DELAY_MS);
1766 >            delay(2 * coreThreadTimeOut);
1767              assertTrue(p.getPoolSize() >= 1);
1768          } finally {
1769              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines