FileDocCategorySizeDatePackage
JSR166TestCase.javaAPI DocAndroid 1.5 API17342Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

JSR166TestCase

public class JSR166TestCase extends TestCase
Base class for JSR166 Junit TCK tests. Defines some constants, utility methods and classes, as well as a simple framework for helping to make sure that assertions failing in generated threads cause the associated test that generated them to itself fail (which JUnit doe not otherwise arrange). The rules for creating such tests are:
  1. All assertions in code running in generated threads must use the forms {@link #threadFail} , {@link #threadAssertTrue} {@link #threadAssertEquals}, or {@link #threadAssertNull}, (not fail, assertTrue, etc.) It is OK (but not particularly recommended) for other code to use these forms too. Only the most typically used JUnit assertion methods are defined this way, but enough to live with.
  2. If you override {@link #setUp} or {@link #tearDown}, make sure to invoke super.setUp and super.tearDown within them. These methods are used to clear and check for thread assertion failures.
  3. All delays and timeouts must use one of the constants SHORT_DELAY_MS, SMALL_DELAY_MS, MEDIUM_DELAY_MS, LONG_DELAY_MS. The idea here is that a SHORT is always discriminable from zero time, and always allows enough time for the small amounts of computation (creating a thread, calling a few methods, etc) needed to reach a timeout point. Similarly, a SMALL is always discriminable as larger than SHORT and smaller than MEDIUM. And so on. These constants are set to conservative values, but even so, if there is ever any doubt, they can all be increased in one spot to rerun tests on slower platforms
  4. All threads generated must be joined inside each test case method (or fail to do so) before returning from the method. The joinPool method can be used to do this when using Executors.

Other notes

  • Usually, there is one testcase method per JSR166 method covering "normal" operation, and then as many exception-testing methods as there are exceptions the method can throw. Sometimes there are multiple tests per JSR166 method when the different "normal" behaviors differ significantly. And sometimes testcases cover multiple methods when they cannot be tested in isolation.
  • The documentation style for testcases is to provide as javadoc a simple sentence or two describing the property that the testcase method purports to test. The javadocs do not say anything about how the property is tested. To find out, read the code.
  • These tests are "conformance tests", and do not attempt to test throughput, latency, scalability or other performance factors (see the separate "jtreg" tests for a set intended to check these for the most central aspects of functionality.) So, most tests use the smallest sensible numbers of threads, collection sizes, etc needed to check basic conformance.
  • The test classes currently do not declare inclusion in any particular package to simplify things for people integrating them in TCK test suites.
  • As a convenience, the main of this class (JSR166TestCase) runs all JSR166 unit tests.

Fields Summary
public static long
SHORT_DELAY_MS
public static long
SMALL_DELAY_MS
public static long
MEDIUM_DELAY_MS
public static long
LONG_DELAY_MS
volatile boolean
threadFailed
Flag set true if any threadAssert methods fail
static final int
SIZE
The number of elements to place in collections, arrays, etc.
static final Integer
zero
static final Integer
one
static final Integer
two
static final Integer
three
static final Integer
four
static final Integer
five
static final Integer
six
static final Integer
seven
static final Integer
eight
static final Integer
nine
static final Integer
m1
static final Integer
m2
static final Integer
m3
static final Integer
m4
static final Integer
m5
static final Integer
m10
static final String
TEST_STRING
Constructors Summary
Methods Summary
protected longgetShortDelay()
Return the shortest timed delay. This could be reimplemented to use for example a Property.

        return 50;
    
public voidjoinPool(java.util.concurrent.ExecutorService exec)
Wait out termination of a thread pool or fail doing so

        try {
            exec.shutdown();
            assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
        } catch(SecurityException ok) {
            // Allowed in case test doesn't have privs
        } catch(InterruptedException ie) {
            fail("Unexpected exception");
        }
    
public static voidmain(java.lang.String[] args)
Runs all JSR166 unit tests using junit.textui.TestRunner

        int iters = 1;
        if (args.length > 0) 
            iters = Integer.parseInt(args[0]);
        Test s = suite();
        for (int i = 0; i < iters; ++i) {
            junit.textui.TestRunner.run (s);
            System.gc();
            System.runFinalization();
        }
        System.exit(0);
    
protected voidsetDelays()
Set delays as multiples of SHORT_DELAY.

        SHORT_DELAY_MS = getShortDelay();
        SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
        MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
        LONG_DELAY_MS = SHORT_DELAY_MS * 50;
    
public voidsetUp()
Initialize test to indicate that no thread assertions have failed

 
        setDelays();
        threadFailed = false;  
    
public voidshouldThrow()
fail with message "should throw exception"

        fail("Should throw exception");
    
public static junit.framework.Testsuite()
Collects all JSR166 unit tests as one suite

        TestSuite suite = tests.TestSuiteFactory.createTestSuite("JSR166 Unit Tests");
        // BEGIN android-changed
        suite.addTest(AbstractExecutorServiceTest.suite());
        suite.addTest(AbstractQueueTest.suite());
        suite.addTest(AbstractQueuedSynchronizerTest.suite());
        suite.addTest(ArrayBlockingQueueTest.suite());
        suite.addTest(AtomicBooleanTest.suite()); 
        suite.addTest(AtomicIntegerArrayTest.suite()); 
        suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); 
        suite.addTest(AtomicIntegerTest.suite()); 
        suite.addTest(AtomicLongArrayTest.suite()); 
        suite.addTest(AtomicLongFieldUpdaterTest.suite()); 
        suite.addTest(AtomicLongTest.suite()); 
        suite.addTest(AtomicMarkableReferenceTest.suite()); 
        suite.addTest(AtomicReferenceArrayTest.suite()); 
        suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); 
        suite.addTest(AtomicReferenceTest.suite()); 
        suite.addTest(AtomicStampedReferenceTest.suite()); 
        suite.addTest(ConcurrentHashMapTest.suite());
        suite.addTest(ConcurrentLinkedQueueTest.suite());
        suite.addTest(CopyOnWriteArrayListTest.suite());
        suite.addTest(CopyOnWriteArraySetTest.suite());
        suite.addTest(CountDownLatchTest.suite());
        suite.addTest(CyclicBarrierTest.suite());
        suite.addTest(DelayQueueTest.suite());
        suite.addTest(ExchangerTest.suite());
        suite.addTest(ExecutorsTest.suite());
        suite.addTest(ExecutorCompletionServiceTest.suite());
        suite.addTest(FutureTaskTest.suite());
        suite.addTest(LinkedBlockingQueueTest.suite());
        suite.addTest(LinkedListTest.suite());
        suite.addTest(LockSupportTest.suite());
        suite.addTest(PriorityBlockingQueueTest.suite());
        suite.addTest(PriorityQueueTest.suite());
        suite.addTest(ReentrantLockTest.suite());
        suite.addTest(ReentrantReadWriteLockTest.suite());
        suite.addTest(ScheduledExecutorTest.suite());
        suite.addTest(SemaphoreTest.suite());
        suite.addTest(SynchronousQueueTest.suite());
        suite.addTest(SystemTest.suite());
        suite.addTest(ThreadLocalTest.suite());
        suite.addTest(ThreadPoolExecutorTest.suite());
        suite.addTest(ThreadTest.suite());
        suite.addTest(TimeUnitTest.suite());
        // END android-changed
        return suite;
    
