Package thread.reentrancy.conditions

Source Code of thread.reentrancy.conditions.TestBoundedBuffer

package thread.reentrancy.conditions;

import junit.framework.TestCase;
import org.junit.Test;
import thread.reentrancy.conditions.BoundedBuffer;

/**
* TestBoundedBuffer
* <p/>
* Basic unit tests for BoundedBuffer
*
* @author Brian Goetz and Tim Peierls
*/
public class TestBoundedBuffer extends TestCase {
    private static final long LOCKUP_DETECT_TIMEOUT = 1000;

    @Test
    public void testTakeBlocksWhenEmpty() {
        final   BoundedBuffer bb = new BoundedBuffer();
        Thread taker = new Thread() {
            public void run() {
                try {
                    Object unused = bb.take();
                    fail(); // if we get here, it's an error
                } catch (InterruptedException success) {
                }
            }
        };
        try {
             bb.put(1);
            taker.start();
            Thread.sleep(LOCKUP_DETECT_TIMEOUT);
            taker.interrupt();
            // The timed join ensures that the test completes even if
            // take gets stuck in some unexpected way.
            taker.join(LOCKUP_DETECT_TIMEOUT);
            assertFalse(taker.isAlive());
        } catch (Exception unexpected) {
            fail();
        }
    }


}
TOP

Related Classes of thread.reentrancy.conditions.TestBoundedBuffer

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.