(no commit message)
[utils] / support / general / src / test / java / org / wamblee / concurrency / ReadWriteLockTest.java
index e0d22f774923620b3e2a9ade3c7042bc4bf51b2e..5b0d510e1806fd03fdb80facdc672e3ea2267f28 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.wamblee.concurrency;
 
-import junit.framework.Assert;
 import junit.framework.TestCase;
 
-
 /**
  * Testing the read-write lock class. Note: in case of problems, test cases
  * could hang.
- *
+ * 
  * @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 static final int HALF_SECOND = 100;
+
+    private static final int ONE_SECOND = 200;
+
+    private static final int TWO_SECONDS = 400;
     private ReadWriteLock lock;
-    private int                   nReaders;
-    private int                   nWriters;
+    private int nReaders;
+    private int nWriters;
 
     /**
      * Constructor for ReadWriteLockTest.
-     *
+     * 
      * @param aName
      */
     public ReadWriteLockTest(String aName) {
@@ -109,13 +100,14 @@ public class ReadWriteLockTest extends TestCase {
 
     /**
      * Verify concurrent access by multiple readers is possible.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testMultipleReaders() throws InterruptedException {
         Runnable runnable = new ReadLocker(lock, this, TWO_SECONDS);
 
-        Thread   t1 = new Thread(runnable);
+        Thread t1 = new Thread(runnable);
         t1.start();
 
         Thread t2 = new Thread(runnable);
@@ -128,13 +120,14 @@ public class ReadWriteLockTest extends TestCase {
 
     /**
      * Verify that only one writer at a time can acquire the write lock.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testSingleWriter() throws InterruptedException {
         WriteLocker writer = new WriteLocker(lock, this, ONE_SECOND);
-        Thread      t1 = new Thread(writer);
-        Thread      t2 = new Thread(writer);
+        Thread t1 = new Thread(writer);
+        Thread t2 = new Thread(writer);
 
         t1.start();
         t2.start();
@@ -147,14 +140,16 @@ public class ReadWriteLockTest extends TestCase {
 
     /**
      * Verify that multiple writers cannot acquire the write lock concurrently.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testMultipleWriters() throws InterruptedException {
-        WriteLocker writer1 = new WriteLocker(lock, this, HALF_SECOND + ONE_SECOND);
+        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);
+        Thread t1 = new Thread(writer1);
+        Thread t2 = new Thread(writer2);
 
         t1.start();
         Thread.sleep(HALF_SECOND);
@@ -167,23 +162,24 @@ public class ReadWriteLockTest extends TestCase {
         Thread.sleep(ONE_SECOND);
 
         // at t = 2, the second writer still must have
-        // a lock. 
+        // a lock.
         assertTrue(getWriterCount() == 1);
         t1.join();
         t2.join();
     }
 
     /**
-     * Verify that after the first reader acquires a lock,  a subsequent writer
+     * Verify that after the first reader acquires a lock, a subsequent writer
      * can only acquire the lock after the reader has released it.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testReadWrite1() throws InterruptedException {
-        ReadLocker  readLocker  = new ReadLocker(lock, this, TWO_SECONDS);
-        Thread      t1          = new Thread(readLocker);
+        ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS);
+        Thread t1 = new Thread(readLocker);
         WriteLocker writeLocker = new WriteLocker(lock, this, TWO_SECONDS);
-        Thread      t2          = new Thread(writeLocker);
+        Thread t2 = new Thread(writeLocker);
 
         t1.start(); // acquire read lock
         Thread.sleep(HALF_SECOND);
@@ -192,13 +188,13 @@ public class ReadWriteLockTest extends TestCase {
         Thread.sleep(HALF_SECOND);
 
         // 1 second underway, reader still holding the
-        //   lock so write lock cannot be acquired.
+        // lock so write lock cannot be acquired.
         assertTrue(getReaderCount() == 1);
         assertTrue(getWriterCount() == 0);
         Thread.sleep(ONE_SECOND + HALF_SECOND);
 
-        // 2.5 seconds underway, read lock released and 
-        // write lock must be acquired. 
+        // 2.5 seconds underway, read lock released and
+        // write lock must be acquired.
         assertTrue("Wrong no. of readers: " + getReaderCount(),
             getReaderCount() == 0);
         assertTrue(getWriterCount() == 1);
@@ -209,16 +205,19 @@ public class ReadWriteLockTest extends TestCase {
     /**
      * Verify that when multiple readers have acquired a read lock, the writer
      * can only acquire the lock after all readers have released it.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testReadWrite2() throws InterruptedException {
-        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);
+        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, TWO_SECONDS);
-        Thread      t3          = new Thread(writeLocker);
+        Thread t3 = new Thread(writeLocker);
 
         t1.start(); // acquire read lock [0, 2.5]
         Thread.sleep(ONE_SECOND);
@@ -228,10 +227,10 @@ public class ReadWriteLockTest extends TestCase {
         Thread.sleep(HALF_SECOND);
         // t = 1.5
         assertTrue(getReaderCount() == 2);
-        t3.start(); // write lock 
+        t3.start(); // write lock
         Thread.sleep(HALF_SECOND);
 
-        // 2 seconds, 
+        // 2 seconds,
         assertTrue(getReaderCount() == 2);
         assertTrue(getWriterCount() == 0);
         Thread.sleep(ONE_SECOND);
@@ -242,8 +241,8 @@ public class ReadWriteLockTest extends TestCase {
         assertTrue(getWriterCount() == 0);
         Thread.sleep(ONE_SECOND);
 
-        // 4 seconds underway, write lock must have 
-        // been acquired. 
+        // 4 seconds underway, write lock must have
+        // been acquired.
         assertTrue(getReaderCount() == 0);
         assertTrue(getWriterCount() == 1);
 
@@ -253,16 +252,17 @@ public class ReadWriteLockTest extends TestCase {
     }
 
     /**
-     * Verify that after a writer acquires a lock,  a subsequent reader can
-     * only acquire the lock after the writer has released it.
-     *
-     * @throws InterruptedException May not occur.
+     * Verify that after a writer acquires a lock, a subsequent reader can only
+     * acquire the lock after the writer has released it.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testReadWrite3() throws InterruptedException {
-        ReadLocker  readLocker  = new ReadLocker(lock, this, TWO_SECONDS);
-        Thread      t1          = new Thread(readLocker);
+        ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS);
+        Thread t1 = new Thread(readLocker);
         WriteLocker writeLocker = new WriteLocker(lock, this, TWO_SECONDS);
-        Thread      t2          = new Thread(writeLocker);
+        Thread t2 = new Thread(writeLocker);
 
         t2.start(); // acquire write lock
         Thread.sleep(HALF_SECOND);
@@ -271,13 +271,13 @@ public class ReadWriteLockTest extends TestCase {
         Thread.sleep(HALF_SECOND);
 
         // 1 second underway, writer still holding the
-        //   lock so read lock cannot be acquired.
+        // lock so read lock cannot be acquired.
         assertTrue(getWriterCount() == 1);
         assertTrue(getReaderCount() == 0);
         Thread.sleep(ONE_SECOND + HALF_SECOND);
 
-        // 2.5 seconds underway, write lock released and 
-        // read lock must be acquired. 
+        // 2.5 seconds underway, write lock released and
+        // read lock must be acquired.
         assertTrue("Wrong no. of writers: " + getReaderCount(),
             getWriterCount() == 0);
         assertTrue(getReaderCount() == 1);
@@ -286,29 +286,29 @@ public class ReadWriteLockTest extends TestCase {
     }
 
     /*
-     * The following test cases are for testing whether or not
-     * the read write lock checks the locking correctly.
-     * Strictly speaking, these checks wouldn't be necessary
-     * because it involves the contract of the ReadWriteLock which
-     * must be obeyed by users of the ReadWriteLock. Nevertheless,
-     * this is tested anyway to be absolutely sure.
+     * The following test cases are for testing whether or not the read write
+     * lock checks the locking correctly. Strictly speaking, these checks
+     * wouldn't be necessary because it involves the contract of the
+     * ReadWriteLock which must be obeyed by users of the ReadWriteLock.
+     * Nevertheless, this is tested anyway to be absolutely sure.
      */
 
     /**
-     * Acquire a read lock from one thread, release it from another. Verify
-     * that a RuntimeException is thrown.
-     *
-     * @throws InterruptedException May not occur.
+     * Acquire a read lock from one thread, release it from another. Verify that
+     * a RuntimeException is thrown.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testReleaseReadFromWrongThread() throws InterruptedException {
         Thread t1 = null;
 
         try {
             t1 = new Thread(new Runnable() {
-                        public void run() {
-                            ReadWriteLockTest.this.lock.acquireRead();
-                        }
-                    });
+                public void run() {
+                    ReadWriteLockTest.this.lock.acquireRead();
+                }
+            });
             t1.start();
             Thread.sleep(ONE_SECOND); // wait until thread is started
             lock.releaseRead(); // release lock from wrong thread.
@@ -324,18 +324,19 @@ public class ReadWriteLockTest extends TestCase {
     /**
      * Acquire a write lock from one thread, release it from another. Verify
      * that a RuntimeException is thrown.
-     *
-     * @throws InterruptedException May not occur.
+     * 
+     * @throws InterruptedException
+     *             May not occur.
      */
     public void testReleaseWriteFromWrongThread() throws InterruptedException {
         Thread t1 = null;
 
         try {
             t1 = new Thread(new Runnable() {
-                        public void run() {
-                            ReadWriteLockTest.this.lock.acquireWrite();
-                        }
-                    });
+                public void run() {
+                    ReadWriteLockTest.this.lock.acquireWrite();
+                }
+            });
             t1.start();
             Thread.sleep(ONE_SECOND); // wait until thread is started
             lock.releaseWrite(); // release lock from wrong thread.
@@ -349,8 +350,8 @@ public class ReadWriteLockTest extends TestCase {
     }
 
     /**
-     * Try to acquire a read lock multiple times. Verify that a
-     * RuntimeException is thrown.
+     * Try to acquire a read lock multiple times. Verify that a RuntimeException
+     * is thrown.
      */
     public void testAcquireReadTwice() {
         try {
@@ -440,22 +441,21 @@ public class ReadWriteLockTest extends TestCase {
     }
 }
 
-
 /**
- * ReadLocker acquires a read lock and performs a callback when  the lock as
- * been acquired, sleeps for a designated amount of time, releases the read
- * lock, and performs a callback after the lock has been released.
+ * ReadLocker acquires a read lock and performs a callback when the lock as been
+ * acquired, sleeps for a designated amount of time, releases the read lock, and
+ * performs a callback after the lock has been released.
  */
 class ReadLocker implements Runnable {
-    private ReadWriteLock     lock;
+    private ReadWriteLock lock;
     private ReadWriteLockTest lockTest;
-    private int               sleepTime;
+    private int sleepTime;
 
     public ReadLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
         int aSleepTime) {
-        lock          = aLock;
-        lockTest      = aLockTest;
-        sleepTime     = aSleepTime;
+        lock = aLock;
+        lockTest = aLockTest;
+        sleepTime = aSleepTime;
     }
 
     public void run() {
@@ -465,8 +465,8 @@ class ReadLocker implements Runnable {
         try {
             Thread.sleep(sleepTime);
         } catch (InterruptedException e) {
-            Assert.fail("ReadLocker thread was interrupted."
-                Thread.currentThread());
+            throw new RuntimeException("ReadLocker thread was interrupted." +
+                Thread.currentThread());
         }
 
         lock.releaseRead();
@@ -474,22 +474,21 @@ class ReadLocker implements Runnable {
     }
 }
 
-
 /**
- * WriteLocker acquires a write lock and performs a callback when  the lock as
+ * WriteLocker acquires a write lock and performs a callback when the lock as
  * been acquired, sleeps for a designated amount of time, releases the write
  * lock, and performs a callback after the lock has been released.
  */
 class WriteLocker implements Runnable {
-    private ReadWriteLock     lock;
+    private ReadWriteLock lock;
     private ReadWriteLockTest lockTest;
-    private int               sleepTime;
+    private int sleepTime;
 
     public WriteLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
         int aSleepTime) {
-        lock          = aLock;
-        lockTest      = aLockTest;
-        sleepTime     = aSleepTime;
+        lock = aLock;
+        lockTest = aLockTest;
+        sleepTime = aSleepTime;
     }
 
     public void run() {
@@ -499,8 +498,8 @@ class WriteLocker implements Runnable {
         try {
             Thread.sleep(sleepTime);
         } catch (InterruptedException e) {
-            Assert.fail("WriteLocker thread was interrupted: "
-                Thread.currentThread());
+            throw new RuntimeException("WriteLocker thread was interrupted: " +
+                Thread.currentThread());
         }
 
         lock.releaseWrite();