--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/09/17 14:19:52 1.28 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2011/05/29 07:01:17 1.42 @@ -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 */ @@ -233,18 +232,18 @@ public class ForkJoinPoolTest extends JS * performs its defined action */ public void testSetUncaughtExceptionHandler() throws InterruptedException { - final CountDownLatch uncaughtExceptionHappened = new CountDownLatch(1); + final CountDownLatch uehInvoked = new CountDownLatch(1); final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { - uncaughtExceptionHappened.countDown(); + uehInvoked.countDown(); }}; ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); try { assertSame(eh, p.getUncaughtExceptionHandler()); - p.execute(new FailingTask()); - uncaughtExceptionHappened.await(); + p.execute(new FibTask(8)); + assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); } finally { p.shutdownNow(); // failure might have prevented processing task joinPool(p); @@ -252,17 +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 { - p.invoke(new FibTask(20)); + assertTrue(p.isQuiescent()); + long startTime = System.nanoTime(); + FibTask f = new FibTask(20); + p.invoke(f); assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); - Thread.sleep(MEDIUM_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()); @@ -272,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); } @@ -315,9 +329,9 @@ public class ForkJoinPoolTest extends JS try { ReentrantLock lock = new ReentrantLock(); ManagedLocker locker = new ManagedLocker(lock); - ForkJoinTask f = new LockingFibTask(30, locker, lock); + ForkJoinTask f = new LockingFibTask(20, locker, lock); p.execute(f); - assertEquals(832040, (int) f.get()); + assertEquals(6765, (int) f.get()); } finally { p.shutdownNow(); // don't wait out shutdown } @@ -327,15 +341,17 @@ public class ForkJoinPoolTest extends JS * pollSubmission returns unexecuted submitted task, if present */ public void testPollSubmission() { + final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); try { - ForkJoinTask a = p.submit(new MediumRunnable()); - ForkJoinTask b = p.submit(new MediumRunnable()); - ForkJoinTask c = p.submit(new MediumRunnable()); + ForkJoinTask a = p.submit(awaiter(done)); + ForkJoinTask b = p.submit(awaiter(done)); + ForkJoinTask c = p.submit(awaiter(done)); ForkJoinTask r = p.pollSubmission(); assertTrue(r == a || r == b || r == c); assertFalse(r.isDone()); } finally { + done.countDown(); joinPool(p); } } @@ -344,11 +360,12 @@ public class ForkJoinPoolTest extends JS * drainTasksTo transfers unexecuted submitted tasks, if present */ public void testDrainTasksTo() { + final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); try { - ForkJoinTask a = p.submit(new MediumRunnable()); - ForkJoinTask b = p.submit(new MediumRunnable()); - ForkJoinTask c = p.submit(new MediumRunnable()); + ForkJoinTask a = p.submit(awaiter(done)); + ForkJoinTask b = p.submit(awaiter(done)); + ForkJoinTask c = p.submit(awaiter(done)); ArrayList al = new ArrayList(); p.drainTasksTo(al); assertTrue(al.size() > 0); @@ -357,11 +374,11 @@ public class ForkJoinPoolTest extends JS assertFalse(r.isDone()); } } finally { + done.countDown(); joinPool(p); } } - // FJ Versions of AbstractExecutorService tests /** @@ -370,17 +387,22 @@ public class ForkJoinPoolTest extends JS public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); + final AtomicBoolean done = new AtomicBoolean(false); + CheckedRunnable task = new CheckedRunnable() { + public void realRun() { + done.set(true); + }}; Future future = e.submit(task); - future.get(); - assertTrue(task.done); + assertNull(future.get()); + assertNull(future.get(0, MILLISECONDS)); + assertTrue(done.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } finally { joinPool(e); } } - /** * Completed submit(callable) returns result */ @@ -388,8 +410,9 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try { Future future = e.submit(new StringTask()); - String result = future.get(); - assertSame(TEST_STRING, result); + assertSame(TEST_STRING, future.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } finally { joinPool(e); } @@ -402,8 +425,9 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try { Future future = e.submit(new NoOpRunnable()); - future.get(); + assertNull(future.get()); assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } finally { joinPool(e); } @@ -416,113 +440,71 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try { Future future = e.submit(new NoOpRunnable(), TEST_STRING); - String result = future.get(); - assertSame(TEST_STRING, result); + assertSame(TEST_STRING, future.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } finally { joinPool(e); } } - /** - * A submitted privileged action to completion + * A submitted privileged action runs to completion */ - public void testSubmitPrivilegedAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { - ExecutorService e = new ForkJoinPool(1); - try { + public void testSubmitPrivilegedAction() throws Exception { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); Future future = e.submit(Executors.callable(new PrivilegedAction() { public Object run() { return TEST_STRING; }})); - Object result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); - } - } finally { - Policy.setPolicy(savedPolicy); - } + assertSame(TEST_STRING, future.get()); + }}; + + runWithPermissions(r, + new RuntimePermission("modifyThread")); } /** - * A submitted a privileged exception action runs to completion + * A submitted privileged exception action runs to completion */ - public void testSubmitPrivilegedExceptionAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { - ExecutorService e = new ForkJoinPool(1); - try { + public void testSubmitPrivilegedExceptionAction() throws Exception { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() { return TEST_STRING; }})); - Object result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); - } - } finally { - Policy.setPolicy(savedPolicy); - } + assertSame(TEST_STRING, future.get()); + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** * A submitted failed privileged exception action reports exception */ - public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { - ExecutorService e = new ForkJoinPool(1); - try { + public void testSubmitFailedPrivilegedExceptionAction() throws Exception { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() throws Exception { throw new IndexOutOfBoundsException(); }})); - Object result = future.get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof IndexOutOfBoundsException); - } finally { - joinPool(e); - } - } finally { - Policy.setPolicy(savedPolicy); - } + try { + future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + }}}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** @@ -530,9 +512,8 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteNullRunnable() { ExecutorService e = new ForkJoinPool(1); - TrackedShortRunnable task = null; try { - Future future = e.submit(task); + Future future = e.submit((Runnable) null); shouldThrow(); } catch (NullPointerException success) { } finally { @@ -540,15 +521,13 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(null callable) throws NullPointerException */ public void testSubmitNullCallable() { ExecutorService e = new ForkJoinPool(1); - StringTask t = null; try { - Future future = e.submit(t); + Future future = e.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) { } finally { @@ -556,7 +535,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(callable).get() throws InterruptedException if interrupted */ @@ -785,7 +763,6 @@ public class ForkJoinPoolTest extends JS } } - /** * timed invokeAny(null) throws NullPointerException */