1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.io.ByteArrayInputStream; |
11 |
> |
import java.io.ByteArrayOutputStream; |
12 |
> |
import java.io.ObjectInputStream; |
13 |
> |
import java.io.ObjectOutputStream; |
14 |
> |
import java.util.Arrays; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.PropertyPermission; |
17 |
|
import java.util.concurrent.*; |
18 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
19 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import java.io.*; |
21 |
< |
import java.security.*; |
20 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
21 |
> |
import java.security.CodeSource; |
22 |
> |
import java.security.Permission; |
23 |
> |
import java.security.PermissionCollection; |
24 |
> |
import java.security.Permissions; |
25 |
> |
import java.security.Policy; |
26 |
> |
import java.security.ProtectionDomain; |
27 |
> |
import java.security.SecurityPermission; |
28 |
|
|
29 |
|
/** |
30 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
103 |
|
private static final boolean useSecurityManager = |
104 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
105 |
|
|
106 |
+ |
protected static final boolean expensiveTests = |
107 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
108 |
+ |
|
109 |
+ |
/** |
110 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
111 |
+ |
* take more than profileThreshold milliseconds to execute. |
112 |
+ |
*/ |
113 |
+ |
private static final boolean profileTests = |
114 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
115 |
+ |
|
116 |
+ |
/** |
117 |
+ |
* The number of milliseconds that tests are permitted for |
118 |
+ |
* execution without being reported, when profileTests is set. |
119 |
+ |
*/ |
120 |
+ |
private static final long profileThreshold = |
121 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
122 |
+ |
|
123 |
+ |
protected void runTest() throws Throwable { |
124 |
+ |
if (profileTests) |
125 |
+ |
runTestProfiled(); |
126 |
+ |
else |
127 |
+ |
super.runTest(); |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
protected void runTestProfiled() throws Throwable { |
131 |
+ |
long t0 = System.nanoTime(); |
132 |
+ |
try { |
133 |
+ |
super.runTest(); |
134 |
+ |
} finally { |
135 |
+ |
long elapsedMillis = |
136 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
137 |
+ |
if (elapsedMillis >= profileThreshold) |
138 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
139 |
+ |
} |
140 |
+ |
} |
141 |
+ |
|
142 |
|
/** |
143 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
144 |
|
*/ |
148 |
|
Policy.setPolicy(permissivePolicy()); |
149 |
|
System.setSecurityManager(new SecurityManager()); |
150 |
|
} |
151 |
< |
int iters = 1; |
152 |
< |
if (args.length > 0) |
104 |
< |
iters = Integer.parseInt(args[0]); |
151 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
152 |
> |
|
153 |
|
Test s = suite(); |
154 |
|
for (int i = 0; i < iters; ++i) { |
155 |
|
junit.textui.TestRunner.run(s); |
159 |
|
System.exit(0); |
160 |
|
} |
161 |
|
|
162 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
163 |
+ |
TestSuite suite = new TestSuite(); |
164 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
165 |
+ |
if (suiteOrClass instanceof TestSuite) |
166 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
167 |
+ |
else if (suiteOrClass instanceof Class) |
168 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
169 |
+ |
else |
170 |
+ |
throw new ClassCastException("not a test suite or class"); |
171 |
+ |
} |
172 |
+ |
return suite; |
173 |
+ |
} |
174 |
+ |
|
175 |
|
/** |
176 |
< |
* Collects all JSR166 unit tests as one suite |
176 |
> |
* Collects all JSR166 unit tests as one suite. |
177 |
|
*/ |
178 |
|
public static Test suite() { |
179 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
180 |
< |
|
181 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
182 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
183 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
184 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
185 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
186 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
187 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
188 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
189 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
190 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
191 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
192 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
193 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
194 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
195 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
196 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
197 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
198 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
199 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
200 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
201 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
202 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
203 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
204 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
205 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
206 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
207 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
208 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
209 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
210 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
211 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
212 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
213 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
214 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
215 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
216 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
217 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
218 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
219 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
220 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
221 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
222 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
223 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
224 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
225 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
226 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
227 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
228 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
229 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
230 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
231 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
232 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
233 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
234 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
235 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
236 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
237 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
238 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
239 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
240 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
241 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
242 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
243 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
183 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
184 |
< |
|
185 |
< |
return suite; |
179 |
> |
return newTestSuite( |
180 |
> |
ForkJoinPoolTest.suite(), |
181 |
> |
ForkJoinTaskTest.suite(), |
182 |
> |
RecursiveActionTest.suite(), |
183 |
> |
RecursiveTaskTest.suite(), |
184 |
> |
LinkedTransferQueueTest.suite(), |
185 |
> |
PhaserTest.suite(), |
186 |
> |
ThreadLocalRandomTest.suite(), |
187 |
> |
AbstractExecutorServiceTest.suite(), |
188 |
> |
AbstractQueueTest.suite(), |
189 |
> |
AbstractQueuedSynchronizerTest.suite(), |
190 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
191 |
> |
ArrayBlockingQueueTest.suite(), |
192 |
> |
ArrayDequeTest.suite(), |
193 |
> |
AtomicBooleanTest.suite(), |
194 |
> |
AtomicIntegerArrayTest.suite(), |
195 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
196 |
> |
AtomicIntegerTest.suite(), |
197 |
> |
AtomicLongArrayTest.suite(), |
198 |
> |
AtomicLongFieldUpdaterTest.suite(), |
199 |
> |
AtomicLongTest.suite(), |
200 |
> |
AtomicMarkableReferenceTest.suite(), |
201 |
> |
AtomicReferenceArrayTest.suite(), |
202 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
203 |
> |
AtomicReferenceTest.suite(), |
204 |
> |
AtomicStampedReferenceTest.suite(), |
205 |
> |
ConcurrentHashMapTest.suite(), |
206 |
> |
ConcurrentLinkedDequeTest.suite(), |
207 |
> |
ConcurrentLinkedQueueTest.suite(), |
208 |
> |
ConcurrentSkipListMapTest.suite(), |
209 |
> |
ConcurrentSkipListSubMapTest.suite(), |
210 |
> |
ConcurrentSkipListSetTest.suite(), |
211 |
> |
ConcurrentSkipListSubSetTest.suite(), |
212 |
> |
CopyOnWriteArrayListTest.suite(), |
213 |
> |
CopyOnWriteArraySetTest.suite(), |
214 |
> |
CountDownLatchTest.suite(), |
215 |
> |
CyclicBarrierTest.suite(), |
216 |
> |
DelayQueueTest.suite(), |
217 |
> |
EntryTest.suite(), |
218 |
> |
ExchangerTest.suite(), |
219 |
> |
ExecutorsTest.suite(), |
220 |
> |
ExecutorCompletionServiceTest.suite(), |
221 |
> |
FutureTaskTest.suite(), |
222 |
> |
LinkedBlockingDequeTest.suite(), |
223 |
> |
LinkedBlockingQueueTest.suite(), |
224 |
> |
LinkedListTest.suite(), |
225 |
> |
LockSupportTest.suite(), |
226 |
> |
PriorityBlockingQueueTest.suite(), |
227 |
> |
PriorityQueueTest.suite(), |
228 |
> |
ReentrantLockTest.suite(), |
229 |
> |
ReentrantReadWriteLockTest.suite(), |
230 |
> |
ScheduledExecutorTest.suite(), |
231 |
> |
ScheduledExecutorSubclassTest.suite(), |
232 |
> |
SemaphoreTest.suite(), |
233 |
> |
SynchronousQueueTest.suite(), |
234 |
> |
SystemTest.suite(), |
235 |
> |
ThreadLocalTest.suite(), |
236 |
> |
ThreadPoolExecutorTest.suite(), |
237 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
238 |
> |
ThreadTest.suite(), |
239 |
> |
TimeUnitTest.suite(), |
240 |
> |
TreeMapTest.suite(), |
241 |
> |
TreeSetTest.suite(), |
242 |
> |
TreeSubMapTest.suite(), |
243 |
> |
TreeSubSetTest.suite()); |
244 |
|
} |
245 |
|
|
246 |
|
|
264 |
|
*/ |
265 |
|
protected void setDelays() { |
266 |
|
SHORT_DELAY_MS = getShortDelay(); |
267 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
267 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
268 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
269 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
269 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* Flag set true if any threadAssert methods fail |
273 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
274 |
|
*/ |
275 |
< |
volatile boolean threadFailed; |
275 |
> |
private final AtomicReference<Throwable> threadFailure |
276 |
> |
= new AtomicReference<Throwable>(null); |
277 |
|
|
278 |
|
/** |
279 |
< |
* Initializes test to indicate that no thread assertions have failed |
279 |
> |
* Records an exception so that it can be rethrown later in the test |
280 |
> |
* harness thread, triggering a test case failure. Only the first |
281 |
> |
* failure is recorded; subsequent calls to this method from within |
282 |
> |
* the same test have no effect. |
283 |
|
*/ |
284 |
+ |
public void threadRecordFailure(Throwable t) { |
285 |
+ |
threadFailure.compareAndSet(null, t); |
286 |
+ |
} |
287 |
+ |
|
288 |
|
public void setUp() { |
289 |
|
setDelays(); |
224 |
– |
threadFailed = false; |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
< |
* Triggers test case failure if any thread assertions have failed |
294 |
< |
*/ |
295 |
< |
public void tearDown() { |
296 |
< |
assertFalse(threadFailed); |
293 |
> |
* Triggers test case failure if any thread assertions have failed, |
294 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
295 |
> |
* earlier by threadRecordFailure. |
296 |
> |
*/ |
297 |
> |
public void tearDown() throws Exception { |
298 |
> |
Throwable t = threadFailure.getAndSet(null); |
299 |
> |
if (t != null) { |
300 |
> |
if (t instanceof Error) |
301 |
> |
throw (Error) t; |
302 |
> |
else if (t instanceof RuntimeException) |
303 |
> |
throw (RuntimeException) t; |
304 |
> |
else if (t instanceof Exception) |
305 |
> |
throw (Exception) t; |
306 |
> |
else { |
307 |
> |
AssertionFailedError afe = |
308 |
> |
new AssertionFailedError(t.toString()); |
309 |
> |
afe.initCause(t); |
310 |
> |
throw afe; |
311 |
> |
} |
312 |
> |
} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
< |
* Fail, also setting status to indicate current testcase should fail |
316 |
> |
* Just like fail(reason), but additionally recording (using |
317 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
318 |
> |
* the current testcase will fail. |
319 |
|
*/ |
320 |
|
public void threadFail(String reason) { |
321 |
< |
threadFailed = true; |
322 |
< |
fail(reason); |
321 |
> |
try { |
322 |
> |
fail(reason); |
323 |
> |
} catch (AssertionFailedError t) { |
324 |
> |
threadRecordFailure(t); |
325 |
> |
fail(reason); |
326 |
> |
} |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* If expression not true, set status to indicate current testcase |
331 |
< |
* should fail |
330 |
> |
* Just like assertTrue(b), but additionally recording (using |
331 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
332 |
> |
* the current testcase will fail. |
333 |
|
*/ |
334 |
|
public void threadAssertTrue(boolean b) { |
335 |
< |
if (!b) { |
248 |
< |
threadFailed = true; |
335 |
> |
try { |
336 |
|
assertTrue(b); |
337 |
+ |
} catch (AssertionFailedError t) { |
338 |
+ |
threadRecordFailure(t); |
339 |
+ |
throw t; |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* If expression not false, set status to indicate current testcase |
345 |
< |
* should fail |
344 |
> |
* Just like assertFalse(b), but additionally recording (using |
345 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
346 |
> |
* the current testcase will fail. |
347 |
|
*/ |
348 |
|
public void threadAssertFalse(boolean b) { |
349 |
< |
if (b) { |
259 |
< |
threadFailed = true; |
349 |
> |
try { |
350 |
|
assertFalse(b); |
351 |
+ |
} catch (AssertionFailedError t) { |
352 |
+ |
threadRecordFailure(t); |
353 |
+ |
throw t; |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* If argument not null, set status to indicate current testcase |
359 |
< |
* should fail |
358 |
> |
* Just like assertNull(x), but additionally recording (using |
359 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
360 |
> |
* the current testcase will fail. |
361 |
|
*/ |
362 |
|
public void threadAssertNull(Object x) { |
363 |
< |
if (x != null) { |
270 |
< |
threadFailed = true; |
363 |
> |
try { |
364 |
|
assertNull(x); |
365 |
+ |
} catch (AssertionFailedError t) { |
366 |
+ |
threadRecordFailure(t); |
367 |
+ |
throw t; |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* If arguments not equal, set status to indicate current testcase |
373 |
< |
* should fail |
372 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
373 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
374 |
> |
* the current testcase will fail. |
375 |
|
*/ |
376 |
|
public void threadAssertEquals(long x, long y) { |
377 |
< |
if (x != y) { |
281 |
< |
threadFailed = true; |
377 |
> |
try { |
378 |
|
assertEquals(x, y); |
379 |
+ |
} catch (AssertionFailedError t) { |
380 |
+ |
threadRecordFailure(t); |
381 |
+ |
throw t; |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* If arguments not equal, set status to indicate current testcase |
387 |
< |
* should fail |
386 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
387 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
388 |
> |
* the current testcase will fail. |
389 |
|
*/ |
390 |
|
public void threadAssertEquals(Object x, Object y) { |
391 |
< |
if (x != y && (x == null || !x.equals(y))) { |
292 |
< |
threadFailed = true; |
391 |
> |
try { |
392 |
|
assertEquals(x, y); |
393 |
+ |
} catch (AssertionFailedError t) { |
394 |
+ |
threadRecordFailure(t); |
395 |
+ |
throw t; |
396 |
+ |
} catch (Throwable t) { |
397 |
+ |
threadUnexpectedException(t); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* If arguments not identical, set status to indicate current testcase |
403 |
< |
* should fail |
402 |
> |
* Just like assertSame(x, y), but additionally recording (using |
403 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
404 |
> |
* the current testcase will fail. |
405 |
|
*/ |
406 |
|
public void threadAssertSame(Object x, Object y) { |
407 |
< |
if (x != y) { |
303 |
< |
threadFailed = true; |
407 |
> |
try { |
408 |
|
assertSame(x, y); |
409 |
+ |
} catch (AssertionFailedError t) { |
410 |
+ |
threadRecordFailure(t); |
411 |
+ |
throw t; |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* threadFail with message "should throw exception" |
416 |
> |
* Calls threadFail with message "should throw exception". |
417 |
|
*/ |
418 |
|
public void threadShouldThrow() { |
419 |
< |
threadFailed = true; |
313 |
< |
fail("should throw exception"); |
419 |
> |
threadFail("should throw exception"); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* threadFail with message "should throw" + exceptionName |
423 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
424 |
|
*/ |
425 |
|
public void threadShouldThrow(String exceptionName) { |
426 |
< |
threadFailed = true; |
321 |
< |
fail("should throw " + exceptionName); |
426 |
> |
threadFail("should throw " + exceptionName); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* threadFail with message "Unexpected exception" |
430 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
431 |
> |
* then rethrows the exception, wrapping it in an |
432 |
> |
* AssertionFailedError if necessary. |
433 |
|
*/ |
434 |
< |
public void threadUnexpectedException() { |
435 |
< |
threadFailed = true; |
436 |
< |
fail("Unexpected exception"); |
434 |
> |
public void threadUnexpectedException(Throwable t) { |
435 |
> |
threadRecordFailure(t); |
436 |
> |
t.printStackTrace(); |
437 |
> |
if (t instanceof RuntimeException) |
438 |
> |
throw (RuntimeException) t; |
439 |
> |
else if (t instanceof Error) |
440 |
> |
throw (Error) t; |
441 |
> |
else { |
442 |
> |
AssertionFailedError afe = |
443 |
> |
new AssertionFailedError("unexpected exception: " + t); |
444 |
> |
t.initCause(t); |
445 |
> |
throw afe; |
446 |
> |
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* threadFail with message "Unexpected exception", with argument |
450 |
> |
* Delays, via Thread.sleep for the given millisecond delay, but |
451 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
452 |
> |
* until time elapses. |
453 |
|
*/ |
454 |
< |
public void threadUnexpectedException(Throwable ex) { |
455 |
< |
threadFailed = true; |
456 |
< |
ex.printStackTrace(); |
457 |
< |
fail("Unexpected exception: " + ex); |
454 |
> |
public static void delay(long millis) throws InterruptedException { |
455 |
> |
long startTime = System.nanoTime(); |
456 |
> |
long ns = millis * 1000 * 1000; |
457 |
> |
for (;;) { |
458 |
> |
if (millis > 0L) |
459 |
> |
Thread.sleep(millis); |
460 |
> |
else // too short to sleep |
461 |
> |
Thread.yield(); |
462 |
> |
long d = ns - (System.nanoTime() - startTime); |
463 |
> |
if (d > 0L) |
464 |
> |
millis = d / (1000 * 1000); |
465 |
> |
else |
466 |
> |
break; |
467 |
> |
} |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
< |
* Wait out termination of a thread pool or fail doing so |
471 |
> |
* Waits out termination of a thread pool or fails doing so. |
472 |
|
*/ |
473 |
|
public void joinPool(ExecutorService exec) { |
474 |
|
try { |
475 |
|
exec.shutdown(); |
476 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
476 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
477 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
478 |
|
} catch (SecurityException ok) { |
479 |
|
// Allowed in case test doesn't have privs |
480 |
|
} catch (InterruptedException ie) { |
482 |
|
} |
483 |
|
} |
484 |
|
|
485 |
+ |
/** |
486 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
487 |
+ |
*/ |
488 |
+ |
public void assertThreadStaysAlive(Thread thread, long millis) { |
489 |
+ |
try { |
490 |
+ |
// No need to optimize the failing case via Thread.join. |
491 |
+ |
delay(millis); |
492 |
+ |
assertTrue(thread.isAlive()); |
493 |
+ |
} catch (InterruptedException ie) { |
494 |
+ |
fail("Unexpected InterruptedException"); |
495 |
+ |
} |
496 |
+ |
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* fail with message "should throw exception" |
499 |
> |
* Fails with message "should throw exception". |
500 |
|
*/ |
501 |
|
public void shouldThrow() { |
502 |
|
fail("Should throw exception"); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
< |
* fail with message "should throw " + exceptionName |
506 |
> |
* Fails with message "should throw " + exceptionName. |
507 |
|
*/ |
508 |
|
public void shouldThrow(String exceptionName) { |
509 |
|
fail("Should throw " + exceptionName); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
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 |
– |
/** |
513 |
|
* The number of elements to place in collections, arrays, etc. |
514 |
|
*/ |
515 |
|
public static final int SIZE = 20; |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
623 |
> |
* Sleeps until the given time has elapsed. |
624 |
> |
* Throws AssertionFailedError if interrupted. |
625 |
> |
*/ |
626 |
> |
void sleep(long millis) { |
627 |
> |
try { |
628 |
> |
delay(millis); |
629 |
> |
} catch (InterruptedException ie) { |
630 |
> |
AssertionFailedError afe = |
631 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
632 |
> |
afe.initCause(ie); |
633 |
> |
throw afe; |
634 |
> |
} |
635 |
> |
} |
636 |
> |
|
637 |
> |
/** |
638 |
> |
* Sleeps until the timeout has elapsed, or interrupted. |
639 |
|
* Does <em>NOT</em> throw InterruptedException. |
640 |
|
*/ |
641 |
|
void sleepTillInterrupted(long timeoutMillis) { |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* Returns a new started Thread running the given runnable. |
648 |
> |
* Waits up to the specified number of milliseconds for the given |
649 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
650 |
> |
*/ |
651 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
652 |
> |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
653 |
> |
long t0 = System.nanoTime(); |
654 |
> |
for (;;) { |
655 |
> |
Thread.State s = thread.getState(); |
656 |
> |
if (s == Thread.State.BLOCKED || |
657 |
> |
s == Thread.State.WAITING || |
658 |
> |
s == Thread.State.TIMED_WAITING) |
659 |
> |
return; |
660 |
> |
else if (s == Thread.State.TERMINATED) |
661 |
> |
fail("Unexpected thread termination"); |
662 |
> |
else if (System.nanoTime() - t0 > timeoutNanos) { |
663 |
> |
threadAssertTrue(thread.isAlive()); |
664 |
> |
return; |
665 |
> |
} |
666 |
> |
Thread.yield(); |
667 |
> |
} |
668 |
> |
} |
669 |
> |
|
670 |
> |
/** |
671 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
672 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
673 |
> |
*/ |
674 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
675 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
676 |
> |
} |
677 |
> |
|
678 |
> |
/** |
679 |
> |
* Returns the number of milliseconds since time given by |
680 |
> |
* startNanoTime, which must have been previously returned from a |
681 |
> |
* call to {@link System.nanoTime()}. |
682 |
> |
*/ |
683 |
> |
long millisElapsedSince(long startNanoTime) { |
684 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
685 |
> |
} |
686 |
> |
|
687 |
> |
/** |
688 |
> |
* Returns a new started daemon Thread running the given runnable. |
689 |
|
*/ |
690 |
|
Thread newStartedThread(Runnable runnable) { |
691 |
|
Thread t = new Thread(runnable); |
692 |
+ |
t.setDaemon(true); |
693 |
|
t.start(); |
694 |
|
return t; |
695 |
|
} |
696 |
|
|
697 |
+ |
/** |
698 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
699 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
700 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
701 |
+ |
*/ |
702 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
703 |
+ |
try { |
704 |
+ |
t.join(timeoutMillis); |
705 |
+ |
} catch (InterruptedException ie) { |
706 |
+ |
threadUnexpectedException(ie); |
707 |
+ |
} finally { |
708 |
+ |
if (t.isAlive()) { |
709 |
+ |
t.interrupt(); |
710 |
+ |
fail("Test timed out"); |
711 |
+ |
} |
712 |
+ |
} |
713 |
+ |
} |
714 |
+ |
|
715 |
+ |
/** |
716 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
717 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
718 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
719 |
+ |
*/ |
720 |
+ |
void awaitTermination(Thread t) { |
721 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
722 |
+ |
} |
723 |
+ |
|
724 |
|
// Some convenient Runnable classes |
725 |
|
|
726 |
|
public abstract class CheckedRunnable implements Runnable { |
797 |
|
return realCall(); |
798 |
|
} catch (Throwable t) { |
799 |
|
threadUnexpectedException(t); |
800 |
+ |
return null; |
801 |
|
} |
592 |
– |
return null; |
802 |
|
} |
803 |
|
} |
804 |
|
|
805 |
< |
public abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
805 |
> |
public abstract class CheckedInterruptedCallable<T> |
806 |
> |
implements Callable<T> { |
807 |
|
protected abstract T realCall() throws Throwable; |
808 |
|
|
809 |
|
public final T call() { |
835 |
|
|
836 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
837 |
|
return new CheckedCallable<String>() { |
838 |
< |
public String realCall() { |
838 |
> |
protected String realCall() { |
839 |
|
try { |
840 |
|
latch.await(); |
841 |
|
} catch (InterruptedException quittingTime) {} |
843 |
|
}}; |
844 |
|
} |
845 |
|
|
846 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
847 |
+ |
return new CheckedRunnable() { |
848 |
+ |
public void realRun() throws InterruptedException { |
849 |
+ |
await(latch); |
850 |
+ |
}}; |
851 |
+ |
} |
852 |
+ |
|
853 |
+ |
public void await(CountDownLatch latch) { |
854 |
+ |
try { |
855 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
856 |
+ |
} catch (Throwable t) { |
857 |
+ |
threadUnexpectedException(t); |
858 |
+ |
} |
859 |
+ |
} |
860 |
+ |
|
861 |
|
public static class NPETask implements Callable<String> { |
862 |
|
public String call() { throw new NullPointerException(); } |
863 |
|
} |
868 |
|
|
869 |
|
public class ShortRunnable extends CheckedRunnable { |
870 |
|
protected void realRun() throws Throwable { |
871 |
< |
Thread.sleep(SHORT_DELAY_MS); |
871 |
> |
delay(SHORT_DELAY_MS); |
872 |
|
} |
873 |
|
} |
874 |
|
|
875 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
876 |
|
protected void realRun() throws InterruptedException { |
877 |
< |
Thread.sleep(SHORT_DELAY_MS); |
877 |
> |
delay(SHORT_DELAY_MS); |
878 |
|
} |
879 |
|
} |
880 |
|
|
881 |
|
public class SmallRunnable extends CheckedRunnable { |
882 |
|
protected void realRun() throws Throwable { |
883 |
< |
Thread.sleep(SMALL_DELAY_MS); |
883 |
> |
delay(SMALL_DELAY_MS); |
884 |
|
} |
885 |
|
} |
886 |
|
|
887 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
888 |
|
protected void realRun() { |
889 |
|
try { |
890 |
< |
Thread.sleep(SMALL_DELAY_MS); |
890 |
> |
delay(SMALL_DELAY_MS); |
891 |
|
} catch (InterruptedException ok) {} |
892 |
|
} |
893 |
|
} |
894 |
|
|
895 |
|
public class SmallCallable extends CheckedCallable { |
896 |
|
protected Object realCall() throws InterruptedException { |
897 |
< |
Thread.sleep(SMALL_DELAY_MS); |
897 |
> |
delay(SMALL_DELAY_MS); |
898 |
|
return Boolean.TRUE; |
899 |
|
} |
900 |
|
} |
901 |
|
|
677 |
– |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
678 |
– |
protected void realRun() throws InterruptedException { |
679 |
– |
Thread.sleep(SMALL_DELAY_MS); |
680 |
– |
} |
681 |
– |
} |
682 |
– |
|
902 |
|
public class MediumRunnable extends CheckedRunnable { |
903 |
|
protected void realRun() throws Throwable { |
904 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
904 |
> |
delay(MEDIUM_DELAY_MS); |
905 |
|
} |
906 |
|
} |
907 |
|
|
908 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
909 |
|
protected void realRun() throws InterruptedException { |
910 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
910 |
> |
delay(MEDIUM_DELAY_MS); |
911 |
|
} |
912 |
|
} |
913 |
|
|
914 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
915 |
+ |
return new CheckedRunnable() { |
916 |
+ |
protected void realRun() { |
917 |
+ |
try { |
918 |
+ |
delay(timeoutMillis); |
919 |
+ |
} catch (InterruptedException ok) {} |
920 |
+ |
}}; |
921 |
+ |
} |
922 |
+ |
|
923 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
924 |
|
protected void realRun() { |
925 |
|
try { |
926 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
926 |
> |
delay(MEDIUM_DELAY_MS); |
927 |
|
} catch (InterruptedException ok) {} |
928 |
|
} |
929 |
|
} |
931 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
932 |
|
protected void realRun() { |
933 |
|
try { |
934 |
< |
Thread.sleep(LONG_DELAY_MS); |
934 |
> |
delay(LONG_DELAY_MS); |
935 |
|
} catch (InterruptedException ok) {} |
936 |
|
} |
937 |
|
} |
945 |
|
} |
946 |
|
} |
947 |
|
|
948 |
+ |
public interface TrackedRunnable extends Runnable { |
949 |
+ |
boolean isDone(); |
950 |
+ |
} |
951 |
+ |
|
952 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
953 |
+ |
return new TrackedRunnable() { |
954 |
+ |
private volatile boolean done = false; |
955 |
+ |
public boolean isDone() { return done; } |
956 |
+ |
public void run() { |
957 |
+ |
try { |
958 |
+ |
delay(timeoutMillis); |
959 |
+ |
done = true; |
960 |
+ |
} catch (InterruptedException ok) {} |
961 |
+ |
} |
962 |
+ |
}; |
963 |
+ |
} |
964 |
+ |
|
965 |
|
public static class TrackedShortRunnable implements Runnable { |
966 |
|
public volatile boolean done = false; |
967 |
|
public void run() { |
968 |
|
try { |
969 |
< |
Thread.sleep(SMALL_DELAY_MS); |
969 |
> |
delay(SHORT_DELAY_MS); |
970 |
> |
done = true; |
971 |
> |
} catch (InterruptedException ok) {} |
972 |
> |
} |
973 |
> |
} |
974 |
> |
|
975 |
> |
public static class TrackedSmallRunnable implements Runnable { |
976 |
> |
public volatile boolean done = false; |
977 |
> |
public void run() { |
978 |
> |
try { |
979 |
> |
delay(SMALL_DELAY_MS); |
980 |
|
done = true; |
981 |
|
} catch (InterruptedException ok) {} |
982 |
|
} |
986 |
|
public volatile boolean done = false; |
987 |
|
public void run() { |
988 |
|
try { |
989 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
989 |
> |
delay(MEDIUM_DELAY_MS); |
990 |
|
done = true; |
991 |
|
} catch (InterruptedException ok) {} |
992 |
|
} |
996 |
|
public volatile boolean done = false; |
997 |
|
public void run() { |
998 |
|
try { |
999 |
< |
Thread.sleep(LONG_DELAY_MS); |
999 |
> |
delay(LONG_DELAY_MS); |
1000 |
|
done = true; |
1001 |
|
} catch (InterruptedException ok) {} |
1002 |
|
} |
1013 |
|
public volatile boolean done = false; |
1014 |
|
public Object call() { |
1015 |
|
try { |
1016 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1016 |
> |
delay(SMALL_DELAY_MS); |
1017 |
|
done = true; |
1018 |
|
} catch (InterruptedException ok) {} |
1019 |
|
return Boolean.TRUE; |
1020 |
|
} |
1021 |
|
} |
1022 |
|
|
1023 |
+ |
/** |
1024 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
1025 |
+ |
*/ |
1026 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1027 |
+ |
protected abstract void realCompute() throws Throwable; |
1028 |
+ |
|
1029 |
+ |
public final void compute() { |
1030 |
+ |
try { |
1031 |
+ |
realCompute(); |
1032 |
+ |
} catch (Throwable t) { |
1033 |
+ |
threadUnexpectedException(t); |
1034 |
+ |
} |
1035 |
+ |
} |
1036 |
+ |
} |
1037 |
+ |
|
1038 |
+ |
/** |
1039 |
+ |
* Analog of CheckedCallable for RecursiveTask |
1040 |
+ |
*/ |
1041 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1042 |
+ |
protected abstract T realCompute() throws Throwable; |
1043 |
+ |
|
1044 |
+ |
public final T compute() { |
1045 |
+ |
try { |
1046 |
+ |
return realCompute(); |
1047 |
+ |
} catch (Throwable t) { |
1048 |
+ |
threadUnexpectedException(t); |
1049 |
+ |
return null; |
1050 |
+ |
} |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
|
|
1054 |
|
/** |
1055 |
|
* For use as RejectedExecutionHandler in constructors |
1059 |
|
ThreadPoolExecutor executor) {} |
1060 |
|
} |
1061 |
|
|
1062 |
+ |
/** |
1063 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1064 |
+ |
* of throwing checked exceptions. |
1065 |
+ |
*/ |
1066 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1067 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1068 |
+ |
|
1069 |
+ |
public int await() { |
1070 |
+ |
try { |
1071 |
+ |
return super.await(); |
1072 |
+ |
} catch (Exception e) { |
1073 |
+ |
AssertionFailedError afe = |
1074 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1075 |
+ |
afe.initCause(e); |
1076 |
+ |
throw afe; |
1077 |
+ |
} |
1078 |
+ |
} |
1079 |
+ |
} |
1080 |
+ |
|
1081 |
+ |
public void checkEmpty(BlockingQueue q) { |
1082 |
+ |
try { |
1083 |
+ |
assertTrue(q.isEmpty()); |
1084 |
+ |
assertEquals(0, q.size()); |
1085 |
+ |
assertNull(q.peek()); |
1086 |
+ |
assertNull(q.poll()); |
1087 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1088 |
+ |
assertEquals(q.toString(), "[]"); |
1089 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1090 |
+ |
assertFalse(q.iterator().hasNext()); |
1091 |
+ |
try { |
1092 |
+ |
q.element(); |
1093 |
+ |
shouldThrow(); |
1094 |
+ |
} catch (NoSuchElementException success) {} |
1095 |
+ |
try { |
1096 |
+ |
q.iterator().next(); |
1097 |
+ |
shouldThrow(); |
1098 |
+ |
} catch (NoSuchElementException success) {} |
1099 |
+ |
try { |
1100 |
+ |
q.remove(); |
1101 |
+ |
shouldThrow(); |
1102 |
+ |
} catch (NoSuchElementException success) {} |
1103 |
+ |
} catch (InterruptedException ie) { |
1104 |
+ |
threadUnexpectedException(ie); |
1105 |
+ |
} |
1106 |
+ |
} |
1107 |
+ |
|
1108 |
+ |
@SuppressWarnings("unchecked") |
1109 |
+ |
public <T> T serialClone(T o) { |
1110 |
+ |
try { |
1111 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1112 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1113 |
+ |
oos.writeObject(o); |
1114 |
+ |
oos.flush(); |
1115 |
+ |
oos.close(); |
1116 |
+ |
ByteArrayInputStream bin = |
1117 |
+ |
new ByteArrayInputStream(bos.toByteArray()); |
1118 |
+ |
ObjectInputStream ois = new ObjectInputStream(bin); |
1119 |
+ |
return (T) ois.readObject(); |
1120 |
+ |
} catch (Throwable t) { |
1121 |
+ |
threadUnexpectedException(t); |
1122 |
+ |
return null; |
1123 |
+ |
} |
1124 |
+ |
} |
1125 |
|
} |