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.security.CodeSource; |
30 |
|
import java.security.Permission; |
31 |
|
import java.security.PermissionCollection; |
75 |
|
* |
76 |
|
* </ol> |
77 |
|
* |
78 |
< |
* <p> <b>Other notes</b> |
78 |
> |
* <p><b>Other notes</b> |
79 |
|
* <ul> |
80 |
|
* |
81 |
|
* <li> Usually, there is one testcase method per JSR166 method |
111 |
|
private static final boolean useSecurityManager = |
112 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
113 |
|
|
114 |
+ |
protected static final boolean expensiveTests = |
115 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
116 |
+ |
|
117 |
+ |
/** |
118 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
119 |
+ |
* take more than profileThreshold milliseconds to execute. |
120 |
+ |
*/ |
121 |
+ |
private static final boolean profileTests = |
122 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
123 |
+ |
|
124 |
+ |
/** |
125 |
+ |
* The number of milliseconds that tests are permitted for |
126 |
+ |
* execution without being reported, when profileTests is set. |
127 |
+ |
*/ |
128 |
+ |
private static final long profileThreshold = |
129 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
130 |
+ |
|
131 |
+ |
protected void runTest() throws Throwable { |
132 |
+ |
if (profileTests) |
133 |
+ |
runTestProfiled(); |
134 |
+ |
else |
135 |
+ |
super.runTest(); |
136 |
+ |
} |
137 |
+ |
|
138 |
+ |
protected void runTestProfiled() throws Throwable { |
139 |
+ |
long t0 = System.nanoTime(); |
140 |
+ |
try { |
141 |
+ |
super.runTest(); |
142 |
+ |
} finally { |
143 |
+ |
long elapsedMillis = |
144 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
145 |
+ |
if (elapsedMillis >= profileThreshold) |
146 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
147 |
+ |
} |
148 |
+ |
} |
149 |
+ |
|
150 |
|
/** |
151 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
151 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
> |
* Optional command line arg provides the number of iterations to |
153 |
> |
* repeat running the tests. |
154 |
|
*/ |
155 |
|
public static void main(String[] args) { |
156 |
|
if (useSecurityManager) { |
158 |
|
Policy.setPolicy(permissivePolicy()); |
159 |
|
System.setSecurityManager(new SecurityManager()); |
160 |
|
} |
161 |
< |
int iters = 1; |
162 |
< |
if (args.length > 0) |
110 |
< |
iters = Integer.parseInt(args[0]); |
161 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
162 |
> |
|
163 |
|
Test s = suite(); |
164 |
|
for (int i = 0; i < iters; ++i) { |
165 |
|
junit.textui.TestRunner.run(s); |
169 |
|
System.exit(0); |
170 |
|
} |
171 |
|
|
172 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
173 |
+ |
TestSuite suite = new TestSuite(); |
174 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
175 |
+ |
if (suiteOrClass instanceof TestSuite) |
176 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
177 |
+ |
else if (suiteOrClass instanceof Class) |
178 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
179 |
+ |
else |
180 |
+ |
throw new ClassCastException("not a test suite or class"); |
181 |
+ |
} |
182 |
+ |
return suite; |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
< |
* Collects all JSR166 unit tests as one suite |
216 |
> |
* Collects all JSR166 unit tests as one suite. |
217 |
|
*/ |
218 |
|
public static Test suite() { |
219 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
220 |
< |
|
221 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
222 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
223 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
224 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
225 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
226 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
227 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
228 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
229 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
230 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
231 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
232 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
233 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
234 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
235 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
236 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
237 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
238 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
239 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
240 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
241 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
242 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
243 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
244 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
245 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
246 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
247 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
248 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
249 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
250 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
251 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
252 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
253 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
254 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
255 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
256 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
257 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
258 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
259 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
260 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
261 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
262 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
263 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
264 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
265 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
266 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
267 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
268 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
269 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
270 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
271 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
272 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
273 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
274 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
275 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
276 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
277 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
278 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
279 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
280 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
281 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
282 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
283 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
284 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
> |
ForkJoinPoolTest.suite(), |
222 |
> |
ForkJoinTaskTest.suite(), |
223 |
> |
RecursiveActionTest.suite(), |
224 |
> |
RecursiveTaskTest.suite(), |
225 |
> |
LinkedTransferQueueTest.suite(), |
226 |
> |
PhaserTest.suite(), |
227 |
> |
ThreadLocalRandomTest.suite(), |
228 |
> |
AbstractExecutorServiceTest.suite(), |
229 |
> |
AbstractQueueTest.suite(), |
230 |
> |
AbstractQueuedSynchronizerTest.suite(), |
231 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
232 |
> |
ArrayBlockingQueueTest.suite(), |
233 |
> |
ArrayDequeTest.suite(), |
234 |
> |
AtomicBooleanTest.suite(), |
235 |
> |
AtomicIntegerArrayTest.suite(), |
236 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
237 |
> |
AtomicIntegerTest.suite(), |
238 |
> |
AtomicLongArrayTest.suite(), |
239 |
> |
AtomicLongFieldUpdaterTest.suite(), |
240 |
> |
AtomicLongTest.suite(), |
241 |
> |
AtomicMarkableReferenceTest.suite(), |
242 |
> |
AtomicReferenceArrayTest.suite(), |
243 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
244 |
> |
AtomicReferenceTest.suite(), |
245 |
> |
AtomicStampedReferenceTest.suite(), |
246 |
> |
ConcurrentHashMapTest.suite(), |
247 |
> |
ConcurrentLinkedDequeTest.suite(), |
248 |
> |
ConcurrentLinkedQueueTest.suite(), |
249 |
> |
ConcurrentSkipListMapTest.suite(), |
250 |
> |
ConcurrentSkipListSubMapTest.suite(), |
251 |
> |
ConcurrentSkipListSetTest.suite(), |
252 |
> |
ConcurrentSkipListSubSetTest.suite(), |
253 |
> |
CopyOnWriteArrayListTest.suite(), |
254 |
> |
CopyOnWriteArraySetTest.suite(), |
255 |
> |
CountDownLatchTest.suite(), |
256 |
> |
CyclicBarrierTest.suite(), |
257 |
> |
DelayQueueTest.suite(), |
258 |
> |
EntryTest.suite(), |
259 |
> |
ExchangerTest.suite(), |
260 |
> |
ExecutorsTest.suite(), |
261 |
> |
ExecutorCompletionServiceTest.suite(), |
262 |
> |
FutureTaskTest.suite(), |
263 |
> |
LinkedBlockingDequeTest.suite(), |
264 |
> |
LinkedBlockingQueueTest.suite(), |
265 |
> |
LinkedListTest.suite(), |
266 |
> |
LockSupportTest.suite(), |
267 |
> |
PriorityBlockingQueueTest.suite(), |
268 |
> |
PriorityQueueTest.suite(), |
269 |
> |
ReentrantLockTest.suite(), |
270 |
> |
ReentrantReadWriteLockTest.suite(), |
271 |
> |
ScheduledExecutorTest.suite(), |
272 |
> |
ScheduledExecutorSubclassTest.suite(), |
273 |
> |
SemaphoreTest.suite(), |
274 |
> |
SynchronousQueueTest.suite(), |
275 |
> |
SystemTest.suite(), |
276 |
> |
ThreadLocalTest.suite(), |
277 |
> |
ThreadPoolExecutorTest.suite(), |
278 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
279 |
> |
ThreadTest.suite(), |
280 |
> |
TimeUnitTest.suite(), |
281 |
> |
TreeMapTest.suite(), |
282 |
> |
TreeSetTest.suite(), |
283 |
> |
TreeSubMapTest.suite(), |
284 |
> |
TreeSubSetTest.suite()); |
285 |
> |
|
286 |
> |
// Java8+ test classes |
287 |
> |
if (atLeastJava8()) { |
288 |
> |
String[] java8TestClassNames = { |
289 |
> |
"CompletableFutureTest", |
290 |
> |
"ConcurrentHashMap8Test", |
291 |
> |
"CountedCompleterTest", |
292 |
> |
"DoubleAccumulatorTest", |
293 |
> |
"DoubleAdderTest", |
294 |
> |
"ForkJoinPool8Test", |
295 |
> |
"LongAccumulatorTest", |
296 |
> |
"LongAdderTest", |
297 |
> |
"StampedLockTest", |
298 |
> |
}; |
299 |
> |
addNamedTestClasses(suite, java8TestClassNames); |
300 |
> |
} |
301 |
|
|
302 |
|
return suite; |
303 |
|
} |
317 |
|
return 50; |
318 |
|
} |
319 |
|
|
209 |
– |
|
320 |
|
/** |
321 |
|
* Sets delays as multiples of SHORT_DELAY. |
322 |
|
*/ |
324 |
|
SHORT_DELAY_MS = getShortDelay(); |
325 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
326 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
327 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
327 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
328 |
> |
} |
329 |
> |
|
330 |
> |
/** |
331 |
> |
* Returns a timeout in milliseconds to be used in tests that |
332 |
> |
* verify that operations block or time out. |
333 |
> |
*/ |
334 |
> |
long timeoutMillis() { |
335 |
> |
return SHORT_DELAY_MS / 4; |
336 |
> |
} |
337 |
> |
|
338 |
> |
/** |
339 |
> |
* Returns a new Date instance representing a time delayMillis |
340 |
> |
* milliseconds in the future. |
341 |
> |
*/ |
342 |
> |
Date delayedDate(long delayMillis) { |
343 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
+ |
* Extra checks that get done for all test cases. |
368 |
+ |
* |
369 |
|
* Triggers test case failure if any thread assertions have failed, |
370 |
|
* by rethrowing, in the test harness thread, any exception recorded |
371 |
|
* earlier by threadRecordFailure. |
372 |
+ |
* |
373 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
374 |
|
*/ |
375 |
|
public void tearDown() throws Exception { |
376 |
< |
Throwable t = threadFailure.get(); |
376 |
> |
Throwable t = threadFailure.getAndSet(null); |
377 |
|
if (t != null) { |
378 |
|
if (t instanceof Error) |
379 |
|
throw (Error) t; |
381 |
|
throw (RuntimeException) t; |
382 |
|
else if (t instanceof Exception) |
383 |
|
throw (Exception) t; |
384 |
< |
else |
385 |
< |
throw new AssertionError(t); |
384 |
> |
else { |
385 |
> |
AssertionFailedError afe = |
386 |
> |
new AssertionFailedError(t.toString()); |
387 |
> |
afe.initCause(t); |
388 |
> |
throw afe; |
389 |
> |
} |
390 |
> |
} |
391 |
> |
|
392 |
> |
if (Thread.interrupted()) |
393 |
> |
throw new AssertionFailedError("interrupt status set in main thread"); |
394 |
> |
|
395 |
> |
checkForkJoinPoolThreadLeaks(); |
396 |
> |
} |
397 |
> |
|
398 |
> |
/** |
399 |
> |
* Find missing try { ... } finally { joinPool(e); } |
400 |
> |
*/ |
401 |
> |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
402 |
> |
Thread[] survivors = new Thread[5]; |
403 |
> |
int count = Thread.enumerate(survivors); |
404 |
> |
for (int i = 0; i < count; i++) { |
405 |
> |
Thread thread = survivors[i]; |
406 |
> |
String name = thread.getName(); |
407 |
> |
if (name.startsWith("ForkJoinPool-")) { |
408 |
> |
// give thread some time to terminate |
409 |
> |
thread.join(LONG_DELAY_MS); |
410 |
> |
if (!thread.isAlive()) continue; |
411 |
> |
thread.stop(); |
412 |
> |
throw new AssertionFailedError |
413 |
> |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
414 |
> |
toString(), name)); |
415 |
> |
} |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
|
* Just like fail(reason), but additionally recording (using |
421 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
422 |
< |
* testcase will fail. |
421 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
422 |
> |
* the current testcase will fail. |
423 |
|
*/ |
424 |
|
public void threadFail(String reason) { |
425 |
|
try { |
426 |
|
fail(reason); |
427 |
< |
} catch (Throwable t) { |
427 |
> |
} catch (AssertionFailedError t) { |
428 |
|
threadRecordFailure(t); |
429 |
|
fail(reason); |
430 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
|
* Just like assertTrue(b), but additionally recording (using |
435 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
436 |
< |
* testcase will fail. |
435 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
436 |
> |
* the current testcase will fail. |
437 |
|
*/ |
438 |
|
public void threadAssertTrue(boolean b) { |
439 |
|
try { |
440 |
|
assertTrue(b); |
441 |
< |
} catch (AssertionError t) { |
441 |
> |
} catch (AssertionFailedError t) { |
442 |
|
threadRecordFailure(t); |
443 |
|
throw t; |
444 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* Just like assertFalse(b), but additionally recording (using |
449 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
450 |
< |
* current testcase will fail. |
449 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
450 |
> |
* the current testcase will fail. |
451 |
|
*/ |
452 |
|
public void threadAssertFalse(boolean b) { |
453 |
|
try { |
454 |
|
assertFalse(b); |
455 |
< |
} catch (AssertionError t) { |
455 |
> |
} catch (AssertionFailedError t) { |
456 |
|
threadRecordFailure(t); |
457 |
|
throw t; |
458 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
|
* Just like assertNull(x), but additionally recording (using |
463 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
464 |
< |
* current testcase will fail. |
463 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
464 |
> |
* the current testcase will fail. |
465 |
|
*/ |
466 |
|
public void threadAssertNull(Object x) { |
467 |
|
try { |
468 |
|
assertNull(x); |
469 |
< |
} catch (AssertionError t) { |
469 |
> |
} catch (AssertionFailedError t) { |
470 |
|
threadRecordFailure(t); |
471 |
|
throw t; |
472 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
|
* Just like assertEquals(x, y), but additionally recording (using |
477 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
478 |
< |
* current testcase will fail. |
477 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
478 |
> |
* the current testcase will fail. |
479 |
|
*/ |
480 |
|
public void threadAssertEquals(long x, long y) { |
481 |
|
try { |
482 |
|
assertEquals(x, y); |
483 |
< |
} catch (AssertionError t) { |
483 |
> |
} catch (AssertionFailedError t) { |
484 |
|
threadRecordFailure(t); |
485 |
|
throw t; |
486 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
|
* Just like assertEquals(x, y), but additionally recording (using |
491 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
492 |
< |
* current testcase will fail. |
491 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
492 |
> |
* the current testcase will fail. |
493 |
|
*/ |
494 |
|
public void threadAssertEquals(Object x, Object y) { |
495 |
|
try { |
496 |
|
assertEquals(x, y); |
497 |
< |
} catch (AssertionError t) { |
497 |
> |
} catch (AssertionFailedError t) { |
498 |
|
threadRecordFailure(t); |
499 |
|
throw t; |
500 |
+ |
} catch (Throwable t) { |
501 |
+ |
threadUnexpectedException(t); |
502 |
|
} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
|
* Just like assertSame(x, y), but additionally recording (using |
507 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
508 |
< |
* current testcase will fail. |
507 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
508 |
> |
* the current testcase will fail. |
509 |
|
*/ |
510 |
|
public void threadAssertSame(Object x, Object y) { |
511 |
|
try { |
512 |
|
assertSame(x, y); |
513 |
< |
} catch (AssertionError t) { |
513 |
> |
} catch (AssertionFailedError t) { |
514 |
|
threadRecordFailure(t); |
515 |
|
throw t; |
516 |
|
} |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* Calls threadFail with message "Unexpected exception" + ex. |
534 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
535 |
> |
* then rethrows the exception, wrapping it in an |
536 |
> |
* AssertionFailedError if necessary. |
537 |
|
*/ |
538 |
|
public void threadUnexpectedException(Throwable t) { |
539 |
|
threadRecordFailure(t); |
540 |
|
t.printStackTrace(); |
377 |
– |
// Rethrow, wrapping in an AssertionError if necessary |
541 |
|
if (t instanceof RuntimeException) |
542 |
|
throw (RuntimeException) t; |
543 |
|
else if (t instanceof Error) |
544 |
|
throw (Error) t; |
545 |
|
else { |
546 |
< |
AssertionError ae = new AssertionError("unexpected exception: " + t); |
547 |
< |
t.initCause(t); |
548 |
< |
throw ae; |
549 |
< |
} |
546 |
> |
AssertionFailedError afe = |
547 |
> |
new AssertionFailedError("unexpected exception: " + t); |
548 |
> |
afe.initCause(t); |
549 |
> |
throw afe; |
550 |
> |
} |
551 |
> |
} |
552 |
> |
|
553 |
> |
/** |
554 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
555 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
556 |
> |
* until time elapses. |
557 |
> |
*/ |
558 |
> |
static void delay(long millis) throws InterruptedException { |
559 |
> |
long startTime = System.nanoTime(); |
560 |
> |
long ns = millis * 1000 * 1000; |
561 |
> |
for (;;) { |
562 |
> |
if (millis > 0L) |
563 |
> |
Thread.sleep(millis); |
564 |
> |
else // too short to sleep |
565 |
> |
Thread.yield(); |
566 |
> |
long d = ns - (System.nanoTime() - startTime); |
567 |
> |
if (d > 0L) |
568 |
> |
millis = d / (1000 * 1000); |
569 |
> |
else |
570 |
> |
break; |
571 |
> |
} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* Waits out termination of a thread pool or fails doing so. |
576 |
|
*/ |
577 |
< |
public void joinPool(ExecutorService exec) { |
577 |
> |
void joinPool(ExecutorService exec) { |
578 |
|
try { |
579 |
|
exec.shutdown(); |
580 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
581 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
581 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
582 |
|
} catch (SecurityException ok) { |
583 |
|
// Allowed in case test doesn't have privs |
584 |
|
} catch (InterruptedException ie) { |
586 |
|
} |
587 |
|
} |
588 |
|
|
589 |
+ |
/** |
590 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
591 |
+ |
*/ |
592 |
+ |
static void printAllStackTraces() { |
593 |
+ |
for (ThreadInfo info : |
594 |
+ |
ManagementFactory.getThreadMXBean() |
595 |
+ |
.dumpAllThreads(true, true)) |
596 |
+ |
System.err.print(info); |
597 |
+ |
} |
598 |
+ |
|
599 |
+ |
/** |
600 |
+ |
* Checks that thread does not terminate within the default |
601 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
602 |
+ |
*/ |
603 |
+ |
void assertThreadStaysAlive(Thread thread) { |
604 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
605 |
+ |
} |
606 |
+ |
|
607 |
+ |
/** |
608 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
609 |
+ |
*/ |
610 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
611 |
+ |
try { |
612 |
+ |
// No need to optimize the failing case via Thread.join. |
613 |
+ |
delay(millis); |
614 |
+ |
assertTrue(thread.isAlive()); |
615 |
+ |
} catch (InterruptedException ie) { |
616 |
+ |
fail("Unexpected InterruptedException"); |
617 |
+ |
} |
618 |
+ |
} |
619 |
+ |
|
620 |
+ |
/** |
621 |
+ |
* Checks that the threads do not terminate within the default |
622 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
623 |
+ |
*/ |
624 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
625 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
626 |
+ |
} |
627 |
+ |
|
628 |
+ |
/** |
629 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
630 |
+ |
*/ |
631 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
632 |
+ |
try { |
633 |
+ |
// No need to optimize the failing case via Thread.join. |
634 |
+ |
delay(millis); |
635 |
+ |
for (Thread thread : threads) |
636 |
+ |
assertTrue(thread.isAlive()); |
637 |
+ |
} catch (InterruptedException ie) { |
638 |
+ |
fail("Unexpected InterruptedException"); |
639 |
+ |
} |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
/** |
643 |
+ |
* Checks that future.get times out, with the default timeout of |
644 |
+ |
* {@code timeoutMillis()}. |
645 |
+ |
*/ |
646 |
+ |
void assertFutureTimesOut(Future future) { |
647 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
652 |
+ |
*/ |
653 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
654 |
+ |
long startTime = System.nanoTime(); |
655 |
+ |
try { |
656 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
657 |
+ |
shouldThrow(); |
658 |
+ |
} catch (TimeoutException success) { |
659 |
+ |
} catch (Exception e) { |
660 |
+ |
threadUnexpectedException(e); |
661 |
+ |
} finally { future.cancel(true); } |
662 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
663 |
+ |
} |
664 |
|
|
665 |
|
/** |
666 |
|
* Fails with message "should throw exception". |
677 |
|
} |
678 |
|
|
679 |
|
/** |
420 |
– |
* Fails with message "Unexpected exception: " + ex. |
421 |
– |
*/ |
422 |
– |
public void unexpectedException(Throwable ex) { |
423 |
– |
ex.printStackTrace(); |
424 |
– |
fail("Unexpected exception: " + ex); |
425 |
– |
} |
426 |
– |
|
427 |
– |
|
428 |
– |
/** |
680 |
|
* The number of elements to place in collections, arrays, etc. |
681 |
|
*/ |
682 |
|
public static final int SIZE = 20; |
713 |
|
SecurityManager sm = System.getSecurityManager(); |
714 |
|
if (sm == null) { |
715 |
|
r.run(); |
716 |
+ |
} |
717 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
718 |
+ |
} |
719 |
+ |
|
720 |
+ |
/** |
721 |
+ |
* Runs Runnable r with a security policy that permits precisely |
722 |
+ |
* the specified permissions. If there is no current security |
723 |
+ |
* manager, a temporary one is set for the duration of the |
724 |
+ |
* Runnable. We require that any security manager permit |
725 |
+ |
* getPolicy/setPolicy. |
726 |
+ |
*/ |
727 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
728 |
+ |
Permission... permissions) { |
729 |
+ |
SecurityManager sm = System.getSecurityManager(); |
730 |
+ |
if (sm == null) { |
731 |
|
Policy savedPolicy = Policy.getPolicy(); |
732 |
|
try { |
733 |
|
Policy.setPolicy(permissivePolicy()); |
734 |
|
System.setSecurityManager(new SecurityManager()); |
735 |
< |
runWithPermissions(r, permissions); |
735 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
736 |
|
} finally { |
737 |
|
System.setSecurityManager(null); |
738 |
|
Policy.setPolicy(savedPolicy); |
780 |
|
return perms.implies(p); |
781 |
|
} |
782 |
|
public void refresh() {} |
783 |
+ |
public String toString() { |
784 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
785 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
786 |
+ |
ps.add(e.nextElement()); |
787 |
+ |
return "AdjustablePolicy with permissions " + ps; |
788 |
+ |
} |
789 |
|
} |
790 |
|
|
791 |
|
/** |
808 |
|
} |
809 |
|
|
810 |
|
/** |
811 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
812 |
< |
* Does <em>NOT</em> throw InterruptedException. |
811 |
> |
* Sleeps until the given time has elapsed. |
812 |
> |
* Throws AssertionFailedError if interrupted. |
813 |
|
*/ |
814 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
814 |
> |
void sleep(long millis) { |
815 |
|
try { |
816 |
< |
Thread.sleep(timeoutMillis); |
817 |
< |
} catch (InterruptedException wakeup) {} |
816 |
> |
delay(millis); |
817 |
> |
} catch (InterruptedException ie) { |
818 |
> |
AssertionFailedError afe = |
819 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
820 |
> |
afe.initCause(ie); |
821 |
> |
throw afe; |
822 |
> |
} |
823 |
> |
} |
824 |
> |
|
825 |
> |
/** |
826 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
827 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
828 |
> |
*/ |
829 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
830 |
> |
long startTime = System.nanoTime(); |
831 |
> |
for (;;) { |
832 |
> |
Thread.State s = thread.getState(); |
833 |
> |
if (s == Thread.State.BLOCKED || |
834 |
> |
s == Thread.State.WAITING || |
835 |
> |
s == Thread.State.TIMED_WAITING) |
836 |
> |
return; |
837 |
> |
else if (s == Thread.State.TERMINATED) |
838 |
> |
fail("Unexpected thread termination"); |
839 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
840 |
> |
threadAssertTrue(thread.isAlive()); |
841 |
> |
return; |
842 |
> |
} |
843 |
> |
Thread.yield(); |
844 |
> |
} |
845 |
|
} |
846 |
|
|
847 |
|
/** |
848 |
< |
* Returns a new started Thread running the given runnable. |
848 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
849 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
850 |
> |
*/ |
851 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
852 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
853 |
> |
} |
854 |
> |
|
855 |
> |
/** |
856 |
> |
* Returns the number of milliseconds since time given by |
857 |
> |
* startNanoTime, which must have been previously returned from a |
858 |
> |
* call to {@link System.nanoTime()}. |
859 |
> |
*/ |
860 |
> |
long millisElapsedSince(long startNanoTime) { |
861 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
862 |
> |
} |
863 |
> |
|
864 |
> |
/** |
865 |
> |
* Returns a new started daemon Thread running the given runnable. |
866 |
|
*/ |
867 |
|
Thread newStartedThread(Runnable runnable) { |
868 |
|
Thread t = new Thread(runnable); |
869 |
+ |
t.setDaemon(true); |
870 |
|
t.start(); |
871 |
|
return t; |
872 |
|
} |
873 |
|
|
874 |
+ |
/** |
875 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
876 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
877 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
878 |
+ |
*/ |
879 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
880 |
+ |
try { |
881 |
+ |
t.join(timeoutMillis); |
882 |
+ |
} catch (InterruptedException ie) { |
883 |
+ |
threadUnexpectedException(ie); |
884 |
+ |
} finally { |
885 |
+ |
if (t.getState() != Thread.State.TERMINATED) { |
886 |
+ |
t.interrupt(); |
887 |
+ |
fail("Test timed out"); |
888 |
+ |
} |
889 |
+ |
} |
890 |
+ |
} |
891 |
+ |
|
892 |
+ |
/** |
893 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
894 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
895 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
896 |
+ |
*/ |
897 |
+ |
void awaitTermination(Thread t) { |
898 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
899 |
+ |
} |
900 |
+ |
|
901 |
|
// Some convenient Runnable classes |
902 |
|
|
903 |
|
public abstract class CheckedRunnable implements Runnable { |
960 |
|
realRun(); |
961 |
|
threadShouldThrow("InterruptedException"); |
962 |
|
} catch (InterruptedException success) { |
963 |
+ |
threadAssertFalse(Thread.interrupted()); |
964 |
|
} catch (Throwable t) { |
965 |
|
threadUnexpectedException(t); |
966 |
|
} |
990 |
|
threadShouldThrow("InterruptedException"); |
991 |
|
return result; |
992 |
|
} catch (InterruptedException success) { |
993 |
+ |
threadAssertFalse(Thread.interrupted()); |
994 |
|
} catch (Throwable t) { |
995 |
|
threadUnexpectedException(t); |
996 |
|
} |
1014 |
|
|
1015 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1016 |
|
return new CheckedCallable<String>() { |
1017 |
< |
public String realCall() { |
1017 |
> |
protected String realCall() { |
1018 |
|
try { |
1019 |
|
latch.await(); |
1020 |
|
} catch (InterruptedException quittingTime) {} |
1022 |
|
}}; |
1023 |
|
} |
1024 |
|
|
1025 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1026 |
+ |
return new CheckedRunnable() { |
1027 |
+ |
public void realRun() throws InterruptedException { |
1028 |
+ |
await(latch); |
1029 |
+ |
}}; |
1030 |
+ |
} |
1031 |
+ |
|
1032 |
+ |
public void await(CountDownLatch latch) { |
1033 |
+ |
try { |
1034 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1035 |
+ |
} catch (Throwable t) { |
1036 |
+ |
threadUnexpectedException(t); |
1037 |
+ |
} |
1038 |
+ |
} |
1039 |
+ |
|
1040 |
+ |
public void await(Semaphore semaphore) { |
1041 |
+ |
try { |
1042 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1043 |
+ |
} catch (Throwable t) { |
1044 |
+ |
threadUnexpectedException(t); |
1045 |
+ |
} |
1046 |
+ |
} |
1047 |
+ |
|
1048 |
+ |
// /** |
1049 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1050 |
+ |
// */ |
1051 |
+ |
// public void await(AtomicBoolean flag) { |
1052 |
+ |
// await(flag, LONG_DELAY_MS); |
1053 |
+ |
// } |
1054 |
+ |
|
1055 |
+ |
// /** |
1056 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1057 |
+ |
// */ |
1058 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1059 |
+ |
// long startTime = System.nanoTime(); |
1060 |
+ |
// while (!flag.get()) { |
1061 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1062 |
+ |
// throw new AssertionFailedError("timed out"); |
1063 |
+ |
// Thread.yield(); |
1064 |
+ |
// } |
1065 |
+ |
// } |
1066 |
+ |
|
1067 |
|
public static class NPETask implements Callable<String> { |
1068 |
|
public String call() { throw new NullPointerException(); } |
1069 |
|
} |
1074 |
|
|
1075 |
|
public class ShortRunnable extends CheckedRunnable { |
1076 |
|
protected void realRun() throws Throwable { |
1077 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1077 |
> |
delay(SHORT_DELAY_MS); |
1078 |
|
} |
1079 |
|
} |
1080 |
|
|
1081 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1082 |
|
protected void realRun() throws InterruptedException { |
1083 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1083 |
> |
delay(SHORT_DELAY_MS); |
1084 |
|
} |
1085 |
|
} |
1086 |
|
|
1087 |
|
public class SmallRunnable extends CheckedRunnable { |
1088 |
|
protected void realRun() throws Throwable { |
1089 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1089 |
> |
delay(SMALL_DELAY_MS); |
1090 |
|
} |
1091 |
|
} |
1092 |
|
|
1093 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1094 |
|
protected void realRun() { |
1095 |
|
try { |
1096 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1096 |
> |
delay(SMALL_DELAY_MS); |
1097 |
|
} catch (InterruptedException ok) {} |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1101 |
|
public class SmallCallable extends CheckedCallable { |
1102 |
|
protected Object realCall() throws InterruptedException { |
1103 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1103 |
> |
delay(SMALL_DELAY_MS); |
1104 |
|
return Boolean.TRUE; |
1105 |
|
} |
1106 |
|
} |
1107 |
|
|
720 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
721 |
– |
protected void realRun() throws InterruptedException { |
722 |
– |
Thread.sleep(SMALL_DELAY_MS); |
723 |
– |
} |
724 |
– |
} |
725 |
– |
|
1108 |
|
public class MediumRunnable extends CheckedRunnable { |
1109 |
|
protected void realRun() throws Throwable { |
1110 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1110 |
> |
delay(MEDIUM_DELAY_MS); |
1111 |
|
} |
1112 |
|
} |
1113 |
|
|
1114 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1115 |
|
protected void realRun() throws InterruptedException { |
1116 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1116 |
> |
delay(MEDIUM_DELAY_MS); |
1117 |
|
} |
1118 |
|
} |
1119 |
|
|
1120 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1121 |
+ |
return new CheckedRunnable() { |
1122 |
+ |
protected void realRun() { |
1123 |
+ |
try { |
1124 |
+ |
delay(timeoutMillis); |
1125 |
+ |
} catch (InterruptedException ok) {} |
1126 |
+ |
}}; |
1127 |
+ |
} |
1128 |
+ |
|
1129 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1130 |
|
protected void realRun() { |
1131 |
|
try { |
1132 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1132 |
> |
delay(MEDIUM_DELAY_MS); |
1133 |
|
} catch (InterruptedException ok) {} |
1134 |
|
} |
1135 |
|
} |
1137 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1138 |
|
protected void realRun() { |
1139 |
|
try { |
1140 |
< |
Thread.sleep(LONG_DELAY_MS); |
1140 |
> |
delay(LONG_DELAY_MS); |
1141 |
|
} catch (InterruptedException ok) {} |
1142 |
|
} |
1143 |
|
} |
1151 |
|
} |
1152 |
|
} |
1153 |
|
|
1154 |
+ |
public interface TrackedRunnable extends Runnable { |
1155 |
+ |
boolean isDone(); |
1156 |
+ |
} |
1157 |
+ |
|
1158 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1159 |
+ |
return new TrackedRunnable() { |
1160 |
+ |
private volatile boolean done = false; |
1161 |
+ |
public boolean isDone() { return done; } |
1162 |
+ |
public void run() { |
1163 |
+ |
try { |
1164 |
+ |
delay(timeoutMillis); |
1165 |
+ |
done = true; |
1166 |
+ |
} catch (InterruptedException ok) {} |
1167 |
+ |
} |
1168 |
+ |
}; |
1169 |
+ |
} |
1170 |
+ |
|
1171 |
|
public static class TrackedShortRunnable implements Runnable { |
1172 |
|
public volatile boolean done = false; |
1173 |
|
public void run() { |
1174 |
|
try { |
1175 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1175 |
> |
delay(SHORT_DELAY_MS); |
1176 |
> |
done = true; |
1177 |
> |
} catch (InterruptedException ok) {} |
1178 |
> |
} |
1179 |
> |
} |
1180 |
> |
|
1181 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1182 |
> |
public volatile boolean done = false; |
1183 |
> |
public void run() { |
1184 |
> |
try { |
1185 |
> |
delay(SMALL_DELAY_MS); |
1186 |
|
done = true; |
1187 |
|
} catch (InterruptedException ok) {} |
1188 |
|
} |
1192 |
|
public volatile boolean done = false; |
1193 |
|
public void run() { |
1194 |
|
try { |
1195 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1195 |
> |
delay(MEDIUM_DELAY_MS); |
1196 |
|
done = true; |
1197 |
|
} catch (InterruptedException ok) {} |
1198 |
|
} |
1202 |
|
public volatile boolean done = false; |
1203 |
|
public void run() { |
1204 |
|
try { |
1205 |
< |
Thread.sleep(LONG_DELAY_MS); |
1205 |
> |
delay(LONG_DELAY_MS); |
1206 |
|
done = true; |
1207 |
|
} catch (InterruptedException ok) {} |
1208 |
|
} |
1219 |
|
public volatile boolean done = false; |
1220 |
|
public Object call() { |
1221 |
|
try { |
1222 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1222 |
> |
delay(SMALL_DELAY_MS); |
1223 |
|
done = true; |
1224 |
|
} catch (InterruptedException ok) {} |
1225 |
|
return Boolean.TRUE; |
1265 |
|
ThreadPoolExecutor executor) {} |
1266 |
|
} |
1267 |
|
|
1268 |
+ |
/** |
1269 |
+ |
* A CyclicBarrier that uses timed await and fails with |
1270 |
+ |
* AssertionFailedErrors instead of throwing checked exceptions. |
1271 |
+ |
*/ |
1272 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1273 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1274 |
+ |
|
1275 |
+ |
public int await() { |
1276 |
+ |
try { |
1277 |
+ |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1278 |
+ |
} catch (TimeoutException e) { |
1279 |
+ |
throw new AssertionFailedError("timed out"); |
1280 |
+ |
} catch (Exception e) { |
1281 |
+ |
AssertionFailedError afe = |
1282 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1283 |
+ |
afe.initCause(e); |
1284 |
+ |
throw afe; |
1285 |
+ |
} |
1286 |
+ |
} |
1287 |
+ |
} |
1288 |
+ |
|
1289 |
+ |
void checkEmpty(BlockingQueue q) { |
1290 |
+ |
try { |
1291 |
+ |
assertTrue(q.isEmpty()); |
1292 |
+ |
assertEquals(0, q.size()); |
1293 |
+ |
assertNull(q.peek()); |
1294 |
+ |
assertNull(q.poll()); |
1295 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1296 |
+ |
assertEquals(q.toString(), "[]"); |
1297 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1298 |
+ |
assertFalse(q.iterator().hasNext()); |
1299 |
+ |
try { |
1300 |
+ |
q.element(); |
1301 |
+ |
shouldThrow(); |
1302 |
+ |
} catch (NoSuchElementException success) {} |
1303 |
+ |
try { |
1304 |
+ |
q.iterator().next(); |
1305 |
+ |
shouldThrow(); |
1306 |
+ |
} catch (NoSuchElementException success) {} |
1307 |
+ |
try { |
1308 |
+ |
q.remove(); |
1309 |
+ |
shouldThrow(); |
1310 |
+ |
} catch (NoSuchElementException success) {} |
1311 |
+ |
} catch (InterruptedException ie) { |
1312 |
+ |
threadUnexpectedException(ie); |
1313 |
+ |
} |
1314 |
+ |
} |
1315 |
+ |
|
1316 |
+ |
void assertSerialEquals(Object x, Object y) { |
1317 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1318 |
+ |
} |
1319 |
+ |
|
1320 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1321 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1322 |
+ |
} |
1323 |
+ |
|
1324 |
+ |
byte[] serialBytes(Object o) { |
1325 |
+ |
try { |
1326 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1327 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1328 |
+ |
oos.writeObject(o); |
1329 |
+ |
oos.flush(); |
1330 |
+ |
oos.close(); |
1331 |
+ |
return bos.toByteArray(); |
1332 |
+ |
} catch (Throwable t) { |
1333 |
+ |
threadUnexpectedException(t); |
1334 |
+ |
return new byte[0]; |
1335 |
+ |
} |
1336 |
+ |
} |
1337 |
+ |
|
1338 |
+ |
@SuppressWarnings("unchecked") |
1339 |
+ |
<T> T serialClone(T o) { |
1340 |
+ |
try { |
1341 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1342 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1343 |
+ |
T clone = (T) ois.readObject(); |
1344 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1345 |
+ |
return clone; |
1346 |
+ |
} catch (Throwable t) { |
1347 |
+ |
threadUnexpectedException(t); |
1348 |
+ |
return null; |
1349 |
+ |
} |
1350 |
+ |
} |
1351 |
|
} |