public voidtearDown()
Trigger test case failure if any thread assertions have failed

 
        assertFalse(threadFailed);  
    
public voidthreadAssertEquals(long x, long y)
If arguments not equal, set status to indicate current testcase should fail

        if (x != y) {
            threadFailed = true;
            assertEquals(x, y);
        }
    
public voidthreadAssertEquals(java.lang.Object x, java.lang.Object y)
If arguments not equal, set status to indicate current testcase should fail

        if (x != y && (x == null || !x.equals(y))) {
            threadFailed = true;
            assertEquals(x, y);
        }
    
public voidthreadAssertFalse(boolean b)
If expression not false, set status to indicate current testcase should fail

        if (b) {
            threadFailed = true;
            assertFalse(b);
        }
    
public voidthreadAssertNull(java.lang.Object x)
If argument not null, set status to indicate current testcase should fail

        if (x != null) {
            threadFailed = true;
            assertNull(x);
        }
    
public voidthreadAssertTrue(boolean b)
If expression not true, set status to indicate current testcase should fail

        if (!b) {
            threadFailed = true;
            assertTrue(b);
        }
    
public voidthreadFail(java.lang.String reason)
Fail, also setting status to indicate current testcase should fail

        threadFailed = true;
        fail(reason);
    
public voidthreadShouldThrow()
threadFail with message "should throw exception"

        threadFailed = true;
        fail("should throw exception");
    
public voidthreadUnexpectedException()
threadFail with message "Unexpected exception"

        threadFailed = true;
        fail("Unexpected exception");
    
public voidunexpectedException()
fail with message "Unexpected exception"

        fail("Unexpected exception");