6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
/* |
10 |
+ |
* @test |
11 |
+ |
* @summary JSR-166 tck tests |
12 |
+ |
* @build * |
13 |
+ |
* @run junit/othervm/timeout=1000 JSR166TestCase |
14 |
+ |
*/ |
15 |
+ |
|
16 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
18 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
27 |
|
import java.lang.reflect.Constructor; |
28 |
|
import java.lang.reflect.Method; |
29 |
|
import java.lang.reflect.Modifier; |
30 |
+ |
import java.nio.file.Files; |
31 |
+ |
import java.nio.file.Paths; |
32 |
|
import java.security.CodeSource; |
33 |
|
import java.security.Permission; |
34 |
|
import java.security.PermissionCollection; |
61 |
|
import java.util.concurrent.ThreadPoolExecutor; |
62 |
|
import java.util.concurrent.TimeoutException; |
63 |
|
import java.util.concurrent.atomic.AtomicReference; |
64 |
+ |
import java.util.regex.Matcher; |
65 |
|
import java.util.regex.Pattern; |
66 |
|
|
67 |
|
import junit.framework.AssertionFailedError; |
119 |
|
* methods as there are exceptions the method can throw. Sometimes |
120 |
|
* there are multiple tests per JSR166 method when the different |
121 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
122 |
< |
* cover multiple methods when they cannot be tested in |
113 |
< |
* isolation. |
122 |
> |
* cover multiple methods when they cannot be tested in isolation. |
123 |
|
* |
124 |
|
* <li>The documentation style for testcases is to provide as javadoc |
125 |
|
* a simple sentence or two describing the property that the testcase |
182 |
|
private static final int suiteRuns = |
183 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
184 |
|
|
185 |
+ |
/** |
186 |
+ |
* The scaling factor to apply to standard delays used in tests. |
187 |
+ |
*/ |
188 |
+ |
private static final int delayFactor = |
189 |
+ |
Integer.getInteger("jsr166.delay.factor", 1); |
190 |
+ |
|
191 |
|
public JSR166TestCase() { super(); } |
192 |
|
public JSR166TestCase(String name) { super(name); } |
193 |
|
|
203 |
|
return (regex == null) ? null : Pattern.compile(regex); |
204 |
|
} |
205 |
|
|
206 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
207 |
|
static volatile TestCase currentTestCase; |
208 |
+ |
// static volatile int currentRun = 0; |
209 |
|
static { |
210 |
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
211 |
< |
// avoid spurious reports with enormous runsPerTest |
212 |
< |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
211 |
> |
// Avoid spurious reports with enormous runsPerTest. |
212 |
> |
// A single test case run should never take more than 1 second. |
213 |
> |
// But let's cap it at the high end too ... |
214 |
> |
final int timeoutMinutes = |
215 |
> |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
216 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
217 |
|
try { MINUTES.sleep(timeoutMinutes); } |
218 |
|
catch (InterruptedException unexpected) { break; } |
219 |
|
if (lastTestCase == currentTestCase) { |
220 |
< |
System.err.println |
221 |
< |
("Looks like we're stuck running test: " |
222 |
< |
+ lastTestCase); |
223 |
< |
System.err.println("availableProcessors=" + |
224 |
< |
Runtime.getRuntime().availableProcessors()); |
220 |
> |
System.err.printf( |
221 |
> |
"Looks like we're stuck running test: %s%n", |
222 |
> |
lastTestCase); |
223 |
> |
// System.err.printf( |
224 |
> |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
225 |
> |
// lastTestCase, currentRun, runsPerTest); |
226 |
> |
// System.err.println("availableProcessors=" + |
227 |
> |
// Runtime.getRuntime().availableProcessors()); |
228 |
> |
// System.err.printf("cpu model = %s%n", cpuModel()); |
229 |
|
dumpTestThreads(); |
230 |
|
// one stack dump is probably enough; more would be spam |
231 |
|
break; |
237 |
|
thread.start(); |
238 |
|
} |
239 |
|
|
240 |
+ |
// public static String cpuModel() { |
241 |
+ |
// try { |
242 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
243 |
+ |
// .matcher(new String( |
244 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
245 |
+ |
// matcher.find(); |
246 |
+ |
// return matcher.group(1); |
247 |
+ |
// } catch (Exception ex) { return null; } |
248 |
+ |
// } |
249 |
+ |
|
250 |
|
public void runBare() throws Throwable { |
251 |
|
currentTestCase = this; |
252 |
|
if (methodFilter == null |
256 |
|
|
257 |
|
protected void runTest() throws Throwable { |
258 |
|
for (int i = 0; i < runsPerTest; i++) { |
259 |
+ |
// currentRun = i; |
260 |
|
if (profileTests) |
261 |
|
runTestProfiled(); |
262 |
|
else |
285 |
|
main(suite(), args); |
286 |
|
} |
287 |
|
|
288 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
289 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
290 |
+ |
long runTime; |
291 |
+ |
public void startTest(Test test) {} |
292 |
+ |
protected void printHeader(long runTime) { |
293 |
+ |
this.runTime = runTime; // defer printing for later |
294 |
+ |
} |
295 |
+ |
protected void printFooter(TestResult result) { |
296 |
+ |
if (result.wasSuccessful()) { |
297 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
298 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
299 |
+ |
} else { |
300 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
301 |
+ |
super.printFooter(result); |
302 |
+ |
} |
303 |
+ |
} |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
/** |
307 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
308 |
+ |
* fluff, like printing a "." for each test case. |
309 |
+ |
*/ |
310 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
311 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
312 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
313 |
+ |
return runner; |
314 |
+ |
} |
315 |
+ |
|
316 |
|
/** |
317 |
|
* Runs all unit tests in the given test suite. |
318 |
|
* Actual behavior influenced by jsr166.* system properties. |
324 |
|
System.setSecurityManager(new SecurityManager()); |
325 |
|
} |
326 |
|
for (int i = 0; i < suiteRuns; i++) { |
327 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
327 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
328 |
|
if (!result.wasSuccessful()) |
329 |
|
System.exit(1); |
330 |
|
System.gc(); |
561 |
|
public static long LONG_DELAY_MS; |
562 |
|
|
563 |
|
/** |
564 |
< |
* Returns the shortest timed delay. This could |
565 |
< |
* be reimplemented to use for example a Property. |
564 |
> |
* Returns the shortest timed delay. This can be scaled up for |
565 |
> |
* slow machines using the jsr166.delay.factor system property. |
566 |
|
*/ |
567 |
|
protected long getShortDelay() { |
568 |
< |
return 50; |
568 |
> |
return 50 * delayFactor; |
569 |
|
} |
570 |
|
|
571 |
|
/** |
1268 |
|
} finally { |
1269 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1270 |
|
t.interrupt(); |
1271 |
< |
threadFail("Test timed out"); |
1271 |
> |
threadFail("timed out waiting for thread to terminate"); |
1272 |
|
} |
1273 |
|
} |
1274 |
|
} |
1436 |
|
|
1437 |
|
public void await(CountDownLatch latch) { |
1438 |
|
try { |
1439 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1439 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1440 |
> |
fail("timed out waiting for CountDownLatch for " |
1441 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1442 |
|
} catch (Throwable fail) { |
1443 |
|
threadUnexpectedException(fail); |
1444 |
|
} |
1446 |
|
|
1447 |
|
public void await(Semaphore semaphore) { |
1448 |
|
try { |
1449 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1449 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1450 |
> |
fail("timed out waiting for Semaphore for " |
1451 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1452 |
|
} catch (Throwable fail) { |
1453 |
|
threadUnexpectedException(fail); |
1454 |
|
} |