|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjunit.framework.Assert
junit.framework.TestCase
edu.rice.cs.drjava.DrJavaTestCase
edu.rice.cs.util.ReaderWriterLockTest
public class ReaderWriterLockTest
Attempts to test the correctness of the ReaderWriterLock class, which allows multiple reader and writer threads to safely access a shared resource. (Multiple readers can be active at a time, but only one writer can be active, during which time no readers can be active.) This can be difficult to test because there is little control over how the threads are actually scheduled.
| Nested Class Summary | |
|---|---|
class |
ReaderWriterLockTest.PrintCommand
Command pattern class to print to a buffer. |
class |
ReaderWriterLockTest.PrinterReaderThread
A ReaderThread which repeatedly prints to a buffer. |
class |
ReaderWriterLockTest.PrinterWriterThread
A WriterThread which repeatedly prints to a buffer. |
class |
ReaderWriterLockTest.ReaderThread
A reader thread. |
class |
ReaderWriterLockTest.WriterThread
A writer thread. |
| Nested classes/interfaces inherited from class junit.framework.TestCase |
|---|
junit.framework.TestCase.WrappedException |
| Field Summary | |
|---|---|
protected ReaderWriterLock |
_lock
|
private int |
_notifyCount
Number of notifications expected before we actually notify. |
private Object |
_notifyObject
Object to provide semaphore-like synchronization. |
| Constructor Summary | |
|---|---|
ReaderWriterLockTest()
|
|
| Method Summary | |
|---|---|
private void |
_notify()
Notifies the _notifyObject (semaphore) when the _notifyCount reaches 0. |
void |
setUp()
Creates a new lock for the tests. |
void |
testCannotReadInAWrite()
Ensure that a writing thread cannot perform a read. |
void |
testCannotWriteInARead()
Ensure that a reading thread cannot perform a write. |
void |
testCannotWriteInAWrite()
Ensure that a writing thread cannot perform an additional write. |
void |
testMultipleReaders()
Tests that multiple readers can run without causing deadlock. |
void |
testMultipleReadersAndWriters()
We would like to test the following schedule. |
void |
testMultipleWriters()
Tests that multiple writers run in mutually exclusive intervals without causing deadlock. |
void |
testReaderMultipleReads()
Ensure that a single thread can perform multiple reads. |
| Methods inherited from class edu.rice.cs.drjava.DrJavaTestCase |
|---|
setConfigSetting, setDocText, tearDown |
| Methods inherited from class junit.framework.TestCase |
|---|
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString |
| Methods inherited from class junit.framework.Assert |
|---|
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected volatile ReaderWriterLock _lock
private volatile int _notifyCount
private final Object _notifyObject
| Constructor Detail |
|---|
public ReaderWriterLockTest()
| Method Detail |
|---|
public void setUp()
throws Exception
setUp in class DrJavaTestCaseException - This convention is mandated by JUnit.TestCase, the superclass of this class.private void _notify()
public void testMultipleReaders()
throws InterruptedException
InterruptedException
public void testMultipleWriters()
throws InterruptedException
InterruptedException
public void testReaderMultipleReads()
throws InterruptedException
InterruptedExceptionpublic void testCannotWriteInARead()
public void testCannotWriteInAWrite()
public void testCannotReadInAWrite()
public void testMultipleReadersAndWriters()
throws InterruptedException
W1 |***********| W2 |..........*****| R1 |..............********| R2 |............****| W3 |...................***| R3 |.....................****| R4 |................*******| R5 |***|Key: "." means waiting, "*" means running This is next to impossible to set up in Java. What we'd really like is a unit-testing framework that allows us to easily specify such a schedule in a test. (Conveniently, Corky Cartwright has applied for a Texas ATP grant to develop just such a framework.) So, instead, we'll just set up these threads, let them run, and enforce that no one interferes with output from a writer.
InterruptedException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||