--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2009/11/21 02:33:20 1.9 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2011/05/31 16:16:23 1.18 @@ -1,29 +1,35 @@ /* * 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.ArrayBlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorCompletionService; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.concurrent.atomic.*; -import java.math.BigInteger; +import java.util.concurrent.atomic.AtomicBoolean; import java.security.*; 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); } - /** * Creating a new ECS with null Executor throw NPE */ @@ -31,8 +37,7 @@ public class ExecutorCompletionServiceTe try { ExecutorCompletionService ecs = new ExecutorCompletionService(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -43,8 +48,7 @@ public class ExecutorCompletionServiceTe ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -82,7 +86,7 @@ public class ExecutorCompletionServiceTe /** * A taken submitted task is completed */ - public void testTake() { + public void testTake() throws InterruptedException { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); try { @@ -90,8 +94,6 @@ public class ExecutorCompletionServiceTe ecs.submit(c); Future f = ecs.take(); assertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -100,7 +102,7 @@ public class ExecutorCompletionServiceTe /** * Take returns the same future object returned by submit */ - public void testTake2() { + public void testTake2() throws InterruptedException { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); try { @@ -108,8 +110,6 @@ public class ExecutorCompletionServiceTe Future f1 = ecs.submit(c); Future f2 = ecs.take(); assertSame(f1, f2); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -118,23 +118,23 @@ public class ExecutorCompletionServiceTe /** * If poll returns non-null, the returned task is completed */ - public void testPoll1() { + public void testPoll1() throws Exception { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); try { assertNull(ecs.poll()); Callable c = new StringTask(); ecs.submit(c); - Thread.sleep(SHORT_DELAY_MS); - for (;;) { - Future f = ecs.poll(); - if (f != null) { - assertTrue(f.isDone()); - break; - } + + long startTime = System.nanoTime(); + Future f; + while ((f = ecs.poll()) == null) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out"); + Thread.yield(); } - } catch (Exception ex) { - unexpectedException(); + assertTrue(f.isDone()); + assertSame(TEST_STRING, f.get()); } finally { joinPool(e); } @@ -143,7 +143,7 @@ public class ExecutorCompletionServiceTe /** * If timed poll returns non-null, the returned task is completed */ - public void testPoll2() { + public void testPoll2() throws InterruptedException { ExecutorService e = Executors.newCachedThreadPool(); ExecutorCompletionService ecs = new ExecutorCompletionService(e); try { @@ -153,82 +153,73 @@ public class ExecutorCompletionServiceTe Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); if (f != null) 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( + + /** + * Submitting to underlying AES that overrides newTaskFor(Callable) + * returns and eventually runs Future returned by newTaskFor. + */ + public void testNewTaskForCallable() throws InterruptedException { + 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( + 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()); + } finally { + joinPool(e); + } + } + + /** + * Submitting to underlying AES that overrides newTaskFor(Runnable,T) + * returns and eventually runs Future returned by newTaskFor. + */ + public void testNewTaskForRunnable() throws InterruptedException { + 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); - } - } - - + 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()); + } finally { + joinPool(e); + } + } }