edu.rice.cs.util
Class ReaderWriterLockTest

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by edu.rice.cs.drjava.DrJavaTestCase
              extended by edu.rice.cs.util.ReaderWriterLockTest
All Implemented Interfaces:
junit.framework.Test

public class ReaderWriterLockTest
extends DrJavaTestCase

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.

Version:
$Id: ReaderWriterLockTest.java 5680 2012-08-17 16:13:35Z rcartwright $

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

_lock

protected volatile ReaderWriterLock _lock

_notifyCount

private volatile int _notifyCount
Number of notifications expected before we actually notify.


_notifyObject

private final Object _notifyObject
Object to provide semaphore-like synchronization.

Constructor Detail

ReaderWriterLockTest

public ReaderWriterLockTest()
Method Detail

setUp

public void setUp()
           throws Exception
Creates a new lock for the tests.

Overrides:
setUp in class DrJavaTestCase
Throws:
Exception - This convention is mandated by JUnit.TestCase, the superclass of this class.

_notify

private void _notify()
Notifies the _notifyObject (semaphore) when the _notifyCount reaches 0. (Decrements the count on each call.)


testMultipleReaders

public void testMultipleReaders()
                         throws InterruptedException
Tests that multiple readers can run without causing deadlock. We can't really impose any ordering on their output.

Throws:
InterruptedException

testMultipleWriters

public void testMultipleWriters()
                         throws InterruptedException
Tests that multiple writers run in mutually exclusive intervals without causing deadlock.

Throws:
InterruptedException

testReaderMultipleReads

public void testReaderMultipleReads()
                             throws InterruptedException
Ensure that a single thread can perform multiple reads.

Throws:
InterruptedException

testCannotWriteInARead

public void testCannotWriteInARead()
Ensure that a reading thread cannot perform a write.


testCannotWriteInAWrite

public void testCannotWriteInAWrite()
Ensure that a writing thread cannot perform an additional write.


testCannotReadInAWrite

public void testCannotReadInAWrite()
Ensure that a writing thread cannot perform a read.


testMultipleReadersAndWriters

public void testMultipleReadersAndWriters()
                                   throws InterruptedException
We would like to test the following schedule.
 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.

Throws:
InterruptedException