49 |
|
import java.io.ObjectInputStream; |
50 |
|
import java.io.ObjectOutputStream; |
51 |
|
import java.lang.management.ManagementFactory; |
52 |
+ |
import java.lang.management.LockInfo; |
53 |
|
import java.lang.management.ThreadInfo; |
54 |
|
import java.lang.management.ThreadMXBean; |
55 |
|
import java.lang.reflect.Constructor; |
74 |
|
import java.util.List; |
75 |
|
import java.util.NoSuchElementException; |
76 |
|
import java.util.PropertyPermission; |
77 |
+ |
import java.util.Queue; |
78 |
|
import java.util.Set; |
79 |
|
import java.util.concurrent.BlockingQueue; |
80 |
|
import java.util.concurrent.Callable; |
151 |
|
* but even so, if there is ever any doubt, they can all be increased |
152 |
|
* in one spot to rerun tests on slower platforms. |
153 |
|
* |
154 |
+ |
* Class Item is used for elements of collections and related |
155 |
+ |
* purposes. Many tests rely on their keys being equal to ints. To |
156 |
+ |
* check these, methods mustEqual, mustContain, etc adapt the JUnit |
157 |
+ |
* assert methods to intercept ints. |
158 |
+ |
* |
159 |
|
* <li>All threads generated must be joined inside each test case |
160 |
|
* method (or {@code fail} to do so) before returning from the |
161 |
|
* method. The {@code joinPool} method can be used to do this when |
195 |
|
* </ul> |
196 |
|
*/ |
197 |
|
public class JSR166TestCase extends TestCase { |
198 |
+ |
// No longer run with custom securityManagers |
199 |
|
private static final boolean useSecurityManager = |
200 |
< |
Boolean.getBoolean("jsr166.useSecurityManager"); |
200 |
> |
Boolean.getBoolean("jsr166.useSecurityManager"); |
201 |
|
|
202 |
|
protected static final boolean expensiveTests = |
203 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
251 |
|
} |
252 |
|
} |
253 |
|
|
254 |
+ |
private static final ThreadMXBean THREAD_MXBEAN |
255 |
+ |
= ManagementFactory.getThreadMXBean(); |
256 |
+ |
|
257 |
|
/** |
258 |
|
* The scaling factor to apply to standard delays used in tests. |
259 |
|
* May be initialized from any of: |
295 |
|
static volatile TestCase currentTestCase; |
296 |
|
// static volatile int currentRun = 0; |
297 |
|
static { |
298 |
< |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
298 |
> |
Runnable wedgedTestDetector = new Runnable() { public void run() { |
299 |
|
// Avoid spurious reports with enormous runsPerTest. |
300 |
|
// A single test case run should never take more than 1 second. |
301 |
|
// But let's cap it at the high end too ... |
302 |
< |
final int timeoutMinutes = |
303 |
< |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
302 |
> |
final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1) |
303 |
> |
* Math.max((int) delayFactor, 1); |
304 |
> |
final int timeoutMinutes = Math.min(15, timeoutMinutesMin); |
305 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
306 |
|
try { MINUTES.sleep(timeoutMinutes); } |
307 |
|
catch (InterruptedException unexpected) { break; } |
321 |
|
} |
322 |
|
lastTestCase = currentTestCase; |
323 |
|
}}}; |
324 |
< |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
324 |
> |
Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector"); |
325 |
|
thread.setDaemon(true); |
326 |
|
thread.start(); |
327 |
|
} |
365 |
|
// Never report first run of any test; treat it as a |
366 |
|
// warmup run, notably to trigger all needed classloading, |
367 |
|
if (i > 0) |
368 |
< |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
368 |
> |
System.out.printf("%s: %d%n", toString(), elapsedMillis); |
369 |
|
} |
370 |
|
} |
371 |
|
|
408 |
|
* Runs all unit tests in the given test suite. |
409 |
|
* Actual behavior influenced by jsr166.* system properties. |
410 |
|
*/ |
411 |
+ |
@SuppressWarnings("removal") |
412 |
|
static void main(Test suite, String[] args) { |
413 |
|
if (useSecurityManager) { |
414 |
|
System.err.println("Setting a permissive security manager"); |
415 |
|
Policy.setPolicy(permissivePolicy()); |
416 |
< |
System.setSecurityManager(new SecurityManager()); |
416 |
> |
try { |
417 |
> |
System.setSecurityManager(new SecurityManager()); |
418 |
> |
} catch (Throwable ok) { // failure OK during deprecation |
419 |
> |
} |
420 |
|
} |
421 |
|
for (int i = 0; i < suiteRuns; i++) { |
422 |
|
TestResult result = newPithyTestRunner().doRun(suite); |
457 |
|
public static final String JAVA_SPECIFICATION_VERSION; |
458 |
|
static { |
459 |
|
try { |
460 |
< |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
460 |
> |
@SuppressWarnings("removal") double jcv = |
461 |
> |
java.security.AccessController.doPrivileged( |
462 |
|
new java.security.PrivilegedAction<Double>() { |
463 |
|
public Double run() { |
464 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
465 |
< |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
465 |
> |
JAVA_CLASS_VERSION = jcv; |
466 |
> |
@SuppressWarnings("removal") String jsv = |
467 |
> |
java.security.AccessController.doPrivileged( |
468 |
|
new java.security.PrivilegedAction<String>() { |
469 |
|
public String run() { |
470 |
|
return System.getProperty("java.specification.version");}}); |
471 |
+ |
JAVA_SPECIFICATION_VERSION = jsv; |
472 |
|
} catch (Throwable t) { |
473 |
|
throw new Error(t); |
474 |
|
} |
480 |
|
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
481 |
|
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
482 |
|
public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; } |
483 |
+ |
public static boolean atLeastJava12() { return JAVA_CLASS_VERSION >= 56.0; } |
484 |
+ |
public static boolean atLeastJava13() { return JAVA_CLASS_VERSION >= 57.0; } |
485 |
+ |
public static boolean atLeastJava14() { return JAVA_CLASS_VERSION >= 58.0; } |
486 |
+ |
public static boolean atLeastJava15() { return JAVA_CLASS_VERSION >= 59.0; } |
487 |
+ |
public static boolean atLeastJava16() { return JAVA_CLASS_VERSION >= 60.0; } |
488 |
+ |
public static boolean atLeastJava17() { return JAVA_CLASS_VERSION >= 61.0; } |
489 |
|
|
490 |
|
/** |
491 |
|
* Collects all JSR166 unit tests as one suite. |
537 |
|
ExecutorsTest.suite(), |
538 |
|
ExecutorCompletionServiceTest.suite(), |
539 |
|
FutureTaskTest.suite(), |
540 |
+ |
HashtableTest.suite(), |
541 |
|
LinkedBlockingDequeTest.suite(), |
542 |
|
LinkedBlockingQueueTest.suite(), |
543 |
|
LinkedListTest.suite(), |
577 |
|
"HashMapTest", |
578 |
|
"LinkedBlockingDeque8Test", |
579 |
|
"LinkedBlockingQueue8Test", |
580 |
+ |
"LinkedHashMapTest", |
581 |
|
"LongAccumulatorTest", |
582 |
|
"LongAdderTest", |
583 |
|
"SplittableRandomTest", |
605 |
|
addNamedTestClasses(suite, java9TestClassNames); |
606 |
|
} |
607 |
|
|
608 |
+ |
if (atLeastJava17()) { |
609 |
+ |
String[] java17TestClassNames = { |
610 |
+ |
"ForkJoinPool19Test", |
611 |
+ |
}; |
612 |
+ |
addNamedTestClasses(suite, java17TestClassNames); |
613 |
+ |
} |
614 |
|
return suite; |
615 |
|
} |
616 |
|
|
682 |
|
public static long MEDIUM_DELAY_MS; |
683 |
|
public static long LONG_DELAY_MS; |
684 |
|
|
685 |
+ |
/** |
686 |
+ |
* A delay significantly longer than LONG_DELAY_MS. |
687 |
+ |
* Use this in a thread that is waited for via awaitTermination(Thread). |
688 |
+ |
*/ |
689 |
+ |
public static long LONGER_DELAY_MS; |
690 |
+ |
|
691 |
|
private static final long RANDOM_TIMEOUT; |
692 |
|
private static final long RANDOM_EXPIRED_TIMEOUT; |
693 |
|
private static final TimeUnit RANDOM_TIMEUNIT; |
716 |
|
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
717 |
|
|
718 |
|
/** |
719 |
+ |
* Returns a random boolean; a "coin flip". |
720 |
+ |
*/ |
721 |
+ |
static boolean randomBoolean() { |
722 |
+ |
return ThreadLocalRandom.current().nextBoolean(); |
723 |
+ |
} |
724 |
+ |
|
725 |
+ |
/** |
726 |
+ |
* Returns a random element from given choices. |
727 |
+ |
*/ |
728 |
+ |
<T> T chooseRandomly(List<T> choices) { |
729 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
730 |
+ |
} |
731 |
+ |
|
732 |
+ |
/** |
733 |
+ |
* Returns a random element from given choices. |
734 |
+ |
*/ |
735 |
+ |
@SuppressWarnings("unchecked") |
736 |
+ |
<T> T chooseRandomly(T... choices) { |
737 |
+ |
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
738 |
+ |
} |
739 |
+ |
|
740 |
+ |
/** |
741 |
|
* Returns the shortest timed delay. This can be scaled up for |
742 |
|
* slow machines using the jsr166.delay.factor system property, |
743 |
|
* or via jtreg's -timeoutFactor: flag. |
755 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
756 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
757 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
758 |
+ |
LONGER_DELAY_MS = 2 * LONG_DELAY_MS; |
759 |
|
} |
760 |
|
|
761 |
|
private static final long TIMEOUT_DELAY_MS |
794 |
|
*/ |
795 |
|
public void threadRecordFailure(Throwable t) { |
796 |
|
System.err.println(t); |
797 |
< |
dumpTestThreads(); |
798 |
< |
threadFailure.compareAndSet(null, t); |
797 |
> |
if (threadFailure.compareAndSet(null, t)) |
798 |
> |
dumpTestThreads(); |
799 |
|
} |
800 |
|
|
801 |
|
public void setUp() { |
1069 |
|
void joinPool(ExecutorService pool) { |
1070 |
|
try { |
1071 |
|
pool.shutdown(); |
1072 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
1072 |
> |
if (!pool.awaitTermination(20 * LONG_DELAY_MS, MILLISECONDS)) { |
1073 |
|
try { |
1074 |
|
threadFail("ExecutorService " + pool + |
1075 |
|
" did not terminate in a timely manner"); |
1116 |
|
} |
1117 |
|
} |
1118 |
|
|
1119 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1120 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1121 |
+ |
final String name = info.getThreadName(); |
1122 |
+ |
String lockName; |
1123 |
+ |
if (name == null) |
1124 |
+ |
return true; |
1125 |
+ |
if (name.equals("Signal Dispatcher") |
1126 |
+ |
|| name.equals("WedgedTestDetector")) |
1127 |
+ |
return false; |
1128 |
+ |
if (name.equals("Reference Handler")) { |
1129 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1130 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1131 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1132 |
+ |
&& stackTrace.length > 0 |
1133 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1134 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1135 |
+ |
return false; |
1136 |
+ |
// jdk8 Reference Handler stacktrace |
1137 |
+ |
if ((lockName = info.getLockName()) != null |
1138 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1139 |
+ |
return false; |
1140 |
+ |
} |
1141 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1142 |
+ |
&& (lockName = info.getLockName()) != null |
1143 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1144 |
+ |
return false; |
1145 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1146 |
+ |
&& (lockName = info.getLockName()) != null |
1147 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1148 |
+ |
return false; |
1149 |
+ |
return true; |
1150 |
+ |
} |
1151 |
+ |
|
1152 |
|
/** |
1153 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1154 |
|
* Uninteresting threads are filtered out. |
1155 |
|
*/ |
1156 |
+ |
@SuppressWarnings("removal") |
1157 |
|
static void dumpTestThreads() { |
1158 |
|
SecurityManager sm = System.getSecurityManager(); |
1159 |
|
if (sm != null) { |
1164 |
|
} |
1165 |
|
} |
1166 |
|
|
1070 |
– |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1167 |
|
System.err.println("------ stacktrace dump start ------"); |
1168 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1169 |
< |
final String name = info.getThreadName(); |
1170 |
< |
String lockName; |
1075 |
< |
if ("Signal Dispatcher".equals(name)) |
1076 |
< |
continue; |
1077 |
< |
if ("Reference Handler".equals(name) |
1078 |
< |
&& (lockName = info.getLockName()) != null |
1079 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1080 |
< |
continue; |
1081 |
< |
if ("Finalizer".equals(name) |
1082 |
< |
&& (lockName = info.getLockName()) != null |
1083 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1084 |
< |
continue; |
1085 |
< |
if ("checkForWedgedTest".equals(name)) |
1086 |
< |
continue; |
1087 |
< |
System.err.print(info); |
1088 |
< |
} |
1168 |
> |
for (ThreadInfo info : THREAD_MXBEAN.dumpAllThreads(true, true)) |
1169 |
> |
if (threadOfInterest(info)) |
1170 |
> |
System.err.print(info); |
1171 |
|
System.err.println("------ stacktrace dump end ------"); |
1172 |
|
|
1173 |
|
if (sm != null) System.setSecurityManager(sm); |
1194 |
|
} |
1195 |
|
|
1196 |
|
/** |
1197 |
+ |
* Returns the thread's blocker's class name, if any, else null. |
1198 |
+ |
*/ |
1199 |
+ |
String blockerClassName(Thread thread) { |
1200 |
+ |
ThreadInfo threadInfo; LockInfo lockInfo; |
1201 |
+ |
if ((threadInfo = THREAD_MXBEAN.getThreadInfo(thread.getId(), 0)) != null |
1202 |
+ |
&& (lockInfo = threadInfo.getLockInfo()) != null) |
1203 |
+ |
return lockInfo.getClassName(); |
1204 |
+ |
return null; |
1205 |
+ |
} |
1206 |
+ |
|
1207 |
+ |
/** |
1208 |
|
* Checks that future.get times out, with the default timeout of |
1209 |
|
* {@code timeoutMillis()}. |
1210 |
|
*/ |
1211 |
< |
void assertFutureTimesOut(Future future) { |
1211 |
> |
void assertFutureTimesOut(Future<?> future) { |
1212 |
|
assertFutureTimesOut(future, timeoutMillis()); |
1213 |
|
} |
1214 |
|
|
1215 |
|
/** |
1216 |
|
* Checks that future.get times out, with the given millisecond timeout. |
1217 |
|
*/ |
1218 |
< |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
1218 |
> |
void assertFutureTimesOut(Future<?> future, long timeoutMillis) { |
1219 |
|
long startTime = System.nanoTime(); |
1220 |
|
try { |
1221 |
|
future.get(timeoutMillis, MILLISECONDS); |
1223 |
|
} catch (TimeoutException success) { |
1224 |
|
} catch (Exception fail) { |
1225 |
|
threadUnexpectedException(fail); |
1226 |
< |
} finally { future.cancel(true); } |
1226 |
> |
} |
1227 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1228 |
+ |
assertFalse(future.isDone()); |
1229 |
|
} |
1230 |
|
|
1231 |
|
/** |
1250 |
|
|
1251 |
|
/** |
1252 |
|
* The number of elements to place in collections, arrays, etc. |
1253 |
+ |
* Must be at least ten; |
1254 |
|
*/ |
1255 |
< |
public static final int SIZE = 20; |
1161 |
< |
|
1162 |
< |
// Some convenient Integer constants |
1255 |
> |
public static final int SIZE = 32; |
1256 |
|
|
1257 |
< |
public static final Integer zero = new Integer(0); |
1258 |
< |
public static final Integer one = new Integer(1); |
1259 |
< |
public static final Integer two = new Integer(2); |
1260 |
< |
public static final Integer three = new Integer(3); |
1261 |
< |
public static final Integer four = new Integer(4); |
1262 |
< |
public static final Integer five = new Integer(5); |
1263 |
< |
public static final Integer six = new Integer(6); |
1264 |
< |
public static final Integer seven = new Integer(7); |
1265 |
< |
public static final Integer eight = new Integer(8); |
1266 |
< |
public static final Integer nine = new Integer(9); |
1267 |
< |
public static final Integer m1 = new Integer(-1); |
1268 |
< |
public static final Integer m2 = new Integer(-2); |
1269 |
< |
public static final Integer m3 = new Integer(-3); |
1270 |
< |
public static final Integer m4 = new Integer(-4); |
1271 |
< |
public static final Integer m5 = new Integer(-5); |
1272 |
< |
public static final Integer m6 = new Integer(-6); |
1273 |
< |
public static final Integer m10 = new Integer(-10); |
1257 |
> |
static Item[] seqItems(int size) { |
1258 |
> |
Item[] s = new Item[size]; |
1259 |
> |
for (int i = 0; i < size; ++i) |
1260 |
> |
s[i] = new Item(i); |
1261 |
> |
return s; |
1262 |
> |
} |
1263 |
> |
static Item[] negativeSeqItems(int size) { |
1264 |
> |
Item[] s = new Item[size]; |
1265 |
> |
for (int i = 0; i < size; ++i) |
1266 |
> |
s[i] = new Item(-i); |
1267 |
> |
return s; |
1268 |
> |
} |
1269 |
> |
|
1270 |
> |
// Many tests rely on defaultItems all being sequential nonnegative |
1271 |
> |
public static final Item[] defaultItems = seqItems(SIZE); |
1272 |
> |
|
1273 |
> |
static Item itemFor(int i) { // check cache for defaultItems |
1274 |
> |
Item[] items = defaultItems; |
1275 |
> |
return (i >= 0 && i < items.length) ? items[i] : new Item(i); |
1276 |
> |
} |
1277 |
> |
|
1278 |
> |
public static final Item zero = defaultItems[0]; |
1279 |
> |
public static final Item one = defaultItems[1]; |
1280 |
> |
public static final Item two = defaultItems[2]; |
1281 |
> |
public static final Item three = defaultItems[3]; |
1282 |
> |
public static final Item four = defaultItems[4]; |
1283 |
> |
public static final Item five = defaultItems[5]; |
1284 |
> |
public static final Item six = defaultItems[6]; |
1285 |
> |
public static final Item seven = defaultItems[7]; |
1286 |
> |
public static final Item eight = defaultItems[8]; |
1287 |
> |
public static final Item nine = defaultItems[9]; |
1288 |
> |
public static final Item ten = defaultItems[10]; |
1289 |
> |
|
1290 |
> |
public static final Item[] negativeItems = negativeSeqItems(SIZE); |
1291 |
> |
|
1292 |
> |
public static final Item minusOne = negativeItems[1]; |
1293 |
> |
public static final Item minusTwo = negativeItems[2]; |
1294 |
> |
public static final Item minusThree = negativeItems[3]; |
1295 |
> |
public static final Item minusFour = negativeItems[4]; |
1296 |
> |
public static final Item minusFive = negativeItems[5]; |
1297 |
> |
public static final Item minusSix = negativeItems[6]; |
1298 |
> |
public static final Item minusSeven = negativeItems[7]; |
1299 |
> |
public static final Item minusEight = negativeItems[8]; |
1300 |
> |
public static final Item minusNone = negativeItems[9]; |
1301 |
> |
public static final Item minusTen = negativeItems[10]; |
1302 |
> |
|
1303 |
> |
// elements expected to be missing |
1304 |
> |
public static final Item fortytwo = new Item(42); |
1305 |
> |
public static final Item eightysix = new Item(86); |
1306 |
> |
public static final Item ninetynine = new Item(99); |
1307 |
> |
|
1308 |
> |
// Interop across Item, int |
1309 |
> |
|
1310 |
> |
static void mustEqual(Item x, Item y) { |
1311 |
> |
if (x != y) |
1312 |
> |
assertEquals(x.value, y.value); |
1313 |
> |
} |
1314 |
> |
static void mustEqual(Item x, int y) { |
1315 |
> |
assertEquals(x.value, y); |
1316 |
> |
} |
1317 |
> |
static void mustEqual(int x, Item y) { |
1318 |
> |
assertEquals(x, y.value); |
1319 |
> |
} |
1320 |
> |
static void mustEqual(int x, int y) { |
1321 |
> |
assertEquals(x, y); |
1322 |
> |
} |
1323 |
> |
static void mustEqual(Object x, Object y) { |
1324 |
> |
if (x != y) |
1325 |
> |
assertEquals(x, y); |
1326 |
> |
} |
1327 |
> |
static void mustEqual(int x, Object y) { |
1328 |
> |
if (y instanceof Item) |
1329 |
> |
assertEquals(x, ((Item)y).value); |
1330 |
> |
else fail(); |
1331 |
> |
} |
1332 |
> |
static void mustEqual(Object x, int y) { |
1333 |
> |
if (x instanceof Item) |
1334 |
> |
assertEquals(((Item)x).value, y); |
1335 |
> |
else fail(); |
1336 |
> |
} |
1337 |
> |
static void mustEqual(boolean x, boolean y) { |
1338 |
> |
assertEquals(x, y); |
1339 |
> |
} |
1340 |
> |
static void mustEqual(long x, long y) { |
1341 |
> |
assertEquals(x, y); |
1342 |
> |
} |
1343 |
> |
static void mustEqual(double x, double y) { |
1344 |
> |
assertEquals(x, y); |
1345 |
> |
} |
1346 |
> |
static void mustContain(Collection<Item> c, int i) { |
1347 |
> |
assertTrue(c.contains(itemFor(i))); |
1348 |
> |
} |
1349 |
> |
static void mustContain(Collection<Item> c, Item i) { |
1350 |
> |
assertTrue(c.contains(i)); |
1351 |
> |
} |
1352 |
> |
static void mustNotContain(Collection<Item> c, int i) { |
1353 |
> |
assertFalse(c.contains(itemFor(i))); |
1354 |
> |
} |
1355 |
> |
static void mustNotContain(Collection<Item> c, Item i) { |
1356 |
> |
assertFalse(c.contains(i)); |
1357 |
> |
} |
1358 |
> |
static void mustRemove(Collection<Item> c, int i) { |
1359 |
> |
assertTrue(c.remove(itemFor(i))); |
1360 |
> |
} |
1361 |
> |
static void mustRemove(Collection<Item> c, Item i) { |
1362 |
> |
assertTrue(c.remove(i)); |
1363 |
> |
} |
1364 |
> |
static void mustNotRemove(Collection<Item> c, int i) { |
1365 |
> |
assertFalse(c.remove(itemFor(i))); |
1366 |
> |
} |
1367 |
> |
static void mustNotRemove(Collection<Item> c, Item i) { |
1368 |
> |
assertFalse(c.remove(i)); |
1369 |
> |
} |
1370 |
> |
static void mustAdd(Collection<Item> c, int i) { |
1371 |
> |
assertTrue(c.add(itemFor(i))); |
1372 |
> |
} |
1373 |
> |
static void mustAdd(Collection<Item> c, Item i) { |
1374 |
> |
assertTrue(c.add(i)); |
1375 |
> |
} |
1376 |
> |
static void mustOffer(Queue<Item> c, int i) { |
1377 |
> |
assertTrue(c.offer(itemFor(i))); |
1378 |
> |
} |
1379 |
> |
static void mustOffer(Queue<Item> c, Item i) { |
1380 |
> |
assertTrue(c.offer(i)); |
1381 |
> |
} |
1382 |
|
|
1383 |
|
/** |
1384 |
|
* Runs Runnable r with a security policy that permits precisely |
1387 |
|
* security manager. We require that any security manager permit |
1388 |
|
* getPolicy/setPolicy. |
1389 |
|
*/ |
1390 |
+ |
@SuppressWarnings("removal") |
1391 |
|
public void runWithPermissions(Runnable r, Permission... permissions) { |
1392 |
|
SecurityManager sm = System.getSecurityManager(); |
1393 |
|
if (sm == null) { |
1403 |
|
* Runnable. We require that any security manager permit |
1404 |
|
* getPolicy/setPolicy. |
1405 |
|
*/ |
1406 |
+ |
@SuppressWarnings("removal") |
1407 |
|
public void runWithSecurityManagerWithPermissions(Runnable r, |
1408 |
|
Permission... permissions) { |
1409 |
+ |
if (!useSecurityManager) return; |
1410 |
|
SecurityManager sm = System.getSecurityManager(); |
1411 |
|
if (sm == null) { |
1412 |
|
Policy savedPolicy = Policy.getPolicy(); |
1414 |
|
Policy.setPolicy(permissivePolicy()); |
1415 |
|
System.setSecurityManager(new SecurityManager()); |
1416 |
|
runWithSecurityManagerWithPermissions(r, permissions); |
1417 |
+ |
} catch (UnsupportedOperationException ok) { |
1418 |
|
} finally { |
1419 |
< |
System.setSecurityManager(null); |
1420 |
< |
Policy.setPolicy(savedPolicy); |
1419 |
> |
try { |
1420 |
> |
System.setSecurityManager(null); |
1421 |
> |
Policy.setPolicy(savedPolicy); |
1422 |
> |
} catch (Exception ok) { |
1423 |
> |
} |
1424 |
|
} |
1425 |
|
} else { |
1426 |
|
Policy savedPolicy = Policy.getPolicy(); |
1447 |
|
* A security policy where new permissions can be dynamically added |
1448 |
|
* or all cleared. |
1449 |
|
*/ |
1450 |
+ |
@SuppressWarnings("removal") |
1451 |
|
public static class AdjustablePolicy extends java.security.Policy { |
1452 |
|
Permissions perms = new Permissions(); |
1453 |
|
AdjustablePolicy(Permission... permissions) { |
1477 |
|
/** |
1478 |
|
* Returns a policy containing all the permissions we ever need. |
1479 |
|
*/ |
1480 |
+ |
@SuppressWarnings("removal") |
1481 |
|
public static Policy permissivePolicy() { |
1482 |
|
return new AdjustablePolicy |
1483 |
|
// Permissions j.u.c. needs directly |
1515 |
|
Callable<Boolean> waitingForGodot) { |
1516 |
|
for (long startTime = 0L;;) { |
1517 |
|
switch (thread.getState()) { |
1518 |
+ |
default: break; |
1519 |
|
case BLOCKED: case WAITING: case TIMED_WAITING: |
1520 |
|
try { |
1521 |
|
if (waitingForGodot == null || waitingForGodot.call()) |
1568 |
|
} |
1569 |
|
|
1570 |
|
/** |
1571 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1572 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1573 |
+ |
*/ |
1574 |
+ |
void awaitInterrupted() { |
1575 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1576 |
+ |
if (startTime == 0L) |
1577 |
+ |
startTime = System.nanoTime(); |
1578 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1579 |
+ |
fail("timed out waiting for thread interrupt"); |
1580 |
+ |
Thread.yield(); |
1581 |
+ |
} |
1582 |
+ |
} |
1583 |
+ |
|
1584 |
+ |
/** |
1585 |
|
* Returns the number of milliseconds since time given by |
1586 |
|
* startNanoTime, which must have been previously returned from a |
1587 |
|
* call to {@link System#nanoTime()}. |
1590 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1591 |
|
} |
1592 |
|
|
1368 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1369 |
– |
// long startTime = System.nanoTime(); |
1370 |
– |
// try { |
1371 |
– |
// r.run(); |
1372 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1373 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1374 |
– |
// throw new AssertionError("did not return promptly"); |
1375 |
– |
// } |
1376 |
– |
|
1377 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1378 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1379 |
– |
// } |
1380 |
– |
|
1593 |
|
/** |
1594 |
|
* Checks that timed f.get() returns the expected value, and does not |
1595 |
|
* wait for the timeout to elapse before returning. |
1620 |
|
} |
1621 |
|
|
1622 |
|
/** |
1623 |
+ |
* Returns a new started daemon Thread running the given action, |
1624 |
+ |
* wrapped in a CheckedRunnable. |
1625 |
+ |
*/ |
1626 |
+ |
Thread newStartedThread(Action action) { |
1627 |
+ |
return newStartedThread(checkedRunnable(action)); |
1628 |
+ |
} |
1629 |
+ |
|
1630 |
+ |
/** |
1631 |
|
* Waits for the specified time (in milliseconds) for the thread |
1632 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1633 |
|
* the thread (in the hope that it may terminate later) and fails. |
1634 |
|
*/ |
1635 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1635 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1636 |
|
try { |
1637 |
< |
t.join(timeoutMillis); |
1637 |
> |
thread.join(timeoutMillis); |
1638 |
|
} catch (InterruptedException fail) { |
1639 |
|
threadUnexpectedException(fail); |
1640 |
< |
} finally { |
1641 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1642 |
< |
t.interrupt(); |
1643 |
< |
threadFail("timed out waiting for thread to terminate"); |
1640 |
> |
} |
1641 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1642 |
> |
String detail = String.format( |
1643 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1644 |
> |
thread, thread.getState()); |
1645 |
> |
try { |
1646 |
> |
threadFail(detail); |
1647 |
> |
} finally { |
1648 |
> |
// Interrupt thread __after__ having reported its stack trace |
1649 |
> |
thread.interrupt(); |
1650 |
|
} |
1651 |
|
} |
1652 |
|
} |
1674 |
|
} |
1675 |
|
} |
1676 |
|
|
1677 |
+ |
Runnable checkedRunnable(Action action) { |
1678 |
+ |
return new CheckedRunnable() { |
1679 |
+ |
public void realRun() throws Throwable { |
1680 |
+ |
action.run(); |
1681 |
+ |
}}; |
1682 |
+ |
} |
1683 |
+ |
|
1684 |
|
public abstract class ThreadShouldThrow extends Thread { |
1685 |
|
protected abstract void realRun() throws Throwable; |
1686 |
|
|
1735 |
|
public void run() {} |
1736 |
|
} |
1737 |
|
|
1738 |
< |
public static class NoOpCallable implements Callable { |
1738 |
> |
public static class NoOpCallable implements Callable<Object> { |
1739 |
|
public Object call() { return Boolean.TRUE; } |
1740 |
|
} |
1741 |
|
|
1842 |
|
public String call() { throw new NullPointerException(); } |
1843 |
|
} |
1844 |
|
|
1612 |
– |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1613 |
– |
protected void realRun() { |
1614 |
– |
try { |
1615 |
– |
delay(SMALL_DELAY_MS); |
1616 |
– |
} catch (InterruptedException ok) {} |
1617 |
– |
} |
1618 |
– |
} |
1619 |
– |
|
1845 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1846 |
|
return new CheckedRunnable() { |
1847 |
|
protected void realRun() { |
1919 |
|
|
1920 |
|
public int await() { |
1921 |
|
try { |
1922 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1922 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1923 |
|
} catch (TimeoutException timedOut) { |
1924 |
|
throw new AssertionError("timed out"); |
1925 |
|
} catch (Exception fail) { |
1928 |
|
} |
1929 |
|
} |
1930 |
|
|
1931 |
< |
void checkEmpty(BlockingQueue q) { |
1931 |
> |
void checkEmpty(BlockingQueue<?> q) { |
1932 |
|
try { |
1933 |
|
assertTrue(q.isEmpty()); |
1934 |
|
assertEquals(0, q.size()); |
1975 |
|
} |
1976 |
|
} |
1977 |
|
|
1978 |
< |
void assertImmutable(final Object o) { |
1978 |
> |
@SuppressWarnings("unchecked") |
1979 |
> |
void assertImmutable(Object o) { |
1980 |
|
if (o instanceof Collection) { |
1981 |
|
assertThrows( |
1982 |
|
UnsupportedOperationException.class, |
1983 |
< |
new Runnable() { public void run() { |
1758 |
< |
((Collection) o).add(null);}}); |
1983 |
> |
() -> ((Collection) o).add(null)); |
1984 |
|
} |
1985 |
|
} |
1986 |
|
|
2040 |
|
} |
2041 |
|
|
2042 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
2043 |
< |
Runnable... throwingActions) { |
2044 |
< |
for (Runnable throwingAction : throwingActions) { |
2043 |
> |
Action... throwingActions) { |
2044 |
> |
for (Action throwingAction : throwingActions) { |
2045 |
|
boolean threw = false; |
2046 |
|
try { throwingAction.run(); } |
2047 |
|
catch (Throwable t) { |
2125 |
|
shouldThrow(); |
2126 |
|
} catch (NullPointerException success) {} |
2127 |
|
try { |
2128 |
< |
es.submit((Callable) null); |
2128 |
> |
es.submit((Callable<?>) null); |
2129 |
|
shouldThrow(); |
2130 |
|
} catch (NullPointerException success) {} |
2131 |
|
|
2137 |
|
shouldThrow(); |
2138 |
|
} catch (NullPointerException success) {} |
2139 |
|
try { |
2140 |
< |
ses.schedule((Callable) null, |
2140 |
> |
ses.schedule((Callable<?>) null, |
2141 |
|
randomTimeout(), randomTimeUnit()); |
2142 |
|
shouldThrow(); |
2143 |
|
} catch (NullPointerException success) {} |
2296 |
|
else { |
2297 |
|
assertEquals(x.isEmpty(), y.isEmpty()); |
2298 |
|
assertEquals(x.size(), y.size()); |
2299 |
< |
assertEquals(new HashSet(x), new HashSet(y)); |
2299 |
> |
assertEquals(new HashSet<Object>(x), new HashSet<Object>(y)); |
2300 |
|
if (x instanceof Deque) { |
2301 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2302 |
|
assertTrue(Arrays.equals(x.toArray(new Object[0]), |