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.
9 package tests.api.java.util.concurrent; // android-added
11 import junit.framework.*;
13 import java.util.concurrent.*;
14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 import java.security.*;
19 * Base class for JSR166 Junit TCK tests. Defines some constants,
20 * utility methods and classes, as well as a simple framework for
21 * helping to make sure that assertions failing in generated threads
22 * cause the associated test that generated them to itself fail (which
23 * JUnit does not otherwise arrange). The rules for creating such
28 * <li> All assertions in code running in generated threads must use
29 * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
30 * #threadAssertEquals}, or {@link #threadAssertNull}, (not
31 * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
32 * particularly recommended) for other code to use these forms too.
33 * Only the most typically used JUnit assertion methods are defined
34 * this way, but enough to live with.</li>
36 * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
37 * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
38 * them. These methods are used to clear and check for thread
39 * assertion failures.</li>
41 * <li>All delays and timeouts must use one of the constants <tt>
42 * SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>,
43 * <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always
44 * discriminable from zero time, and always allows enough time for the
45 * small amounts of computation (creating a thread, calling a few
46 * methods, etc) needed to reach a timeout point. Similarly, a SMALL
47 * is always discriminable as larger than SHORT and smaller than
48 * MEDIUM. And so on. These constants are set to conservative values,
49 * but even so, if there is ever any doubt, they can all be increased
50 * in one spot to rerun tests on slower platforms.</li>
52 * <li> All threads generated must be joined inside each test case
53 * method (or <tt>fail</tt> to do so) before returning from the
54 * method. The <tt> joinPool</tt> method can be used to do this when
55 * using Executors.</li>
59 * <p> <b>Other notes</b>
62 * <li> Usually, there is one testcase method per JSR166 method
63 * covering "normal" operation, and then as many exception-testing
64 * methods as there are exceptions the method can throw. Sometimes
65 * there are multiple tests per JSR166 method when the different
66 * "normal" behaviors differ significantly. And sometimes testcases
67 * cover multiple methods when they cannot be tested in
70 * <li> The documentation style for testcases is to provide as javadoc
71 * a simple sentence or two describing the property that the testcase
72 * method purports to test. The javadocs do not say anything about how
73 * the property is tested. To find out, read the code.</li>
75 * <li> These tests are "conformance tests", and do not attempt to
76 * test throughput, latency, scalability or other performance factors
77 * (see the separate "jtreg" tests for a set intended to check these
78 * for the most central aspects of functionality.) So, most tests use
79 * the smallest sensible numbers of threads, collection sizes, etc
80 * needed to check basic conformance.</li>
82 * <li>The test classes currently do not declare inclusion in
83 * any particular package to simplify things for people integrating
84 * them in TCK test suites.</li>
86 * <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase)
87 * runs all JSR166 unit tests.</li>
91 public class JSR166TestCase extends TestCase {
92 private static final boolean useSecurityManager =
93 Boolean.getBoolean("jsr166.useSecurityManager");
95 // BEGIN android-removed
97 // * Runs all JSR166 unit tests using junit.textui.TestRunner
99 // public static void main(String[] args) {
100 // if (useSecurityManager) {
101 // System.err.println("Setting a permissive security manager");
102 // Policy.setPolicy(permissivePolicy());
103 // System.setSecurityManager(new SecurityManager());
106 // if (args.length > 0)
107 // iters = Integer.parseInt(args[0]);
109 // for (int i = 0; i < iters; ++i) {
110 // junit.textui.TestRunner.run(s);
112 // System.runFinalization();
116 // END android-removed
119 * Collects all JSR166 unit tests as one suite
121 public static Test suite() {
122 TestSuite suite = new TestSuite("JSR166 Unit Tests");
124 suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
125 suite.addTest(new TestSuite(AbstractQueueTest.class));
126 suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
127 suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
128 suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
129 suite.addTest(new TestSuite(ArrayDequeTest.class));
130 suite.addTest(new TestSuite(AtomicBooleanTest.class));
131 suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
132 suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
133 suite.addTest(new TestSuite(AtomicIntegerTest.class));
134 suite.addTest(new TestSuite(AtomicLongArrayTest.class));
135 suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
136 suite.addTest(new TestSuite(AtomicLongTest.class));
137 suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
138 suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
139 suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
140 suite.addTest(new TestSuite(AtomicReferenceTest.class));
141 suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
142 suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
143 suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
144 suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
145 suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
146 suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
147 suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
148 suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
149 suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
150 suite.addTest(new TestSuite(CountDownLatchTest.class));
151 suite.addTest(new TestSuite(CyclicBarrierTest.class));
152 suite.addTest(new TestSuite(DelayQueueTest.class));
153 suite.addTest(new TestSuite(EntryTest.class));
154 suite.addTest(new TestSuite(ExchangerTest.class));
155 suite.addTest(new TestSuite(ExecutorsTest.class));
156 suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
157 suite.addTest(new TestSuite(FutureTaskTest.class));
158 suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
159 suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
160 suite.addTest(new TestSuite(LinkedListTest.class));
161 suite.addTest(new TestSuite(LockSupportTest.class));
162 suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
163 suite.addTest(new TestSuite(PriorityQueueTest.class));
164 suite.addTest(new TestSuite(ReentrantLockTest.class));
165 suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
166 suite.addTest(new TestSuite(ScheduledExecutorTest.class));
167 suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
168 suite.addTest(new TestSuite(SemaphoreTest.class));
169 suite.addTest(new TestSuite(SynchronousQueueTest.class));
170 suite.addTest(new TestSuite(SystemTest.class));
171 suite.addTest(new TestSuite(ThreadLocalTest.class));
172 suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
173 suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
174 suite.addTest(new TestSuite(ThreadTest.class));
175 suite.addTest(new TestSuite(TimeUnitTest.class));
181 public static long SHORT_DELAY_MS;
182 public static long SMALL_DELAY_MS;
183 public static long MEDIUM_DELAY_MS;
184 public static long LONG_DELAY_MS;
188 * Returns the shortest timed delay. This could
189 * be reimplemented to use for example a Property.
191 protected long getShortDelay() {
192 // BEGIN android-changed
193 // original value is 50
195 // END android-changed
200 * Sets delays as multiples of SHORT_DELAY.
202 protected void setDelays() {
203 SHORT_DELAY_MS = getShortDelay();
204 SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
205 MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
206 LONG_DELAY_MS = SHORT_DELAY_MS * 50;
210 * Flag set true if any threadAssert methods fail
212 volatile boolean threadFailed;
215 * Initializes test to indicate that no thread assertions have failed
217 public void setUp() {
219 threadFailed = false;
223 * Triggers test case failure if any thread assertions have failed
225 public void tearDown() {
226 assertFalse(threadFailed);
230 * Fail, also setting status to indicate current testcase should fail
232 public void threadFail(String reason) {
238 * If expression not true, set status to indicate current testcase
241 public void threadAssertTrue(boolean b) {
249 * If expression not false, set status to indicate current testcase
252 public void threadAssertFalse(boolean b) {
260 * If argument not null, set status to indicate current testcase
263 public void threadAssertNull(Object x) {
271 * If arguments not equal, set status to indicate current testcase
274 public void threadAssertEquals(long x, long y) {
282 * If arguments not equal, set status to indicate current testcase
285 public void threadAssertEquals(Object x, Object y) {
286 if (x != y && (x == null || !x.equals(y))) {
293 * threadFail with message "should throw exception"
295 public void threadShouldThrow() {
297 fail("should throw exception");
301 * threadFail with message "should throw" + exceptionName
303 public void threadShouldThrow(String exceptionName) {
305 fail("should throw " + exceptionName);
309 * threadFail with message "Unexpected exception"
311 public void threadUnexpectedException() {
313 fail("Unexpected exception");
317 * threadFail with message "Unexpected exception", with argument
319 public void threadUnexpectedException(Throwable ex) {
321 ex.printStackTrace();
322 fail("Unexpected exception: " + ex);
326 * Wait out termination of a thread pool or fail doing so
328 public void joinPool(ExecutorService exec) {
331 assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
332 } catch (SecurityException ok) {
333 // Allowed in case test doesn't have privs
334 } catch (InterruptedException ie) {
335 fail("Unexpected InterruptedException");
341 * fail with message "should throw exception"
343 public void shouldThrow() {
344 fail("Should throw exception");
348 * fail with message "should throw " + exceptionName
350 public void shouldThrow(String exceptionName) {
351 fail("Should throw " + exceptionName);
355 * fail with message "Unexpected exception"
357 public void unexpectedException() {
358 fail("Unexpected exception");
362 * fail with message "Unexpected exception", with argument
364 public void unexpectedException(Throwable ex) {
365 ex.printStackTrace();
366 fail("Unexpected exception: " + ex);
371 * The number of elements to place in collections, arrays, etc.
373 public static final int SIZE = 20;
375 // Some convenient Integer constants
377 public static final Integer zero = new Integer(0);
378 public static final Integer one = new Integer(1);
379 public static final Integer two = new Integer(2);
380 public static final Integer three = new Integer(3);
381 public static final Integer four = new Integer(4);
382 public static final Integer five = new Integer(5);
383 public static final Integer six = new Integer(6);
384 public static final Integer seven = new Integer(7);
385 public static final Integer eight = new Integer(8);
386 public static final Integer nine = new Integer(9);
387 public static final Integer m1 = new Integer(-1);
388 public static final Integer m2 = new Integer(-2);
389 public static final Integer m3 = new Integer(-3);
390 public static final Integer m4 = new Integer(-4);
391 public static final Integer m5 = new Integer(-5);
392 public static final Integer m6 = new Integer(-6);
393 public static final Integer m10 = new Integer(-10);
397 * Runs Runnable r with a security policy that permits precisely
398 * the specified permissions. If there is no current security
399 * manager, the runnable is run twice, both with and without a
400 * security manager. We require that any security manager permit
401 * getPolicy/setPolicy.
403 public void runWithPermissions(Runnable r, Permission... permissions) {
404 SecurityManager sm = System.getSecurityManager();
407 Policy savedPolicy = Policy.getPolicy();
409 Policy.setPolicy(permissivePolicy());
410 System.setSecurityManager(new SecurityManager());
411 runWithPermissions(r, permissions);
413 System.setSecurityManager(null);
414 Policy.setPolicy(savedPolicy);
417 Policy savedPolicy = Policy.getPolicy();
418 AdjustablePolicy policy = new AdjustablePolicy(permissions);
419 Policy.setPolicy(policy);
424 policy.addPermission(new SecurityPermission("setPolicy"));
425 Policy.setPolicy(savedPolicy);
431 * Runs a runnable without any permissions.
433 public void runWithoutPermissions(Runnable r) {
434 runWithPermissions(r);
438 * A security policy where new permissions can be dynamically added
441 public static class AdjustablePolicy extends java.security.Policy {
442 Permissions perms = new Permissions();
443 AdjustablePolicy(Permission... permissions) {
444 for (Permission permission : permissions)
445 perms.add(permission);
447 void addPermission(Permission perm) { perms.add(perm); }
448 void clearPermissions() { perms = new Permissions(); }
449 public PermissionCollection getPermissions(CodeSource cs) {
452 public PermissionCollection getPermissions(ProtectionDomain pd) {
455 public boolean implies(ProtectionDomain pd, Permission p) {
456 return perms.implies(p);
458 public void refresh() {}
462 * Returns a policy containing all the permissions we ever need.
464 public static Policy permissivePolicy() {
465 return new AdjustablePolicy
466 // Permissions j.u.c. needs directly
467 (new RuntimePermission("modifyThread"),
468 new RuntimePermission("getClassLoader"),
469 new RuntimePermission("setContextClassLoader"),
470 // Permissions needed to change permissions!
471 new SecurityPermission("getPolicy"),
472 new SecurityPermission("setPolicy"),
473 new RuntimePermission("setSecurityManager"),
474 // Permissions needed by the junit test harness
475 new RuntimePermission("accessDeclaredMembers"),
476 new PropertyPermission("*", "read"),
477 new java.io.FilePermission("<<ALL FILES>>", "read"));
481 * Sleep until the timeout has elapsed, or interrupted.
482 * Does <em>NOT</em> throw InterruptedException.
484 void sleepTillInterrupted(long timeoutMillis) {
486 Thread.sleep(timeoutMillis);
487 } catch (InterruptedException wakeup) {}
491 * Returns a new started Thread running the given runnable.
493 Thread newStartedThread(Runnable runnable) {
494 Thread t = new Thread(runnable);
499 // Some convenient Runnable classes
501 public abstract class CheckedRunnable implements Runnable {
502 protected abstract void realRun() throws Throwable;
504 public final void run() {
507 } catch (Throwable t) {
508 threadUnexpectedException(t);
513 public abstract class RunnableShouldThrow implements Runnable {
514 protected abstract void realRun() throws Throwable;
516 final Class<?> exceptionClass;
518 <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
519 this.exceptionClass = exceptionClass;
522 public final void run() {
525 threadShouldThrow(exceptionClass.getSimpleName());
526 } catch (Throwable t) {
527 if (! exceptionClass.isInstance(t))
528 threadUnexpectedException(t);
533 public abstract class ThreadShouldThrow extends Thread {
534 protected abstract void realRun() throws Throwable;
536 final Class<?> exceptionClass;
538 <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
539 this.exceptionClass = exceptionClass;
542 public final void run() {
545 threadShouldThrow(exceptionClass.getSimpleName());
546 } catch (Throwable t) {
547 if (! exceptionClass.isInstance(t))
548 threadUnexpectedException(t);
553 public abstract class CheckedInterruptedRunnable implements Runnable {
554 protected abstract void realRun() throws Throwable;
556 public final void run() {
559 threadShouldThrow("InterruptedException");
560 } catch (InterruptedException success) {
561 } catch (Throwable t) {
562 threadUnexpectedException(t);
567 public abstract class CheckedCallable<T> implements Callable<T> {
568 protected abstract T realCall() throws Throwable;
570 public final T call() {
573 } catch (Throwable t) {
574 threadUnexpectedException(t);
580 public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
581 protected abstract T realCall() throws Throwable;
583 public final T call() {
585 T result = realCall();
586 threadShouldThrow("InterruptedException");
588 } catch (InterruptedException success) {
589 } catch (Throwable t) {
590 threadUnexpectedException(t);
596 public static class NoOpRunnable implements Runnable {
600 public static class NoOpCallable implements Callable {
601 public Object call() { return Boolean.TRUE; }
604 public static final String TEST_STRING = "a test string";
606 public static class StringTask implements Callable<String> {
607 public String call() { return TEST_STRING; }
610 public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
611 return new CheckedCallable<String>() {
612 public String realCall() {
615 } catch (InterruptedException quittingTime) {}
620 public static class NPETask implements Callable<String> {
621 public String call() { throw new NullPointerException(); }
624 public static class CallableOne implements Callable<Integer> {
625 public Integer call() { return one; }
628 public class ShortRunnable extends CheckedRunnable {
629 protected void realRun() throws Throwable {
630 Thread.sleep(SHORT_DELAY_MS);
634 public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
635 protected void realRun() throws InterruptedException {
636 Thread.sleep(SHORT_DELAY_MS);
640 public class SmallRunnable extends CheckedRunnable {
641 protected void realRun() throws Throwable {
642 Thread.sleep(SMALL_DELAY_MS);
646 public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
647 protected void realRun() {
649 Thread.sleep(SMALL_DELAY_MS);
650 } catch (InterruptedException ok) {}
654 public class SmallCallable extends CheckedCallable {
655 protected Object realCall() throws InterruptedException {
656 Thread.sleep(SMALL_DELAY_MS);
661 public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
662 protected void realRun() throws InterruptedException {
663 Thread.sleep(SMALL_DELAY_MS);
667 public class MediumRunnable extends CheckedRunnable {
668 protected void realRun() throws Throwable {
669 Thread.sleep(MEDIUM_DELAY_MS);
673 public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
674 protected void realRun() throws InterruptedException {
675 Thread.sleep(MEDIUM_DELAY_MS);
679 public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
680 protected void realRun() {
682 Thread.sleep(MEDIUM_DELAY_MS);
683 } catch (InterruptedException ok) {}
687 public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
688 protected void realRun() {
690 Thread.sleep(LONG_DELAY_MS);
691 } catch (InterruptedException ok) {}
696 * For use as ThreadFactory in constructors
698 public static class SimpleThreadFactory implements ThreadFactory {
699 public Thread newThread(Runnable r) {
700 return new Thread(r);
704 public static class TrackedShortRunnable implements Runnable {
705 public volatile boolean done = false;
708 Thread.sleep(SMALL_DELAY_MS);
710 } catch (InterruptedException ok) {}
714 public static class TrackedMediumRunnable implements Runnable {
715 public volatile boolean done = false;
718 Thread.sleep(MEDIUM_DELAY_MS);
720 } catch (InterruptedException ok) {}
724 public static class TrackedLongRunnable implements Runnable {
725 public volatile boolean done = false;
728 Thread.sleep(LONG_DELAY_MS);
730 } catch (InterruptedException ok) {}
734 public static class TrackedNoOpRunnable implements Runnable {
735 public volatile boolean done = false;
741 public static class TrackedCallable implements Callable {
742 public volatile boolean done = false;
743 public Object call() {
745 Thread.sleep(SMALL_DELAY_MS);
747 } catch (InterruptedException ok) {}
754 * For use as RejectedExecutionHandler in constructors
756 public static class NoOpREHandler implements RejectedExecutionHandler {
757 public void rejectedExecution(Runnable r,
758 ThreadPoolExecutor executor) {}