--- jsr166/src/test/tck/ExecutorsTest.java 2003/09/26 15:33:13 1.6 +++ jsr166/src/test/tck/ExecutorsTest.java 2004/01/22 14:07:50 1.15 @@ -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. */ @@ -10,40 +11,29 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; import java.math.BigInteger; +import java.security.*; public class ExecutorsTest 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(ExecutorsTest.class); - } - - private static final String TEST_STRING = "a test string"; - - private static class StringTask implements Callable { - public String call() { return TEST_STRING; } - } - - static class DirectExecutor implements Executor { - public void execute(Runnable r) { - r.run(); - } + return new TestSuite(ExecutorsTest.class); } static class TimedCallable implements Callable { - private final Executor exec; + private final ExecutorService exec; private final Callable func; private final long msecs; - TimedCallable(Executor exec, Callable func, long msecs) { + TimedCallable(ExecutorService exec, Callable func, long msecs) { this.exec = exec; this.func = func; this.msecs = msecs; } public T call() throws Exception { - Future ftask = Executors.execute(exec, func); + Future ftask = exec.submit(func); try { return ftask.get(msecs, TimeUnit.MILLISECONDS); } finally { @@ -79,7 +69,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -90,7 +80,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -114,7 +104,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -125,7 +115,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -141,6 +131,20 @@ public class ExecutorsTest extends JSR16 } /** + * A new SingleThreadExecutor cannot be casted to concrete implementation + */ + public void testCastNewSingleThreadExecutor() { + ExecutorService e = Executors.newSingleThreadExecutor(); + try { + ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; + } catch (ClassCastException success) { + } finally { + joinPool(e); + } + } + + + /** * A new newFixedThreadPool can execute runnables */ public void testNewFixedThreadPool1() { @@ -148,7 +152,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -159,7 +163,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -186,372 +190,462 @@ public class ExecutorsTest extends JSR16 } } + /** - * execute of runnable runs it to completion + * An unconfigurable newFixedThreadPool can execute runnables */ - public void testExecuteRunnable () { - try { - Executor e = new DirectExecutor(); - TrackedRunnable task = new TrackedRunnable(); - assertFalse(task.done); - Future future = Executors.execute(e, task, TEST_STRING); - String result = future.get(); - assertTrue(task.done); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + public void testunconfigurableExecutorService() { + ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + joinPool(e); } /** - * invoke of a runnable runs it to completion + * unconfigurableExecutorService(null) throws NPE */ - public void testInvokeRunnable () { + public void testunconfigurableExecutorServiceNPE() { try { - Executor e = new DirectExecutor(); - TrackedRunnable task = new TrackedRunnable(); - assertFalse(task.done); - Executors.invoke(e, task); - assertTrue(task.done); + ExecutorService e = Executors.unconfigurableExecutorService(null); } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); + catch (NullPointerException success) { } } /** - * execute of a callable runs it to completion + * unconfigurableScheduledExecutorService(null) throws NPE */ - public void testExecuteCallable () { + public void testunconfigurableScheduledExecutorServiceNPE() { try { - Executor e = new DirectExecutor(); - Future future = Executors.execute(e, new StringTask()); - String result = future.get(); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); + ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); } - catch (InterruptedException ex) { - unexpectedException(); + catch (NullPointerException success) { } } + /** - * invoke of a collable runs it to completion + * a newSingleThreadScheduledExecutor successfully runs delayed task */ - public void testInvokeCallable () { - try { - Executor e = new DirectExecutor(); - String result = Executors.invoke(e, new StringTask()); - - assertSame(TEST_STRING, result); + public void testNewSingleThreadScheduledExecutor() { + try { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); + Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); + } catch(RejectedExecutionException e){} + catch(Exception e){ + e.printStackTrace(); + unexpectedException(); } - catch (ExecutionException ex) { + } + + /** + * a newScheduledThreadPool successfully runs delayed task + */ + public void testnewScheduledThreadPool() { + try { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); + Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); + } catch(RejectedExecutionException e){} + catch(Exception e){ + e.printStackTrace(); unexpectedException(); } - catch (InterruptedException ex) { + } + + /** + * an unconfigurable newScheduledThreadPool successfully runs delayed task + */ + public void testunconfigurableScheduledExecutorService() { + try { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); + Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); + } catch(RejectedExecutionException e){} + catch(Exception e){ + e.printStackTrace(); unexpectedException(); } } /** - * execute with null executor throws NPE + * timeouts from execute will time out if they compute too long. */ - public void testNullExecuteRunnable () { + public void testTimedCallable() { + int N = 10000; + ExecutorService executor = Executors.newSingleThreadExecutor(); + List> tasks = new ArrayList>(N); try { - TrackedRunnable task = new TrackedRunnable(); - assertFalse(task.done); - Future future = Executors.execute(null, task, TEST_STRING); - shouldThrow(); - } - catch (NullPointerException success) { + long startTime = System.currentTimeMillis(); + + long i = 0; + while (tasks.size() < N) { + tasks.add(new TimedCallable(executor, new Fib(i), 1)); + i += 10; + } + + int iters = 0; + BigInteger sum = BigInteger.ZERO; + for (Iterator> it = tasks.iterator(); it.hasNext();) { + try { + ++iters; + sum = sum.add(it.next().call()); + } + catch (TimeoutException success) { + assertTrue(iters > 0); + return; + } + catch (Exception e) { + unexpectedException(); + } + } + // if by chance we didn't ever time out, total time must be small + long elapsed = System.currentTimeMillis() - startTime; + assertTrue(elapsed < N); } - catch (Exception ex) { - unexpectedException(); + finally { + joinPool(executor); } } + /** - * execute with a null runnable throws NPE + * ThreadPoolExecutor using defaultThreadFactory has + * specified group, priority, daemon status, and name */ - public void testExecuteNullRunnable() { + public void testDefaultThreadFactory() { + final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); + Runnable r = new Runnable() { + public void run() { + try { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + } catch (SecurityException ok) { + // Also pass if not allowed to change setting + } + } + }; + ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); + + e.execute(r); try { - Executor e = new DirectExecutor(); - TrackedRunnable task = null; - Future future = Executors.execute(e, task, TEST_STRING); - shouldThrow(); + e.shutdown(); + } catch(SecurityException ok) { } - catch (NullPointerException success) { - } - catch (Exception ex) { + + try { + Thread.sleep(SHORT_DELAY_MS); + } catch (Exception eX) { unexpectedException(); + } finally { + joinPool(e); } } /** - * invoke of a null runnable throws NPE + * ThreadPoolExecutor using privilegedThreadFactory has + * specified group, priority, daemon status, name, + * access control context and context class loader */ - public void testInvokeNullRunnable () { + public void testPrivilegedThreadFactory() { + Policy savedPolicy = null; try { - Executor e = new DirectExecutor(); - TrackedRunnable task = null; - Executors.invoke(e, task); - shouldThrow(); + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; } - catch (NullPointerException success) { + final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); + final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); + final AccessControlContext thisacc = AccessController.getContext(); + Runnable r = new Runnable() { + public void run() { + try { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + threadAssertTrue(thisccl == current.getContextClassLoader()); + threadAssertTrue(thisacc.equals(AccessController.getContext())); + } catch(SecurityException ok) { + // Also pass if not allowed to change settings + } + } + }; + ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); + + Policy.setPolicy(savedPolicy); + e.execute(r); + try { + e.shutdown(); + } catch(SecurityException ok) { } - catch (Exception ex) { + try { + Thread.sleep(SHORT_DELAY_MS); + } catch (Exception ex) { unexpectedException(); + } finally { + joinPool(e); } + } + static class CheckCCL implements Callable { + public Object call() { + AccessControlContext acc = AccessController.getContext(); + acc.checkPermission(new RuntimePermission("getContextClassLoader")); + return null; + } + } + + /** - * execute of a null callable throws NPE + * Without class loader permissions, creating + * privilegedCallableUsingCurrentClassLoader throws ACE */ - public void testExecuteNullCallable () { + public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { + Policy savedPolicy = null; try { - Executor e = new DirectExecutor(); - StringTask t = null; - Future future = Executors.execute(e, t); - shouldThrow(); + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; } - catch (NullPointerException success) { - } - catch (Exception ex) { + + try { + Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); + shouldThrow(); + } catch(AccessControlException success) { + } catch(Exception ex) { unexpectedException(); + } + finally { + Policy.setPolicy(savedPolicy); } } /** - * invoke of a null callable throws NPE + * Without class loader permissions, calling + * privilegedCallableUsingCurrentClassLoader throws ACE */ - public void testInvokeNullCallable () { + public void testprivilegedCallableUsingCCLWithPrivs() { + Policy savedPolicy = null; try { - Executor e = new DirectExecutor(); - StringTask t = null; - String result = Executors.invoke(e, t); - shouldThrow(); - } - catch (NullPointerException success) { + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; } - catch (Exception ex) { + + try { + Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); + task.call(); + } catch(Exception ex) { unexpectedException(); + } + finally { + Policy.setPolicy(savedPolicy); + } + } + + class CallCCL implements Runnable { + final Callable task; + CallCCL(Callable t) { task = t; } + public void run() { + try { + task.call(); + threadShouldThrow(); + } catch(AccessControlException success) { + } catch(Exception ex) { + threadUnexpectedException(); + } } } /** - * execute(Executor, Runnable) throws RejectedExecutionException - * if saturated. + * Without permissions, calling privilegedCallable throws ACE */ - public void testExecute1() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); + public void testprivilegedCallableWithNoPrivs() { + Thread t1; try { - - for(int i = 0; i < 5; ++i){ - Executors.execute(p, new MediumRunnable(), Boolean.TRUE); - } - shouldThrow(); - } catch(RejectedExecutionException success){} - joinPool(p); + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + Policy.setPolicy(policy); + Callable task = Executors.privilegedCallable(new CheckCCL()); + t1 = new Thread(new CallCCL(task)); + Policy.setPolicy(savedPolicy); + } catch (AccessControlException ok) { + return; + } + + try { + t1.start(); + t1.join(); + } catch(Exception ex) { + unexpectedException(); + } } /** - * execute(Executor, Callable)throws RejectedExecutionException - * if saturated. + * With permissions, calling privilegedCallable succeeds */ - public void testExecute2() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); + public void testprivilegedCallableWithPrivs() { + Policy savedPolicy = null; try { - for(int i = 0; i < 5; ++i) { - Executors.execute(p, new SmallCallable()); - } - shouldThrow(); - } catch(RejectedExecutionException e){} - joinPool(p); + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; + } + + Callable task = Executors.privilegedCallable(new CheckCCL()); + try { + task.call(); + } catch(Exception ex) { + unexpectedException(); + } finally { + Policy.setPolicy(savedPolicy); + } } - /** - * invoke(Executor, Runnable) throws InterruptedException if - * caller interrupted. - */ - public void testInterruptedInvoke() { - 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 { - Executors.invoke(p,new Runnable() { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - shouldThrow(); - } catch(InterruptedException e){ - } - } - }); - } catch(InterruptedException success){ - } catch(Exception e) { - unexpectedException(); - } - - } - }); + * callable(Runnable) returns null when called + */ + public void testCallable1() { try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - } catch(Exception e){ + Callable c = Executors.callable(new NoOpRunnable()); + assertNull(c.call()); + } catch(Exception ex) { unexpectedException(); } - joinPool(p); + } /** - * invoke(Executor, Runnable) throws ExecutionException if - * runnable throws exception. - */ - public void testInvoke3() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + * callable(Runnable, result) returns result when called + */ + public void testCallable2() { try { - Runnable r = new Runnable() { - public void run() { - int i = 5/0; - } - }; - - for(int i =0; i < 5; i++){ - Executors.invoke(p,r); - } - - shouldThrow(); - } catch(ExecutionException success){ - } catch(Exception e){ + Callable c = Executors.callable(new NoOpRunnable(), one); + assertEquals(one, c.call()); + } catch(Exception ex) { unexpectedException(); } - joinPool(p); } - - /** - * invoke(Executor, Callable) throws InterruptedException if - * callable throws exception - */ - public void testInvoke5() { - final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); - - final Callable c = new Callable() { - public Object call() { - try { - Executors.invoke(p, new SmallCallable()); - 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){} - } - }); + * callable(PrivilegedAction) returns its result when called + */ + public void testCallable3() { try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(InterruptedException e){ + Callable c = Executors.callable(new PrivilegedAction() { + public Object run() { return one; }}); + assertEquals(one, c.call()); + } catch(Exception ex) { unexpectedException(); } - - joinPool(p); } /** - * invoke(Executor, Callable) will throw ExecutionException - * if callable throws exception - */ - public void testInvoke6() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); - + * callable(PrivilegedExceptionAction) returns its result when called + */ + public void testCallable4() { try { - Callable c = new Callable() { - public Object call() { - int i = 5/0; - return Boolean.TRUE; - } - }; - - for(int i =0; i < 5; i++){ - Executors.invoke(p,c); - } - - shouldThrow(); - } - catch(ExecutionException success){ - } catch(Exception e) { + Callable c = Executors.callable(new PrivilegedExceptionAction() { + public Object run() { return one; }}); + assertEquals(one, c.call()); + } catch(Exception ex) { unexpectedException(); } - joinPool(p); } + /** + * callable(null Runnable) throws NPE + */ + public void testCallableNPE1() { + try { + Runnable r = null; + Callable c = Executors.callable(r); + } catch (NullPointerException success) { + } + } /** - * timeouts from execute will time out if they compute too long. - */ - public void testTimedCallable() { - int N = 10000; - ExecutorService executor = Executors.newSingleThreadExecutor(); - List> tasks = new ArrayList>(N); + * callable(null, result) throws NPE + */ + public void testCallableNPE2() { try { - long startTime = System.currentTimeMillis(); - - long i = 0; - while (tasks.size() < N) { - tasks.add(new TimedCallable(executor, new Fib(i), 1)); - i += 10; - } - - int iters = 0; - BigInteger sum = BigInteger.ZERO; - for (Iterator> it = tasks.iterator(); it.hasNext();) { - try { - ++iters; - sum = sum.add(it.next().call()); - } - catch (TimeoutException success) { - assertTrue(iters > 0); - return; - } - catch (Exception e) { - unexpectedException(); - } - } - // if by chance we didn't ever time out, total time must be small - long elapsed = System.currentTimeMillis() - startTime; - assertTrue(elapsed < N); + Runnable r = null; + Callable c = Executors.callable(r, one); + } catch (NullPointerException success) { } - finally { - joinPool(executor); + } + + /** + * callable(null PrivilegedAction) throws NPE + */ + public void testCallableNPE3() { + try { + PrivilegedAction r = null; + Callable c = Executors.callable(r); + } catch (NullPointerException success) { } } - + /** + * callable(null PrivilegedExceptionAction) throws NPE + */ + public void testCallableNPE4() { + try { + PrivilegedExceptionAction r = null; + Callable c = Executors.callable(r); + } catch (NullPointerException success) { + } + } }