--- jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2011/05/07 19:34:51 1.27 +++ jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2014/12/31 19:05:43 1.33 @@ -6,12 +6,33 @@ * Pat Fisher, Mike Judd. */ -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.concurrent.locks.*; -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { public static void main(String[] args) { @@ -115,7 +136,6 @@ public class ThreadPoolExecutorSubclassT } } - static class CustomTPE extends ThreadPoolExecutor { protected RunnableFuture newTaskFor(Callable c) { return new CustomTask(c); @@ -162,22 +182,32 @@ public class ThreadPoolExecutorSubclassT workQueue, threadFactory, handler); } - 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 CustomTPE() { 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; + } } static class FailingThreadFactory implements ThreadFactory { @@ -188,7 +218,6 @@ public class ThreadPoolExecutorSubclassT } } - /** * execute successfully executes a runnable */ @@ -286,12 +315,16 @@ public class ThreadPoolExecutorSubclassT 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); } @@ -315,7 +348,6 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** * getThreadFactory returns factory in constructor if not set */ @@ -337,7 +369,6 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** * setThreadFactory(null) throws NPE */ @@ -374,7 +405,6 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** * setRejectedExecutionHandler(null) throws NPE */ @@ -389,7 +419,6 @@ public class ThreadPoolExecutorSubclassT } } - /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -484,7 +513,7 @@ public class ThreadPoolExecutorSubclassT } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { @@ -495,7 +524,6 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** * isTerminated is false before termination, true after */ @@ -669,7 +697,7 @@ public class ThreadPoolExecutorSubclassT } /** - * shutDownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run */ public void testShutdownNow() { ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); @@ -689,7 +717,6 @@ public class ThreadPoolExecutorSubclassT // Exception Tests - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -750,8 +777,6 @@ public class ThreadPoolExecutorSubclassT } catch (NullPointerException success) {} } - - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -823,7 +848,6 @@ public class ThreadPoolExecutorSubclassT } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -895,7 +919,6 @@ public class ThreadPoolExecutorSubclassT } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -981,7 +1004,6 @@ public class ThreadPoolExecutorSubclassT } catch (NullPointerException success) {} } - /** * execute throws RejectedExecutionException if saturated. */ @@ -1131,7 +1153,6 @@ public class ThreadPoolExecutorSubclassT } } - /** * execute using DiscardOldestPolicy drops task on shutdown */ @@ -1149,7 +1170,6 @@ public class ThreadPoolExecutorSubclassT } } - /** * execute(null) throws NPE */ @@ -1214,7 +1234,6 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** * setKeepAliveTime throws IllegalArgumentException * when given a negative value @@ -1239,7 +1258,7 @@ public class ThreadPoolExecutorSubclassT public void testTerminated() { CustomTPE p = new CustomTPE(); try { p.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p.terminatedCalled); + assertTrue(p.terminatedCalled()); joinPool(p); } @@ -1249,12 +1268,15 @@ public class ThreadPoolExecutorSubclassT public void testBeforeAfter() throws InterruptedException { CustomTPE p = new CustomTPE(); 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); + p.execute(new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}); + await(p.afterCalled); + assertEquals(0, done.getCount()); + assertTrue(p.afterCalled()); + assertTrue(p.beforeCalled()); try { p.shutdown(); } catch (SecurityException ok) { return; } } finally { joinPool(p); @@ -1303,7 +1325,6 @@ public class ThreadPoolExecutorSubclassT } } - /** * invokeAny(null) throws NPE */ @@ -1465,8 +1486,6 @@ public class ThreadPoolExecutorSubclassT } } - - /** * timed invokeAny(null) throws NPE */ @@ -1674,16 +1693,11 @@ public class ThreadPoolExecutorSubclassT 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); } @@ -1726,9 +1740,10 @@ public class ThreadPoolExecutorSubclassT * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new CustomTPE(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1738,12 +1753,13 @@ public class ThreadPoolExecutorSubclassT 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); @@ -1754,9 +1770,10 @@ public class ThreadPoolExecutorSubclassT * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new CustomTPE(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1766,7 +1783,7 @@ public class ThreadPoolExecutorSubclassT threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - delay(SMALL_DELAY_MS); + delay(2 * coreThreadTimeOut); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p);