--- jsr166/src/test/tck/ForkJoinPoolTest.java 2009/11/21 10:25:05 1.10 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2010/02/22 11:25:16 1.18 @@ -235,7 +235,7 @@ public class ForkJoinPoolTest extends JS ForkJoinPool p = null; try { p = new ForkJoinPool(1); - assertTrue(p.getPoolSize() == 0); + assertTrue(p.getActiveThreadCount() == 0); Future future = p.submit(new StringTask()); assertTrue(p.getPoolSize() == 1); @@ -412,7 +412,7 @@ public class ForkJoinPoolTest extends JS p.execute(f); assertTrue(p.getPoolSize() >= 4); int r = f.get(); - assertTrue(r == 832040); + assertTrue(r == 832040); } finally { p.shutdownNow(); // don't wait out shutdown } @@ -504,95 +504,64 @@ public class ForkJoinPoolTest extends JS assertSame(TEST_STRING, result); } - /** - * 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); - Future future = e.submit(Executors.callable(new PrivilegedAction() { + 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 { - Policy.setPolicy(savedPolicy); - } + Object result = future.get(); + assertSame(TEST_STRING, result); + }}; + + 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + 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 { - Policy.setPolicy(savedPolicy); - } + Object result = future.get(); + assertSame(TEST_STRING, result); + }}; + + 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + 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) { - } finally { - Policy.setPolicy(savedPolicy); - } + try { + Object result = future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + }}}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** @@ -629,7 +598,7 @@ public class ForkJoinPoolTest extends JS final ForkJoinPool p = new ForkJoinPool(1); Thread t = new Thread(new CheckedInterruptedRunnable() { - void realRun() throws Throwable { + public void realRun() throws Throwable { p.submit(new CheckedCallable() { public Object realCall() throws Throwable { try { @@ -661,7 +630,9 @@ public class ForkJoinPoolTest extends JS return Boolean.TRUE; }}).get(); shouldThrow(); - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof ArithmeticException); + } joinPool(p); } @@ -699,9 +670,9 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny3() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(null); e.invokeAny(l); shouldThrow(); } catch (NullPointerException success) { @@ -714,21 +685,17 @@ public class ForkJoinPoolTest extends JS * invokeAny(c) throws NullPointerException if c has null elements */ public void testInvokeAny4() throws Throwable { + CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new Callable() { - public String call() { - // The delay gives the pool a chance to notice - // the null element. - sleepTillInterrupted(SMALL_DELAY_MS); - return "foo"; - }}); - l.add(null); e.invokeAny(l); shouldThrow(); } catch (NullPointerException success) { } finally { + latch.countDown(); joinPool(e); } } @@ -738,12 +705,13 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); e.invokeAny(l); shouldThrow(); } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -755,7 +723,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny6() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -798,10 +766,10 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); e.invokeAll(l); shouldThrow(); } catch (NullPointerException success) { @@ -816,15 +784,15 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures = e.invokeAll(l); + assertEquals(1, futures.size()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); - List> result = e.invokeAll(l); - assertEquals(1, result.size()); - for (Future future : result) - future.get(); + futures.get(0).get(); shouldThrow(); } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -836,12 +804,12 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - List> result = e.invokeAll(l); - assertEquals(2, result.size()); - for (Future future : result) + List> futures = e.invokeAll(l); + assertEquals(2, futures.size()); + for (Future future : futures) assertSame(TEST_STRING, future.get()); } finally { joinPool(e); @@ -868,9 +836,9 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAnyNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new StringTask()); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); e.invokeAny(l, MEDIUM_DELAY_MS, null); shouldThrow(); } catch (NullPointerException success) { @@ -898,15 +866,17 @@ public class ForkJoinPoolTest extends JS * timed invokeAny(c) throws NullPointerException if c has null elements */ public void testTimedInvokeAny3() throws Throwable { + CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { } finally { + latch.countDown(); joinPool(e); } } @@ -916,12 +886,13 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAny4() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -933,7 +904,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); @@ -962,9 +933,9 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAllNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new StringTask()); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); e.invokeAll(l, MEDIUM_DELAY_MS, null); shouldThrow(); } catch (NullPointerException success) { @@ -993,10 +964,10 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { @@ -1010,16 +981,16 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures + = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(1, futures.size()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); - List> result - = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - assertEquals(1, result.size()); - for (Future future : result) - future.get(); + futures.get(0).get(); shouldThrow(); } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -1031,13 +1002,13 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - List> result + List> futures = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - assertEquals(2, result.size()); - for (Future future : result) + assertEquals(2, futures.size()); + for (Future future : futures) assertSame(TEST_STRING, future.get()); } finally { joinPool(e);