--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/08/25 00:07:03 1.21 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2010/09/13 20:48:58 1.25 @@ -4,10 +4,10 @@ * http://creativecommons.org/licenses/publicdomain */ - import junit.framework.*; -import java.util.*; -import java.util.concurrent.Executor; +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; @@ -20,16 +20,19 @@ import java.util.concurrent.RejectedExec import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; -import java.util.concurrent.RecursiveAction; import java.util.concurrent.RecursiveTask; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.*; -import java.security.*; +import java.util.concurrent.locks.ReentrantLock; +import java.security.AccessControlException; +import java.security.Policy; +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; public class ForkJoinPoolTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } + public static Test suite() { return new TestSuite(ForkJoinPoolTest.class); } @@ -156,9 +159,8 @@ public class ForkJoinPoolTest extends JS * tasks, and quiescent running state. */ public void testDefaultInitialState() { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(1); try { - p = new ForkJoinPool(1); assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); assertTrue(p.isQuiescent()); @@ -201,9 +203,8 @@ public class ForkJoinPoolTest extends JS * getParallelism returns size set in constructor */ public void testGetParallelism() { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(1); try { - p = new ForkJoinPool(1); assertTrue(p.getParallelism() == 1); } finally { joinPool(p); @@ -214,13 +215,11 @@ public class ForkJoinPoolTest extends JS * getPoolSize returns number of started workers. */ public void testGetPoolSize() { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(1); try { - p = new ForkJoinPool(1); assertTrue(p.getActiveThreadCount() == 0); Future future = p.submit(new StringTask()); assertTrue(p.getPoolSize() == 1); - } finally { joinPool(p); } @@ -233,10 +232,9 @@ public class ForkJoinPoolTest extends JS * performs its defined action */ public void testSetUncaughtExceptionHandler() throws InterruptedException { - ForkJoinPool p = null; + MyHandler eh = new MyHandler(); + ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); try { - MyHandler eh = new MyHandler(); - p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); assert(eh == p.getUncaughtExceptionHandler()); p.execute(new FailingTask()); Thread.sleep(MEDIUM_DELAY_MS); @@ -253,9 +251,8 @@ public class ForkJoinPoolTest extends JS * construction parameters continue to hold */ public void testisQuiescent() throws InterruptedException { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(2); try { - p = new ForkJoinPool(2); p.invoke(new FibTask(20)); assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); @@ -278,9 +275,8 @@ public class ForkJoinPoolTest extends JS * Completed submit(ForkJoinTask) returns result */ public void testSubmitForkJoinTask() throws Throwable { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(1); try { - p = new ForkJoinPool(1); ForkJoinTask f = p.submit(new FibTask(8)); int r = f.get(); assertTrue(r == 21); @@ -293,9 +289,8 @@ public class ForkJoinPoolTest extends JS * A task submitted after shutdown is rejected */ public void testSubmitAfterShutdown() { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(1); try { - p = new ForkJoinPool(1); p.shutdown(); assertTrue(p.isShutdown()); ForkJoinTask f = p.submit(new FibTask(8)); @@ -310,9 +305,8 @@ public class ForkJoinPoolTest extends JS * Pool maintains parallelism when using ManagedBlocker */ public void testBlockingForkJoinTask() throws Throwable { - ForkJoinPool p = null; + ForkJoinPool p = new ForkJoinPool(4); try { - p = new ForkJoinPool(4); ReentrantLock lock = new ReentrantLock(); ManagedLocker locker = new ManagedLocker(lock); ForkJoinTask f = new LockingFibTask(30, locker, lock); @@ -328,9 +322,8 @@ public class ForkJoinPoolTest extends JS * pollSubmission returns unexecuted submitted task, if present */ public void testPollSubmission() { - SubFJP p = null; + SubFJP p = new SubFJP(); try { - p = new SubFJP(); ForkJoinTask a = p.submit(new MediumRunnable()); ForkJoinTask b = p.submit(new MediumRunnable()); ForkJoinTask c = p.submit(new MediumRunnable()); @@ -346,9 +339,8 @@ public class ForkJoinPoolTest extends JS * drainTasksTo transfers unexecuted submitted tasks, if present */ public void testDrainTasksTo() { - SubFJP p = null; + SubFJP p = new SubFJP(); try { - p = new SubFJP(); ForkJoinTask a = p.submit(new MediumRunnable()); ForkJoinTask b = p.submit(new MediumRunnable()); ForkJoinTask c = p.submit(new MediumRunnable()); @@ -372,11 +364,15 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Future future = e.submit(task); - future.get(); - assertTrue(task.done); + try { + TrackedShortRunnable task = new TrackedShortRunnable(); + assertFalse(task.done); + Future future = e.submit(task); + future.get(); + assertTrue(task.done); + } finally { + joinPool(e); + } } @@ -385,9 +381,13 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitCallable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(new StringTask()); - String result = future.get(); - assertSame(TEST_STRING, result); + try { + Future future = e.submit(new StringTask()); + String result = future.get(); + assertSame(TEST_STRING, result); + } finally { + joinPool(e); + } } /** @@ -395,9 +395,13 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(new NoOpRunnable()); - future.get(); - assertTrue(future.isDone()); + try { + Future future = e.submit(new NoOpRunnable()); + future.get(); + assertTrue(future.isDone()); + } finally { + joinPool(e); + } } /** @@ -405,9 +409,13 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitRunnable2() throws Throwable { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(new NoOpRunnable(), TEST_STRING); - String result = future.get(); - assertSame(TEST_STRING, result); + try { + Future future = e.submit(new NoOpRunnable(), TEST_STRING); + String result = future.get(); + assertSame(TEST_STRING, result); + } finally { + joinPool(e); + } } @@ -425,17 +433,21 @@ public class ForkJoinPoolTest extends JS } catch (AccessControlException ok) { return; } + try { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(Executors.callable(new PrivilegedAction() { + try { + Future future = e.submit(Executors.callable(new PrivilegedAction() { public Object run() { return TEST_STRING; }})); - Object result = future.get(); - assertSame(TEST_STRING, result); - } - finally { + Object result = future.get(); + assertSame(TEST_STRING, result); + } finally { + joinPool(e); + } + } finally { Policy.setPolicy(savedPolicy); } } @@ -457,15 +469,18 @@ public class ForkJoinPoolTest extends JS try { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + try { + Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() { return TEST_STRING; }})); - Object result = future.get(); - assertSame(TEST_STRING, result); - } - finally { + Object result = future.get(); + assertSame(TEST_STRING, result); + } finally { + joinPool(e); + } + } finally { Policy.setPolicy(savedPolicy); } } @@ -485,18 +500,21 @@ public class ForkJoinPoolTest extends JS return; } - try { ExecutorService e = new ForkJoinPool(1); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + 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); + Object result = future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + } finally { + joinPool(e); + } } finally { Policy.setPolicy(savedPolicy); } @@ -506,12 +524,15 @@ public class ForkJoinPoolTest extends JS * execute(null runnable) throws NullPointerException */ public void testExecuteNullRunnable() { + ExecutorService e = new ForkJoinPool(1); try { - ExecutorService e = new ForkJoinPool(1); TrackedShortRunnable task = null; Future future = e.submit(task); shouldThrow(); - } catch (NullPointerException success) {} + } catch (NullPointerException success) { + } finally { + joinPool(e); + } } @@ -519,12 +540,15 @@ public class ForkJoinPoolTest extends JS * submit(null callable) throws NullPointerException */ public void testSubmitNullCallable() { + ExecutorService e = new ForkJoinPool(1); try { - ExecutorService e = new ForkJoinPool(1); StringTask t = null; Future future = e.submit(t); shouldThrow(); - } catch (NullPointerException success) {} + } catch (NullPointerException success) { + } finally { + joinPool(e); + } } @@ -570,9 +594,9 @@ public class ForkJoinPoolTest extends JS shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof ArithmeticException); + } finally { + joinPool(p); } - - joinPool(p); } /**