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; |
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; |
61 |
|
import java.util.concurrent.ThreadFactory; |
62 |
|
import java.util.concurrent.ThreadPoolExecutor; |
63 |
|
import java.util.concurrent.TimeoutException; |
64 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
65 |
|
import java.util.concurrent.atomic.AtomicReference; |
66 |
+ |
import java.util.regex.Matcher; |
67 |
|
import java.util.regex.Pattern; |
68 |
|
|
69 |
|
import junit.framework.AssertionFailedError; |
121 |
|
* methods as there are exceptions the method can throw. Sometimes |
122 |
|
* there are multiple tests per JSR166 method when the different |
123 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
124 |
< |
* cover multiple methods when they cannot be tested in |
113 |
< |
* isolation. |
124 |
> |
* cover multiple methods when they cannot be tested in isolation. |
125 |
|
* |
126 |
|
* <li>The documentation style for testcases is to provide as javadoc |
127 |
|
* a simple sentence or two describing the property that the testcase |
184 |
|
private static final int suiteRuns = |
185 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
186 |
|
|
187 |
+ |
/** |
188 |
+ |
* Returns the value of the system property, or NaN if not defined. |
189 |
+ |
*/ |
190 |
+ |
private static float systemPropertyValue(String name) { |
191 |
+ |
String floatString = System.getProperty(name); |
192 |
+ |
if (floatString == null) |
193 |
+ |
return Float.NaN; |
194 |
+ |
try { |
195 |
+ |
return Float.parseFloat(floatString); |
196 |
+ |
} catch (NumberFormatException ex) { |
197 |
+ |
throw new IllegalArgumentException( |
198 |
+ |
String.format("Bad float value in system property %s=%s", |
199 |
+ |
name, floatString)); |
200 |
+ |
} |
201 |
+ |
} |
202 |
+ |
|
203 |
+ |
/** |
204 |
+ |
* The scaling factor to apply to standard delays used in tests. |
205 |
+ |
* May be initialized from any of: |
206 |
+ |
* - the "jsr166.delay.factor" system property |
207 |
+ |
* - the "test.timeout.factor" system property (as used by jtreg) |
208 |
+ |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
209 |
+ |
* - hard-coded fuzz factor when using a known slowpoke VM |
210 |
+ |
*/ |
211 |
+ |
private static final float delayFactor = delayFactor(); |
212 |
+ |
|
213 |
+ |
private static float delayFactor() { |
214 |
+ |
float x; |
215 |
+ |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
216 |
+ |
return x; |
217 |
+ |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
218 |
+ |
return x; |
219 |
+ |
String prop = System.getProperty("java.vm.version"); |
220 |
+ |
if (prop != null && prop.matches(".*debug.*")) |
221 |
+ |
return 4.0f; // How much slower is fastdebug than product?! |
222 |
+ |
return 1.0f; |
223 |
+ |
} |
224 |
+ |
|
225 |
|
public JSR166TestCase() { super(); } |
226 |
|
public JSR166TestCase(String name) { super(name); } |
227 |
|
|
237 |
|
return (regex == null) ? null : Pattern.compile(regex); |
238 |
|
} |
239 |
|
|
240 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
241 |
|
static volatile TestCase currentTestCase; |
242 |
+ |
// static volatile int currentRun = 0; |
243 |
|
static { |
244 |
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
245 |
< |
// avoid spurious reports with enormous runsPerTest |
246 |
< |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
245 |
> |
// Avoid spurious reports with enormous runsPerTest. |
246 |
> |
// A single test case run should never take more than 1 second. |
247 |
> |
// But let's cap it at the high end too ... |
248 |
> |
final int timeoutMinutes = |
249 |
> |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
250 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
251 |
|
try { MINUTES.sleep(timeoutMinutes); } |
252 |
|
catch (InterruptedException unexpected) { break; } |
253 |
|
if (lastTestCase == currentTestCase) { |
254 |
< |
System.err.println |
255 |
< |
("Looks like we're stuck running test: " |
256 |
< |
+ lastTestCase); |
254 |
> |
System.err.printf( |
255 |
> |
"Looks like we're stuck running test: %s%n", |
256 |
> |
lastTestCase); |
257 |
> |
// System.err.printf( |
258 |
> |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
259 |
> |
// lastTestCase, currentRun, runsPerTest); |
260 |
> |
// System.err.println("availableProcessors=" + |
261 |
> |
// Runtime.getRuntime().availableProcessors()); |
262 |
> |
// System.err.printf("cpu model = %s%n", cpuModel()); |
263 |
|
dumpTestThreads(); |
264 |
+ |
// one stack dump is probably enough; more would be spam |
265 |
+ |
break; |
266 |
|
} |
267 |
|
lastTestCase = currentTestCase; |
268 |
|
}}}; |
271 |
|
thread.start(); |
272 |
|
} |
273 |
|
|
274 |
+ |
// public static String cpuModel() { |
275 |
+ |
// try { |
276 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
277 |
+ |
// .matcher(new String( |
278 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
279 |
+ |
// matcher.find(); |
280 |
+ |
// return matcher.group(1); |
281 |
+ |
// } catch (Exception ex) { return null; } |
282 |
+ |
// } |
283 |
+ |
|
284 |
|
public void runBare() throws Throwable { |
285 |
|
currentTestCase = this; |
286 |
|
if (methodFilter == null |
290 |
|
|
291 |
|
protected void runTest() throws Throwable { |
292 |
|
for (int i = 0; i < runsPerTest; i++) { |
293 |
+ |
// currentRun = i; |
294 |
|
if (profileTests) |
295 |
|
runTestProfiled(); |
296 |
|
else |
319 |
|
main(suite(), args); |
320 |
|
} |
321 |
|
|
322 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
323 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
324 |
+ |
long runTime; |
325 |
+ |
public void startTest(Test test) {} |
326 |
+ |
protected void printHeader(long runTime) { |
327 |
+ |
this.runTime = runTime; // defer printing for later |
328 |
+ |
} |
329 |
+ |
protected void printFooter(TestResult result) { |
330 |
+ |
if (result.wasSuccessful()) { |
331 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
332 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
333 |
+ |
} else { |
334 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
335 |
+ |
super.printFooter(result); |
336 |
+ |
} |
337 |
+ |
} |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
/** |
341 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
342 |
+ |
* fluff, like printing a "." for each test case. |
343 |
+ |
*/ |
344 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
345 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
346 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
347 |
+ |
return runner; |
348 |
+ |
} |
349 |
+ |
|
350 |
|
/** |
351 |
|
* Runs all unit tests in the given test suite. |
352 |
|
* Actual behavior influenced by jsr166.* system properties. |
358 |
|
System.setSecurityManager(new SecurityManager()); |
359 |
|
} |
360 |
|
for (int i = 0; i < suiteRuns; i++) { |
361 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
361 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
362 |
|
if (!result.wasSuccessful()) |
363 |
|
System.exit(1); |
364 |
|
System.gc(); |
595 |
|
public static long LONG_DELAY_MS; |
596 |
|
|
597 |
|
/** |
598 |
< |
* Returns the shortest timed delay. This could |
599 |
< |
* be reimplemented to use for example a Property. |
598 |
> |
* Returns the shortest timed delay. This can be scaled up for |
599 |
> |
* slow machines using the jsr166.delay.factor system property, |
600 |
> |
* or via jtreg's -timeoutFactor: flag. |
601 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
602 |
|
*/ |
603 |
|
protected long getShortDelay() { |
604 |
< |
return 50; |
604 |
> |
return (long) (50 * delayFactor); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
696 |
> |
* Finds missing PoolCleaners |
697 |
|
*/ |
698 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
699 |
|
Thread[] survivors = new Thread[7]; |
852 |
|
/** |
853 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
854 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
855 |
< |
* until time elapses. |
855 |
> |
* until time elapses. Ensures that the given time, as measured |
856 |
> |
* by System.nanoTime(), has elapsed. |
857 |
|
*/ |
858 |
|
static void delay(long millis) throws InterruptedException { |
859 |
< |
long startTime = System.nanoTime(); |
860 |
< |
long ns = millis * 1000 * 1000; |
861 |
< |
for (;;) { |
859 |
> |
long nanos = millis * (1000 * 1000); |
860 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
861 |
> |
do { |
862 |
|
if (millis > 0L) |
863 |
|
Thread.sleep(millis); |
864 |
|
else // too short to sleep |
865 |
|
Thread.yield(); |
866 |
< |
long d = ns - (System.nanoTime() - startTime); |
867 |
< |
if (d > 0L) |
868 |
< |
millis = d / (1000 * 1000); |
765 |
< |
else |
766 |
< |
break; |
767 |
< |
} |
866 |
> |
nanos = wakeupTime - System.nanoTime(); |
867 |
> |
millis = nanos / (1000 * 1000); |
868 |
> |
} while (nanos >= 0L); |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
|
* Allows use of try-with-resources with per-test thread pools. |
873 |
|
*/ |
874 |
< |
class PoolCloser<T extends ExecutorService> |
875 |
< |
implements AutoCloseable { |
876 |
< |
public final T pool; |
776 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
874 |
> |
class PoolCleaner implements AutoCloseable { |
875 |
> |
private final ExecutorService pool; |
876 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
877 |
|
public void close() { joinPool(pool); } |
878 |
|
} |
879 |
|
|
880 |
|
/** |
881 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
882 |
+ |
*/ |
883 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
884 |
+ |
private final Runnable releaser; |
885 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
886 |
+ |
super(pool); |
887 |
+ |
this.releaser = releaser; |
888 |
+ |
} |
889 |
+ |
public void close() { |
890 |
+ |
try { |
891 |
+ |
releaser.run(); |
892 |
+ |
} finally { |
893 |
+ |
super.close(); |
894 |
+ |
} |
895 |
+ |
} |
896 |
+ |
} |
897 |
+ |
|
898 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
899 |
+ |
return new PoolCleaner(pool); |
900 |
+ |
} |
901 |
+ |
|
902 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
903 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
904 |
+ |
} |
905 |
+ |
|
906 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
907 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
908 |
+ |
} |
909 |
+ |
|
910 |
+ |
Runnable releaser(final CountDownLatch latch) { |
911 |
+ |
return new Runnable() { public void run() { |
912 |
+ |
do { latch.countDown(); } |
913 |
+ |
while (latch.getCount() > 0); |
914 |
+ |
}}; |
915 |
+ |
} |
916 |
+ |
|
917 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
918 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
919 |
+ |
} |
920 |
+ |
|
921 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
922 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
923 |
+ |
} |
924 |
+ |
|
925 |
+ |
/** |
926 |
|
* Waits out termination of a thread pool or fails doing so. |
927 |
|
*/ |
928 |
|
void joinPool(ExecutorService pool) { |
935 |
|
} finally { |
936 |
|
// last resort, for the benefit of subsequent tests |
937 |
|
pool.shutdownNow(); |
938 |
< |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
938 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
939 |
|
} |
940 |
|
} |
941 |
|
} catch (SecurityException ok) { |
954 |
|
* necessarily individually slow because they must block. |
955 |
|
*/ |
956 |
|
void testInParallel(Action ... actions) { |
957 |
< |
try (PoolCloser<ExecutorService> poolCloser |
958 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
814 |
< |
ExecutorService pool = poolCloser.pool; |
957 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
958 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
959 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
960 |
|
for (final Action action : actions) |
961 |
|
futures.add(pool.submit(new CheckedRunnable() { |
1312 |
|
} finally { |
1313 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1314 |
|
t.interrupt(); |
1315 |
< |
fail("Test timed out"); |
1315 |
> |
threadFail("timed out waiting for thread to terminate"); |
1316 |
|
} |
1317 |
|
} |
1318 |
|
} |
1460 |
|
}}; |
1461 |
|
} |
1462 |
|
|
1463 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1464 |
< |
return new CheckedRunnable() { |
1465 |
< |
public void realRun() throws InterruptedException { |
1466 |
< |
await(latch); |
1467 |
< |
}}; |
1463 |
> |
class LatchAwaiter extends CheckedRunnable { |
1464 |
> |
static final int NEW = 0; |
1465 |
> |
static final int RUNNING = 1; |
1466 |
> |
static final int DONE = 2; |
1467 |
> |
final CountDownLatch latch; |
1468 |
> |
int state = NEW; |
1469 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1470 |
> |
public void realRun() throws InterruptedException { |
1471 |
> |
state = 1; |
1472 |
> |
await(latch); |
1473 |
> |
state = 2; |
1474 |
> |
} |
1475 |
|
} |
1476 |
|
|
1477 |
< |
public void await(CountDownLatch latch) { |
1477 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1478 |
> |
return new LatchAwaiter(latch); |
1479 |
> |
} |
1480 |
> |
|
1481 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1482 |
|
try { |
1483 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1483 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1484 |
> |
fail("timed out waiting for CountDownLatch for " |
1485 |
> |
+ (timeoutMillis/1000) + " sec"); |
1486 |
|
} catch (Throwable fail) { |
1487 |
|
threadUnexpectedException(fail); |
1488 |
|
} |
1489 |
|
} |
1490 |
|
|
1491 |
+ |
public void await(CountDownLatch latch) { |
1492 |
+ |
await(latch, LONG_DELAY_MS); |
1493 |
+ |
} |
1494 |
+ |
|
1495 |
|
public void await(Semaphore semaphore) { |
1496 |
|
try { |
1497 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1497 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1498 |
> |
fail("timed out waiting for Semaphore for " |
1499 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1500 |
|
} catch (Throwable fail) { |
1501 |
|
threadUnexpectedException(fail); |
1502 |
|
} |