--- jsr166/src/test/tck/ReentrantLockTest.java 2010/09/16 00:52:49 1.37 +++ jsr166/src/test/tck/ReentrantLockTest.java 2011/05/07 19:03:26 1.45 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -32,7 +32,6 @@ public class ReentrantLockTest extends J } } - /** * A runnable calling lockInterruptibly that expects to be * interrupted @@ -50,6 +49,10 @@ public class ReentrantLockTest extends J */ static class PublicReentrantLock extends ReentrantLock { PublicReentrantLock() { super(); } + PublicReentrantLock(boolean fair) { super(fair); } + public Thread getOwner() { + return super.getOwner(); + } public Collection getQueuedThreads() { return super.getQueuedThreads(); } @@ -59,42 +62,119 @@ public class ReentrantLockTest extends J } /** - * Constructor sets given fairness + * Releases write lock, checking that it had a hold count of 1. + */ + void releaseLock(PublicReentrantLock lock) { + assertLockedBy(lock, Thread.currentThread()); + lock.unlock(); + assertFalse(lock.isHeldByCurrentThread()); + assertNotLocked(lock); + } + + /** + * Spin-waits until lock.hasQueuedThread(t) becomes true. + */ + void waitForQueuedThread(PublicReentrantLock lock, Thread t) { + long startTime = System.nanoTime(); + while (!lock.hasQueuedThread(t)) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + throw new AssertionError("timed out"); + Thread.yield(); + } + assertTrue(t.isAlive()); + assertTrue(lock.getOwner() != t); + } + + /** + * Checks that lock is not locked. + */ + void assertNotLocked(PublicReentrantLock lock) { + assertFalse(lock.isLocked()); + assertFalse(lock.isHeldByCurrentThread()); + assertNull(lock.getOwner()); + assertEquals(0, lock.getHoldCount()); + } + + /** + * Checks that lock is locked by the given thread. + */ + void assertLockedBy(PublicReentrantLock lock, Thread t) { + assertTrue(lock.isLocked()); + assertSame(t, lock.getOwner()); + assertEquals(t == Thread.currentThread(), + lock.isHeldByCurrentThread()); + assertEquals(t == Thread.currentThread(), + lock.getHoldCount() > 0); + } + + /** + * Checks that condition c has no waiters. + */ + void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { + assertHasWaiters(lock, c, new Thread[] {}); + } + + /** + * Checks that condition c has exactly the given waiter threads. + */ + void assertHasWaiters(PublicReentrantLock lock, Condition c, + Thread... threads) { + lock.lock(); + assertEquals(threads.length > 0, lock.hasWaiters(c)); + assertEquals(threads.length, lock.getWaitQueueLength(c)); + assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); + assertEquals(threads.length, lock.getWaitingThreads(c).size()); + assertEquals(new HashSet(lock.getWaitingThreads(c)), + new HashSet(Arrays.asList(threads))); + lock.unlock(); + } + + /** + * Constructor sets given fairness, and is in unlocked state */ public void testConstructor() { - assertFalse(new ReentrantLock().isFair()); - assertFalse(new ReentrantLock(false).isFair()); - assertTrue(new ReentrantLock(true).isFair()); + PublicReentrantLock lock; + + lock = new PublicReentrantLock(); + assertFalse(lock.isFair()); + assertNotLocked(lock); + + lock = new PublicReentrantLock(true); + assertTrue(lock.isFair()); + assertNotLocked(lock); + + lock = new PublicReentrantLock(false); + assertFalse(lock.isFair()); + assertNotLocked(lock); } /** * locking an unlocked lock succeeds */ public void testLock() { - ReentrantLock rl = new ReentrantLock(); - rl.lock(); - assertTrue(rl.isLocked()); - rl.unlock(); - assertFalse(rl.isLocked()); + PublicReentrantLock lock = new PublicReentrantLock(); + lock.lock(); + assertLockedBy(lock, Thread.currentThread()); + releaseLock(lock); } /** * locking an unlocked fair lock succeeds */ public void testFairLock() { - ReentrantLock rl = new ReentrantLock(true); - rl.lock(); - assertTrue(rl.isLocked()); - rl.unlock(); + PublicReentrantLock lock = new PublicReentrantLock(true); + lock.lock(); + assertLockedBy(lock, Thread.currentThread()); + releaseLock(lock); } /** * Unlocking an unlocked lock throws IllegalMonitorStateException */ public void testUnlock_IllegalMonitorStateException() { - ReentrantLock rl = new ReentrantLock(); + ReentrantLock lock = new ReentrantLock(); try { - rl.unlock(); + lock.unlock(); shouldThrow(); } catch (IllegalMonitorStateException success) {} } @@ -103,95 +183,89 @@ public class ReentrantLockTest extends J * tryLock on an unlocked lock succeeds */ public void testTryLock() { - ReentrantLock rl = new ReentrantLock(); - assertTrue(rl.tryLock()); - assertTrue(rl.isLocked()); - rl.unlock(); + PublicReentrantLock lock = new PublicReentrantLock(); + assertTrue(lock.tryLock()); + assertLockedBy(lock, Thread.currentThread()); + releaseLock(lock); } - /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testHasQueuedThreads() throws InterruptedException { + final PublicReentrantLock lock = new PublicReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertFalse(lock.hasQueuedThreads()); lock.lock(); + assertFalse(lock.hasQueuedThreads()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t1); assertTrue(lock.hasQueuedThreads()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t2); assertTrue(lock.hasQueuedThreads()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t1); assertTrue(lock.hasQueuedThreads()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t2); assertFalse(lock.hasQueuedThreads()); - t1.join(); - t2.join(); } /** * getQueueLength reports number of waiting threads */ public void testGetQueueLength() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertEquals(0, lock.getQueueLength()); lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t1); assertEquals(1, lock.getQueueLength()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t2); assertEquals(2, lock.getQueueLength()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t1); assertEquals(1, lock.getQueueLength()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t2); assertEquals(0, lock.getQueueLength()); - t1.join(); - t2.join(); } /** * getQueueLength reports number of waiting threads */ public void testGetQueueLength_fair() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(true); + final PublicReentrantLock lock = new PublicReentrantLock(true); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertEquals(0, lock.getQueueLength()); lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t1); assertEquals(1, lock.getQueueLength()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t2); assertEquals(2, lock.getQueueLength()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t1); assertEquals(1, lock.getQueueLength()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t2); assertEquals(0, lock.getQueueLength()); - t1.join(); - t2.join(); } /** * hasQueuedThread(null) throws NPE */ public void testHasQueuedThreadNPE() { - final ReentrantLock sync = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); try { - sync.hasQueuedThread(null); + lock.hasQueuedThread(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -200,33 +274,30 @@ public class ReentrantLockTest extends J * hasQueuedThread reports whether a thread is queued. */ public void testHasQueuedThread() throws InterruptedException { - final ReentrantLock sync = new ReentrantLock(); - Thread t1 = new Thread(new InterruptedLockRunnable(sync)); - Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); - assertFalse(sync.hasQueuedThread(t1)); - assertFalse(sync.hasQueuedThread(t2)); - sync.lock(); + final PublicReentrantLock lock = new PublicReentrantLock(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + assertFalse(lock.hasQueuedThread(t1)); + assertFalse(lock.hasQueuedThread(t2)); + lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.hasQueuedThread(t1)); + waitForQueuedThread(lock, t1); + assertTrue(lock.hasQueuedThread(t1)); + assertFalse(lock.hasQueuedThread(t2)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.hasQueuedThread(t1)); - assertTrue(sync.hasQueuedThread(t2)); + waitForQueuedThread(lock, t2); + assertTrue(lock.hasQueuedThread(t1)); + assertTrue(lock.hasQueuedThread(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.hasQueuedThread(t1)); - assertTrue(sync.hasQueuedThread(t2)); - sync.unlock(); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.hasQueuedThread(t1)); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.hasQueuedThread(t2)); - t1.join(); - t2.join(); + awaitTermination(t1); + assertFalse(lock.hasQueuedThread(t1)); + assertTrue(lock.hasQueuedThread(t2)); + lock.unlock(); + awaitTermination(t2); + assertFalse(lock.hasQueuedThread(t1)); + assertFalse(lock.hasQueuedThread(t2)); } - /** * getQueuedThreads includes waiting threads */ @@ -238,72 +309,72 @@ public class ReentrantLockTest extends J lock.lock(); assertTrue(lock.getQueuedThreads().isEmpty()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t1); + assertEquals(1, lock.getQueuedThreads().size()); assertTrue(lock.getQueuedThreads().contains(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t2); + assertEquals(2, lock.getQueuedThreads().size()); assertTrue(lock.getQueuedThreads().contains(t1)); assertTrue(lock.getQueuedThreads().contains(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t1); assertFalse(lock.getQueuedThreads().contains(t1)); assertTrue(lock.getQueuedThreads().contains(t2)); + assertEquals(1, lock.getQueuedThreads().size()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + awaitTermination(t2); assertTrue(lock.getQueuedThreads().isEmpty()); - t1.join(); - t2.join(); } - /** * timed tryLock is interruptible. */ - public void testInterruptedException2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testTryLock_Interrupted() throws InterruptedException { + final PublicReentrantLock lock = new PublicReentrantLock(); lock.lock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); + lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + waitForQueuedThread(lock, t); t.interrupt(); - t.join(); + awaitTermination(t); + releaseLock(lock); } - /** - * TryLock on a locked lock fails + * tryLock on a locked lock fails */ public void testTryLockWhenLocked() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); lock.lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertFalse(lock.tryLock()); }}); - t.start(); - t.join(); - lock.unlock(); + awaitTermination(t); + releaseLock(lock); } /** * Timed tryLock on a locked lock times out */ public void testTryLock_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); lock.lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertFalse(lock.tryLock(1, MILLISECONDS)); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); }}); - t.start(); - t.join(); - lock.unlock(); + awaitTermination(t); + releaseLock(lock); } /** @@ -321,59 +392,63 @@ public class ReentrantLockTest extends J } } - /** * isLocked is true when locked and false when not */ - public void testIsLocked() throws InterruptedException { + public void testIsLocked() throws Exception { final ReentrantLock lock = new ReentrantLock(); + assertFalse(lock.isLocked()); + lock.lock(); + assertTrue(lock.isLocked()); lock.lock(); assertTrue(lock.isLocked()); lock.unlock(); + assertTrue(lock.isLocked()); + lock.unlock(); assertFalse(lock.isLocked()); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + final CyclicBarrier barrier = new CyclicBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws Exception { lock.lock(); - Thread.sleep(SMALL_DELAY_MS); + assertTrue(lock.isLocked()); + barrier.await(); + barrier.await(); lock.unlock(); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + barrier.await(); assertTrue(lock.isLocked()); - t.join(); + barrier.await(); + awaitTermination(t); assertFalse(lock.isLocked()); } - /** * lockInterruptibly is interruptible. */ - public void testLockInterruptibly1() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testLockInterruptibly_Interrupted() throws InterruptedException { + final PublicReentrantLock lock = new PublicReentrantLock(); lock.lock(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread t = newStartedThread(new InterruptedLockRunnable(lock)); + waitForQueuedThread(lock, t); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - lock.unlock(); - t.join(); + awaitTermination(t); + releaseLock(lock); } /** * lockInterruptibly succeeds when unlocked, else is interruptible */ - public void testLockInterruptibly2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testLockInterruptibly_Interrupted2() throws InterruptedException { + final PublicReentrantLock lock = new PublicReentrantLock(); lock.lockInterruptibly(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread t = newStartedThread(new InterruptedLockRunnable(lock)); + waitForQueuedThread(lock, t); t.interrupt(); assertTrue(lock.isLocked()); assertTrue(lock.isHeldByCurrentThread()); - t.join(); + awaitTermination(t); + releaseLock(lock); } /** @@ -386,6 +461,18 @@ public class ReentrantLockTest extends J 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) {} } /** @@ -407,19 +494,26 @@ public class ReentrantLockTest extends J final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); - long t = c.awaitNanos(100); - assertTrue(t <= 0); + 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.unlock(); } /** - * timed await without a signal times out + * timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); - assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + assertFalse(c.await(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); } @@ -430,8 +524,11 @@ public class ReentrantLockTest extends J final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.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() + 10))); + assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); } @@ -439,22 +536,25 @@ public class ReentrantLockTest extends J * await returns when signalled */ public void testAwait() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + locked.countDown(); c.await(); lock.unlock(); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); lock.lock(); + assertHasWaiters(lock, c, t); c.signal(); + assertHasNoWaiters(lock, c); + assertTrue(t.isAlive()); lock.unlock(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); } /** @@ -479,7 +579,6 @@ public class ReentrantLockTest extends J } catch (NullPointerException success) {} } - /** * getWaitingThreads throws NPE if null */ @@ -491,9 +590,8 @@ public class ReentrantLockTest extends J } catch (NullPointerException success) {} } - /** - * hasWaiters throws IAE if not owned + * hasWaiters throws IllegalArgumentException if not owned */ public void testHasWaitersIAE() { final ReentrantLock lock = new ReentrantLock(); @@ -506,7 +604,7 @@ public class ReentrantLockTest extends J } /** - * hasWaiters throws IMSE if not locked + * hasWaiters throws IllegalMonitorStateException if not locked */ public void testHasWaitersIMSE() { final ReentrantLock lock = new ReentrantLock(); @@ -517,9 +615,8 @@ public class ReentrantLockTest extends J } catch (IllegalMonitorStateException success) {} } - /** - * getWaitQueueLength throws IAE if not owned + * getWaitQueueLength throws IllegalArgumentException if not owned */ public void testGetWaitQueueLengthIAE() { final ReentrantLock lock = new ReentrantLock(); @@ -532,7 +629,7 @@ public class ReentrantLockTest extends J } /** - * getWaitQueueLength throws IMSE if not locked + * getWaitQueueLength throws IllegalMonitorStateException if not locked */ public void testGetWaitQueueLengthIMSE() { final ReentrantLock lock = new ReentrantLock(); @@ -543,9 +640,8 @@ public class ReentrantLockTest extends J } catch (IllegalMonitorStateException success) {} } - /** - * getWaitingThreads throws IAE if not owned + * getWaitingThreads throws IllegalArgumentException if not owned */ public void testGetWaitingThreadsIAE() { final PublicReentrantLock lock = new PublicReentrantLock(); @@ -558,7 +654,7 @@ public class ReentrantLockTest extends J } /** - * getWaitingThreads throws IMSE if not locked + * getWaitingThreads throws IllegalMonitorStateException if not locked */ public void testGetWaitingThreadsIMSE() { final PublicReentrantLock lock = new PublicReentrantLock(); @@ -569,49 +665,51 @@ public class ReentrantLockTest extends J } catch (IllegalMonitorStateException success) {} } - /** * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + assertHasNoWaiters(lock, c); assertFalse(lock.hasWaiters(c)); - assertEquals(0, lock.getWaitQueueLength(c)); + locked.countDown(); c.await(); + assertHasNoWaiters(lock, c); + assertFalse(lock.hasWaiters(c)); lock.unlock(); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); lock.lock(); + assertHasWaiters(lock, c, t); assertTrue(lock.hasWaiters(c)); - assertEquals(1, lock.getWaitQueueLength(c)); c.signal(); - lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); - lock.lock(); + assertHasNoWaiters(lock, c); assertFalse(lock.hasWaiters(c)); - assertEquals(0, lock.getWaitQueueLength(c)); lock.unlock(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); + assertHasNoWaiters(lock, c); } /** * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); + final CountDownLatch locked1 = new CountDownLatch(1); + final CountDownLatch locked2 = new CountDownLatch(1); Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); + locked1.countDown(); c.await(); lock.unlock(); }}); @@ -621,28 +719,37 @@ public class ReentrantLockTest extends J lock.lock(); assertTrue(lock.hasWaiters(c)); assertEquals(1, lock.getWaitQueueLength(c)); + locked2.countDown(); c.await(); lock.unlock(); }}); + lock.lock(); + assertEquals(0, lock.getWaitQueueLength(c)); + lock.unlock(); + t1.start(); - Thread.sleep(SHORT_DELAY_MS); + locked1.await(); + + lock.lock(); + assertHasWaiters(lock, c, t1); + assertEquals(1, lock.getWaitQueueLength(c)); + lock.unlock(); + t2.start(); - Thread.sleep(SHORT_DELAY_MS); + locked2.await(); + lock.lock(); - assertTrue(lock.hasWaiters(c)); + assertHasWaiters(lock, c, t1, t2); assertEquals(2, lock.getWaitQueueLength(c)); c.signalAll(); + assertHasNoWaiters(lock, c); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); - lock.lock(); - assertFalse(lock.hasWaiters(c)); - assertEquals(0, lock.getWaitQueueLength(c)); - lock.unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); + + awaitTermination(t1); + awaitTermination(t2); + + assertHasNoWaiters(lock, c); } /** @@ -651,10 +758,13 @@ public class ReentrantLockTest extends J public void testGetWaitingThreads() throws InterruptedException { final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); + final CountDownLatch locked1 = new CountDownLatch(1); + final CountDownLatch locked2 = new CountDownLatch(1); Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); assertTrue(lock.getWaitingThreads(c).isEmpty()); + locked1.countDown(); c.await(); lock.unlock(); }}); @@ -663,6 +773,7 @@ public class ReentrantLockTest extends J public void realRun() throws InterruptedException { lock.lock(); assertFalse(lock.getWaitingThreads(c).isEmpty()); + locked2.countDown(); c.await(); lock.unlock(); }}); @@ -670,52 +781,33 @@ public class ReentrantLockTest extends J lock.lock(); assertTrue(lock.getWaitingThreads(c).isEmpty()); lock.unlock(); + t1.start(); - Thread.sleep(SHORT_DELAY_MS); + locked1.await(); + + lock.lock(); + assertHasWaiters(lock, c, t1); + assertTrue(lock.getWaitingThreads(c).contains(t1)); + assertFalse(lock.getWaitingThreads(c).contains(t2)); + assertEquals(1, lock.getWaitingThreads(c).size()); + lock.unlock(); + t2.start(); - Thread.sleep(SHORT_DELAY_MS); + locked2.await(); + lock.lock(); - assertTrue(lock.hasWaiters(c)); + assertHasWaiters(lock, c, t1, t2); assertTrue(lock.getWaitingThreads(c).contains(t1)); assertTrue(lock.getWaitingThreads(c).contains(t2)); + assertEquals(2, lock.getWaitingThreads(c).size()); c.signalAll(); + assertHasNoWaiters(lock, c); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); - lock.lock(); - assertFalse(lock.hasWaiters(c)); - assertTrue(lock.getWaitingThreads(c).isEmpty()); - lock.unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); - } - - /** A helper class for uninterruptible wait tests */ - class UninterruptibleThread extends Thread { - private ReentrantLock lock; - private Condition c; - - public volatile boolean canAwake = false; - public volatile boolean interrupted = false; - public volatile boolean lockStarted = false; - - public UninterruptibleThread(ReentrantLock lock, Condition c) { - this.lock = lock; - this.c = c; - } - public synchronized void run() { - lock.lock(); - lockStarted = true; - - while (!canAwake) { - c.awaitUninterruptibly(); - } + awaitTermination(t1); + awaitTermination(t2); - interrupted = isInterrupted(); - lock.unlock(); - } + assertHasNoWaiters(lock, c); } /** @@ -724,154 +816,200 @@ public class ReentrantLockTest extends J public void testAwaitUninterruptibly() throws InterruptedException { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - UninterruptibleThread thread = new UninterruptibleThread(lock, c); - - thread.start(); - - while (!thread.lockStarted) { - Thread.sleep(100); - } + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + lock.lock(); + locked.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.unlock(); + }}); + locked.await(); lock.lock(); - try { - thread.interrupt(); - thread.canAwake = true; - c.signal(); - } finally { - lock.unlock(); - } - - thread.join(); - assertTrue(thread.interrupted); - assertFalse(thread.isAlive()); + lock.unlock(); + t.interrupt(); + long timeoutMillis = 10; + assertThreadJoinTimesOut(t, timeoutMillis); + lock.lock(); + c.signal(); + lock.unlock(); + awaitTermination(t); } /** * await is interruptible */ public void testAwait_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - c.await(); + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + locked.countDown(); + try { + c.await(); + } finally { + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + lock.unlock(); + assertFalse(Thread.interrupted()); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + assertHasWaiters(lock, c, t); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); + assertFalse(lock.isLocked()); } /** * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + locked.countDown(); + try { + c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); + } finally { + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + lock.unlock(); + assertFalse(Thread.interrupted()); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + assertHasWaiters(lock, c, t); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); + assertFalse(lock.isLocked()); } /** * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + locked.countDown(); java.util.Date d = new java.util.Date(); - c.awaitUntil(new java.util.Date(d.getTime() + 10000)); + try { + c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)); + } finally { + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + assertHasNoWaiters(lock, c); + lock.unlock(); + assertFalse(Thread.interrupted()); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + assertHasWaiters(lock, c, t); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); + assertFalse(lock.isLocked()); } /** * signalAll wakes up all threads */ public void testSignalAll() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + final CountDownLatch locked = new CountDownLatch(2); + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + locked.countDown(); c.await(); lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + locked.countDown(); c.await(); lock.unlock(); }}); - t1.start(); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); lock.lock(); + assertHasWaiters(lock, c, t1, t2); c.signalAll(); + assertHasNoWaiters(lock, c); lock.unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); + awaitTermination(t1); + awaitTermination(t2); } /** * await after multiple reentrant locking preserves lock count */ public void testAwaitLockCount() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + final CountDownLatch locked = new CountDownLatch(2); + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + assertLockedBy(lock, Thread.currentThread()); assertEquals(1, lock.getHoldCount()); + locked.countDown(); c.await(); + assertLockedBy(lock, Thread.currentThread()); assertEquals(1, lock.getHoldCount()); lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); lock.lock(); + assertLockedBy(lock, Thread.currentThread()); assertEquals(2, lock.getHoldCount()); + locked.countDown(); c.await(); + assertLockedBy(lock, Thread.currentThread()); assertEquals(2, lock.getHoldCount()); lock.unlock(); lock.unlock(); }}); - t1.start(); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); lock.lock(); + assertHasWaiters(lock, c, t1, t2); + assertEquals(1, lock.getHoldCount()); c.signalAll(); + assertHasNoWaiters(lock, c); lock.unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); + awaitTermination(t1); + awaitTermination(t2); } /** @@ -908,5 +1046,4 @@ public class ReentrantLockTest extends J String ls = lock.toString(); assertTrue(ls.indexOf("Locked") >= 0); } - }