--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2004/01/20 20:20:56 1.15 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2011/03/15 19:47:06 1.30 @@ -1,225 +1,149 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * http://creativecommons.org/publicdomain/zero/1.0/ + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ 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 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); } - /** + /** * A no-frills implementation of AbstractExecutorService, designed * to test the submit methods only. */ 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; } /** * execute(runnable) runs it to completion */ - public void testExecuteRunnable() { - try { - ExecutorService e = new DirectExecutorService(); - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Future future = e.submit(task); - future.get(); - assertTrue(task.done); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + public void testExecuteRunnable() throws Exception { + ExecutorService e = new DirectExecutorService(); + TrackedShortRunnable task = new TrackedShortRunnable(); + assertFalse(task.done); + Future future = e.submit(task); + future.get(); + assertTrue(task.done); } /** * Completed submit(callable) returns result */ - public void testSubmitCallable() { - try { - ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new StringTask()); - String result = future.get(); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + public void testSubmitCallable() throws Exception { + ExecutorService e = new DirectExecutorService(); + Future future = e.submit(new StringTask()); + String result = future.get(); + assertSame(TEST_STRING, result); } /** * Completed submit(runnable) returns successfully */ - public void testSubmitRunnable() { - try { - ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new NoOpRunnable()); - future.get(); - assertTrue(future.isDone()); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + public void testSubmitRunnable() throws Exception { + ExecutorService e = new DirectExecutorService(); + Future future = e.submit(new NoOpRunnable()); + future.get(); + assertTrue(future.isDone()); } /** * Completed submit(runnable, result) returns result */ - public void testSubmitRunnable2() { - try { - ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new NoOpRunnable(), TEST_STRING); - String result = future.get(); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + public void testSubmitRunnable2() throws Exception { + ExecutorService e = new DirectExecutorService(); + Future future = e.submit(new NoOpRunnable(), TEST_STRING); + String result = future.get(); + assertSame(TEST_STRING, result); } /** - * A submitted privileged action to completion + * A submitted privileged action runs to completion */ - public void testSubmitPrivilegedAction() { - 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() { + public void testSubmitPrivilegedAction() throws Exception { + 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); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } - 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() { - 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() { + public void testSubmitPrivilegedExceptionAction() throws Exception { + 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); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } - finally { - Policy.setPolicy(savedPolicy); - } + assertSame(TEST_STRING, future.get()); + }}; + + runWithPermissions(r); } /** * A submitted failed privileged exception action reports exception */ - public void testSubmitFailedPrivilegedExceptionAction() { - 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() { + public void testSubmitFailedPrivilegedExceptionAction() throws Exception { + 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(); }})); - Object result = future.get(); - shouldThrow(); - } - catch (ExecutionException success) { - } - catch (InterruptedException ex) { - unexpectedException(); - } - finally { - Policy.setPolicy(savedPolicy); - } + try { + future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + }}}; + + runWithPermissions(r); } /** @@ -228,15 +152,9 @@ public class AbstractExecutorServiceTest public void testExecuteNullRunnable() { try { ExecutorService e = new DirectExecutorService(); - TrackedShortRunnable task = null; - Future future = e.submit(task); + e.submit((Runnable) null); shouldThrow(); - } - catch (NullPointerException success) { - } - catch (Exception ex) { - unexpectedException(); - } + } catch (NullPointerException success) {} } @@ -246,149 +164,60 @@ public class AbstractExecutorServiceTest public void testSubmitNullCallable() { try { ExecutorService e = new DirectExecutorService(); - StringTask t = null; - Future future = e.submit(t); - shouldThrow(); - } - catch (NullPointerException success) { - } - catch (Exception ex) { - unexpectedException(); - } - } - - /** - * submit(runnable) throws RejectedExecutionException if - * executor is saturated. - */ - public void testExecute1() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1)); - try { - - for(int i = 0; i < 5; ++i){ - p.submit(new MediumRunnable()); - } - shouldThrow(); - } catch(RejectedExecutionException success){} - joinPool(p); - } - - /** - * submit(callable) throws RejectedExecutionException - * if executor is saturated. - */ - public void testExecute2() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1)); - try { - for(int i = 0; i < 5; ++i) { - p.submit(new SmallCallable()); - } + e.submit((Callable) null); shouldThrow(); - } catch(RejectedExecutionException e){} - joinPool(p); - } - - - /** - * Blocking on submit(callable) throws InterruptedException if - * caller interrupted. - */ - public void testInterruptedSubmit() { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - Thread t = new Thread(new Runnable() { - public void run() { - try { - p.submit(new Callable() { - public Object call() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - shouldThrow(); - } catch(InterruptedException e){ - } - return null; - } - }).get(); - } catch(InterruptedException success){ - } catch(Exception e) { - unexpectedException(); - } - - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - } catch(Exception e){ - unexpectedException(); - } - joinPool(p); + } catch (NullPointerException success) {} } /** - * get of submitted callable throws Exception if callable - * interrupted + * submit(callable).get() throws InterruptedException if interrupted */ - public void testSubmitIE() { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - - final Callable c = new Callable() { - public Object call() { - try { - p.submit(new SmallCallable()).get(); - shouldThrow(); - } catch(InterruptedException e){} - catch(RejectedExecutionException e2){} - catch(ExecutionException e3){} - return Boolean.TRUE; - } - }; - - - - Thread t = new Thread(new Runnable() { - public void run() { - try { - c.call(); - } catch(Exception e){} - } - }); - try { + public void testInterruptedSubmit() throws InterruptedException { + 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(); - Thread.sleep(SHORT_DELAY_MS); + submitted.await(); t.interrupt(); t.join(); - } catch(InterruptedException e){ - unexpectedException(); + } finally { + quittingTime.countDown(); + joinPool(p); } - - joinPool(p); } /** - * get of submit(callable) throws ExecutionException if callable - * throws exception + * get of submit(callable) throws ExecutionException if callable + * throws exception */ - public void testSubmitEE() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - - try { - Callable c = new Callable() { - public Object call() { - int i = 5/0; - return Boolean.TRUE; - } - }; + public void testSubmitEE() throws InterruptedException { + ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 1, + 60, TimeUnit.SECONDS, + new ArrayBlockingQueue(10)); - for(int i =0; i < 5; i++){ - p.submit(c).get(); - } + Callable c = new Callable() { + public Object call() { return 5/0; }}; + try { + p.submit(c).get(); shouldThrow(); - } - catch(ExecutionException success){ - } catch(Exception e) { - unexpectedException(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof ArithmeticException); } joinPool(p); } @@ -396,13 +225,12 @@ public class AbstractExecutorServiceTest /** * invokeAny(null) throws NPE */ - public void testInvokeAny1() { + public void testInvokeAny1() throws Exception { ExecutorService e = new DirectExecutorService(); try { e.invokeAny(null); + shouldThrow(); } catch (NullPointerException success) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -411,13 +239,12 @@ public class AbstractExecutorServiceTest /** * invokeAny(empty collection) throws IAE */ - public void testInvokeAny2() { + public void testInvokeAny2() throws Exception { ExecutorService e = new DirectExecutorService(); try { e.invokeAny(new ArrayList>()); + shouldThrow(); } catch (IllegalArgumentException success) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -426,17 +253,16 @@ public class AbstractExecutorServiceTest /** * invokeAny(c) throws NPE if c has null elements */ - public void testInvokeAny3() { + public void testInvokeAny3() throws Exception { 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 StringTask()); - l.add(null); e.invokeAny(l); + shouldThrow(); } catch (NullPointerException success) { - } catch(Exception ex) { - ex.printStackTrace(); - unexpectedException(); } finally { joinPool(e); } @@ -445,15 +271,15 @@ public class AbstractExecutorServiceTest /** * invokeAny(c) throws ExecutionException if no task in c completes */ - public void testInvokeAny4() { + 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); - } catch(ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -462,17 +288,14 @@ public class AbstractExecutorServiceTest /** * invokeAny(c) returns result of some task in c if at least one completes */ - public void testInvokeAny5() { + 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); assertSame(TEST_STRING, result); - } catch (ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -481,13 +304,12 @@ public class AbstractExecutorServiceTest /** * invokeAll(null) throws NPE */ - public void testInvokeAll1() { + public void testInvokeAll1() throws InterruptedException { ExecutorService e = new DirectExecutorService(); try { e.invokeAll(null); + shouldThrow(); } catch (NullPointerException success) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -496,13 +318,11 @@ public class AbstractExecutorServiceTest /** * invokeAll(empty collection) returns empty collection */ - public void testInvokeAll2() { + public void testInvokeAll2() throws InterruptedException { ExecutorService e = new DirectExecutorService(); try { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -511,16 +331,15 @@ public class AbstractExecutorServiceTest /** * invokeAll(c) throws NPE if c has null elements */ - public void testInvokeAll3() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -529,18 +348,19 @@ public class AbstractExecutorServiceTest /** * get of returned element of invokeAll(c) throws exception on failed task */ - public void testInvokeAll4() { + 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 (Iterator> it = result.iterator(); it.hasNext();) - it.next().get(); - } catch(ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + 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); } @@ -549,19 +369,16 @@ public class AbstractExecutorServiceTest /** * invokeAll(c) returns results of all completed tasks in c */ - public void testInvokeAll5() { + 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 (Iterator> it = result.iterator(); it.hasNext();) - assertSame(TEST_STRING, it.next().get()); - } catch (ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + List> futures = e.invokeAll(l); + assertEquals(2, futures.size()); + for (Future future : futures) + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } @@ -571,13 +388,12 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(null) throws NPE */ - public void testTimedInvokeAny1() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -586,15 +402,14 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(null time unit) throws NPE */ - public void testTimedInvokeAnyNullTimeUnit() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -603,13 +418,12 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(empty collection) throws IAE */ - public void testTimedInvokeAny2() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -618,17 +432,16 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(c) throws NPE if c has null elements */ - public void testTimedInvokeAny3() { + public void testTimedInvokeAny3() throws Exception { 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 StringTask()); - l.add(null); - e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (NullPointerException success) { - } catch(Exception ex) { - ex.printStackTrace(); - unexpectedException(); } finally { joinPool(e); } @@ -637,15 +450,15 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(c) throws ExecutionException if no task completes */ - public void testTimedInvokeAny4() { + 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); - } catch(ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -654,17 +467,14 @@ public class AbstractExecutorServiceTest /** * timed invokeAny(c) returns result of some task in c */ - public void testTimedInvokeAny5() { + 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); - } catch (ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -673,13 +483,12 @@ public class AbstractExecutorServiceTest /** * timed invokeAll(null) throws NPE */ - public void testTimedInvokeAll1() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -688,15 +497,14 @@ public class AbstractExecutorServiceTest /** * timed invokeAll(null time unit) throws NPE */ - public void testTimedInvokeAllNullTimeUnit() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -705,13 +513,11 @@ public class AbstractExecutorServiceTest /** * timed invokeAll(empty collection) returns empty collection */ - public void testTimedInvokeAll2() { + 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()); - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -720,16 +526,15 @@ public class AbstractExecutorServiceTest /** * timed invokeAll(c) throws NPE if c has null elements */ - public void testTimedInvokeAll3() { + 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) { - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -738,18 +543,20 @@ public class AbstractExecutorServiceTest /** * get of returned element of invokeAll(c) throws exception on failed task */ - public void testTimedInvokeAll4() { + 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 (Iterator> it = result.iterator(); it.hasNext();) - it.next().get(); - } catch(ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + 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); } @@ -758,19 +565,17 @@ public class AbstractExecutorServiceTest /** * timed invokeAll(c) returns results of all completed tasks in c */ - public void testTimedInvokeAll5() { + 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 (Iterator> it = result.iterator(); it.hasNext();) - assertSame(TEST_STRING, it.next().get()); - } catch (ExecutionException success) { - } catch(Exception ex) { - unexpectedException(); + 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); } @@ -779,26 +584,26 @@ public class AbstractExecutorServiceTest /** * timed invokeAll cancels tasks not completed by timeout */ - public void testTimedInvokeAll6() { + 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(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), 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, SHORT_DELAY_MS, MILLISECONDS); + assertEquals(3, futures.size()); + Iterator> it = futures.iterator(); Future f1 = it.next(); Future f2 = it.next(); Future f3 = it.next(); assertTrue(f1.isDone()); assertFalse(f1.isCancelled()); assertTrue(f2.isDone()); + assertFalse(f2.isCancelled()); assertTrue(f3.isDone()); assertTrue(f3.isCancelled()); - } catch(Exception ex) { - unexpectedException(); } finally { joinPool(e); }