--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/09/17 17:07:47 1.30 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2010/11/29 07:42:58 1.36 @@ -316,9 +316,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 } @@ -328,15 +328,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); } } @@ -345,11 +347,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); @@ -358,6 +361,7 @@ public class ForkJoinPoolTest extends JS assertFalse(r.isDone()); } } finally { + done.countDown(); joinPool(p); } } @@ -371,11 +375,14 @@ public class ForkJoinPoolTest extends JS public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); + TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); + assertFalse(task.isDone()); Future future = e.submit(task); - future.get(); - assertTrue(task.done); + assertNull(future.get()); + assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(task.isDone()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } finally { joinPool(e); } @@ -389,8 +396,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 +411,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 +426,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")); } /** @@ -531,9 +498,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 { @@ -547,9 +513,8 @@ public class ForkJoinPoolTest extends JS */ 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 {