--- jsr166/src/test/tck/ForkJoinPoolTest.java 2011/05/15 17:32:29 1.40 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2015/10/02 21:52:36 1.56 @@ -4,41 +4,42 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.AbstractExecutorService; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.Callable; -import java.util.concurrent.Future; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.concurrent.CancellationException; -import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.security.AccessControlException; -import java.security.Policy; -import java.security.PrivilegedAction; -import java.security.PrivilegedExceptionAction; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinPoolTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ForkJoinPoolTest.class); } - /** + /* * Testing coverage notes: * * 1. shutdown and related methods are tested via super.joinPool. @@ -106,7 +107,7 @@ public class ForkJoinPoolTest extends JS static final class FibTask extends RecursiveTask { final int number; FibTask(int n) { number = n; } - public Integer compute() { + protected Integer compute() { int n = number; if (n <= 1) return n; @@ -134,7 +135,7 @@ public class ForkJoinPoolTest extends JS this.locker = locker; this.lock = lock; } - public Integer compute() { + protected Integer compute() { int n; LockingFibTask f1 = null; LockingFibTask f2 = null; @@ -198,7 +199,6 @@ public class ForkJoinPoolTest extends JS } catch (NullPointerException success) {} } - /** * getParallelism returns size set in constructor */ @@ -226,6 +226,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 @@ -242,8 +269,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); @@ -256,7 +286,7 @@ public class ForkJoinPoolTest extends JS * the task has completed successfully, and construction * parameters continue to hold */ - public void testisQuiescent() throws Exception { + public void testIsQuiescent() throws Exception { ForkJoinPool p = new ForkJoinPool(2); try { assertTrue(p.isQuiescent()); @@ -379,7 +409,6 @@ public class ForkJoinPoolTest extends JS } } - // FJ Versions of AbstractExecutorService tests /** @@ -388,12 +417,14 @@ 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()); - Future future = e.submit(task); + final AtomicBoolean done = new AtomicBoolean(false); + Future future = e.submit(new CheckedRunnable() { + public void realRun() { + done.set(true); + }}); 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 { @@ -401,7 +432,6 @@ public class ForkJoinPoolTest extends JS } } - /** * Completed submit(callable) returns result */ @@ -451,35 +481,39 @@ public class ForkJoinPoolTest extends JS * A submitted privileged action runs to completion */ 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); - Future future = e.submit(Executors.callable(new PrivilegedAction() { - public Object run() { - return TEST_STRING; - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try { + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); - }}; + } finally { + joinPool(e); + } + }}; - runWithPermissions(r, - new RuntimePermission("modifyThread")); + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** * A submitted privileged exception action runs to completion */ 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() { - return TEST_STRING; - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try { + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); - }}; + } finally { + joinPool(e); + } + }}; runWithPermissions(r, new RuntimePermission("modifyThread")); } @@ -488,20 +522,24 @@ public class ForkJoinPoolTest extends JS * A submitted failed privileged exception action reports exception */ 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() throws Exception { - throw new IndexOutOfBoundsException(); - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try { + Future future = e.submit(callable); try { future.get(); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof IndexOutOfBoundsException); - }}}; + } + } finally { + joinPool(e); + } + }}; runWithPermissions(r, new RuntimePermission("modifyThread")); } @@ -520,7 +558,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(null callable) throws NullPointerException */ @@ -535,7 +572,6 @@ public class ForkJoinPoolTest extends JS } } - /** * submit(callable).get() throws InterruptedException if interrupted */ @@ -573,10 +609,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); @@ -764,7 +798,6 @@ public class ForkJoinPoolTest extends JS } } - /** * timed invokeAny(null) throws NullPointerException */ @@ -954,7 +987,7 @@ public class ForkJoinPoolTest extends JS l.add(new StringTask()); l.add(new StringTask()); List> futures - = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get());