--- jsr166/src/test/tck/JSR166TestCase.java 2014/06/08 00:15:50 1.116 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/02/27 22:10:29 1.129 @@ -6,7 +6,9 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; @@ -14,26 +16,41 @@ import java.io.ObjectOutputStream; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.reflect.Method; +import java.security.CodeSource; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.security.SecurityPermission; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Enumeration; +import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.PropertyPermission; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.RecursiveTask; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; import java.util.regex.Pattern; -import java.security.CodeSource; -import java.security.Permission; -import java.security.PermissionCollection; -import java.security.Permissions; -import java.security.Policy; -import java.security.ProtectionDomain; -import java.security.SecurityPermission; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; /** * Base class for JSR166 Junit TCK tests. Defines some constants, @@ -116,6 +133,13 @@ public class JSR166TestCase extends Test Boolean.getBoolean("jsr166.expensiveTests"); /** + * If true, also run tests that are not part of the official tck + * because they test unspecified implementation details. + */ + protected static final boolean testImplementationDetails = + Boolean.getBoolean("jsr166.testImplementationDetails"); + + /** * If true, report on stdout all "slow" tests, that is, ones that * take more than profileThreshold milliseconds to execute. */ @@ -442,7 +466,7 @@ public class JSR166TestCase extends Test } /** - * Find missing try { ... } finally { joinPool(e); } + * Finds missing try { ... } finally { joinPool(e); } */ void checkForkJoinPoolThreadLeaks() throws InterruptedException { Thread[] survivors = new Thread[5]; @@ -540,11 +564,11 @@ public class JSR166TestCase extends Test public void threadAssertEquals(Object x, Object y) { try { assertEquals(x, y); - } catch (AssertionFailedError t) { - threadRecordFailure(t); - throw t; - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (AssertionFailedError fail) { + threadRecordFailure(fail); + throw fail; + } catch (Throwable fail) { + threadUnexpectedException(fail); } } @@ -556,9 +580,9 @@ public class JSR166TestCase extends Test public void threadAssertSame(Object x, Object y) { try { assertSame(x, y); - } catch (AssertionFailedError t) { - threadRecordFailure(t); - throw t; + } catch (AssertionFailedError fail) { + threadRecordFailure(fail); + throw fail; } } @@ -623,11 +647,12 @@ public class JSR166TestCase extends Test void joinPool(ExecutorService exec) { try { exec.shutdown(); - assertTrue("ExecutorService did not terminate in a timely manner", - exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); + if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) + fail("ExecutorService " + exec + + " did not terminate in a timely manner"); } catch (SecurityException ok) { // Allowed in case test doesn't have privs - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } @@ -658,7 +683,7 @@ public class JSR166TestCase extends Test // No need to optimize the failing case via Thread.join. delay(millis); assertTrue(thread.isAlive()); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } @@ -680,7 +705,7 @@ public class JSR166TestCase extends Test delay(millis); for (Thread thread : threads) assertTrue(thread.isAlive()); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } @@ -702,8 +727,8 @@ public class JSR166TestCase extends Test future.get(timeoutMillis, MILLISECONDS); shouldThrow(); } catch (TimeoutException success) { - } catch (Exception e) { - threadUnexpectedException(e); + } catch (Exception fail) { + threadUnexpectedException(fail); } finally { future.cancel(true); } assertTrue(millisElapsedSince(startTime) >= timeoutMillis); } @@ -859,10 +884,10 @@ public class JSR166TestCase extends Test void sleep(long millis) { try { delay(millis); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); - afe.initCause(ie); + afe.initCause(fail); throw afe; } } @@ -900,12 +925,42 @@ public class JSR166TestCase extends Test /** * Returns the number of milliseconds since time given by * startNanoTime, which must have been previously returned from a - * call to {@link System.nanoTime()}. + * call to {@link System#nanoTime()}. */ - long millisElapsedSince(long startNanoTime) { + static long millisElapsedSince(long startNanoTime) { return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); } +// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { +// long startTime = System.nanoTime(); +// try { +// r.run(); +// } catch (Throwable fail) { threadUnexpectedException(fail); } +// if (millisElapsedSince(startTime) > timeoutMillis/2) +// throw new AssertionFailedError("did not return promptly"); +// } + +// void assertTerminatesPromptly(Runnable r) { +// assertTerminatesPromptly(LONG_DELAY_MS/2, r); +// } + + /** + * Checks that timed f.get() returns the expected value, and does not + * wait for the timeout to elapse before returning. + */ + void checkTimedGet(Future f, T expectedValue, long timeoutMillis) { + long startTime = System.nanoTime(); + try { + assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + if (millisElapsedSince(startTime) > timeoutMillis/2) + throw new AssertionFailedError("timed get did not return promptly"); + } + + void checkTimedGet(Future f, T expectedValue) { + checkTimedGet(f, expectedValue, LONG_DELAY_MS); + } + /** * Returns a new started daemon Thread running the given runnable. */ @@ -924,8 +979,8 @@ public class JSR166TestCase extends Test void awaitTermination(Thread t, long timeoutMillis) { try { t.join(timeoutMillis); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); + } catch (InterruptedException fail) { + threadUnexpectedException(fail); } finally { if (t.getState() != Thread.State.TERMINATED) { t.interrupt(); @@ -951,8 +1006,8 @@ public class JSR166TestCase extends Test public final void run() { try { realRun(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -1006,8 +1061,8 @@ public class JSR166TestCase extends Test threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { threadAssertFalse(Thread.interrupted()); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -1018,8 +1073,8 @@ public class JSR166TestCase extends Test public final T call() { try { return realCall(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -1036,8 +1091,8 @@ public class JSR166TestCase extends Test return result; } catch (InterruptedException success) { threadAssertFalse(Thread.interrupted()); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } return null; } @@ -1077,16 +1132,16 @@ public class JSR166TestCase extends Test public void await(CountDownLatch latch) { try { assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } public void await(Semaphore semaphore) { try { assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } @@ -1280,8 +1335,8 @@ public class JSR166TestCase extends Test @Override protected final void compute() { try { realCompute(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -1295,8 +1350,8 @@ public class JSR166TestCase extends Test @Override protected final T compute() { try { return realCompute(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -1320,12 +1375,12 @@ public class JSR166TestCase extends Test public int await() { try { return super.await(2 * LONG_DELAY_MS, MILLISECONDS); - } catch (TimeoutException e) { + } catch (TimeoutException timedOut) { throw new AssertionFailedError("timed out"); - } catch (Exception e) { + } catch (Exception fail) { AssertionFailedError afe = - new AssertionFailedError("Unexpected exception: " + e); - afe.initCause(e); + new AssertionFailedError("Unexpected exception: " + fail); + afe.initCause(fail); throw afe; } } @@ -1353,9 +1408,7 @@ public class JSR166TestCase extends Test q.remove(); shouldThrow(); } catch (NoSuchElementException success) {} - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } void assertSerialEquals(Object x, Object y) { @@ -1374,8 +1427,8 @@ public class JSR166TestCase extends Test oos.flush(); oos.close(); return bos.toByteArray(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return new byte[0]; } } @@ -1388,8 +1441,8 @@ public class JSR166TestCase extends Test T clone = (T) ois.readObject(); assertSame(o.getClass(), clone.getClass()); return clone; - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -1414,4 +1467,12 @@ public class JSR166TestCase extends Test shouldThrow(expectedExceptionClass.getName()); } } + + public void assertIteratorExhausted(Iterator it) { + try { + it.next(); + shouldThrow(); + } catch (NoSuchElementException success) {} + assertFalse(it.hasNext()); + } }