--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/07 19:03:26 1.59 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2015/04/25 04:55:31 1.73 @@ -6,17 +6,24 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.io.*; -import java.util.*; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ReentrantReadWriteLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ReentrantReadWriteLockTest.class); @@ -67,7 +74,7 @@ public class ReentrantReadWriteLockTest */ void releaseWriteLock(PublicReentrantReadWriteLock lock) { ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(1, lock.getWriteHoldCount()); writeLock.unlock(); assertNotWriteLocked(lock); @@ -80,11 +87,11 @@ public class ReentrantReadWriteLockTest long startTime = System.nanoTime(); while (!lock.hasQueuedThread(t)) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) - throw new AssertionError("timed out"); + throw new AssertionFailedError("timed out"); Thread.yield(); } assertTrue(t.isAlive()); - assertTrue(lock.getOwner() != t); + assertNotSame(t, lock.getOwner()); } /** @@ -94,8 +101,9 @@ public class ReentrantReadWriteLockTest assertFalse(lock.isWriteLocked()); assertFalse(lock.isWriteLockedByCurrentThread()); assertFalse(lock.writeLock().isHeldByCurrentThread()); - assertNull(lock.getOwner()); assertEquals(0, lock.getWriteHoldCount()); + assertEquals(0, lock.writeLock().getHoldCount()); + assertNull(lock.getOwner()); } /** @@ -110,10 +118,19 @@ public class ReentrantReadWriteLockTest lock.writeLock().isHeldByCurrentThread()); assertEquals(t == Thread.currentThread(), lock.getWriteHoldCount() > 0); + assertEquals(t == Thread.currentThread(), + lock.writeLock().getHoldCount() > 0); assertEquals(0, lock.getReadLockCount()); } /** + * Checks that lock is write-locked by the current thread. + */ + void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) { + assertWriteLockedBy(lock, Thread.currentThread()); + } + + /** * Checks that condition c has no waiters. */ void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { @@ -135,6 +152,33 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } + + /** + * Awaits condition using the specified AwaitMethod. + */ + void await(Condition c, AwaitMethod awaitMethod) + throws InterruptedException { + switch (awaitMethod) { + case await: + c.await(); + break; + case awaitTimed: + assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); + break; + case awaitNanos: + long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); + assertTrue(nanosRemaining > 0); + break; + case awaitUntil: + java.util.Date d = new java.util.Date(); + assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); + break; + default: + throw new AssertionError(); + } + } + /** * Constructor sets given fairness, and is in unlocked state */ @@ -160,30 +204,14 @@ public class ReentrantReadWriteLockTest /** * write-locking and read-locking an unlocked lock succeed */ - public void testLock() { - PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testLock() { testLock(false); } + public void testLock_fair() { testLock(true); } + public void testLock(boolean fair) { + PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); assertNotWriteLocked(lock); lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); - lock.writeLock().unlock(); - assertNotWriteLocked(lock); - assertEquals(0, lock.getReadLockCount()); - lock.readLock().lock(); - assertNotWriteLocked(lock); - assertEquals(1, lock.getReadLockCount()); - lock.readLock().unlock(); - assertNotWriteLocked(lock); - assertEquals(0, lock.getReadLockCount()); - } - - /** - * locking an unlocked fair lock succeeds - */ - public void testFairLock() { - PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); - assertNotWriteLocked(lock); - lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); lock.writeLock().unlock(); assertNotWriteLocked(lock); assertEquals(0, lock.getReadLockCount()); @@ -198,8 +226,10 @@ public class ReentrantReadWriteLockTest /** * getWriteHoldCount returns number of recursive holds */ - public void testGetWriteHoldCount() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetWriteHoldCount() { testGetWriteHoldCount(false); } + public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); } + public void testGetWriteHoldCount(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); for (int i = 1; i <= SIZE; i++) { lock.writeLock().lock(); assertEquals(i,lock.getWriteHoldCount()); @@ -211,10 +241,12 @@ public class ReentrantReadWriteLockTest } /** - * WriteLock.getHoldCount returns number of recursive holds + * writelock.getHoldCount returns number of recursive holds */ - public void testGetHoldCount() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetHoldCount() { testGetHoldCount(false); } + public void testGetHoldCount_fair() { testGetHoldCount(true); } + public void testGetHoldCount(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); for (int i = 1; i <= SIZE; i++) { lock.writeLock().lock(); assertEquals(i,lock.writeLock().getHoldCount()); @@ -228,8 +260,10 @@ public class ReentrantReadWriteLockTest /** * getReadHoldCount returns number of recursive holds */ - public void testGetReadHoldCount() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetReadHoldCount() { testGetReadHoldCount(false); } + public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); } + public void testGetReadHoldCount(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); for (int i = 1; i <= SIZE; i++) { lock.readLock().lock(); assertEquals(i,lock.getReadHoldCount()); @@ -243,8 +277,10 @@ public class ReentrantReadWriteLockTest /** * write-unlocking an unlocked lock throws IllegalMonitorStateException */ - public void testWriteUnlock_IllegalMonitorStateException() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); } + public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); } + public void testWriteUnlock_IMSE(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.writeLock().unlock(); shouldThrow(); @@ -254,8 +290,10 @@ public class ReentrantReadWriteLockTest /** * read-unlocking an unlocked lock throws IllegalMonitorStateException */ - public void testReadUnlock_IllegalMonitorStateException() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); } + public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); } + public void testReadUnlock_IMSE(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.readLock().unlock(); shouldThrow(); @@ -265,8 +303,11 @@ public class ReentrantReadWriteLockTest /** * write-lockInterruptibly is interruptible */ - public void testWriteLockInterruptibly_Interrupted() throws Exception { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testWriteLockInterruptibly_Interruptible() { testWriteLockInterruptibly_Interruptible(false); } + public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); } + public void testWriteLockInterruptibly_Interruptible(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { @@ -282,8 +323,11 @@ public class ReentrantReadWriteLockTest /** * timed write-tryLock is interruptible */ - public void testWriteTryLock_Interrupted() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testWriteTryLock_Interruptible() { testWriteTryLock_Interruptible(false); } + public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); } + public void testWriteTryLock_Interruptible(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { @@ -299,8 +343,11 @@ public class ReentrantReadWriteLockTest /** * read-lockInterruptibly is interruptible */ - public void testReadLockInterruptibly_Interrupted() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testReadLockInterruptibly_Interruptible() { testReadLockInterruptibly_Interruptible(false); } + public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); } + public void testReadLockInterruptibly_Interruptible(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { @@ -316,8 +363,11 @@ public class ReentrantReadWriteLockTest /** * timed read-tryLock is interruptible */ - public void testReadTryLock_Interrupted() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testReadTryLock_Interruptible() { testReadTryLock_Interruptible(false); } + public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); } + public void testReadTryLock_Interruptible(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { @@ -331,10 +381,29 @@ public class ReentrantReadWriteLockTest } /** + * write-tryLock on an unlocked lock succeeds + */ + public void testWriteTryLock() { testWriteTryLock(false); } + public void testWriteTryLock_fair() { testWriteTryLock(true); } + public void testWriteTryLock(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); + assertTrue(lock.writeLock().tryLock()); + assertWriteLockedByMoi(lock); + assertTrue(lock.writeLock().tryLock()); + assertWriteLockedByMoi(lock); + lock.writeLock().unlock(); + releaseWriteLock(lock); + } + + /** * write-tryLock fails if locked */ - public void testWriteTryLockWhenLocked() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } + public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); } + public void testWriteTryLockWhenLocked(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -348,8 +417,11 @@ public class ReentrantReadWriteLockTest /** * read-tryLock fails if locked */ - public void testReadTryLockWhenLocked() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testReadTryLockWhenLocked() { testReadTryLockWhenLocked(false); } + public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); } + public void testReadTryLockWhenLocked(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -363,13 +435,19 @@ public class ReentrantReadWriteLockTest /** * Multiple threads can hold a read lock when not write-locked */ - public void testMultipleReadLocks() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testMultipleReadLocks() { testMultipleReadLocks(false); } + public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); } + public void testMultipleReadLocks(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.readLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { - public void realRun() { + public void realRun() throws InterruptedException { assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); + assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS)); + lock.readLock().unlock(); + lock.readLock().lock(); + lock.readLock().unlock(); }}); awaitTermination(t); @@ -379,10 +457,12 @@ public class ReentrantReadWriteLockTest /** * A writelock succeeds only after a reading thread unlocks */ - public void testWriteAfterReadLock() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testWriteAfterReadLock() { testWriteAfterReadLock(false); } + public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); } + public void testWriteAfterReadLock(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.readLock().lock(); - Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertEquals(1, lock.getReadLockCount()); @@ -402,8 +482,11 @@ public class ReentrantReadWriteLockTest /** * A writelock succeeds only after reading threads unlock */ - public void testWriteAfterMultipleReadLocks() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testWriteAfterMultipleReadLocks() { testWriteAfterMultipleReadLocks(false); } + public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); } + public void testWriteAfterMultipleReadLocks(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.readLock().lock(); lock.readLock().lock(); Thread t1 = newStartedThread(new CheckedRunnable() { @@ -433,10 +516,11 @@ public class ReentrantReadWriteLockTest /** * A thread that tries to acquire a fair read lock (non-reentrantly) - * will block if there is a waiting writer thread. + * will block if there is a waiting writer thread */ - public void testReaderWriterReaderFairFifo() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); + public void testReaderWriterReaderFairFifo() { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(true); final AtomicBoolean t1GotLock = new AtomicBoolean(false); lock.readLock().lock(); @@ -471,8 +555,11 @@ public class ReentrantReadWriteLockTest /** * Readlocks succeed only after a writing thread unlocks */ - public void testReadAfterWriteLock() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testReadAfterWriteLock() { testReadAfterWriteLock(false); } + public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); } + public void testReadAfterWriteLock(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -495,8 +582,10 @@ public class ReentrantReadWriteLockTest /** * Read trylock succeeds if write locked by current thread */ - public void testReadHoldingWriteLock() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testReadHoldingWriteLock() { testReadHoldingWriteLock(false); } + public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); } + public void testReadHoldingWriteLock(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.writeLock().lock(); assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); @@ -504,118 +593,57 @@ public class ReentrantReadWriteLockTest } /** - * Read lock succeeds if write locked by current thread even if - * other threads are waiting for readlock + * Read trylock succeeds (barging) even in the presence of waiting + * readers and/or writers */ - public void testReadHoldingWriteLock2() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - lock.writeLock().lock(); + public void testReadTryLockBarging() { testReadTryLockBarging(false); } + public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } + public void testReadTryLockBarging(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.readLock().lock(); - lock.readLock().unlock(); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { - lock.readLock().lock(); - lock.readLock().unlock(); - }}); - Thread t2 = newStartedThread(new CheckedRunnable() { - public void realRun() { - lock.readLock().lock(); - lock.readLock().unlock(); + lock.writeLock().lock(); + lock.writeLock().unlock(); }}); waitForQueuedThread(lock, t1); - waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); - lock.readLock().lock(); - lock.readLock().unlock(); - releaseWriteLock(lock); - awaitTermination(t1); - awaitTermination(t2); - } - - /** - * Read lock succeeds if write locked by current thread even if - * other threads are waiting for writelock - */ - public void testReadHoldingWriteLock3() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - lock.writeLock().lock(); - lock.readLock().lock(); - lock.readLock().unlock(); - Thread t1 = newStartedThread(new CheckedRunnable() { - public void realRun() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - }}); Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { - lock.writeLock().lock(); - lock.writeLock().unlock(); + lock.readLock().lock(); + lock.readLock().unlock(); }}); - waitForQueuedThread(lock, t1); - waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); - lock.readLock().lock(); - lock.readLock().unlock(); - releaseWriteLock(lock); - awaitTermination(t1); - awaitTermination(t2); - } - - /** - * Write lock succeeds if write locked by current thread even if - * other threads are waiting for writelock - */ - public void testWriteHoldingWriteLock4() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - lock.writeLock().lock(); - lock.writeLock().lock(); - lock.writeLock().unlock(); + if (fair) + waitForQueuedThread(lock, t2); - Thread t1 = newStartedThread(new CheckedRunnable() { + Thread t3 = newStartedThread(new CheckedRunnable() { public void realRun() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - }}); - Thread t2 = newStartedThread(new CheckedRunnable() { - public void realRun() { - lock.writeLock().lock(); - lock.writeLock().unlock(); + lock.readLock().tryLock(); + lock.readLock().unlock(); }}); - waitForQueuedThread(lock, t1); - waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); - assertEquals(1, lock.getWriteHoldCount()); - lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); - assertEquals(2, lock.getWriteHoldCount()); - lock.writeLock().unlock(); - releaseWriteLock(lock); + assertTrue(lock.getReadLockCount() > 0); + awaitTermination(t3); + assertTrue(t1.isAlive()); + if (fair) assertTrue(t2.isAlive()); + lock.readLock().unlock(); awaitTermination(t1); awaitTermination(t2); } /** - * Fair Read trylock succeeds if write locked by current thread - */ - public void testReadHoldingWriteLockFair() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); - lock.writeLock().lock(); - assertTrue(lock.readLock().tryLock()); - lock.readLock().unlock(); - lock.writeLock().unlock(); - } - - /** - * Fair Read lock succeeds if write locked by current thread even if + * Read lock succeeds if write locked by current thread even if * other threads are waiting for readlock */ - public void testReadHoldingWriteLockFair2() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); + public void testReadHoldingWriteLock2() { testReadHoldingWriteLock2(false); } + public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); } + public void testReadHoldingWriteLock2(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); lock.readLock().lock(); lock.readLock().unlock(); @@ -633,7 +661,7 @@ public class ReentrantReadWriteLockTest waitForQueuedThread(lock, t1); waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); lock.readLock().lock(); lock.readLock().unlock(); releaseWriteLock(lock); @@ -642,11 +670,14 @@ public class ReentrantReadWriteLockTest } /** - * Fair Read lock succeeds if write locked by current thread even if + * Read lock succeeds if write locked by current thread even if * other threads are waiting for writelock */ - public void testReadHoldingWriteLockFair3() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); + public void testReadHoldingWriteLock3() { testReadHoldingWriteLock3(false); } + public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); } + public void testReadHoldingWriteLock3(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); lock.readLock().lock(); lock.readLock().unlock(); @@ -664,21 +695,28 @@ public class ReentrantReadWriteLockTest waitForQueuedThread(lock, t1); waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); lock.readLock().lock(); lock.readLock().unlock(); - releaseWriteLock(lock); + assertWriteLockedByMoi(lock); + lock.writeLock().unlock(); awaitTermination(t1); awaitTermination(t2); } /** - * Fair Write lock succeeds if write locked by current thread even if + * Write lock succeeds if write locked by current thread even if * other threads are waiting for writelock */ - public void testWriteHoldingWriteLockFair4() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); + public void testWriteHoldingWriteLock4() { testWriteHoldingWriteLock4(false); } + public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); } + public void testWriteHoldingWriteLock4(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); + lock.writeLock().lock(); lock.writeLock().lock(); + lock.writeLock().unlock(); + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); @@ -692,14 +730,15 @@ public class ReentrantReadWriteLockTest waitForQueuedThread(lock, t1); waitForQueuedThread(lock, t2); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(1, lock.getWriteHoldCount()); lock.writeLock().lock(); + assertWriteLockedByMoi(lock); assertEquals(2, lock.getWriteHoldCount()); lock.writeLock().unlock(); - lock.writeLock().lock(); + assertWriteLockedByMoi(lock); + assertEquals(1, lock.getWriteHoldCount()); lock.writeLock().unlock(); - releaseWriteLock(lock); awaitTermination(t1); awaitTermination(t2); } @@ -707,8 +746,10 @@ public class ReentrantReadWriteLockTest /** * Read tryLock succeeds if readlocked but not writelocked */ - public void testTryLockWhenReadLocked() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testTryLockWhenReadLocked() { testTryLockWhenReadLocked(false); } + public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); } + public void testTryLockWhenReadLocked(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.readLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -723,39 +764,10 @@ public class ReentrantReadWriteLockTest /** * write tryLock fails when readlocked */ - public void testWriteTryLockWhenReadLocked() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - lock.readLock().lock(); - Thread t = newStartedThread(new CheckedRunnable() { - public void realRun() { - assertFalse(lock.writeLock().tryLock()); - }}); - - awaitTermination(t); - lock.readLock().unlock(); - } - - /** - * Fair Read tryLock succeeds if readlocked but not writelocked - */ - public void testTryLockWhenReadLockedFair() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); - lock.readLock().lock(); - Thread t = newStartedThread(new CheckedRunnable() { - public void realRun() { - assertTrue(lock.readLock().tryLock()); - lock.readLock().unlock(); - }}); - - awaitTermination(t); - lock.readLock().unlock(); - } - - /** - * Fair write tryLock fails when readlocked - */ - public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + public void testWriteTryLockWhenReadLocked() { testWriteTryLockWhenReadLocked(false); } + public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); } + public void testWriteTryLockWhenReadLocked(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.readLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -769,8 +781,11 @@ public class ReentrantReadWriteLockTest /** * write timed tryLock times out if locked */ - public void testWriteTryLock_Timeout() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } + public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } + public void testWriteTryLock_Timeout(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -781,15 +796,16 @@ public class ReentrantReadWriteLockTest }}); awaitTermination(t); - assertTrue(lock.writeLock().isHeldByCurrentThread()); - lock.writeLock().unlock(); + releaseWriteLock(lock); } /** * read timed tryLock times out if write-locked */ - public void testReadTryLock_Timeout() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testReadTryLock_Timeout() { testReadTryLock_Timeout(false); } + public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); } + public void testReadTryLock_Timeout(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.writeLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -805,11 +821,16 @@ public class ReentrantReadWriteLockTest } /** - * write lockInterruptibly succeeds if lock free else is interruptible + * write lockInterruptibly succeeds if unlocked, else is interruptible */ - public void testWriteLockInterruptibly() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - lock.writeLock().lockInterruptibly(); + public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } + public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } + public void testWriteLockInterruptibly(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); + try { + lock.writeLock().lockInterruptibly(); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); @@ -817,6 +838,7 @@ public class ReentrantReadWriteLockTest waitForQueuedThread(lock, t); t.interrupt(); + assertTrue(lock.writeLock().isHeldByCurrentThread()); awaitTermination(t); releaseWriteLock(lock); } @@ -824,9 +846,16 @@ public class ReentrantReadWriteLockTest /** * read lockInterruptibly succeeds if lock free else is interruptible */ - public void testReadLockInterruptibly() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - lock.writeLock().lockInterruptibly(); + public void testReadLockInterruptibly() { testReadLockInterruptibly(false); } + public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); } + public void testReadLockInterruptibly(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); + try { + lock.readLock().lockInterruptibly(); + lock.readLock().unlock(); + lock.writeLock().lockInterruptibly(); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); @@ -841,32 +870,31 @@ public class ReentrantReadWriteLockTest /** * Calling await without holding lock throws IllegalMonitorStateException */ - public void testAwait_IllegalMonitor() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - try { - c.await(); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} - try { - c.await(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} - try { - c.awaitNanos(100); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} - try { - c.awaitUninterruptibly(); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} + public void testAwait_IMSE() { testAwait_IMSE(false); } + public void testAwait_IMSE_fair() { testAwait_IMSE(true); } + public void testAwait_IMSE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); + final Condition c = lock.writeLock().newCondition(); + for (AwaitMethod awaitMethod : AwaitMethod.values()) { + long startTime = System.nanoTime(); + try { + await(c, awaitMethod); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (InterruptedException fail) { + threadUnexpectedException(fail); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } } /** * Calling signal without holding lock throws IllegalMonitorStateException */ - public void testSignal_IllegalMonitor() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testSignal_IMSE() { testSignal_IMSE(false); } + public void testSignal_IMSE_fair() { testSignal_IMSE(true); } + public void testSignal_IMSE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { c.signal(); @@ -877,8 +905,10 @@ public class ReentrantReadWriteLockTest /** * Calling signalAll without holding lock throws IllegalMonitorStateException */ - public void testSignalAll_IllegalMonitor() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testSignalAll_IMSE() { testSignalAll_IMSE(false); } + public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); } + public void testSignalAll_IMSE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { c.signalAll(); @@ -889,53 +919,71 @@ public class ReentrantReadWriteLockTest /** * awaitNanos without a signal times out */ - public void testAwaitNanos_Timeout() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - lock.writeLock().lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); - long nanosRemaining = c.awaitNanos(timeoutNanos); - assertTrue(nanosRemaining <= 0); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.writeLock().unlock(); + public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } + public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } + public void testAwaitNanos_Timeout(boolean fair) { + try { + final ReentrantReadWriteLock lock = + new ReentrantReadWriteLock(fair); + final Condition c = lock.writeLock().newCondition(); + lock.writeLock().lock(); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(timeoutNanos); + assertTrue(nanosRemaining <= 0); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.writeLock().unlock(); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** * timed await without a signal times out */ - public void testAwait_Timeout() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - lock.writeLock().lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - assertFalse(c.await(timeoutMillis, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.writeLock().unlock(); + public void testAwait_Timeout() { testAwait_Timeout(false); } + public void testAwait_Timeout_fair() { testAwait_Timeout(true); } + public void testAwait_Timeout(boolean fair) { + try { + final ReentrantReadWriteLock lock = + new ReentrantReadWriteLock(fair); + final Condition c = lock.writeLock().newCondition(); + lock.writeLock().lock(); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + assertFalse(c.await(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.writeLock().unlock(); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** * awaitUntil without a signal times out */ - public void testAwaitUntil_Timeout() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - lock.writeLock().lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.writeLock().unlock(); + public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } + public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } + public void testAwaitUntil_Timeout(boolean fair) { + try { + final ReentrantReadWriteLock lock = + new ReentrantReadWriteLock(fair); + final Condition c = lock.writeLock().newCondition(); + lock.writeLock().lock(); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + java.util.Date d = new java.util.Date(); + assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.writeLock().unlock(); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** * await returns when signalled */ - public void testAwait() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testAwait() { testAwait(false); } + public void testAwait_fair() { testAwait(true); } + public void testAwait(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -946,7 +994,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); }}); - locked.await(); + await(locked); lock.writeLock().lock(); assertHasWaiters(lock, c, t); c.signal(); @@ -957,118 +1005,85 @@ public class ReentrantReadWriteLockTest } /** - * awaitUninterruptibly doesn't abort on interrupt + * awaitUninterruptibly is uninterruptible */ - public void testAwaitUninterruptibly() throws InterruptedException { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } + public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } + public void testAwaitUninterruptibly(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); - final CountDownLatch locked = new CountDownLatch(1); - Thread t = newStartedThread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(2); + + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { + // Interrupt before awaitUninterruptibly lock.writeLock().lock(); - locked.countDown(); + pleaseInterrupt.countDown(); + Thread.currentThread().interrupt(); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.writeLock().unlock(); }}); - locked.await(); - lock.writeLock().lock(); - lock.writeLock().unlock(); - t.interrupt(); - long timeoutMillis = 10; - assertThreadJoinTimesOut(t, timeoutMillis); - lock.writeLock().lock(); - c.signal(); - lock.writeLock().unlock(); - awaitTermination(t); - } - - /** - * await is interruptible - */ - public void testAwait_Interrupt() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - final CountDownLatch locked = new CountDownLatch(1); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() { + // Interrupt during awaitUninterruptibly lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); - assertHasNoWaiters(lock, c); - locked.countDown(); - try { - c.await(); - } finally { - assertWriteLockedBy(lock, Thread.currentThread()); - assertHasNoWaiters(lock, c); - lock.writeLock().unlock(); - assertFalse(Thread.interrupted()); - } + pleaseInterrupt.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.writeLock().unlock(); }}); - locked.await(); - assertHasWaiters(lock, c, t); - t.interrupt(); - awaitTermination(t); - assertNotWriteLocked(lock); - } + await(pleaseInterrupt); + lock.writeLock().lock(); + lock.writeLock().unlock(); + t2.interrupt(); - /** - * awaitNanos is interruptible - */ - public void testAwaitNanos_Interrupt() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); - final Condition c = lock.writeLock().newCondition(); - final CountDownLatch locked = new CountDownLatch(1); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); - assertHasNoWaiters(lock, c); - locked.countDown(); - try { - c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); - } finally { - assertWriteLockedBy(lock, Thread.currentThread()); - assertHasNoWaiters(lock, c); - lock.writeLock().unlock(); - assertFalse(Thread.interrupted()); - } - }}); + assertThreadStaysAlive(t1); + assertTrue(t2.isAlive()); - locked.await(); - assertHasWaiters(lock, c, t); - t.interrupt(); - awaitTermination(t); - assertNotWriteLocked(lock); + lock.writeLock().lock(); + c.signalAll(); + lock.writeLock().unlock(); + + awaitTermination(t1); + awaitTermination(t2); } /** - * awaitUntil is interruptible + * await/awaitNanos/awaitUntil is interruptible */ - public void testAwaitUntil_Interrupt() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } + public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } + public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } + public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } + public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } + public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } + public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } + public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); } + public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(1); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertHasNoWaiters(lock, c); locked.countDown(); - java.util.Date d = new java.util.Date(); try { - c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)); + await(c, awaitMethod); } finally { - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertHasNoWaiters(lock, c); lock.writeLock().unlock(); assertFalse(Thread.interrupted()); } }}); - locked.await(); + await(locked); assertHasWaiters(lock, c, t); t.interrupt(); awaitTermination(t); @@ -1078,28 +1093,33 @@ public class ReentrantReadWriteLockTest /** * signalAll wakes up all threads */ - public void testSignalAll() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } + public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } + public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } + public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } + public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } + public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } + public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } + public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); } + public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(2); final Lock writeLock = lock.writeLock(); - Thread t1 = newStartedThread(new CheckedRunnable() { + class Awaiter extends CheckedRunnable { public void realRun() throws InterruptedException { writeLock.lock(); locked.countDown(); - c.await(); + await(c, awaitMethod); writeLock.unlock(); - }}); + } + } - Thread t2 = newStartedThread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - writeLock.lock(); - locked.countDown(); - c.await(); - writeLock.unlock(); - }}); + Thread t1 = newStartedThread(new Awaiter()); + Thread t2 = newStartedThread(new Awaiter()); - locked.await(); + await(locked); writeLock.lock(); assertHasWaiters(lock, c, t1, t2); c.signalAll(); @@ -1110,10 +1130,13 @@ public class ReentrantReadWriteLockTest } /** - * signal wakes up waiting threads in FIFO order. + * signal wakes up waiting threads in FIFO order */ - public void testSignalWakesFifo() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testSignalWakesFifo() { testSignalWakesFifo(false); } + public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } + public void testSignalWakesFifo(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked1 = new CountDownLatch(1); final CountDownLatch locked2 = new CountDownLatch(1); @@ -1126,7 +1149,7 @@ public class ReentrantReadWriteLockTest writeLock.unlock(); }}); - locked1.await(); + await(locked1); Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -1136,7 +1159,7 @@ public class ReentrantReadWriteLockTest writeLock.unlock(); }}); - locked2.await(); + await(locked2); writeLock.lock(); assertHasWaiters(lock, c, t1, t2); @@ -1157,18 +1180,21 @@ public class ReentrantReadWriteLockTest /** * await after multiple reentrant locking preserves lock count */ - public void testAwaitLockCount() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testAwaitLockCount() { testAwaitLockCount(false); } + public void testAwaitLockCount_fair() { testAwaitLockCount(true); } + public void testAwaitLockCount(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(2); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(1, lock.writeLock().getHoldCount()); locked.countDown(); c.await(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(1, lock.writeLock().getHoldCount()); lock.writeLock().unlock(); }}); @@ -1177,17 +1203,17 @@ public class ReentrantReadWriteLockTest public void realRun() throws InterruptedException { lock.writeLock().lock(); lock.writeLock().lock(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(2, lock.writeLock().getHoldCount()); locked.countDown(); c.await(); - assertWriteLockedBy(lock, Thread.currentThread()); + assertWriteLockedByMoi(lock); assertEquals(2, lock.writeLock().getHoldCount()); lock.writeLock().unlock(); lock.writeLock().unlock(); }}); - locked.await(); + await(locked); lock.writeLock().lock(); assertHasWaiters(lock, c, t1, t2); c.signalAll(); @@ -1200,28 +1226,38 @@ public class ReentrantReadWriteLockTest /** * A serialized lock deserializes as unlocked */ - public void testSerialization() throws Exception { - ReentrantReadWriteLock l = new ReentrantReadWriteLock(); - l.readLock().lock(); - l.readLock().unlock(); - - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(l); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); - r.readLock().lock(); - r.readLock().unlock(); + public void testSerialization() { testSerialization(false); } + public void testSerialization_fair() { testSerialization(true); } + public void testSerialization(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); + lock.writeLock().lock(); + lock.readLock().lock(); + + ReentrantReadWriteLock clone = serialClone(lock); + assertEquals(lock.isFair(), clone.isFair()); + assertTrue(lock.isWriteLocked()); + assertFalse(clone.isWriteLocked()); + assertEquals(1, lock.getReadLockCount()); + assertEquals(0, clone.getReadLockCount()); + clone.writeLock().lock(); + clone.readLock().lock(); + assertTrue(clone.isWriteLocked()); + assertEquals(1, clone.getReadLockCount()); + clone.readLock().unlock(); + clone.writeLock().unlock(); + assertFalse(clone.isWriteLocked()); + assertEquals(1, lock.getReadLockCount()); + assertEquals(0, clone.getReadLockCount()); } /** * hasQueuedThreads reports whether there are waiting threads */ - public void testHasQueuedThreads() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testHasQueuedThreads() { testHasQueuedThreads(false); } + public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); } + public void testHasQueuedThreads(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertFalse(lock.hasQueuedThreads()); @@ -1244,8 +1280,10 @@ public class ReentrantReadWriteLockTest /** * hasQueuedThread(null) throws NPE */ - public void testHasQueuedThreadNPE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); } + public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); } + public void testHasQueuedThreadNPE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.hasQueuedThread(null); shouldThrow(); @@ -1253,10 +1291,13 @@ public class ReentrantReadWriteLockTest } /** - * hasQueuedThread reports whether a thread is queued. + * hasQueuedThread reports whether a thread is queued */ - public void testHasQueuedThread() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testHasQueuedThread() { testHasQueuedThread(false); } + public void testHasQueuedThread_fair() { testHasQueuedThread(true); } + public void testHasQueuedThread(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertFalse(lock.hasQueuedThread(t1)); @@ -1283,8 +1324,11 @@ public class ReentrantReadWriteLockTest /** * getQueueLength reports number of waiting threads */ - public void testGetQueueLength() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetQueueLength() { testGetQueueLength(false); } + public void testGetQueueLength_fair() { testGetQueueLength(true); } + public void testGetQueueLength(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertEquals(0, lock.getQueueLength()); @@ -1306,8 +1350,11 @@ public class ReentrantReadWriteLockTest /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetQueuedThreads() { testGetQueuedThreads(false); } + public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); } + public void testGetQueuedThreads(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertTrue(lock.getQueuedThreads().isEmpty()); @@ -1335,8 +1382,10 @@ public class ReentrantReadWriteLockTest /** * hasWaiters throws NPE if null */ - public void testHasWaitersNPE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testHasWaitersNPE() { testHasWaitersNPE(false); } + public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); } + public void testHasWaitersNPE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.hasWaiters(null); shouldThrow(); @@ -1346,8 +1395,10 @@ public class ReentrantReadWriteLockTest /** * getWaitQueueLength throws NPE if null */ - public void testGetWaitQueueLengthNPE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); } + public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); } + public void testGetWaitQueueLengthNPE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.getWaitQueueLength(null); shouldThrow(); @@ -1357,8 +1408,10 @@ public class ReentrantReadWriteLockTest /** * getWaitingThreads throws NPE if null */ - public void testGetWaitingThreadsNPE() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); } + public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); } + public void testGetWaitingThreadsNPE(boolean fair) { + final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair); try { lock.getWaitingThreads(null); shouldThrow(); @@ -1368,10 +1421,12 @@ public class ReentrantReadWriteLockTest /** * hasWaiters throws IllegalArgumentException if not owned */ - public void testHasWaitersIAE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testHasWaitersIAE() { testHasWaitersIAE(false); } + public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); } + public void testHasWaitersIAE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); - final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair); try { lock2.hasWaiters(c); shouldThrow(); @@ -1381,8 +1436,10 @@ public class ReentrantReadWriteLockTest /** * hasWaiters throws IllegalMonitorStateException if not locked */ - public void testHasWaitersIMSE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testHasWaitersIMSE() { testHasWaitersIMSE(false); } + public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); } + public void testHasWaitersIMSE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { lock.hasWaiters(c); @@ -1393,10 +1450,12 @@ public class ReentrantReadWriteLockTest /** * getWaitQueueLength throws IllegalArgumentException if not owned */ - public void testGetWaitQueueLengthIAE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); } + public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); } + public void testGetWaitQueueLengthIAE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); - final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair); try { lock2.getWaitQueueLength(c); shouldThrow(); @@ -1406,8 +1465,10 @@ public class ReentrantReadWriteLockTest /** * getWaitQueueLength throws IllegalMonitorStateException if not locked */ - public void testGetWaitQueueLengthIMSE() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); } + public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); } + public void testGetWaitQueueLengthIMSE(boolean fair) { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { lock.getWaitQueueLength(c); @@ -1418,10 +1479,14 @@ public class ReentrantReadWriteLockTest /** * getWaitingThreads throws IllegalArgumentException if not owned */ - public void testGetWaitingThreadsIAE() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); } + public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); } + public void testGetWaitingThreadsIAE(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); - final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); + final PublicReentrantReadWriteLock lock2 = + new PublicReentrantReadWriteLock(fair); try { lock2.getWaitingThreads(c); shouldThrow(); @@ -1431,8 +1496,11 @@ public class ReentrantReadWriteLockTest /** * getWaitingThreads throws IllegalMonitorStateException if not locked */ - public void testGetWaitingThreadsIMSE() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); } + public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); } + public void testGetWaitingThreadsIMSE(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { lock.getWaitingThreads(c); @@ -1443,8 +1511,11 @@ public class ReentrantReadWriteLockTest /** * hasWaiters returns true when a thread is waiting, else false */ - public void testHasWaiters() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testHasWaiters() { testHasWaiters(false); } + public void testHasWaiters_fair() { testHasWaiters(true); } + public void testHasWaiters(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -1459,7 +1530,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); }}); - locked.await(); + await(locked); lock.writeLock().lock(); assertHasWaiters(lock, c, t); assertTrue(lock.hasWaiters(c)); @@ -1474,8 +1545,11 @@ public class ReentrantReadWriteLockTest /** * getWaitQueueLength returns number of waiting threads */ - public void testGetWaitQueueLength() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetWaitQueueLength() { testGetWaitQueueLength(false); } + public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); } + public void testGetWaitQueueLength(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -1487,7 +1561,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); }}); - locked.await(); + await(locked); lock.writeLock().lock(); assertHasWaiters(lock, c, t); assertEquals(1, lock.getWaitQueueLength(c)); @@ -1501,8 +1575,11 @@ public class ReentrantReadWriteLockTest /** * getWaitingThreads returns only and all waiting threads */ - public void testGetWaitingThreads() throws InterruptedException { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + public void testGetWaitingThreads() { testGetWaitingThreads(false); } + public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); } + public void testGetWaitingThreads(boolean fair) { + final PublicReentrantReadWriteLock lock = + new PublicReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final CountDownLatch locked1 = new CountDownLatch(1); final CountDownLatch locked2 = new CountDownLatch(1); @@ -1529,9 +1606,9 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); t1.start(); - locked1.await(); + await(locked1); t2.start(); - locked2.await(); + await(locked2); lock.writeLock().lock(); assertTrue(lock.hasWaiters(c)); @@ -1551,46 +1628,47 @@ public class ReentrantReadWriteLockTest /** * toString indicates current lock state */ - public void testToString() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - String us = lock.toString(); - assertTrue(us.indexOf("Write locks = 0") >= 0); - assertTrue(us.indexOf("Read locks = 0") >= 0); - lock.writeLock().lock(); - String ws = lock.toString(); - assertTrue(ws.indexOf("Write locks = 1") >= 0); - assertTrue(ws.indexOf("Read locks = 0") >= 0); + public void testToString() { testToString(false); } + public void testToString_fair() { testToString(true); } + public void testToString(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); + assertTrue(lock.toString().contains("Write locks = 0")); + assertTrue(lock.toString().contains("Read locks = 0")); + lock.writeLock().lock(); + assertTrue(lock.toString().contains("Write locks = 1")); + assertTrue(lock.toString().contains("Read locks = 0")); lock.writeLock().unlock(); lock.readLock().lock(); lock.readLock().lock(); - String rs = lock.toString(); - assertTrue(rs.indexOf("Write locks = 0") >= 0); - assertTrue(rs.indexOf("Read locks = 2") >= 0); + assertTrue(lock.toString().contains("Write locks = 0")); + assertTrue(lock.toString().contains("Read locks = 2")); } /** * readLock.toString indicates current lock state */ - public void testReadLockToString() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - String us = lock.readLock().toString(); - assertTrue(us.indexOf("Read locks = 0") >= 0); + public void testReadLockToString() { testReadLockToString(false); } + public void testReadLockToString_fair() { testReadLockToString(true); } + public void testReadLockToString(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); + assertTrue(lock.readLock().toString().contains("Read locks = 0")); lock.readLock().lock(); lock.readLock().lock(); - String rs = lock.readLock().toString(); - assertTrue(rs.indexOf("Read locks = 2") >= 0); + assertTrue(lock.readLock().toString().contains("Read locks = 2")); } /** * writeLock.toString indicates current lock state */ - public void testWriteLockToString() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - String us = lock.writeLock().toString(); - assertTrue(us.indexOf("Unlocked") >= 0); + public void testWriteLockToString() { testWriteLockToString(false); } + public void testWriteLockToString_fair() { testWriteLockToString(true); } + public void testWriteLockToString(boolean fair) { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); + assertTrue(lock.writeLock().toString().contains("Unlocked")); lock.writeLock().lock(); - String ls = lock.writeLock().toString(); - assertTrue(ls.indexOf("Locked") >= 0); + assertTrue(lock.writeLock().toString().contains("Locked")); + lock.writeLock().unlock(); + assertTrue(lock.writeLock().toString().contains("Unlocked")); } }