--- jsr166/src/test/tck/FutureTaskTest.java 2013/04/21 06:54:11 1.37 +++ jsr166/src/test/tck/FutureTaskTest.java 2017/08/16 17:18:34 1.47 @@ -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); @@ -100,9 +107,7 @@ public class FutureTaskTest extends JSR1 try { assertSame(expected, f.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertSame(expected, f.get(5L, SECONDS)); + assertSame(expected, f.get(randomTimeout(), randomTimeUnit())); } catch (Throwable fail) { threadUnexpectedException(fail); } } @@ -117,7 +122,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 +132,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 +148,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 +264,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 +281,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 +414,7 @@ public class FutureTaskTest extends JSR1 delay(LONG_DELAY_MS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); Thread t = newStartedThread(task); @@ -478,13 +484,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(); }}); @@ -802,4 +807,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()); + } + } + }