--- jsr166/src/test/tck/ReentrantLockTest.java 2011/03/15 19:47:07 1.39 +++ jsr166/src/test/tck/ReentrantLockTest.java 2011/05/24 23:40:14 1.49 @@ -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,146 @@ public class ReentrantLockTest extends J } /** - * Constructor sets given fairness + * Releases write lock, checking that it had a hold count of 1. */ - public void testConstructor() { - assertFalse(new ReentrantLock().isFair()); - assertFalse(new ReentrantLock(false).isFair()); - assertTrue(new ReentrantLock(true).isFair()); + void releaseLock(PublicReentrantLock lock) { + assertLockedByMoi(lock); + lock.unlock(); + assertFalse(lock.isHeldByCurrentThread()); + assertNotLocked(lock); } /** - * locking an unlocked lock succeeds + * 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 AssertionFailedError("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 lock is locked by the current thread. + */ + void assertLockedByMoi(PublicReentrantLock lock) { + assertLockedBy(lock, Thread.currentThread()); + } + + /** + * 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(); + } + + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + + /** + * Awaits condition using the specified AwaitMethod. */ - public void testLock() { - ReentrantLock rl = new ReentrantLock(); - rl.lock(); - assertTrue(rl.isLocked()); - rl.unlock(); - assertFalse(rl.isLocked()); + void await(Condition c, AwaitMethod awaitMethod) + throws InterruptedException { + long timeoutMillis = 2 * LONG_DELAY_MS; + switch (awaitMethod) { + case await: + c.await(); + break; + case awaitTimed: + assertTrue(c.await(timeoutMillis, MILLISECONDS)); + break; + case awaitNanos: + long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(nanosTimeout); + assertTrue(nanosRemaining > 0); + break; + case awaitUntil: + assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); + break; + } } /** - * locking an unlocked fair lock succeeds - */ - public void testFairLock() { - ReentrantLock rl = new ReentrantLock(true); - rl.lock(); - assertTrue(rl.isLocked()); - rl.unlock(); + * Constructor sets given fairness, and is in unlocked state + */ + public void testConstructor() { + 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() { testLock(false); } + public void testLock_fair() { testLock(true); } + public void testLock(boolean fair) { + PublicReentrantLock lock = new PublicReentrantLock(fair); + lock.lock(); + assertLockedByMoi(lock); + releaseLock(lock); } /** * Unlocking an unlocked lock throws IllegalMonitorStateException */ - public void testUnlock_IllegalMonitorStateException() { - ReentrantLock rl = new ReentrantLock(); + public void testUnlock_IMSE() { testUnlock_IMSE(false); } + public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } + public void testUnlock_IMSE(boolean fair) { + ReentrantLock lock = new ReentrantLock(fair); try { - rl.unlock(); + lock.unlock(); shouldThrow(); } catch (IllegalMonitorStateException success) {} } @@ -102,215 +209,206 @@ 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(); + public void testTryLock() { testTryLock(false); } + public void testTryLock_fair() { testTryLock(true); } + public void testTryLock(boolean fair) { + PublicReentrantLock lock = new PublicReentrantLock(fair); + assertTrue(lock.tryLock()); + assertLockedByMoi(lock); + assertTrue(lock.tryLock()); + assertLockedByMoi(lock); + lock.unlock(); + releaseLock(lock); } - /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testHasQueuedThreads() { testHasQueuedThreads(false); } + public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); } + public void testHasQueuedThreads(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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(); + public void testGetQueueLength() { testGetQueueLength(false); } + public void testGetQueueLength_fair() { testGetQueueLength(true); } + public void testGetQueueLength(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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); - 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); - assertEquals(1, lock.getQueueLength()); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(2, lock.getQueueLength()); - t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(1, lock.getQueueLength()); - lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(0, lock.getQueueLength()); - t1.join(); - t2.join(); } /** * hasQueuedThread(null) throws NPE */ - public void testHasQueuedThreadNPE() { - final ReentrantLock sync = new ReentrantLock(); + public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); } + public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); } + public void testHasQueuedThreadNPE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); try { - sync.hasQueuedThread(null); + lock.hasQueuedThread(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * hasQueuedThread reports whether a thread is queued. + * 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(); + public void testHasQueuedThread() { testHasQueuedThread(false); } + public void testHasQueuedThread_fair() { testHasQueuedThread(true); } + public void testHasQueuedThread(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); + 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 */ - public void testGetQueuedThreads() throws InterruptedException { - final PublicReentrantLock lock = new PublicReentrantLock(); + public void testGetQueuedThreads() { testGetQueuedThreads(false); } + public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); } + public void testGetQueuedThreads(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertTrue(lock.getQueuedThreads().isEmpty()); 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. + * timed tryLock is interruptible */ - public void testInterruptedException2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } + public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } + public void testTryLock_Interruptible(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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(); + public void testTryLockWhenLocked() { testTryLockWhenLocked(false); } + public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); } + public void testTryLockWhenLocked(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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(); + public void testTryLock_Timeout() { testTryLock_Timeout(false); } + public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } + public void testTryLock_Timeout(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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); } /** * getHoldCount returns number of recursive holds */ - public void testGetHoldCount() { - ReentrantLock lock = new ReentrantLock(); + public void testGetHoldCount() { testGetHoldCount(false); } + public void testGetHoldCount_fair() { testGetHoldCount(true); } + public void testGetHoldCount(boolean fair) { + ReentrantLock lock = new ReentrantLock(fair); for (int i = 1; i <= SIZE; i++) { lock.lock(); assertEquals(i, lock.getHoldCount()); @@ -321,78 +419,91 @@ public class ReentrantLockTest extends J } } - /** * isLocked is true when locked and false when not */ - public void testIsLocked() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - assertTrue(lock.isLocked()); - lock.unlock(); - assertFalse(lock.isLocked()); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - lock.lock(); - Thread.sleep(SMALL_DELAY_MS); - lock.unlock(); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.isLocked()); - t.join(); - assertFalse(lock.isLocked()); - } - - - /** - * lockInterruptibly is interruptible. - */ - public void testLockInterruptibly1() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - lock.unlock(); - t.join(); + public void testIsLocked() { testIsLocked(false); } + public void testIsLocked_fair() { testIsLocked(true); } + public void testIsLocked(boolean fair) { + try { + final ReentrantLock lock = new ReentrantLock(fair); + assertFalse(lock.isLocked()); + lock.lock(); + assertTrue(lock.isLocked()); + lock.lock(); + assertTrue(lock.isLocked()); + lock.unlock(); + assertTrue(lock.isLocked()); + lock.unlock(); + assertFalse(lock.isLocked()); + final CyclicBarrier barrier = new CyclicBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws Exception { + lock.lock(); + assertTrue(lock.isLocked()); + barrier.await(); + barrier.await(); + lock.unlock(); + }}); + + barrier.await(); + assertTrue(lock.isLocked()); + barrier.await(); + awaitTermination(t); + assertFalse(lock.isLocked()); + } catch (Exception e) { + threadUnexpectedException(e); + } } /** * lockInterruptibly succeeds when unlocked, else is interruptible */ - public void testLockInterruptibly2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lockInterruptibly(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + public void testLockInterruptibly() { testLockInterruptibly(false); } + public void testLockInterruptibly_fair() { testLockInterruptibly(true); } + public void testLockInterruptibly(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); + try { + lock.lockInterruptibly(); + } catch (InterruptedException ie) { + threadUnexpectedException(ie); + } + assertLockedByMoi(lock); + Thread t = newStartedThread(new InterruptedLockRunnable(lock)); + waitForQueuedThread(lock, t); t.interrupt(); assertTrue(lock.isLocked()); assertTrue(lock.isHeldByCurrentThread()); - t.join(); + awaitTermination(t); + releaseLock(lock); } /** * Calling await without holding lock throws IllegalMonitorStateException */ - public void testAwait_IllegalMonitor() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - final Condition c = lock.newCondition(); - try { - c.await(); - 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 ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + for (AwaitMethod awaitMethod : AwaitMethod.values()) { + long startTime = System.nanoTime(); + try { + await(c, awaitMethod); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (InterruptedException e) { threadUnexpectedException(e); } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } } /** * Calling signal without holding lock throws IllegalMonitorStateException */ - public void testSignal_IllegalMonitor() { - final ReentrantLock lock = new ReentrantLock(); + public void testSignal_IMSE() { testSignal_IMSE(false); } + public void testSignal_IMSE_fair() { testSignal_IMSE(true); } + public void testSignal_IMSE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); try { c.signal(); @@ -403,65 +514,100 @@ public class ReentrantLockTest extends J /** * awaitNanos without a signal times out */ - public void testAwaitNanos_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - final Condition c = lock.newCondition(); - lock.lock(); - long t = c.awaitNanos(100); - assertTrue(t <= 0); - lock.unlock(); + public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } + public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } + public void testAwaitNanos_Timeout(boolean fair) { + try { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + lock.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.unlock(); + } catch (InterruptedException e) { + threadUnexpectedException(e); + } } /** * 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)); - lock.unlock(); + public void testAwait_Timeout() { testAwait_Timeout(false); } + public void testAwait_Timeout_fair() { testAwait_Timeout(true); } + public void testAwait_Timeout(boolean fair) { + try { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + lock.lock(); + long startTime = System.nanoTime(); + long timeoutMillis = 10; + assertFalse(c.await(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); + } catch (InterruptedException e) { + threadUnexpectedException(e); + } } /** * awaitUntil without a signal times out */ - public void testAwaitUntil_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - final Condition c = lock.newCondition(); - lock.lock(); - java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); - lock.unlock(); + public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } + public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } + public void testAwaitUntil_Timeout(boolean fair) { + try { + final ReentrantLock lock = new ReentrantLock(fair); + 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() + timeoutMillis))); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); + } catch (InterruptedException e) { + threadUnexpectedException(e); + } } /** * await returns when signalled */ - public void testAwait() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testAwait() { testAwait(false); } + public void testAwait_fair() { testAwait(true); } + public void testAwait(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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); + await(locked); 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); } /** * hasWaiters throws NPE if null */ - public void testHasWaitersNPE() { - final ReentrantLock lock = new ReentrantLock(); + public void testHasWaitersNPE() { testHasWaitersNPE(false); } + public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); } + public void testHasWaitersNPE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); try { lock.hasWaiters(null); shouldThrow(); @@ -471,34 +617,38 @@ public class ReentrantLockTest extends J /** * getWaitQueueLength throws NPE if null */ - public void testGetWaitQueueLengthNPE() { - final ReentrantLock lock = new ReentrantLock(); + public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); } + public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); } + public void testGetWaitQueueLengthNPE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); try { lock.getWaitQueueLength(null); shouldThrow(); } catch (NullPointerException success) {} } - /** * getWaitingThreads throws NPE if null */ - public void testGetWaitingThreadsNPE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); } + public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); } + public void testGetWaitingThreadsNPE(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); try { lock.getWaitingThreads(null); shouldThrow(); } catch (NullPointerException success) {} } - /** - * hasWaiters throws IAE if not owned + * hasWaiters throws IllegalArgumentException if not owned */ - public void testHasWaitersIAE() { - final ReentrantLock lock = new ReentrantLock(); + public void testHasWaitersIAE() { testHasWaitersIAE(false); } + public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); } + public void testHasWaitersIAE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); - final ReentrantLock lock2 = new ReentrantLock(); + final ReentrantLock lock2 = new ReentrantLock(fair); try { lock2.hasWaiters(c); shouldThrow(); @@ -506,10 +656,12 @@ 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(); + public void testHasWaitersIMSE() { testHasWaitersIMSE(false); } + public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); } + public void testHasWaitersIMSE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); try { lock.hasWaiters(c); @@ -517,14 +669,15 @@ 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(); + public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); } + public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); } + public void testGetWaitQueueLengthIAE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); - final ReentrantLock lock2 = new ReentrantLock(); + final ReentrantLock lock2 = new ReentrantLock(fair); try { lock2.getWaitQueueLength(c); shouldThrow(); @@ -532,10 +685,12 @@ 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(); + public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); } + public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); } + public void testGetWaitQueueLengthIMSE(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); try { lock.getWaitQueueLength(c); @@ -543,14 +698,15 @@ 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(); + public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); } + public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); } + public void testGetWaitingThreadsIAE(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - final PublicReentrantLock lock2 = new PublicReentrantLock(); + final PublicReentrantLock lock2 = new PublicReentrantLock(fair); try { lock2.getWaitingThreads(c); shouldThrow(); @@ -558,10 +714,12 @@ 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(); + public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); } + public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); } + public void testGetWaitingThreadsIMSE(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); try { lock.getWaitingThreads(c); @@ -569,49 +727,55 @@ 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(); + public void testHasWaiters() { testHasWaiters(false); } + public void testHasWaiters_fair() { testHasWaiters(true); } + public void testHasWaiters(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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); + await(locked); 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(); + public void testGetWaitQueueLength() { testGetWaitQueueLength(false); } + public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); } + public void testGetWaitQueueLength(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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,40 +785,54 @@ 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); + await(locked1); + + lock.lock(); + assertHasWaiters(lock, c, t1); + assertEquals(1, lock.getWaitQueueLength(c)); + lock.unlock(); + t2.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked2); + 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); } /** * getWaitingThreads returns only and all waiting threads */ - public void testGetWaitingThreads() throws InterruptedException { - final PublicReentrantLock lock = new PublicReentrantLock(); + public void testGetWaitingThreads() { testGetWaitingThreads(false); } + public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); } + public void testGetWaitingThreads(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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 +841,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,243 +849,271 @@ public class ReentrantLockTest extends J lock.lock(); assertTrue(lock.getWaitingThreads(c).isEmpty()); lock.unlock(); + t1.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked1); + + 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); + await(locked2); + 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; + awaitTermination(t1); + awaitTermination(t2); - 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(); - } - - interrupted = isInterrupted(); - lock.unlock(); - } + assertHasNoWaiters(lock, c); } /** * awaitUninterruptibly doesn't abort on interrupt */ - 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); - } - - lock.lock(); - try { - thread.interrupt(); - thread.canAwake = true; - c.signal(); - } finally { - lock.unlock(); - } - - thread.join(); - assertTrue(thread.interrupted); - assertFalse(thread.isAlive()); - } - - /** - * await is interruptible - */ - public void testAwait_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } + public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } + public void testAwaitUninterruptibly(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { lock.lock(); - c.await(); + locked.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.unlock(); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked); + lock.lock(); + lock.unlock(); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + long timeoutMillis = 10; + assertThreadStaysAlive(t, timeoutMillis); + lock.lock(); + c.signal(); + lock.unlock(); + awaitTermination(t); } /** - * awaitNanos is interruptible + * await/awaitNanos/awaitUntil is interruptible */ - public void testAwaitNanos_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + 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 PublicReentrantLock lock = + new PublicReentrantLock(fair); 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)); + assertLockedByMoi(lock); + assertHasNoWaiters(lock, c); + locked.countDown(); + try { + await(c, awaitMethod); + } finally { + assertLockedByMoi(lock); + assertHasNoWaiters(lock, c); + lock.unlock(); + assertFalse(Thread.interrupted()); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked); + assertHasWaiters(lock, c, t); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); + assertNotLocked(lock); } /** - * awaitUntil is interruptible + * signalAll wakes up all threads */ - public void testAwaitUntil_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + 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 PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(2); + class Awaiter extends CheckedRunnable { public void realRun() throws InterruptedException { lock.lock(); - java.util.Date d = new java.util.Date(); - c.awaitUntil(new java.util.Date(d.getTime() + 10000)); - }}); + locked.countDown(); + await(c, awaitMethod); + lock.unlock(); + } + } - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + Thread t1 = newStartedThread(new Awaiter()); + Thread t2 = newStartedThread(new Awaiter()); + + await(locked); + lock.lock(); + assertHasWaiters(lock, c, t1, t2); + c.signalAll(); + assertHasNoWaiters(lock, c); + lock.unlock(); + awaitTermination(t1); + awaitTermination(t2); } /** - * signalAll wakes up all threads + * signal wakes up waiting threads in FIFO order */ - public void testSignalAll() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + public void testSignalWakesFifo() { testSignalWakesFifo(false); } + public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } + public void testSignalWakesFifo(boolean fair) { + final PublicReentrantLock lock = + new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + final CountDownLatch locked1 = new CountDownLatch(1); + final CountDownLatch locked2 = new CountDownLatch(1); + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + locked1.countDown(); c.await(); lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + await(locked1); + + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); + locked2.countDown(); c.await(); lock.unlock(); }}); - t1.start(); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked2); + lock.lock(); - c.signalAll(); + assertHasWaiters(lock, c, t1, t2); + assertFalse(lock.hasQueuedThreads()); + c.signal(); + assertHasWaiters(lock, c, t2); + assertTrue(lock.hasQueuedThread(t1)); + assertFalse(lock.hasQueuedThread(t2)); + c.signal(); + assertHasNoWaiters(lock, c); + assertTrue(lock.hasQueuedThread(t1)); + assertTrue(lock.hasQueuedThread(t2)); 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(); + public void testAwaitLockCount() { testAwaitLockCount(false); } + public void testAwaitLockCount_fair() { testAwaitLockCount(true); } + public void testAwaitLockCount(boolean fair) { + final PublicReentrantLock lock = new PublicReentrantLock(fair); 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(); + assertLockedByMoi(lock); assertEquals(1, lock.getHoldCount()); + locked.countDown(); c.await(); + assertLockedByMoi(lock); 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(); + assertLockedByMoi(lock); assertEquals(2, lock.getHoldCount()); + locked.countDown(); c.await(); + assertLockedByMoi(lock); assertEquals(2, lock.getHoldCount()); lock.unlock(); lock.unlock(); }}); - t1.start(); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); + await(locked); 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); } /** * A serialized lock deserializes as unlocked */ - public void testSerialization() throws Exception { - ReentrantLock l = new ReentrantLock(); - l.lock(); - l.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)); - ReentrantLock r = (ReentrantLock) in.readObject(); - r.lock(); - r.unlock(); + public void testSerialization() { testSerialization(false); } + public void testSerialization_fair() { testSerialization(true); } + public void testSerialization(boolean fair) { + ReentrantLock lock = new ReentrantLock(fair); + lock.lock(); + + ReentrantLock clone = serialClone(lock); + assertEquals(lock.isFair(), clone.isFair()); + assertTrue(lock.isLocked()); + assertFalse(clone.isLocked()); + assertEquals(1, lock.getHoldCount()); + assertEquals(0, clone.getHoldCount()); + clone.lock(); + clone.lock(); + assertTrue(clone.isLocked()); + assertEquals(2, clone.getHoldCount()); + assertEquals(1, lock.getHoldCount()); + clone.unlock(); + clone.unlock(); + assertTrue(lock.isLocked()); + assertFalse(clone.isLocked()); } /** * toString indicates current lock state */ - public void testToString() { - ReentrantLock lock = new ReentrantLock(); - String us = lock.toString(); - assertTrue(us.indexOf("Unlocked") >= 0); + public void testToString() { testToString(false); } + public void testToString_fair() { testToString(true); } + public void testToString(boolean fair) { + ReentrantLock lock = new ReentrantLock(fair); + assertTrue(lock.toString().contains("Unlocked")); lock.lock(); - String ls = lock.toString(); - assertTrue(ls.indexOf("Locked") >= 0); + assertTrue(lock.toString().contains("Locked")); + lock.unlock(); + assertTrue(lock.toString().contains("Unlocked")); } - }