6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
/* |
10 |
+ |
* @test |
11 |
+ |
* @summary JSR-166 tck tests |
12 |
+ |
* @modules java.management |
13 |
+ |
* @build * |
14 |
+ |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
15 |
+ |
*/ |
16 |
+ |
|
17 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
19 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
28 |
|
import java.lang.reflect.Constructor; |
29 |
|
import java.lang.reflect.Method; |
30 |
|
import java.lang.reflect.Modifier; |
31 |
+ |
import java.nio.file.Files; |
32 |
+ |
import java.nio.file.Paths; |
33 |
|
import java.security.CodeSource; |
34 |
|
import java.security.Permission; |
35 |
|
import java.security.PermissionCollection; |
61 |
|
import java.util.concurrent.ThreadFactory; |
62 |
|
import java.util.concurrent.ThreadPoolExecutor; |
63 |
|
import java.util.concurrent.TimeoutException; |
64 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
65 |
|
import java.util.concurrent.atomic.AtomicReference; |
66 |
+ |
import java.util.regex.Matcher; |
67 |
|
import java.util.regex.Pattern; |
68 |
|
|
69 |
|
import junit.framework.AssertionFailedError; |
121 |
|
* methods as there are exceptions the method can throw. Sometimes |
122 |
|
* there are multiple tests per JSR166 method when the different |
123 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
124 |
< |
* cover multiple methods when they cannot be tested in |
113 |
< |
* isolation. |
124 |
> |
* cover multiple methods when they cannot be tested in isolation. |
125 |
|
* |
126 |
|
* <li>The documentation style for testcases is to provide as javadoc |
127 |
|
* a simple sentence or two describing the property that the testcase |
184 |
|
private static final int suiteRuns = |
185 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
186 |
|
|
187 |
+ |
private static float systemPropertyValue(String name, float defaultValue) { |
188 |
+ |
String floatString = System.getProperty(name); |
189 |
+ |
if (floatString == null) |
190 |
+ |
return defaultValue; |
191 |
+ |
try { |
192 |
+ |
return Float.parseFloat(floatString); |
193 |
+ |
} catch (NumberFormatException ex) { |
194 |
+ |
throw new IllegalArgumentException( |
195 |
+ |
String.format("Bad float value in system property %s=%s", |
196 |
+ |
name, floatString)); |
197 |
+ |
} |
198 |
+ |
} |
199 |
+ |
|
200 |
+ |
/** |
201 |
+ |
* The scaling factor to apply to standard delays used in tests. |
202 |
+ |
*/ |
203 |
+ |
private static final float delayFactor = |
204 |
+ |
systemPropertyValue("jsr166.delay.factor", 1.0f); |
205 |
+ |
|
206 |
+ |
/** |
207 |
+ |
* The timeout factor as used in the jtreg test harness. |
208 |
+ |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
209 |
+ |
*/ |
210 |
+ |
private static final float jtregTestTimeoutFactor |
211 |
+ |
= systemPropertyValue("test.timeout.factor", 1.0f); |
212 |
+ |
|
213 |
|
public JSR166TestCase() { super(); } |
214 |
|
public JSR166TestCase(String name) { super(name); } |
215 |
|
|
225 |
|
return (regex == null) ? null : Pattern.compile(regex); |
226 |
|
} |
227 |
|
|
228 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
229 |
|
static volatile TestCase currentTestCase; |
230 |
+ |
// static volatile int currentRun = 0; |
231 |
|
static { |
232 |
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
233 |
< |
// avoid spurious reports with enormous runsPerTest |
234 |
< |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
233 |
> |
// Avoid spurious reports with enormous runsPerTest. |
234 |
> |
// A single test case run should never take more than 1 second. |
235 |
> |
// But let's cap it at the high end too ... |
236 |
> |
final int timeoutMinutes = |
237 |
> |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
238 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
239 |
|
try { MINUTES.sleep(timeoutMinutes); } |
240 |
|
catch (InterruptedException unexpected) { break; } |
241 |
|
if (lastTestCase == currentTestCase) { |
242 |
< |
System.err.println |
243 |
< |
("Looks like we're stuck running test: " |
244 |
< |
+ lastTestCase); |
242 |
> |
System.err.printf( |
243 |
> |
"Looks like we're stuck running test: %s%n", |
244 |
> |
lastTestCase); |
245 |
> |
// System.err.printf( |
246 |
> |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
247 |
> |
// lastTestCase, currentRun, runsPerTest); |
248 |
> |
// System.err.println("availableProcessors=" + |
249 |
> |
// Runtime.getRuntime().availableProcessors()); |
250 |
> |
// System.err.printf("cpu model = %s%n", cpuModel()); |
251 |
|
dumpTestThreads(); |
252 |
|
// one stack dump is probably enough; more would be spam |
253 |
|
break; |
259 |
|
thread.start(); |
260 |
|
} |
261 |
|
|
262 |
+ |
// public static String cpuModel() { |
263 |
+ |
// try { |
264 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
265 |
+ |
// .matcher(new String( |
266 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
267 |
+ |
// matcher.find(); |
268 |
+ |
// return matcher.group(1); |
269 |
+ |
// } catch (Exception ex) { return null; } |
270 |
+ |
// } |
271 |
+ |
|
272 |
|
public void runBare() throws Throwable { |
273 |
|
currentTestCase = this; |
274 |
|
if (methodFilter == null |
278 |
|
|
279 |
|
protected void runTest() throws Throwable { |
280 |
|
for (int i = 0; i < runsPerTest; i++) { |
281 |
+ |
// currentRun = i; |
282 |
|
if (profileTests) |
283 |
|
runTestProfiled(); |
284 |
|
else |
307 |
|
main(suite(), args); |
308 |
|
} |
309 |
|
|
310 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
311 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
312 |
+ |
long runTime; |
313 |
+ |
public void startTest(Test test) {} |
314 |
+ |
protected void printHeader(long runTime) { |
315 |
+ |
this.runTime = runTime; // defer printing for later |
316 |
+ |
} |
317 |
+ |
protected void printFooter(TestResult result) { |
318 |
+ |
if (result.wasSuccessful()) { |
319 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
320 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
321 |
+ |
} else { |
322 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
323 |
+ |
super.printFooter(result); |
324 |
+ |
} |
325 |
+ |
} |
326 |
+ |
} |
327 |
+ |
|
328 |
+ |
/** |
329 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
330 |
+ |
* fluff, like printing a "." for each test case. |
331 |
+ |
*/ |
332 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
333 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
334 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
335 |
+ |
return runner; |
336 |
+ |
} |
337 |
+ |
|
338 |
|
/** |
339 |
|
* Runs all unit tests in the given test suite. |
340 |
|
* Actual behavior influenced by jsr166.* system properties. |
346 |
|
System.setSecurityManager(new SecurityManager()); |
347 |
|
} |
348 |
|
for (int i = 0; i < suiteRuns; i++) { |
349 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
349 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
350 |
|
if (!result.wasSuccessful()) |
351 |
|
System.exit(1); |
352 |
|
System.gc(); |
583 |
|
public static long LONG_DELAY_MS; |
584 |
|
|
585 |
|
/** |
586 |
< |
* Returns the shortest timed delay. This could |
587 |
< |
* be reimplemented to use for example a Property. |
586 |
> |
* Returns the shortest timed delay. This can be scaled up for |
587 |
> |
* slow machines using the jsr166.delay.factor system property, |
588 |
> |
* or via jtreg's -timeoutFactor:<val> flag. |
589 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
590 |
|
*/ |
591 |
|
protected long getShortDelay() { |
592 |
< |
return 50; |
592 |
> |
return (long) (50 * delayFactor * jtregTestTimeoutFactor); |
593 |
|
} |
594 |
|
|
595 |
|
/** |
902 |
|
}}; |
903 |
|
} |
904 |
|
|
905 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
906 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
907 |
+ |
} |
908 |
+ |
|
909 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
910 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
911 |
+ |
} |
912 |
+ |
|
913 |
|
/** |
914 |
|
* Waits out termination of a thread pool or fails doing so. |
915 |
|
*/ |
1300 |
|
} finally { |
1301 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1302 |
|
t.interrupt(); |
1303 |
< |
threadFail("Test timed out"); |
1303 |
> |
threadFail("timed out waiting for thread to terminate"); |
1304 |
|
} |
1305 |
|
} |
1306 |
|
} |
1468 |
|
|
1469 |
|
public void await(CountDownLatch latch) { |
1470 |
|
try { |
1471 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1471 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1472 |
> |
fail("timed out waiting for CountDownLatch for " |
1473 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1474 |
|
} catch (Throwable fail) { |
1475 |
|
threadUnexpectedException(fail); |
1476 |
|
} |
1478 |
|
|
1479 |
|
public void await(Semaphore semaphore) { |
1480 |
|
try { |
1481 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1481 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1482 |
> |
fail("timed out waiting for Semaphore for " |
1483 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1484 |
|
} catch (Throwable fail) { |
1485 |
|
threadUnexpectedException(fail); |
1486 |
|
} |