--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2011/05/06 11:22:08 1.41 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2011/05/29 13:55:36 1.46 @@ -21,20 +21,31 @@ public class ThreadPoolExecutorTest exte } static class ExtendedTPE extends ThreadPoolExecutor { - volatile boolean beforeCalled = false; - volatile boolean afterCalled = false; - volatile boolean terminatedCalled = false; + final CountDownLatch beforeCalled = new CountDownLatch(1); + final CountDownLatch afterCalled = new CountDownLatch(1); + final CountDownLatch terminatedCalled = new CountDownLatch(1); + public ExtendedTPE() { super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue()); } protected void beforeExecute(Thread t, Runnable r) { - beforeCalled = true; + beforeCalled.countDown(); } protected void afterExecute(Runnable r, Throwable t) { - afterCalled = true; + afterCalled.countDown(); } protected void terminated() { - terminatedCalled = true; + terminatedCalled.countDown(); + } + + public boolean beforeCalled() { + return beforeCalled.getCount() == 0; + } + public boolean afterCalled() { + return afterCalled.getCount() == 0; + } + public boolean terminatedCalled() { + return terminatedCalled.getCount() == 0; } } @@ -46,7 +57,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute successfully executes a runnable */ @@ -150,12 +160,16 @@ public class ThreadPoolExecutorTest exte threadProceed.await(); threadDone.countDown(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); threadDone.await(); - delay(SHORT_DELAY_MS); - assertEquals(1, p.getCompletedTaskCount()); + long startTime = System.nanoTime(); + while (p.getCompletedTaskCount() != 1) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out"); + Thread.yield(); + } } finally { joinPool(p); } @@ -185,7 +199,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * getThreadFactory returns factory in constructor if not set */ @@ -215,7 +228,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setThreadFactory(null) throws NPE */ @@ -262,7 +274,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setRejectedExecutionHandler(null) throws NPE */ @@ -280,7 +291,6 @@ public class ThreadPoolExecutorTest exte } } - /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -378,7 +388,7 @@ public class ThreadPoolExecutorTest exte } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { final ThreadPoolExecutor p = @@ -391,7 +401,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * isTerminated is false before termination, true after */ @@ -564,9 +573,9 @@ public class ThreadPoolExecutorTest exte } /** - * shutDownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run */ - public void testShutDownNow() { + public void testShutdownNow() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, @@ -587,7 +596,6 @@ public class ThreadPoolExecutorTest exte // Exception Tests - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -660,8 +668,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -753,7 +759,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -845,7 +850,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -1212,7 +1216,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute using DiscardOldestPolicy drops task on shutdown */ @@ -1234,7 +1237,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute(null) throws NPE */ @@ -1307,7 +1309,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setKeepAliveTime throws IllegalArgumentException * when given a negative value @@ -1333,7 +1334,7 @@ public class ThreadPoolExecutorTest exte public void testTerminated() { ExtendedTPE p = new ExtendedTPE(); try { p.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p.terminatedCalled); + assertTrue(p.terminatedCalled()); joinPool(p); } @@ -1343,12 +1344,16 @@ public class ThreadPoolExecutorTest exte public void testBeforeAfter() throws InterruptedException { ExtendedTPE p = new ExtendedTPE(); try { - TrackedNoOpRunnable r = new TrackedNoOpRunnable(); - p.execute(r); - delay(SHORT_DELAY_MS); - assertTrue(r.done); - assertTrue(p.beforeCalled); - assertTrue(p.afterCalled); + final CountDownLatch done = new CountDownLatch(1); + final CheckedRunnable task = new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}; + p.execute(task); + await(p.afterCalled); + assertEquals(0, done.getCount()); + assertTrue(p.afterCalled()); + assertTrue(p.beforeCalled()); try { p.shutdown(); } catch (SecurityException ok) { return; } } finally { joinPool(p); @@ -1406,7 +1411,6 @@ public class ThreadPoolExecutorTest exte } } - /** * invokeAny(null) throws NPE */ @@ -1600,8 +1604,6 @@ public class ThreadPoolExecutorTest exte } } - - /** * timed invokeAny(null) throws NPE */ @@ -1848,16 +1850,11 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); List> futures = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); - assertEquals(3, futures.size()); - Iterator> it = futures.iterator(); - Future f1 = it.next(); - Future f2 = it.next(); - Future f3 = it.next(); - assertTrue(f1.isDone()); - assertTrue(f2.isDone()); - assertTrue(f3.isDone()); - assertFalse(f1.isCancelled()); - assertTrue(f2.isCancelled()); + assertEquals(l.size(), futures.size()); + for (Future future : futures) + assertTrue(future.isDone()); + assertFalse(futures.get(0).isCancelled()); + assertTrue(futures.get(1).isCancelled()); } finally { joinPool(e); } @@ -1903,24 +1900,26 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { p.allowCoreThreadTimeOut(true); p.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { + public void realRun() { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); - for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { - if (p.getPoolSize() == 0) - break; - delay(10); - } + await(threadStarted); + delay(coreThreadTimeOut); + long startTime = System.nanoTime(); + while (p.getPoolSize() > 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertEquals(0, p.getPoolSize()); } finally { joinPool(p); @@ -1931,9 +1930,10 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1943,7 +1943,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - delay(SMALL_DELAY_MS); + delay(2 * coreThreadTimeOut); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p); @@ -1977,7 +1977,7 @@ public class ThreadPoolExecutorTest exte // enough time to run all tasks assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); } finally { - p.shutdown(); + joinPool(p); } }