Package org.jgroups.tests

Source Code of org.jgroups.tests.ReentrantLockTest

package org.jgroups.tests;

import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


/**
* Tests the ReentrantLock
* @author Bela Ban
* @version $Id: ReentrantLockTest.java,v 1.2 2006/08/31 14:08:49 belaban Exp $
*/
public class ReentrantLockTest extends TestCase {
    ReentrantLock lock;

    public ReentrantLockTest(String name) {
        super(name);
    }

    public void setUp() throws Exception {
        super.setUp();
        lock=new ReentrantLock();
    }

    public void tearDown() throws Exception {
        releaseAll(lock);
        lock=null;
        super.tearDown();
    }



    public void testAcquireLock() {
        try {
            lock.acquire();
            assertEquals(1, lock.holds());
            lock.acquire();
            assertEquals(2, lock.holds());
            release(lock);
            assertEquals(1, lock.holds());
            release(lock);
            assertEquals(0, lock.holds());
        }
        catch(InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void testAcquireLock2() {
        try {
            lock.acquire();
            assertEquals(1, lock.holds());
            lock.acquire();
            assertEquals(2, lock.holds());
            releaseAll(lock);
            assertEquals(0, lock.holds());
        }
        catch(InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void release(ReentrantLock lock) {
        if(lock != null && lock.holds() > 0)
            lock.release();
    }

    private void releaseAll(ReentrantLock lock) {
        if(lock != null) {
            long holds=lock.holds();
            if(holds > 0)
                lock.release(holds);
        }
    }



    public static Test suite() {
        return new TestSuite(ReentrantLockTest.class);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(ReentrantLockTest.suite());
    }
}
TOP

Related Classes of org.jgroups.tests.ReentrantLockTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.