6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
/* |
10 |
+ |
* @test |
11 |
+ |
* @summary JSR-166 tck tests (conformance testing mode) |
12 |
+ |
* @build * |
13 |
+ |
* @modules java.management |
14 |
+ |
* @run junit/othervm/timeout=1000 JSR166TestCase |
15 |
+ |
*/ |
16 |
+ |
|
17 |
+ |
/* |
18 |
+ |
* @test |
19 |
+ |
* @summary JSR-166 tck tests (whitebox tests allowed) |
20 |
+ |
* @build * |
21 |
+ |
* @modules java.base/java.util.concurrent:open |
22 |
+ |
* java.management |
23 |
+ |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
24 |
+ |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 JSR166TestCase |
25 |
+ |
*/ |
26 |
+ |
|
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
29 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
38 |
|
import java.lang.reflect.Constructor; |
39 |
|
import java.lang.reflect.Method; |
40 |
|
import java.lang.reflect.Modifier; |
41 |
+ |
import java.nio.file.Files; |
42 |
+ |
import java.nio.file.Paths; |
43 |
|
import java.security.CodeSource; |
44 |
|
import java.security.Permission; |
45 |
|
import java.security.PermissionCollection; |
49 |
|
import java.security.SecurityPermission; |
50 |
|
import java.util.ArrayList; |
51 |
|
import java.util.Arrays; |
52 |
+ |
import java.util.Collection; |
53 |
+ |
import java.util.Collections; |
54 |
|
import java.util.Date; |
55 |
|
import java.util.Enumeration; |
56 |
|
import java.util.Iterator; |
70 |
|
import java.util.concurrent.RecursiveTask; |
71 |
|
import java.util.concurrent.RejectedExecutionHandler; |
72 |
|
import java.util.concurrent.Semaphore; |
73 |
+ |
import java.util.concurrent.SynchronousQueue; |
74 |
|
import java.util.concurrent.ThreadFactory; |
75 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
76 |
|
import java.util.concurrent.ThreadPoolExecutor; |
77 |
|
import java.util.concurrent.TimeoutException; |
78 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
79 |
|
import java.util.concurrent.atomic.AtomicReference; |
80 |
+ |
import java.util.regex.Matcher; |
81 |
|
import java.util.regex.Pattern; |
82 |
|
|
83 |
|
import junit.framework.AssertionFailedError; |
135 |
|
* methods as there are exceptions the method can throw. Sometimes |
136 |
|
* there are multiple tests per JSR166 method when the different |
137 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
138 |
< |
* cover multiple methods when they cannot be tested in |
113 |
< |
* isolation. |
138 |
> |
* cover multiple methods when they cannot be tested in isolation. |
139 |
|
* |
140 |
|
* <li>The documentation style for testcases is to provide as javadoc |
141 |
|
* a simple sentence or two describing the property that the testcase |
198 |
|
private static final int suiteRuns = |
199 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
200 |
|
|
201 |
+ |
/** |
202 |
+ |
* Returns the value of the system property, or NaN if not defined. |
203 |
+ |
*/ |
204 |
+ |
private static float systemPropertyValue(String name) { |
205 |
+ |
String floatString = System.getProperty(name); |
206 |
+ |
if (floatString == null) |
207 |
+ |
return Float.NaN; |
208 |
+ |
try { |
209 |
+ |
return Float.parseFloat(floatString); |
210 |
+ |
} catch (NumberFormatException ex) { |
211 |
+ |
throw new IllegalArgumentException( |
212 |
+ |
String.format("Bad float value in system property %s=%s", |
213 |
+ |
name, floatString)); |
214 |
+ |
} |
215 |
+ |
} |
216 |
+ |
|
217 |
+ |
/** |
218 |
+ |
* The scaling factor to apply to standard delays used in tests. |
219 |
+ |
* May be initialized from any of: |
220 |
+ |
* - the "jsr166.delay.factor" system property |
221 |
+ |
* - the "test.timeout.factor" system property (as used by jtreg) |
222 |
+ |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
223 |
+ |
* - hard-coded fuzz factor when using a known slowpoke VM |
224 |
+ |
*/ |
225 |
+ |
private static final float delayFactor = delayFactor(); |
226 |
+ |
|
227 |
+ |
private static float delayFactor() { |
228 |
+ |
float x; |
229 |
+ |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
230 |
+ |
return x; |
231 |
+ |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
232 |
+ |
return x; |
233 |
+ |
String prop = System.getProperty("java.vm.version"); |
234 |
+ |
if (prop != null && prop.matches(".*debug.*")) |
235 |
+ |
return 4.0f; // How much slower is fastdebug than product?! |
236 |
+ |
return 1.0f; |
237 |
+ |
} |
238 |
+ |
|
239 |
|
public JSR166TestCase() { super(); } |
240 |
|
public JSR166TestCase(String name) { super(name); } |
241 |
|
|
251 |
|
return (regex == null) ? null : Pattern.compile(regex); |
252 |
|
} |
253 |
|
|
254 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
255 |
|
static volatile TestCase currentTestCase; |
256 |
+ |
// static volatile int currentRun = 0; |
257 |
|
static { |
258 |
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
259 |
< |
// avoid spurious reports with enormous runsPerTest |
260 |
< |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
259 |
> |
// Avoid spurious reports with enormous runsPerTest. |
260 |
> |
// A single test case run should never take more than 1 second. |
261 |
> |
// But let's cap it at the high end too ... |
262 |
> |
final int timeoutMinutes = |
263 |
> |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
264 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
265 |
|
try { MINUTES.sleep(timeoutMinutes); } |
266 |
|
catch (InterruptedException unexpected) { break; } |
267 |
|
if (lastTestCase == currentTestCase) { |
268 |
< |
System.err.println |
269 |
< |
("Looks like we're stuck running test: " |
270 |
< |
+ lastTestCase); |
268 |
> |
System.err.printf( |
269 |
> |
"Looks like we're stuck running test: %s%n", |
270 |
> |
lastTestCase); |
271 |
> |
// System.err.printf( |
272 |
> |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
273 |
> |
// lastTestCase, currentRun, runsPerTest); |
274 |
> |
// System.err.println("availableProcessors=" + |
275 |
> |
// Runtime.getRuntime().availableProcessors()); |
276 |
> |
// System.err.printf("cpu model = %s%n", cpuModel()); |
277 |
|
dumpTestThreads(); |
278 |
+ |
// one stack dump is probably enough; more would be spam |
279 |
+ |
break; |
280 |
|
} |
281 |
|
lastTestCase = currentTestCase; |
282 |
|
}}}; |
285 |
|
thread.start(); |
286 |
|
} |
287 |
|
|
288 |
+ |
// public static String cpuModel() { |
289 |
+ |
// try { |
290 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
291 |
+ |
// .matcher(new String( |
292 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
293 |
+ |
// matcher.find(); |
294 |
+ |
// return matcher.group(1); |
295 |
+ |
// } catch (Exception ex) { return null; } |
296 |
+ |
// } |
297 |
+ |
|
298 |
|
public void runBare() throws Throwable { |
299 |
|
currentTestCase = this; |
300 |
|
if (methodFilter == null |
304 |
|
|
305 |
|
protected void runTest() throws Throwable { |
306 |
|
for (int i = 0; i < runsPerTest; i++) { |
307 |
+ |
// currentRun = i; |
308 |
|
if (profileTests) |
309 |
|
runTestProfiled(); |
310 |
|
else |
333 |
|
main(suite(), args); |
334 |
|
} |
335 |
|
|
336 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
337 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
338 |
+ |
long runTime; |
339 |
+ |
public void startTest(Test test) {} |
340 |
+ |
protected void printHeader(long runTime) { |
341 |
+ |
this.runTime = runTime; // defer printing for later |
342 |
+ |
} |
343 |
+ |
protected void printFooter(TestResult result) { |
344 |
+ |
if (result.wasSuccessful()) { |
345 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
346 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
347 |
+ |
} else { |
348 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
349 |
+ |
super.printFooter(result); |
350 |
+ |
} |
351 |
+ |
} |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
/** |
355 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
356 |
+ |
* fluff, like printing a "." for each test case. |
357 |
+ |
*/ |
358 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
359 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
360 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
361 |
+ |
return runner; |
362 |
+ |
} |
363 |
+ |
|
364 |
|
/** |
365 |
|
* Runs all unit tests in the given test suite. |
366 |
|
* Actual behavior influenced by jsr166.* system properties. |
372 |
|
System.setSecurityManager(new SecurityManager()); |
373 |
|
} |
374 |
|
for (int i = 0; i < suiteRuns; i++) { |
375 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
375 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
376 |
|
if (!result.wasSuccessful()) |
377 |
|
System.exit(1); |
378 |
|
System.gc(); |
456 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
457 |
|
ArrayBlockingQueueTest.suite(), |
458 |
|
ArrayDequeTest.suite(), |
459 |
+ |
ArrayListTest.suite(), |
460 |
|
AtomicBooleanTest.suite(), |
461 |
|
AtomicIntegerArrayTest.suite(), |
462 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
479 |
|
CopyOnWriteArrayListTest.suite(), |
480 |
|
CopyOnWriteArraySetTest.suite(), |
481 |
|
CountDownLatchTest.suite(), |
482 |
+ |
CountedCompleterTest.suite(), |
483 |
|
CyclicBarrierTest.suite(), |
484 |
|
DelayQueueTest.suite(), |
485 |
|
EntryTest.suite(), |
508 |
|
TreeMapTest.suite(), |
509 |
|
TreeSetTest.suite(), |
510 |
|
TreeSubMapTest.suite(), |
511 |
< |
TreeSubSetTest.suite()); |
511 |
> |
TreeSubSetTest.suite(), |
512 |
> |
VectorTest.suite()); |
513 |
|
|
514 |
|
// Java8+ test classes |
515 |
|
if (atLeastJava8()) { |
516 |
|
String[] java8TestClassNames = { |
517 |
+ |
"ArrayDeque8Test", |
518 |
|
"Atomic8Test", |
519 |
|
"CompletableFutureTest", |
520 |
|
"ConcurrentHashMap8Test", |
521 |
< |
"CountedCompleterTest", |
521 |
> |
"CountedCompleter8Test", |
522 |
|
"DoubleAccumulatorTest", |
523 |
|
"DoubleAdderTest", |
524 |
|
"ForkJoinPool8Test", |
529 |
|
"StampedLockTest", |
530 |
|
"SubmissionPublisherTest", |
531 |
|
"ThreadLocalRandom8Test", |
532 |
+ |
"TimeUnit8Test", |
533 |
|
}; |
534 |
|
addNamedTestClasses(suite, java8TestClassNames); |
535 |
|
} |
537 |
|
// Java9+ test classes |
538 |
|
if (atLeastJava9()) { |
539 |
|
String[] java9TestClassNames = { |
540 |
< |
// Currently empty, but expecting varhandle tests |
540 |
> |
"AtomicBoolean9Test", |
541 |
> |
"AtomicInteger9Test", |
542 |
> |
"AtomicIntegerArray9Test", |
543 |
> |
"AtomicLong9Test", |
544 |
> |
"AtomicLongArray9Test", |
545 |
> |
"AtomicReference9Test", |
546 |
> |
"AtomicReferenceArray9Test", |
547 |
> |
"ExecutorCompletionService9Test", |
548 |
|
}; |
549 |
|
addNamedTestClasses(suite, java9TestClassNames); |
550 |
|
} |
621 |
|
public static long LONG_DELAY_MS; |
622 |
|
|
623 |
|
/** |
624 |
< |
* Returns the shortest timed delay. This could |
625 |
< |
* be reimplemented to use for example a Property. |
624 |
> |
* Returns the shortest timed delay. This can be scaled up for |
625 |
> |
* slow machines using the jsr166.delay.factor system property, |
626 |
> |
* or via jtreg's -timeoutFactor: flag. |
627 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
628 |
|
*/ |
629 |
|
protected long getShortDelay() { |
630 |
< |
return 50; |
630 |
> |
return (long) (50 * delayFactor); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
722 |
> |
* Finds missing PoolCleaners |
723 |
|
*/ |
724 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
725 |
|
Thread[] survivors = new Thread[7]; |
878 |
|
/** |
879 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
880 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
881 |
< |
* until time elapses. |
881 |
> |
* until time elapses. Ensures that the given time, as measured |
882 |
> |
* by System.nanoTime(), has elapsed. |
883 |
|
*/ |
884 |
|
static void delay(long millis) throws InterruptedException { |
885 |
< |
long startTime = System.nanoTime(); |
886 |
< |
long ns = millis * 1000 * 1000; |
887 |
< |
for (;;) { |
885 |
> |
long nanos = millis * (1000 * 1000); |
886 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
887 |
> |
do { |
888 |
|
if (millis > 0L) |
889 |
|
Thread.sleep(millis); |
890 |
|
else // too short to sleep |
891 |
|
Thread.yield(); |
892 |
< |
long d = ns - (System.nanoTime() - startTime); |
893 |
< |
if (d > 0L) |
894 |
< |
millis = d / (1000 * 1000); |
765 |
< |
else |
766 |
< |
break; |
767 |
< |
} |
892 |
> |
nanos = wakeupTime - System.nanoTime(); |
893 |
> |
millis = nanos / (1000 * 1000); |
894 |
> |
} while (nanos >= 0L); |
895 |
|
} |
896 |
|
|
897 |
|
/** |
903 |
|
public void close() { joinPool(pool); } |
904 |
|
} |
905 |
|
|
906 |
+ |
/** |
907 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
908 |
+ |
*/ |
909 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
910 |
+ |
private final Runnable releaser; |
911 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
912 |
+ |
super(pool); |
913 |
+ |
this.releaser = releaser; |
914 |
+ |
} |
915 |
+ |
public void close() { |
916 |
+ |
try { |
917 |
+ |
releaser.run(); |
918 |
+ |
} finally { |
919 |
+ |
super.close(); |
920 |
+ |
} |
921 |
+ |
} |
922 |
+ |
} |
923 |
+ |
|
924 |
|
PoolCleaner cleaner(ExecutorService pool) { |
925 |
|
return new PoolCleaner(pool); |
926 |
|
} |
927 |
|
|
928 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
929 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
930 |
+ |
} |
931 |
+ |
|
932 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
933 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
934 |
+ |
} |
935 |
+ |
|
936 |
+ |
Runnable releaser(final CountDownLatch latch) { |
937 |
+ |
return new Runnable() { public void run() { |
938 |
+ |
do { latch.countDown(); } |
939 |
+ |
while (latch.getCount() > 0); |
940 |
+ |
}}; |
941 |
+ |
} |
942 |
+ |
|
943 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
944 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
945 |
+ |
} |
946 |
+ |
|
947 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
948 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
949 |
+ |
} |
950 |
+ |
|
951 |
|
/** |
952 |
|
* Waits out termination of a thread pool or fails doing so. |
953 |
|
*/ |
961 |
|
} finally { |
962 |
|
// last resort, for the benefit of subsequent tests |
963 |
|
pool.shutdownNow(); |
964 |
< |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
964 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
965 |
|
} |
966 |
|
} |
967 |
|
} catch (SecurityException ok) { |
971 |
|
} |
972 |
|
} |
973 |
|
|
974 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
974 |
> |
/** |
975 |
> |
* Like Runnable, but with the freedom to throw anything. |
976 |
> |
* junit folks had the same idea: |
977 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
978 |
> |
*/ |
979 |
|
interface Action { public void run() throws Throwable; } |
980 |
|
|
981 |
|
/** |
1006 |
|
* Uninteresting threads are filtered out. |
1007 |
|
*/ |
1008 |
|
static void dumpTestThreads() { |
1009 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1010 |
+ |
if (sm != null) { |
1011 |
+ |
try { |
1012 |
+ |
System.setSecurityManager(null); |
1013 |
+ |
} catch (SecurityException giveUp) { |
1014 |
+ |
return; |
1015 |
+ |
} |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1019 |
|
System.err.println("------ stacktrace dump start ------"); |
1020 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1021 |
< |
String name = info.getThreadName(); |
1021 |
> |
final String name = info.getThreadName(); |
1022 |
> |
String lockName; |
1023 |
|
if ("Signal Dispatcher".equals(name)) |
1024 |
|
continue; |
1025 |
|
if ("Reference Handler".equals(name) |
1026 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1026 |
> |
&& (lockName = info.getLockName()) != null |
1027 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1028 |
|
continue; |
1029 |
|
if ("Finalizer".equals(name) |
1030 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1030 |
> |
&& (lockName = info.getLockName()) != null |
1031 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1032 |
|
continue; |
1033 |
|
if ("checkForWedgedTest".equals(name)) |
1034 |
|
continue; |
1035 |
|
System.err.print(info); |
1036 |
|
} |
1037 |
|
System.err.println("------ stacktrace dump end ------"); |
1038 |
+ |
|
1039 |
+ |
if (sm != null) System.setSecurityManager(sm); |
1040 |
|
} |
1041 |
|
|
1042 |
|
/** |
1253 |
|
* Sleeps until the given time has elapsed. |
1254 |
|
* Throws AssertionFailedError if interrupted. |
1255 |
|
*/ |
1256 |
< |
void sleep(long millis) { |
1256 |
> |
static void sleep(long millis) { |
1257 |
|
try { |
1258 |
|
delay(millis); |
1259 |
|
} catch (InterruptedException fail) { |
1269 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1270 |
|
*/ |
1271 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1272 |
< |
long startTime = System.nanoTime(); |
1272 |
> |
long startTime = 0L; |
1273 |
|
for (;;) { |
1274 |
|
Thread.State s = thread.getState(); |
1275 |
|
if (s == Thread.State.BLOCKED || |
1278 |
|
return; |
1279 |
|
else if (s == Thread.State.TERMINATED) |
1280 |
|
fail("Unexpected thread termination"); |
1281 |
+ |
else if (startTime == 0L) |
1282 |
+ |
startTime = System.nanoTime(); |
1283 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1284 |
|
threadAssertTrue(thread.isAlive()); |
1285 |
|
return; |
1358 |
|
} finally { |
1359 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1360 |
|
t.interrupt(); |
1361 |
< |
fail("Test timed out"); |
1361 |
> |
threadFail("timed out waiting for thread to terminate"); |
1362 |
|
} |
1363 |
|
} |
1364 |
|
} |
1507 |
|
} |
1508 |
|
|
1509 |
|
class LatchAwaiter extends CheckedRunnable { |
1510 |
< |
final static int NEW = 0; |
1511 |
< |
final static int RUNNING = 1; |
1512 |
< |
final static int DONE = 2; |
1510 |
> |
static final int NEW = 0; |
1511 |
> |
static final int RUNNING = 1; |
1512 |
> |
static final int DONE = 2; |
1513 |
|
final CountDownLatch latch; |
1514 |
|
int state = NEW; |
1515 |
|
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1519 |
|
state = 2; |
1520 |
|
} |
1521 |
|
} |
1522 |
< |
|
1522 |
> |
|
1523 |
|
public LatchAwaiter awaiter(CountDownLatch latch) { |
1524 |
|
return new LatchAwaiter(latch); |
1525 |
|
} |
1526 |
|
|
1527 |
< |
public void await(CountDownLatch latch) { |
1527 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1528 |
|
try { |
1529 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1529 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1530 |
> |
fail("timed out waiting for CountDownLatch for " |
1531 |
> |
+ (timeoutMillis/1000) + " sec"); |
1532 |
|
} catch (Throwable fail) { |
1533 |
|
threadUnexpectedException(fail); |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
+ |
public void await(CountDownLatch latch) { |
1538 |
+ |
await(latch, LONG_DELAY_MS); |
1539 |
+ |
} |
1540 |
+ |
|
1541 |
|
public void await(Semaphore semaphore) { |
1542 |
|
try { |
1543 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1543 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1544 |
> |
fail("timed out waiting for Semaphore for " |
1545 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1546 |
|
} catch (Throwable fail) { |
1547 |
|
threadUnexpectedException(fail); |
1548 |
|
} |
1772 |
|
* A CyclicBarrier that uses timed await and fails with |
1773 |
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1774 |
|
*/ |
1775 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1775 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1776 |
|
public CheckedBarrier(int parties) { super(parties); } |
1777 |
|
|
1778 |
|
public int await() { |
1836 |
|
} |
1837 |
|
} |
1838 |
|
|
1839 |
+ |
void assertImmutable(final Object o) { |
1840 |
+ |
if (o instanceof Collection) { |
1841 |
+ |
assertThrows( |
1842 |
+ |
UnsupportedOperationException.class, |
1843 |
+ |
new Runnable() { public void run() { |
1844 |
+ |
((Collection) o).add(null);}}); |
1845 |
+ |
} |
1846 |
+ |
} |
1847 |
+ |
|
1848 |
|
@SuppressWarnings("unchecked") |
1849 |
|
<T> T serialClone(T o) { |
1850 |
|
try { |
1851 |
|
ObjectInputStream ois = new ObjectInputStream |
1852 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1853 |
|
T clone = (T) ois.readObject(); |
1854 |
+ |
if (o == clone) assertImmutable(o); |
1855 |
|
assertSame(o.getClass(), clone.getClass()); |
1856 |
|
return clone; |
1857 |
|
} catch (Throwable fail) { |
1860 |
|
} |
1861 |
|
} |
1862 |
|
|
1863 |
+ |
/** |
1864 |
+ |
* A version of serialClone that leaves error handling (for |
1865 |
+ |
* e.g. NotSerializableException) up to the caller. |
1866 |
+ |
*/ |
1867 |
+ |
@SuppressWarnings("unchecked") |
1868 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1869 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1870 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1871 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1872 |
+ |
oos.writeObject(o); |
1873 |
+ |
oos.flush(); |
1874 |
+ |
oos.close(); |
1875 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1876 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1877 |
+ |
T clone = (T) ois.readObject(); |
1878 |
+ |
if (o == clone) assertImmutable(o); |
1879 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1880 |
+ |
return clone; |
1881 |
+ |
} |
1882 |
+ |
|
1883 |
+ |
/** |
1884 |
+ |
* If o implements Cloneable and has a public clone method, |
1885 |
+ |
* returns a clone of o, else null. |
1886 |
+ |
*/ |
1887 |
+ |
@SuppressWarnings("unchecked") |
1888 |
+ |
<T> T cloneableClone(T o) { |
1889 |
+ |
if (!(o instanceof Cloneable)) return null; |
1890 |
+ |
final T clone; |
1891 |
+ |
try { |
1892 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1893 |
+ |
} catch (NoSuchMethodException ok) { |
1894 |
+ |
return null; |
1895 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1896 |
+ |
throw new Error(unexpected); |
1897 |
+ |
} |
1898 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1899 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1900 |
+ |
return clone; |
1901 |
+ |
} |
1902 |
+ |
|
1903 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1904 |
|
Runnable... throwingActions) { |
1905 |
|
for (Runnable throwingAction : throwingActions) { |
1928 |
|
} catch (NoSuchElementException success) {} |
1929 |
|
assertFalse(it.hasNext()); |
1930 |
|
} |
1931 |
+ |
|
1932 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1933 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1934 |
+ |
} |
1935 |
+ |
|
1936 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1937 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1938 |
+ |
} |
1939 |
+ |
|
1940 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1941 |
+ |
static final ExecutorService cachedThreadPool = |
1942 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1943 |
+ |
1000L, MILLISECONDS, |
1944 |
+ |
new SynchronousQueue<Runnable>()); |
1945 |
+ |
|
1946 |
+ |
static <T> void shuffle(T[] array) { |
1947 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1948 |
+ |
} |
1949 |
|
} |