--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/11/18 19:04:17 1.33 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2013/05/20 16:46:23 1.50 @@ -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,8 +22,10 @@ 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 static java.util.concurrent.TimeUnit.NANOSECONDS; import java.security.AccessControlException; import java.security.Policy; import java.security.PrivilegedAction; @@ -38,7 +40,7 @@ public class ForkJoinPoolTest extends JS return new TestSuite(ForkJoinPoolTest.class); } - /** + /* * Testing coverage notes: * * 1. shutdown and related methods are tested via super.joinPool. @@ -164,7 +166,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 +200,6 @@ public class ForkJoinPoolTest extends JS } catch (NullPointerException success) {} } - /** * getParallelism returns size set in constructor */ @@ -227,6 +227,33 @@ public class ForkJoinPoolTest extends JS } /** + * awaitTermination on a non-shutdown pool times out + */ + public void testAwaitTermination_timesOut() throws InterruptedException { + ForkJoinPool p = new ForkJoinPool(1); + assertFalse(p.isTerminated()); + assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); + assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); + assertFalse(p.awaitTermination(-1L, NANOSECONDS)); + assertFalse(p.awaitTermination(-1L, MILLISECONDS)); + assertFalse(p.awaitTermination(0L, NANOSECONDS)); + assertFalse(p.awaitTermination(0L, MILLISECONDS)); + long timeoutNanos = 999999L; + long startTime = System.nanoTime(); + assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); + assertTrue(System.nanoTime() - startTime >= timeoutNanos); + assertFalse(p.isTerminated()); + startTime = System.nanoTime(); + long timeoutMillis = timeoutMillis(); + assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + assertFalse(p.isTerminated()); + p.shutdown(); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + } + + /** * setUncaughtExceptionHandler changes handler for uncaught exceptions. * * Additionally tests: Overriding ForkJoinWorkerThread.onStart @@ -243,8 +270,11 @@ public class ForkJoinPoolTest extends JS eh, false); try { assertSame(eh, p.getUncaughtExceptionHandler()); - p.execute(new FibTask(8)); - assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); + try { + p.execute(new FibTask(8)); + assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); + } catch (RejectedExecutionException ok) { + } } finally { p.shutdownNow(); // failure might have prevented processing task joinPool(p); @@ -252,18 +282,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 +315,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); } @@ -328,15 +372,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 ShortRunnable()); - ForkJoinTask b = p.submit(new ShortRunnable()); - ForkJoinTask c = p.submit(new ShortRunnable()); + 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); } } @@ -345,11 +391,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 ShortRunnable()); - ForkJoinTask b = p.submit(new ShortRunnable()); - ForkJoinTask c = p.submit(new ShortRunnable()); + 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); @@ -358,11 +405,11 @@ public class ForkJoinPoolTest extends JS assertFalse(r.isDone()); } } finally { + done.countDown(); joinPool(p); } } - // FJ Versions of AbstractExecutorService tests /** @@ -371,17 +418,22 @@ 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); - future.get(); - assertTrue(task.isDone()); + 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 */ @@ -389,8 +441,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); } @@ -403,8 +456,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); } @@ -417,113 +471,79 @@ 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 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 { + public void testSubmitPrivilegedAction() throws Exception { + final Callable callable = Executors.callable(new PrivilegedAction() { + public Object run() { return TEST_STRING; }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); try { - Future future = e.submit(Executors.callable(new PrivilegedAction() { - public Object run() { - return TEST_STRING; - }})); - - Object result = future.get(); - assertSame(TEST_STRING, result); + Future future = e.submit(callable); + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** * 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 { + public void testSubmitPrivilegedExceptionAction() throws Exception { + final Callable callable = + Executors.callable(new PrivilegedExceptionAction() { + public Object run() { return TEST_STRING; }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); try { - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() { - return TEST_STRING; - }})); - - Object result = future.get(); - assertSame(TEST_STRING, result); + Future future = e.submit(callable); + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + 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 { + public void testSubmitFailedPrivilegedExceptionAction() throws Exception { + final Callable callable = + Executors.callable(new PrivilegedExceptionAction() { + public Object run() { throw new IndexOutOfBoundsException(); }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); try { - 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); + Future future = e.submit(callable); + try { + future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + } } finally { joinPool(e); } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** @@ -540,7 +560,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(null callable) throws NullPointerException */ @@ -555,7 +574,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(callable).get() throws InterruptedException if interrupted */ @@ -593,10 +611,8 @@ public class ForkJoinPoolTest extends JS ForkJoinPool p = new ForkJoinPool(1); try { p.submit(new Callable() { - public Object call() { - int i = 5/0; - return Boolean.TRUE; - }}).get(); + public Object call() { throw new ArithmeticException(); }}) + .get(); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof ArithmeticException); @@ -784,7 +800,6 @@ public class ForkJoinPoolTest extends JS } } - /** * timed invokeAny(null) throws NullPointerException */