--- jsr166/src/test/tck/JSR166TestCase.java 2015/10/09 01:26:36 1.172
+++ jsr166/src/test/tck/JSR166TestCase.java 2016/05/23 18:19:48 1.193
@@ -6,6 +6,14 @@
* Pat Fisher, Mike Judd.
*/
+/*
+ * @test
+ * @summary JSR-166 tck tests
+ * @modules java.management
+ * @build *
+ * @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase
+ */
+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
@@ -50,9 +58,11 @@ import java.util.concurrent.RecursiveAct
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.Semaphore;
+import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -112,8 +122,7 @@ import junit.framework.TestSuite;
* methods as there are exceptions the method can throw. Sometimes
* there are multiple tests per JSR166 method when the different
* "normal" behaviors differ significantly. And sometimes testcases
- * cover multiple methods when they cannot be tested in
- * isolation.
+ * cover multiple methods when they cannot be tested in isolation.
*
*
The documentation style for testcases is to provide as javadoc
* a simple sentence or two describing the property that the testcase
@@ -176,6 +185,44 @@ public class JSR166TestCase extends Test
private static final int suiteRuns =
Integer.getInteger("jsr166.suiteRuns", 1);
+ /**
+ * Returns the value of the system property, or NaN if not defined.
+ */
+ private static float systemPropertyValue(String name) {
+ String floatString = System.getProperty(name);
+ if (floatString == null)
+ return Float.NaN;
+ try {
+ return Float.parseFloat(floatString);
+ } catch (NumberFormatException ex) {
+ throw new IllegalArgumentException(
+ String.format("Bad float value in system property %s=%s",
+ name, floatString));
+ }
+ }
+
+ /**
+ * The scaling factor to apply to standard delays used in tests.
+ * May be initialized from any of:
+ * - the "jsr166.delay.factor" system property
+ * - the "test.timeout.factor" system property (as used by jtreg)
+ * See: http://openjdk.java.net/jtreg/tag-spec.html
+ * - hard-coded fuzz factor when using a known slowpoke VM
+ */
+ private static final float delayFactor = delayFactor();
+
+ private static float delayFactor() {
+ float x;
+ if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
+ return x;
+ if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
+ return x;
+ String prop = System.getProperty("java.vm.version");
+ if (prop != null && prop.matches(".*debug.*"))
+ return 4.0f; // How much slower is fastdebug than product?!
+ return 1.0f;
+ }
+
public JSR166TestCase() { super(); }
public JSR166TestCase(String name) { super(name); }
@@ -193,21 +240,27 @@ public class JSR166TestCase extends Test
// Instrumentation to debug very rare, but very annoying hung test runs.
static volatile TestCase currentTestCase;
- static volatile int currentRun = 0;
+ // static volatile int currentRun = 0;
static {
Runnable checkForWedgedTest = new Runnable() { public void run() {
- // avoid spurious reports with enormous runsPerTest
- final int timeoutMinutes = Math.max(runsPerTest / 10, 1);
+ // Avoid spurious reports with enormous runsPerTest.
+ // A single test case run should never take more than 1 second.
+ // But let's cap it at the high end too ...
+ final int timeoutMinutes =
+ Math.min(15, Math.max(runsPerTest / 60, 1));
for (TestCase lastTestCase = currentTestCase;;) {
try { MINUTES.sleep(timeoutMinutes); }
catch (InterruptedException unexpected) { break; }
if (lastTestCase == currentTestCase) {
System.err.printf(
- "Looks like we're stuck running test: %s (%d/%d)%n",
- lastTestCase, currentRun, runsPerTest);
- System.err.println("availableProcessors=" +
- Runtime.getRuntime().availableProcessors());
- System.err.printf("cpu model = %s%n", cpuModel());
+ "Looks like we're stuck running test: %s%n",
+ lastTestCase);
+// System.err.printf(
+// "Looks like we're stuck running test: %s (%d/%d)%n",
+// lastTestCase, currentRun, runsPerTest);
+// System.err.println("availableProcessors=" +
+// Runtime.getRuntime().availableProcessors());
+// System.err.printf("cpu model = %s%n", cpuModel());
dumpTestThreads();
// one stack dump is probably enough; more would be spam
break;
@@ -219,15 +272,15 @@ public class JSR166TestCase extends Test
thread.start();
}
- public static String cpuModel() {
- try {
- Matcher matcher = Pattern.compile("model name\\s*: (.*)")
- .matcher(new String(
- Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
- matcher.find();
- return matcher.group(1);
- } catch (Exception ex) { return null; }
- }
+// public static String cpuModel() {
+// try {
+// Matcher matcher = Pattern.compile("model name\\s*: (.*)")
+// .matcher(new String(
+// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
+// matcher.find();
+// return matcher.group(1);
+// } catch (Exception ex) { return null; }
+// }
public void runBare() throws Throwable {
currentTestCase = this;
@@ -238,7 +291,7 @@ public class JSR166TestCase extends Test
protected void runTest() throws Throwable {
for (int i = 0; i < runsPerTest; i++) {
- currentRun = i;
+ // currentRun = i;
if (profileTests)
runTestProfiled();
else
@@ -267,6 +320,34 @@ public class JSR166TestCase extends Test
main(suite(), args);
}
+ static class PithyResultPrinter extends junit.textui.ResultPrinter {
+ PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
+ long runTime;
+ public void startTest(Test test) {}
+ protected void printHeader(long runTime) {
+ this.runTime = runTime; // defer printing for later
+ }
+ protected void printFooter(TestResult result) {
+ if (result.wasSuccessful()) {
+ getWriter().println("OK (" + result.runCount() + " tests)"
+ + " Time: " + elapsedTimeAsString(runTime));
+ } else {
+ getWriter().println("Time: " + elapsedTimeAsString(runTime));
+ super.printFooter(result);
+ }
+ }
+ }
+
+ /**
+ * Returns a TestRunner that doesn't bother with unnecessary
+ * fluff, like printing a "." for each test case.
+ */
+ static junit.textui.TestRunner newPithyTestRunner() {
+ junit.textui.TestRunner runner = new junit.textui.TestRunner();
+ runner.setPrinter(new PithyResultPrinter(System.out));
+ return runner;
+ }
+
/**
* Runs all unit tests in the given test suite.
* Actual behavior influenced by jsr166.* system properties.
@@ -278,7 +359,7 @@ public class JSR166TestCase extends Test
System.setSecurityManager(new SecurityManager());
}
for (int i = 0; i < suiteRuns; i++) {
- TestResult result = junit.textui.TestRunner.run(suite);
+ TestResult result = newPithyTestRunner().doRun(suite);
if (!result.wasSuccessful())
System.exit(1);
System.gc();
@@ -431,6 +512,7 @@ public class JSR166TestCase extends Test
"StampedLockTest",
"SubmissionPublisherTest",
"ThreadLocalRandom8Test",
+ "TimeUnit8Test",
};
addNamedTestClasses(suite, java8TestClassNames);
}
@@ -438,7 +520,7 @@ public class JSR166TestCase extends Test
// Java9+ test classes
if (atLeastJava9()) {
String[] java9TestClassNames = {
- // Currently empty, but expecting varhandle tests
+ "ExecutorCompletionService9Test",
};
addNamedTestClasses(suite, java9TestClassNames);
}
@@ -515,11 +597,13 @@ public class JSR166TestCase extends Test
public static long LONG_DELAY_MS;
/**
- * Returns the shortest timed delay. This could
- * be reimplemented to use for example a Property.
+ * Returns the shortest timed delay. This can be scaled up for
+ * slow machines using the jsr166.delay.factor system property,
+ * or via jtreg's -timeoutFactor: flag.
+ * http://openjdk.java.net/jtreg/command-help.html
*/
protected long getShortDelay() {
- return 50;
+ return (long) (50 * delayFactor);
}
/**
@@ -832,6 +916,14 @@ public class JSR166TestCase extends Test
}};
}
+ PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
+ return new PoolCleanerWithReleaser(pool, releaser(flag));
+ }
+
+ Runnable releaser(final AtomicBoolean flag) {
+ return new Runnable() { public void run() { flag.set(true); }};
+ }
+
/**
* Waits out termination of a thread pool or fails doing so.
*/
@@ -1222,7 +1314,7 @@ public class JSR166TestCase extends Test
} finally {
if (t.getState() != Thread.State.TERMINATED) {
t.interrupt();
- threadFail("Test timed out");
+ threadFail("timed out waiting for thread to terminate");
}
}
}
@@ -1388,17 +1480,25 @@ public class JSR166TestCase extends Test
return new LatchAwaiter(latch);
}
- public void await(CountDownLatch latch) {
+ public void await(CountDownLatch latch, long timeoutMillis) {
try {
- assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
+ if (!latch.await(timeoutMillis, MILLISECONDS))
+ fail("timed out waiting for CountDownLatch for "
+ + (timeoutMillis/1000) + " sec");
} catch (Throwable fail) {
threadUnexpectedException(fail);
}
}
+ public void await(CountDownLatch latch) {
+ await(latch, LONG_DELAY_MS);
+ }
+
public void await(Semaphore semaphore) {
try {
- assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
+ if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
+ fail("timed out waiting for Semaphore for "
+ + (LONG_DELAY_MS/1000) + " sec");
} catch (Throwable fail) {
threadUnexpectedException(fail);
}
@@ -1734,4 +1834,19 @@ public class JSR166TestCase extends Test
} catch (NoSuchElementException success) {}
assertFalse(it.hasNext());
}
+
+ public Callable callableThrowing(final Exception ex) {
+ return new Callable() { public T call() throws Exception { throw ex; }};
+ }
+
+ public Runnable runnableThrowing(final RuntimeException ex) {
+ return new Runnable() { public void run() { throw ex; }};
+ }
+
+ /** A reusable thread pool to be shared by tests. */
+ static final ExecutorService cachedThreadPool =
+ new ThreadPoolExecutor(0, Integer.MAX_VALUE,
+ 1000L, MILLISECONDS,
+ new SynchronousQueue());
+
}