--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2005/05/03 16:02:00 1.3 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2009/11/21 02:33:20 1.9 @@ -2,20 +2,22 @@ * 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. + * 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.util.concurrent.atomic.*; import java.math.BigInteger; import java.security.*; -public class ExecutorCompletionServiceTest extends JSR166TestCase{ +public class ExecutorCompletionServiceTest 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(ExecutorCompletionServiceTest.class); @@ -24,7 +26,7 @@ public class ExecutorCompletionServiceTe /** * Creating a new ECS with null Executor throw NPE - */ + */ public void testConstructorNPE() { try { ExecutorCompletionService ecs = new ExecutorCompletionService(null); @@ -35,7 +37,7 @@ public class ExecutorCompletionServiceTe /** * Creating a new ECS with null queue throw NPE - */ + */ public void testConstructorNPE2() { try { ExecutorService e = Executors.newCachedThreadPool(); @@ -47,7 +49,7 @@ public class ExecutorCompletionServiceTe /** * Submitting a null callable throws NPE - */ + */ public void testSubmitNPE() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -63,7 +65,7 @@ public class ExecutorCompletionServiceTe /** * Submitting a null runnable throws NPE - */ + */ public void testSubmitNPE2() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -79,7 +81,7 @@ public class ExecutorCompletionServiceTe /** * A taken submitted task is completed - */ + */ public void testTake() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -97,7 +99,7 @@ public class ExecutorCompletionServiceTe /** * Take returns the same future object returned by submit - */ + */ public void testTake2() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -115,7 +117,7 @@ public class ExecutorCompletionServiceTe /** * If poll returns non-null, the returned task is completed - */ + */ public void testPoll1() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -140,7 +142,7 @@ public class ExecutorCompletionServiceTe /** * If timed poll returns non-null, the returned task is completed - */ + */ public void testPoll2() { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); @@ -148,8 +150,8 @@ public class ExecutorCompletionServiceTe assertNull(ecs.poll()); Callable c = new StringTask(); ecs.submit(c); - Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - if (f != null) + Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); + if (f != null) assertTrue(f.isDone()); } catch (Exception ex) { unexpectedException(); @@ -157,5 +159,76 @@ public class ExecutorCompletionServiceTe joinPool(e); } } + /** + * Submitting to underlying AES that overrides newTaskFor(Callable) + * returns and eventually runs Future returned by newTaskFor. + */ + public void testNewTaskForCallable() { + final AtomicBoolean done = new AtomicBoolean(false); + class MyCallableFuture extends FutureTask { + MyCallableFuture(Callable c) { super(c); } + protected void done() { done.set(true); } + } + ExecutorService e = new ThreadPoolExecutor( + 1, 1, 30L, TimeUnit.SECONDS, + new ArrayBlockingQueue(1)) { + protected RunnableFuture newTaskFor(Callable c) { + return new MyCallableFuture(c); + } + }; + ExecutorCompletionService ecs = + new ExecutorCompletionService(e); + try { + assertNull(ecs.poll()); + Callable c = new StringTask(); + Future f1 = ecs.submit(c); + assertTrue("submit must return MyCallableFuture", + f1 instanceof MyCallableFuture); + Future f2 = ecs.take(); + assertSame("submit and take must return same objects", f1, f2); + assertTrue("completed task must have set done", done.get()); + } catch (Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * Submitting to underlying AES that overrides newTaskFor(Runnable,T) + * returns and eventually runs Future returned by newTaskFor. + */ + public void testNewTaskForRunnable() { + final AtomicBoolean done = new AtomicBoolean(false); + class MyRunnableFuture extends FutureTask { + MyRunnableFuture(Runnable t, V r) { super(t, r); } + protected void done() { done.set(true); } + } + ExecutorService e = new ThreadPoolExecutor( + 1, 1, 30L, TimeUnit.SECONDS, + new ArrayBlockingQueue(1)) { + protected RunnableFuture newTaskFor(Runnable t, T r) { + return new MyRunnableFuture(t, r); + } + }; + ExecutorCompletionService ecs = + new ExecutorCompletionService(e); + try { + assertNull(ecs.poll()); + Runnable r = new NoOpRunnable(); + Future f1 = ecs.submit(r, null); + assertTrue("submit must return MyRunnableFuture", + f1 instanceof MyRunnableFuture); + Future f2 = ecs.take(); + assertSame("submit and take must return same objects", f1, f2); + assertTrue("completed task must have set done", done.get()); + } catch (Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + }