--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2016/08/24 22:22:39 1.27 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2021/01/26 13:33:06 1.30 @@ -38,7 +38,7 @@ public class ExecutorCompletionServiceTe */ public void testConstructorNPE() { try { - new ExecutorCompletionService(null); + new ExecutorCompletionService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -48,7 +48,7 @@ public class ExecutorCompletionServiceTe */ public void testConstructorNPE2() { try { - new ExecutorCompletionService(cachedThreadPool, null); + new ExecutorCompletionService(cachedThreadPool, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -57,9 +57,9 @@ public class ExecutorCompletionServiceTe * ecs.submit(null) throws NullPointerException */ public void testSubmitNullCallable() { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); try { - cs.submit((Callable) null); + cs.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -68,7 +68,7 @@ public class ExecutorCompletionServiceTe * ecs.submit(null, val) throws NullPointerException */ public void testSubmitNullRunnable() { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); try { cs.submit((Runnable) null, Boolean.TRUE); shouldThrow(); @@ -78,11 +78,10 @@ public class ExecutorCompletionServiceTe /** * A taken submitted task is completed */ - public void testTake() - throws InterruptedException, ExecutionException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + public void testTake() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); cs.submit(new StringTask()); - Future f = cs.take(); + Future f = cs.take(); assertTrue(f.isDone()); assertSame(TEST_STRING, f.get()); } @@ -91,23 +90,22 @@ public class ExecutorCompletionServiceTe * Take returns the same future object returned by submit */ public void testTake2() throws InterruptedException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); - Future f1 = cs.submit(new StringTask()); - Future f2 = cs.take(); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + Future f1 = cs.submit(new StringTask()); + Future f2 = cs.take(); assertSame(f1, f2); } /** * poll returns non-null when the returned task is completed */ - public void testPoll1() - throws InterruptedException, ExecutionException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + public void testPoll1() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); assertNull(cs.poll()); cs.submit(new StringTask()); long startTime = System.nanoTime(); - Future f; + Future f; while ((f = cs.poll()) == null) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) fail("timed out"); @@ -120,15 +118,15 @@ public class ExecutorCompletionServiceTe /** * timed poll returns non-null when the returned task is completed */ - public void testPoll2() - throws InterruptedException, ExecutionException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + public void testPoll2() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); assertNull(cs.poll()); cs.submit(new StringTask()); long startTime = System.nanoTime(); - Future f; - while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { + Future f; + while ((f = cs.poll(timeoutMillis(), MILLISECONDS)) == null) { + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); if (millisElapsedSince(startTime) > LONG_DELAY_MS) fail("timed out"); Thread.yield(); @@ -140,12 +138,11 @@ public class ExecutorCompletionServiceTe /** * poll returns null before the returned task is completed */ - public void testPollReturnsNull() - throws InterruptedException, ExecutionException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + public void testPollReturnsNullBeforeCompletion() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); final CountDownLatch proceed = new CountDownLatch(1); - cs.submit(new Callable() { public String call() throws Exception { - proceed.await(); + cs.submit(new Callable() { public String call() throws Exception { + await(proceed); return TEST_STRING; }}); assertNull(cs.poll()); @@ -161,29 +158,28 @@ public class ExecutorCompletionServiceTe /** * successful and failed tasks are both returned */ - public void testTaskAssortment() - throws InterruptedException, ExecutionException { - CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + public void testTaskAssortment() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); ArithmeticException ex = new ArithmeticException(); - for (int i = 0; i < 2; i++) { + final int rounds = 2; + for (int i = rounds; i--> 0; ) { cs.submit(new StringTask()); cs.submit(callableThrowing(ex)); cs.submit(runnableThrowing(ex), null); } int normalCompletions = 0; int exceptionalCompletions = 0; - for (int i = 0; i < 3 * 2; i++) { + for (int i = 3 * rounds; i--> 0; ) { try { - if (cs.take().get() == TEST_STRING) - normalCompletions++; - } - catch (ExecutionException expected) { - assertTrue(expected.getCause() instanceof ArithmeticException); + assertSame(TEST_STRING, cs.take().get()); + normalCompletions++; + } catch (ExecutionException expected) { + assertSame(ex, expected.getCause()); exceptionalCompletions++; } } - assertEquals(2 * 1, normalCompletions); - assertEquals(2 * 2, exceptionalCompletions); + assertEquals(1 * rounds, normalCompletions); + assertEquals(2 * rounds, exceptionalCompletions); assertNull(cs.poll()); } @@ -208,10 +204,10 @@ public class ExecutorCompletionServiceTe try (PoolCleaner cleaner = cleaner(e)) { assertNull(cs.poll()); Callable c = new StringTask(); - Future f1 = cs.submit(c); + Future f1 = cs.submit(c); assertTrue("submit must return MyCallableFuture", f1 instanceof MyCallableFuture); - Future f2 = cs.take(); + Future f2 = cs.take(); assertSame("submit and take must return same objects", f1, f2); assertTrue("completed task must have set done", done.get()); } @@ -238,10 +234,10 @@ public class ExecutorCompletionServiceTe try (PoolCleaner cleaner = cleaner(e)) { assertNull(cs.poll()); Runnable r = new NoOpRunnable(); - Future f1 = cs.submit(r, null); + Future f1 = cs.submit(r, null); assertTrue("submit must return MyRunnableFuture", f1 instanceof MyRunnableFuture); - Future f2 = cs.take(); + Future f2 = cs.take(); assertSame("submit and take must return same objects", f1, f2); assertTrue("completed task must have set done", done.get()); }