ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
Revision: 1.35
Committed: Mon Aug 3 19:07:51 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.34: +71 -68 lines
Log Message:
checked runnables; import static MILLISECONDS; coding style cleanups

File Contents

# Content
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
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12 import java.io.*;
13 import java.security.*;
14
15 /**
16 * Base class for JSR166 Junit TCK tests. Defines some constants,
17 * utility methods and classes, as well as a simple framework for
18 * helping to make sure that assertions failing in generated threads
19 * cause the associated test that generated them to itself fail (which
20 * JUnit does not otherwise arrange). The rules for creating such
21 * tests are:
22 *
23 * <ol>
24 *
25 * <li> All assertions in code running in generated threads must use
26 * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
27 * #threadAssertEquals}, or {@link #threadAssertNull}, (not
28 * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
29 * particularly recommended) for other code to use these forms too.
30 * Only the most typically used JUnit assertion methods are defined
31 * this way, but enough to live with.</li>
32 *
33 * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
34 * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
35 * them. These methods are used to clear and check for thread
36 * assertion failures.</li>
37 *
38 * <li>All delays and timeouts must use one of the constants <tt>
39 * SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>,
40 * <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always
41 * discriminable from zero time, and always allows enough time for the
42 * small amounts of computation (creating a thread, calling a few
43 * methods, etc) needed to reach a timeout point. Similarly, a SMALL
44 * is always discriminable as larger than SHORT and smaller than
45 * MEDIUM. And so on. These constants are set to conservative values,
46 * but even so, if there is ever any doubt, they can all be increased
47 * in one spot to rerun tests on slower platforms.</li>
48 *
49 * <li> All threads generated must be joined inside each test case
50 * method (or <tt>fail</tt> to do so) before returning from the
51 * method. The <tt> joinPool</tt> method can be used to do this when
52 * using Executors.</li>
53 *
54 * </ol>
55 *
56 * <p> <b>Other notes</b>
57 * <ul>
58 *
59 * <li> Usually, there is one testcase method per JSR166 method
60 * covering "normal" operation, and then as many exception-testing
61 * methods as there are exceptions the method can throw. Sometimes
62 * there are multiple tests per JSR166 method when the different
63 * "normal" behaviors differ significantly. And sometimes testcases
64 * cover multiple methods when they cannot be tested in
65 * isolation.</li>
66 *
67 * <li> The documentation style for testcases is to provide as javadoc
68 * a simple sentence or two describing the property that the testcase
69 * method purports to test. The javadocs do not say anything about how
70 * the property is tested. To find out, read the code.</li>
71 *
72 * <li> These tests are "conformance tests", and do not attempt to
73 * test throughput, latency, scalability or other performance factors
74 * (see the separate "jtreg" tests for a set intended to check these
75 * for the most central aspects of functionality.) So, most tests use
76 * the smallest sensible numbers of threads, collection sizes, etc
77 * needed to check basic conformance.</li>
78 *
79 * <li>The test classes currently do not declare inclusion in
80 * any particular package to simplify things for people integrating
81 * them in TCK test suites.</li>
82 *
83 * <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase)
84 * runs all JSR166 unit tests.</li>
85 *
86 * </ul>
87 */
88 public class JSR166TestCase extends TestCase {
89 /**
90 * Runs all JSR166 unit tests using junit.textui.TestRunner
91 */
92 public static void main (String[] args) {
93 int iters = 1;
94 if (args.length > 0)
95 iters = Integer.parseInt(args[0]);
96 Test s = suite();
97 for (int i = 0; i < iters; ++i) {
98 junit.textui.TestRunner.run (s);
99 System.gc();
100 System.runFinalization();
101 }
102 System.exit(0);
103 }
104
105 /**
106 * Collects all JSR166 unit tests as one suite
107 */
108 public static Test suite ( ) {
109 TestSuite suite = new TestSuite("JSR166 Unit Tests");
110
111 suite.addTest(new TestSuite(ForkJoinPoolTest.class));
112 suite.addTest(new TestSuite(ForkJoinTaskTest.class));
113 suite.addTest(new TestSuite(RecursiveActionTest.class));
114 suite.addTest(new TestSuite(RecursiveTaskTest.class));
115 suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
116 suite.addTest(new TestSuite(PhaserTest.class));
117 suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
118 suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
119 suite.addTest(new TestSuite(AbstractQueueTest.class));
120 suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
121 suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
122 suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
123 suite.addTest(new TestSuite(ArrayDequeTest.class));
124 suite.addTest(new TestSuite(AtomicBooleanTest.class));
125 suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
126 suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
127 suite.addTest(new TestSuite(AtomicIntegerTest.class));
128 suite.addTest(new TestSuite(AtomicLongArrayTest.class));
129 suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
130 suite.addTest(new TestSuite(AtomicLongTest.class));
131 suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
132 suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
133 suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
134 suite.addTest(new TestSuite(AtomicReferenceTest.class));
135 suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
136 suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
137 suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
138 suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
139 suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
140 suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
141 suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
142 suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
143 suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
144 suite.addTest(new TestSuite(CountDownLatchTest.class));
145 suite.addTest(new TestSuite(CyclicBarrierTest.class));
146 suite.addTest(new TestSuite(DelayQueueTest.class));
147 suite.addTest(new TestSuite(EntryTest.class));
148 suite.addTest(new TestSuite(ExchangerTest.class));
149 suite.addTest(new TestSuite(ExecutorsTest.class));
150 suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
151 suite.addTest(new TestSuite(FutureTaskTest.class));
152 suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
153 suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
154 suite.addTest(new TestSuite(LinkedListTest.class));
155 suite.addTest(new TestSuite(LockSupportTest.class));
156 suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
157 suite.addTest(new TestSuite(PriorityQueueTest.class));
158 suite.addTest(new TestSuite(ReentrantLockTest.class));
159 suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
160 suite.addTest(new TestSuite(ScheduledExecutorTest.class));
161 suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
162 suite.addTest(new TestSuite(SemaphoreTest.class));
163 suite.addTest(new TestSuite(SynchronousQueueTest.class));
164 suite.addTest(new TestSuite(SystemTest.class));
165 suite.addTest(new TestSuite(ThreadLocalTest.class));
166 suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
167 suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
168 suite.addTest(new TestSuite(ThreadTest.class));
169 suite.addTest(new TestSuite(TimeUnitTest.class));
170 suite.addTest(new TestSuite(TreeMapTest.class));
171 suite.addTest(new TestSuite(TreeSetTest.class));
172 suite.addTest(new TestSuite(TreeSubMapTest.class));
173 suite.addTest(new TestSuite(TreeSubSetTest.class));
174
175 return suite;
176 }
177
178
179 public static long SHORT_DELAY_MS;
180 public static long SMALL_DELAY_MS;
181 public static long MEDIUM_DELAY_MS;
182 public static long LONG_DELAY_MS;
183
184
185 /**
186 * Returns the shortest timed delay. This could
187 * be reimplemented to use for example a Property.
188 */
189 protected long getShortDelay() {
190 return 50;
191 }
192
193
194 /**
195 * Sets delays as multiples of SHORT_DELAY.
196 */
197 protected void setDelays() {
198 SHORT_DELAY_MS = getShortDelay();
199 SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
200 MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
201 LONG_DELAY_MS = SHORT_DELAY_MS * 50;
202 }
203
204 /**
205 * Flag set true if any threadAssert methods fail
206 */
207 volatile boolean threadFailed;
208
209 /**
210 * Initializes test to indicate that no thread assertions have failed
211 */
212 public void setUp() {
213 setDelays();
214 threadFailed = false;
215 }
216
217 /**
218 * Triggers test case failure if any thread assertions have failed
219 */
220 public void tearDown() {
221 assertFalse(threadFailed);
222 }
223
224 /**
225 * Fail, also setting status to indicate current testcase should fail
226 */
227 public void threadFail(String reason) {
228 threadFailed = true;
229 fail(reason);
230 }
231
232 /**
233 * If expression not true, set status to indicate current testcase
234 * should fail
235 */
236 public void threadAssertTrue(boolean b) {
237 if (!b) {
238 threadFailed = true;
239 assertTrue(b);
240 }
241 }
242
243 /**
244 * If expression not false, set status to indicate current testcase
245 * should fail
246 */
247 public void threadAssertFalse(boolean b) {
248 if (b) {
249 threadFailed = true;
250 assertFalse(b);
251 }
252 }
253
254 /**
255 * If argument not null, set status to indicate current testcase
256 * should fail
257 */
258 public void threadAssertNull(Object x) {
259 if (x != null) {
260 threadFailed = true;
261 assertNull(x);
262 }
263 }
264
265 /**
266 * If arguments not equal, set status to indicate current testcase
267 * should fail
268 */
269 public void threadAssertEquals(long x, long y) {
270 if (x != y) {
271 threadFailed = true;
272 assertEquals(x, y);
273 }
274 }
275
276 /**
277 * If arguments not equal, set status to indicate current testcase
278 * should fail
279 */
280 public void threadAssertEquals(Object x, Object y) {
281 if (x != y && (x == null || !x.equals(y))) {
282 threadFailed = true;
283 assertEquals(x, y);
284 }
285 }
286
287 /**
288 * threadFail with message "should throw exception"
289 */
290 public void threadShouldThrow() {
291 try {
292 threadFailed = true;
293 fail("should throw exception");
294 } catch (AssertionFailedError e) {
295 e.printStackTrace();
296 throw e;
297 }
298 }
299
300 /**
301 * threadFail with message "Unexpected exception"
302 */
303 public void threadUnexpectedException() {
304 threadFailed = true;
305 fail("Unexpected exception");
306 }
307
308 /**
309 * threadFail with message "Unexpected exception", with argument
310 */
311 public void threadUnexpectedException(Throwable ex) {
312 threadFailed = true;
313 ex.printStackTrace();
314 fail("Unexpected exception: " + ex);
315 }
316
317 /**
318 * Wait out termination of a thread pool or fail doing so
319 */
320 public void joinPool(ExecutorService exec) {
321 try {
322 exec.shutdown();
323 assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
324 } catch (SecurityException ok) {
325 // Allowed in case test doesn't have privs
326 } catch (InterruptedException ie) {
327 fail("Unexpected exception");
328 }
329 }
330
331
332 /**
333 * fail with message "should throw exception"
334 */
335 public void shouldThrow() {
336 fail("Should throw exception");
337 }
338
339 /**
340 * fail with message "Unexpected exception"
341 */
342 public void unexpectedException() {
343 fail("Unexpected exception");
344 }
345
346
347 /**
348 * The number of elements to place in collections, arrays, etc.
349 */
350 static final int SIZE = 20;
351
352 // Some convenient Integer constants
353
354 static final Integer zero = new Integer(0);
355 static final Integer one = new Integer(1);
356 static final Integer two = new Integer(2);
357 static final Integer three = new Integer(3);
358 static final Integer four = new Integer(4);
359 static final Integer five = new Integer(5);
360 static final Integer six = new Integer(6);
361 static final Integer seven = new Integer(7);
362 static final Integer eight = new Integer(8);
363 static final Integer nine = new Integer(9);
364 static final Integer m1 = new Integer(-1);
365 static final Integer m2 = new Integer(-2);
366 static final Integer m3 = new Integer(-3);
367 static final Integer m4 = new Integer(-4);
368 static final Integer m5 = new Integer(-5);
369 static final Integer m6 = new Integer(-6);
370 static final Integer m10 = new Integer(-10);
371
372
373 /**
374 * A security policy where new permissions can be dynamically added
375 * or all cleared.
376 */
377 static class AdjustablePolicy extends java.security.Policy {
378 Permissions perms = new Permissions();
379 AdjustablePolicy() { }
380 void addPermission(Permission perm) { perms.add(perm); }
381 void clearPermissions() { perms = new Permissions(); }
382 public PermissionCollection getPermissions(CodeSource cs) {
383 return perms;
384 }
385 public PermissionCollection getPermissions(ProtectionDomain pd) {
386 return perms;
387 }
388 public boolean implies(ProtectionDomain pd, Permission p) {
389 return perms.implies(p);
390 }
391 public void refresh() {}
392 }
393
394
395 // Some convenient Runnable classes
396
397 abstract class CheckedRunnable implements Runnable {
398 abstract void realRun() throws Throwable;
399
400 public final void run() {
401 try {
402 realRun();
403 } catch (Throwable t) {
404 threadUnexpectedException(t);
405 }
406 }
407 }
408
409 abstract class CheckedInterruptedRunnable implements Runnable {
410 abstract void realRun() throws Throwable;
411
412 public final void run() {
413 try {
414 realRun();
415 threadShouldThrow();
416 } catch (InterruptedException success) {
417 } catch (Throwable t) {
418 threadUnexpectedException(t);
419 }
420 }
421 }
422
423 abstract class CheckedCallable<T> implements Callable<T> {
424 abstract T realCall() throws Throwable;
425
426 public final T call() {
427 try {
428 return realCall();
429 } catch (Throwable t) {
430 threadUnexpectedException(t);
431 return null;
432 }
433 }
434 }
435
436 static class NoOpRunnable implements Runnable {
437 public void run() {}
438 }
439
440 static class NoOpCallable implements Callable {
441 public Object call() { return Boolean.TRUE; }
442 }
443
444 static final String TEST_STRING = "a test string";
445
446 static class StringTask implements Callable<String> {
447 public String call() { return TEST_STRING; }
448 }
449
450 static class NPETask implements Callable<String> {
451 public String call() { throw new NullPointerException(); }
452 }
453
454 static class CallableOne implements Callable<Integer> {
455 public Integer call() { return one; }
456 }
457
458 class ShortRunnable extends CheckedRunnable {
459 void realRun() throws Throwable {
460 Thread.sleep(SHORT_DELAY_MS);
461 }
462 }
463
464 class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
465 void realRun() throws InterruptedException {
466 Thread.sleep(SHORT_DELAY_MS);
467 }
468 }
469
470 class SmallRunnable extends CheckedRunnable {
471 void realRun() throws Throwable {
472 Thread.sleep(SMALL_DELAY_MS);
473 }
474 }
475
476 class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
477 void realRun() {
478 try {
479 Thread.sleep(SMALL_DELAY_MS);
480 }
481 catch (InterruptedException ok) {
482 }
483 }
484 }
485
486 class SmallCallable extends CheckedCallable {
487 Object realCall() throws Throwable {
488 Thread.sleep(SMALL_DELAY_MS);
489 return Boolean.TRUE;
490 }
491 }
492
493 class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
494 void realRun() throws InterruptedException {
495 Thread.sleep(SMALL_DELAY_MS);
496 }
497 }
498
499 class MediumRunnable extends CheckedRunnable {
500 void realRun() throws Throwable {
501 Thread.sleep(MEDIUM_DELAY_MS);
502 }
503 }
504
505 class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
506 void realRun() throws InterruptedException {
507 Thread.sleep(MEDIUM_DELAY_MS);
508 }
509 }
510
511 class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
512 void realRun() {
513 try {
514 Thread.sleep(MEDIUM_DELAY_MS);
515 }
516 catch (InterruptedException ok) {
517 }
518 }
519 }
520
521 class LongPossiblyInterruptedRunnable extends CheckedRunnable {
522 void realRun() {
523 try {
524 Thread.sleep(LONG_DELAY_MS);
525 }
526 catch (InterruptedException ok) {
527 }
528 }
529 }
530
531 /**
532 * For use as ThreadFactory in constructors
533 */
534 static class SimpleThreadFactory implements ThreadFactory {
535 public Thread newThread(Runnable r) {
536 return new Thread(r);
537 }
538 }
539
540 static class TrackedShortRunnable implements Runnable {
541 volatile boolean done = false;
542 public void run() {
543 try {
544 Thread.sleep(SMALL_DELAY_MS);
545 done = true;
546 } catch (Exception e) {
547 }
548 }
549 }
550
551 static class TrackedMediumRunnable implements Runnable {
552 volatile boolean done = false;
553 public void run() {
554 try {
555 Thread.sleep(MEDIUM_DELAY_MS);
556 done = true;
557 } catch (Exception e) {
558 }
559 }
560 }
561
562 static class TrackedLongRunnable implements Runnable {
563 volatile boolean done = false;
564 public void run() {
565 try {
566 Thread.sleep(LONG_DELAY_MS);
567 done = true;
568 } catch (Exception e) {
569 }
570 }
571 }
572
573 static class TrackedNoOpRunnable implements Runnable {
574 volatile boolean done = false;
575 public void run() {
576 done = true;
577 }
578 }
579
580 static class TrackedCallable implements Callable {
581 volatile boolean done = false;
582 public Object call() {
583 try {
584 Thread.sleep(SMALL_DELAY_MS);
585 done = true;
586 } catch (Exception e) {
587 }
588 return Boolean.TRUE;
589 }
590 }
591
592
593 /**
594 * For use as RejectedExecutionHandler in constructors
595 */
596 static class NoOpREHandler implements RejectedExecutionHandler {
597 public void rejectedExecution(Runnable r,
598 ThreadPoolExecutor executor) {}
599 }
600
601 }