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 |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
+ |
import java.io.ByteArrayInputStream; |
11 |
+ |
import java.io.ByteArrayOutputStream; |
12 |
+ |
import java.io.ObjectInputStream; |
13 |
+ |
import java.io.ObjectOutputStream; |
14 |
+ |
import java.lang.management.ManagementFactory; |
15 |
+ |
import java.lang.management.ThreadInfo; |
16 |
+ |
import java.lang.reflect.Method; |
17 |
+ |
import java.util.ArrayList; |
18 |
+ |
import java.util.Arrays; |
19 |
+ |
import java.util.Date; |
20 |
+ |
import java.util.Enumeration; |
21 |
+ |
import java.util.List; |
22 |
+ |
import java.util.NoSuchElementException; |
23 |
|
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
+ |
import java.util.regex.Pattern; |
30 |
|
import java.security.CodeSource; |
31 |
|
import java.security.Permission; |
32 |
|
import java.security.PermissionCollection; |
76 |
|
* |
77 |
|
* </ol> |
78 |
|
* |
79 |
< |
* <p> <b>Other notes</b> |
79 |
> |
* <p><b>Other notes</b> |
80 |
|
* <ul> |
81 |
|
* |
82 |
|
* <li> Usually, there is one testcase method per JSR166 method |
112 |
|
private static final boolean useSecurityManager = |
113 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
114 |
|
|
115 |
+ |
protected static final boolean expensiveTests = |
116 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
117 |
+ |
|
118 |
+ |
protected static final boolean testImplementationDetails = |
119 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
120 |
+ |
|
121 |
+ |
/** |
122 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
123 |
+ |
* take more than profileThreshold milliseconds to execute. |
124 |
+ |
*/ |
125 |
+ |
private static final boolean profileTests = |
126 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
127 |
+ |
|
128 |
+ |
/** |
129 |
+ |
* The number of milliseconds that tests are permitted for |
130 |
+ |
* execution without being reported, when profileTests is set. |
131 |
+ |
*/ |
132 |
+ |
private static final long profileThreshold = |
133 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
134 |
+ |
|
135 |
+ |
/** |
136 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
137 |
+ |
*/ |
138 |
+ |
private static final int runsPerTest = |
139 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
140 |
+ |
|
141 |
+ |
/** |
142 |
+ |
* A filter for tests to run, matching strings of the form |
143 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
144 |
+ |
* Usefully combined with jsr166.runsPerTest. |
145 |
+ |
*/ |
146 |
+ |
private static final Pattern methodFilter = methodFilter(); |
147 |
+ |
|
148 |
+ |
private static Pattern methodFilter() { |
149 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
150 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
151 |
+ |
} |
152 |
+ |
|
153 |
+ |
protected void runTest() throws Throwable { |
154 |
+ |
if (methodFilter == null |
155 |
+ |
|| methodFilter.matcher(toString()).find()) { |
156 |
+ |
for (int i = 0; i < runsPerTest; i++) { |
157 |
+ |
if (profileTests) |
158 |
+ |
runTestProfiled(); |
159 |
+ |
else |
160 |
+ |
super.runTest(); |
161 |
+ |
} |
162 |
+ |
} |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
protected void runTestProfiled() throws Throwable { |
166 |
+ |
// Warmup run, notably to trigger all needed classloading. |
167 |
+ |
super.runTest(); |
168 |
+ |
long t0 = System.nanoTime(); |
169 |
+ |
try { |
170 |
+ |
super.runTest(); |
171 |
+ |
} finally { |
172 |
+ |
long elapsedMillis = millisElapsedSince(t0); |
173 |
+ |
if (elapsedMillis >= profileThreshold) |
174 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
175 |
+ |
} |
176 |
+ |
} |
177 |
+ |
|
178 |
|
/** |
179 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
179 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
180 |
> |
* Optional command line arg provides the number of iterations to |
181 |
> |
* repeat running the tests. |
182 |
|
*/ |
183 |
|
public static void main(String[] args) { |
184 |
|
if (useSecurityManager) { |
197 |
|
System.exit(0); |
198 |
|
} |
199 |
|
|
200 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
201 |
+ |
TestSuite suite = new TestSuite(); |
202 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
203 |
+ |
if (suiteOrClass instanceof TestSuite) |
204 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
205 |
+ |
else if (suiteOrClass instanceof Class) |
206 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
207 |
+ |
else |
208 |
+ |
throw new ClassCastException("not a test suite or class"); |
209 |
+ |
} |
210 |
+ |
return suite; |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
214 |
+ |
String... testClassNames) { |
215 |
+ |
for (String testClassName : testClassNames) { |
216 |
+ |
try { |
217 |
+ |
Class<?> testClass = Class.forName(testClassName); |
218 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
219 |
+ |
new Class<?>[0]); |
220 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
221 |
+ |
} catch (Exception e) { |
222 |
+ |
throw new Error("Missing test class", e); |
223 |
+ |
} |
224 |
+ |
} |
225 |
+ |
} |
226 |
+ |
|
227 |
+ |
public static final double JAVA_CLASS_VERSION; |
228 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
229 |
+ |
static { |
230 |
+ |
try { |
231 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
232 |
+ |
new java.security.PrivilegedAction<Double>() { |
233 |
+ |
public Double run() { |
234 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
235 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
236 |
+ |
new java.security.PrivilegedAction<String>() { |
237 |
+ |
public String run() { |
238 |
+ |
return System.getProperty("java.specification.version");}}); |
239 |
+ |
} catch (Throwable t) { |
240 |
+ |
throw new Error(t); |
241 |
+ |
} |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
245 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
246 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
247 |
+ |
public static boolean atLeastJava9() { |
248 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
249 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
250 |
+ |
} |
251 |
+ |
|
252 |
|
/** |
253 |
< |
* Collects all JSR166 unit tests as one suite |
253 |
> |
* Collects all JSR166 unit tests as one suite. |
254 |
|
*/ |
255 |
|
public static Test suite() { |
256 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
257 |
< |
|
258 |
< |
suite.addTest(ForkJoinPoolTest.suite()); |
259 |
< |
suite.addTest(ForkJoinTaskTest.suite()); |
260 |
< |
suite.addTest(RecursiveActionTest.suite()); |
261 |
< |
suite.addTest(RecursiveTaskTest.suite()); |
262 |
< |
suite.addTest(LinkedTransferQueueTest.suite()); |
263 |
< |
suite.addTest(PhaserTest.suite()); |
264 |
< |
suite.addTest(ThreadLocalRandomTest.suite()); |
265 |
< |
suite.addTest(AbstractExecutorServiceTest.suite()); |
266 |
< |
suite.addTest(AbstractQueueTest.suite()); |
267 |
< |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
268 |
< |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
269 |
< |
suite.addTest(ArrayBlockingQueueTest.suite()); |
270 |
< |
suite.addTest(ArrayDequeTest.suite()); |
271 |
< |
suite.addTest(AtomicBooleanTest.suite()); |
272 |
< |
suite.addTest(AtomicIntegerArrayTest.suite()); |
273 |
< |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
274 |
< |
suite.addTest(AtomicIntegerTest.suite()); |
275 |
< |
suite.addTest(AtomicLongArrayTest.suite()); |
276 |
< |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
277 |
< |
suite.addTest(AtomicLongTest.suite()); |
278 |
< |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
279 |
< |
suite.addTest(AtomicReferenceArrayTest.suite()); |
280 |
< |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
281 |
< |
suite.addTest(AtomicReferenceTest.suite()); |
282 |
< |
suite.addTest(AtomicStampedReferenceTest.suite()); |
283 |
< |
suite.addTest(ConcurrentHashMapTest.suite()); |
284 |
< |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
285 |
< |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
286 |
< |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
287 |
< |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
288 |
< |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
289 |
< |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
290 |
< |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
291 |
< |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
292 |
< |
suite.addTest(CountDownLatchTest.suite()); |
293 |
< |
suite.addTest(CyclicBarrierTest.suite()); |
294 |
< |
suite.addTest(DelayQueueTest.suite()); |
295 |
< |
suite.addTest(EntryTest.suite()); |
296 |
< |
suite.addTest(ExchangerTest.suite()); |
297 |
< |
suite.addTest(ExecutorsTest.suite()); |
298 |
< |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
299 |
< |
suite.addTest(FutureTaskTest.suite()); |
300 |
< |
suite.addTest(LinkedBlockingDequeTest.suite()); |
301 |
< |
suite.addTest(LinkedBlockingQueueTest.suite()); |
302 |
< |
suite.addTest(LinkedListTest.suite()); |
303 |
< |
suite.addTest(LockSupportTest.suite()); |
304 |
< |
suite.addTest(PriorityBlockingQueueTest.suite()); |
305 |
< |
suite.addTest(PriorityQueueTest.suite()); |
306 |
< |
suite.addTest(ReentrantLockTest.suite()); |
307 |
< |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
308 |
< |
suite.addTest(ScheduledExecutorTest.suite()); |
309 |
< |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
310 |
< |
suite.addTest(SemaphoreTest.suite()); |
311 |
< |
suite.addTest(SynchronousQueueTest.suite()); |
312 |
< |
suite.addTest(SystemTest.suite()); |
313 |
< |
suite.addTest(ThreadLocalTest.suite()); |
314 |
< |
suite.addTest(ThreadPoolExecutorTest.suite()); |
315 |
< |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
316 |
< |
suite.addTest(ThreadTest.suite()); |
317 |
< |
suite.addTest(TimeUnitTest.suite()); |
318 |
< |
suite.addTest(TreeMapTest.suite()); |
319 |
< |
suite.addTest(TreeSetTest.suite()); |
320 |
< |
suite.addTest(TreeSubMapTest.suite()); |
321 |
< |
suite.addTest(TreeSubSetTest.suite()); |
256 |
> |
// Java7+ test classes |
257 |
> |
TestSuite suite = newTestSuite( |
258 |
> |
ForkJoinPoolTest.suite(), |
259 |
> |
ForkJoinTaskTest.suite(), |
260 |
> |
RecursiveActionTest.suite(), |
261 |
> |
RecursiveTaskTest.suite(), |
262 |
> |
LinkedTransferQueueTest.suite(), |
263 |
> |
PhaserTest.suite(), |
264 |
> |
ThreadLocalRandomTest.suite(), |
265 |
> |
AbstractExecutorServiceTest.suite(), |
266 |
> |
AbstractQueueTest.suite(), |
267 |
> |
AbstractQueuedSynchronizerTest.suite(), |
268 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
269 |
> |
ArrayBlockingQueueTest.suite(), |
270 |
> |
ArrayDequeTest.suite(), |
271 |
> |
AtomicBooleanTest.suite(), |
272 |
> |
AtomicIntegerArrayTest.suite(), |
273 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
274 |
> |
AtomicIntegerTest.suite(), |
275 |
> |
AtomicLongArrayTest.suite(), |
276 |
> |
AtomicLongFieldUpdaterTest.suite(), |
277 |
> |
AtomicLongTest.suite(), |
278 |
> |
AtomicMarkableReferenceTest.suite(), |
279 |
> |
AtomicReferenceArrayTest.suite(), |
280 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
281 |
> |
AtomicReferenceTest.suite(), |
282 |
> |
AtomicStampedReferenceTest.suite(), |
283 |
> |
ConcurrentHashMapTest.suite(), |
284 |
> |
ConcurrentLinkedDequeTest.suite(), |
285 |
> |
ConcurrentLinkedQueueTest.suite(), |
286 |
> |
ConcurrentSkipListMapTest.suite(), |
287 |
> |
ConcurrentSkipListSubMapTest.suite(), |
288 |
> |
ConcurrentSkipListSetTest.suite(), |
289 |
> |
ConcurrentSkipListSubSetTest.suite(), |
290 |
> |
CopyOnWriteArrayListTest.suite(), |
291 |
> |
CopyOnWriteArraySetTest.suite(), |
292 |
> |
CountDownLatchTest.suite(), |
293 |
> |
CyclicBarrierTest.suite(), |
294 |
> |
DelayQueueTest.suite(), |
295 |
> |
EntryTest.suite(), |
296 |
> |
ExchangerTest.suite(), |
297 |
> |
ExecutorsTest.suite(), |
298 |
> |
ExecutorCompletionServiceTest.suite(), |
299 |
> |
FutureTaskTest.suite(), |
300 |
> |
LinkedBlockingDequeTest.suite(), |
301 |
> |
LinkedBlockingQueueTest.suite(), |
302 |
> |
LinkedListTest.suite(), |
303 |
> |
LockSupportTest.suite(), |
304 |
> |
PriorityBlockingQueueTest.suite(), |
305 |
> |
PriorityQueueTest.suite(), |
306 |
> |
ReentrantLockTest.suite(), |
307 |
> |
ReentrantReadWriteLockTest.suite(), |
308 |
> |
ScheduledExecutorTest.suite(), |
309 |
> |
ScheduledExecutorSubclassTest.suite(), |
310 |
> |
SemaphoreTest.suite(), |
311 |
> |
SynchronousQueueTest.suite(), |
312 |
> |
SystemTest.suite(), |
313 |
> |
ThreadLocalTest.suite(), |
314 |
> |
ThreadPoolExecutorTest.suite(), |
315 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
316 |
> |
ThreadTest.suite(), |
317 |
> |
TimeUnitTest.suite(), |
318 |
> |
TreeMapTest.suite(), |
319 |
> |
TreeSetTest.suite(), |
320 |
> |
TreeSubMapTest.suite(), |
321 |
> |
TreeSubSetTest.suite()); |
322 |
> |
|
323 |
> |
// Java8+ test classes |
324 |
> |
if (atLeastJava8()) { |
325 |
> |
String[] java8TestClassNames = { |
326 |
> |
"Atomic8Test", |
327 |
> |
"CompletableFutureTest", |
328 |
> |
"ConcurrentHashMap8Test", |
329 |
> |
"CountedCompleterTest", |
330 |
> |
"DoubleAccumulatorTest", |
331 |
> |
"DoubleAdderTest", |
332 |
> |
"ForkJoinPool8Test", |
333 |
> |
"ForkJoinTask8Test", |
334 |
> |
"LongAccumulatorTest", |
335 |
> |
"LongAdderTest", |
336 |
> |
"SplittableRandomTest", |
337 |
> |
"StampedLockTest", |
338 |
> |
"ThreadLocalRandom8Test", |
339 |
> |
}; |
340 |
> |
addNamedTestClasses(suite, java8TestClassNames); |
341 |
> |
} |
342 |
> |
|
343 |
> |
// Java9+ test classes |
344 |
> |
if (atLeastJava9()) { |
345 |
> |
String[] java9TestClassNames = { |
346 |
> |
"ThreadPoolExecutor9Test", |
347 |
> |
}; |
348 |
> |
addNamedTestClasses(suite, java9TestClassNames); |
349 |
> |
} |
350 |
|
|
351 |
|
return suite; |
352 |
|
} |
353 |
|
|
354 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
355 |
|
|
356 |
|
public static long SHORT_DELAY_MS; |
357 |
|
public static long SMALL_DELAY_MS; |
358 |
|
public static long MEDIUM_DELAY_MS; |
359 |
|
public static long LONG_DELAY_MS; |
360 |
|
|
199 |
– |
|
361 |
|
/** |
362 |
|
* Returns the shortest timed delay. This could |
363 |
|
* be reimplemented to use for example a Property. |
366 |
|
return 50; |
367 |
|
} |
368 |
|
|
208 |
– |
|
369 |
|
/** |
370 |
|
* Sets delays as multiples of SHORT_DELAY. |
371 |
|
*/ |
373 |
|
SHORT_DELAY_MS = getShortDelay(); |
374 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
375 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
376 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
376 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
377 |
> |
} |
378 |
> |
|
379 |
> |
/** |
380 |
> |
* Returns a timeout in milliseconds to be used in tests that |
381 |
> |
* verify that operations block or time out. |
382 |
> |
*/ |
383 |
> |
long timeoutMillis() { |
384 |
> |
return SHORT_DELAY_MS / 4; |
385 |
> |
} |
386 |
> |
|
387 |
> |
/** |
388 |
> |
* Returns a new Date instance representing a time delayMillis |
389 |
> |
* milliseconds in the future. |
390 |
> |
*/ |
391 |
> |
Date delayedDate(long delayMillis) { |
392 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
+ |
* Extra checks that get done for all test cases. |
417 |
+ |
* |
418 |
|
* Triggers test case failure if any thread assertions have failed, |
419 |
|
* by rethrowing, in the test harness thread, any exception recorded |
420 |
|
* earlier by threadRecordFailure. |
421 |
+ |
* |
422 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
423 |
|
*/ |
424 |
|
public void tearDown() throws Exception { |
425 |
< |
Throwable t = threadFailure.get(); |
425 |
> |
Throwable t = threadFailure.getAndSet(null); |
426 |
|
if (t != null) { |
427 |
|
if (t instanceof Error) |
428 |
|
throw (Error) t; |
430 |
|
throw (RuntimeException) t; |
431 |
|
else if (t instanceof Exception) |
432 |
|
throw (Exception) t; |
433 |
< |
else |
434 |
< |
throw new AssertionError(t); |
433 |
> |
else { |
434 |
> |
AssertionFailedError afe = |
435 |
> |
new AssertionFailedError(t.toString()); |
436 |
> |
afe.initCause(t); |
437 |
> |
throw afe; |
438 |
> |
} |
439 |
> |
} |
440 |
> |
|
441 |
> |
if (Thread.interrupted()) |
442 |
> |
throw new AssertionFailedError("interrupt status set in main thread"); |
443 |
> |
|
444 |
> |
checkForkJoinPoolThreadLeaks(); |
445 |
> |
} |
446 |
> |
|
447 |
> |
/** |
448 |
> |
* Find missing try { ... } finally { joinPool(e); } |
449 |
> |
*/ |
450 |
> |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
451 |
> |
Thread[] survivors = new Thread[5]; |
452 |
> |
int count = Thread.enumerate(survivors); |
453 |
> |
for (int i = 0; i < count; i++) { |
454 |
> |
Thread thread = survivors[i]; |
455 |
> |
String name = thread.getName(); |
456 |
> |
if (name.startsWith("ForkJoinPool-")) { |
457 |
> |
// give thread some time to terminate |
458 |
> |
thread.join(LONG_DELAY_MS); |
459 |
> |
if (!thread.isAlive()) continue; |
460 |
> |
thread.stop(); |
461 |
> |
throw new AssertionFailedError |
462 |
> |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
463 |
> |
toString(), name)); |
464 |
> |
} |
465 |
|
} |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
|
* Just like fail(reason), but additionally recording (using |
470 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
471 |
< |
* testcase will fail. |
470 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
471 |
> |
* the current testcase will fail. |
472 |
|
*/ |
473 |
|
public void threadFail(String reason) { |
474 |
|
try { |
475 |
|
fail(reason); |
476 |
< |
} catch (Throwable t) { |
476 |
> |
} catch (AssertionFailedError t) { |
477 |
|
threadRecordFailure(t); |
478 |
|
fail(reason); |
479 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* Just like assertTrue(b), but additionally recording (using |
484 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
485 |
< |
* testcase will fail. |
484 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
485 |
> |
* the current testcase will fail. |
486 |
|
*/ |
487 |
|
public void threadAssertTrue(boolean b) { |
488 |
|
try { |
489 |
|
assertTrue(b); |
490 |
< |
} catch (AssertionError t) { |
490 |
> |
} catch (AssertionFailedError t) { |
491 |
|
threadRecordFailure(t); |
492 |
|
throw t; |
493 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
|
* Just like assertFalse(b), but additionally recording (using |
498 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
499 |
< |
* current testcase will fail. |
498 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
499 |
> |
* the current testcase will fail. |
500 |
|
*/ |
501 |
|
public void threadAssertFalse(boolean b) { |
502 |
|
try { |
503 |
|
assertFalse(b); |
504 |
< |
} catch (AssertionError t) { |
504 |
> |
} catch (AssertionFailedError t) { |
505 |
|
threadRecordFailure(t); |
506 |
|
throw t; |
507 |
|
} |
509 |
|
|
510 |
|
/** |
511 |
|
* Just like assertNull(x), but additionally recording (using |
512 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
513 |
< |
* current testcase will fail. |
512 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
513 |
> |
* the current testcase will fail. |
514 |
|
*/ |
515 |
|
public void threadAssertNull(Object x) { |
516 |
|
try { |
517 |
|
assertNull(x); |
518 |
< |
} catch (AssertionError t) { |
518 |
> |
} catch (AssertionFailedError t) { |
519 |
|
threadRecordFailure(t); |
520 |
|
throw t; |
521 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
|
* Just like assertEquals(x, y), but additionally recording (using |
526 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
527 |
< |
* current testcase will fail. |
526 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
527 |
> |
* the current testcase will fail. |
528 |
|
*/ |
529 |
|
public void threadAssertEquals(long x, long y) { |
530 |
|
try { |
531 |
|
assertEquals(x, y); |
532 |
< |
} catch (AssertionError t) { |
532 |
> |
} catch (AssertionFailedError t) { |
533 |
|
threadRecordFailure(t); |
534 |
|
throw t; |
535 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
|
* Just like assertEquals(x, y), but additionally recording (using |
540 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
541 |
< |
* current testcase will fail. |
540 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
541 |
> |
* the current testcase will fail. |
542 |
|
*/ |
543 |
|
public void threadAssertEquals(Object x, Object y) { |
544 |
|
try { |
545 |
|
assertEquals(x, y); |
546 |
< |
} catch (AssertionError t) { |
546 |
> |
} catch (AssertionFailedError t) { |
547 |
|
threadRecordFailure(t); |
548 |
|
throw t; |
549 |
+ |
} catch (Throwable t) { |
550 |
+ |
threadUnexpectedException(t); |
551 |
|
} |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
|
* Just like assertSame(x, y), but additionally recording (using |
556 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
557 |
< |
* current testcase will fail. |
556 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
557 |
> |
* the current testcase will fail. |
558 |
|
*/ |
559 |
|
public void threadAssertSame(Object x, Object y) { |
560 |
|
try { |
561 |
|
assertSame(x, y); |
562 |
< |
} catch (AssertionError t) { |
562 |
> |
} catch (AssertionFailedError t) { |
563 |
|
threadRecordFailure(t); |
564 |
|
throw t; |
565 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* Calls threadFail with message "Unexpected exception" + ex. |
583 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
584 |
> |
* then rethrows the exception, wrapping it in an |
585 |
> |
* AssertionFailedError if necessary. |
586 |
|
*/ |
587 |
|
public void threadUnexpectedException(Throwable t) { |
588 |
|
threadRecordFailure(t); |
589 |
|
t.printStackTrace(); |
376 |
– |
// Rethrow, wrapping in an AssertionError if necessary |
590 |
|
if (t instanceof RuntimeException) |
591 |
|
throw (RuntimeException) t; |
592 |
|
else if (t instanceof Error) |
593 |
|
throw (Error) t; |
594 |
|
else { |
595 |
< |
AssertionError ae = new AssertionError("unexpected exception: " + t); |
596 |
< |
t.initCause(t); |
597 |
< |
throw ae; |
595 |
> |
AssertionFailedError afe = |
596 |
> |
new AssertionFailedError("unexpected exception: " + t); |
597 |
> |
afe.initCause(t); |
598 |
> |
throw afe; |
599 |
> |
} |
600 |
> |
} |
601 |
> |
|
602 |
> |
/** |
603 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
604 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
605 |
> |
* until time elapses. |
606 |
> |
*/ |
607 |
> |
static void delay(long millis) throws InterruptedException { |
608 |
> |
long startTime = System.nanoTime(); |
609 |
> |
long ns = millis * 1000 * 1000; |
610 |
> |
for (;;) { |
611 |
> |
if (millis > 0L) |
612 |
> |
Thread.sleep(millis); |
613 |
> |
else // too short to sleep |
614 |
> |
Thread.yield(); |
615 |
> |
long d = ns - (System.nanoTime() - startTime); |
616 |
> |
if (d > 0L) |
617 |
> |
millis = d / (1000 * 1000); |
618 |
> |
else |
619 |
> |
break; |
620 |
|
} |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
|
* Waits out termination of a thread pool or fails doing so. |
625 |
|
*/ |
626 |
< |
public void joinPool(ExecutorService exec) { |
626 |
> |
void joinPool(ExecutorService exec) { |
627 |
|
try { |
628 |
|
exec.shutdown(); |
629 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
630 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
630 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
631 |
|
} catch (SecurityException ok) { |
632 |
|
// Allowed in case test doesn't have privs |
633 |
|
} catch (InterruptedException ie) { |
635 |
|
} |
636 |
|
} |
637 |
|
|
638 |
+ |
/** |
639 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
640 |
+ |
*/ |
641 |
+ |
static void printAllStackTraces() { |
642 |
+ |
for (ThreadInfo info : |
643 |
+ |
ManagementFactory.getThreadMXBean() |
644 |
+ |
.dumpAllThreads(true, true)) |
645 |
+ |
System.err.print(info); |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
/** |
649 |
+ |
* Checks that thread does not terminate within the default |
650 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
651 |
+ |
*/ |
652 |
+ |
void assertThreadStaysAlive(Thread thread) { |
653 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
654 |
+ |
} |
655 |
+ |
|
656 |
+ |
/** |
657 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
658 |
+ |
*/ |
659 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
660 |
+ |
try { |
661 |
+ |
// No need to optimize the failing case via Thread.join. |
662 |
+ |
delay(millis); |
663 |
+ |
assertTrue(thread.isAlive()); |
664 |
+ |
} catch (InterruptedException ie) { |
665 |
+ |
fail("Unexpected InterruptedException"); |
666 |
+ |
} |
667 |
+ |
} |
668 |
+ |
|
669 |
+ |
/** |
670 |
+ |
* Checks that the threads do not terminate within the default |
671 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
672 |
+ |
*/ |
673 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
674 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
/** |
678 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
679 |
+ |
*/ |
680 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
681 |
+ |
try { |
682 |
+ |
// No need to optimize the failing case via Thread.join. |
683 |
+ |
delay(millis); |
684 |
+ |
for (Thread thread : threads) |
685 |
+ |
assertTrue(thread.isAlive()); |
686 |
+ |
} catch (InterruptedException ie) { |
687 |
+ |
fail("Unexpected InterruptedException"); |
688 |
+ |
} |
689 |
+ |
} |
690 |
+ |
|
691 |
+ |
/** |
692 |
+ |
* Checks that future.get times out, with the default timeout of |
693 |
+ |
* {@code timeoutMillis()}. |
694 |
+ |
*/ |
695 |
+ |
void assertFutureTimesOut(Future future) { |
696 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
697 |
+ |
} |
698 |
+ |
|
699 |
+ |
/** |
700 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
701 |
+ |
*/ |
702 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
703 |
+ |
long startTime = System.nanoTime(); |
704 |
+ |
try { |
705 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
706 |
+ |
shouldThrow(); |
707 |
+ |
} catch (TimeoutException success) { |
708 |
+ |
} catch (Exception e) { |
709 |
+ |
threadUnexpectedException(e); |
710 |
+ |
} finally { future.cancel(true); } |
711 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
712 |
+ |
} |
713 |
|
|
714 |
|
/** |
715 |
|
* Fails with message "should throw exception". |
726 |
|
} |
727 |
|
|
728 |
|
/** |
419 |
– |
* Fails with message "Unexpected exception: " + ex. |
420 |
– |
*/ |
421 |
– |
public void unexpectedException(Throwable ex) { |
422 |
– |
ex.printStackTrace(); |
423 |
– |
fail("Unexpected exception: " + ex); |
424 |
– |
} |
425 |
– |
|
426 |
– |
|
427 |
– |
/** |
729 |
|
* The number of elements to place in collections, arrays, etc. |
730 |
|
*/ |
731 |
|
public static final int SIZE = 20; |
750 |
|
public static final Integer m6 = new Integer(-6); |
751 |
|
public static final Integer m10 = new Integer(-10); |
752 |
|
|
452 |
– |
|
753 |
|
/** |
754 |
|
* Runs Runnable r with a security policy that permits precisely |
755 |
|
* the specified permissions. If there is no current security |
761 |
|
SecurityManager sm = System.getSecurityManager(); |
762 |
|
if (sm == null) { |
763 |
|
r.run(); |
764 |
+ |
} |
765 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
766 |
+ |
} |
767 |
+ |
|
768 |
+ |
/** |
769 |
+ |
* Runs Runnable r with a security policy that permits precisely |
770 |
+ |
* the specified permissions. If there is no current security |
771 |
+ |
* manager, a temporary one is set for the duration of the |
772 |
+ |
* Runnable. We require that any security manager permit |
773 |
+ |
* getPolicy/setPolicy. |
774 |
+ |
*/ |
775 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
776 |
+ |
Permission... permissions) { |
777 |
+ |
SecurityManager sm = System.getSecurityManager(); |
778 |
+ |
if (sm == null) { |
779 |
|
Policy savedPolicy = Policy.getPolicy(); |
780 |
|
try { |
781 |
|
Policy.setPolicy(permissivePolicy()); |
782 |
|
System.setSecurityManager(new SecurityManager()); |
783 |
< |
runWithPermissions(r, permissions); |
783 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
784 |
|
} finally { |
785 |
|
System.setSecurityManager(null); |
786 |
|
Policy.setPolicy(savedPolicy); |
828 |
|
return perms.implies(p); |
829 |
|
} |
830 |
|
public void refresh() {} |
831 |
+ |
public String toString() { |
832 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
833 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
834 |
+ |
ps.add(e.nextElement()); |
835 |
+ |
return "AdjustablePolicy with permissions " + ps; |
836 |
+ |
} |
837 |
|
} |
838 |
|
|
839 |
|
/** |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
860 |
< |
* Does <em>NOT</em> throw InterruptedException. |
859 |
> |
* Sleeps until the given time has elapsed. |
860 |
> |
* Throws AssertionFailedError if interrupted. |
861 |
|
*/ |
862 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
862 |
> |
void sleep(long millis) { |
863 |
|
try { |
864 |
< |
Thread.sleep(timeoutMillis); |
865 |
< |
} catch (InterruptedException wakeup) {} |
864 |
> |
delay(millis); |
865 |
> |
} catch (InterruptedException ie) { |
866 |
> |
AssertionFailedError afe = |
867 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
868 |
> |
afe.initCause(ie); |
869 |
> |
throw afe; |
870 |
> |
} |
871 |
> |
} |
872 |
> |
|
873 |
> |
/** |
874 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
875 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
876 |
> |
*/ |
877 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
878 |
> |
long startTime = System.nanoTime(); |
879 |
> |
for (;;) { |
880 |
> |
Thread.State s = thread.getState(); |
881 |
> |
if (s == Thread.State.BLOCKED || |
882 |
> |
s == Thread.State.WAITING || |
883 |
> |
s == Thread.State.TIMED_WAITING) |
884 |
> |
return; |
885 |
> |
else if (s == Thread.State.TERMINATED) |
886 |
> |
fail("Unexpected thread termination"); |
887 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
888 |
> |
threadAssertTrue(thread.isAlive()); |
889 |
> |
return; |
890 |
> |
} |
891 |
> |
Thread.yield(); |
892 |
> |
} |
893 |
|
} |
894 |
|
|
895 |
|
/** |
896 |
< |
* Returns a new started Thread running the given runnable. |
896 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
897 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
898 |
> |
*/ |
899 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
900 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
901 |
> |
} |
902 |
> |
|
903 |
> |
/** |
904 |
> |
* Returns the number of milliseconds since time given by |
905 |
> |
* startNanoTime, which must have been previously returned from a |
906 |
> |
* call to {@link System.nanoTime()}. |
907 |
> |
*/ |
908 |
> |
static long millisElapsedSince(long startNanoTime) { |
909 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
910 |
> |
} |
911 |
> |
|
912 |
> |
/** |
913 |
> |
* Returns a new started daemon Thread running the given runnable. |
914 |
|
*/ |
915 |
|
Thread newStartedThread(Runnable runnable) { |
916 |
|
Thread t = new Thread(runnable); |
917 |
+ |
t.setDaemon(true); |
918 |
|
t.start(); |
919 |
|
return t; |
920 |
|
} |
921 |
|
|
922 |
+ |
/** |
923 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
924 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
925 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
926 |
+ |
*/ |
927 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
928 |
+ |
try { |
929 |
+ |
t.join(timeoutMillis); |
930 |
+ |
} catch (InterruptedException ie) { |
931 |
+ |
threadUnexpectedException(ie); |
932 |
+ |
} finally { |
933 |
+ |
if (t.getState() != Thread.State.TERMINATED) { |
934 |
+ |
t.interrupt(); |
935 |
+ |
fail("Test timed out"); |
936 |
+ |
} |
937 |
+ |
} |
938 |
+ |
} |
939 |
+ |
|
940 |
+ |
/** |
941 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
942 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
943 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
944 |
+ |
*/ |
945 |
+ |
void awaitTermination(Thread t) { |
946 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
947 |
+ |
} |
948 |
+ |
|
949 |
|
// Some convenient Runnable classes |
950 |
|
|
951 |
|
public abstract class CheckedRunnable implements Runnable { |
1008 |
|
realRun(); |
1009 |
|
threadShouldThrow("InterruptedException"); |
1010 |
|
} catch (InterruptedException success) { |
1011 |
+ |
threadAssertFalse(Thread.interrupted()); |
1012 |
|
} catch (Throwable t) { |
1013 |
|
threadUnexpectedException(t); |
1014 |
|
} |
1038 |
|
threadShouldThrow("InterruptedException"); |
1039 |
|
return result; |
1040 |
|
} catch (InterruptedException success) { |
1041 |
+ |
threadAssertFalse(Thread.interrupted()); |
1042 |
|
} catch (Throwable t) { |
1043 |
|
threadUnexpectedException(t); |
1044 |
|
} |
1062 |
|
|
1063 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1064 |
|
return new CheckedCallable<String>() { |
1065 |
< |
public String realCall() { |
1065 |
> |
protected String realCall() { |
1066 |
|
try { |
1067 |
|
latch.await(); |
1068 |
|
} catch (InterruptedException quittingTime) {} |
1070 |
|
}}; |
1071 |
|
} |
1072 |
|
|
1073 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1074 |
+ |
return new CheckedRunnable() { |
1075 |
+ |
public void realRun() throws InterruptedException { |
1076 |
+ |
await(latch); |
1077 |
+ |
}}; |
1078 |
+ |
} |
1079 |
+ |
|
1080 |
+ |
public void await(CountDownLatch latch) { |
1081 |
+ |
try { |
1082 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1083 |
+ |
} catch (Throwable t) { |
1084 |
+ |
threadUnexpectedException(t); |
1085 |
+ |
} |
1086 |
+ |
} |
1087 |
+ |
|
1088 |
+ |
public void await(Semaphore semaphore) { |
1089 |
+ |
try { |
1090 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1091 |
+ |
} catch (Throwable t) { |
1092 |
+ |
threadUnexpectedException(t); |
1093 |
+ |
} |
1094 |
+ |
} |
1095 |
+ |
|
1096 |
+ |
// /** |
1097 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1098 |
+ |
// */ |
1099 |
+ |
// public void await(AtomicBoolean flag) { |
1100 |
+ |
// await(flag, LONG_DELAY_MS); |
1101 |
+ |
// } |
1102 |
+ |
|
1103 |
+ |
// /** |
1104 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1105 |
+ |
// */ |
1106 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1107 |
+ |
// long startTime = System.nanoTime(); |
1108 |
+ |
// while (!flag.get()) { |
1109 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1110 |
+ |
// throw new AssertionFailedError("timed out"); |
1111 |
+ |
// Thread.yield(); |
1112 |
+ |
// } |
1113 |
+ |
// } |
1114 |
+ |
|
1115 |
|
public static class NPETask implements Callable<String> { |
1116 |
|
public String call() { throw new NullPointerException(); } |
1117 |
|
} |
1122 |
|
|
1123 |
|
public class ShortRunnable extends CheckedRunnable { |
1124 |
|
protected void realRun() throws Throwable { |
1125 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1125 |
> |
delay(SHORT_DELAY_MS); |
1126 |
|
} |
1127 |
|
} |
1128 |
|
|
1129 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1130 |
|
protected void realRun() throws InterruptedException { |
1131 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1131 |
> |
delay(SHORT_DELAY_MS); |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
|
public class SmallRunnable extends CheckedRunnable { |
1136 |
|
protected void realRun() throws Throwable { |
1137 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1137 |
> |
delay(SMALL_DELAY_MS); |
1138 |
|
} |
1139 |
|
} |
1140 |
|
|
1141 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1142 |
|
protected void realRun() { |
1143 |
|
try { |
1144 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1144 |
> |
delay(SMALL_DELAY_MS); |
1145 |
|
} catch (InterruptedException ok) {} |
1146 |
|
} |
1147 |
|
} |
1148 |
|
|
1149 |
|
public class SmallCallable extends CheckedCallable { |
1150 |
|
protected Object realCall() throws InterruptedException { |
1151 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1151 |
> |
delay(SMALL_DELAY_MS); |
1152 |
|
return Boolean.TRUE; |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
719 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
720 |
– |
protected void realRun() throws InterruptedException { |
721 |
– |
Thread.sleep(SMALL_DELAY_MS); |
722 |
– |
} |
723 |
– |
} |
724 |
– |
|
1156 |
|
public class MediumRunnable extends CheckedRunnable { |
1157 |
|
protected void realRun() throws Throwable { |
1158 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1158 |
> |
delay(MEDIUM_DELAY_MS); |
1159 |
|
} |
1160 |
|
} |
1161 |
|
|
1162 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1163 |
|
protected void realRun() throws InterruptedException { |
1164 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1164 |
> |
delay(MEDIUM_DELAY_MS); |
1165 |
|
} |
1166 |
|
} |
1167 |
|
|
1168 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1169 |
+ |
return new CheckedRunnable() { |
1170 |
+ |
protected void realRun() { |
1171 |
+ |
try { |
1172 |
+ |
delay(timeoutMillis); |
1173 |
+ |
} catch (InterruptedException ok) {} |
1174 |
+ |
}}; |
1175 |
+ |
} |
1176 |
+ |
|
1177 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1178 |
|
protected void realRun() { |
1179 |
|
try { |
1180 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1180 |
> |
delay(MEDIUM_DELAY_MS); |
1181 |
|
} catch (InterruptedException ok) {} |
1182 |
|
} |
1183 |
|
} |
1185 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1186 |
|
protected void realRun() { |
1187 |
|
try { |
1188 |
< |
Thread.sleep(LONG_DELAY_MS); |
1188 |
> |
delay(LONG_DELAY_MS); |
1189 |
|
} catch (InterruptedException ok) {} |
1190 |
|
} |
1191 |
|
} |
1199 |
|
} |
1200 |
|
} |
1201 |
|
|
1202 |
+ |
public interface TrackedRunnable extends Runnable { |
1203 |
+ |
boolean isDone(); |
1204 |
+ |
} |
1205 |
+ |
|
1206 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1207 |
+ |
return new TrackedRunnable() { |
1208 |
+ |
private volatile boolean done = false; |
1209 |
+ |
public boolean isDone() { return done; } |
1210 |
+ |
public void run() { |
1211 |
+ |
try { |
1212 |
+ |
delay(timeoutMillis); |
1213 |
+ |
done = true; |
1214 |
+ |
} catch (InterruptedException ok) {} |
1215 |
+ |
} |
1216 |
+ |
}; |
1217 |
+ |
} |
1218 |
+ |
|
1219 |
|
public static class TrackedShortRunnable implements Runnable { |
1220 |
|
public volatile boolean done = false; |
1221 |
|
public void run() { |
1222 |
|
try { |
1223 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1223 |
> |
delay(SHORT_DELAY_MS); |
1224 |
> |
done = true; |
1225 |
> |
} catch (InterruptedException ok) {} |
1226 |
> |
} |
1227 |
> |
} |
1228 |
> |
|
1229 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1230 |
> |
public volatile boolean done = false; |
1231 |
> |
public void run() { |
1232 |
> |
try { |
1233 |
> |
delay(SMALL_DELAY_MS); |
1234 |
|
done = true; |
1235 |
|
} catch (InterruptedException ok) {} |
1236 |
|
} |
1240 |
|
public volatile boolean done = false; |
1241 |
|
public void run() { |
1242 |
|
try { |
1243 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1243 |
> |
delay(MEDIUM_DELAY_MS); |
1244 |
|
done = true; |
1245 |
|
} catch (InterruptedException ok) {} |
1246 |
|
} |
1250 |
|
public volatile boolean done = false; |
1251 |
|
public void run() { |
1252 |
|
try { |
1253 |
< |
Thread.sleep(LONG_DELAY_MS); |
1253 |
> |
delay(LONG_DELAY_MS); |
1254 |
|
done = true; |
1255 |
|
} catch (InterruptedException ok) {} |
1256 |
|
} |
1267 |
|
public volatile boolean done = false; |
1268 |
|
public Object call() { |
1269 |
|
try { |
1270 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1270 |
> |
delay(SMALL_DELAY_MS); |
1271 |
|
done = true; |
1272 |
|
} catch (InterruptedException ok) {} |
1273 |
|
return Boolean.TRUE; |
1280 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1281 |
|
protected abstract void realCompute() throws Throwable; |
1282 |
|
|
1283 |
< |
public final void compute() { |
1283 |
> |
@Override protected final void compute() { |
1284 |
|
try { |
1285 |
|
realCompute(); |
1286 |
|
} catch (Throwable t) { |
1295 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1296 |
|
protected abstract T realCompute() throws Throwable; |
1297 |
|
|
1298 |
< |
public final T compute() { |
1298 |
> |
@Override protected final T compute() { |
1299 |
|
try { |
1300 |
|
return realCompute(); |
1301 |
|
} catch (Throwable t) { |
1313 |
|
ThreadPoolExecutor executor) {} |
1314 |
|
} |
1315 |
|
|
1316 |
+ |
/** |
1317 |
+ |
* A CyclicBarrier that uses timed await and fails with |
1318 |
+ |
* AssertionFailedErrors instead of throwing checked exceptions. |
1319 |
+ |
*/ |
1320 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1321 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1322 |
+ |
|
1323 |
+ |
public int await() { |
1324 |
+ |
try { |
1325 |
+ |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1326 |
+ |
} catch (TimeoutException e) { |
1327 |
+ |
throw new AssertionFailedError("timed out"); |
1328 |
+ |
} catch (Exception e) { |
1329 |
+ |
AssertionFailedError afe = |
1330 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1331 |
+ |
afe.initCause(e); |
1332 |
+ |
throw afe; |
1333 |
+ |
} |
1334 |
+ |
} |
1335 |
+ |
} |
1336 |
+ |
|
1337 |
+ |
void checkEmpty(BlockingQueue q) { |
1338 |
+ |
try { |
1339 |
+ |
assertTrue(q.isEmpty()); |
1340 |
+ |
assertEquals(0, q.size()); |
1341 |
+ |
assertNull(q.peek()); |
1342 |
+ |
assertNull(q.poll()); |
1343 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1344 |
+ |
assertEquals(q.toString(), "[]"); |
1345 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1346 |
+ |
assertFalse(q.iterator().hasNext()); |
1347 |
+ |
try { |
1348 |
+ |
q.element(); |
1349 |
+ |
shouldThrow(); |
1350 |
+ |
} catch (NoSuchElementException success) {} |
1351 |
+ |
try { |
1352 |
+ |
q.iterator().next(); |
1353 |
+ |
shouldThrow(); |
1354 |
+ |
} catch (NoSuchElementException success) {} |
1355 |
+ |
try { |
1356 |
+ |
q.remove(); |
1357 |
+ |
shouldThrow(); |
1358 |
+ |
} catch (NoSuchElementException success) {} |
1359 |
+ |
} catch (InterruptedException ie) { |
1360 |
+ |
threadUnexpectedException(ie); |
1361 |
+ |
} |
1362 |
+ |
} |
1363 |
+ |
|
1364 |
+ |
void assertSerialEquals(Object x, Object y) { |
1365 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1366 |
+ |
} |
1367 |
+ |
|
1368 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1369 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1370 |
+ |
} |
1371 |
+ |
|
1372 |
+ |
byte[] serialBytes(Object o) { |
1373 |
+ |
try { |
1374 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1375 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1376 |
+ |
oos.writeObject(o); |
1377 |
+ |
oos.flush(); |
1378 |
+ |
oos.close(); |
1379 |
+ |
return bos.toByteArray(); |
1380 |
+ |
} catch (Throwable t) { |
1381 |
+ |
threadUnexpectedException(t); |
1382 |
+ |
return new byte[0]; |
1383 |
+ |
} |
1384 |
+ |
} |
1385 |
+ |
|
1386 |
+ |
@SuppressWarnings("unchecked") |
1387 |
+ |
<T> T serialClone(T o) { |
1388 |
+ |
try { |
1389 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1390 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1391 |
+ |
T clone = (T) ois.readObject(); |
1392 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1393 |
+ |
return clone; |
1394 |
+ |
} catch (Throwable t) { |
1395 |
+ |
threadUnexpectedException(t); |
1396 |
+ |
return null; |
1397 |
+ |
} |
1398 |
+ |
} |
1399 |
+ |
|
1400 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1401 |
+ |
Runnable... throwingActions) { |
1402 |
+ |
for (Runnable throwingAction : throwingActions) { |
1403 |
+ |
boolean threw = false; |
1404 |
+ |
try { throwingAction.run(); } |
1405 |
+ |
catch (Throwable t) { |
1406 |
+ |
threw = true; |
1407 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1408 |
+ |
AssertionFailedError afe = |
1409 |
+ |
new AssertionFailedError |
1410 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1411 |
+ |
", got " + t.getClass().getName()); |
1412 |
+ |
afe.initCause(t); |
1413 |
+ |
threadUnexpectedException(afe); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
+ |
if (!threw) |
1417 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1418 |
+ |
} |
1419 |
+ |
} |
1420 |
|
} |