--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/15 17:30:21 1.62 +++ 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); @@ -84,7 +91,7 @@ public class ReentrantReadWriteLockTest Thread.yield(); } assertTrue(t.isAlive()); - assertTrue(lock.getOwner() != t); + assertNotSame(t, lock.getOwner()); } /** @@ -145,7 +152,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } - enum AwaitMethod { await, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -156,6 +163,9 @@ public class ReentrantReadWriteLockTest 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); @@ -164,6 +174,8 @@ public class ReentrantReadWriteLockTest 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(); } } @@ -504,7 +516,7 @@ 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() { final PublicReentrantReadWriteLock lock = @@ -581,7 +593,8 @@ public class ReentrantReadWriteLockTest } /** - * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers. + * Read trylock succeeds (barging) even in the presence of waiting + * readers and/or writers */ public void testReadTryLockBarging() { testReadTryLockBarging(false); } public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } @@ -817,9 +830,7 @@ public class ReentrantReadWriteLockTest new PublicReentrantReadWriteLock(fair); try { lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); @@ -844,9 +855,7 @@ public class ReentrantReadWriteLockTest lock.readLock().lockInterruptibly(); lock.readLock().unlock(); lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); @@ -866,28 +875,17 @@ public class ReentrantReadWriteLockTest public void testAwait_IMSE(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); - long startTime = System.nanoTime(); - try { - try { - c.await(); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} - try { - c.await(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} - try { - c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} + for (AwaitMethod awaitMethod : AwaitMethod.values()) { + long startTime = System.nanoTime(); try { - c.awaitUninterruptibly(); + await(c, awaitMethod); shouldThrow(); - } catch (IllegalMonitorStateException success) {} - } catch (InterruptedException ie) { - threadUnexpectedException(ie); + } catch (IllegalMonitorStateException success) { + } catch (InterruptedException fail) { + threadUnexpectedException(fail); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } - assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); } /** @@ -936,9 +934,7 @@ public class ReentrantReadWriteLockTest assertTrue(nanosRemaining <= 0); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -957,9 +953,7 @@ public class ReentrantReadWriteLockTest assertFalse(c.await(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -979,9 +973,7 @@ public class ReentrantReadWriteLockTest assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -1013,33 +1005,50 @@ public class ReentrantReadWriteLockTest } /** - * awaitUninterruptibly doesn't abort on interrupt + * awaitUninterruptibly is uninterruptible */ 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(); }}); - await(locked); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() { + // Interrupt during awaitUninterruptibly + lock.writeLock().lock(); + pleaseInterrupt.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.writeLock().unlock(); + }}); + + await(pleaseInterrupt); lock.writeLock().lock(); lock.writeLock().unlock(); - t.interrupt(); - long timeoutMillis = 10; - assertThreadStaysAlive(t, timeoutMillis); + t2.interrupt(); + + assertThreadStaysAlive(t1); + assertTrue(t2.isAlive()); + lock.writeLock().lock(); - c.signal(); + c.signalAll(); lock.writeLock().unlock(); - awaitTermination(t); + + awaitTermination(t1); + awaitTermination(t2); } /** @@ -1047,6 +1056,8 @@ public class ReentrantReadWriteLockTest */ 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); } @@ -1084,6 +1095,8 @@ public class ReentrantReadWriteLockTest */ 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); } @@ -1117,7 +1130,7 @@ public class ReentrantReadWriteLockTest } /** - * signal wakes up waiting threads in FIFO order. + * signal wakes up waiting threads in FIFO order */ public void testSignalWakesFifo() { testSignalWakesFifo(false); } public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } @@ -1278,7 +1291,7 @@ public class ReentrantReadWriteLockTest } /** - * hasQueuedThread reports whether a thread is queued. + * hasQueuedThread reports whether a thread is queued */ public void testHasQueuedThread() { testHasQueuedThread(false); } public void testHasQueuedThread_fair() { testHasQueuedThread(true); } @@ -1619,19 +1632,16 @@ public class ReentrantReadWriteLockTest public void testToString_fair() { testToString(true); } public void testToString(boolean fair) { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); - 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); + 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")); } /** @@ -1641,12 +1651,10 @@ public class ReentrantReadWriteLockTest public void testReadLockToString_fair() { testReadLockToString(true); } public void testReadLockToString(boolean fair) { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); - String us = lock.readLock().toString(); - assertTrue(us.indexOf("Read locks = 0") >= 0); + 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")); } /** @@ -1656,11 +1664,11 @@ public class ReentrantReadWriteLockTest public void testWriteLockToString_fair() { testWriteLockToString(true); } public void testWriteLockToString(boolean fair) { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); - String us = lock.writeLock().toString(); - assertTrue(us.indexOf("Unlocked") >= 0); + 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")); } }