7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MINUTES; |
11 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
12 |
|
|
13 |
|
import java.io.ByteArrayInputStream; |
16 |
|
import java.io.ObjectOutputStream; |
17 |
|
import java.lang.management.ManagementFactory; |
18 |
|
import java.lang.management.ThreadInfo; |
19 |
+ |
import java.lang.management.ThreadMXBean; |
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 |
+ |
// 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.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; |
220 |
+ |
} |
221 |
+ |
lastTestCase = currentTestCase; |
222 |
+ |
}}}; |
223 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
224 |
+ |
thread.setDaemon(true); |
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 |
241 |
|
|| methodFilter.matcher(toString()).find()) |
242 |
|
super.runBare(); |
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(); |
539 |
|
} else { |
540 |
|
return new TestSuite(); |
541 |
|
} |
463 |
– |
|
542 |
|
} |
543 |
|
|
544 |
|
// Delays for timing-dependent tests, in milliseconds. |
596 |
|
* the same test have no effect. |
597 |
|
*/ |
598 |
|
public void threadRecordFailure(Throwable t) { |
599 |
+ |
System.err.println(t); |
600 |
+ |
dumpTestThreads(); |
601 |
|
threadFailure.compareAndSet(null, t); |
602 |
|
} |
603 |
|
|
608 |
|
void tearDownFail(String format, Object... args) { |
609 |
|
String msg = toString() + ": " + String.format(format, args); |
610 |
|
System.err.println(msg); |
611 |
< |
printAllStackTraces(); |
611 |
> |
dumpTestThreads(); |
612 |
|
throw new AssertionFailedError(msg); |
613 |
|
} |
614 |
|
|
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
648 |
> |
* Finds missing PoolCleaners |
649 |
|
*/ |
650 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
651 |
|
Thread[] survivors = new Thread[7]; |
677 |
|
fail(reason); |
678 |
|
} catch (AssertionFailedError t) { |
679 |
|
threadRecordFailure(t); |
680 |
< |
fail(reason); |
680 |
> |
throw t; |
681 |
|
} |
682 |
|
} |
683 |
|
|
804 |
|
/** |
805 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
806 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
807 |
< |
* until time elapses. |
807 |
> |
* until time elapses. Ensures that the given time, as measured |
808 |
> |
* by System.nanoTime(), has elapsed. |
809 |
|
*/ |
810 |
|
static void delay(long millis) throws InterruptedException { |
811 |
< |
long startTime = System.nanoTime(); |
812 |
< |
long ns = millis * 1000 * 1000; |
813 |
< |
for (;;) { |
811 |
> |
long nanos = millis * (1000 * 1000); |
812 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
813 |
> |
do { |
814 |
|
if (millis > 0L) |
815 |
|
Thread.sleep(millis); |
816 |
|
else // too short to sleep |
817 |
|
Thread.yield(); |
818 |
< |
long d = ns - (System.nanoTime() - startTime); |
819 |
< |
if (d > 0L) |
820 |
< |
millis = d / (1000 * 1000); |
821 |
< |
else |
822 |
< |
break; |
818 |
> |
nanos = wakeupTime - System.nanoTime(); |
819 |
> |
millis = nanos / (1000 * 1000); |
820 |
> |
} while (nanos >= 0L); |
821 |
> |
} |
822 |
> |
|
823 |
> |
/** |
824 |
> |
* Allows use of try-with-resources with per-test thread pools. |
825 |
> |
*/ |
826 |
> |
class PoolCleaner implements AutoCloseable { |
827 |
> |
private final ExecutorService pool; |
828 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
829 |
> |
public void close() { joinPool(pool); } |
830 |
> |
} |
831 |
> |
|
832 |
> |
/** |
833 |
> |
* An extension of PoolCleaner that has an action to release the pool. |
834 |
> |
*/ |
835 |
> |
class PoolCleanerWithReleaser extends PoolCleaner { |
836 |
> |
private final Runnable releaser; |
837 |
> |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
838 |
> |
super(pool); |
839 |
> |
this.releaser = releaser; |
840 |
> |
} |
841 |
> |
public void close() { |
842 |
> |
try { |
843 |
> |
releaser.run(); |
844 |
> |
} finally { |
845 |
> |
super.close(); |
846 |
> |
} |
847 |
|
} |
848 |
|
} |
849 |
|
|
850 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
851 |
+ |
return new PoolCleaner(pool); |
852 |
+ |
} |
853 |
+ |
|
854 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
855 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
856 |
+ |
} |
857 |
+ |
|
858 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
859 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
860 |
+ |
} |
861 |
+ |
|
862 |
+ |
Runnable releaser(final CountDownLatch latch) { |
863 |
+ |
return new Runnable() { public void run() { |
864 |
+ |
do { latch.countDown(); } |
865 |
+ |
while (latch.getCount() > 0); |
866 |
+ |
}}; |
867 |
+ |
} |
868 |
+ |
|
869 |
|
/** |
870 |
|
* Waits out termination of a thread pool or fails doing so. |
871 |
|
*/ |
872 |
|
void joinPool(ExecutorService pool) { |
873 |
|
try { |
874 |
|
pool.shutdown(); |
875 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
876 |
< |
fail("ExecutorService " + pool + |
877 |
< |
" did not terminate in a timely manner"); |
875 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
876 |
> |
try { |
877 |
> |
threadFail("ExecutorService " + pool + |
878 |
> |
" did not terminate in a timely manner"); |
879 |
> |
} finally { |
880 |
> |
// last resort, for the benefit of subsequent tests |
881 |
> |
pool.shutdownNow(); |
882 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
883 |
> |
} |
884 |
> |
} |
885 |
|
} catch (SecurityException ok) { |
886 |
|
// Allowed in case test doesn't have privs |
887 |
|
} catch (InterruptedException fail) { |
888 |
< |
fail("Unexpected InterruptedException"); |
888 |
> |
threadFail("Unexpected InterruptedException"); |
889 |
|
} |
890 |
|
} |
891 |
|
|
899 |
|
*/ |
900 |
|
void testInParallel(Action ... actions) { |
901 |
|
ExecutorService pool = Executors.newCachedThreadPool(); |
902 |
< |
try { |
902 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
903 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
904 |
|
for (final Action action : actions) |
905 |
|
futures.add(pool.submit(new CheckedRunnable() { |
912 |
|
} catch (Exception ex) { |
913 |
|
threadUnexpectedException(ex); |
914 |
|
} |
784 |
– |
} finally { |
785 |
– |
joinPool(pool); |
915 |
|
} |
916 |
|
} |
917 |
|
|
918 |
|
/** |
919 |
< |
* A debugging tool to print all stack traces, as jstack does. |
919 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
920 |
> |
* Uninteresting threads are filtered out. |
921 |
|
*/ |
922 |
< |
static void printAllStackTraces() { |
923 |
< |
for (ThreadInfo info : |
924 |
< |
ManagementFactory.getThreadMXBean() |
925 |
< |
.dumpAllThreads(true, true)) |
922 |
> |
static void dumpTestThreads() { |
923 |
> |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
924 |
> |
System.err.println("------ stacktrace dump start ------"); |
925 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
926 |
> |
String name = info.getThreadName(); |
927 |
> |
if ("Signal Dispatcher".equals(name)) |
928 |
> |
continue; |
929 |
> |
if ("Reference Handler".equals(name) |
930 |
> |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
931 |
> |
continue; |
932 |
> |
if ("Finalizer".equals(name) |
933 |
> |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
934 |
> |
continue; |
935 |
> |
if ("checkForWedgedTest".equals(name)) |
936 |
> |
continue; |
937 |
|
System.err.print(info); |
938 |
+ |
} |
939 |
+ |
System.err.println("------ stacktrace dump end ------"); |
940 |
|
} |
941 |
|
|
942 |
|
/** |
956 |
|
delay(millis); |
957 |
|
assertTrue(thread.isAlive()); |
958 |
|
} catch (InterruptedException fail) { |
959 |
< |
fail("Unexpected InterruptedException"); |
959 |
> |
threadFail("Unexpected InterruptedException"); |
960 |
|
} |
961 |
|
} |
962 |
|
|
978 |
|
for (Thread thread : threads) |
979 |
|
assertTrue(thread.isAlive()); |
980 |
|
} catch (InterruptedException fail) { |
981 |
< |
fail("Unexpected InterruptedException"); |
981 |
> |
threadFail("Unexpected InterruptedException"); |
982 |
|
} |
983 |
|
} |
984 |
|
|
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 |
|
} |
1404 |
|
}}; |
1405 |
|
} |
1406 |
|
|
1407 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1408 |
< |
return new CheckedRunnable() { |
1409 |
< |
public void realRun() throws InterruptedException { |
1410 |
< |
await(latch); |
1411 |
< |
}}; |
1407 |
> |
class LatchAwaiter extends CheckedRunnable { |
1408 |
> |
static final int NEW = 0; |
1409 |
> |
static final int RUNNING = 1; |
1410 |
> |
static final int DONE = 2; |
1411 |
> |
final CountDownLatch latch; |
1412 |
> |
int state = NEW; |
1413 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1414 |
> |
public void realRun() throws InterruptedException { |
1415 |
> |
state = 1; |
1416 |
> |
await(latch); |
1417 |
> |
state = 2; |
1418 |
> |
} |
1419 |
> |
} |
1420 |
> |
|
1421 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1422 |
> |
return new LatchAwaiter(latch); |
1423 |
|
} |
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 |
|
} |