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

CountDownLatchTest

public class CountDownLatchTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run (suite());        
    
public static junit.framework.Testsuite()

        return new TestSuite(CountDownLatchTest.class);
    
public voidtestAwait()
await returns after countDown to zero, but not before

        final CountDownLatch l = new CountDownLatch(2);

        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(l.getCount() > 0);
                        l.await();
                        threadAssertTrue(l.getCount() == 0);
                    } catch(InterruptedException e){
                        threadUnexpectedException();
                    }
                }
            });
        t.start();
        try {
            assertEquals(l.getCount(), 2);
            Thread.sleep(SHORT_DELAY_MS);
            l.countDown();
            assertEquals(l.getCount(), 1);
            l.countDown();
            assertEquals(l.getCount(), 0);
            t.join();
        } catch (InterruptedException e){
            unexpectedException();
        }
    
public voidtestAwaitTimeout()
timed await times out if not counted down before timeout

        final CountDownLatch l = new CountDownLatch(1);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(l.getCount() > 0);
                        threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
                        threadAssertTrue(l.getCount() > 0);
                    } catch(InterruptedException ie){
                        threadUnexpectedException();
                    }
                }
            });
        t.start();
        try {
            assertEquals(l.getCount(), 1);
            t.join();
        } catch (InterruptedException e){
            unexpectedException();
        }
    
public voidtestAwait_InterruptedException()
await throws IE if interrupted before counted down

        final CountDownLatch l = new CountDownLatch(1);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(l.getCount() > 0);
                        l.await();
                        threadShouldThrow();
                    } catch(InterruptedException success){}
                }
            });
        t.start();
        try {
            assertEquals(l.getCount(), 1);
            t.interrupt();
            t.join();
        } catch (InterruptedException e){
            unexpectedException();
        }
    
public voidtestConstructor()
negative constructor argument throws IAE

        try {
            new CountDownLatch(-1);
            shouldThrow();
        } catch(IllegalArgumentException success){}
    
public voidtestCountDown()
countDown decrements count when positive and has no effect when zero

        final CountDownLatch l = new CountDownLatch(1);
        assertEquals(1, l.getCount());
        l.countDown();
        assertEquals(0, l.getCount());
        l.countDown();
        assertEquals(0, l.getCount());
    
public voidtestGetCount()
getCount returns initial count and decreases after countDown

        final CountDownLatch l = new CountDownLatch(2);
        assertEquals(2, l.getCount());
        l.countDown();
        assertEquals(1, l.getCount());
    
public voidtestTimedAwait()
timed await returns after countDown to zero

        final CountDownLatch l = new CountDownLatch(2);

        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(l.getCount() > 0);
                        threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
                    } catch(InterruptedException e){
                        threadUnexpectedException();
                    }
                }
            });
        t.start();
        try {
            assertEquals(l.getCount(), 2);
            Thread.sleep(SHORT_DELAY_MS);
            l.countDown();
            assertEquals(l.getCount(), 1);
            l.countDown();
            assertEquals(l.getCount(), 0);
            t.join();
        } catch (InterruptedException e){
            unexpectedException();
        }
    
public voidtestTimedAwait_InterruptedException()
timed await throws IE if interrupted before counted down

        final CountDownLatch l = new CountDownLatch(1);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(l.getCount() > 0);
                        l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
                        threadShouldThrow();                        
                    } catch(InterruptedException success){}
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(l.getCount(), 1);
            t.interrupt();
            t.join();
        } catch (InterruptedException e){
            unexpectedException();
        }
    
public voidtestToString()
toString indicates current count

        CountDownLatch s = new CountDownLatch(2);
        String us = s.toString();
        assertTrue(us.indexOf("Count = 2") >= 0);
        s.countDown();
        String s1 = s.toString();
        assertTrue(s1.indexOf("Count = 1") >= 0);
        s.countDown();
        String s2 = s.toString();
        assertTrue(s2.indexOf("Count = 0") >= 0);