20 |
|
import java.lang.reflect.Constructor; |
21 |
|
import java.lang.reflect.Method; |
22 |
|
import java.lang.reflect.Modifier; |
23 |
+ |
import java.nio.file.Files; |
24 |
+ |
import java.nio.file.Paths; |
25 |
|
import java.security.CodeSource; |
26 |
|
import java.security.Permission; |
27 |
|
import java.security.PermissionCollection; |
54 |
|
import java.util.concurrent.ThreadPoolExecutor; |
55 |
|
import java.util.concurrent.TimeoutException; |
56 |
|
import java.util.concurrent.atomic.AtomicReference; |
57 |
+ |
import java.util.regex.Matcher; |
58 |
|
import java.util.regex.Pattern; |
59 |
|
|
60 |
|
import junit.framework.AssertionFailedError; |
191 |
|
return (regex == null) ? null : Pattern.compile(regex); |
192 |
|
} |
193 |
|
|
194 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
195 |
|
static volatile TestCase currentTestCase; |
196 |
+ |
// static volatile int currentRun = 0; |
197 |
|
static { |
198 |
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
199 |
< |
// avoid spurious reports with enormous runsPerTest |
200 |
< |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
199 |
> |
// Avoid spurious reports with enormous runsPerTest. |
200 |
> |
// A single test case run should never take more than 1 second. |
201 |
> |
// But let's cap it at the high end too ... |
202 |
> |
final int timeoutMinutes = |
203 |
> |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
204 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
205 |
|
try { MINUTES.sleep(timeoutMinutes); } |
206 |
|
catch (InterruptedException unexpected) { break; } |
207 |
|
if (lastTestCase == currentTestCase) { |
208 |
< |
System.err.println |
209 |
< |
("Looks like we're stuck running test: " |
210 |
< |
+ lastTestCase); |
208 |
> |
System.err.printf( |
209 |
> |
"Looks like we're stuck running test: %s%n", |
210 |
> |
lastTestCase); |
211 |
> |
// System.err.printf( |
212 |
> |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
213 |
> |
// lastTestCase, currentRun, runsPerTest); |
214 |
> |
// System.err.println("availableProcessors=" + |
215 |
> |
// Runtime.getRuntime().availableProcessors()); |
216 |
> |
// System.err.printf("cpu model = %s%n", cpuModel()); |
217 |
|
dumpTestThreads(); |
218 |
|
// one stack dump is probably enough; more would be spam |
219 |
|
break; |
225 |
|
thread.start(); |
226 |
|
} |
227 |
|
|
228 |
+ |
// public static String cpuModel() { |
229 |
+ |
// try { |
230 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
231 |
+ |
// .matcher(new String( |
232 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
233 |
+ |
// matcher.find(); |
234 |
+ |
// return matcher.group(1); |
235 |
+ |
// } catch (Exception ex) { return null; } |
236 |
+ |
// } |
237 |
+ |
|
238 |
|
public void runBare() throws Throwable { |
239 |
|
currentTestCase = this; |
240 |
|
if (methodFilter == null |
244 |
|
|
245 |
|
protected void runTest() throws Throwable { |
246 |
|
for (int i = 0; i < runsPerTest; i++) { |
247 |
+ |
// currentRun = i; |
248 |
|
if (profileTests) |
249 |
|
runTestProfiled(); |
250 |
|
else |
273 |
|
main(suite(), args); |
274 |
|
} |
275 |
|
|
276 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
277 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
278 |
+ |
long runTime; |
279 |
+ |
public void startTest(Test test) {} |
280 |
+ |
protected void printHeader(long runTime) { |
281 |
+ |
this.runTime = runTime; // defer printing for later |
282 |
+ |
} |
283 |
+ |
protected void printFooter(TestResult result) { |
284 |
+ |
if (result.wasSuccessful()) { |
285 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
286 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
287 |
+ |
} else { |
288 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
289 |
+ |
super.printFooter(result); |
290 |
+ |
} |
291 |
+ |
} |
292 |
+ |
} |
293 |
+ |
|
294 |
+ |
/** |
295 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
296 |
+ |
* fluff, like printing a "." for each test case. |
297 |
+ |
*/ |
298 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
299 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
300 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
301 |
+ |
return runner; |
302 |
+ |
} |
303 |
+ |
|
304 |
|
/** |
305 |
|
* Runs all unit tests in the given test suite. |
306 |
|
* Actual behavior influenced by jsr166.* system properties. |
312 |
|
System.setSecurityManager(new SecurityManager()); |
313 |
|
} |
314 |
|
for (int i = 0; i < suiteRuns; i++) { |
315 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
315 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
316 |
|
if (!result.wasSuccessful()) |
317 |
|
System.exit(1); |
318 |
|
System.gc(); |
1256 |
|
} finally { |
1257 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1258 |
|
t.interrupt(); |
1259 |
< |
fail("Test timed out"); |
1259 |
> |
threadFail("timed out waiting for thread to terminate"); |
1260 |
|
} |
1261 |
|
} |
1262 |
|
} |
1424 |
|
|
1425 |
|
public void await(CountDownLatch latch) { |
1426 |
|
try { |
1427 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1427 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1428 |
> |
fail("timed out waiting for CountDownLatch for " |
1429 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1430 |
|
} catch (Throwable fail) { |
1431 |
|
threadUnexpectedException(fail); |
1432 |
|
} |
1434 |
|
|
1435 |
|
public void await(Semaphore semaphore) { |
1436 |
|
try { |
1437 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1437 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1438 |
> |
fail("timed out waiting for Semaphore for " |
1439 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1440 |
|
} catch (Throwable fail) { |
1441 |
|
threadUnexpectedException(fail); |
1442 |
|
} |