--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2003/12/23 19:40:24 1.1 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2005/08/01 19:54:04 1.5 @@ -1,14 +1,16 @@ /* - * 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 java.util.concurrent.atomic.*; import java.math.BigInteger; import java.security.*; @@ -86,7 +88,7 @@ public class ExecutorCompletionServiceTe Callable c = new StringTask(); ecs.submit(c); Future f = ecs.take(); - assert(f.isDone()); + assertTrue(f.isDone()); } catch (Exception ex) { unexpectedException(); } finally { @@ -126,7 +128,7 @@ public class ExecutorCompletionServiceTe for (;;) { Future f = ecs.poll(); if (f != null) { - assert(f.isDone()); + assertTrue(f.isDone()); break; } } @@ -149,12 +151,83 @@ public class ExecutorCompletionServiceTe ecs.submit(c); Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); if (f != null) - assert(f.isDone()); + assertTrue(f.isDone()); } catch (Exception ex) { unexpectedException(); } finally { 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()); + Callable c = new StringTask(); + Future f1 = ecs.submit(c); + 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); + } + } + + }