X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Ftest%2Forg%2Fwamblee%2Fconcurrency%2FReadWriteLockTest.java;h=2c159e9437639c4c563b12d0b0010003ffedfe81;hb=8f2d78e446f48a1ed156b252998ae17cd6f0ba2b;hp=84afd1c59658ee0bf4d9ff5c321f170fd89ebe42;hpb=caa126385642ffc57478e928ab871bc09c53e993;p=utils diff --git a/support/test/org/wamblee/concurrency/ReadWriteLockTest.java b/support/test/org/wamblee/concurrency/ReadWriteLockTest.java index 84afd1c5..2c159e94 100644 --- a/support/test/org/wamblee/concurrency/ReadWriteLockTest.java +++ b/support/test/org/wamblee/concurrency/ReadWriteLockTest.java @@ -26,9 +26,21 @@ import junit.framework.TestCase; * @see ReadWriteLock */ public class ReadWriteLockTest extends TestCase { + /** + * + */ + private static final int HALF_SECOND = 500; + /** + * + */ + private static final int ONE_SECOND = 1000; + /** + * + */ + private static final int TWO_SECONDS = 2000; private ReadWriteLock _lock; - int _nReaders; - int _nWriters; + private int _nReaders; + private int _nWriters; /** * Constructor for ReadWriteLockTest. @@ -101,14 +113,14 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testMultipleReaders() throws InterruptedException { - Runnable runnable = new ReadLocker(_lock, this, 2000); + Runnable runnable = new ReadLocker(_lock, this, TWO_SECONDS); Thread t1 = new Thread(runnable); t1.start(); Thread t2 = new Thread(runnable); t2.start(); - Thread.sleep(1000); + Thread.sleep(ONE_SECOND); assertTrue("Not enough readers!", getReaderCount() == 2); t1.join(); t2.join(); @@ -120,13 +132,13 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testSingleWriter() throws InterruptedException { - WriteLocker writer = new WriteLocker(_lock, this, 1000); + WriteLocker writer = new WriteLocker(_lock, this, ONE_SECOND); Thread t1 = new Thread(writer); Thread t2 = new Thread(writer); t1.start(); t2.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue("Wrong writer count: " + getWriterCount(), getWriterCount() == 1); t1.join(); @@ -139,20 +151,20 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testMultipleWriters() throws InterruptedException { - WriteLocker writer1 = new WriteLocker(_lock, this, 1500); - WriteLocker writer2 = new WriteLocker(_lock, this, 1000); + WriteLocker writer1 = new WriteLocker(_lock, this, HALF_SECOND + ONE_SECOND); + WriteLocker writer2 = new WriteLocker(_lock, this, ONE_SECOND); Thread t1 = new Thread(writer1); Thread t2 = new Thread(writer2); t1.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue(getWriterCount() == 1); t2.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue(getWriterCount() == 1); // first writer still // has the lock. - Thread.sleep(1000); + Thread.sleep(ONE_SECOND); // at t = 2, the second writer still must have // a lock. @@ -168,22 +180,22 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite1() throws InterruptedException { - ReadLocker readLocker = new ReadLocker(_lock, this, 2000); + ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS); Thread t1 = new Thread(readLocker); - WriteLocker writeLocker = new WriteLocker(_lock, this, 2000); + WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS); Thread t2 = new Thread(writeLocker); t1.start(); // acquire read lock - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue(getReaderCount() == 1); t2.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); // 1 second underway, reader still holding the // lock so write lock cannot be acquired. assertTrue(getReaderCount() == 1); assertTrue(getWriterCount() == 0); - Thread.sleep(1500); + Thread.sleep(ONE_SECOND + HALF_SECOND); // 2.5 seconds underway, read lock released and // write lock must be acquired. @@ -201,32 +213,32 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite2() throws InterruptedException { - ReadLocker readLocker1 = new ReadLocker(_lock, this, 2500); - ReadLocker readLocker2 = new ReadLocker(_lock, this, 2500); + ReadLocker readLocker1 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND); + ReadLocker readLocker2 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND); Thread t1 = new Thread(readLocker1); Thread t2 = new Thread(readLocker2); - WriteLocker writeLocker = new WriteLocker(_lock, this, 2000); + WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS); Thread t3 = new Thread(writeLocker); t1.start(); // acquire read lock - Thread.sleep(1000); + Thread.sleep(ONE_SECOND); assertTrue(getReaderCount() == 1); t2.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue(getReaderCount() == 2); t3.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); // 2 seconds, assertTrue(getReaderCount() == 2); assertTrue(getWriterCount() == 0); - Thread.sleep(1000); + Thread.sleep(ONE_SECOND); // 3 seconds underway, first read lock must // have been released. assertTrue(getReaderCount() == 1); assertTrue(getWriterCount() == 0); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); // 4 seconds underway, write lock must have // been acquired. @@ -245,22 +257,22 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite3() throws InterruptedException { - ReadLocker readLocker = new ReadLocker(_lock, this, 2000); + ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS); Thread t1 = new Thread(readLocker); - WriteLocker writeLocker = new WriteLocker(_lock, this, 2000); + WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS); Thread t2 = new Thread(writeLocker); t2.start(); // acquire write lock - Thread.sleep(500); + Thread.sleep(HALF_SECOND); assertTrue(getWriterCount() == 1); t1.start(); - Thread.sleep(500); + Thread.sleep(HALF_SECOND); // 1 second underway, writer still holding the // lock so read lock cannot be acquired. assertTrue(getWriterCount() == 1); assertTrue(getReaderCount() == 0); - Thread.sleep(1500); + Thread.sleep(ONE_SECOND + HALF_SECOND); // 2.5 seconds underway, write lock released and // read lock must be acquired. @@ -296,7 +308,7 @@ public class ReadWriteLockTest extends TestCase { } }); t1.start(); - Thread.sleep(1000); // wait until thread is started + Thread.sleep(ONE_SECOND); // wait until thread is started _lock.releaseRead(); // release lock from wrong thread. } catch (RuntimeException e) { return; // ok @@ -323,7 +335,7 @@ public class ReadWriteLockTest extends TestCase { } }); t1.start(); - Thread.sleep(1000); // wait until thread is started + Thread.sleep(ONE_SECOND); // wait until thread is started _lock.releaseWrite(); // release lock from wrong thread. } catch (RuntimeException e) { return; // ok @@ -433,15 +445,15 @@ public class ReadWriteLockTest extends TestCase { * lock, and performs a callback after the lock has been released. */ class ReadLocker implements Runnable { - ReadWriteLock _lock; - ReadWriteLockTest _lockTest; - int _sleepTime; - - public ReadLocker(ReadWriteLock lock, ReadWriteLockTest lockTest, - int sleepTime) { - _lock = lock; - _lockTest = lockTest; - _sleepTime = sleepTime; + private ReadWriteLock _lock; + private ReadWriteLockTest _lockTest; + private int _sleepTime; + + public ReadLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest, + int aSleepTime) { + _lock = aLock; + _lockTest = aLockTest; + _sleepTime = aSleepTime; } public void run() { @@ -467,15 +479,15 @@ class ReadLocker implements Runnable { * lock, and performs a callback after the lock has been released. */ class WriteLocker implements Runnable { - ReadWriteLock _lock; - ReadWriteLockTest _lockTest; - int _sleepTime; - - public WriteLocker(ReadWriteLock lock, ReadWriteLockTest lockTest, - int sleepTime) { - _lock = lock; - _lockTest = lockTest; - _sleepTime = sleepTime; + private ReadWriteLock _lock; + private ReadWriteLockTest _lockTest; + private int _sleepTime; + + public WriteLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest, + int aSleepTime) { + _lock = aLock; + _lockTest = aLockTest; + _sleepTime = aSleepTime; } public void run() {