ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
Revision: 1.38
Committed: Wed Aug 5 00:43:59 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +19 -0 lines
Log Message:
more infrastructure

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