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.util.Arrays; |
15 |
+ |
import java.util.Date; |
16 |
+ |
import java.util.NoSuchElementException; |
17 |
|
import java.util.PropertyPermission; |
18 |
|
import java.util.concurrent.*; |
19 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
20 |
|
import java.util.concurrent.atomic.AtomicReference; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
23 |
|
import java.security.CodeSource; |
24 |
|
import java.security.Permission; |
25 |
|
import java.security.PermissionCollection; |
105 |
|
private static final boolean useSecurityManager = |
106 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
107 |
|
|
108 |
+ |
protected static final boolean expensiveTests = |
109 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
110 |
+ |
|
111 |
+ |
/** |
112 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
113 |
+ |
* take more than profileThreshold milliseconds to execute. |
114 |
+ |
*/ |
115 |
+ |
private static final boolean profileTests = |
116 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
117 |
+ |
|
118 |
+ |
/** |
119 |
+ |
* The number of milliseconds that tests are permitted for |
120 |
+ |
* execution without being reported, when profileTests is set. |
121 |
+ |
*/ |
122 |
+ |
private static final long profileThreshold = |
123 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
124 |
+ |
|
125 |
+ |
protected void runTest() throws Throwable { |
126 |
+ |
if (profileTests) |
127 |
+ |
runTestProfiled(); |
128 |
+ |
else |
129 |
+ |
super.runTest(); |
130 |
+ |
} |
131 |
+ |
|
132 |
+ |
protected void runTestProfiled() throws Throwable { |
133 |
+ |
long t0 = System.nanoTime(); |
134 |
+ |
try { |
135 |
+ |
super.runTest(); |
136 |
+ |
} finally { |
137 |
+ |
long elapsedMillis = |
138 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
139 |
+ |
if (elapsedMillis >= profileThreshold) |
140 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
141 |
+ |
} |
142 |
+ |
} |
143 |
+ |
|
144 |
|
/** |
145 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
146 |
|
*/ |
161 |
|
System.exit(0); |
162 |
|
} |
163 |
|
|
164 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
165 |
+ |
TestSuite suite = new TestSuite(); |
166 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
167 |
+ |
if (suiteOrClass instanceof TestSuite) |
168 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
169 |
+ |
else if (suiteOrClass instanceof Class) |
170 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
171 |
+ |
else |
172 |
+ |
throw new ClassCastException("not a test suite or class"); |
173 |
+ |
} |
174 |
+ |
return suite; |
175 |
+ |
} |
176 |
+ |
|
177 |
|
/** |
178 |
< |
* Collects all JSR166 unit tests as one suite |
178 |
> |
* Collects all JSR166 unit tests as one suite. |
179 |
|
*/ |
180 |
|
public static Test suite() { |
181 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
182 |
< |
|
183 |
< |
suite.addTest(ForkJoinPoolTest.suite()); |
184 |
< |
suite.addTest(ForkJoinTaskTest.suite()); |
185 |
< |
suite.addTest(RecursiveActionTest.suite()); |
186 |
< |
suite.addTest(RecursiveTaskTest.suite()); |
187 |
< |
suite.addTest(LinkedTransferQueueTest.suite()); |
188 |
< |
suite.addTest(PhaserTest.suite()); |
189 |
< |
suite.addTest(ThreadLocalRandomTest.suite()); |
190 |
< |
suite.addTest(AbstractExecutorServiceTest.suite()); |
191 |
< |
suite.addTest(AbstractQueueTest.suite()); |
192 |
< |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
193 |
< |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
194 |
< |
suite.addTest(ArrayBlockingQueueTest.suite()); |
195 |
< |
suite.addTest(ArrayDequeTest.suite()); |
196 |
< |
suite.addTest(AtomicBooleanTest.suite()); |
197 |
< |
suite.addTest(AtomicIntegerArrayTest.suite()); |
198 |
< |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
199 |
< |
suite.addTest(AtomicIntegerTest.suite()); |
200 |
< |
suite.addTest(AtomicLongArrayTest.suite()); |
201 |
< |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
202 |
< |
suite.addTest(AtomicLongTest.suite()); |
203 |
< |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
204 |
< |
suite.addTest(AtomicReferenceArrayTest.suite()); |
205 |
< |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
206 |
< |
suite.addTest(AtomicReferenceTest.suite()); |
207 |
< |
suite.addTest(AtomicStampedReferenceTest.suite()); |
208 |
< |
suite.addTest(ConcurrentHashMapTest.suite()); |
209 |
< |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
210 |
< |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
211 |
< |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
212 |
< |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
213 |
< |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
214 |
< |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
215 |
< |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
216 |
< |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
217 |
< |
suite.addTest(CountDownLatchTest.suite()); |
218 |
< |
suite.addTest(CyclicBarrierTest.suite()); |
219 |
< |
suite.addTest(DelayQueueTest.suite()); |
220 |
< |
suite.addTest(EntryTest.suite()); |
221 |
< |
suite.addTest(ExchangerTest.suite()); |
222 |
< |
suite.addTest(ExecutorsTest.suite()); |
223 |
< |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
224 |
< |
suite.addTest(FutureTaskTest.suite()); |
225 |
< |
suite.addTest(LinkedBlockingDequeTest.suite()); |
226 |
< |
suite.addTest(LinkedBlockingQueueTest.suite()); |
227 |
< |
suite.addTest(LinkedListTest.suite()); |
228 |
< |
suite.addTest(LockSupportTest.suite()); |
229 |
< |
suite.addTest(PriorityBlockingQueueTest.suite()); |
230 |
< |
suite.addTest(PriorityQueueTest.suite()); |
231 |
< |
suite.addTest(ReentrantLockTest.suite()); |
232 |
< |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
233 |
< |
suite.addTest(ScheduledExecutorTest.suite()); |
234 |
< |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
235 |
< |
suite.addTest(SemaphoreTest.suite()); |
236 |
< |
suite.addTest(SynchronousQueueTest.suite()); |
237 |
< |
suite.addTest(SystemTest.suite()); |
238 |
< |
suite.addTest(ThreadLocalTest.suite()); |
239 |
< |
suite.addTest(ThreadPoolExecutorTest.suite()); |
240 |
< |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
241 |
< |
suite.addTest(ThreadTest.suite()); |
242 |
< |
suite.addTest(TimeUnitTest.suite()); |
243 |
< |
suite.addTest(TreeMapTest.suite()); |
244 |
< |
suite.addTest(TreeSetTest.suite()); |
245 |
< |
suite.addTest(TreeSubMapTest.suite()); |
188 |
< |
suite.addTest(TreeSubSetTest.suite()); |
189 |
< |
|
190 |
< |
return suite; |
181 |
> |
return newTestSuite( |
182 |
> |
ForkJoinPoolTest.suite(), |
183 |
> |
ForkJoinTaskTest.suite(), |
184 |
> |
RecursiveActionTest.suite(), |
185 |
> |
RecursiveTaskTest.suite(), |
186 |
> |
LinkedTransferQueueTest.suite(), |
187 |
> |
PhaserTest.suite(), |
188 |
> |
ThreadLocalRandomTest.suite(), |
189 |
> |
AbstractExecutorServiceTest.suite(), |
190 |
> |
AbstractQueueTest.suite(), |
191 |
> |
AbstractQueuedSynchronizerTest.suite(), |
192 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
193 |
> |
ArrayBlockingQueueTest.suite(), |
194 |
> |
ArrayDequeTest.suite(), |
195 |
> |
AtomicBooleanTest.suite(), |
196 |
> |
AtomicIntegerArrayTest.suite(), |
197 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
198 |
> |
AtomicIntegerTest.suite(), |
199 |
> |
AtomicLongArrayTest.suite(), |
200 |
> |
AtomicLongFieldUpdaterTest.suite(), |
201 |
> |
AtomicLongTest.suite(), |
202 |
> |
AtomicMarkableReferenceTest.suite(), |
203 |
> |
AtomicReferenceArrayTest.suite(), |
204 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
205 |
> |
AtomicReferenceTest.suite(), |
206 |
> |
AtomicStampedReferenceTest.suite(), |
207 |
> |
ConcurrentHashMapTest.suite(), |
208 |
> |
ConcurrentLinkedDequeTest.suite(), |
209 |
> |
ConcurrentLinkedQueueTest.suite(), |
210 |
> |
ConcurrentSkipListMapTest.suite(), |
211 |
> |
ConcurrentSkipListSubMapTest.suite(), |
212 |
> |
ConcurrentSkipListSetTest.suite(), |
213 |
> |
ConcurrentSkipListSubSetTest.suite(), |
214 |
> |
CopyOnWriteArrayListTest.suite(), |
215 |
> |
CopyOnWriteArraySetTest.suite(), |
216 |
> |
CountDownLatchTest.suite(), |
217 |
> |
CyclicBarrierTest.suite(), |
218 |
> |
DelayQueueTest.suite(), |
219 |
> |
EntryTest.suite(), |
220 |
> |
ExchangerTest.suite(), |
221 |
> |
ExecutorsTest.suite(), |
222 |
> |
ExecutorCompletionServiceTest.suite(), |
223 |
> |
FutureTaskTest.suite(), |
224 |
> |
LinkedBlockingDequeTest.suite(), |
225 |
> |
LinkedBlockingQueueTest.suite(), |
226 |
> |
LinkedListTest.suite(), |
227 |
> |
LockSupportTest.suite(), |
228 |
> |
PriorityBlockingQueueTest.suite(), |
229 |
> |
PriorityQueueTest.suite(), |
230 |
> |
ReentrantLockTest.suite(), |
231 |
> |
ReentrantReadWriteLockTest.suite(), |
232 |
> |
ScheduledExecutorTest.suite(), |
233 |
> |
ScheduledExecutorSubclassTest.suite(), |
234 |
> |
SemaphoreTest.suite(), |
235 |
> |
SynchronousQueueTest.suite(), |
236 |
> |
SystemTest.suite(), |
237 |
> |
ThreadLocalTest.suite(), |
238 |
> |
ThreadPoolExecutorTest.suite(), |
239 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
240 |
> |
ThreadTest.suite(), |
241 |
> |
TimeUnitTest.suite(), |
242 |
> |
TreeMapTest.suite(), |
243 |
> |
TreeSetTest.suite(), |
244 |
> |
TreeSubMapTest.suite(), |
245 |
> |
TreeSubSetTest.suite()); |
246 |
|
} |
247 |
|
|
248 |
|
|
260 |
|
return 50; |
261 |
|
} |
262 |
|
|
208 |
– |
|
263 |
|
/** |
264 |
|
* Sets delays as multiples of SHORT_DELAY. |
265 |
|
*/ |
267 |
|
SHORT_DELAY_MS = getShortDelay(); |
268 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
269 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
270 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
270 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
271 |
> |
} |
272 |
> |
|
273 |
> |
/** |
274 |
> |
* Returns a timeout in milliseconds to be used in tests that |
275 |
> |
* verify that operations block or time out. |
276 |
> |
*/ |
277 |
> |
long timeoutMillis() { |
278 |
> |
return SHORT_DELAY_MS / 4; |
279 |
> |
} |
280 |
> |
|
281 |
> |
/** |
282 |
> |
* Returns a new Date instance representing a time delayMillis |
283 |
> |
* milliseconds in the future. |
284 |
> |
*/ |
285 |
> |
Date delayedDate(long delayMillis) { |
286 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
+ |
* Extra checks that get done for all test cases. |
311 |
+ |
* |
312 |
|
* Triggers test case failure if any thread assertions have failed, |
313 |
|
* by rethrowing, in the test harness thread, any exception recorded |
314 |
|
* earlier by threadRecordFailure. |
315 |
+ |
* |
316 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
317 |
|
*/ |
318 |
|
public void tearDown() throws Exception { |
319 |
< |
Throwable t = threadFailure.get(); |
319 |
> |
Throwable t = threadFailure.getAndSet(null); |
320 |
|
if (t != null) { |
321 |
|
if (t instanceof Error) |
322 |
|
throw (Error) t; |
324 |
|
throw (RuntimeException) t; |
325 |
|
else if (t instanceof Exception) |
326 |
|
throw (Exception) t; |
327 |
< |
else |
328 |
< |
throw new AssertionError(t); |
327 |
> |
else { |
328 |
> |
AssertionFailedError afe = |
329 |
> |
new AssertionFailedError(t.toString()); |
330 |
> |
afe.initCause(t); |
331 |
> |
throw afe; |
332 |
> |
} |
333 |
|
} |
334 |
+ |
|
335 |
+ |
if (Thread.interrupted()) |
336 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
|
* Just like fail(reason), but additionally recording (using |
341 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
342 |
< |
* testcase will fail. |
341 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
342 |
> |
* the current testcase will fail. |
343 |
|
*/ |
344 |
|
public void threadFail(String reason) { |
345 |
|
try { |
346 |
|
fail(reason); |
347 |
< |
} catch (Throwable t) { |
347 |
> |
} catch (AssertionFailedError t) { |
348 |
|
threadRecordFailure(t); |
349 |
|
fail(reason); |
350 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Just like assertTrue(b), but additionally recording (using |
355 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
356 |
< |
* testcase will fail. |
355 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
356 |
> |
* the current testcase will fail. |
357 |
|
*/ |
358 |
|
public void threadAssertTrue(boolean b) { |
359 |
|
try { |
360 |
|
assertTrue(b); |
361 |
< |
} catch (AssertionError t) { |
361 |
> |
} catch (AssertionFailedError t) { |
362 |
|
threadRecordFailure(t); |
363 |
|
throw t; |
364 |
|
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Just like assertFalse(b), but additionally recording (using |
369 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
370 |
< |
* current testcase will fail. |
369 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
370 |
> |
* the current testcase will fail. |
371 |
|
*/ |
372 |
|
public void threadAssertFalse(boolean b) { |
373 |
|
try { |
374 |
|
assertFalse(b); |
375 |
< |
} catch (AssertionError t) { |
375 |
> |
} catch (AssertionFailedError t) { |
376 |
|
threadRecordFailure(t); |
377 |
|
throw t; |
378 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
|
* Just like assertNull(x), but additionally recording (using |
383 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
384 |
< |
* current testcase will fail. |
383 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
384 |
> |
* the current testcase will fail. |
385 |
|
*/ |
386 |
|
public void threadAssertNull(Object x) { |
387 |
|
try { |
388 |
|
assertNull(x); |
389 |
< |
} catch (AssertionError t) { |
389 |
> |
} catch (AssertionFailedError t) { |
390 |
|
threadRecordFailure(t); |
391 |
|
throw t; |
392 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Just like assertEquals(x, y), but additionally recording (using |
397 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
398 |
< |
* current testcase will fail. |
397 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
398 |
> |
* the current testcase will fail. |
399 |
|
*/ |
400 |
|
public void threadAssertEquals(long x, long y) { |
401 |
|
try { |
402 |
|
assertEquals(x, y); |
403 |
< |
} catch (AssertionError t) { |
403 |
> |
} catch (AssertionFailedError t) { |
404 |
|
threadRecordFailure(t); |
405 |
|
throw t; |
406 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* Just like assertEquals(x, y), but additionally recording (using |
411 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
412 |
< |
* current testcase will fail. |
411 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
412 |
> |
* the current testcase will fail. |
413 |
|
*/ |
414 |
|
public void threadAssertEquals(Object x, Object y) { |
415 |
|
try { |
416 |
|
assertEquals(x, y); |
417 |
< |
} catch (AssertionError t) { |
417 |
> |
} catch (AssertionFailedError t) { |
418 |
|
threadRecordFailure(t); |
419 |
|
throw t; |
420 |
+ |
} catch (Throwable t) { |
421 |
+ |
threadUnexpectedException(t); |
422 |
|
} |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* Just like assertSame(x, y), but additionally recording (using |
427 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
428 |
< |
* current testcase will fail. |
427 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
428 |
> |
* the current testcase will fail. |
429 |
|
*/ |
430 |
|
public void threadAssertSame(Object x, Object y) { |
431 |
|
try { |
432 |
|
assertSame(x, y); |
433 |
< |
} catch (AssertionError t) { |
433 |
> |
} catch (AssertionFailedError t) { |
434 |
|
threadRecordFailure(t); |
435 |
|
throw t; |
436 |
|
} |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* Calls threadFail with message "Unexpected exception" + ex. |
454 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
455 |
> |
* then rethrows the exception, wrapping it in an |
456 |
> |
* AssertionFailedError if necessary. |
457 |
|
*/ |
458 |
|
public void threadUnexpectedException(Throwable t) { |
459 |
|
threadRecordFailure(t); |
460 |
|
t.printStackTrace(); |
376 |
– |
// Rethrow, wrapping in an AssertionError if necessary |
461 |
|
if (t instanceof RuntimeException) |
462 |
|
throw (RuntimeException) t; |
463 |
|
else if (t instanceof Error) |
464 |
|
throw (Error) t; |
465 |
|
else { |
466 |
< |
AssertionError ae = new AssertionError("unexpected exception: " + t); |
467 |
< |
t.initCause(t); |
468 |
< |
throw ae; |
466 |
> |
AssertionFailedError afe = |
467 |
> |
new AssertionFailedError("unexpected exception: " + t); |
468 |
> |
afe.initCause(t); |
469 |
> |
throw afe; |
470 |
> |
} |
471 |
> |
} |
472 |
> |
|
473 |
> |
/** |
474 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
475 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
476 |
> |
* until time elapses. |
477 |
> |
*/ |
478 |
> |
static void delay(long millis) throws InterruptedException { |
479 |
> |
long startTime = System.nanoTime(); |
480 |
> |
long ns = millis * 1000 * 1000; |
481 |
> |
for (;;) { |
482 |
> |
if (millis > 0L) |
483 |
> |
Thread.sleep(millis); |
484 |
> |
else // too short to sleep |
485 |
> |
Thread.yield(); |
486 |
> |
long d = ns - (System.nanoTime() - startTime); |
487 |
> |
if (d > 0L) |
488 |
> |
millis = d / (1000 * 1000); |
489 |
> |
else |
490 |
> |
break; |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
|
* Waits out termination of a thread pool or fails doing so. |
496 |
|
*/ |
497 |
< |
public void joinPool(ExecutorService exec) { |
497 |
> |
void joinPool(ExecutorService exec) { |
498 |
|
try { |
499 |
|
exec.shutdown(); |
500 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
501 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
501 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
502 |
|
} catch (SecurityException ok) { |
503 |
|
// Allowed in case test doesn't have privs |
504 |
|
} catch (InterruptedException ie) { |
506 |
|
} |
507 |
|
} |
508 |
|
|
509 |
+ |
/** |
510 |
+ |
* Checks that thread does not terminate within the default |
511 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
512 |
+ |
*/ |
513 |
+ |
void assertThreadStaysAlive(Thread thread) { |
514 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
515 |
+ |
} |
516 |
+ |
|
517 |
+ |
/** |
518 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
519 |
+ |
*/ |
520 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
521 |
+ |
try { |
522 |
+ |
// No need to optimize the failing case via Thread.join. |
523 |
+ |
delay(millis); |
524 |
+ |
assertTrue(thread.isAlive()); |
525 |
+ |
} catch (InterruptedException ie) { |
526 |
+ |
fail("Unexpected InterruptedException"); |
527 |
+ |
} |
528 |
+ |
} |
529 |
+ |
|
530 |
+ |
/** |
531 |
+ |
* Checks that the threads do not terminate within the default |
532 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
533 |
+ |
*/ |
534 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
535 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
536 |
+ |
} |
537 |
+ |
|
538 |
+ |
/** |
539 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
540 |
+ |
*/ |
541 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
542 |
+ |
try { |
543 |
+ |
// No need to optimize the failing case via Thread.join. |
544 |
+ |
delay(millis); |
545 |
+ |
for (Thread thread : threads) |
546 |
+ |
assertTrue(thread.isAlive()); |
547 |
+ |
} catch (InterruptedException ie) { |
548 |
+ |
fail("Unexpected InterruptedException"); |
549 |
+ |
} |
550 |
+ |
} |
551 |
+ |
|
552 |
+ |
/** |
553 |
+ |
* Checks that future.get times out, with the default timeout of |
554 |
+ |
* {@code timeoutMillis()}. |
555 |
+ |
*/ |
556 |
+ |
void assertFutureTimesOut(Future future) { |
557 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
558 |
+ |
} |
559 |
+ |
|
560 |
+ |
/** |
561 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
562 |
+ |
*/ |
563 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
564 |
+ |
long startTime = System.nanoTime(); |
565 |
+ |
try { |
566 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
567 |
+ |
shouldThrow(); |
568 |
+ |
} catch (TimeoutException success) { |
569 |
+ |
} catch (Exception e) { |
570 |
+ |
threadUnexpectedException(e); |
571 |
+ |
} finally { future.cancel(true); } |
572 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
573 |
+ |
} |
574 |
|
|
575 |
|
/** |
576 |
|
* Fails with message "should throw exception". |
587 |
|
} |
588 |
|
|
589 |
|
/** |
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 |
– |
/** |
590 |
|
* The number of elements to place in collections, arrays, etc. |
591 |
|
*/ |
592 |
|
public static final int SIZE = 20; |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
701 |
< |
* Does <em>NOT</em> throw InterruptedException. |
700 |
> |
* Sleeps until the given time has elapsed. |
701 |
> |
* Throws AssertionFailedError if interrupted. |
702 |
|
*/ |
703 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
703 |
> |
void sleep(long millis) { |
704 |
|
try { |
705 |
< |
Thread.sleep(timeoutMillis); |
706 |
< |
} catch (InterruptedException wakeup) {} |
705 |
> |
delay(millis); |
706 |
> |
} catch (InterruptedException ie) { |
707 |
> |
AssertionFailedError afe = |
708 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
709 |
> |
afe.initCause(ie); |
710 |
> |
throw afe; |
711 |
> |
} |
712 |
|
} |
713 |
|
|
714 |
|
/** |
715 |
< |
* Returns a new started Thread running the given runnable. |
715 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
716 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
717 |
> |
*/ |
718 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
719 |
> |
long startTime = System.nanoTime(); |
720 |
> |
for (;;) { |
721 |
> |
Thread.State s = thread.getState(); |
722 |
> |
if (s == Thread.State.BLOCKED || |
723 |
> |
s == Thread.State.WAITING || |
724 |
> |
s == Thread.State.TIMED_WAITING) |
725 |
> |
return; |
726 |
> |
else if (s == Thread.State.TERMINATED) |
727 |
> |
fail("Unexpected thread termination"); |
728 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
729 |
> |
threadAssertTrue(thread.isAlive()); |
730 |
> |
return; |
731 |
> |
} |
732 |
> |
Thread.yield(); |
733 |
> |
} |
734 |
> |
} |
735 |
> |
|
736 |
> |
/** |
737 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
738 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
739 |
> |
*/ |
740 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
741 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
742 |
> |
} |
743 |
> |
|
744 |
> |
/** |
745 |
> |
* Returns the number of milliseconds since time given by |
746 |
> |
* startNanoTime, which must have been previously returned from a |
747 |
> |
* call to {@link System.nanoTime()}. |
748 |
> |
*/ |
749 |
> |
long millisElapsedSince(long startNanoTime) { |
750 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
751 |
> |
} |
752 |
> |
|
753 |
> |
/** |
754 |
> |
* Returns a new started daemon Thread running the given runnable. |
755 |
|
*/ |
756 |
|
Thread newStartedThread(Runnable runnable) { |
757 |
|
Thread t = new Thread(runnable); |
758 |
+ |
t.setDaemon(true); |
759 |
|
t.start(); |
760 |
|
return t; |
761 |
|
} |
762 |
|
|
763 |
+ |
/** |
764 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
765 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
766 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
767 |
+ |
*/ |
768 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
769 |
+ |
try { |
770 |
+ |
t.join(timeoutMillis); |
771 |
+ |
} catch (InterruptedException ie) { |
772 |
+ |
threadUnexpectedException(ie); |
773 |
+ |
} finally { |
774 |
+ |
if (t.getState() != Thread.State.TERMINATED) { |
775 |
+ |
t.interrupt(); |
776 |
+ |
fail("Test timed out"); |
777 |
+ |
} |
778 |
+ |
} |
779 |
+ |
} |
780 |
+ |
|
781 |
+ |
/** |
782 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
783 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
784 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
785 |
+ |
*/ |
786 |
+ |
void awaitTermination(Thread t) { |
787 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
788 |
+ |
} |
789 |
+ |
|
790 |
|
// Some convenient Runnable classes |
791 |
|
|
792 |
|
public abstract class CheckedRunnable implements Runnable { |
849 |
|
realRun(); |
850 |
|
threadShouldThrow("InterruptedException"); |
851 |
|
} catch (InterruptedException success) { |
852 |
+ |
threadAssertFalse(Thread.interrupted()); |
853 |
|
} catch (Throwable t) { |
854 |
|
threadUnexpectedException(t); |
855 |
|
} |
879 |
|
threadShouldThrow("InterruptedException"); |
880 |
|
return result; |
881 |
|
} catch (InterruptedException success) { |
882 |
+ |
threadAssertFalse(Thread.interrupted()); |
883 |
|
} catch (Throwable t) { |
884 |
|
threadUnexpectedException(t); |
885 |
|
} |
903 |
|
|
904 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
905 |
|
return new CheckedCallable<String>() { |
906 |
< |
public String realCall() { |
906 |
> |
protected String realCall() { |
907 |
|
try { |
908 |
|
latch.await(); |
909 |
|
} catch (InterruptedException quittingTime) {} |
911 |
|
}}; |
912 |
|
} |
913 |
|
|
914 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
915 |
+ |
return new CheckedRunnable() { |
916 |
+ |
public void realRun() throws InterruptedException { |
917 |
+ |
await(latch); |
918 |
+ |
}}; |
919 |
+ |
} |
920 |
+ |
|
921 |
+ |
public void await(CountDownLatch latch) { |
922 |
+ |
try { |
923 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
924 |
+ |
} catch (Throwable t) { |
925 |
+ |
threadUnexpectedException(t); |
926 |
+ |
} |
927 |
+ |
} |
928 |
+ |
|
929 |
+ |
public void await(Semaphore semaphore) { |
930 |
+ |
try { |
931 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
932 |
+ |
} catch (Throwable t) { |
933 |
+ |
threadUnexpectedException(t); |
934 |
+ |
} |
935 |
+ |
} |
936 |
+ |
|
937 |
+ |
// /** |
938 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
939 |
+ |
// */ |
940 |
+ |
// public void await(AtomicBoolean flag) { |
941 |
+ |
// await(flag, LONG_DELAY_MS); |
942 |
+ |
// } |
943 |
+ |
|
944 |
+ |
// /** |
945 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
946 |
+ |
// */ |
947 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
948 |
+ |
// long startTime = System.nanoTime(); |
949 |
+ |
// while (!flag.get()) { |
950 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
951 |
+ |
// throw new AssertionFailedError("timed out"); |
952 |
+ |
// Thread.yield(); |
953 |
+ |
// } |
954 |
+ |
// } |
955 |
+ |
|
956 |
|
public static class NPETask implements Callable<String> { |
957 |
|
public String call() { throw new NullPointerException(); } |
958 |
|
} |
963 |
|
|
964 |
|
public class ShortRunnable extends CheckedRunnable { |
965 |
|
protected void realRun() throws Throwable { |
966 |
< |
Thread.sleep(SHORT_DELAY_MS); |
966 |
> |
delay(SHORT_DELAY_MS); |
967 |
|
} |
968 |
|
} |
969 |
|
|
970 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
971 |
|
protected void realRun() throws InterruptedException { |
972 |
< |
Thread.sleep(SHORT_DELAY_MS); |
972 |
> |
delay(SHORT_DELAY_MS); |
973 |
|
} |
974 |
|
} |
975 |
|
|
976 |
|
public class SmallRunnable extends CheckedRunnable { |
977 |
|
protected void realRun() throws Throwable { |
978 |
< |
Thread.sleep(SMALL_DELAY_MS); |
978 |
> |
delay(SMALL_DELAY_MS); |
979 |
|
} |
980 |
|
} |
981 |
|
|
982 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
983 |
|
protected void realRun() { |
984 |
|
try { |
985 |
< |
Thread.sleep(SMALL_DELAY_MS); |
985 |
> |
delay(SMALL_DELAY_MS); |
986 |
|
} catch (InterruptedException ok) {} |
987 |
|
} |
988 |
|
} |
989 |
|
|
990 |
|
public class SmallCallable extends CheckedCallable { |
991 |
|
protected Object realCall() throws InterruptedException { |
992 |
< |
Thread.sleep(SMALL_DELAY_MS); |
992 |
> |
delay(SMALL_DELAY_MS); |
993 |
|
return Boolean.TRUE; |
994 |
|
} |
995 |
|
} |
996 |
|
|
719 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
720 |
– |
protected void realRun() throws InterruptedException { |
721 |
– |
Thread.sleep(SMALL_DELAY_MS); |
722 |
– |
} |
723 |
– |
} |
724 |
– |
|
997 |
|
public class MediumRunnable extends CheckedRunnable { |
998 |
|
protected void realRun() throws Throwable { |
999 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
999 |
> |
delay(MEDIUM_DELAY_MS); |
1000 |
|
} |
1001 |
|
} |
1002 |
|
|
1003 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1004 |
|
protected void realRun() throws InterruptedException { |
1005 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1005 |
> |
delay(MEDIUM_DELAY_MS); |
1006 |
|
} |
1007 |
|
} |
1008 |
|
|
1009 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1010 |
+ |
return new CheckedRunnable() { |
1011 |
+ |
protected void realRun() { |
1012 |
+ |
try { |
1013 |
+ |
delay(timeoutMillis); |
1014 |
+ |
} catch (InterruptedException ok) {} |
1015 |
+ |
}}; |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1019 |
|
protected void realRun() { |
1020 |
|
try { |
1021 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1021 |
> |
delay(MEDIUM_DELAY_MS); |
1022 |
|
} catch (InterruptedException ok) {} |
1023 |
|
} |
1024 |
|
} |
1026 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1027 |
|
protected void realRun() { |
1028 |
|
try { |
1029 |
< |
Thread.sleep(LONG_DELAY_MS); |
1029 |
> |
delay(LONG_DELAY_MS); |
1030 |
|
} catch (InterruptedException ok) {} |
1031 |
|
} |
1032 |
|
} |
1040 |
|
} |
1041 |
|
} |
1042 |
|
|
1043 |
+ |
public interface TrackedRunnable extends Runnable { |
1044 |
+ |
boolean isDone(); |
1045 |
+ |
} |
1046 |
+ |
|
1047 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1048 |
+ |
return new TrackedRunnable() { |
1049 |
+ |
private volatile boolean done = false; |
1050 |
+ |
public boolean isDone() { return done; } |
1051 |
+ |
public void run() { |
1052 |
+ |
try { |
1053 |
+ |
delay(timeoutMillis); |
1054 |
+ |
done = true; |
1055 |
+ |
} catch (InterruptedException ok) {} |
1056 |
+ |
} |
1057 |
+ |
}; |
1058 |
+ |
} |
1059 |
+ |
|
1060 |
|
public static class TrackedShortRunnable implements Runnable { |
1061 |
|
public volatile boolean done = false; |
1062 |
|
public void run() { |
1063 |
|
try { |
1064 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1064 |
> |
delay(SHORT_DELAY_MS); |
1065 |
> |
done = true; |
1066 |
> |
} catch (InterruptedException ok) {} |
1067 |
> |
} |
1068 |
> |
} |
1069 |
> |
|
1070 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1071 |
> |
public volatile boolean done = false; |
1072 |
> |
public void run() { |
1073 |
> |
try { |
1074 |
> |
delay(SMALL_DELAY_MS); |
1075 |
|
done = true; |
1076 |
|
} catch (InterruptedException ok) {} |
1077 |
|
} |
1081 |
|
public volatile boolean done = false; |
1082 |
|
public void run() { |
1083 |
|
try { |
1084 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1084 |
> |
delay(MEDIUM_DELAY_MS); |
1085 |
|
done = true; |
1086 |
|
} catch (InterruptedException ok) {} |
1087 |
|
} |
1091 |
|
public volatile boolean done = false; |
1092 |
|
public void run() { |
1093 |
|
try { |
1094 |
< |
Thread.sleep(LONG_DELAY_MS); |
1094 |
> |
delay(LONG_DELAY_MS); |
1095 |
|
done = true; |
1096 |
|
} catch (InterruptedException ok) {} |
1097 |
|
} |
1108 |
|
public volatile boolean done = false; |
1109 |
|
public Object call() { |
1110 |
|
try { |
1111 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1111 |
> |
delay(SMALL_DELAY_MS); |
1112 |
|
done = true; |
1113 |
|
} catch (InterruptedException ok) {} |
1114 |
|
return Boolean.TRUE; |
1154 |
|
ThreadPoolExecutor executor) {} |
1155 |
|
} |
1156 |
|
|
1157 |
+ |
/** |
1158 |
+ |
* A CyclicBarrier that uses timed await and fails with |
1159 |
+ |
* AssertionFailedErrors instead of throwing checked exceptions. |
1160 |
+ |
*/ |
1161 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1162 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1163 |
+ |
|
1164 |
+ |
public int await() { |
1165 |
+ |
try { |
1166 |
+ |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1167 |
+ |
} catch (TimeoutException e) { |
1168 |
+ |
throw new AssertionFailedError("timed out"); |
1169 |
+ |
} catch (Exception e) { |
1170 |
+ |
AssertionFailedError afe = |
1171 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1172 |
+ |
afe.initCause(e); |
1173 |
+ |
throw afe; |
1174 |
+ |
} |
1175 |
+ |
} |
1176 |
+ |
} |
1177 |
+ |
|
1178 |
+ |
void checkEmpty(BlockingQueue q) { |
1179 |
+ |
try { |
1180 |
+ |
assertTrue(q.isEmpty()); |
1181 |
+ |
assertEquals(0, q.size()); |
1182 |
+ |
assertNull(q.peek()); |
1183 |
+ |
assertNull(q.poll()); |
1184 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1185 |
+ |
assertEquals(q.toString(), "[]"); |
1186 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1187 |
+ |
assertFalse(q.iterator().hasNext()); |
1188 |
+ |
try { |
1189 |
+ |
q.element(); |
1190 |
+ |
shouldThrow(); |
1191 |
+ |
} catch (NoSuchElementException success) {} |
1192 |
+ |
try { |
1193 |
+ |
q.iterator().next(); |
1194 |
+ |
shouldThrow(); |
1195 |
+ |
} catch (NoSuchElementException success) {} |
1196 |
+ |
try { |
1197 |
+ |
q.remove(); |
1198 |
+ |
shouldThrow(); |
1199 |
+ |
} catch (NoSuchElementException success) {} |
1200 |
+ |
} catch (InterruptedException ie) { |
1201 |
+ |
threadUnexpectedException(ie); |
1202 |
+ |
} |
1203 |
+ |
} |
1204 |
+ |
|
1205 |
+ |
@SuppressWarnings("unchecked") |
1206 |
+ |
<T> T serialClone(T o) { |
1207 |
+ |
try { |
1208 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1209 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1210 |
+ |
oos.writeObject(o); |
1211 |
+ |
oos.flush(); |
1212 |
+ |
oos.close(); |
1213 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1214 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1215 |
+ |
T clone = (T) ois.readObject(); |
1216 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1217 |
+ |
return clone; |
1218 |
+ |
} catch (Throwable t) { |
1219 |
+ |
threadUnexpectedException(t); |
1220 |
+ |
return null; |
1221 |
+ |
} |
1222 |
+ |
} |
1223 |
|
} |