--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2010/10/09 19:30:34 1.27 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2013/01/14 22:05:39 1.33 @@ -1,17 +1,16 @@ /* * 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 java.util.concurrent.atomic.AtomicBoolean; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.math.BigInteger; import java.security.*; public class AbstractExecutorServiceTest extends JSR166TestCase { @@ -46,14 +45,19 @@ public class AbstractExecutorServiceTest */ public void testExecuteRunnable() throws Exception { ExecutorService e = new DirectExecutorService(); - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); + final AtomicBoolean done = new AtomicBoolean(false); + CheckedRunnable task = new CheckedRunnable() { + public void realRun() { + done.set(true); + }}; Future future = e.submit(task); - future.get(); - assertTrue(task.done); + assertNull(future.get()); + assertNull(future.get(0, MILLISECONDS)); + assertTrue(done.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } - /** * Completed submit(callable) returns result */ @@ -84,7 +88,6 @@ public class AbstractExecutorServiceTest assertSame(TEST_STRING, result); } - /** * A submitted privileged action runs to completion */ @@ -157,7 +160,6 @@ public class AbstractExecutorServiceTest } catch (NullPointerException success) {} } - /** * submit(null callable) throws NPE */ @@ -170,53 +172,6 @@ public class AbstractExecutorServiceTest } /** - * 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 { @@ -248,28 +203,6 @@ 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 */ @@ -280,7 +213,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(); @@ -294,8 +227,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); @@ -309,8 +241,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>()); @@ -326,9 +257,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); @@ -455,7 +386,6 @@ public class AbstractExecutorServiceTest } } - /** * timed invokeAny(null) throws NPE */ @@ -505,9 +435,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); @@ -660,20 +590,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); }