--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/11/29 07:42:58 1.36 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2011/05/29 13:45:35 1.43 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import junit.framework.*; @@ -22,6 +22,7 @@ import java.util.concurrent.ForkJoinTask import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveTask; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.security.AccessControlException; @@ -164,7 +165,6 @@ public class ForkJoinPoolTest extends JS try { assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); - assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); assertEquals(0, p.getActiveThreadCount()); assertEquals(0, p.getStealCount()); @@ -199,7 +199,6 @@ public class ForkJoinPoolTest extends JS } catch (NullPointerException success) {} } - /** * getParallelism returns size set in constructor */ @@ -252,18 +251,30 @@ public class ForkJoinPoolTest extends JS } /** - * After invoking a single task, isQuiescent is true, - * queues are empty, threads are not active, and - * construction parameters continue to hold + * After invoking a single task, isQuiescent eventually becomes + * true, at which time queues are empty, threads are not active, + * the task has completed successfully, and construction + * parameters continue to hold */ - public void testisQuiescent() throws InterruptedException { + public void testIsQuiescent() throws Exception { ForkJoinPool p = new ForkJoinPool(2); try { assertTrue(p.isQuiescent()); - p.invoke(new FibTask(20)); + long startTime = System.nanoTime(); + FibTask f = new FibTask(20); + p.invoke(f); assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); - Thread.sleep(SMALL_DELAY_MS); + while (! p.isQuiescent()) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + throw new AssertionFailedError("timed out"); + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); assertEquals(0, p.getActiveThreadCount()); @@ -273,6 +284,8 @@ public class ForkJoinPoolTest extends JS assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); + assertTrue(f.isDone()); + assertEquals(6765, (int) f.get()); } finally { joinPool(p); } @@ -366,7 +379,6 @@ public class ForkJoinPoolTest extends JS } } - // FJ Versions of AbstractExecutorService tests /** @@ -375,12 +387,15 @@ public class ForkJoinPoolTest extends JS public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); - assertFalse(task.isDone()); + final AtomicBoolean done = new AtomicBoolean(false); + CheckedRunnable task = new CheckedRunnable() { + public void realRun() { + done.set(true); + }}; Future future = e.submit(task); assertNull(future.get()); - assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(task.isDone()); + assertNull(future.get(0, MILLISECONDS)); + assertTrue(done.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); } finally { @@ -388,7 +403,6 @@ public class ForkJoinPoolTest extends JS } } - /** * Completed submit(callable) returns result */ @@ -507,7 +521,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(null callable) throws NullPointerException */ @@ -522,7 +535,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(callable).get() throws InterruptedException if interrupted */ @@ -751,7 +763,6 @@ public class ForkJoinPoolTest extends JS } } - /** * timed invokeAny(null) throws NullPointerException */