--- jsr166/src/test/tck/ReentrantLockTest.java 2003/12/27 18:27:02 1.11 +++ jsr166/src/test/tck/ReentrantLockTest.java 2004/12/05 21:56:41 1.20 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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 + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -56,6 +57,10 @@ public class ReentrantLockTest extends J public Collection getQueuedThreads() { return super.getQueuedThreads(); } + public Collection getWaitingThreads(Condition c) { + return super.getWaitingThreads(c); + } + } @@ -102,7 +107,7 @@ public class ReentrantLockTest extends J } /** - * trylock on an unlocked lock succeeds + * tryLock on an unlocked lock succeeds */ public void testTryLock() { ReentrantLock rl = new ReentrantLock(); @@ -113,6 +118,35 @@ public class ReentrantLockTest extends J /** + * hasQueuedThreads reports whether there are waiting threads + */ + public void testhasQueuedThreads() { + final ReentrantLock lock = new ReentrantLock(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + assertFalse(lock.hasQueuedThreads()); + lock.lock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasQueuedThreads()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasQueuedThreads()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasQueuedThreads()); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(lock.hasQueuedThreads()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** * getQueueLength reports number of waiting threads */ public void testGetQueueLength() { @@ -142,6 +176,82 @@ public class ReentrantLockTest extends J } /** + * getQueueLength reports number of waiting threads + */ + public void testGetQueueLength_fair() { + final ReentrantLock lock = new ReentrantLock(true); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + 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(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * hasQueuedThread(null) throws NPE + */ + public void testHasQueuedThreadNPE() { + final ReentrantLock sync = new ReentrantLock(); + try { + sync.hasQueuedThread(null); + shouldThrow(); + } catch (NullPointerException success) { + } + } + + /** + * hasQueuedThread reports whether a thread is queued. + */ + public void testHasQueuedThread() { + final ReentrantLock sync = new ReentrantLock(); + Thread t1 = new Thread(new InterruptedLockRunnable(sync)); + Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); + try { + assertFalse(sync.hasQueuedThread(t1)); + assertFalse(sync.hasQueuedThread(t2)); + sync.lock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasQueuedThread(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasQueuedThread(t1)); + assertTrue(sync.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(); + } catch(Exception e){ + unexpectedException(); + } + } + + + /** * getQueuedThreads includes waiting threads */ public void testGetQueuedThreads() { @@ -175,7 +285,7 @@ public class ReentrantLockTest extends J /** - * timed trylock is interruptible. + * timed tryLock is interruptible. */ public void testInterruptedException2() { final ReentrantLock lock = new ReentrantLock(); @@ -198,7 +308,7 @@ public class ReentrantLockTest extends J /** - * Trylock on a locked lock fails + * TryLock on a locked lock fails */ public void testTryLockWhenLocked() { final ReentrantLock lock = new ReentrantLock(); @@ -218,7 +328,7 @@ public class ReentrantLockTest extends J } /** - * Timed trylock on a locked lock times out + * Timed tryLock on a locked lock times out */ public void testTryLock_Timeout() { final ReentrantLock lock = new ReentrantLock(); @@ -388,7 +498,7 @@ public class ReentrantLockTest extends J final Condition c = lock.newCondition(); try { lock.lock(); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); lock.unlock(); } catch (Exception ex) { @@ -405,7 +515,7 @@ public class ReentrantLockTest extends J try { lock.lock(); java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); + c.awaitUntil(new java.util.Date(d.getTime() + 10)); lock.unlock(); } catch (Exception ex) { @@ -418,7 +528,7 @@ public class ReentrantLockTest extends J */ public void testAwait() { final ReentrantLock lock = new ReentrantLock(); - final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); + final Condition c = lock.newCondition(); Thread t = new Thread(new Runnable() { public void run() { try { @@ -447,17 +557,158 @@ public class ReentrantLockTest extends J } /** + * hasWaiters throws NPE if null + */ + public void testHasWaitersNPE() { + final ReentrantLock lock = new ReentrantLock(); + try { + lock.hasWaiters(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitQueueLength throws NPE if null + */ + public void testGetWaitQueueLengthNPE() { + final ReentrantLock lock = new ReentrantLock(); + try { + lock.getWaitQueueLength(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * getWaitingThreads throws NPE if null + */ + public void testGetWaitingThreadsNPE() { + final PublicReentrantLock lock = new PublicReentrantLock(); + try { + lock.getWaitingThreads(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * hasWaiters throws IAE if not owned + */ + public void testHasWaitersIAE() { + final ReentrantLock lock = new ReentrantLock(); + final Condition c = (lock.newCondition()); + final ReentrantLock lock2 = new ReentrantLock(); + try { + lock2.hasWaiters(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * hasWaiters throws IMSE if not locked + */ + public void testHasWaitersIMSE() { + final ReentrantLock lock = new ReentrantLock(); + final Condition c = (lock.newCondition()); + try { + lock.hasWaiters(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * getWaitQueueLength throws IAE if not owned + */ + public void testGetWaitQueueLengthIAE() { + final ReentrantLock lock = new ReentrantLock(); + final Condition c = (lock.newCondition()); + final ReentrantLock lock2 = new ReentrantLock(); + try { + lock2.getWaitQueueLength(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitQueueLength throws IMSE if not locked + */ + public void testGetWaitQueueLengthIMSE() { + final ReentrantLock lock = new ReentrantLock(); + final Condition c = (lock.newCondition()); + try { + lock.getWaitQueueLength(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * getWaitingThreads throws IAE if not owned + */ + public void testGetWaitingThreadsIAE() { + final PublicReentrantLock lock = new PublicReentrantLock(); + final Condition c = (lock.newCondition()); + final PublicReentrantLock lock2 = new PublicReentrantLock(); + try { + lock2.getWaitingThreads(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitingThreads throws IMSE if not locked + */ + public void testGetWaitingThreadsIMSE() { + final PublicReentrantLock lock = new PublicReentrantLock(); + final Condition c = (lock.newCondition()); + try { + lock.getWaitingThreads(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + + /** * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() { final ReentrantLock lock = new ReentrantLock(); - final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); + final Condition c = lock.newCondition(); Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); - threadAssertFalse(c.hasWaiters()); - threadAssertEquals(0, c.getWaitQueueLength()); + threadAssertFalse(lock.hasWaiters(c)); + threadAssertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); } @@ -471,14 +722,14 @@ public class ReentrantLockTest extends J t.start(); Thread.sleep(SHORT_DELAY_MS); lock.lock(); - assertTrue(c.hasWaiters()); - assertEquals(1, c.getWaitQueueLength()); + assertTrue(lock.hasWaiters(c)); + assertEquals(1, lock.getWaitQueueLength(c)); c.signal(); lock.unlock(); Thread.sleep(SHORT_DELAY_MS); lock.lock(); - assertFalse(c.hasWaiters()); - assertEquals(0, c.getWaitQueueLength()); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); lock.unlock(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -493,13 +744,13 @@ public class ReentrantLockTest extends J */ public void testGetWaitQueueLength() { final ReentrantLock lock = new ReentrantLock(); - final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); + final Condition c = lock.newCondition(); Thread t1 = new Thread(new Runnable() { public void run() { try { lock.lock(); - threadAssertFalse(c.hasWaiters()); - threadAssertEquals(0, c.getWaitQueueLength()); + threadAssertFalse(lock.hasWaiters(c)); + threadAssertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); } @@ -513,8 +764,8 @@ public class ReentrantLockTest extends J public void run() { try { lock.lock(); - threadAssertTrue(c.hasWaiters()); - threadAssertEquals(1, c.getWaitQueueLength()); + threadAssertTrue(lock.hasWaiters(c)); + threadAssertEquals(1, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); } @@ -530,14 +781,14 @@ public class ReentrantLockTest extends J t2.start(); Thread.sleep(SHORT_DELAY_MS); lock.lock(); - assertTrue(c.hasWaiters()); - assertEquals(2, c.getWaitQueueLength()); + assertTrue(lock.hasWaiters(c)); + assertEquals(2, lock.getWaitQueueLength(c)); c.signalAll(); lock.unlock(); Thread.sleep(SHORT_DELAY_MS); lock.lock(); - assertFalse(c.hasWaiters()); - assertEquals(0, c.getWaitQueueLength()); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); lock.unlock(); t1.join(SHORT_DELAY_MS); t2.join(SHORT_DELAY_MS); @@ -550,6 +801,71 @@ public class ReentrantLockTest extends J } /** + * getWaitingThreads returns only and all waiting threads + */ + public void testGetWaitingThreads() { + final PublicReentrantLock lock = new PublicReentrantLock(); + final Condition c = lock.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + lock.lock(); + assertTrue(lock.getWaitingThreads(c).isEmpty()); + lock.unlock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + assertTrue(lock.hasWaiters(c)); + assertTrue(lock.getWaitingThreads(c).contains(t1)); + assertTrue(lock.getWaitingThreads(c).contains(t2)); + c.signalAll(); + 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()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + + + /** * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { @@ -620,7 +936,7 @@ public class ReentrantLockTest extends J public void run() { try { lock.lock(); - c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); + c.awaitNanos(1000 * 1000 * 1000); // 1 sec lock.unlock(); threadShouldThrow(); } @@ -747,4 +1063,16 @@ public class ReentrantLockTest extends J } } + /** + * toString indicates current lock state + */ + public void testToString() { + ReentrantLock lock = new ReentrantLock(); + String us = lock.toString(); + assertTrue(us.indexOf("Unlocked") >= 0); + lock.lock(); + String ls = lock.toString(); + assertTrue(ls.indexOf("Locked") >= 0); + } + }