--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2014/12/31 19:05:42 1.19 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2016/05/03 23:06:12 1.23 @@ -10,6 +10,7 @@ import static java.util.concurrent.TimeU import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; +import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; @@ -25,7 +26,7 @@ import junit.framework.TestSuite; public class ExecutorCompletionServiceTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ExecutorCompletionServiceTest.class); @@ -36,7 +37,7 @@ public class ExecutorCompletionServiceTe */ public void testConstructorNPE() { try { - ExecutorCompletionService ecs = new ExecutorCompletionService(null); + new ExecutorCompletionService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -47,7 +48,7 @@ public class ExecutorCompletionServiceTe public void testConstructorNPE2() { try { ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); + new ExecutorCompletionService(e, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -56,15 +57,14 @@ public class ExecutorCompletionServiceTe * Submitting a null callable throws NPE */ public void testSubmitNPE() { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { Callable c = null; - ecs.submit(c); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try { + ecs.submit(c); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -72,15 +72,14 @@ public class ExecutorCompletionServiceTe * Submitting a null runnable throws NPE */ public void testSubmitNPE2() { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { Runnable r = null; - ecs.submit(r, Boolean.TRUE); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try { + ecs.submit(r, Boolean.TRUE); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -88,15 +87,13 @@ public class ExecutorCompletionServiceTe * A taken submitted task is completed */ public void testTake() throws InterruptedException { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { Callable c = new StringTask(); ecs.submit(c); Future f = ecs.take(); assertTrue(f.isDone()); - } finally { - joinPool(e); } } @@ -104,15 +101,13 @@ public class ExecutorCompletionServiceTe * Take returns the same future object returned by submit */ public void testTake2() throws InterruptedException { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { Callable c = new StringTask(); Future f1 = ecs.submit(c); Future f2 = ecs.take(); assertSame(f1, f2); - } finally { - joinPool(e); } } @@ -120,9 +115,9 @@ public class ExecutorCompletionServiceTe * If poll returns non-null, the returned task is completed */ public void testPoll1() throws Exception { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { assertNull(ecs.poll()); Callable c = new StringTask(); ecs.submit(c); @@ -136,8 +131,6 @@ public class ExecutorCompletionServiceTe } assertTrue(f.isDone()); assertSame(TEST_STRING, f.get()); - } finally { - joinPool(e); } } @@ -145,17 +138,15 @@ public class ExecutorCompletionServiceTe * If timed poll returns non-null, the returned task is completed */ public void testPoll2() throws InterruptedException { - ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e); - try { + final ExecutorService e = Executors.newCachedThreadPool(); + final ExecutorCompletionService ecs = new ExecutorCompletionService(e); + try (PoolCleaner cleaner = cleaner(e)) { assertNull(ecs.poll()); Callable c = new StringTask(); ecs.submit(c); Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); if (f != null) assertTrue(f.isDone()); - } finally { - joinPool(e); } } @@ -169,15 +160,16 @@ public class ExecutorCompletionServiceTe 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 { + final ExecutorService e = + new ThreadPoolExecutor(1, 1, + 30L, TimeUnit.SECONDS, + new ArrayBlockingQueue(1)) { + protected RunnableFuture newTaskFor(Callable c) { + return new MyCallableFuture(c); + }}; + CompletionService ecs = + new ExecutorCompletionService<>(e); + try (PoolCleaner cleaner = cleaner(e)) { assertNull(ecs.poll()); Callable c = new StringTask(); Future f1 = ecs.submit(c); @@ -186,8 +178,6 @@ public class ExecutorCompletionServiceTe 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); } } @@ -201,15 +191,16 @@ public class ExecutorCompletionServiceTe 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 { + final ExecutorService e = + new ThreadPoolExecutor(1, 1, + 30L, TimeUnit.SECONDS, + new ArrayBlockingQueue(1)) { + protected RunnableFuture newTaskFor(Runnable t, T r) { + return new MyRunnableFuture(t, r); + }}; + final CompletionService ecs = + new ExecutorCompletionService<>(e); + try (PoolCleaner cleaner = cleaner(e)) { assertNull(ecs.poll()); Runnable r = new NoOpRunnable(); Future f1 = ecs.submit(r, null); @@ -218,8 +209,6 @@ public class ExecutorCompletionServiceTe 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); } }