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, |
105 |
|
Policy.setPolicy(permissivePolicy()); |
106 |
|
System.setSecurityManager(new SecurityManager()); |
107 |
|
} |
108 |
< |
int iters = 1; |
109 |
< |
if (args.length > 0) |
104 |
< |
iters = Integer.parseInt(args[0]); |
108 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
109 |
> |
|
110 |
|
Test s = suite(); |
111 |
|
for (int i = 0; i < iters; ++i) { |
112 |
|
junit.textui.TestRunner.run(s); |
116 |
|
System.exit(0); |
117 |
|
} |
118 |
|
|
119 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
120 |
+ |
TestSuite suite = new TestSuite(); |
121 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
122 |
+ |
if (suiteOrClass instanceof TestSuite) |
123 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
124 |
+ |
else if (suiteOrClass instanceof Class) |
125 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
126 |
+ |
else |
127 |
+ |
throw new ClassCastException("not a test suite or class"); |
128 |
+ |
} |
129 |
+ |
return suite; |
130 |
+ |
} |
131 |
+ |
|
132 |
|
/** |
133 |
< |
* Collects all JSR166 unit tests as one suite |
133 |
> |
* Collects all JSR166 unit tests as one suite. |
134 |
|
*/ |
135 |
|
public static Test suite() { |
136 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
137 |
< |
|
138 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
139 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
140 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
141 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
142 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
143 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
144 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
145 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
146 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
147 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
148 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
149 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
150 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
151 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
152 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
153 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
154 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
155 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
156 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
157 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
158 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
159 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
160 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
161 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
162 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
163 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
164 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
165 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
166 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
167 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
168 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
169 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
170 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
171 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
172 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
173 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
174 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
175 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
176 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
177 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
178 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
179 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
180 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
181 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
182 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
183 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
184 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
185 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
186 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
187 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
188 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
189 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
190 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
191 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
192 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
193 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
194 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
195 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
196 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
197 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
198 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
199 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
200 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
183 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
184 |
< |
|
185 |
< |
return suite; |
136 |
> |
return newTestSuite( |
137 |
> |
ForkJoinPoolTest.suite(), |
138 |
> |
ForkJoinTaskTest.suite(), |
139 |
> |
RecursiveActionTest.suite(), |
140 |
> |
RecursiveTaskTest.suite(), |
141 |
> |
LinkedTransferQueueTest.suite(), |
142 |
> |
PhaserTest.suite(), |
143 |
> |
ThreadLocalRandomTest.suite(), |
144 |
> |
AbstractExecutorServiceTest.suite(), |
145 |
> |
AbstractQueueTest.suite(), |
146 |
> |
AbstractQueuedSynchronizerTest.suite(), |
147 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
148 |
> |
ArrayBlockingQueueTest.suite(), |
149 |
> |
ArrayDequeTest.suite(), |
150 |
> |
AtomicBooleanTest.suite(), |
151 |
> |
AtomicIntegerArrayTest.suite(), |
152 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
153 |
> |
AtomicIntegerTest.suite(), |
154 |
> |
AtomicLongArrayTest.suite(), |
155 |
> |
AtomicLongFieldUpdaterTest.suite(), |
156 |
> |
AtomicLongTest.suite(), |
157 |
> |
AtomicMarkableReferenceTest.suite(), |
158 |
> |
AtomicReferenceArrayTest.suite(), |
159 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
160 |
> |
AtomicReferenceTest.suite(), |
161 |
> |
AtomicStampedReferenceTest.suite(), |
162 |
> |
ConcurrentHashMapTest.suite(), |
163 |
> |
ConcurrentLinkedDequeTest.suite(), |
164 |
> |
ConcurrentLinkedQueueTest.suite(), |
165 |
> |
ConcurrentSkipListMapTest.suite(), |
166 |
> |
ConcurrentSkipListSubMapTest.suite(), |
167 |
> |
ConcurrentSkipListSetTest.suite(), |
168 |
> |
ConcurrentSkipListSubSetTest.suite(), |
169 |
> |
CopyOnWriteArrayListTest.suite(), |
170 |
> |
CopyOnWriteArraySetTest.suite(), |
171 |
> |
CountDownLatchTest.suite(), |
172 |
> |
CyclicBarrierTest.suite(), |
173 |
> |
DelayQueueTest.suite(), |
174 |
> |
EntryTest.suite(), |
175 |
> |
ExchangerTest.suite(), |
176 |
> |
ExecutorsTest.suite(), |
177 |
> |
ExecutorCompletionServiceTest.suite(), |
178 |
> |
FutureTaskTest.suite(), |
179 |
> |
LinkedBlockingDequeTest.suite(), |
180 |
> |
LinkedBlockingQueueTest.suite(), |
181 |
> |
LinkedListTest.suite(), |
182 |
> |
LockSupportTest.suite(), |
183 |
> |
PriorityBlockingQueueTest.suite(), |
184 |
> |
PriorityQueueTest.suite(), |
185 |
> |
ReentrantLockTest.suite(), |
186 |
> |
ReentrantReadWriteLockTest.suite(), |
187 |
> |
ScheduledExecutorTest.suite(), |
188 |
> |
ScheduledExecutorSubclassTest.suite(), |
189 |
> |
SemaphoreTest.suite(), |
190 |
> |
SynchronousQueueTest.suite(), |
191 |
> |
SystemTest.suite(), |
192 |
> |
ThreadLocalTest.suite(), |
193 |
> |
ThreadPoolExecutorTest.suite(), |
194 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
195 |
> |
ThreadTest.suite(), |
196 |
> |
TimeUnitTest.suite(), |
197 |
> |
TreeMapTest.suite(), |
198 |
> |
TreeSetTest.suite(), |
199 |
> |
TreeSubMapTest.suite(), |
200 |
> |
TreeSubSetTest.suite()); |
201 |
|
} |
202 |
|
|
203 |
|
|
221 |
|
*/ |
222 |
|
protected void setDelays() { |
223 |
|
SHORT_DELAY_MS = getShortDelay(); |
224 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
224 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
225 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
226 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
226 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* Flag set true if any threadAssert methods fail |
230 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
231 |
|
*/ |
232 |
< |
volatile boolean threadFailed; |
232 |
> |
private final AtomicReference<Throwable> threadFailure |
233 |
> |
= new AtomicReference<Throwable>(null); |
234 |
|
|
235 |
|
/** |
236 |
< |
* Initializes test to indicate that no thread assertions have failed |
236 |
> |
* Records an exception so that it can be rethrown later in the test |
237 |
> |
* harness thread, triggering a test case failure. Only the first |
238 |
> |
* failure is recorded; subsequent calls to this method from within |
239 |
> |
* the same test have no effect. |
240 |
|
*/ |
241 |
+ |
public void threadRecordFailure(Throwable t) { |
242 |
+ |
threadFailure.compareAndSet(null, t); |
243 |
+ |
} |
244 |
+ |
|
245 |
|
public void setUp() { |
246 |
|
setDelays(); |
224 |
– |
threadFailed = false; |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* Triggers test case failure if any thread assertions have failed |
251 |
< |
*/ |
252 |
< |
public void tearDown() { |
253 |
< |
assertFalse(threadFailed); |
250 |
> |
* Triggers test case failure if any thread assertions have failed, |
251 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
252 |
> |
* earlier by threadRecordFailure. |
253 |
> |
*/ |
254 |
> |
public void tearDown() throws Exception { |
255 |
> |
Throwable t = threadFailure.get(); |
256 |
> |
if (t != null) { |
257 |
> |
if (t instanceof Error) |
258 |
> |
throw (Error) t; |
259 |
> |
else if (t instanceof RuntimeException) |
260 |
> |
throw (RuntimeException) t; |
261 |
> |
else if (t instanceof Exception) |
262 |
> |
throw (Exception) t; |
263 |
> |
else { |
264 |
> |
AssertionFailedError afe = |
265 |
> |
new AssertionFailedError(t.toString()); |
266 |
> |
afe.initCause(t); |
267 |
> |
throw afe; |
268 |
> |
} |
269 |
> |
} |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* Fail, also setting status to indicate current testcase should fail |
273 |
> |
* Just like fail(reason), but additionally recording (using |
274 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
275 |
> |
* the current testcase will fail. |
276 |
|
*/ |
277 |
|
public void threadFail(String reason) { |
278 |
< |
threadFailed = true; |
279 |
< |
fail(reason); |
278 |
> |
try { |
279 |
> |
fail(reason); |
280 |
> |
} catch (AssertionFailedError t) { |
281 |
> |
threadRecordFailure(t); |
282 |
> |
fail(reason); |
283 |
> |
} |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* If expression not true, set status to indicate current testcase |
288 |
< |
* should fail |
287 |
> |
* Just like assertTrue(b), but additionally recording (using |
288 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
289 |
> |
* the current testcase will fail. |
290 |
|
*/ |
291 |
|
public void threadAssertTrue(boolean b) { |
292 |
< |
if (!b) { |
248 |
< |
threadFailed = true; |
292 |
> |
try { |
293 |
|
assertTrue(b); |
294 |
+ |
} catch (AssertionFailedError t) { |
295 |
+ |
threadRecordFailure(t); |
296 |
+ |
throw t; |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* If expression not false, set status to indicate current testcase |
302 |
< |
* should fail |
301 |
> |
* Just like assertFalse(b), but additionally recording (using |
302 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
303 |
> |
* the current testcase will fail. |
304 |
|
*/ |
305 |
|
public void threadAssertFalse(boolean b) { |
306 |
< |
if (b) { |
259 |
< |
threadFailed = true; |
306 |
> |
try { |
307 |
|
assertFalse(b); |
308 |
+ |
} catch (AssertionFailedError t) { |
309 |
+ |
threadRecordFailure(t); |
310 |
+ |
throw t; |
311 |
|
} |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
< |
* If argument not null, set status to indicate current testcase |
316 |
< |
* should fail |
315 |
> |
* Just like assertNull(x), but additionally recording (using |
316 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
317 |
> |
* the current testcase will fail. |
318 |
|
*/ |
319 |
|
public void threadAssertNull(Object x) { |
320 |
< |
if (x != null) { |
270 |
< |
threadFailed = true; |
320 |
> |
try { |
321 |
|
assertNull(x); |
322 |
+ |
} catch (AssertionFailedError t) { |
323 |
+ |
threadRecordFailure(t); |
324 |
+ |
throw t; |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* If arguments not equal, set status to indicate current testcase |
330 |
< |
* should fail |
329 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
330 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
331 |
> |
* the current testcase will fail. |
332 |
|
*/ |
333 |
|
public void threadAssertEquals(long x, long y) { |
334 |
< |
if (x != y) { |
281 |
< |
threadFailed = true; |
334 |
> |
try { |
335 |
|
assertEquals(x, y); |
336 |
+ |
} catch (AssertionFailedError t) { |
337 |
+ |
threadRecordFailure(t); |
338 |
+ |
throw t; |
339 |
|
} |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* If arguments not equal, set status to indicate current testcase |
344 |
< |
* should fail |
343 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
344 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
345 |
> |
* the current testcase will fail. |
346 |
|
*/ |
347 |
|
public void threadAssertEquals(Object x, Object y) { |
348 |
< |
if (x != y && (x == null || !x.equals(y))) { |
292 |
< |
threadFailed = true; |
348 |
> |
try { |
349 |
|
assertEquals(x, y); |
350 |
+ |
} catch (AssertionFailedError t) { |
351 |
+ |
threadRecordFailure(t); |
352 |
+ |
throw t; |
353 |
+ |
} catch (Throwable t) { |
354 |
+ |
threadUnexpectedException(t); |
355 |
|
} |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
< |
* If arguments not identical, set status to indicate current testcase |
360 |
< |
* should fail |
359 |
> |
* Just like assertSame(x, y), but additionally recording (using |
360 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
361 |
> |
* the current testcase will fail. |
362 |
|
*/ |
363 |
|
public void threadAssertSame(Object x, Object y) { |
364 |
< |
if (x != y) { |
303 |
< |
threadFailed = true; |
364 |
> |
try { |
365 |
|
assertSame(x, y); |
366 |
+ |
} catch (AssertionFailedError t) { |
367 |
+ |
threadRecordFailure(t); |
368 |
+ |
throw t; |
369 |
|
} |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
< |
* threadFail with message "should throw exception" |
373 |
> |
* Calls threadFail with message "should throw exception". |
374 |
|
*/ |
375 |
|
public void threadShouldThrow() { |
376 |
< |
threadFailed = true; |
313 |
< |
fail("should throw exception"); |
376 |
> |
threadFail("should throw exception"); |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
< |
* threadFail with message "should throw" + exceptionName |
380 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
381 |
|
*/ |
382 |
|
public void threadShouldThrow(String exceptionName) { |
383 |
< |
threadFailed = true; |
321 |
< |
fail("should throw " + exceptionName); |
383 |
> |
threadFail("should throw " + exceptionName); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* threadFail with message "Unexpected exception" |
387 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
388 |
> |
* then rethrows the exception, wrapping it in an |
389 |
> |
* AssertionFailedError if necessary. |
390 |
|
*/ |
391 |
< |
public void threadUnexpectedException() { |
392 |
< |
threadFailed = true; |
393 |
< |
fail("Unexpected exception"); |
394 |
< |
} |
395 |
< |
|
396 |
< |
/** |
397 |
< |
* threadFail with message "Unexpected exception", with argument |
398 |
< |
*/ |
399 |
< |
public void threadUnexpectedException(Throwable ex) { |
400 |
< |
threadFailed = true; |
401 |
< |
ex.printStackTrace(); |
402 |
< |
fail("Unexpected exception: " + ex); |
391 |
> |
public void threadUnexpectedException(Throwable t) { |
392 |
> |
threadRecordFailure(t); |
393 |
> |
t.printStackTrace(); |
394 |
> |
if (t instanceof RuntimeException) |
395 |
> |
throw (RuntimeException) t; |
396 |
> |
else if (t instanceof Error) |
397 |
> |
throw (Error) t; |
398 |
> |
else { |
399 |
> |
AssertionFailedError afe = |
400 |
> |
new AssertionFailedError("unexpected exception: " + t); |
401 |
> |
t.initCause(t); |
402 |
> |
throw afe; |
403 |
> |
} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
< |
* Wait out termination of a thread pool or fail doing so |
407 |
> |
* Waits out termination of a thread pool or fails doing so. |
408 |
|
*/ |
409 |
|
public void joinPool(ExecutorService exec) { |
410 |
|
try { |
411 |
|
exec.shutdown(); |
412 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
412 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
413 |
> |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
414 |
|
} catch (SecurityException ok) { |
415 |
|
// Allowed in case test doesn't have privs |
416 |
|
} catch (InterruptedException ie) { |
420 |
|
|
421 |
|
|
422 |
|
/** |
423 |
< |
* fail with message "should throw exception" |
423 |
> |
* Fails with message "should throw exception". |
424 |
|
*/ |
425 |
|
public void shouldThrow() { |
426 |
|
fail("Should throw exception"); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* fail with message "should throw " + exceptionName |
430 |
> |
* Fails with message "should throw " + exceptionName. |
431 |
|
*/ |
432 |
|
public void shouldThrow(String exceptionName) { |
433 |
|
fail("Should throw " + exceptionName); |
434 |
|
} |
435 |
|
|
436 |
|
/** |
371 |
– |
* fail with message "Unexpected exception" |
372 |
– |
*/ |
373 |
– |
public void unexpectedException() { |
374 |
– |
fail("Unexpected exception"); |
375 |
– |
} |
376 |
– |
|
377 |
– |
/** |
378 |
– |
* fail with message "Unexpected exception", with argument |
379 |
– |
*/ |
380 |
– |
public void unexpectedException(Throwable ex) { |
381 |
– |
ex.printStackTrace(); |
382 |
– |
fail("Unexpected exception: " + ex); |
383 |
– |
} |
384 |
– |
|
385 |
– |
|
386 |
– |
/** |
437 |
|
* The number of elements to place in collections, arrays, etc. |
438 |
|
*/ |
439 |
|
public static final int SIZE = 20; |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
547 |
> |
* Sleeps until the given time has elapsed. |
548 |
> |
* Throws AssertionFailedError if interrupted. |
549 |
> |
*/ |
550 |
> |
void sleep(long millis) { |
551 |
> |
try { |
552 |
> |
Thread.sleep(millis); |
553 |
> |
} catch (InterruptedException ie) { |
554 |
> |
AssertionFailedError afe = |
555 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
556 |
> |
afe.initCause(ie); |
557 |
> |
throw afe; |
558 |
> |
} |
559 |
> |
} |
560 |
> |
|
561 |
> |
/** |
562 |
> |
* Sleeps until the timeout has elapsed, or interrupted. |
563 |
|
* Does <em>NOT</em> throw InterruptedException. |
564 |
|
*/ |
565 |
|
void sleepTillInterrupted(long timeoutMillis) { |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
< |
* Returns a new started Thread running the given runnable. |
572 |
> |
* Returns a new started daemon Thread running the given runnable. |
573 |
|
*/ |
574 |
|
Thread newStartedThread(Runnable runnable) { |
575 |
|
Thread t = new Thread(runnable); |
576 |
+ |
t.setDaemon(true); |
577 |
|
t.start(); |
578 |
|
return t; |
579 |
|
} |
580 |
|
|
581 |
+ |
/** |
582 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
583 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
584 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
585 |
+ |
*/ |
586 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
587 |
+ |
try { |
588 |
+ |
t.join(timeoutMillis); |
589 |
+ |
} catch (InterruptedException ie) { |
590 |
+ |
threadUnexpectedException(ie); |
591 |
+ |
} finally { |
592 |
+ |
if (t.isAlive()) { |
593 |
+ |
t.interrupt(); |
594 |
+ |
fail("Test timed out"); |
595 |
+ |
} |
596 |
+ |
} |
597 |
+ |
} |
598 |
+ |
|
599 |
|
// Some convenient Runnable classes |
600 |
|
|
601 |
|
public abstract class CheckedRunnable implements Runnable { |
672 |
|
return realCall(); |
673 |
|
} catch (Throwable t) { |
674 |
|
threadUnexpectedException(t); |
675 |
+ |
return null; |
676 |
|
} |
592 |
– |
return null; |
677 |
|
} |
678 |
|
} |
679 |
|
|
680 |
< |
public abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
680 |
> |
public abstract class CheckedInterruptedCallable<T> |
681 |
> |
implements Callable<T> { |
682 |
|
protected abstract T realCall() throws Throwable; |
683 |
|
|
684 |
|
public final T call() { |
759 |
|
} |
760 |
|
} |
761 |
|
|
677 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
678 |
– |
protected void realRun() throws InterruptedException { |
679 |
– |
Thread.sleep(SMALL_DELAY_MS); |
680 |
– |
} |
681 |
– |
} |
682 |
– |
|
762 |
|
public class MediumRunnable extends CheckedRunnable { |
763 |
|
protected void realRun() throws Throwable { |
764 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
844 |
|
} |
845 |
|
} |
846 |
|
|
847 |
+ |
/** |
848 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
849 |
+ |
*/ |
850 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
851 |
+ |
protected abstract void realCompute() throws Throwable; |
852 |
+ |
|
853 |
+ |
public final void compute() { |
854 |
+ |
try { |
855 |
+ |
realCompute(); |
856 |
+ |
} catch (Throwable t) { |
857 |
+ |
threadUnexpectedException(t); |
858 |
+ |
} |
859 |
+ |
} |
860 |
+ |
} |
861 |
+ |
|
862 |
+ |
/** |
863 |
+ |
* Analog of CheckedCallable for RecursiveTask |
864 |
+ |
*/ |
865 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
866 |
+ |
protected abstract T realCompute() throws Throwable; |
867 |
+ |
|
868 |
+ |
public final T compute() { |
869 |
+ |
try { |
870 |
+ |
return realCompute(); |
871 |
+ |
} catch (Throwable t) { |
872 |
+ |
threadUnexpectedException(t); |
873 |
+ |
return null; |
874 |
+ |
} |
875 |
+ |
} |
876 |
+ |
} |
877 |
|
|
878 |
|
/** |
879 |
|
* For use as RejectedExecutionHandler in constructors |
883 |
|
ThreadPoolExecutor executor) {} |
884 |
|
} |
885 |
|
|
886 |
+ |
/** |
887 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
888 |
+ |
* of throwing checked exceptions. |
889 |
+ |
*/ |
890 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
891 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
892 |
+ |
|
893 |
+ |
public int await() { |
894 |
+ |
try { |
895 |
+ |
return super.await(); |
896 |
+ |
} catch (Exception e) { |
897 |
+ |
AssertionFailedError afe = |
898 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
899 |
+ |
afe.initCause(e); |
900 |
+ |
throw afe; |
901 |
+ |
} |
902 |
+ |
} |
903 |
+ |
} |
904 |
+ |
|
905 |
|
} |