--- jsr166/src/test/tck/ExecutorsTest.java 2003/12/09 19:09:24 1.9 +++ jsr166/src/test/tck/ExecutorsTest.java 2009/11/21 02:33:20 1.26 @@ -1,52 +1,42 @@ /* - * 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. */ 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 ExecutorsTest extends JSR166TestCase{ +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(); - } - } - 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); + return ftask.get(msecs, MILLISECONDS); } finally { ftask.cancel(true); } @@ -80,7 +70,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -91,7 +81,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -101,9 +91,7 @@ public class ExecutorsTest extends JSR16 try { ExecutorService e = Executors.newCachedThreadPool(null); shouldThrow(); - } - catch(NullPointerException success) { - } + } catch (NullPointerException success) {} } @@ -115,7 +103,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -126,7 +114,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -136,11 +124,24 @@ public class ExecutorsTest extends JSR16 try { ExecutorService e = Executors.newSingleThreadExecutor(null); shouldThrow(); - } - catch(NullPointerException success) { + } catch (NullPointerException success) {} + } + + /** + * A new SingleThreadExecutor cannot be casted to concrete implementation + */ + public void testCastNewSingleThreadExecutor() { + ExecutorService e = Executors.newSingleThreadExecutor(); + try { + ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; + shouldThrow(); + } catch (ClassCastException success) { + } finally { + joinPool(e); } } + /** * A new newFixedThreadPool can execute runnables */ @@ -149,7 +150,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -160,7 +161,7 @@ public class ExecutorsTest extends JSR16 e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); - e.shutdown(); + joinPool(e); } /** @@ -170,9 +171,7 @@ public class ExecutorsTest extends JSR16 try { ExecutorService e = Executors.newFixedThreadPool(2, null); shouldThrow(); - } - catch(NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -182,544 +181,425 @@ public class ExecutorsTest extends JSR16 try { ExecutorService e = Executors.newFixedThreadPool(0); shouldThrow(); - } - catch(IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } + /** - * execute of runnable runs it to completion + * An unconfigurable newFixedThreadPool can execute runnables */ - public void testExecuteRunnable() { - try { - Executor e = new DirectExecutor(); - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Future future = Executors.execute(e, task); - future.get(); - assertTrue(task.done); - } - 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(); - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Executors.invoke(e, task); - assertTrue(task.done); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + ExecutorService e = Executors.unconfigurableExecutorService(null); + shouldThrow(); + } 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(); - } - catch (InterruptedException ex) { - unexpectedException(); - } + ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** - * execute of a privileged action runs it to completion + * a newSingleThreadScheduledExecutor successfully runs delayed task */ - public void testExecutePrivilegedAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - try { - Executor e = new DirectExecutor(); - Future future = Executors.execute(e, 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 { - Policy.setPolicy(savedPolicy); - } + public void testNewSingleThreadScheduledExecutor() throws Exception { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); } /** - * execute of a privileged exception action runs it to completion + * a newScheduledThreadPool successfully runs delayed task */ - public void testExecutePrivilegedExceptionAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - try { - Executor e = new DirectExecutor(); - Future future = Executors.execute(e, new PrivilegedExceptionAction() { - public Object run() { - return TEST_STRING; - }}); + public void testnewScheduledThreadPool() throws Exception { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); + } - Object result = future.get(); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); - } - finally { - Policy.setPolicy(savedPolicy); - } + /** + * an unconfigurable newScheduledThreadPool successfully runs delayed task + */ + public void testunconfigurableScheduledExecutorService() throws Exception { + TrackedCallable callable = new TrackedCallable(); + ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + joinPool(p1); } /** - * execute of a failed privileged exception action reports exception + * timeouts from execute will time out if they compute too long. */ - public void testExecuteFailedPrivilegedExceptionAction() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); + public void testTimedCallable() throws Exception { + int N = 10000; + ExecutorService executor = Executors.newSingleThreadExecutor(); + List> tasks = new ArrayList>(N); try { - Executor e = new DirectExecutor(); - Future future = Executors.execute(e, new PrivilegedExceptionAction() { - public Object run() throws Exception { - throw new IndexOutOfBoundsException(); - }}); + long startTime = System.currentTimeMillis(); - Object result = future.get(); - shouldThrow(); - } - catch (ExecutionException success) { - } - catch (InterruptedException ex) { - unexpectedException(); + 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; + } + } + // if by chance we didn't ever time out, total time must be small + long elapsed = System.currentTimeMillis() - startTime; + assertTrue(elapsed < N); } finally { - Policy.setPolicy(savedPolicy); + joinPool(executor); } } + /** - * invoke of a collable runs it to completion + * ThreadPoolExecutor using defaultThreadFactory has + * specified group, priority, daemon status, and name */ - public void testInvokeCallable() { - try { - Executor e = new DirectExecutor(); - String result = Executors.invoke(e, new StringTask()); + public void testDefaultThreadFactory() throws Exception { + 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()); - assertSame(TEST_STRING, result); - } - catch (ExecutionException ex) { - unexpectedException(); + e.execute(r); + try { + e.shutdown(); + } catch (SecurityException ok) { } - catch (InterruptedException ex) { - unexpectedException(); + + try { + Thread.sleep(SHORT_DELAY_MS); + } finally { + joinPool(e); } } /** - * execute with null executor throws NPE + * ThreadPoolExecutor using privilegedThreadFactory has + * specified group, priority, daemon status, name, + * access control context and context class loader */ - public void testNullExecuteRunnable() { + public void testPrivilegedThreadFactory() throws Exception { + Policy savedPolicy = null; try { - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Future future = Executors.execute(null, 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) { - unexpectedException(); + try { + Thread.sleep(SHORT_DELAY_MS); + } finally { + joinPool(e); } + } - /** - * execute with a null runnable throws NPE - */ - public void testExecuteNullRunnable() { - try { - Executor e = new DirectExecutor(); - TrackedShortRunnable task = null; - Future future = Executors.execute(e, task); - shouldThrow(); - } - catch (NullPointerException success) { + void checkCCL() { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + sm.checkPermission(new RuntimePermission("setContextClassLoader")); + sm.checkPermission(new RuntimePermission("getClassLoader")); } - catch (Exception ex) { - unexpectedException(); + } + + class CheckCCL implements Callable { + public Object call() { + checkCCL(); + return null; } } + /** - * invoke of a null runnable throws NPE + * Without class loader permissions, creating + * privilegedCallableUsingCurrentClassLoader throws ACE */ - public void testInvokeNullRunnable() { + public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { + Policy savedPolicy = null; try { - Executor e = new DirectExecutor(); - TrackedShortRunnable task = null; - Executors.invoke(e, task); - shouldThrow(); - } - catch (NullPointerException success) { + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; } - catch (Exception ex) { - unexpectedException(); + + // Check if program still has too many permissions to run test + try { + checkCCL(); + // too many privileges to test; so return + Policy.setPolicy(savedPolicy); + return; + } catch (AccessControlException ok) { } - } - /** - * execute of a null callable throws NPE - */ - public void testExecuteNullCallable() { try { - Executor e = new DirectExecutor(); - StringTask t = null; - Future future = Executors.execute(e, t); + Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); shouldThrow(); - } - catch (NullPointerException success) { - } - catch (Exception ex) { - unexpectedException(); + } catch (AccessControlException success) { + } finally { + Policy.setPolicy(savedPolicy); } } /** - * invoke of a null callable throws NPE + * With class loader permissions, calling + * privilegedCallableUsingCurrentClassLoader does not throw ACE */ - public void testInvokeNullCallable() { + public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { + Policy savedPolicy = null; try { - Executor e = new DirectExecutor(); - StringTask t = null; - String result = Executors.invoke(e, t); - 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) { + + try { + Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); + task.call(); } - catch (Exception ex) { - unexpectedException(); + finally { + Policy.setPolicy(savedPolicy); } } /** - * 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() throws Exception { + Callable task; + Policy savedPolicy = null; + AdjustablePolicy policy = null; + AccessControlContext noprivAcc = null; + try { + savedPolicy = Policy.getPolicy(); + policy = new AdjustablePolicy(); + Policy.setPolicy(policy); + noprivAcc = AccessController.getContext(); + task = Executors.privilegedCallable(new CheckCCL()); + Policy.setPolicy(savedPolicy); + } catch (AccessControlException ok) { + return; // program has too few permissions to set up test + } + + // Make sure that program doesn't have too many permissions try { - - for(int i = 0; i < 5; ++i){ - Executors.execute(p, new MediumRunnable()); - } - shouldThrow(); - } catch(RejectedExecutionException success){} - joinPool(p); - } + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + checkCCL(); + return null; + }}, noprivAcc); + // too many permssions; skip test + return; + } catch (AccessControlException ok) { + } - /** - * execute(Executor, Callable)throws RejectedExecutionException - * if saturated. - */ - public void testExecute2() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); try { - for(int i = 0; i < 5; ++i) { - Executors.execute(p, new SmallCallable()); - } + task.call(); shouldThrow(); - } catch(RejectedExecutionException e){} - joinPool(p); + } catch (AccessControlException success) {} } - /** - * invoke(Executor, Runnable) throws InterruptedException if - * caller interrupted. + * With permissions, calling privilegedCallable succeeds */ - 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(); - } - - } - }); + public void testprivilegedCallableWithPrivs() throws Exception { + Policy savedPolicy = null; try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - } catch(Exception e){ - unexpectedException(); + savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + } catch (AccessControlException ok) { + return; } - 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 task = Executors.privilegedCallable(new CheckCCL()); 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){ - unexpectedException(); + task.call(); + } finally { + Policy.setPolicy(savedPolicy); } - joinPool(p); } - - /** - * invoke(Executor, Callable) throws InterruptedException if - * callable throws exception + * callable(Runnable) returns null when called */ - 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; - } - }; + public void testCallable1() throws Exception { + Callable c = Executors.callable(new NoOpRunnable()); + assertNull(c.call()); + } + /** + * callable(Runnable, result) returns result when called + */ + public void testCallable2() throws Exception { + Callable c = Executors.callable(new NoOpRunnable(), one); + assertEquals(one, c.call()); + } - - Thread t = new Thread(new Runnable() { - public void run() { - try { - c.call(); - } catch(Exception e){} - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(InterruptedException e){ - unexpectedException(); - } - - joinPool(p); + /** + * callable(PrivilegedAction) returns its result when called + */ + public void testCallable3() throws Exception { + Callable c = Executors.callable(new PrivilegedAction() { + public Object run() { return one; }}); + assertEquals(one, c.call()); } /** - * invoke(Executor, Callable) will throw ExecutionException - * if callable throws exception + * callable(PrivilegedExceptionAction) returns its result when called */ - public void testInvoke6() { - ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + public void testCallable4() throws Exception { + Callable c = Executors.callable(new PrivilegedExceptionAction() { + public Object run() { return one; }}); + assertEquals(one, c.call()); + } + + /** + * callable(null Runnable) throws NPE + */ + public void testCallableNPE1() { 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); - } - + Callable c = Executors.callable((Runnable) null); shouldThrow(); - } - catch(ExecutionException success){ - } catch(Exception e) { - unexpectedException(); - } - joinPool(p); + } catch (NullPointerException success) {} } - - /** - * timeouts from execute will time out if they compute too long. + * callable(null, result) throws NPE */ - public void testTimedCallable() { - int N = 10000; - ExecutorService executor = Executors.newSingleThreadExecutor(); - List> tasks = new ArrayList>(N); + 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); - } - finally { - joinPool(executor); - } + Callable c = Executors.callable((Runnable) null, one); + shouldThrow(); + } catch (NullPointerException success) {} } - /** - * ThreadPoolExecutor using defaultThreadFactory has - * specified group, priority, daemon status, and name + * callable(null PrivilegedAction) throws NPE */ - public void testDefaultThreadFactory() { - final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); - Runnable r = new Runnable() { - public void run() { - 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")); - } - }; - ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); - - e.execute(r); - e.shutdown(); + public void testCallableNPE3() { try { - Thread.sleep(SHORT_DELAY_MS); - } catch (Exception eX) { - unexpectedException(); - } finally { - joinPool(e); - } + Callable c = Executors.callable((PrivilegedAction) null); + shouldThrow(); + } catch (NullPointerException success) {} } /** - * ThreadPoolExecutor using privilegedThreadFactory has - * specified group, priority, daemon status, name, - * access control context and context class loader + * callable(null PrivilegedExceptionAction) throws NPE */ - public void testPrivilegedThreadFactory() { - Policy savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); - final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); - final AccessControlContext thisacc = AccessController.getContext(); - Runnable r = new Runnable() { - public void run() { - 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())); - } - }; - ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); - - Policy.setPolicy(savedPolicy); - e.execute(r); - e.shutdown(); + public void testCallableNPE4() { try { - Thread.sleep(SHORT_DELAY_MS); - } catch (Exception ex) { - unexpectedException(); - } finally { - joinPool(e); - } - + Callable c = Executors.callable((PrivilegedExceptionAction) null); + shouldThrow(); + } catch (NullPointerException success) {} } + }