--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/21 06:24:33 1.63 +++ 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, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -167,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(); } } @@ -507,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 = @@ -584,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); } @@ -820,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(); @@ -847,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(); @@ -875,7 +881,9 @@ public class ReentrantReadWriteLockTest await(c, awaitMethod); shouldThrow(); } catch (IllegalMonitorStateException success) { - } catch (InterruptedException e) { threadUnexpectedException(e); } + } catch (InterruptedException fail) { + threadUnexpectedException(fail); + } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -926,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); } } /** @@ -947,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); } } /** @@ -969,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); } } /** @@ -1003,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); } /** @@ -1111,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); } @@ -1272,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); } @@ -1613,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")); } /** @@ -1635,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")); } /** @@ -1650,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")); } }