--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2003/12/22 16:25:38 1.7 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2009/11/16 05:30:07 1.18 @@ -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. */ @@ -12,7 +13,7 @@ import java.util.concurrent.*; 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()); } @@ -20,7 +21,7 @@ public class AbstractExecutorServiceTest return new TestSuite(AbstractExecutorServiceTest.class); } - /** + /** * A no-frills implementation of AbstractExecutorService, designed * to test the submit methods only. */ @@ -35,7 +36,7 @@ public class AbstractExecutorServiceTest } /** - * execute of runnable runs it to completion + * execute(runnable) runs it to completion */ public void testExecuteRunnable() { try { @@ -56,7 +57,7 @@ public class AbstractExecutorServiceTest /** - * completed submit of callable returns result + * Completed submit(callable) returns result */ public void testSubmitCallable() { try { @@ -74,7 +75,7 @@ public class AbstractExecutorServiceTest } /** - * completed submit of runnable returns successfully + * Completed submit(runnable) returns successfully */ public void testSubmitRunnable() { try { @@ -92,7 +93,7 @@ public class AbstractExecutorServiceTest } /** - * completed submit of (runnable, result) returns result + * Completed submit(runnable, result) returns result */ public void testSubmitRunnable2() { try { @@ -111,14 +112,19 @@ public class AbstractExecutorServiceTest /** - * submit of a privileged action runs it to completion + * A submitted privileged action to completion */ public void testSubmitPrivilegedAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); + 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() { @@ -136,19 +142,29 @@ public class AbstractExecutorServiceTest unexpectedException(); } finally { - Policy.setPolicy(savedPolicy); + try { + Policy.setPolicy(savedPolicy); + } catch (AccessControlException ok) { + return; + } } } /** - * submit of a privileged exception action runs it to completion + * A submitted a privileged exception action runs to completion */ public void testSubmitPrivilegedExceptionAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); + 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() { @@ -171,14 +187,21 @@ public class AbstractExecutorServiceTest } /** - * submit of a failed privileged exception action reports exception + * A submitted failed privileged exception action reports exception */ public void testSubmitFailedPrivilegedExceptionAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); + 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() { @@ -200,7 +223,7 @@ public class AbstractExecutorServiceTest } /** - * execute with a null runnable throws NPE + * execute(null runnable) throws NPE */ public void testExecuteNullRunnable() { try { @@ -218,7 +241,7 @@ public class AbstractExecutorServiceTest /** - * submit of a null callable throws NPE + * submit(null callable) throws NPE */ public void testSubmitNullCallable() { try { @@ -235,43 +258,43 @@ public class AbstractExecutorServiceTest } /** - * submit of Runnable throws RejectedExecutionException if - * saturated. + * submit(runnable) throws RejectedExecutionException if + * executor is saturated. */ public void testExecute1() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1)); try { - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { p.submit(new MediumRunnable()); } shouldThrow(); - } catch(RejectedExecutionException success){} + } catch (RejectedExecutionException success) {} joinPool(p); } /** - * Completed submit of Callable throws RejectedExecutionException - * if saturated. + * submit(callable) throws RejectedExecutionException + * if executor is saturated. */ public void testExecute2() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1)); try { - for(int i = 0; i < 5; ++i) { + for (int i = 0; i < 5; ++i) { p.submit(new SmallCallable()); } shouldThrow(); - } catch(RejectedExecutionException e){} + } catch (RejectedExecutionException e) {} joinPool(p); } /** - * blocking on submit of Callable throws InterruptedException if + * Blocking on submit(callable) throws InterruptedException if * caller interrupted. */ public void testInterruptedSubmit() { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); Thread t = new Thread(new Runnable() { public void run() { try { @@ -280,13 +303,13 @@ public class AbstractExecutorServiceTest try { Thread.sleep(MEDIUM_DELAY_MS); shouldThrow(); - } catch(InterruptedException e){ + } catch (InterruptedException e) { } return null; } }).get(); - } catch(InterruptedException success){ - } catch(Exception e) { + } catch (InterruptedException success) { + } catch (Exception e) { unexpectedException(); } @@ -296,27 +319,27 @@ public class AbstractExecutorServiceTest t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); } joinPool(p); } /** - * get of submit of Callable throws Exception if callable + * get of submitted callable throws Exception if callable * interrupted */ public void testSubmitIE() { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + 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){} + } catch (InterruptedException e) {} + catch (RejectedExecutionException e2) {} + catch (ExecutionException e3) {} return Boolean.TRUE; } }; @@ -327,7 +350,7 @@ public class AbstractExecutorServiceTest public void run() { try { c.call(); - } catch(Exception e){} + } catch (Exception e) {} } }); try { @@ -335,7 +358,7 @@ public class AbstractExecutorServiceTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch(InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } @@ -343,11 +366,11 @@ public class AbstractExecutorServiceTest } /** - * completed submit of 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,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); try { Callable c = new Callable() { @@ -357,14 +380,14 @@ public class AbstractExecutorServiceTest } }; - for(int i =0; i < 5; i++){ + for (int i =0; i < 5; i++) { p.submit(c).get(); } shouldThrow(); } - catch(ExecutionException success){ - } catch(Exception e) { + catch (ExecutionException success) { + } catch (Exception e) { unexpectedException(); } joinPool(p); @@ -378,7 +401,7 @@ public class AbstractExecutorServiceTest try { e.invokeAny(null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -393,7 +416,7 @@ public class AbstractExecutorServiceTest try { e.invokeAny(new ArrayList>()); } catch (IllegalArgumentException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -411,7 +434,7 @@ public class AbstractExecutorServiceTest l.add(null); e.invokeAny(l); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { ex.printStackTrace(); unexpectedException(); } finally { @@ -420,19 +443,16 @@ public class AbstractExecutorServiceTest } /** - * invokeAny(c) throws ExecutionException if no task completes + * invokeAny(c) throws ExecutionException if no task in c completes */ public void testInvokeAny4() { 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) { + e.invokeAny(l); + } catch (ExecutionException success) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -440,7 +460,7 @@ public class AbstractExecutorServiceTest } /** - * invokeAny(c) returns result of some task + * invokeAny(c) returns result of some task in c if at least one completes */ public void testInvokeAny5() { ExecutorService e = new DirectExecutorService(); @@ -451,7 +471,7 @@ public class AbstractExecutorServiceTest String result = e.invokeAny(l); assertSame(TEST_STRING, result); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -466,7 +486,7 @@ public class AbstractExecutorServiceTest try { e.invokeAll(null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -481,7 +501,7 @@ public class AbstractExecutorServiceTest try { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -499,7 +519,7 @@ public class AbstractExecutorServiceTest l.add(null); e.invokeAll(l); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -507,7 +527,7 @@ public class AbstractExecutorServiceTest } /** - * get of element of invokeAll(c) throws exception on failed task + * get of returned element of invokeAll(c) throws exception on failed task */ public void testInvokeAll4() { ExecutorService e = new DirectExecutorService(); @@ -516,10 +536,10 @@ public class AbstractExecutorServiceTest l.add(new NPETask()); List> result = e.invokeAll(l); assertEquals(1, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) it.next().get(); - } catch(ExecutionException success) { - } catch(Exception ex) { + } catch (ExecutionException success) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -527,7 +547,7 @@ public class AbstractExecutorServiceTest } /** - * invokeAll(c) returns results of all completed tasks + * invokeAll(c) returns results of all completed tasks in c */ public void testInvokeAll5() { ExecutorService e = new DirectExecutorService(); @@ -537,10 +557,247 @@ public class AbstractExecutorServiceTest l.add(new StringTask()); List> result = e.invokeAll(l); assertEquals(2, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) assertSame(TEST_STRING, it.next().get()); } catch (ExecutionException success) { - } catch(Exception ex) { + } 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 time unit) 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 in c + */ + 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 time unit) 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 returned 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 in c + */ + 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 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 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(); + Future f1 = it.next(); + Future f2 = it.next(); + Future f3 = it.next(); + assertTrue(f1.isDone()); + assertFalse(f1.isCancelled()); + assertTrue(f2.isDone()); + assertTrue(f3.isDone()); + assertTrue(f3.isCancelled()); + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e);