7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.PropertyPermission; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
< |
import java.io.*; |
15 |
< |
import java.security.*; |
14 |
> |
import java.security.CodeSource; |
15 |
> |
import java.security.Permission; |
16 |
> |
import java.security.PermissionCollection; |
17 |
> |
import java.security.Permissions; |
18 |
> |
import java.security.Policy; |
19 |
> |
import java.security.ProtectionDomain; |
20 |
> |
import java.security.SecurityPermission; |
21 |
|
|
22 |
|
/** |
23 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
96 |
|
private static final boolean useSecurityManager = |
97 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
98 |
|
|
99 |
+ |
protected static final boolean expensiveTests = |
100 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
101 |
+ |
|
102 |
+ |
/** |
103 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
104 |
+ |
* take more than profileThreshold milliseconds to execute. |
105 |
+ |
*/ |
106 |
+ |
private static final boolean profileTests = |
107 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
108 |
+ |
|
109 |
+ |
/** |
110 |
+ |
* The number of milliseconds that tests are permitted for |
111 |
+ |
* execution without being reported, when profileTests is set. |
112 |
+ |
*/ |
113 |
+ |
private static final long profileThreshold = |
114 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
115 |
+ |
|
116 |
+ |
protected void runTest() throws Throwable { |
117 |
+ |
if (profileTests) |
118 |
+ |
runTestProfiled(); |
119 |
+ |
else |
120 |
+ |
super.runTest(); |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
protected void runTestProfiled() throws Throwable { |
124 |
+ |
long t0 = System.nanoTime(); |
125 |
+ |
try { |
126 |
+ |
super.runTest(); |
127 |
+ |
} finally { |
128 |
+ |
long elapsedMillis = |
129 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
130 |
+ |
if (elapsedMillis >= profileThreshold) |
131 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
132 |
+ |
} |
133 |
+ |
} |
134 |
+ |
|
135 |
|
/** |
136 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
137 |
|
*/ |
141 |
|
Policy.setPolicy(permissivePolicy()); |
142 |
|
System.setSecurityManager(new SecurityManager()); |
143 |
|
} |
144 |
< |
int iters = 1; |
145 |
< |
if (args.length > 0) |
104 |
< |
iters = Integer.parseInt(args[0]); |
144 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
145 |
> |
|
146 |
|
Test s = suite(); |
147 |
|
for (int i = 0; i < iters; ++i) { |
148 |
|
junit.textui.TestRunner.run(s); |
152 |
|
System.exit(0); |
153 |
|
} |
154 |
|
|
155 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
156 |
+ |
TestSuite suite = new TestSuite(); |
157 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
158 |
+ |
if (suiteOrClass instanceof TestSuite) |
159 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
160 |
+ |
else if (suiteOrClass instanceof Class) |
161 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
162 |
+ |
else |
163 |
+ |
throw new ClassCastException("not a test suite or class"); |
164 |
+ |
} |
165 |
+ |
return suite; |
166 |
+ |
} |
167 |
+ |
|
168 |
|
/** |
169 |
< |
* Collects all JSR166 unit tests as one suite |
169 |
> |
* Collects all JSR166 unit tests as one suite. |
170 |
|
*/ |
171 |
|
public static Test suite() { |
172 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
173 |
< |
|
174 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
175 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
176 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
177 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
178 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
179 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
180 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
181 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
182 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
183 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
184 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
185 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
186 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
187 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
188 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
189 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
190 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
191 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
192 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
193 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
194 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
195 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
196 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
197 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
198 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
199 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
200 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
201 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
202 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
203 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
204 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
205 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
206 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
207 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
208 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
209 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
210 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
211 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
212 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
213 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
214 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
215 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
216 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
217 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
218 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
219 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
220 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
221 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
222 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
223 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
224 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
225 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
226 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
227 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
228 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
229 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
230 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
231 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
232 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
233 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
234 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
235 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
236 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
183 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
184 |
< |
|
185 |
< |
return suite; |
172 |
> |
return newTestSuite( |
173 |
> |
ForkJoinPoolTest.suite(), |
174 |
> |
ForkJoinTaskTest.suite(), |
175 |
> |
RecursiveActionTest.suite(), |
176 |
> |
RecursiveTaskTest.suite(), |
177 |
> |
LinkedTransferQueueTest.suite(), |
178 |
> |
PhaserTest.suite(), |
179 |
> |
ThreadLocalRandomTest.suite(), |
180 |
> |
AbstractExecutorServiceTest.suite(), |
181 |
> |
AbstractQueueTest.suite(), |
182 |
> |
AbstractQueuedSynchronizerTest.suite(), |
183 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
184 |
> |
ArrayBlockingQueueTest.suite(), |
185 |
> |
ArrayDequeTest.suite(), |
186 |
> |
AtomicBooleanTest.suite(), |
187 |
> |
AtomicIntegerArrayTest.suite(), |
188 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
189 |
> |
AtomicIntegerTest.suite(), |
190 |
> |
AtomicLongArrayTest.suite(), |
191 |
> |
AtomicLongFieldUpdaterTest.suite(), |
192 |
> |
AtomicLongTest.suite(), |
193 |
> |
AtomicMarkableReferenceTest.suite(), |
194 |
> |
AtomicReferenceArrayTest.suite(), |
195 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
196 |
> |
AtomicReferenceTest.suite(), |
197 |
> |
AtomicStampedReferenceTest.suite(), |
198 |
> |
ConcurrentHashMapTest.suite(), |
199 |
> |
ConcurrentLinkedDequeTest.suite(), |
200 |
> |
ConcurrentLinkedQueueTest.suite(), |
201 |
> |
ConcurrentSkipListMapTest.suite(), |
202 |
> |
ConcurrentSkipListSubMapTest.suite(), |
203 |
> |
ConcurrentSkipListSetTest.suite(), |
204 |
> |
ConcurrentSkipListSubSetTest.suite(), |
205 |
> |
CopyOnWriteArrayListTest.suite(), |
206 |
> |
CopyOnWriteArraySetTest.suite(), |
207 |
> |
CountDownLatchTest.suite(), |
208 |
> |
CyclicBarrierTest.suite(), |
209 |
> |
DelayQueueTest.suite(), |
210 |
> |
EntryTest.suite(), |
211 |
> |
ExchangerTest.suite(), |
212 |
> |
ExecutorsTest.suite(), |
213 |
> |
ExecutorCompletionServiceTest.suite(), |
214 |
> |
FutureTaskTest.suite(), |
215 |
> |
LinkedBlockingDequeTest.suite(), |
216 |
> |
LinkedBlockingQueueTest.suite(), |
217 |
> |
LinkedListTest.suite(), |
218 |
> |
LockSupportTest.suite(), |
219 |
> |
PriorityBlockingQueueTest.suite(), |
220 |
> |
PriorityQueueTest.suite(), |
221 |
> |
ReentrantLockTest.suite(), |
222 |
> |
ReentrantReadWriteLockTest.suite(), |
223 |
> |
ScheduledExecutorTest.suite(), |
224 |
> |
ScheduledExecutorSubclassTest.suite(), |
225 |
> |
SemaphoreTest.suite(), |
226 |
> |
SynchronousQueueTest.suite(), |
227 |
> |
SystemTest.suite(), |
228 |
> |
ThreadLocalTest.suite(), |
229 |
> |
ThreadPoolExecutorTest.suite(), |
230 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
231 |
> |
ThreadTest.suite(), |
232 |
> |
TimeUnitTest.suite(), |
233 |
> |
TreeMapTest.suite(), |
234 |
> |
TreeSetTest.suite(), |
235 |
> |
TreeSubMapTest.suite(), |
236 |
> |
TreeSubSetTest.suite()); |
237 |
|
} |
238 |
|
|
239 |
|
|
257 |
|
*/ |
258 |
|
protected void setDelays() { |
259 |
|
SHORT_DELAY_MS = getShortDelay(); |
260 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
260 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
261 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
262 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
262 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* Flag set true if any threadAssert methods fail |
266 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
267 |
|
*/ |
268 |
< |
volatile boolean threadFailed; |
268 |
> |
private final AtomicReference<Throwable> threadFailure |
269 |
> |
= new AtomicReference<Throwable>(null); |
270 |
|
|
271 |
|
/** |
272 |
< |
* Initializes test to indicate that no thread assertions have failed |
272 |
> |
* Records an exception so that it can be rethrown later in the test |
273 |
> |
* harness thread, triggering a test case failure. Only the first |
274 |
> |
* failure is recorded; subsequent calls to this method from within |
275 |
> |
* the same test have no effect. |
276 |
|
*/ |
277 |
+ |
public void threadRecordFailure(Throwable t) { |
278 |
+ |
threadFailure.compareAndSet(null, t); |
279 |
+ |
} |
280 |
+ |
|
281 |
|
public void setUp() { |
282 |
|
setDelays(); |
224 |
– |
threadFailed = false; |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* Triggers test case failure if any thread assertions have failed |
287 |
< |
*/ |
288 |
< |
public void tearDown() { |
289 |
< |
assertFalse(threadFailed); |
286 |
> |
* Triggers test case failure if any thread assertions have failed, |
287 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
288 |
> |
* earlier by threadRecordFailure. |
289 |
> |
*/ |
290 |
> |
public void tearDown() throws Exception { |
291 |
> |
Throwable t = threadFailure.get(); |
292 |
> |
if (t != null) { |
293 |
> |
if (t instanceof Error) |
294 |
> |
throw (Error) t; |
295 |
> |
else if (t instanceof RuntimeException) |
296 |
> |
throw (RuntimeException) t; |
297 |
> |
else if (t instanceof Exception) |
298 |
> |
throw (Exception) t; |
299 |
> |
else { |
300 |
> |
AssertionFailedError afe = |
301 |
> |
new AssertionFailedError(t.toString()); |
302 |
> |
afe.initCause(t); |
303 |
> |
throw afe; |
304 |
> |
} |
305 |
> |
} |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
< |
* Fail, also setting status to indicate current testcase should fail |
309 |
> |
* Just like fail(reason), but additionally recording (using |
310 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
311 |
> |
* the current testcase will fail. |
312 |
|
*/ |
313 |
|
public void threadFail(String reason) { |
314 |
< |
threadFailed = true; |
315 |
< |
fail(reason); |
314 |
> |
try { |
315 |
> |
fail(reason); |
316 |
> |
} catch (AssertionFailedError t) { |
317 |
> |
threadRecordFailure(t); |
318 |
> |
fail(reason); |
319 |
> |
} |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* If expression not true, set status to indicate current testcase |
324 |
< |
* should fail |
323 |
> |
* Just like assertTrue(b), but additionally recording (using |
324 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
325 |
> |
* the current testcase will fail. |
326 |
|
*/ |
327 |
|
public void threadAssertTrue(boolean b) { |
328 |
< |
if (!b) { |
248 |
< |
threadFailed = true; |
328 |
> |
try { |
329 |
|
assertTrue(b); |
330 |
+ |
} catch (AssertionFailedError t) { |
331 |
+ |
threadRecordFailure(t); |
332 |
+ |
throw t; |
333 |
|
} |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* If expression not false, set status to indicate current testcase |
338 |
< |
* should fail |
337 |
> |
* Just like assertFalse(b), but additionally recording (using |
338 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
339 |
> |
* the current testcase will fail. |
340 |
|
*/ |
341 |
|
public void threadAssertFalse(boolean b) { |
342 |
< |
if (b) { |
259 |
< |
threadFailed = true; |
342 |
> |
try { |
343 |
|
assertFalse(b); |
344 |
+ |
} catch (AssertionFailedError t) { |
345 |
+ |
threadRecordFailure(t); |
346 |
+ |
throw t; |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* If argument not null, set status to indicate current testcase |
352 |
< |
* should fail |
351 |
> |
* Just like assertNull(x), but additionally recording (using |
352 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
353 |
> |
* the current testcase will fail. |
354 |
|
*/ |
355 |
|
public void threadAssertNull(Object x) { |
356 |
< |
if (x != null) { |
270 |
< |
threadFailed = true; |
356 |
> |
try { |
357 |
|
assertNull(x); |
358 |
+ |
} catch (AssertionFailedError t) { |
359 |
+ |
threadRecordFailure(t); |
360 |
+ |
throw t; |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* If arguments not equal, set status to indicate current testcase |
366 |
< |
* should fail |
365 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
366 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
367 |
> |
* the current testcase will fail. |
368 |
|
*/ |
369 |
|
public void threadAssertEquals(long x, long y) { |
370 |
< |
if (x != y) { |
281 |
< |
threadFailed = true; |
370 |
> |
try { |
371 |
|
assertEquals(x, y); |
372 |
+ |
} catch (AssertionFailedError t) { |
373 |
+ |
threadRecordFailure(t); |
374 |
+ |
throw t; |
375 |
|
} |
376 |
|
} |
377 |
|
|
378 |
|
/** |
379 |
< |
* If arguments not equal, set status to indicate current testcase |
380 |
< |
* should fail |
379 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
380 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
381 |
> |
* the current testcase will fail. |
382 |
|
*/ |
383 |
|
public void threadAssertEquals(Object x, Object y) { |
384 |
< |
if (x != y && (x == null || !x.equals(y))) { |
292 |
< |
threadFailed = true; |
384 |
> |
try { |
385 |
|
assertEquals(x, y); |
386 |
+ |
} catch (AssertionFailedError t) { |
387 |
+ |
threadRecordFailure(t); |
388 |
+ |
throw t; |
389 |
+ |
} catch (Throwable t) { |
390 |
+ |
threadUnexpectedException(t); |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* threadFail with message "should throw exception" |
395 |
> |
* Just like assertSame(x, y), but additionally recording (using |
396 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
397 |
> |
* the current testcase will fail. |
398 |
|
*/ |
399 |
< |
public void threadShouldThrow() { |
400 |
< |
threadFailed = true; |
401 |
< |
fail("should throw exception"); |
399 |
> |
public void threadAssertSame(Object x, Object y) { |
400 |
> |
try { |
401 |
> |
assertSame(x, y); |
402 |
> |
} catch (AssertionFailedError t) { |
403 |
> |
threadRecordFailure(t); |
404 |
> |
throw t; |
405 |
> |
} |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
< |
* threadFail with message "should throw" + exceptionName |
409 |
> |
* Calls threadFail with message "should throw exception". |
410 |
|
*/ |
411 |
< |
public void threadShouldThrow(String exceptionName) { |
412 |
< |
threadFailed = true; |
310 |
< |
fail("should throw " + exceptionName); |
411 |
> |
public void threadShouldThrow() { |
412 |
> |
threadFail("should throw exception"); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* threadFail with message "Unexpected exception" |
416 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
417 |
|
*/ |
418 |
< |
public void threadUnexpectedException() { |
419 |
< |
threadFailed = true; |
318 |
< |
fail("Unexpected exception"); |
418 |
> |
public void threadShouldThrow(String exceptionName) { |
419 |
> |
threadFail("should throw " + exceptionName); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* threadFail with message "Unexpected exception", with argument |
423 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
424 |
> |
* then rethrows the exception, wrapping it in an |
425 |
> |
* AssertionFailedError if necessary. |
426 |
|
*/ |
427 |
< |
public void threadUnexpectedException(Throwable ex) { |
428 |
< |
threadFailed = true; |
429 |
< |
ex.printStackTrace(); |
430 |
< |
fail("Unexpected exception: " + ex); |
427 |
> |
public void threadUnexpectedException(Throwable t) { |
428 |
> |
threadRecordFailure(t); |
429 |
> |
t.printStackTrace(); |
430 |
> |
if (t instanceof RuntimeException) |
431 |
> |
throw (RuntimeException) t; |
432 |
> |
else if (t instanceof Error) |
433 |
> |
throw (Error) t; |
434 |
> |
else { |
435 |
> |
AssertionFailedError afe = |
436 |
> |
new AssertionFailedError("unexpected exception: " + t); |
437 |
> |
t.initCause(t); |
438 |
> |
throw afe; |
439 |
> |
} |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
< |
* Wait out termination of a thread pool or fail doing so |
443 |
> |
* Waits out termination of a thread pool or fails doing so. |
444 |
|
*/ |
445 |
|
public void joinPool(ExecutorService exec) { |
446 |
|
try { |
447 |
|
exec.shutdown(); |
448 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
448 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
449 |
> |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
450 |
|
} catch (SecurityException ok) { |
451 |
|
// Allowed in case test doesn't have privs |
452 |
|
} catch (InterruptedException ie) { |
456 |
|
|
457 |
|
|
458 |
|
/** |
459 |
< |
* fail with message "should throw exception" |
459 |
> |
* Fails with message "should throw exception". |
460 |
|
*/ |
461 |
|
public void shouldThrow() { |
462 |
|
fail("Should throw exception"); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
< |
* fail with message "should throw " + exceptionName |
466 |
> |
* Fails with message "should throw " + exceptionName. |
467 |
|
*/ |
468 |
|
public void shouldThrow(String exceptionName) { |
469 |
|
fail("Should throw " + exceptionName); |
470 |
|
} |
471 |
|
|
472 |
|
/** |
360 |
– |
* fail with message "Unexpected exception" |
361 |
– |
*/ |
362 |
– |
public void unexpectedException() { |
363 |
– |
fail("Unexpected exception"); |
364 |
– |
} |
365 |
– |
|
366 |
– |
/** |
367 |
– |
* fail with message "Unexpected exception", with argument |
368 |
– |
*/ |
369 |
– |
public void unexpectedException(Throwable ex) { |
370 |
– |
ex.printStackTrace(); |
371 |
– |
fail("Unexpected exception: " + ex); |
372 |
– |
} |
373 |
– |
|
374 |
– |
|
375 |
– |
/** |
473 |
|
* The number of elements to place in collections, arrays, etc. |
474 |
|
*/ |
475 |
|
public static final int SIZE = 20; |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
583 |
> |
* Sleeps until the given time has elapsed. |
584 |
> |
* Throws AssertionFailedError if interrupted. |
585 |
> |
*/ |
586 |
> |
void sleep(long millis) { |
587 |
> |
try { |
588 |
> |
Thread.sleep(millis); |
589 |
> |
} catch (InterruptedException ie) { |
590 |
> |
AssertionFailedError afe = |
591 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
592 |
> |
afe.initCause(ie); |
593 |
> |
throw afe; |
594 |
> |
} |
595 |
> |
} |
596 |
> |
|
597 |
> |
/** |
598 |
> |
* Sleeps until the timeout has elapsed, or interrupted. |
599 |
|
* Does <em>NOT</em> throw InterruptedException. |
600 |
|
*/ |
601 |
|
void sleepTillInterrupted(long timeoutMillis) { |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* Returns a new started Thread running the given runnable. |
608 |
> |
* Returns a new started daemon Thread running the given runnable. |
609 |
|
*/ |
610 |
|
Thread newStartedThread(Runnable runnable) { |
611 |
|
Thread t = new Thread(runnable); |
612 |
+ |
t.setDaemon(true); |
613 |
|
t.start(); |
614 |
|
return t; |
615 |
|
} |
616 |
|
|
617 |
+ |
/** |
618 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
619 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
620 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
621 |
+ |
*/ |
622 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
623 |
+ |
try { |
624 |
+ |
t.join(timeoutMillis); |
625 |
+ |
} catch (InterruptedException ie) { |
626 |
+ |
threadUnexpectedException(ie); |
627 |
+ |
} finally { |
628 |
+ |
if (t.isAlive()) { |
629 |
+ |
t.interrupt(); |
630 |
+ |
fail("Test timed out"); |
631 |
+ |
} |
632 |
+ |
} |
633 |
+ |
} |
634 |
+ |
|
635 |
|
// Some convenient Runnable classes |
636 |
|
|
637 |
|
public abstract class CheckedRunnable implements Runnable { |
708 |
|
return realCall(); |
709 |
|
} catch (Throwable t) { |
710 |
|
threadUnexpectedException(t); |
711 |
+ |
return null; |
712 |
|
} |
581 |
– |
return null; |
713 |
|
} |
714 |
|
} |
715 |
|
|
716 |
< |
public abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
716 |
> |
public abstract class CheckedInterruptedCallable<T> |
717 |
> |
implements Callable<T> { |
718 |
|
protected abstract T realCall() throws Throwable; |
719 |
|
|
720 |
|
public final T call() { |
795 |
|
} |
796 |
|
} |
797 |
|
|
666 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
667 |
– |
protected void realRun() throws InterruptedException { |
668 |
– |
Thread.sleep(SMALL_DELAY_MS); |
669 |
– |
} |
670 |
– |
} |
671 |
– |
|
798 |
|
public class MediumRunnable extends CheckedRunnable { |
799 |
|
protected void realRun() throws Throwable { |
800 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
807 |
|
} |
808 |
|
} |
809 |
|
|
810 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
811 |
+ |
return new CheckedRunnable() { |
812 |
+ |
protected void realRun() { |
813 |
+ |
try { |
814 |
+ |
Thread.sleep(timeoutMillis); |
815 |
+ |
} catch (InterruptedException ok) {} |
816 |
+ |
}}; |
817 |
+ |
} |
818 |
+ |
|
819 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
820 |
|
protected void realRun() { |
821 |
|
try { |
841 |
|
} |
842 |
|
} |
843 |
|
|
844 |
+ |
public interface TrackedRunnable extends Runnable { |
845 |
+ |
boolean isDone(); |
846 |
+ |
} |
847 |
+ |
|
848 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
849 |
+ |
return new TrackedRunnable() { |
850 |
+ |
private volatile boolean done = false; |
851 |
+ |
public boolean isDone() { return done; } |
852 |
+ |
public void run() { |
853 |
+ |
try { |
854 |
+ |
Thread.sleep(timeoutMillis); |
855 |
+ |
done = true; |
856 |
+ |
} catch (InterruptedException ok) {} |
857 |
+ |
} |
858 |
+ |
}; |
859 |
+ |
} |
860 |
+ |
|
861 |
|
public static class TrackedShortRunnable implements Runnable { |
862 |
|
public volatile boolean done = false; |
863 |
|
public void run() { |
864 |
|
try { |
865 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
866 |
+ |
done = true; |
867 |
+ |
} catch (InterruptedException ok) {} |
868 |
+ |
} |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
public static class TrackedSmallRunnable implements Runnable { |
872 |
+ |
public volatile boolean done = false; |
873 |
+ |
public void run() { |
874 |
+ |
try { |
875 |
|
Thread.sleep(SMALL_DELAY_MS); |
876 |
|
done = true; |
877 |
|
} catch (InterruptedException ok) {} |
916 |
|
} |
917 |
|
} |
918 |
|
|
919 |
+ |
/** |
920 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
921 |
+ |
*/ |
922 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
923 |
+ |
protected abstract void realCompute() throws Throwable; |
924 |
+ |
|
925 |
+ |
public final void compute() { |
926 |
+ |
try { |
927 |
+ |
realCompute(); |
928 |
+ |
} catch (Throwable t) { |
929 |
+ |
threadUnexpectedException(t); |
930 |
+ |
} |
931 |
+ |
} |
932 |
+ |
} |
933 |
+ |
|
934 |
+ |
/** |
935 |
+ |
* Analog of CheckedCallable for RecursiveTask |
936 |
+ |
*/ |
937 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
938 |
+ |
protected abstract T realCompute() throws Throwable; |
939 |
+ |
|
940 |
+ |
public final T compute() { |
941 |
+ |
try { |
942 |
+ |
return realCompute(); |
943 |
+ |
} catch (Throwable t) { |
944 |
+ |
threadUnexpectedException(t); |
945 |
+ |
return null; |
946 |
+ |
} |
947 |
+ |
} |
948 |
+ |
} |
949 |
|
|
950 |
|
/** |
951 |
|
* For use as RejectedExecutionHandler in constructors |
955 |
|
ThreadPoolExecutor executor) {} |
956 |
|
} |
957 |
|
|
958 |
+ |
/** |
959 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
960 |
+ |
* of throwing checked exceptions. |
961 |
+ |
*/ |
962 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
963 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
964 |
+ |
|
965 |
+ |
public int await() { |
966 |
+ |
try { |
967 |
+ |
return super.await(); |
968 |
+ |
} catch (Exception e) { |
969 |
+ |
AssertionFailedError afe = |
970 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
971 |
+ |
afe.initCause(e); |
972 |
+ |
throw afe; |
973 |
+ |
} |
974 |
+ |
} |
975 |
+ |
} |
976 |
+ |
|
977 |
|
} |