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; |
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; |
112 |
|
* methods as there are exceptions the method can throw. Sometimes |
113 |
|
* there are multiple tests per JSR166 method when the different |
114 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
115 |
< |
* cover multiple methods when they cannot be tested in |
112 |
< |
* isolation. |
115 |
> |
* cover multiple methods when they cannot be tested in isolation. |
116 |
|
* |
117 |
|
* <li>The documentation style for testcases is to provide as javadoc |
118 |
|
* a simple sentence or two describing the property that the testcase |
175 |
|
private static final int suiteRuns = |
176 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
177 |
|
|
178 |
+ |
/** |
179 |
+ |
* The scaling factor to apply to standard delays used in tests. |
180 |
+ |
*/ |
181 |
+ |
private static final int delayFactor = |
182 |
+ |
Integer.getInteger("jsr166.delay.factor", 1); |
183 |
+ |
|
184 |
|
public JSR166TestCase() { super(); } |
185 |
|
public JSR166TestCase(String name) { super(name); } |
186 |
|
|
196 |
|
return (regex == null) ? null : Pattern.compile(regex); |
197 |
|
} |
198 |
|
|
199 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
200 |
+ |
static volatile TestCase currentTestCase; |
201 |
+ |
// static volatile int currentRun = 0; |
202 |
+ |
static { |
203 |
+ |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
204 |
+ |
// Avoid spurious reports with enormous runsPerTest. |
205 |
+ |
// A single test case run should never take more than 1 second. |
206 |
+ |
// But let's cap it at the high end too ... |
207 |
+ |
final int timeoutMinutes = |
208 |
+ |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
209 |
+ |
for (TestCase lastTestCase = currentTestCase;;) { |
210 |
+ |
try { MINUTES.sleep(timeoutMinutes); } |
211 |
+ |
catch (InterruptedException unexpected) { break; } |
212 |
+ |
if (lastTestCase == currentTestCase) { |
213 |
+ |
System.err.printf( |
214 |
+ |
"Looks like we're stuck running test: %s%n", |
215 |
+ |
lastTestCase); |
216 |
+ |
// System.err.printf( |
217 |
+ |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
218 |
+ |
// lastTestCase, currentRun, runsPerTest); |
219 |
+ |
// System.err.println("availableProcessors=" + |
220 |
+ |
// Runtime.getRuntime().availableProcessors()); |
221 |
+ |
// System.err.printf("cpu model = %s%n", cpuModel()); |
222 |
+ |
dumpTestThreads(); |
223 |
+ |
// one stack dump is probably enough; more would be spam |
224 |
+ |
break; |
225 |
+ |
} |
226 |
+ |
lastTestCase = currentTestCase; |
227 |
+ |
}}}; |
228 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
229 |
+ |
thread.setDaemon(true); |
230 |
+ |
thread.start(); |
231 |
+ |
} |
232 |
+ |
|
233 |
+ |
// public static String cpuModel() { |
234 |
+ |
// try { |
235 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
236 |
+ |
// .matcher(new String( |
237 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
238 |
+ |
// matcher.find(); |
239 |
+ |
// return matcher.group(1); |
240 |
+ |
// } catch (Exception ex) { return null; } |
241 |
+ |
// } |
242 |
+ |
|
243 |
|
public void runBare() throws Throwable { |
244 |
+ |
currentTestCase = this; |
245 |
|
if (methodFilter == null |
246 |
|
|| methodFilter.matcher(toString()).find()) |
247 |
|
super.runBare(); |
249 |
|
|
250 |
|
protected void runTest() throws Throwable { |
251 |
|
for (int i = 0; i < runsPerTest; i++) { |
252 |
+ |
// currentRun = i; |
253 |
|
if (profileTests) |
254 |
|
runTestProfiled(); |
255 |
|
else |
278 |
|
main(suite(), args); |
279 |
|
} |
280 |
|
|
281 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
282 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
283 |
+ |
long runTime; |
284 |
+ |
public void startTest(Test test) {} |
285 |
+ |
protected void printHeader(long runTime) { |
286 |
+ |
this.runTime = runTime; // defer printing for later |
287 |
+ |
} |
288 |
+ |
protected void printFooter(TestResult result) { |
289 |
+ |
if (result.wasSuccessful()) { |
290 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
291 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
292 |
+ |
} else { |
293 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
294 |
+ |
super.printFooter(result); |
295 |
+ |
} |
296 |
+ |
} |
297 |
+ |
} |
298 |
+ |
|
299 |
+ |
/** |
300 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
301 |
+ |
* fluff, like printing a "." for each test case. |
302 |
+ |
*/ |
303 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
304 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
305 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
306 |
+ |
return runner; |
307 |
+ |
} |
308 |
+ |
|
309 |
|
/** |
310 |
|
* Runs all unit tests in the given test suite. |
311 |
|
* Actual behavior influenced by jsr166.* system properties. |
317 |
|
System.setSecurityManager(new SecurityManager()); |
318 |
|
} |
319 |
|
for (int i = 0; i < suiteRuns; i++) { |
320 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
320 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
321 |
|
if (!result.wasSuccessful()) |
322 |
|
System.exit(1); |
323 |
|
System.gc(); |
554 |
|
public static long LONG_DELAY_MS; |
555 |
|
|
556 |
|
/** |
557 |
< |
* Returns the shortest timed delay. This could |
558 |
< |
* be reimplemented to use for example a Property. |
557 |
> |
* Returns the shortest timed delay. This can be scaled up for |
558 |
> |
* slow machines using the jsr166.delay.factor system property. |
559 |
|
*/ |
560 |
|
protected long getShortDelay() { |
561 |
< |
return 50; |
561 |
> |
return 50 * delayFactor; |
562 |
|
} |
563 |
|
|
564 |
|
/** |
601 |
|
* the same test have no effect. |
602 |
|
*/ |
603 |
|
public void threadRecordFailure(Throwable t) { |
604 |
< |
threadDump(); |
604 |
> |
System.err.println(t); |
605 |
> |
dumpTestThreads(); |
606 |
|
threadFailure.compareAndSet(null, t); |
607 |
|
} |
608 |
|
|
613 |
|
void tearDownFail(String format, Object... args) { |
614 |
|
String msg = toString() + ": " + String.format(format, args); |
615 |
|
System.err.println(msg); |
616 |
< |
threadDump(); |
616 |
> |
dumpTestThreads(); |
617 |
|
throw new AssertionFailedError(msg); |
618 |
|
} |
619 |
|
|
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
653 |
> |
* Finds missing PoolCleaners |
654 |
|
*/ |
655 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
656 |
|
Thread[] survivors = new Thread[7]; |
809 |
|
/** |
810 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
811 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
812 |
< |
* until time elapses. |
812 |
> |
* until time elapses. Ensures that the given time, as measured |
813 |
> |
* by System.nanoTime(), has elapsed. |
814 |
|
*/ |
815 |
|
static void delay(long millis) throws InterruptedException { |
816 |
< |
long startTime = System.nanoTime(); |
817 |
< |
long ns = millis * 1000 * 1000; |
818 |
< |
for (;;) { |
816 |
> |
long nanos = millis * (1000 * 1000); |
817 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
818 |
> |
do { |
819 |
|
if (millis > 0L) |
820 |
|
Thread.sleep(millis); |
821 |
|
else // too short to sleep |
822 |
|
Thread.yield(); |
823 |
< |
long d = ns - (System.nanoTime() - startTime); |
824 |
< |
if (d > 0L) |
825 |
< |
millis = d / (1000 * 1000); |
741 |
< |
else |
742 |
< |
break; |
743 |
< |
} |
823 |
> |
nanos = wakeupTime - System.nanoTime(); |
824 |
> |
millis = nanos / (1000 * 1000); |
825 |
> |
} while (nanos >= 0L); |
826 |
|
} |
827 |
|
|
828 |
|
/** |
829 |
|
* Allows use of try-with-resources with per-test thread pools. |
830 |
|
*/ |
831 |
< |
static class PoolCloser<T extends ExecutorService> |
832 |
< |
implements AutoCloseable { |
833 |
< |
public final T pool; |
752 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
831 |
> |
class PoolCleaner implements AutoCloseable { |
832 |
> |
private final ExecutorService pool; |
833 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
834 |
|
public void close() { joinPool(pool); } |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
839 |
+ |
*/ |
840 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
841 |
+ |
private final Runnable releaser; |
842 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
843 |
+ |
super(pool); |
844 |
+ |
this.releaser = releaser; |
845 |
+ |
} |
846 |
+ |
public void close() { |
847 |
+ |
try { |
848 |
+ |
releaser.run(); |
849 |
+ |
} finally { |
850 |
+ |
super.close(); |
851 |
+ |
} |
852 |
+ |
} |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
856 |
+ |
return new PoolCleaner(pool); |
857 |
+ |
} |
858 |
+ |
|
859 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
860 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
864 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
Runnable releaser(final CountDownLatch latch) { |
868 |
+ |
return new Runnable() { public void run() { |
869 |
+ |
do { latch.countDown(); } |
870 |
+ |
while (latch.getCount() > 0); |
871 |
+ |
}}; |
872 |
+ |
} |
873 |
+ |
|
874 |
+ |
/** |
875 |
|
* Waits out termination of a thread pool or fails doing so. |
876 |
|
*/ |
877 |
< |
static void joinPool(ExecutorService pool) { |
877 |
> |
void joinPool(ExecutorService pool) { |
878 |
|
try { |
879 |
|
pool.shutdown(); |
880 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
881 |
< |
fail("ExecutorService " + pool + |
882 |
< |
" did not terminate in a timely manner"); |
880 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
881 |
> |
try { |
882 |
> |
threadFail("ExecutorService " + pool + |
883 |
> |
" did not terminate in a timely manner"); |
884 |
> |
} finally { |
885 |
> |
// last resort, for the benefit of subsequent tests |
886 |
> |
pool.shutdownNow(); |
887 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
888 |
> |
} |
889 |
> |
} |
890 |
|
} catch (SecurityException ok) { |
891 |
|
// Allowed in case test doesn't have privs |
892 |
|
} catch (InterruptedException fail) { |
893 |
< |
fail("Unexpected InterruptedException"); |
893 |
> |
threadFail("Unexpected InterruptedException"); |
894 |
|
} |
895 |
|
} |
896 |
|
|
903 |
|
* necessarily individually slow because they must block. |
904 |
|
*/ |
905 |
|
void testInParallel(Action ... actions) { |
906 |
< |
try (PoolCloser<ExecutorService> poolCloser |
907 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
783 |
< |
ExecutorService pool = poolCloser.pool; |
906 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
907 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
908 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
909 |
|
for (final Action action : actions) |
910 |
|
futures.add(pool.submit(new CheckedRunnable() { |
921 |
|
} |
922 |
|
|
923 |
|
/** |
924 |
< |
* A debugging tool to print all stack traces, as jstack does. |
924 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
925 |
|
* Uninteresting threads are filtered out. |
926 |
|
*/ |
927 |
< |
static void threadDump() { |
927 |
> |
static void dumpTestThreads() { |
928 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
929 |
|
System.err.println("------ stacktrace dump start ------"); |
930 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
937 |
|
if ("Finalizer".equals(name) |
938 |
|
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
939 |
|
continue; |
940 |
+ |
if ("checkForWedgedTest".equals(name)) |
941 |
+ |
continue; |
942 |
|
System.err.print(info); |
943 |
|
} |
944 |
|
System.err.println("------ stacktrace dump end ------"); |
961 |
|
delay(millis); |
962 |
|
assertTrue(thread.isAlive()); |
963 |
|
} catch (InterruptedException fail) { |
964 |
< |
fail("Unexpected InterruptedException"); |
964 |
> |
threadFail("Unexpected InterruptedException"); |
965 |
|
} |
966 |
|
} |
967 |
|
|
983 |
|
for (Thread thread : threads) |
984 |
|
assertTrue(thread.isAlive()); |
985 |
|
} catch (InterruptedException fail) { |
986 |
< |
fail("Unexpected InterruptedException"); |
986 |
> |
threadFail("Unexpected InterruptedException"); |
987 |
|
} |
988 |
|
} |
989 |
|
|
1261 |
|
} finally { |
1262 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1263 |
|
t.interrupt(); |
1264 |
< |
fail("Test timed out"); |
1264 |
> |
threadFail("timed out waiting for thread to terminate"); |
1265 |
|
} |
1266 |
|
} |
1267 |
|
} |
1409 |
|
}}; |
1410 |
|
} |
1411 |
|
|
1412 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1413 |
< |
return new CheckedRunnable() { |
1414 |
< |
public void realRun() throws InterruptedException { |
1415 |
< |
await(latch); |
1416 |
< |
}}; |
1412 |
> |
class LatchAwaiter extends CheckedRunnable { |
1413 |
> |
static final int NEW = 0; |
1414 |
> |
static final int RUNNING = 1; |
1415 |
> |
static final int DONE = 2; |
1416 |
> |
final CountDownLatch latch; |
1417 |
> |
int state = NEW; |
1418 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1419 |
> |
public void realRun() throws InterruptedException { |
1420 |
> |
state = 1; |
1421 |
> |
await(latch); |
1422 |
> |
state = 2; |
1423 |
> |
} |
1424 |
> |
} |
1425 |
> |
|
1426 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1427 |
> |
return new LatchAwaiter(latch); |
1428 |
|
} |
1429 |
|
|
1430 |
|
public void await(CountDownLatch latch) { |
1431 |
|
try { |
1432 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1432 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1433 |
> |
fail("timed out waiting for CountDownLatch for " |
1434 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1435 |
|
} catch (Throwable fail) { |
1436 |
|
threadUnexpectedException(fail); |
1437 |
|
} |
1439 |
|
|
1440 |
|
public void await(Semaphore semaphore) { |
1441 |
|
try { |
1442 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1442 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1443 |
> |
fail("timed out waiting for Semaphore for " |
1444 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1445 |
|
} catch (Throwable fail) { |
1446 |
|
threadUnexpectedException(fail); |
1447 |
|
} |