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; |
20 |
|
|
21 |
|
import java.io.ByteArrayInputStream; |
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; |
62 |
|
import java.util.concurrent.ThreadPoolExecutor; |
63 |
|
import java.util.concurrent.TimeoutException; |
64 |
|
import java.util.concurrent.atomic.AtomicReference; |
65 |
+ |
import java.util.regex.Matcher; |
66 |
|
import java.util.regex.Pattern; |
67 |
|
|
68 |
|
import junit.framework.AssertionFailedError; |
120 |
|
* methods as there are exceptions the method can throw. Sometimes |
121 |
|
* there are multiple tests per JSR166 method when the different |
122 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
123 |
< |
* cover multiple methods when they cannot be tested in |
112 |
< |
* isolation. |
123 |
> |
* cover multiple methods when they cannot be tested in isolation. |
124 |
|
* |
125 |
|
* <li>The documentation style for testcases is to provide as javadoc |
126 |
|
* a simple sentence or two describing the property that the testcase |
183 |
|
private static final int suiteRuns = |
184 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
185 |
|
|
186 |
+ |
/** |
187 |
+ |
* The scaling factor to apply to standard delays used in tests. |
188 |
+ |
*/ |
189 |
+ |
private static final int delayFactor = |
190 |
+ |
Integer.getInteger("jsr166.delay.factor", 1); |
191 |
+ |
|
192 |
|
public JSR166TestCase() { super(); } |
193 |
|
public JSR166TestCase(String name) { super(name); } |
194 |
|
|
204 |
|
return (regex == null) ? null : Pattern.compile(regex); |
205 |
|
} |
206 |
|
|
207 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
208 |
+ |
static volatile TestCase currentTestCase; |
209 |
+ |
// static volatile int currentRun = 0; |
210 |
+ |
static { |
211 |
+ |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
212 |
+ |
// Avoid spurious reports with enormous runsPerTest. |
213 |
+ |
// A single test case run should never take more than 1 second. |
214 |
+ |
// But let's cap it at the high end too ... |
215 |
+ |
final int timeoutMinutes = |
216 |
+ |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
217 |
+ |
for (TestCase lastTestCase = currentTestCase;;) { |
218 |
+ |
try { MINUTES.sleep(timeoutMinutes); } |
219 |
+ |
catch (InterruptedException unexpected) { break; } |
220 |
+ |
if (lastTestCase == currentTestCase) { |
221 |
+ |
System.err.printf( |
222 |
+ |
"Looks like we're stuck running test: %s%n", |
223 |
+ |
lastTestCase); |
224 |
+ |
// System.err.printf( |
225 |
+ |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
226 |
+ |
// lastTestCase, currentRun, runsPerTest); |
227 |
+ |
// System.err.println("availableProcessors=" + |
228 |
+ |
// Runtime.getRuntime().availableProcessors()); |
229 |
+ |
// System.err.printf("cpu model = %s%n", cpuModel()); |
230 |
+ |
dumpTestThreads(); |
231 |
+ |
// one stack dump is probably enough; more would be spam |
232 |
+ |
break; |
233 |
+ |
} |
234 |
+ |
lastTestCase = currentTestCase; |
235 |
+ |
}}}; |
236 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
237 |
+ |
thread.setDaemon(true); |
238 |
+ |
thread.start(); |
239 |
+ |
} |
240 |
+ |
|
241 |
+ |
// public static String cpuModel() { |
242 |
+ |
// try { |
243 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
244 |
+ |
// .matcher(new String( |
245 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
246 |
+ |
// matcher.find(); |
247 |
+ |
// return matcher.group(1); |
248 |
+ |
// } catch (Exception ex) { return null; } |
249 |
+ |
// } |
250 |
+ |
|
251 |
|
public void runBare() throws Throwable { |
252 |
+ |
currentTestCase = this; |
253 |
|
if (methodFilter == null |
254 |
|
|| methodFilter.matcher(toString()).find()) |
255 |
|
super.runBare(); |
257 |
|
|
258 |
|
protected void runTest() throws Throwable { |
259 |
|
for (int i = 0; i < runsPerTest; i++) { |
260 |
+ |
// currentRun = i; |
261 |
|
if (profileTests) |
262 |
|
runTestProfiled(); |
263 |
|
else |
286 |
|
main(suite(), args); |
287 |
|
} |
288 |
|
|
289 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
290 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
291 |
+ |
long runTime; |
292 |
+ |
public void startTest(Test test) {} |
293 |
+ |
protected void printHeader(long runTime) { |
294 |
+ |
this.runTime = runTime; // defer printing for later |
295 |
+ |
} |
296 |
+ |
protected void printFooter(TestResult result) { |
297 |
+ |
if (result.wasSuccessful()) { |
298 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
299 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
300 |
+ |
} else { |
301 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
302 |
+ |
super.printFooter(result); |
303 |
+ |
} |
304 |
+ |
} |
305 |
+ |
} |
306 |
+ |
|
307 |
+ |
/** |
308 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
309 |
+ |
* fluff, like printing a "." for each test case. |
310 |
+ |
*/ |
311 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
312 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
313 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
314 |
+ |
return runner; |
315 |
+ |
} |
316 |
+ |
|
317 |
|
/** |
318 |
|
* Runs all unit tests in the given test suite. |
319 |
|
* Actual behavior influenced by jsr166.* system properties. |
325 |
|
System.setSecurityManager(new SecurityManager()); |
326 |
|
} |
327 |
|
for (int i = 0; i < suiteRuns; i++) { |
328 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
328 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
329 |
|
if (!result.wasSuccessful()) |
330 |
|
System.exit(1); |
331 |
|
System.gc(); |
562 |
|
public static long LONG_DELAY_MS; |
563 |
|
|
564 |
|
/** |
565 |
< |
* Returns the shortest timed delay. This could |
566 |
< |
* be reimplemented to use for example a Property. |
565 |
> |
* Returns the shortest timed delay. This can be scaled up for |
566 |
> |
* slow machines using the jsr166.delay.factor system property. |
567 |
|
*/ |
568 |
|
protected long getShortDelay() { |
569 |
< |
return 50; |
569 |
> |
return 50 * delayFactor; |
570 |
|
} |
571 |
|
|
572 |
|
/** |
609 |
|
* the same test have no effect. |
610 |
|
*/ |
611 |
|
public void threadRecordFailure(Throwable t) { |
612 |
< |
threadDump(); |
612 |
> |
System.err.println(t); |
613 |
> |
dumpTestThreads(); |
614 |
|
threadFailure.compareAndSet(null, t); |
615 |
|
} |
616 |
|
|
621 |
|
void tearDownFail(String format, Object... args) { |
622 |
|
String msg = toString() + ": " + String.format(format, args); |
623 |
|
System.err.println(msg); |
624 |
< |
threadDump(); |
624 |
> |
dumpTestThreads(); |
625 |
|
throw new AssertionFailedError(msg); |
626 |
|
} |
627 |
|
|
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
661 |
> |
* Finds missing PoolCleaners |
662 |
|
*/ |
663 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
664 |
|
Thread[] survivors = new Thread[7]; |
817 |
|
/** |
818 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
819 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
820 |
< |
* until time elapses. |
820 |
> |
* until time elapses. Ensures that the given time, as measured |
821 |
> |
* by System.nanoTime(), has elapsed. |
822 |
|
*/ |
823 |
|
static void delay(long millis) throws InterruptedException { |
824 |
< |
long startTime = System.nanoTime(); |
825 |
< |
long ns = millis * 1000 * 1000; |
826 |
< |
for (;;) { |
824 |
> |
long nanos = millis * (1000 * 1000); |
825 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
826 |
> |
do { |
827 |
|
if (millis > 0L) |
828 |
|
Thread.sleep(millis); |
829 |
|
else // too short to sleep |
830 |
|
Thread.yield(); |
831 |
< |
long d = ns - (System.nanoTime() - startTime); |
832 |
< |
if (d > 0L) |
833 |
< |
millis = d / (1000 * 1000); |
741 |
< |
else |
742 |
< |
break; |
743 |
< |
} |
831 |
> |
nanos = wakeupTime - System.nanoTime(); |
832 |
> |
millis = nanos / (1000 * 1000); |
833 |
> |
} while (nanos >= 0L); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
837 |
|
* Allows use of try-with-resources with per-test thread pools. |
838 |
|
*/ |
839 |
< |
static class PoolCloser<T extends ExecutorService> |
840 |
< |
implements AutoCloseable { |
841 |
< |
public final T pool; |
752 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
839 |
> |
class PoolCleaner implements AutoCloseable { |
840 |
> |
private final ExecutorService pool; |
841 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
842 |
|
public void close() { joinPool(pool); } |
843 |
|
} |
844 |
|
|
845 |
|
/** |
846 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
847 |
+ |
*/ |
848 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
849 |
+ |
private final Runnable releaser; |
850 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
851 |
+ |
super(pool); |
852 |
+ |
this.releaser = releaser; |
853 |
+ |
} |
854 |
+ |
public void close() { |
855 |
+ |
try { |
856 |
+ |
releaser.run(); |
857 |
+ |
} finally { |
858 |
+ |
super.close(); |
859 |
+ |
} |
860 |
+ |
} |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
864 |
+ |
return new PoolCleaner(pool); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
868 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
872 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
873 |
+ |
} |
874 |
+ |
|
875 |
+ |
Runnable releaser(final CountDownLatch latch) { |
876 |
+ |
return new Runnable() { public void run() { |
877 |
+ |
do { latch.countDown(); } |
878 |
+ |
while (latch.getCount() > 0); |
879 |
+ |
}}; |
880 |
+ |
} |
881 |
+ |
|
882 |
+ |
/** |
883 |
|
* Waits out termination of a thread pool or fails doing so. |
884 |
|
*/ |
885 |
< |
static void joinPool(ExecutorService pool) { |
885 |
> |
void joinPool(ExecutorService pool) { |
886 |
|
try { |
887 |
|
pool.shutdown(); |
888 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
889 |
< |
fail("ExecutorService " + pool + |
890 |
< |
" did not terminate in a timely manner"); |
888 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
889 |
> |
try { |
890 |
> |
threadFail("ExecutorService " + pool + |
891 |
> |
" did not terminate in a timely manner"); |
892 |
> |
} finally { |
893 |
> |
// last resort, for the benefit of subsequent tests |
894 |
> |
pool.shutdownNow(); |
895 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
896 |
> |
} |
897 |
> |
} |
898 |
|
} catch (SecurityException ok) { |
899 |
|
// Allowed in case test doesn't have privs |
900 |
|
} catch (InterruptedException fail) { |
901 |
< |
fail("Unexpected InterruptedException"); |
901 |
> |
threadFail("Unexpected InterruptedException"); |
902 |
|
} |
903 |
|
} |
904 |
|
|
911 |
|
* necessarily individually slow because they must block. |
912 |
|
*/ |
913 |
|
void testInParallel(Action ... actions) { |
914 |
< |
try (PoolCloser<ExecutorService> poolCloser |
915 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
783 |
< |
ExecutorService pool = poolCloser.pool; |
914 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
915 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
916 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
917 |
|
for (final Action action : actions) |
918 |
|
futures.add(pool.submit(new CheckedRunnable() { |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
< |
* A debugging tool to print all stack traces, as jstack does. |
932 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
933 |
|
* Uninteresting threads are filtered out. |
934 |
|
*/ |
935 |
< |
static void threadDump() { |
935 |
> |
static void dumpTestThreads() { |
936 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
937 |
|
System.err.println("------ stacktrace dump start ------"); |
938 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
945 |
|
if ("Finalizer".equals(name) |
946 |
|
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
947 |
|
continue; |
948 |
+ |
if ("checkForWedgedTest".equals(name)) |
949 |
+ |
continue; |
950 |
|
System.err.print(info); |
951 |
|
} |
952 |
|
System.err.println("------ stacktrace dump end ------"); |
969 |
|
delay(millis); |
970 |
|
assertTrue(thread.isAlive()); |
971 |
|
} catch (InterruptedException fail) { |
972 |
< |
fail("Unexpected InterruptedException"); |
972 |
> |
threadFail("Unexpected InterruptedException"); |
973 |
|
} |
974 |
|
} |
975 |
|
|
991 |
|
for (Thread thread : threads) |
992 |
|
assertTrue(thread.isAlive()); |
993 |
|
} catch (InterruptedException fail) { |
994 |
< |
fail("Unexpected InterruptedException"); |
994 |
> |
threadFail("Unexpected InterruptedException"); |
995 |
|
} |
996 |
|
} |
997 |
|
|
1269 |
|
} finally { |
1270 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1271 |
|
t.interrupt(); |
1272 |
< |
fail("Test timed out"); |
1272 |
> |
threadFail("timed out waiting for thread to terminate"); |
1273 |
|
} |
1274 |
|
} |
1275 |
|
} |
1417 |
|
}}; |
1418 |
|
} |
1419 |
|
|
1420 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1421 |
< |
return new CheckedRunnable() { |
1422 |
< |
public void realRun() throws InterruptedException { |
1423 |
< |
await(latch); |
1424 |
< |
}}; |
1420 |
> |
class LatchAwaiter extends CheckedRunnable { |
1421 |
> |
static final int NEW = 0; |
1422 |
> |
static final int RUNNING = 1; |
1423 |
> |
static final int DONE = 2; |
1424 |
> |
final CountDownLatch latch; |
1425 |
> |
int state = NEW; |
1426 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1427 |
> |
public void realRun() throws InterruptedException { |
1428 |
> |
state = 1; |
1429 |
> |
await(latch); |
1430 |
> |
state = 2; |
1431 |
> |
} |
1432 |
> |
} |
1433 |
> |
|
1434 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1435 |
> |
return new LatchAwaiter(latch); |
1436 |
|
} |
1437 |
|
|
1438 |
|
public void await(CountDownLatch latch) { |
1439 |
|
try { |
1440 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1440 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1441 |
> |
fail("timed out waiting for CountDownLatch for " |
1442 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1443 |
|
} catch (Throwable fail) { |
1444 |
|
threadUnexpectedException(fail); |
1445 |
|
} |
1447 |
|
|
1448 |
|
public void await(Semaphore semaphore) { |
1449 |
|
try { |
1450 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1450 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1451 |
> |
fail("timed out waiting for Semaphore for " |
1452 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1453 |
|
} catch (Throwable fail) { |
1454 |
|
threadUnexpectedException(fail); |
1455 |
|
} |