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