--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2009/11/20 05:25:10 1.19 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2010/09/17 00:53:15 1.26 @@ -10,12 +10,13 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.math.BigInteger; import java.security.*; public class AbstractExecutorServiceTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(AbstractExecutorServiceTest.class); @@ -28,10 +29,15 @@ public class AbstractExecutorServiceTest static class DirectExecutorService extends AbstractExecutorService { public void execute(Runnable r) { r.run(); } public void shutdown() { shutdown = true; } - public List shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } + public List shutdownNow() { + shutdown = true; + return Collections.EMPTY_LIST; + } public boolean isShutdown() { return shutdown; } public boolean isTerminated() { return isShutdown(); } - public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } + public boolean awaitTermination(long timeout, TimeUnit unit) { + return isShutdown(); + } private volatile boolean shutdown = false; } @@ -80,98 +86,64 @@ public class AbstractExecutorServiceTest /** - * A submitted privileged action to completion + * A submitted privileged action runs to completion */ public void testSubmitPrivilegedAction() throws Exception { - 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 DirectExecutorService(); - Future future = e.submit(Executors.callable(new PrivilegedAction() { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new DirectExecutorService(); + Future future = e.submit(Executors.callable(new PrivilegedAction() { public Object run() { return TEST_STRING; }})); - Object result = future.get(); - assertSame(TEST_STRING, result); - } - finally { - try { - Policy.setPolicy(savedPolicy); - } catch (AccessControlException ok) { - return; - } - } + assertSame(TEST_STRING, future.get()); + }}; + + runWithPermissions(r, + new RuntimePermission("getClassLoader"), + new RuntimePermission("setContextClassLoader"), + new RuntimePermission("modifyThread")); } /** - * A submitted a privileged exception action runs to completion + * A submitted privileged exception action runs to completion */ public void testSubmitPrivilegedExceptionAction() throws Exception { - 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 DirectExecutorService(); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new DirectExecutorService(); + 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); - } + assertSame(TEST_STRING, future.get()); + }}; + + runWithPermissions(r); } /** * A submitted failed privileged exception action reports exception */ public void testSubmitFailedPrivilegedExceptionAction() throws Exception { - 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 DirectExecutorService(); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { + ExecutorService e = new DirectExecutorService(); + Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() throws Exception { throw new IndexOutOfBoundsException(); }})); - future.get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof IndexOutOfBoundsException); - } - finally { - Policy.setPolicy(savedPolicy); - } + try { + future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + }}}; + + runWithPermissions(r); } /** @@ -245,25 +217,34 @@ public class AbstractExecutorServiceTest /** - * Blocking on submit(callable) throws InterruptedException if - * caller interrupted. + * submit(callable).get() throws InterruptedException if interrupted */ public void testInterruptedSubmit() throws InterruptedException { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws Exception { - p.submit(new CheckedCallable() { - public Object realCall() - throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - return null; - }}).get(); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - joinPool(p); + final CountDownLatch submitted = new CountDownLatch(1); + final CountDownLatch quittingTime = new CountDownLatch(1); + final ExecutorService p + = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, + new ArrayBlockingQueue(10)); + final Callable awaiter = new CheckedCallable() { + public Void realCall() throws InterruptedException { + quittingTime.await(); + return null; + }}; + try { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws Exception { + Future future = p.submit(awaiter); + submitted.countDown(); + future.get(); + }}); + t.start(); + submitted.await(); + t.interrupt(); + t.join(); + } finally { + quittingTime.countDown(); + joinPool(p); + } } /** @@ -344,19 +325,16 @@ public class AbstractExecutorServiceTest * invokeAny(c) throws NPE if c has null elements */ public void testInvokeAny3() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new DirectExecutorService(); + List> l = new ArrayList>(); + l.add(new Callable() { + public Integer call() { return 5/0; }}); + l.add(null); try { - ArrayList> l - = new ArrayList>(); - l.add(new Callable() { - public Integer call() { return 5/0; }}); - l.add(null); e.invokeAny(l); shouldThrow(); } catch (NullPointerException success) { } finally { - latch.countDown(); joinPool(e); } } @@ -366,9 +344,9 @@ public class AbstractExecutorServiceTest */ public void testInvokeAny4() throws InterruptedException { ExecutorService e = new DirectExecutorService(); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); e.invokeAny(l); shouldThrow(); } catch (ExecutionException success) { @@ -384,7 +362,7 @@ public class AbstractExecutorServiceTest public void testInvokeAny5() throws Exception { ExecutorService e = new DirectExecutorService(); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -426,10 +404,10 @@ public class AbstractExecutorServiceTest */ public void testInvokeAll3() throws InterruptedException { ExecutorService e = new DirectExecutorService(); + 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) { @@ -444,18 +422,15 @@ public class AbstractExecutorServiceTest public void testInvokeAll4() throws Exception { ExecutorService e = new DirectExecutorService(); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new NPETask()); - List> result = e.invokeAll(l); - assertEquals(1, result.size()); - for (Future future : result) { - try { - future.get(); - shouldThrow(); - } catch (ExecutionException success) { - Throwable cause = success.getCause(); - assertTrue(cause instanceof NullPointerException); - } + List> futures = e.invokeAll(l); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } } finally { joinPool(e); @@ -468,12 +443,12 @@ public class AbstractExecutorServiceTest public void testInvokeAll5() throws Exception { ExecutorService e = new DirectExecutorService(); 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); @@ -487,7 +462,7 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAny1() throws Exception { ExecutorService e = new DirectExecutorService(); try { - e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { } finally { @@ -500,9 +475,9 @@ public class AbstractExecutorServiceTest */ public void testTimedInvokeAnyNullTimeUnit() throws Exception { ExecutorService e = new DirectExecutorService(); + 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) { @@ -517,7 +492,7 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAny2() throws Exception { ExecutorService e = new DirectExecutorService(); try { - e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (IllegalArgumentException success) { } finally { @@ -529,19 +504,16 @@ public class AbstractExecutorServiceTest * timed invokeAny(c) throws NPE if c has null elements */ public void testTimedInvokeAny3() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new DirectExecutorService(); + List> l = new ArrayList>(); + l.add(new Callable() { + public Integer call() { return 5/0; }}); + l.add(null); try { - ArrayList> l - = new ArrayList>(); - l.add(new Callable() { - public Integer call() { return 5/0; }}); - l.add(null); - e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { } finally { - latch.countDown(); joinPool(e); } } @@ -551,10 +523,10 @@ public class AbstractExecutorServiceTest */ public void testTimedInvokeAny4() throws Exception { ExecutorService e = new DirectExecutorService(); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); - e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); @@ -569,10 +541,10 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAny5() throws Exception { ExecutorService e = new DirectExecutorService(); 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, TimeUnit.MILLISECONDS); + String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); } finally { joinPool(e); @@ -585,7 +557,7 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAll1() throws InterruptedException { ExecutorService e = new DirectExecutorService(); try { - e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { } finally { @@ -598,9 +570,9 @@ public class AbstractExecutorServiceTest */ public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { ExecutorService e = new DirectExecutorService(); + 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) { @@ -615,7 +587,7 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAll2() throws InterruptedException { ExecutorService e = new DirectExecutorService(); try { - List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); } finally { joinPool(e); @@ -627,11 +599,11 @@ public class AbstractExecutorServiceTest */ public void testTimedInvokeAll3() throws InterruptedException { ExecutorService e = new DirectExecutorService(); + 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, TimeUnit.MILLISECONDS); + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) { } finally { @@ -645,16 +617,16 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAll4() throws Exception { ExecutorService e = new DirectExecutorService(); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new NPETask()); - List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(1, result.size()); - for (Future future : result) { - try { - future.get(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } + List> futures = + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } } finally { joinPool(e); @@ -667,12 +639,13 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAll5() throws Exception { ExecutorService e = new DirectExecutorService(); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(2, result.size()); - for (Future future : result) + List> futures = + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(2, futures.size()); + for (Future future : futures) assertSame(TEST_STRING, future.get()); } finally { joinPool(e); @@ -685,13 +658,14 @@ public class AbstractExecutorServiceTest public void testTimedInvokeAll6() throws InterruptedException { ExecutorService e = new DirectExecutorService(); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); l.add(new StringTask()); - List> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(3, result.size()); - Iterator> it = result.iterator(); + List> futures = + e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); + assertEquals(3, futures.size()); + Iterator> it = futures.iterator(); Future f1 = it.next(); Future f2 = it.next(); Future f3 = it.next();