1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
2 |
> |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
> |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
> |
* domain, as explained at |
5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
9 |
|
|
10 |
|
/* |
11 |
|
* @test |
12 |
< |
* @summary JSR-166 tck tests |
12 |
> |
* @summary JSR-166 tck tests, in a number of variations. |
13 |
> |
* The first is the conformance testing variant, |
14 |
> |
* while others also test implementation details. |
15 |
|
* @build * |
16 |
+ |
* @modules java.management |
17 |
|
* @run junit/othervm/timeout=1000 JSR166TestCase |
18 |
+ |
* @run junit/othervm/timeout=1000 |
19 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
20 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
21 |
+ |
* -Djsr166.testImplementationDetails=true |
22 |
+ |
* JSR166TestCase |
23 |
+ |
* @run junit/othervm/timeout=1000 |
24 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
25 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
26 |
+ |
* -Djsr166.testImplementationDetails=true |
27 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 |
28 |
+ |
* JSR166TestCase |
29 |
+ |
* @run junit/othervm/timeout=1000 |
30 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
31 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
32 |
+ |
* -Djsr166.testImplementationDetails=true |
33 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 |
34 |
+ |
* -Djava.util.secureRandomSeed=true |
35 |
+ |
* JSR166TestCase |
36 |
+ |
* @run junit/othervm/timeout=1000/policy=tck.policy |
37 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
38 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
39 |
+ |
* -Djsr166.testImplementationDetails=true |
40 |
+ |
* JSR166TestCase |
41 |
|
*/ |
42 |
|
|
43 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
54 |
|
import java.lang.reflect.Constructor; |
55 |
|
import java.lang.reflect.Method; |
56 |
|
import java.lang.reflect.Modifier; |
30 |
– |
import java.nio.file.Files; |
31 |
– |
import java.nio.file.Paths; |
57 |
|
import java.security.CodeSource; |
58 |
|
import java.security.Permission; |
59 |
|
import java.security.PermissionCollection; |
63 |
|
import java.security.SecurityPermission; |
64 |
|
import java.util.ArrayList; |
65 |
|
import java.util.Arrays; |
66 |
+ |
import java.util.Collection; |
67 |
+ |
import java.util.Collections; |
68 |
|
import java.util.Date; |
69 |
|
import java.util.Enumeration; |
70 |
|
import java.util.Iterator; |
84 |
|
import java.util.concurrent.RecursiveTask; |
85 |
|
import java.util.concurrent.RejectedExecutionHandler; |
86 |
|
import java.util.concurrent.Semaphore; |
87 |
+ |
import java.util.concurrent.SynchronousQueue; |
88 |
|
import java.util.concurrent.ThreadFactory; |
89 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
90 |
|
import java.util.concurrent.ThreadPoolExecutor; |
91 |
+ |
import java.util.concurrent.TimeUnit; |
92 |
|
import java.util.concurrent.TimeoutException; |
93 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
94 |
|
import java.util.concurrent.atomic.AtomicReference; |
64 |
– |
import java.util.regex.Matcher; |
95 |
|
import java.util.regex.Pattern; |
96 |
|
|
97 |
|
import junit.framework.AssertionFailedError; |
213 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
214 |
|
|
215 |
|
/** |
216 |
< |
* The scaling factor to apply to standard delays used in tests. |
216 |
> |
* Returns the value of the system property, or NaN if not defined. |
217 |
|
*/ |
218 |
< |
private static final int delayFactor = |
219 |
< |
Integer.getInteger("jsr166.delay.factor", 1); |
218 |
> |
private static float systemPropertyValue(String name) { |
219 |
> |
String floatString = System.getProperty(name); |
220 |
> |
if (floatString == null) |
221 |
> |
return Float.NaN; |
222 |
> |
try { |
223 |
> |
return Float.parseFloat(floatString); |
224 |
> |
} catch (NumberFormatException ex) { |
225 |
> |
throw new IllegalArgumentException( |
226 |
> |
String.format("Bad float value in system property %s=%s", |
227 |
> |
name, floatString)); |
228 |
> |
} |
229 |
> |
} |
230 |
> |
|
231 |
> |
/** |
232 |
> |
* The scaling factor to apply to standard delays used in tests. |
233 |
> |
* May be initialized from any of: |
234 |
> |
* - the "jsr166.delay.factor" system property |
235 |
> |
* - the "test.timeout.factor" system property (as used by jtreg) |
236 |
> |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
237 |
> |
* - hard-coded fuzz factor when using a known slowpoke VM |
238 |
> |
*/ |
239 |
> |
private static final float delayFactor = delayFactor(); |
240 |
> |
|
241 |
> |
private static float delayFactor() { |
242 |
> |
float x; |
243 |
> |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
244 |
> |
return x; |
245 |
> |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
246 |
> |
return x; |
247 |
> |
String prop = System.getProperty("java.vm.version"); |
248 |
> |
if (prop != null && prop.matches(".*debug.*")) |
249 |
> |
return 4.0f; // How much slower is fastdebug than product?! |
250 |
> |
return 1.0f; |
251 |
> |
} |
252 |
|
|
253 |
|
public JSR166TestCase() { super(); } |
254 |
|
public JSR166TestCase(String name) { super(name); } |
301 |
|
|
302 |
|
// public static String cpuModel() { |
303 |
|
// try { |
304 |
< |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
304 |
> |
// java.util.regex.Matcher matcher |
305 |
> |
// = Pattern.compile("model name\\s*: (.*)") |
306 |
|
// .matcher(new String( |
307 |
< |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
307 |
> |
// java.nio.file.Files.readAllBytes( |
308 |
> |
// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); |
309 |
|
// matcher.find(); |
310 |
|
// return matcher.group(1); |
311 |
|
// } catch (Exception ex) { return null; } |
472 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
473 |
|
ArrayBlockingQueueTest.suite(), |
474 |
|
ArrayDequeTest.suite(), |
475 |
+ |
ArrayListTest.suite(), |
476 |
|
AtomicBooleanTest.suite(), |
477 |
|
AtomicIntegerArrayTest.suite(), |
478 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
495 |
|
CopyOnWriteArrayListTest.suite(), |
496 |
|
CopyOnWriteArraySetTest.suite(), |
497 |
|
CountDownLatchTest.suite(), |
498 |
+ |
CountedCompleterTest.suite(), |
499 |
|
CyclicBarrierTest.suite(), |
500 |
|
DelayQueueTest.suite(), |
501 |
|
EntryTest.suite(), |
524 |
|
TreeMapTest.suite(), |
525 |
|
TreeSetTest.suite(), |
526 |
|
TreeSubMapTest.suite(), |
527 |
< |
TreeSubSetTest.suite()); |
527 |
> |
TreeSubSetTest.suite(), |
528 |
> |
VectorTest.suite()); |
529 |
|
|
530 |
|
// Java8+ test classes |
531 |
|
if (atLeastJava8()) { |
532 |
|
String[] java8TestClassNames = { |
533 |
+ |
"ArrayDeque8Test", |
534 |
|
"Atomic8Test", |
535 |
|
"CompletableFutureTest", |
536 |
|
"ConcurrentHashMap8Test", |
537 |
< |
"CountedCompleterTest", |
537 |
> |
"CountedCompleter8Test", |
538 |
|
"DoubleAccumulatorTest", |
539 |
|
"DoubleAdderTest", |
540 |
|
"ForkJoinPool8Test", |
541 |
|
"ForkJoinTask8Test", |
542 |
+ |
"LinkedBlockingDeque8Test", |
543 |
+ |
"LinkedBlockingQueue8Test", |
544 |
|
"LongAccumulatorTest", |
545 |
|
"LongAdderTest", |
546 |
|
"SplittableRandomTest", |
547 |
|
"StampedLockTest", |
548 |
|
"SubmissionPublisherTest", |
549 |
|
"ThreadLocalRandom8Test", |
550 |
+ |
"TimeUnit8Test", |
551 |
|
}; |
552 |
|
addNamedTestClasses(suite, java8TestClassNames); |
553 |
|
} |
555 |
|
// Java9+ test classes |
556 |
|
if (atLeastJava9()) { |
557 |
|
String[] java9TestClassNames = { |
558 |
< |
// Currently empty, but expecting varhandle tests |
558 |
> |
"AtomicBoolean9Test", |
559 |
> |
"AtomicInteger9Test", |
560 |
> |
"AtomicIntegerArray9Test", |
561 |
> |
"AtomicLong9Test", |
562 |
> |
"AtomicLongArray9Test", |
563 |
> |
"AtomicReference9Test", |
564 |
> |
"AtomicReferenceArray9Test", |
565 |
> |
"ExecutorCompletionService9Test", |
566 |
> |
"ForkJoinPool9Test", |
567 |
|
}; |
568 |
|
addNamedTestClasses(suite, java9TestClassNames); |
569 |
|
} |
574 |
|
/** Returns list of junit-style test method names in given class. */ |
575 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
576 |
|
Method[] methods = testClass.getDeclaredMethods(); |
577 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
577 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
578 |
|
for (Method method : methods) { |
579 |
|
if (method.getName().startsWith("test") |
580 |
|
&& Modifier.isPublic(method.getModifiers()) |
639 |
|
public static long MEDIUM_DELAY_MS; |
640 |
|
public static long LONG_DELAY_MS; |
641 |
|
|
642 |
+ |
private static final long RANDOM_TIMEOUT; |
643 |
+ |
private static final long RANDOM_EXPIRED_TIMEOUT; |
644 |
+ |
private static final TimeUnit RANDOM_TIMEUNIT; |
645 |
+ |
static { |
646 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
647 |
+ |
long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE }; |
648 |
+ |
RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)]; |
649 |
+ |
RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)]; |
650 |
+ |
TimeUnit[] timeUnits = TimeUnit.values(); |
651 |
+ |
RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)]; |
652 |
+ |
} |
653 |
+ |
|
654 |
+ |
/** |
655 |
+ |
* Returns a timeout for use when any value at all will do. |
656 |
+ |
*/ |
657 |
+ |
static long randomTimeout() { return RANDOM_TIMEOUT; } |
658 |
+ |
|
659 |
+ |
/** |
660 |
+ |
* Returns a timeout that means "no waiting", i.e. not positive. |
661 |
+ |
*/ |
662 |
+ |
static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; } |
663 |
+ |
|
664 |
+ |
/** |
665 |
+ |
* Returns a random non-null TimeUnit. |
666 |
+ |
*/ |
667 |
+ |
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
668 |
+ |
|
669 |
|
/** |
670 |
|
* Returns the shortest timed delay. This can be scaled up for |
671 |
< |
* slow machines using the jsr166.delay.factor system property. |
671 |
> |
* slow machines using the jsr166.delay.factor system property, |
672 |
> |
* or via jtreg's -timeoutFactor: flag. |
673 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
674 |
|
*/ |
675 |
|
protected long getShortDelay() { |
676 |
< |
return 50 * delayFactor; |
676 |
> |
return (long) (50 * delayFactor); |
677 |
|
} |
678 |
|
|
679 |
|
/** |
686 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
687 |
|
} |
688 |
|
|
689 |
+ |
private static final long TIMEOUT_DELAY_MS |
690 |
+ |
= (long) (12.0 * Math.cbrt(delayFactor)); |
691 |
+ |
|
692 |
|
/** |
693 |
< |
* Returns a timeout in milliseconds to be used in tests that |
694 |
< |
* verify that operations block or time out. |
693 |
> |
* Returns a timeout in milliseconds to be used in tests that verify |
694 |
> |
* that operations block or time out. We want this to be longer |
695 |
> |
* than the OS scheduling quantum, but not too long, so don't scale |
696 |
> |
* linearly with delayFactor; we use "crazy" cube root instead. |
697 |
|
*/ |
698 |
< |
long timeoutMillis() { |
699 |
< |
return SHORT_DELAY_MS / 4; |
698 |
> |
static long timeoutMillis() { |
699 |
> |
return TIMEOUT_DELAY_MS; |
700 |
|
} |
701 |
|
|
702 |
|
/** |
712 |
|
* The first exception encountered if any threadAssertXXX method fails. |
713 |
|
*/ |
714 |
|
private final AtomicReference<Throwable> threadFailure |
715 |
< |
= new AtomicReference<Throwable>(null); |
715 |
> |
= new AtomicReference<>(null); |
716 |
|
|
717 |
|
/** |
718 |
|
* Records an exception so that it can be rethrown later in the test |
991 |
|
}}; |
992 |
|
} |
993 |
|
|
994 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
995 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
996 |
+ |
} |
997 |
+ |
|
998 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
999 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
1000 |
+ |
} |
1001 |
+ |
|
1002 |
|
/** |
1003 |
|
* Waits out termination of a thread pool or fails doing so. |
1004 |
|
*/ |
1022 |
|
} |
1023 |
|
} |
1024 |
|
|
1025 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
1025 |
> |
/** |
1026 |
> |
* Like Runnable, but with the freedom to throw anything. |
1027 |
> |
* junit folks had the same idea: |
1028 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
1029 |
> |
*/ |
1030 |
|
interface Action { public void run() throws Throwable; } |
1031 |
|
|
1032 |
|
/** |
1057 |
|
* Uninteresting threads are filtered out. |
1058 |
|
*/ |
1059 |
|
static void dumpTestThreads() { |
1060 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1061 |
+ |
if (sm != null) { |
1062 |
+ |
try { |
1063 |
+ |
System.setSecurityManager(null); |
1064 |
+ |
} catch (SecurityException giveUp) { |
1065 |
+ |
return; |
1066 |
+ |
} |
1067 |
+ |
} |
1068 |
+ |
|
1069 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1070 |
|
System.err.println("------ stacktrace dump start ------"); |
1071 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1072 |
< |
String name = info.getThreadName(); |
1072 |
> |
final String name = info.getThreadName(); |
1073 |
> |
String lockName; |
1074 |
|
if ("Signal Dispatcher".equals(name)) |
1075 |
|
continue; |
1076 |
|
if ("Reference Handler".equals(name) |
1077 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1077 |
> |
&& (lockName = info.getLockName()) != null |
1078 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1079 |
|
continue; |
1080 |
|
if ("Finalizer".equals(name) |
1081 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1081 |
> |
&& (lockName = info.getLockName()) != null |
1082 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1083 |
|
continue; |
1084 |
|
if ("checkForWedgedTest".equals(name)) |
1085 |
|
continue; |
1086 |
|
System.err.print(info); |
1087 |
|
} |
1088 |
|
System.err.println("------ stacktrace dump end ------"); |
1089 |
+ |
|
1090 |
+ |
if (sm != null) System.setSecurityManager(sm); |
1091 |
+ |
} |
1092 |
+ |
|
1093 |
+ |
/** |
1094 |
+ |
* Checks that thread eventually enters the expected blocked thread state. |
1095 |
+ |
*/ |
1096 |
+ |
void assertThreadBlocks(Thread thread, Thread.State expected) { |
1097 |
+ |
// always sleep at least 1 ms, with high probability avoiding |
1098 |
+ |
// transitory states |
1099 |
+ |
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1100 |
+ |
try { delay(1); } |
1101 |
+ |
catch (InterruptedException fail) { |
1102 |
+ |
fail("Unexpected InterruptedException"); |
1103 |
+ |
} |
1104 |
+ |
Thread.State s = thread.getState(); |
1105 |
+ |
if (s == expected) |
1106 |
+ |
return; |
1107 |
+ |
else if (s == Thread.State.TERMINATED) |
1108 |
+ |
fail("Unexpected thread termination"); |
1109 |
+ |
} |
1110 |
+ |
fail("timed out waiting for thread to enter thread state " + expected); |
1111 |
|
} |
1112 |
|
|
1113 |
|
/** |
1114 |
|
* Checks that thread does not terminate within the default |
1115 |
|
* millisecond delay of {@code timeoutMillis()}. |
1116 |
+ |
* TODO: REMOVEME |
1117 |
|
*/ |
1118 |
|
void assertThreadStaysAlive(Thread thread) { |
1119 |
|
assertThreadStaysAlive(thread, timeoutMillis()); |
1121 |
|
|
1122 |
|
/** |
1123 |
|
* Checks that thread does not terminate within the given millisecond delay. |
1124 |
+ |
* TODO: REMOVEME |
1125 |
|
*/ |
1126 |
|
void assertThreadStaysAlive(Thread thread, long millis) { |
1127 |
|
try { |
1136 |
|
/** |
1137 |
|
* Checks that the threads do not terminate within the default |
1138 |
|
* millisecond delay of {@code timeoutMillis()}. |
1139 |
+ |
* TODO: REMOVEME |
1140 |
|
*/ |
1141 |
|
void assertThreadsStayAlive(Thread... threads) { |
1142 |
|
assertThreadsStayAlive(timeoutMillis(), threads); |
1144 |
|
|
1145 |
|
/** |
1146 |
|
* Checks that the threads do not terminate within the given millisecond delay. |
1147 |
+ |
* TODO: REMOVEME |
1148 |
|
*/ |
1149 |
|
void assertThreadsStayAlive(long millis, Thread... threads) { |
1150 |
|
try { |
1195 |
|
} |
1196 |
|
|
1197 |
|
/** |
1198 |
+ |
* The maximum number of consecutive spurious wakeups we should |
1199 |
+ |
* tolerate (from APIs like LockSupport.park) before failing a test. |
1200 |
+ |
*/ |
1201 |
+ |
static final int MAX_SPURIOUS_WAKEUPS = 10; |
1202 |
+ |
|
1203 |
+ |
/** |
1204 |
|
* The number of elements to place in collections, arrays, etc. |
1205 |
|
*/ |
1206 |
|
public static final int SIZE = 20; |
1304 |
|
} |
1305 |
|
public void refresh() {} |
1306 |
|
public String toString() { |
1307 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1307 |
> |
List<Permission> ps = new ArrayList<>(); |
1308 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1309 |
|
ps.add(e.nextElement()); |
1310 |
|
return "AdjustablePolicy with permissions " + ps; |
1334 |
|
* Sleeps until the given time has elapsed. |
1335 |
|
* Throws AssertionFailedError if interrupted. |
1336 |
|
*/ |
1337 |
< |
void sleep(long millis) { |
1337 |
> |
static void sleep(long millis) { |
1338 |
|
try { |
1339 |
|
delay(millis); |
1340 |
|
} catch (InterruptedException fail) { |
1350 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1351 |
|
*/ |
1352 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1353 |
< |
long startTime = System.nanoTime(); |
1353 |
> |
long startTime = 0L; |
1354 |
|
for (;;) { |
1355 |
|
Thread.State s = thread.getState(); |
1356 |
|
if (s == Thread.State.BLOCKED || |
1359 |
|
return; |
1360 |
|
else if (s == Thread.State.TERMINATED) |
1361 |
|
fail("Unexpected thread termination"); |
1362 |
+ |
else if (startTime == 0L) |
1363 |
+ |
startTime = System.nanoTime(); |
1364 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1365 |
|
threadAssertTrue(thread.isAlive()); |
1366 |
< |
return; |
1366 |
> |
fail("timed out waiting for thread to enter wait state"); |
1367 |
|
} |
1368 |
|
Thread.yield(); |
1369 |
|
} |
1370 |
|
} |
1371 |
|
|
1372 |
|
/** |
1373 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1374 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1373 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1374 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1375 |
> |
* and additionally satisfy the given condition. |
1376 |
> |
*/ |
1377 |
> |
void waitForThreadToEnterWaitState( |
1378 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1379 |
> |
long startTime = 0L; |
1380 |
> |
for (;;) { |
1381 |
> |
Thread.State s = thread.getState(); |
1382 |
> |
if (s == Thread.State.BLOCKED || |
1383 |
> |
s == Thread.State.WAITING || |
1384 |
> |
s == Thread.State.TIMED_WAITING) { |
1385 |
> |
try { |
1386 |
> |
if (waitingForGodot.call()) |
1387 |
> |
return; |
1388 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1389 |
> |
} |
1390 |
> |
else if (s == Thread.State.TERMINATED) |
1391 |
> |
fail("Unexpected thread termination"); |
1392 |
> |
else if (startTime == 0L) |
1393 |
> |
startTime = System.nanoTime(); |
1394 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1395 |
> |
threadAssertTrue(thread.isAlive()); |
1396 |
> |
fail("timed out waiting for thread to enter wait state"); |
1397 |
> |
} |
1398 |
> |
Thread.yield(); |
1399 |
> |
} |
1400 |
> |
} |
1401 |
> |
|
1402 |
> |
/** |
1403 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1404 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1405 |
|
*/ |
1406 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1407 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1408 |
|
} |
1409 |
|
|
1410 |
|
/** |
1411 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1412 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1413 |
+ |
* and additionally satisfy the given condition. |
1414 |
+ |
*/ |
1415 |
+ |
void waitForThreadToEnterWaitState( |
1416 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1417 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1418 |
+ |
} |
1419 |
+ |
|
1420 |
+ |
/** |
1421 |
|
* Returns the number of milliseconds since time given by |
1422 |
|
* startNanoTime, which must have been previously returned from a |
1423 |
|
* call to {@link System#nanoTime()}. |
1645 |
|
return new LatchAwaiter(latch); |
1646 |
|
} |
1647 |
|
|
1648 |
< |
public void await(CountDownLatch latch) { |
1648 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1649 |
|
try { |
1650 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1650 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1651 |
|
fail("timed out waiting for CountDownLatch for " |
1652 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1652 |
> |
+ (timeoutMillis/1000) + " sec"); |
1653 |
|
} catch (Throwable fail) { |
1654 |
|
threadUnexpectedException(fail); |
1655 |
|
} |
1656 |
|
} |
1657 |
|
|
1658 |
+ |
public void await(CountDownLatch latch) { |
1659 |
+ |
await(latch, LONG_DELAY_MS); |
1660 |
+ |
} |
1661 |
+ |
|
1662 |
|
public void await(Semaphore semaphore) { |
1663 |
|
try { |
1664 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1669 |
|
} |
1670 |
|
} |
1671 |
|
|
1672 |
+ |
public void await(CyclicBarrier barrier) { |
1673 |
+ |
try { |
1674 |
+ |
barrier.await(LONG_DELAY_MS, MILLISECONDS); |
1675 |
+ |
} catch (Throwable fail) { |
1676 |
+ |
threadUnexpectedException(fail); |
1677 |
+ |
} |
1678 |
+ |
} |
1679 |
+ |
|
1680 |
|
// /** |
1681 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1682 |
|
// */ |
1700 |
|
public String call() { throw new NullPointerException(); } |
1701 |
|
} |
1702 |
|
|
1480 |
– |
public static class CallableOne implements Callable<Integer> { |
1481 |
– |
public Integer call() { return one; } |
1482 |
– |
} |
1483 |
– |
|
1484 |
– |
public class ShortRunnable extends CheckedRunnable { |
1485 |
– |
protected void realRun() throws Throwable { |
1486 |
– |
delay(SHORT_DELAY_MS); |
1487 |
– |
} |
1488 |
– |
} |
1489 |
– |
|
1490 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1491 |
– |
protected void realRun() throws InterruptedException { |
1492 |
– |
delay(SHORT_DELAY_MS); |
1493 |
– |
} |
1494 |
– |
} |
1495 |
– |
|
1496 |
– |
public class SmallRunnable extends CheckedRunnable { |
1497 |
– |
protected void realRun() throws Throwable { |
1498 |
– |
delay(SMALL_DELAY_MS); |
1499 |
– |
} |
1500 |
– |
} |
1501 |
– |
|
1703 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1704 |
|
protected void realRun() { |
1705 |
|
try { |
1708 |
|
} |
1709 |
|
} |
1710 |
|
|
1510 |
– |
public class SmallCallable extends CheckedCallable { |
1511 |
– |
protected Object realCall() throws InterruptedException { |
1512 |
– |
delay(SMALL_DELAY_MS); |
1513 |
– |
return Boolean.TRUE; |
1514 |
– |
} |
1515 |
– |
} |
1516 |
– |
|
1517 |
– |
public class MediumRunnable extends CheckedRunnable { |
1518 |
– |
protected void realRun() throws Throwable { |
1519 |
– |
delay(MEDIUM_DELAY_MS); |
1520 |
– |
} |
1521 |
– |
} |
1522 |
– |
|
1523 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1524 |
– |
protected void realRun() throws InterruptedException { |
1525 |
– |
delay(MEDIUM_DELAY_MS); |
1526 |
– |
} |
1527 |
– |
} |
1528 |
– |
|
1711 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1712 |
|
return new CheckedRunnable() { |
1713 |
|
protected void realRun() { |
1717 |
|
}}; |
1718 |
|
} |
1719 |
|
|
1538 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1539 |
– |
protected void realRun() { |
1540 |
– |
try { |
1541 |
– |
delay(MEDIUM_DELAY_MS); |
1542 |
– |
} catch (InterruptedException ok) {} |
1543 |
– |
} |
1544 |
– |
} |
1545 |
– |
|
1546 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1547 |
– |
protected void realRun() { |
1548 |
– |
try { |
1549 |
– |
delay(LONG_DELAY_MS); |
1550 |
– |
} catch (InterruptedException ok) {} |
1551 |
– |
} |
1552 |
– |
} |
1553 |
– |
|
1720 |
|
/** |
1721 |
|
* For use as ThreadFactory in constructors |
1722 |
|
*/ |
1730 |
|
boolean isDone(); |
1731 |
|
} |
1732 |
|
|
1567 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1568 |
– |
return new TrackedRunnable() { |
1569 |
– |
private volatile boolean done = false; |
1570 |
– |
public boolean isDone() { return done; } |
1571 |
– |
public void run() { |
1572 |
– |
try { |
1573 |
– |
delay(timeoutMillis); |
1574 |
– |
done = true; |
1575 |
– |
} catch (InterruptedException ok) {} |
1576 |
– |
} |
1577 |
– |
}; |
1578 |
– |
} |
1579 |
– |
|
1580 |
– |
public static class TrackedShortRunnable implements Runnable { |
1581 |
– |
public volatile boolean done = false; |
1582 |
– |
public void run() { |
1583 |
– |
try { |
1584 |
– |
delay(SHORT_DELAY_MS); |
1585 |
– |
done = true; |
1586 |
– |
} catch (InterruptedException ok) {} |
1587 |
– |
} |
1588 |
– |
} |
1589 |
– |
|
1590 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1591 |
– |
public volatile boolean done = false; |
1592 |
– |
public void run() { |
1593 |
– |
try { |
1594 |
– |
delay(SMALL_DELAY_MS); |
1595 |
– |
done = true; |
1596 |
– |
} catch (InterruptedException ok) {} |
1597 |
– |
} |
1598 |
– |
} |
1599 |
– |
|
1600 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1601 |
– |
public volatile boolean done = false; |
1602 |
– |
public void run() { |
1603 |
– |
try { |
1604 |
– |
delay(MEDIUM_DELAY_MS); |
1605 |
– |
done = true; |
1606 |
– |
} catch (InterruptedException ok) {} |
1607 |
– |
} |
1608 |
– |
} |
1609 |
– |
|
1610 |
– |
public static class TrackedLongRunnable implements Runnable { |
1611 |
– |
public volatile boolean done = false; |
1612 |
– |
public void run() { |
1613 |
– |
try { |
1614 |
– |
delay(LONG_DELAY_MS); |
1615 |
– |
done = true; |
1616 |
– |
} catch (InterruptedException ok) {} |
1617 |
– |
} |
1618 |
– |
} |
1619 |
– |
|
1733 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1734 |
|
public volatile boolean done = false; |
1735 |
|
public void run() { |
1737 |
|
} |
1738 |
|
} |
1739 |
|
|
1627 |
– |
public static class TrackedCallable implements Callable { |
1628 |
– |
public volatile boolean done = false; |
1629 |
– |
public Object call() { |
1630 |
– |
try { |
1631 |
– |
delay(SMALL_DELAY_MS); |
1632 |
– |
done = true; |
1633 |
– |
} catch (InterruptedException ok) {} |
1634 |
– |
return Boolean.TRUE; |
1635 |
– |
} |
1636 |
– |
} |
1637 |
– |
|
1740 |
|
/** |
1741 |
|
* Analog of CheckedRunnable for RecursiveAction |
1742 |
|
*/ |
1780 |
|
* A CyclicBarrier that uses timed await and fails with |
1781 |
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1782 |
|
*/ |
1783 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1783 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1784 |
|
public CheckedBarrier(int parties) { super(parties); } |
1785 |
|
|
1786 |
|
public int await() { |
1803 |
|
assertEquals(0, q.size()); |
1804 |
|
assertNull(q.peek()); |
1805 |
|
assertNull(q.poll()); |
1806 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
1806 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
1807 |
|
assertEquals(q.toString(), "[]"); |
1808 |
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1809 |
|
assertFalse(q.iterator().hasNext()); |
1844 |
|
} |
1845 |
|
} |
1846 |
|
|
1847 |
+ |
void assertImmutable(final Object o) { |
1848 |
+ |
if (o instanceof Collection) { |
1849 |
+ |
assertThrows( |
1850 |
+ |
UnsupportedOperationException.class, |
1851 |
+ |
new Runnable() { public void run() { |
1852 |
+ |
((Collection) o).add(null);}}); |
1853 |
+ |
} |
1854 |
+ |
} |
1855 |
+ |
|
1856 |
|
@SuppressWarnings("unchecked") |
1857 |
|
<T> T serialClone(T o) { |
1858 |
|
try { |
1859 |
|
ObjectInputStream ois = new ObjectInputStream |
1860 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1861 |
|
T clone = (T) ois.readObject(); |
1862 |
+ |
if (o == clone) assertImmutable(o); |
1863 |
|
assertSame(o.getClass(), clone.getClass()); |
1864 |
|
return clone; |
1865 |
|
} catch (Throwable fail) { |
1868 |
|
} |
1869 |
|
} |
1870 |
|
|
1871 |
+ |
/** |
1872 |
+ |
* A version of serialClone that leaves error handling (for |
1873 |
+ |
* e.g. NotSerializableException) up to the caller. |
1874 |
+ |
*/ |
1875 |
+ |
@SuppressWarnings("unchecked") |
1876 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1877 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1878 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1879 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1880 |
+ |
oos.writeObject(o); |
1881 |
+ |
oos.flush(); |
1882 |
+ |
oos.close(); |
1883 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1884 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1885 |
+ |
T clone = (T) ois.readObject(); |
1886 |
+ |
if (o == clone) assertImmutable(o); |
1887 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1888 |
+ |
return clone; |
1889 |
+ |
} |
1890 |
+ |
|
1891 |
+ |
/** |
1892 |
+ |
* If o implements Cloneable and has a public clone method, |
1893 |
+ |
* returns a clone of o, else null. |
1894 |
+ |
*/ |
1895 |
+ |
@SuppressWarnings("unchecked") |
1896 |
+ |
<T> T cloneableClone(T o) { |
1897 |
+ |
if (!(o instanceof Cloneable)) return null; |
1898 |
+ |
final T clone; |
1899 |
+ |
try { |
1900 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1901 |
+ |
} catch (NoSuchMethodException ok) { |
1902 |
+ |
return null; |
1903 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1904 |
+ |
throw new Error(unexpected); |
1905 |
+ |
} |
1906 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1907 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1908 |
+ |
return clone; |
1909 |
+ |
} |
1910 |
+ |
|
1911 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1912 |
|
Runnable... throwingActions) { |
1913 |
|
for (Runnable throwingAction : throwingActions) { |
1936 |
|
} catch (NoSuchElementException success) {} |
1937 |
|
assertFalse(it.hasNext()); |
1938 |
|
} |
1939 |
+ |
|
1940 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1941 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1942 |
+ |
} |
1943 |
+ |
|
1944 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1945 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1946 |
+ |
} |
1947 |
+ |
|
1948 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1949 |
+ |
static final ExecutorService cachedThreadPool = |
1950 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1951 |
+ |
1000L, MILLISECONDS, |
1952 |
+ |
new SynchronousQueue<Runnable>()); |
1953 |
+ |
|
1954 |
+ |
/** |
1955 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1956 |
+ |
* pool (with bounded queue) before saturation (when submission |
1957 |
+ |
* throws RejectedExecutionException). |
1958 |
+ |
*/ |
1959 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1960 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1961 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1962 |
+ |
} |
1963 |
+ |
|
1964 |
+ |
static <T> void shuffle(T[] array) { |
1965 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1966 |
+ |
} |
1967 |
|
} |