--- jsr166/src/test/tck/FutureTaskTest.java 2013/04/21 06:54:11 1.37 +++ jsr166/src/test/tck/FutureTaskTest.java 2019/08/11 22:29:26 1.54 @@ -6,23 +6,30 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class FutureTaskTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(FutureTaskTest.class); @@ -53,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) { @@ -62,6 +70,8 @@ public class FutureTaskTest extends JSR1 } catch (Throwable t) { threadUnexpectedException(t); } + if (exInfo == null) + assertSame(r, r2); assertTrue(f.isDone()); } } @@ -94,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) { @@ -117,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); } @@ -127,7 +138,7 @@ public class FutureTaskTest extends JSR1 pf.set(new Object()); pf.setException(new Error()); for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { - pf.cancel(true); + pf.cancel(mayInterruptIfRunning); } } @@ -143,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()); @@ -259,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()); } @@ -276,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()); } @@ -409,6 +420,7 @@ public class FutureTaskTest extends JSR1 delay(LONG_DELAY_MS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); Thread t = newStartedThread(task); @@ -452,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) @@ -478,13 +490,12 @@ public class FutureTaskTest extends JSR1 final PublicFutureTask task = new PublicFutureTask(new Runnable() { public void run() { + pleaseCancel.countDown(); try { - pleaseCancel.countDown(); delay(LONG_DELAY_MS); - shouldThrow(); - } catch (Throwable t) { - assertTrue(t instanceof InterruptedException); - } + threadShouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable t) { threadUnexpectedException(t); } throw new RuntimeException(); }}); @@ -743,19 +754,23 @@ public class FutureTaskTest extends JSR1 final FutureTask task = new FutureTask(new NoOpCallable()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws Exception { + long startTime = System.nanoTime(); + 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(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); @@ -802,4 +817,72 @@ public class FutureTaskTest extends JSR1 } } + /** + * timed get with most negative timeout works correctly (i.e. no + * underflow bug) + */ + public void testGet_NegativeInfinityTimeout() throws Exception { + final ExecutorService pool = Executors.newFixedThreadPool(10); + final Runnable nop = new Runnable() { public void run() {}}; + final FutureTask task = new FutureTask<>(nop, null); + final List> futures = new ArrayList<>(); + Runnable r = new Runnable() { public void run() { + for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { + try { + task.get(timeout, NANOSECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) {threadUnexpectedException(fail);}}}}; + for (int i = 0; i < 10; i++) + futures.add(pool.submit(r)); + try { + joinPool(pool); + for (Future future : futures) + checkCompletedNormally(future, null); + } finally { + task.run(); // last resort to help terminate + } + } + + /** + * 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()); + } + } + }