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 ... |
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"); |
454 |
|
public static final String JAVA_SPECIFICATION_VERSION; |
455 |
|
static { |
456 |
|
try { |
457 |
< |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
457 |
> |
@SuppressWarnings("removal") double jcv = |
458 |
> |
java.security.AccessController.doPrivileged( |
459 |
|
new java.security.PrivilegedAction<Double>() { |
460 |
|
public Double run() { |
461 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
462 |
< |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
462 |
> |
JAVA_CLASS_VERSION = jcv; |
463 |
> |
@SuppressWarnings("removal") String jsv = |
464 |
> |
java.security.AccessController.doPrivileged( |
465 |
|
new java.security.PrivilegedAction<String>() { |
466 |
|
public String run() { |
467 |
|
return System.getProperty("java.specification.version");}}); |
468 |
+ |
JAVA_SPECIFICATION_VERSION = jsv; |
469 |
|
} catch (Throwable t) { |
470 |
|
throw new Error(t); |
471 |
|
} |
602 |
|
addNamedTestClasses(suite, java9TestClassNames); |
603 |
|
} |
604 |
|
|
605 |
+ |
if (atLeastJava17()) { |
606 |
+ |
String[] java17TestClassNames = { |
607 |
+ |
"ForkJoinPool19Test", |
608 |
+ |
}; |
609 |
+ |
addNamedTestClasses(suite, java17TestClassNames); |
610 |
+ |
} |
611 |
|
return suite; |
612 |
|
} |
613 |
|
|
679 |
|
public static long MEDIUM_DELAY_MS; |
680 |
|
public static long LONG_DELAY_MS; |
681 |
|
|
682 |
+ |
/** |
683 |
+ |
* A delay significantly longer than LONG_DELAY_MS. |
684 |
+ |
* Use this in a thread that is waited for via awaitTermination(Thread). |
685 |
+ |
*/ |
686 |
+ |
public static long LONGER_DELAY_MS; |
687 |
+ |
|
688 |
|
private static final long RANDOM_TIMEOUT; |
689 |
|
private static final long RANDOM_EXPIRED_TIMEOUT; |
690 |
|
private static final TimeUnit RANDOM_TIMEUNIT; |
720 |
|
} |
721 |
|
|
722 |
|
/** |
723 |
+ |
* Returns a random element from given choices. |
724 |
+ |
*/ |
725 |
+ |
<T> T chooseRandomly(List<T> choices) { |
726 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
727 |
+ |
} |
728 |
+ |
|
729 |
+ |
/** |
730 |
+ |
* Returns a random element from given choices. |
731 |
+ |
*/ |
732 |
+ |
@SuppressWarnings("unchecked") |
733 |
+ |
<T> T chooseRandomly(T... choices) { |
734 |
+ |
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
735 |
+ |
} |
736 |
+ |
|
737 |
+ |
/** |
738 |
|
* Returns the shortest timed delay. This can be scaled up for |
739 |
|
* slow machines using the jsr166.delay.factor system property, |
740 |
|
* or via jtreg's -timeoutFactor: flag. |
752 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
753 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
754 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
755 |
+ |
LONGER_DELAY_MS = 2 * LONG_DELAY_MS; |
756 |
|
} |
757 |
|
|
758 |
|
private static final long TIMEOUT_DELAY_MS |
791 |
|
*/ |
792 |
|
public void threadRecordFailure(Throwable t) { |
793 |
|
System.err.println(t); |
794 |
< |
dumpTestThreads(); |
795 |
< |
threadFailure.compareAndSet(null, t); |
794 |
> |
if (threadFailure.compareAndSet(null, t)) |
795 |
> |
dumpTestThreads(); |
796 |
|
} |
797 |
|
|
798 |
|
public void setUp() { |
1066 |
|
void joinPool(ExecutorService pool) { |
1067 |
|
try { |
1068 |
|
pool.shutdown(); |
1069 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
1069 |
> |
if (!pool.awaitTermination(20 * LONG_DELAY_MS, MILLISECONDS)) { |
1070 |
|
try { |
1071 |
|
threadFail("ExecutorService " + pool + |
1072 |
|
" did not terminate in a timely manner"); |
1113 |
|
} |
1114 |
|
} |
1115 |
|
|
1116 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1117 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1118 |
+ |
final String name = info.getThreadName(); |
1119 |
+ |
String lockName; |
1120 |
+ |
if (name == null) |
1121 |
+ |
return true; |
1122 |
+ |
if (name.equals("Signal Dispatcher") |
1123 |
+ |
|| name.equals("WedgedTestDetector")) |
1124 |
+ |
return false; |
1125 |
+ |
if (name.equals("Reference Handler")) { |
1126 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1127 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1128 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1129 |
+ |
&& stackTrace.length > 0 |
1130 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1131 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1132 |
+ |
return false; |
1133 |
+ |
// jdk8 Reference Handler stacktrace |
1134 |
+ |
if ((lockName = info.getLockName()) != null |
1135 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1136 |
+ |
return false; |
1137 |
+ |
} |
1138 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1139 |
+ |
&& (lockName = info.getLockName()) != null |
1140 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1141 |
+ |
return false; |
1142 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1143 |
+ |
&& (lockName = info.getLockName()) != null |
1144 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1145 |
+ |
return false; |
1146 |
+ |
return true; |
1147 |
+ |
} |
1148 |
+ |
|
1149 |
|
/** |
1150 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1151 |
|
* Uninteresting threads are filtered out. |
1152 |
|
*/ |
1153 |
+ |
@SuppressWarnings("removal") |
1154 |
|
static void dumpTestThreads() { |
1155 |
|
SecurityManager sm = System.getSecurityManager(); |
1156 |
|
if (sm != null) { |
1161 |
|
} |
1162 |
|
} |
1163 |
|
|
1086 |
– |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1164 |
|
System.err.println("------ stacktrace dump start ------"); |
1165 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1166 |
< |
final String name = info.getThreadName(); |
1167 |
< |
String lockName; |
1091 |
< |
if ("Signal Dispatcher".equals(name)) |
1092 |
< |
continue; |
1093 |
< |
if ("Reference Handler".equals(name) |
1094 |
< |
&& (lockName = info.getLockName()) != null |
1095 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1096 |
< |
continue; |
1097 |
< |
if ("Finalizer".equals(name) |
1098 |
< |
&& (lockName = info.getLockName()) != null |
1099 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1100 |
< |
continue; |
1101 |
< |
if ("checkForWedgedTest".equals(name)) |
1102 |
< |
continue; |
1103 |
< |
System.err.print(info); |
1104 |
< |
} |
1165 |
> |
for (ThreadInfo info : THREAD_MXBEAN.dumpAllThreads(true, true)) |
1166 |
> |
if (threadOfInterest(info)) |
1167 |
> |
System.err.print(info); |
1168 |
|
System.err.println("------ stacktrace dump end ------"); |
1169 |
|
|
1170 |
|
if (sm != null) System.setSecurityManager(sm); |
1191 |
|
} |
1192 |
|
|
1193 |
|
/** |
1194 |
+ |
* Returns the thread's blocker's class name, if any, else null. |
1195 |
+ |
*/ |
1196 |
+ |
String blockerClassName(Thread thread) { |
1197 |
+ |
ThreadInfo threadInfo; LockInfo lockInfo; |
1198 |
+ |
if ((threadInfo = THREAD_MXBEAN.getThreadInfo(thread.getId(), 0)) != null |
1199 |
+ |
&& (lockInfo = threadInfo.getLockInfo()) != null) |
1200 |
+ |
return lockInfo.getClassName(); |
1201 |
+ |
return null; |
1202 |
+ |
} |
1203 |
+ |
|
1204 |
+ |
/** |
1205 |
|
* Checks that future.get times out, with the default timeout of |
1206 |
|
* {@code timeoutMillis()}. |
1207 |
|
*/ |
1208 |
< |
void assertFutureTimesOut(Future future) { |
1208 |
> |
void assertFutureTimesOut(Future<?> future) { |
1209 |
|
assertFutureTimesOut(future, timeoutMillis()); |
1210 |
|
} |
1211 |
|
|
1212 |
|
/** |
1213 |
|
* Checks that future.get times out, with the given millisecond timeout. |
1214 |
|
*/ |
1215 |
< |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
1215 |
> |
void assertFutureTimesOut(Future<?> future, long timeoutMillis) { |
1216 |
|
long startTime = System.nanoTime(); |
1217 |
|
try { |
1218 |
|
future.get(timeoutMillis, MILLISECONDS); |
1220 |
|
} catch (TimeoutException success) { |
1221 |
|
} catch (Exception fail) { |
1222 |
|
threadUnexpectedException(fail); |
1223 |
< |
} finally { future.cancel(true); } |
1223 |
> |
} |
1224 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1225 |
+ |
assertFalse(future.isDone()); |
1226 |
|
} |
1227 |
|
|
1228 |
|
/** |
1247 |
|
|
1248 |
|
/** |
1249 |
|
* The number of elements to place in collections, arrays, etc. |
1250 |
+ |
* Must be at least ten; |
1251 |
|
*/ |
1252 |
< |
public static final int SIZE = 20; |
1252 |
> |
public static final int SIZE = 32; |
1253 |
|
|
1254 |
< |
// Some convenient Integer constants |
1255 |
< |
|
1256 |
< |
public static final Integer zero = new Integer(0); |
1257 |
< |
public static final Integer one = new Integer(1); |
1258 |
< |
public static final Integer two = new Integer(2); |
1259 |
< |
public static final Integer three = new Integer(3); |
1260 |
< |
public static final Integer four = new Integer(4); |
1261 |
< |
public static final Integer five = new Integer(5); |
1262 |
< |
public static final Integer six = new Integer(6); |
1263 |
< |
public static final Integer seven = new Integer(7); |
1264 |
< |
public static final Integer eight = new Integer(8); |
1265 |
< |
public static final Integer nine = new Integer(9); |
1266 |
< |
public static final Integer m1 = new Integer(-1); |
1267 |
< |
public static final Integer m2 = new Integer(-2); |
1268 |
< |
public static final Integer m3 = new Integer(-3); |
1269 |
< |
public static final Integer m4 = new Integer(-4); |
1270 |
< |
public static final Integer m5 = new Integer(-5); |
1271 |
< |
public static final Integer m6 = new Integer(-6); |
1272 |
< |
public static final Integer m10 = new Integer(-10); |
1254 |
> |
static Item[] seqItems(int size) { |
1255 |
> |
Item[] s = new Item[size]; |
1256 |
> |
for (int i = 0; i < size; ++i) |
1257 |
> |
s[i] = new Item(i); |
1258 |
> |
return s; |
1259 |
> |
} |
1260 |
> |
static Item[] negativeSeqItems(int size) { |
1261 |
> |
Item[] s = new Item[size]; |
1262 |
> |
for (int i = 0; i < size; ++i) |
1263 |
> |
s[i] = new Item(-i); |
1264 |
> |
return s; |
1265 |
> |
} |
1266 |
> |
|
1267 |
> |
// Many tests rely on defaultItems all being sequential nonnegative |
1268 |
> |
public static final Item[] defaultItems = seqItems(SIZE); |
1269 |
> |
|
1270 |
> |
static Item itemFor(int i) { // check cache for defaultItems |
1271 |
> |
Item[] items = defaultItems; |
1272 |
> |
return (i >= 0 && i < items.length) ? items[i] : new Item(i); |
1273 |
> |
} |
1274 |
> |
|
1275 |
> |
public static final Item zero = defaultItems[0]; |
1276 |
> |
public static final Item one = defaultItems[1]; |
1277 |
> |
public static final Item two = defaultItems[2]; |
1278 |
> |
public static final Item three = defaultItems[3]; |
1279 |
> |
public static final Item four = defaultItems[4]; |
1280 |
> |
public static final Item five = defaultItems[5]; |
1281 |
> |
public static final Item six = defaultItems[6]; |
1282 |
> |
public static final Item seven = defaultItems[7]; |
1283 |
> |
public static final Item eight = defaultItems[8]; |
1284 |
> |
public static final Item nine = defaultItems[9]; |
1285 |
> |
public static final Item ten = defaultItems[10]; |
1286 |
> |
|
1287 |
> |
public static final Item[] negativeItems = negativeSeqItems(SIZE); |
1288 |
> |
|
1289 |
> |
public static final Item minusOne = negativeItems[1]; |
1290 |
> |
public static final Item minusTwo = negativeItems[2]; |
1291 |
> |
public static final Item minusThree = negativeItems[3]; |
1292 |
> |
public static final Item minusFour = negativeItems[4]; |
1293 |
> |
public static final Item minusFive = negativeItems[5]; |
1294 |
> |
public static final Item minusSix = negativeItems[6]; |
1295 |
> |
public static final Item minusSeven = negativeItems[7]; |
1296 |
> |
public static final Item minusEight = negativeItems[8]; |
1297 |
> |
public static final Item minusNone = negativeItems[9]; |
1298 |
> |
public static final Item minusTen = negativeItems[10]; |
1299 |
> |
|
1300 |
> |
// elements expected to be missing |
1301 |
> |
public static final Item fortytwo = new Item(42); |
1302 |
> |
public static final Item eightysix = new Item(86); |
1303 |
> |
public static final Item ninetynine = new Item(99); |
1304 |
> |
|
1305 |
> |
// Interop across Item, int |
1306 |
> |
|
1307 |
> |
static void mustEqual(Item x, Item y) { |
1308 |
> |
if (x != y) |
1309 |
> |
assertEquals(x.value, y.value); |
1310 |
> |
} |
1311 |
> |
static void mustEqual(Item x, int y) { |
1312 |
> |
assertEquals(x.value, y); |
1313 |
> |
} |
1314 |
> |
static void mustEqual(int x, Item y) { |
1315 |
> |
assertEquals(x, y.value); |
1316 |
> |
} |
1317 |
> |
static void mustEqual(int x, int y) { |
1318 |
> |
assertEquals(x, y); |
1319 |
> |
} |
1320 |
> |
static void mustEqual(Object x, Object y) { |
1321 |
> |
if (x != y) |
1322 |
> |
assertEquals(x, y); |
1323 |
> |
} |
1324 |
> |
static void mustEqual(int x, Object y) { |
1325 |
> |
if (y instanceof Item) |
1326 |
> |
assertEquals(x, ((Item)y).value); |
1327 |
> |
else fail(); |
1328 |
> |
} |
1329 |
> |
static void mustEqual(Object x, int y) { |
1330 |
> |
if (x instanceof Item) |
1331 |
> |
assertEquals(((Item)x).value, y); |
1332 |
> |
else fail(); |
1333 |
> |
} |
1334 |
> |
static void mustEqual(boolean x, boolean y) { |
1335 |
> |
assertEquals(x, y); |
1336 |
> |
} |
1337 |
> |
static void mustEqual(long x, long y) { |
1338 |
> |
assertEquals(x, y); |
1339 |
> |
} |
1340 |
> |
static void mustEqual(double x, double y) { |
1341 |
> |
assertEquals(x, y); |
1342 |
> |
} |
1343 |
> |
static void mustContain(Collection<Item> c, int i) { |
1344 |
> |
assertTrue(c.contains(itemFor(i))); |
1345 |
> |
} |
1346 |
> |
static void mustContain(Collection<Item> c, Item i) { |
1347 |
> |
assertTrue(c.contains(i)); |
1348 |
> |
} |
1349 |
> |
static void mustNotContain(Collection<Item> c, int i) { |
1350 |
> |
assertFalse(c.contains(itemFor(i))); |
1351 |
> |
} |
1352 |
> |
static void mustNotContain(Collection<Item> c, Item i) { |
1353 |
> |
assertFalse(c.contains(i)); |
1354 |
> |
} |
1355 |
> |
static void mustRemove(Collection<Item> c, int i) { |
1356 |
> |
assertTrue(c.remove(itemFor(i))); |
1357 |
> |
} |
1358 |
> |
static void mustRemove(Collection<Item> c, Item i) { |
1359 |
> |
assertTrue(c.remove(i)); |
1360 |
> |
} |
1361 |
> |
static void mustNotRemove(Collection<Item> c, int i) { |
1362 |
> |
assertFalse(c.remove(itemFor(i))); |
1363 |
> |
} |
1364 |
> |
static void mustNotRemove(Collection<Item> c, Item i) { |
1365 |
> |
assertFalse(c.remove(i)); |
1366 |
> |
} |
1367 |
> |
static void mustAdd(Collection<Item> c, int i) { |
1368 |
> |
assertTrue(c.add(itemFor(i))); |
1369 |
> |
} |
1370 |
> |
static void mustAdd(Collection<Item> c, Item i) { |
1371 |
> |
assertTrue(c.add(i)); |
1372 |
> |
} |
1373 |
> |
static void mustOffer(Queue<Item> c, int i) { |
1374 |
> |
assertTrue(c.offer(itemFor(i))); |
1375 |
> |
} |
1376 |
> |
static void mustOffer(Queue<Item> c, Item i) { |
1377 |
> |
assertTrue(c.offer(i)); |
1378 |
> |
} |
1379 |
|
|
1380 |
|
/** |
1381 |
|
* Runs Runnable r with a security policy that permits precisely |
1384 |
|
* security manager. We require that any security manager permit |
1385 |
|
* getPolicy/setPolicy. |
1386 |
|
*/ |
1387 |
+ |
@SuppressWarnings("removal") |
1388 |
|
public void runWithPermissions(Runnable r, Permission... permissions) { |
1389 |
|
SecurityManager sm = System.getSecurityManager(); |
1390 |
|
if (sm == null) { |
1400 |
|
* Runnable. We require that any security manager permit |
1401 |
|
* getPolicy/setPolicy. |
1402 |
|
*/ |
1403 |
+ |
@SuppressWarnings("removal") |
1404 |
|
public void runWithSecurityManagerWithPermissions(Runnable r, |
1405 |
|
Permission... permissions) { |
1406 |
+ |
if (!useSecurityManager) return; |
1407 |
|
SecurityManager sm = System.getSecurityManager(); |
1408 |
|
if (sm == null) { |
1409 |
|
Policy savedPolicy = Policy.getPolicy(); |
1440 |
|
* A security policy where new permissions can be dynamically added |
1441 |
|
* or all cleared. |
1442 |
|
*/ |
1443 |
+ |
@SuppressWarnings("removal") |
1444 |
|
public static class AdjustablePolicy extends java.security.Policy { |
1445 |
|
Permissions perms = new Permissions(); |
1446 |
|
AdjustablePolicy(Permission... permissions) { |
1470 |
|
/** |
1471 |
|
* Returns a policy containing all the permissions we ever need. |
1472 |
|
*/ |
1473 |
+ |
@SuppressWarnings("removal") |
1474 |
|
public static Policy permissivePolicy() { |
1475 |
|
return new AdjustablePolicy |
1476 |
|
// Permissions j.u.c. needs directly |
1561 |
|
} |
1562 |
|
|
1563 |
|
/** |
1564 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1565 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1566 |
+ |
*/ |
1567 |
+ |
void awaitInterrupted() { |
1568 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1569 |
+ |
if (startTime == 0L) |
1570 |
+ |
startTime = System.nanoTime(); |
1571 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1572 |
+ |
fail("timed out waiting for thread interrupt"); |
1573 |
+ |
Thread.yield(); |
1574 |
+ |
} |
1575 |
+ |
} |
1576 |
+ |
|
1577 |
+ |
/** |
1578 |
|
* Returns the number of milliseconds since time given by |
1579 |
|
* startNanoTime, which must have been previously returned from a |
1580 |
|
* call to {@link System#nanoTime()}. |
1583 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1584 |
|
} |
1585 |
|
|
1385 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1386 |
– |
// long startTime = System.nanoTime(); |
1387 |
– |
// try { |
1388 |
– |
// r.run(); |
1389 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1390 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1391 |
– |
// throw new AssertionError("did not return promptly"); |
1392 |
– |
// } |
1393 |
– |
|
1394 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1395 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1396 |
– |
// } |
1397 |
– |
|
1586 |
|
/** |
1587 |
|
* Checks that timed f.get() returns the expected value, and does not |
1588 |
|
* wait for the timeout to elapse before returning. |
1613 |
|
} |
1614 |
|
|
1615 |
|
/** |
1616 |
+ |
* Returns a new started daemon Thread running the given action, |
1617 |
+ |
* wrapped in a CheckedRunnable. |
1618 |
+ |
*/ |
1619 |
+ |
Thread newStartedThread(Action action) { |
1620 |
+ |
return newStartedThread(checkedRunnable(action)); |
1621 |
+ |
} |
1622 |
+ |
|
1623 |
+ |
/** |
1624 |
|
* Waits for the specified time (in milliseconds) for the thread |
1625 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1626 |
|
* the thread (in the hope that it may terminate later) and fails. |
1627 |
|
*/ |
1628 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1628 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1629 |
|
try { |
1630 |
< |
t.join(timeoutMillis); |
1630 |
> |
thread.join(timeoutMillis); |
1631 |
|
} catch (InterruptedException fail) { |
1632 |
|
threadUnexpectedException(fail); |
1633 |
< |
} finally { |
1634 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1635 |
< |
t.interrupt(); |
1636 |
< |
threadFail("timed out waiting for thread to terminate"); |
1633 |
> |
} |
1634 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1635 |
> |
String detail = String.format( |
1636 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1637 |
> |
thread, thread.getState()); |
1638 |
> |
try { |
1639 |
> |
threadFail(detail); |
1640 |
> |
} finally { |
1641 |
> |
// Interrupt thread __after__ having reported its stack trace |
1642 |
> |
thread.interrupt(); |
1643 |
|
} |
1644 |
|
} |
1645 |
|
} |
1667 |
|
} |
1668 |
|
} |
1669 |
|
|
1670 |
+ |
Runnable checkedRunnable(Action action) { |
1671 |
+ |
return new CheckedRunnable() { |
1672 |
+ |
public void realRun() throws Throwable { |
1673 |
+ |
action.run(); |
1674 |
+ |
}}; |
1675 |
+ |
} |
1676 |
+ |
|
1677 |
|
public abstract class ThreadShouldThrow extends Thread { |
1678 |
|
protected abstract void realRun() throws Throwable; |
1679 |
|
|
1728 |
|
public void run() {} |
1729 |
|
} |
1730 |
|
|
1731 |
< |
public static class NoOpCallable implements Callable { |
1731 |
> |
public static class NoOpCallable implements Callable<Object> { |
1732 |
|
public Object call() { return Boolean.TRUE; } |
1733 |
|
} |
1734 |
|
|
1912 |
|
|
1913 |
|
public int await() { |
1914 |
|
try { |
1915 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1915 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1916 |
|
} catch (TimeoutException timedOut) { |
1917 |
|
throw new AssertionError("timed out"); |
1918 |
|
} catch (Exception fail) { |
1921 |
|
} |
1922 |
|
} |
1923 |
|
|
1924 |
< |
void checkEmpty(BlockingQueue q) { |
1924 |
> |
void checkEmpty(BlockingQueue<?> q) { |
1925 |
|
try { |
1926 |
|
assertTrue(q.isEmpty()); |
1927 |
|
assertEquals(0, q.size()); |
1968 |
|
} |
1969 |
|
} |
1970 |
|
|
1971 |
+ |
@SuppressWarnings("unchecked") |
1972 |
|
void assertImmutable(Object o) { |
1973 |
|
if (o instanceof Collection) { |
1974 |
|
assertThrows( |
2118 |
|
shouldThrow(); |
2119 |
|
} catch (NullPointerException success) {} |
2120 |
|
try { |
2121 |
< |
es.submit((Callable) null); |
2121 |
> |
es.submit((Callable<?>) null); |
2122 |
|
shouldThrow(); |
2123 |
|
} catch (NullPointerException success) {} |
2124 |
|
|
2130 |
|
shouldThrow(); |
2131 |
|
} catch (NullPointerException success) {} |
2132 |
|
try { |
2133 |
< |
ses.schedule((Callable) null, |
2133 |
> |
ses.schedule((Callable<?>) null, |
2134 |
|
randomTimeout(), randomTimeUnit()); |
2135 |
|
shouldThrow(); |
2136 |
|
} catch (NullPointerException success) {} |
2289 |
|
else { |
2290 |
|
assertEquals(x.isEmpty(), y.isEmpty()); |
2291 |
|
assertEquals(x.size(), y.size()); |
2292 |
< |
assertEquals(new HashSet(x), new HashSet(y)); |
2292 |
> |
assertEquals(new HashSet<Object>(x), new HashSet<Object>(y)); |
2293 |
|
if (x instanceof Deque) { |
2294 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2295 |
|
assertTrue(Arrays.equals(x.toArray(new Object[0]), |