--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2010/09/16 02:54:10 1.25 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2015/04/25 04:55:30 1.37 @@ -1,22 +1,36 @@ /* * 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 + * 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.*; + +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import junit.framework.Test; +import junit.framework.TestSuite; public class AbstractExecutorServiceTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(AbstractExecutorServiceTest.class); @@ -29,10 +43,15 @@ public class AbstractExecutorServiceTest 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; } @@ -41,14 +60,18 @@ public class AbstractExecutorServiceTest */ 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); + final AtomicBoolean done = new AtomicBoolean(false); + Future future = e.submit(new CheckedRunnable() { + public void realRun() { + done.set(true); + }}); + assertNull(future.get()); + assertNull(future.get(0, MILLISECONDS)); + assertTrue(done.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } - /** * Completed submit(callable) returns result */ @@ -79,7 +102,6 @@ public class AbstractExecutorServiceTest assertSame(TEST_STRING, result); } - /** * A submitted privileged action runs to completion */ @@ -145,73 +167,25 @@ public class AbstractExecutorServiceTest * execute(null runnable) throws NPE */ public void testExecuteNullRunnable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} } - /** * submit(null callable) throws NPE */ public void testSubmitNullCallable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} } /** - * 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 < 2; ++i) - p.submit(new MediumRunnable()); - for (int i = 0; i < 2; ++i) { - try { - p.submit(new MediumRunnable()); - shouldThrow(); - } catch (RejectedExecutionException success) {} - } - } finally { - 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 < 2; ++i) - p.submit(new MediumRunnable()); - for (int i = 0; i < 2; ++i) { - try { - p.submit(new SmallCallable()); - shouldThrow(); - } catch (RejectedExecutionException success) {} - } - } finally { - joinPool(p); - } - } - - - /** * submit(callable).get() throws InterruptedException if interrupted */ public void testInterruptedSubmit() throws InterruptedException { @@ -243,30 +217,8 @@ public class AbstractExecutorServiceTest } /** - * get of submitted callable throws InterruptedException if callable - * interrupted - */ - public void testSubmitIE() throws InterruptedException { - final ThreadPoolExecutor p = - new ThreadPoolExecutor(1, 1, - 60, TimeUnit.SECONDS, - new ArrayBlockingQueue(10)); - - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws Exception { - p.submit(new SmallCallable()).get(); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - 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() throws InterruptedException { ThreadPoolExecutor p = @@ -275,7 +227,7 @@ public class AbstractExecutorServiceTest new ArrayBlockingQueue(10)); Callable c = new Callable() { - public Object call() { return 5/0; }}; + public Object call() { throw new ArithmeticException(); }}; try { p.submit(c).get(); @@ -289,8 +241,7 @@ public class AbstractExecutorServiceTest /** * invokeAny(null) throws NPE */ - public void testInvokeAny1() - throws InterruptedException, ExecutionException { + public void testInvokeAny1() throws Exception { ExecutorService e = new DirectExecutorService(); try { e.invokeAny(null); @@ -304,8 +255,7 @@ public class AbstractExecutorServiceTest /** * invokeAny(empty collection) throws IAE */ - public void testInvokeAny2() - throws InterruptedException, ExecutionException { + public void testInvokeAny2() throws Exception { ExecutorService e = new DirectExecutorService(); try { e.invokeAny(new ArrayList>()); @@ -321,9 +271,9 @@ public class AbstractExecutorServiceTest */ public void testInvokeAny3() throws Exception { ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new Callable() { - public Integer call() { return 5/0; }}); + List> l = new ArrayList>(); + l.add(new Callable() { + public Long call() { throw new ArithmeticException(); }}); l.add(null); try { e.invokeAny(l); @@ -450,7 +400,6 @@ public class AbstractExecutorServiceTest } } - /** * timed invokeAny(null) throws NPE */ @@ -500,9 +449,9 @@ public class AbstractExecutorServiceTest */ public void testTimedInvokeAny3() throws Exception { ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new Callable() { - public Integer call() { return 5/0; }}); + List> l = new ArrayList>(); + l.add(new Callable() { + public Long call() { throw new ArithmeticException(); }}); l.add(null); try { e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); @@ -655,20 +604,16 @@ public class AbstractExecutorServiceTest try { 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> futures = - e.invokeAll(l, SMALL_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()); - assertTrue(f3.isDone()); - assertTrue(f3.isCancelled()); + e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); + assertEquals(l.size(), futures.size()); + for (Future future : futures) + assertTrue(future.isDone()); + assertFalse(futures.get(0).isCancelled()); + assertFalse(futures.get(1).isCancelled()); + assertTrue(futures.get(2).isCancelled()); } finally { joinPool(e); }