--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2003/12/19 20:38:55 1.4 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2003/12/27 19:26:42 1.10 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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. */ @@ -17,12 +18,12 @@ public class AbstractExecutorServiceTest junit.textui.TestRunner.run (suite()); } public static Test suite() { - return new TestSuite(ExecutorsTest.class); + return new TestSuite(AbstractExecutorServiceTest.class); } /** * A no-frills implementation of AbstractExecutorService, designed - * to test the submit/invoke methods only. + * to test the submit methods only. */ static class DirectExecutorService extends AbstractExecutorService { public void execute(Runnable r) { r.run(); } @@ -34,12 +35,6 @@ public class AbstractExecutorServiceTest private volatile boolean shutdown = false; } - private static final String TEST_STRING = "a test string"; - - private static class StringTask implements Callable { - public String call() { return TEST_STRING; } - } - /** * execute of runnable runs it to completion */ @@ -62,9 +57,9 @@ public class AbstractExecutorServiceTest /** - * execute of a callable runs it to completion + * completed submit of callable returns result */ - public void testExecuteCallable() { + public void testSubmitCallable() { try { ExecutorService e = new DirectExecutorService(); Future future = e.submit(new StringTask()); @@ -79,11 +74,47 @@ public class AbstractExecutorServiceTest } } + /** + * completed submit of 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(); + } + } /** - * execute of a privileged action runs it to completion + * completed submit of (runnable, result) returns result */ - public void testExecutePrivilegedAction() { + 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(); + } + } + + + /** + * submit of a privileged action runs it to completion + */ + public void testSubmitPrivilegedAction() { Policy savedPolicy = Policy.getPolicy(); AdjustablePolicy policy = new AdjustablePolicy(); policy.addPermission(new RuntimePermission("getContextClassLoader")); @@ -91,10 +122,10 @@ public class AbstractExecutorServiceTest Policy.setPolicy(policy); try { ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new PrivilegedAction() { + Future future = e.submit(Executors.callable(new PrivilegedAction() { public Object run() { return TEST_STRING; - }}); + }})); Object result = future.get(); assertSame(TEST_STRING, result); @@ -111,9 +142,9 @@ public class AbstractExecutorServiceTest } /** - * execute of a privileged exception action runs it to completion + * submit of a privileged exception action runs it to completion */ - public void testExecutePrivilegedExceptionAction() { + public void testSubmitPrivilegedExceptionAction() { Policy savedPolicy = Policy.getPolicy(); AdjustablePolicy policy = new AdjustablePolicy(); policy.addPermission(new RuntimePermission("getContextClassLoader")); @@ -121,10 +152,10 @@ public class AbstractExecutorServiceTest Policy.setPolicy(policy); try { ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new PrivilegedExceptionAction() { + Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() { return TEST_STRING; - }}); + }})); Object result = future.get(); assertSame(TEST_STRING, result); @@ -141,9 +172,9 @@ public class AbstractExecutorServiceTest } /** - * execute of a failed privileged exception action reports exception + * submit of a failed privileged exception action reports exception */ - public void testExecuteFailedPrivilegedExceptionAction() { + public void testSubmitFailedPrivilegedExceptionAction() { Policy savedPolicy = Policy.getPolicy(); AdjustablePolicy policy = new AdjustablePolicy(); policy.addPermission(new RuntimePermission("getContextClassLoader")); @@ -151,10 +182,10 @@ public class AbstractExecutorServiceTest Policy.setPolicy(policy); try { ExecutorService e = new DirectExecutorService(); - Future future = e.submit(new PrivilegedExceptionAction() { + Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { public Object run() throws Exception { throw new IndexOutOfBoundsException(); - }}); + }})); Object result = future.get(); shouldThrow(); @@ -170,24 +201,6 @@ public class AbstractExecutorServiceTest } /** - * invoke of a callable runs it to completion - */ - public void testInvokeCallable() { - try { - ExecutorService e = new DirectExecutorService(); - String result = e.invoke(new StringTask()); - - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } - } - - /** * execute with a null runnable throws NPE */ public void testExecuteNullRunnable() { @@ -206,9 +219,9 @@ public class AbstractExecutorServiceTest /** - * execute of a null callable throws NPE + * submit of a null callable throws NPE */ - public void testExecuteNullCallable() { + public void testSubmitNullCallable() { try { ExecutorService e = new DirectExecutorService(); StringTask t = null; @@ -223,25 +236,8 @@ public class AbstractExecutorServiceTest } /** - * invoke of a null callable throws NPE - */ - public void testInvokeNullCallable() { - try { - ExecutorService e = new DirectExecutorService(); - StringTask t = null; - String result = e.invoke(t); - shouldThrow(); - } - catch (NullPointerException success) { - } - catch (Exception ex) { - unexpectedException(); - } - } - - /** - * execute(Executor, Runnable) throws RejectedExecutionException - * if saturated. + * submit of Runnable throws RejectedExecutionException if + * saturated. */ public void testExecute1() { ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); @@ -256,7 +252,7 @@ public class AbstractExecutorServiceTest } /** - * execute(Executor, Callable)throws RejectedExecutionException + * Completed submit of Callable throws RejectedExecutionException * if saturated. */ public void testExecute2() { @@ -272,15 +268,15 @@ public class AbstractExecutorServiceTest /** - * invoke(Executor, Callable) throws InterruptedException if + * blocking on submit of Callable throws InterruptedException if * caller interrupted. */ - public void testInterruptedInvoke() { + public void testInterruptedSubmit() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); Thread t = new Thread(new Runnable() { public void run() { try { - p.invoke(new Callable() { + p.submit(new Callable() { public Object call() { try { Thread.sleep(MEDIUM_DELAY_MS); @@ -289,7 +285,7 @@ public class AbstractExecutorServiceTest } return null; } - }); + }).get(); } catch(InterruptedException success){ } catch(Exception e) { unexpectedException(); @@ -308,16 +304,16 @@ public class AbstractExecutorServiceTest } /** - * invoke(Executor, Callable) throws InterruptedException if - * callable throws exception + * get of submit of Callable throws Exception if callable + * interrupted */ - public void testInvoke5() { + public void testSubmitIE() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); final Callable c = new Callable() { public Object call() { try { - p.invoke(new SmallCallable()); + p.submit(new SmallCallable()).get(); shouldThrow(); } catch(InterruptedException e){} catch(RejectedExecutionException e2){} @@ -348,10 +344,10 @@ public class AbstractExecutorServiceTest } /** - * invoke(Executor, Callable) will throw ExecutionException - * if callable throws exception + * completed submit of Callable throws ExecutionException if + * callable throws exception */ - public void testInvoke6() { + public void testSubmitEE() { ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { @@ -363,7 +359,7 @@ public class AbstractExecutorServiceTest }; for(int i =0; i < 5; i++){ - p.invoke(c); + p.submit(c).get(); } shouldThrow(); @@ -375,4 +371,412 @@ public class AbstractExecutorServiceTest joinPool(p); } + /** + * invokeAny(null) throws NPE + */ + public void testInvokeAny1() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAny(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(empty collection) throws IAE + */ + public void testInvokeAny2() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAny(new ArrayList>()); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws NPE if c has null elements + */ + public void testInvokeAny3() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws ExecutionException if no task completes + */ + public void testInvokeAny4() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) returns result of some task + */ + public void testInvokeAny5() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> 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); + } + } + + /** + * invokeAll(null) throws NPE + */ + public void testInvokeAll1() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAll(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(empty collection) returns empty collection + */ + public void testInvokeAll2() { + ExecutorService e = new DirectExecutorService(); + try { + List> r = e.invokeAll(new ArrayList>()); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) throws NPE if c has null elements + */ + public void testInvokeAll3() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of element of invokeAll(c) throws exception on failed task + */ + public void testInvokeAll4() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> 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(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) returns results of all completed tasks + */ + public void testInvokeAll5() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> 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(); + } finally { + joinPool(e); + } + } + + + /** + * timed invokeAny(null) throws NPE + */ + public void testTimedInvokeAny1() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(,,null) throws NPE + */ + public void testTimedInvokeAnyNullTimeUnit() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAny(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(empty collection) throws IAE + */ + public void testTimedInvokeAny2() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws NPE if c has null elements + */ + public void testTimedInvokeAny3() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws ExecutionException if no task completes + */ + public void testTimedInvokeAny4() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) returns result of some task + */ + public void testTimedInvokeAny5() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(null) throws NPE + */ + public void testTimedInvokeAll1() { + ExecutorService e = new DirectExecutorService(); + try { + e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(,,null) throws NPE + */ + public void testTimedInvokeAllNullTimeUnit() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAll(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(empty collection) returns empty collection + */ + public void testTimedInvokeAll2() { + ExecutorService e = new DirectExecutorService(); + try { + List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) throws NPE if c has null elements + */ + public void testTimedInvokeAll3() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of element of invokeAll(c) throws exception on failed task + */ + public void testTimedInvokeAll4() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> 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(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) returns results of all completed tasks + */ + public void testTimedInvokeAll5() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> 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(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) cancels tasks not completed by timeout + */ + public void testTimedInvokeAll6() { + ExecutorService e = new DirectExecutorService(); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); + List> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertEquals(2, result.size()); + Iterator> it = result.iterator(); + Future f1 = it.next(); + Future f2 = it.next(); + assertTrue(f1.isDone()); + assertFalse(f1.isCancelled()); + assertTrue(f2.isDone()); + // assertTrue(f2.isCancelled()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + }