--- jsr166/src/test/tck/FutureTaskTest.java 2015/04/25 04:55:30 1.43 +++ jsr166/src/test/tck/FutureTaskTest.java 2021/01/27 01:57:24 1.58 @@ -8,7 +8,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import java.util.ArrayList; import java.util.List; @@ -61,8 +60,9 @@ public class FutureTaskTest extends JSR1 pf.run(); pf.runAndReset(); assertEquals(savedRunCount, pf.runCount()); + Object r2 = null; try { - assertSame(r, f.get()); + r2 = f.get(); } catch (CancellationException t) { assertSame(exInfo, CancellationException.class); } catch (ExecutionException t) { @@ -70,6 +70,8 @@ public class FutureTaskTest extends JSR1 } catch (Throwable t) { threadUnexpectedException(t); } + if (exInfo == null) + assertSame(r, r2); assertTrue(f.isDone()); } } @@ -88,7 +90,7 @@ public class FutureTaskTest extends JSR1 void checkIsRunning(Future f) { checkNotDone(f); if (f instanceof FutureTask) { - FutureTask ft = (FutureTask) f; + FutureTask ft = (FutureTask) f; // Check that run methods do nothing ft.run(); if (f instanceof PublicFutureTask) { @@ -102,16 +104,17 @@ public class FutureTaskTest extends JSR1 } } - void checkCompletedNormally(Future f, T expected) { + void checkCompletedNormally(Future f, T expectedValue) { checkIsDone(f); assertFalse(f.isCancelled()); + T v1 = null, v2 = null; try { - assertSame(expected, f.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertSame(expected, f.get(5L, SECONDS)); + v1 = f.get(); + v2 = f.get(randomTimeout(), randomTimeUnit()); } catch (Throwable fail) { threadUnexpectedException(fail); } + assertSame(expectedValue, v1); + assertSame(expectedValue, v2); } void checkCancelled(Future f) { @@ -125,7 +128,7 @@ public class FutureTaskTest extends JSR1 } catch (Throwable fail) { threadUnexpectedException(fail); } try { - f.get(5L, SECONDS); + f.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -151,7 +154,7 @@ public class FutureTaskTest extends JSR1 } catch (Throwable fail) { threadUnexpectedException(fail); } try { - f.get(5L, SECONDS); + f.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { assertSame(t, success.getCause()); @@ -161,7 +164,7 @@ public class FutureTaskTest extends JSR1 /** * Subclass to expose protected methods */ - static class PublicFutureTask extends FutureTask { + static class PublicFutureTask extends FutureTask { private final AtomicInteger runCount; private final AtomicInteger doneCount = new AtomicInteger(0); private final AtomicInteger runAndResetCount = new AtomicInteger(0); @@ -188,12 +191,12 @@ public class FutureTaskTest extends JSR1 }}, result); this.runCount = runCount; } - PublicFutureTask(Callable callable) { + PublicFutureTask(Callable callable) { this(callable, new AtomicInteger(0)); } - private PublicFutureTask(final Callable callable, + private PublicFutureTask(final Callable callable, final AtomicInteger runCount) { - super(new Callable() { + super(new Callable() { public Object call() throws Exception { runCount.getAndIncrement(); return callable.call(); @@ -232,7 +235,7 @@ public class FutureTaskTest extends JSR1 */ public void testConstructor() { try { - new FutureTask(null); + new FutureTask(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -242,7 +245,7 @@ public class FutureTaskTest extends JSR1 */ public void testConstructor2() { try { - new FutureTask(null, Boolean.TRUE); + new FutureTask(null, Boolean.TRUE); shouldThrow(); } catch (NullPointerException success) {} } @@ -267,8 +270,8 @@ public class FutureTaskTest extends JSR1 for (int i = 0; i < 3; i++) { assertTrue(task.runAndReset()); checkNotDone(task); - assertEquals(i+1, task.runCount()); - assertEquals(i+1, task.runAndResetCount()); + assertEquals(i + 1, task.runCount()); + assertEquals(i + 1, task.runAndResetCount()); assertEquals(0, task.setCount()); assertEquals(0, task.setExceptionCount()); } @@ -284,7 +287,7 @@ public class FutureTaskTest extends JSR1 for (int i = 0; i < 3; i++) { assertFalse(task.runAndReset()); assertEquals(0, task.runCount()); - assertEquals(i+1, task.runAndResetCount()); + assertEquals(i + 1, task.runAndResetCount()); assertEquals(0, task.setCount()); assertEquals(0, task.setExceptionCount()); } @@ -417,6 +420,7 @@ public class FutureTaskTest extends JSR1 delay(LONG_DELAY_MS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); Thread t = newStartedThread(task); @@ -460,7 +464,7 @@ public class FutureTaskTest extends JSR1 try { task.cancel(true); shouldThrow(); - } catch (SecurityException expected) {} + } catch (SecurityException success) {} // We failed to deliver the interrupt, but the world retains // its sanity, as if we had done task.cancel(false) @@ -625,8 +629,7 @@ public class FutureTaskTest extends JSR1 public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { final CountDownLatch pleaseCancel = new CountDownLatch(3); final CountDownLatch cancelled = new CountDownLatch(1); - final Callable callable = - new CheckedCallable() { + final Callable callable = new CheckedCallable<>() { public Object realCall() throws InterruptedException { pleaseCancel.countDown(); if (mayInterruptIfRunning) { @@ -673,7 +676,7 @@ public class FutureTaskTest extends JSR1 */ public void testGet_ExecutionException() throws InterruptedException { final ArithmeticException e = new ArithmeticException(); - final PublicFutureTask task = new PublicFutureTask(new Callable() { + final PublicFutureTask task = new PublicFutureTask(new Callable() { public Object call() { throw e; }}); @@ -697,7 +700,7 @@ public class FutureTaskTest extends JSR1 */ public void testTimedGet_ExecutionException2() throws Exception { final ArithmeticException e = new ArithmeticException(); - final PublicFutureTask task = new PublicFutureTask(new Callable() { + final PublicFutureTask task = new PublicFutureTask(new Callable() { public Object call() { throw e; }}); @@ -716,9 +719,9 @@ public class FutureTaskTest extends JSR1 /** * get is interruptible */ - public void testGet_interruptible() { + public void testGet_Interruptible() { final CountDownLatch pleaseInterrupt = new CountDownLatch(1); - final FutureTask task = new FutureTask(new NoOpCallable()); + final FutureTask task = new FutureTask<>(new NoOpCallable()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws Exception { Thread.currentThread().interrupt(); @@ -745,27 +748,28 @@ public class FutureTaskTest extends JSR1 /** * timed get is interruptible */ - public void testTimedGet_interruptible() { + public void testTimedGet_Interruptible() { final CountDownLatch pleaseInterrupt = new CountDownLatch(1); - final FutureTask task = new FutureTask(new NoOpCallable()); + final FutureTask task = new FutureTask<>(new NoOpCallable()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws Exception { Thread.currentThread().interrupt(); try { - task.get(2*LONG_DELAY_MS, MILLISECONDS); + task.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - task.get(2*LONG_DELAY_MS, MILLISECONDS); + task.get(LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkNotDone(task); @@ -775,7 +779,7 @@ public class FutureTaskTest extends JSR1 * A timed out timed get throws TimeoutException */ public void testGet_TimeoutException() throws Exception { - FutureTask task = new FutureTask(new NoOpCallable()); + FutureTask task = new FutureTask<>(new NoOpCallable()); long startTime = System.nanoTime(); try { task.get(timeoutMillis(), MILLISECONDS); @@ -789,7 +793,7 @@ public class FutureTaskTest extends JSR1 * timed get with null TimeUnit throws NullPointerException */ public void testGet_NullTimeUnit() throws Exception { - FutureTask task = new FutureTask(new NoOpCallable()); + FutureTask task = new FutureTask<>(new NoOpCallable()); long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; for (long timeout : timeouts) { @@ -836,4 +840,45 @@ public class FutureTaskTest extends JSR1 } } + /** + * toString indicates current completion state + */ + public void testToString_incomplete() { + FutureTask f = new FutureTask<>(() -> ""); + assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); + if (testImplementationDetails) + assertTrue(f.toString().startsWith( + identityString(f) + "[Not completed, task =")); + } + + public void testToString_normal() { + FutureTask f = new FutureTask<>(() -> ""); + f.run(); + assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); + if (testImplementationDetails) + assertEquals(identityString(f) + "[Completed normally]", + f.toString()); + } + + public void testToString_exception() { + FutureTask f = new FutureTask<>( + () -> { throw new ArithmeticException(); }); + f.run(); + assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); + if (testImplementationDetails) + assertTrue(f.toString().startsWith( + identityString(f) + "[Completed exceptionally: ")); + } + + public void testToString_cancelled() { + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { + FutureTask f = new FutureTask<>(() -> ""); + assertTrue(f.cancel(mayInterruptIfRunning)); + assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); + if (testImplementationDetails) + assertEquals(identityString(f) + "[Cancelled]", + f.toString()); + } + } + }