7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
+ |
import java.util.Arrays; |
11 |
+ |
import java.util.NoSuchElementException; |
12 |
|
import java.util.PropertyPermission; |
13 |
|
import java.util.concurrent.*; |
14 |
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
17 |
|
import java.security.CodeSource; |
18 |
|
import java.security.Permission; |
19 |
|
import java.security.PermissionCollection; |
99 |
|
private static final boolean useSecurityManager = |
100 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
101 |
|
|
102 |
+ |
protected static final boolean expensiveTests = |
103 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
104 |
+ |
|
105 |
+ |
/** |
106 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
107 |
+ |
* take more than profileThreshold milliseconds to execute. |
108 |
+ |
*/ |
109 |
+ |
private static final boolean profileTests = |
110 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
111 |
+ |
|
112 |
+ |
/** |
113 |
+ |
* The number of milliseconds that tests are permitted for |
114 |
+ |
* execution without being reported, when profileTests is set. |
115 |
+ |
*/ |
116 |
+ |
private static final long profileThreshold = |
117 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
118 |
+ |
|
119 |
+ |
protected void runTest() throws Throwable { |
120 |
+ |
if (profileTests) |
121 |
+ |
runTestProfiled(); |
122 |
+ |
else |
123 |
+ |
super.runTest(); |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
protected void runTestProfiled() throws Throwable { |
127 |
+ |
long t0 = System.nanoTime(); |
128 |
+ |
try { |
129 |
+ |
super.runTest(); |
130 |
+ |
} finally { |
131 |
+ |
long elapsedMillis = |
132 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
133 |
+ |
if (elapsedMillis >= profileThreshold) |
134 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
135 |
+ |
} |
136 |
+ |
} |
137 |
+ |
|
138 |
|
/** |
139 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
140 |
|
*/ |
144 |
|
Policy.setPolicy(permissivePolicy()); |
145 |
|
System.setSecurityManager(new SecurityManager()); |
146 |
|
} |
147 |
< |
int iters = 1; |
148 |
< |
if (args.length > 0) |
110 |
< |
iters = Integer.parseInt(args[0]); |
147 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
148 |
> |
|
149 |
|
Test s = suite(); |
150 |
|
for (int i = 0; i < iters; ++i) { |
151 |
|
junit.textui.TestRunner.run(s); |
155 |
|
System.exit(0); |
156 |
|
} |
157 |
|
|
158 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
159 |
+ |
TestSuite suite = new TestSuite(); |
160 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
161 |
+ |
if (suiteOrClass instanceof TestSuite) |
162 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
163 |
+ |
else if (suiteOrClass instanceof Class) |
164 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
165 |
+ |
else |
166 |
+ |
throw new ClassCastException("not a test suite or class"); |
167 |
+ |
} |
168 |
+ |
return suite; |
169 |
+ |
} |
170 |
+ |
|
171 |
|
/** |
172 |
< |
* Collects all JSR166 unit tests as one suite |
172 |
> |
* Collects all JSR166 unit tests as one suite. |
173 |
|
*/ |
174 |
|
public static Test suite() { |
175 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
176 |
< |
|
177 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
178 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
179 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
180 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
181 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
182 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
183 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
184 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
185 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
186 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
187 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
188 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
189 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
190 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
191 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
192 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
193 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
194 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
195 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
196 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
197 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
198 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
199 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
200 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
201 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
202 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
203 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
204 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
205 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
206 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
207 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
208 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
209 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
210 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
211 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
212 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
213 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
214 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
215 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
216 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
217 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
218 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
219 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
220 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
221 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
222 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
223 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
224 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
225 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
226 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
227 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
228 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
229 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
230 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
231 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
232 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
233 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
234 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
235 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
236 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
237 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
238 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
239 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
189 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
190 |
< |
|
191 |
< |
return suite; |
175 |
> |
return newTestSuite( |
176 |
> |
ForkJoinPoolTest.suite(), |
177 |
> |
ForkJoinTaskTest.suite(), |
178 |
> |
RecursiveActionTest.suite(), |
179 |
> |
RecursiveTaskTest.suite(), |
180 |
> |
LinkedTransferQueueTest.suite(), |
181 |
> |
PhaserTest.suite(), |
182 |
> |
ThreadLocalRandomTest.suite(), |
183 |
> |
AbstractExecutorServiceTest.suite(), |
184 |
> |
AbstractQueueTest.suite(), |
185 |
> |
AbstractQueuedSynchronizerTest.suite(), |
186 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
187 |
> |
ArrayBlockingQueueTest.suite(), |
188 |
> |
ArrayDequeTest.suite(), |
189 |
> |
AtomicBooleanTest.suite(), |
190 |
> |
AtomicIntegerArrayTest.suite(), |
191 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
192 |
> |
AtomicIntegerTest.suite(), |
193 |
> |
AtomicLongArrayTest.suite(), |
194 |
> |
AtomicLongFieldUpdaterTest.suite(), |
195 |
> |
AtomicLongTest.suite(), |
196 |
> |
AtomicMarkableReferenceTest.suite(), |
197 |
> |
AtomicReferenceArrayTest.suite(), |
198 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
199 |
> |
AtomicReferenceTest.suite(), |
200 |
> |
AtomicStampedReferenceTest.suite(), |
201 |
> |
ConcurrentHashMapTest.suite(), |
202 |
> |
ConcurrentLinkedDequeTest.suite(), |
203 |
> |
ConcurrentLinkedQueueTest.suite(), |
204 |
> |
ConcurrentSkipListMapTest.suite(), |
205 |
> |
ConcurrentSkipListSubMapTest.suite(), |
206 |
> |
ConcurrentSkipListSetTest.suite(), |
207 |
> |
ConcurrentSkipListSubSetTest.suite(), |
208 |
> |
CopyOnWriteArrayListTest.suite(), |
209 |
> |
CopyOnWriteArraySetTest.suite(), |
210 |
> |
CountDownLatchTest.suite(), |
211 |
> |
CyclicBarrierTest.suite(), |
212 |
> |
DelayQueueTest.suite(), |
213 |
> |
EntryTest.suite(), |
214 |
> |
ExchangerTest.suite(), |
215 |
> |
ExecutorsTest.suite(), |
216 |
> |
ExecutorCompletionServiceTest.suite(), |
217 |
> |
FutureTaskTest.suite(), |
218 |
> |
LinkedBlockingDequeTest.suite(), |
219 |
> |
LinkedBlockingQueueTest.suite(), |
220 |
> |
LinkedListTest.suite(), |
221 |
> |
LockSupportTest.suite(), |
222 |
> |
PriorityBlockingQueueTest.suite(), |
223 |
> |
PriorityQueueTest.suite(), |
224 |
> |
ReentrantLockTest.suite(), |
225 |
> |
ReentrantReadWriteLockTest.suite(), |
226 |
> |
ScheduledExecutorTest.suite(), |
227 |
> |
ScheduledExecutorSubclassTest.suite(), |
228 |
> |
SemaphoreTest.suite(), |
229 |
> |
SynchronousQueueTest.suite(), |
230 |
> |
SystemTest.suite(), |
231 |
> |
ThreadLocalTest.suite(), |
232 |
> |
ThreadPoolExecutorTest.suite(), |
233 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
234 |
> |
ThreadTest.suite(), |
235 |
> |
TimeUnitTest.suite(), |
236 |
> |
TreeMapTest.suite(), |
237 |
> |
TreeSetTest.suite(), |
238 |
> |
TreeSubMapTest.suite(), |
239 |
> |
TreeSubSetTest.suite()); |
240 |
|
} |
241 |
|
|
242 |
|
|
262 |
|
SHORT_DELAY_MS = getShortDelay(); |
263 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
264 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
265 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
265 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
266 |
|
} |
267 |
|
|
268 |
|
/** |
291 |
|
* earlier by threadRecordFailure. |
292 |
|
*/ |
293 |
|
public void tearDown() throws Exception { |
294 |
< |
Throwable t = threadFailure.get(); |
294 |
> |
Throwable t = threadFailure.getAndSet(null); |
295 |
|
if (t != null) { |
296 |
|
if (t instanceof Error) |
297 |
|
throw (Error) t; |
299 |
|
throw (RuntimeException) t; |
300 |
|
else if (t instanceof Exception) |
301 |
|
throw (Exception) t; |
302 |
< |
else |
303 |
< |
throw new AssertionError(t); |
302 |
> |
else { |
303 |
> |
AssertionFailedError afe = |
304 |
> |
new AssertionFailedError(t.toString()); |
305 |
> |
afe.initCause(t); |
306 |
> |
throw afe; |
307 |
> |
} |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* Just like fail(reason), but additionally recording (using |
313 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
314 |
< |
* testcase will fail. |
313 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
314 |
> |
* the current testcase will fail. |
315 |
|
*/ |
316 |
|
public void threadFail(String reason) { |
317 |
|
try { |
318 |
|
fail(reason); |
319 |
< |
} catch (Throwable t) { |
319 |
> |
} catch (AssertionFailedError t) { |
320 |
|
threadRecordFailure(t); |
321 |
|
fail(reason); |
322 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* Just like assertTrue(b), but additionally recording (using |
327 |
< |
* threadRecordFailure) any AssertionError thrown, so that the current |
328 |
< |
* testcase will fail. |
327 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
328 |
> |
* the current testcase will fail. |
329 |
|
*/ |
330 |
|
public void threadAssertTrue(boolean b) { |
331 |
|
try { |
332 |
|
assertTrue(b); |
333 |
< |
} catch (AssertionError t) { |
333 |
> |
} catch (AssertionFailedError t) { |
334 |
|
threadRecordFailure(t); |
335 |
|
throw t; |
336 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
|
* Just like assertFalse(b), but additionally recording (using |
341 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
342 |
< |
* current testcase will fail. |
341 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
342 |
> |
* the current testcase will fail. |
343 |
|
*/ |
344 |
|
public void threadAssertFalse(boolean b) { |
345 |
|
try { |
346 |
|
assertFalse(b); |
347 |
< |
} catch (AssertionError t) { |
347 |
> |
} catch (AssertionFailedError t) { |
348 |
|
threadRecordFailure(t); |
349 |
|
throw t; |
350 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Just like assertNull(x), but additionally recording (using |
355 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
356 |
< |
* current testcase will fail. |
355 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
356 |
> |
* the current testcase will fail. |
357 |
|
*/ |
358 |
|
public void threadAssertNull(Object x) { |
359 |
|
try { |
360 |
|
assertNull(x); |
361 |
< |
} catch (AssertionError t) { |
361 |
> |
} catch (AssertionFailedError t) { |
362 |
|
threadRecordFailure(t); |
363 |
|
throw t; |
364 |
|
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Just like assertEquals(x, y), 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 threadAssertEquals(long x, long y) { |
373 |
|
try { |
374 |
|
assertEquals(x, y); |
375 |
< |
} catch (AssertionError t) { |
375 |
> |
} catch (AssertionFailedError t) { |
376 |
|
threadRecordFailure(t); |
377 |
|
throw t; |
378 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
|
* Just like assertEquals(x, y), 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 threadAssertEquals(Object x, Object y) { |
387 |
|
try { |
388 |
|
assertEquals(x, y); |
389 |
< |
} catch (AssertionError t) { |
389 |
> |
} catch (AssertionFailedError t) { |
390 |
|
threadRecordFailure(t); |
391 |
|
throw t; |
392 |
+ |
} catch (Throwable t) { |
393 |
+ |
threadUnexpectedException(t); |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
|
* Just like assertSame(x, y), but additionally recording (using |
399 |
< |
* threadRecordFailure) any AssertionError thrown, so that the |
400 |
< |
* current testcase will fail. |
399 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
400 |
> |
* the current testcase will fail. |
401 |
|
*/ |
402 |
|
public void threadAssertSame(Object x, Object y) { |
403 |
|
try { |
404 |
|
assertSame(x, y); |
405 |
< |
} catch (AssertionError t) { |
405 |
> |
} catch (AssertionFailedError t) { |
406 |
|
threadRecordFailure(t); |
407 |
|
throw t; |
408 |
|
} |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* Calls threadFail with message "Unexpected exception" + ex. |
426 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
427 |
> |
* then rethrows the exception, wrapping it in an |
428 |
> |
* AssertionFailedError if necessary. |
429 |
|
*/ |
430 |
|
public void threadUnexpectedException(Throwable t) { |
431 |
|
threadRecordFailure(t); |
432 |
|
t.printStackTrace(); |
377 |
– |
// Rethrow, wrapping in an AssertionError if necessary |
433 |
|
if (t instanceof RuntimeException) |
434 |
|
throw (RuntimeException) t; |
435 |
|
else if (t instanceof Error) |
436 |
|
throw (Error) t; |
437 |
|
else { |
438 |
< |
AssertionError ae = new AssertionError("unexpected exception: " + t); |
438 |
> |
AssertionFailedError afe = |
439 |
> |
new AssertionFailedError("unexpected exception: " + t); |
440 |
|
t.initCause(t); |
441 |
< |
throw ae; |
442 |
< |
} |
441 |
> |
throw afe; |
442 |
> |
} |
443 |
|
} |
444 |
|
|
445 |
|
/** |
449 |
|
try { |
450 |
|
exec.shutdown(); |
451 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
452 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
452 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
453 |
|
} catch (SecurityException ok) { |
454 |
|
// Allowed in case test doesn't have privs |
455 |
|
} catch (InterruptedException ie) { |
473 |
|
} |
474 |
|
|
475 |
|
/** |
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 |
– |
/** |
476 |
|
* The number of elements to place in collections, arrays, etc. |
477 |
|
*/ |
478 |
|
public static final int SIZE = 20; |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
586 |
> |
* Sleeps until the given time has elapsed. |
587 |
> |
* Throws AssertionFailedError if interrupted. |
588 |
> |
*/ |
589 |
> |
void sleep(long millis) { |
590 |
> |
try { |
591 |
> |
Thread.sleep(millis); |
592 |
> |
} catch (InterruptedException ie) { |
593 |
> |
AssertionFailedError afe = |
594 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
595 |
> |
afe.initCause(ie); |
596 |
> |
throw afe; |
597 |
> |
} |
598 |
> |
} |
599 |
> |
|
600 |
> |
/** |
601 |
> |
* Sleeps until the timeout has elapsed, or interrupted. |
602 |
|
* Does <em>NOT</em> throw InterruptedException. |
603 |
|
*/ |
604 |
|
void sleepTillInterrupted(long timeoutMillis) { |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
< |
* Returns a new started Thread running the given runnable. |
611 |
> |
* Waits up to the specified number of milliseconds for the given |
612 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
613 |
> |
*/ |
614 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
615 |
> |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
616 |
> |
long t0 = System.nanoTime(); |
617 |
> |
for (;;) { |
618 |
> |
Thread.State s = thread.getState(); |
619 |
> |
if (s == Thread.State.BLOCKED || |
620 |
> |
s == Thread.State.WAITING || |
621 |
> |
s == Thread.State.TIMED_WAITING) |
622 |
> |
return; |
623 |
> |
else if (s == Thread.State.TERMINATED) |
624 |
> |
fail("Unexpected thread termination"); |
625 |
> |
else if (System.nanoTime() - t0 > timeoutNanos) { |
626 |
> |
threadAssertTrue(thread.isAlive()); |
627 |
> |
return; |
628 |
> |
} |
629 |
> |
Thread.yield(); |
630 |
> |
} |
631 |
> |
} |
632 |
> |
|
633 |
> |
/** |
634 |
> |
* Returns the number of milliseconds since time given by |
635 |
> |
* startNanoTime, which must have been previously returned from a |
636 |
> |
* call to {@link System.nanoTime()}. |
637 |
> |
*/ |
638 |
> |
long millisElapsedSince(long startNanoTime) { |
639 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
640 |
> |
} |
641 |
> |
|
642 |
> |
/** |
643 |
> |
* Returns a new started daemon Thread running the given runnable. |
644 |
|
*/ |
645 |
|
Thread newStartedThread(Runnable runnable) { |
646 |
|
Thread t = new Thread(runnable); |
647 |
+ |
t.setDaemon(true); |
648 |
|
t.start(); |
649 |
|
return t; |
650 |
|
} |
651 |
|
|
652 |
+ |
/** |
653 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
654 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
655 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
656 |
+ |
*/ |
657 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
658 |
+ |
try { |
659 |
+ |
t.join(timeoutMillis); |
660 |
+ |
} catch (InterruptedException ie) { |
661 |
+ |
threadUnexpectedException(ie); |
662 |
+ |
} finally { |
663 |
+ |
if (t.isAlive()) { |
664 |
+ |
t.interrupt(); |
665 |
+ |
fail("Test timed out"); |
666 |
+ |
} |
667 |
+ |
} |
668 |
+ |
} |
669 |
+ |
|
670 |
|
// Some convenient Runnable classes |
671 |
|
|
672 |
|
public abstract class CheckedRunnable implements Runnable { |
781 |
|
|
782 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
783 |
|
return new CheckedCallable<String>() { |
784 |
< |
public String realCall() { |
784 |
> |
protected String realCall() { |
785 |
|
try { |
786 |
|
latch.await(); |
787 |
|
} catch (InterruptedException quittingTime) {} |
830 |
|
} |
831 |
|
} |
832 |
|
|
720 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
721 |
– |
protected void realRun() throws InterruptedException { |
722 |
– |
Thread.sleep(SMALL_DELAY_MS); |
723 |
– |
} |
724 |
– |
} |
725 |
– |
|
833 |
|
public class MediumRunnable extends CheckedRunnable { |
834 |
|
protected void realRun() throws Throwable { |
835 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
842 |
|
} |
843 |
|
} |
844 |
|
|
845 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
846 |
+ |
return new CheckedRunnable() { |
847 |
+ |
protected void realRun() { |
848 |
+ |
try { |
849 |
+ |
Thread.sleep(timeoutMillis); |
850 |
+ |
} catch (InterruptedException ok) {} |
851 |
+ |
}}; |
852 |
+ |
} |
853 |
+ |
|
854 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
855 |
|
protected void realRun() { |
856 |
|
try { |
876 |
|
} |
877 |
|
} |
878 |
|
|
879 |
+ |
public interface TrackedRunnable extends Runnable { |
880 |
+ |
boolean isDone(); |
881 |
+ |
} |
882 |
+ |
|
883 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
884 |
+ |
return new TrackedRunnable() { |
885 |
+ |
private volatile boolean done = false; |
886 |
+ |
public boolean isDone() { return done; } |
887 |
+ |
public void run() { |
888 |
+ |
try { |
889 |
+ |
Thread.sleep(timeoutMillis); |
890 |
+ |
done = true; |
891 |
+ |
} catch (InterruptedException ok) {} |
892 |
+ |
} |
893 |
+ |
}; |
894 |
+ |
} |
895 |
+ |
|
896 |
|
public static class TrackedShortRunnable implements Runnable { |
897 |
|
public volatile boolean done = false; |
898 |
|
public void run() { |
899 |
|
try { |
900 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
901 |
+ |
done = true; |
902 |
+ |
} catch (InterruptedException ok) {} |
903 |
+ |
} |
904 |
+ |
} |
905 |
+ |
|
906 |
+ |
public static class TrackedSmallRunnable implements Runnable { |
907 |
+ |
public volatile boolean done = false; |
908 |
+ |
public void run() { |
909 |
+ |
try { |
910 |
|
Thread.sleep(SMALL_DELAY_MS); |
911 |
|
done = true; |
912 |
|
} catch (InterruptedException ok) {} |
990 |
|
ThreadPoolExecutor executor) {} |
991 |
|
} |
992 |
|
|
993 |
+ |
/** |
994 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
995 |
+ |
* of throwing checked exceptions. |
996 |
+ |
*/ |
997 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
998 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
999 |
+ |
|
1000 |
+ |
public int await() { |
1001 |
+ |
try { |
1002 |
+ |
return super.await(); |
1003 |
+ |
} catch (Exception e) { |
1004 |
+ |
AssertionFailedError afe = |
1005 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1006 |
+ |
afe.initCause(e); |
1007 |
+ |
throw afe; |
1008 |
+ |
} |
1009 |
+ |
} |
1010 |
+ |
} |
1011 |
+ |
|
1012 |
+ |
public void checkEmpty(BlockingQueue q) { |
1013 |
+ |
try { |
1014 |
+ |
assertTrue(q.isEmpty()); |
1015 |
+ |
assertEquals(0, q.size()); |
1016 |
+ |
assertNull(q.peek()); |
1017 |
+ |
assertNull(q.poll()); |
1018 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1019 |
+ |
assertEquals(q.toString(), "[]"); |
1020 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1021 |
+ |
assertFalse(q.iterator().hasNext()); |
1022 |
+ |
try { |
1023 |
+ |
q.element(); |
1024 |
+ |
shouldThrow(); |
1025 |
+ |
} catch (NoSuchElementException success) {} |
1026 |
+ |
try { |
1027 |
+ |
q.iterator().next(); |
1028 |
+ |
shouldThrow(); |
1029 |
+ |
} catch (NoSuchElementException success) {} |
1030 |
+ |
try { |
1031 |
+ |
q.remove(); |
1032 |
+ |
shouldThrow(); |
1033 |
+ |
} catch (NoSuchElementException success) {} |
1034 |
+ |
} catch (InterruptedException ie) { |
1035 |
+ |
threadUnexpectedException(ie); |
1036 |
+ |
} |
1037 |
+ |
} |
1038 |
+ |
|
1039 |
|
} |