--- jsr166/src/test/tck/ReentrantLockTest.java 2003/12/27 19:26:43 1.12 +++ jsr166/src/test/tck/ReentrantLockTest.java 2003/12/28 21:56:18 1.13 @@ -57,6 +57,10 @@ public class ReentrantLockTest extends J public Collection getQueuedThreads() { return super.getQueuedThreads(); } + public Collection getWaitingThreads(Condition c) { + return super.getWaitingThreads(c); + } + } @@ -114,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() { @@ -419,7 +452,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 { @@ -448,17 +481,114 @@ public class ReentrantLockTest extends J } /** + * 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(); } @@ -472,14 +602,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()); @@ -494,13 +624,73 @@ 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(lock.hasWaiters(c)); + threadAssertEquals(0, lock.getWaitQueueLength(c)); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertTrue(lock.hasWaiters(c)); + threadAssertEquals(1, lock.getWaitQueueLength(c)); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + assertTrue(lock.hasWaiters(c)); + assertEquals(2, lock.getWaitQueueLength(c)); + c.signalAll(); + 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()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * 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(); - threadAssertFalse(c.hasWaiters()); - threadAssertEquals(0, c.getWaitQueueLength()); + threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.unlock(); } @@ -514,8 +704,7 @@ public class ReentrantLockTest extends J public void run() { try { lock.lock(); - threadAssertTrue(c.hasWaiters()); - threadAssertEquals(1, c.getWaitQueueLength()); + threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.unlock(); } @@ -526,19 +715,23 @@ public class ReentrantLockTest extends J }); 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(c.hasWaiters()); - assertEquals(2, c.getWaitQueueLength()); + 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(c.hasWaiters()); - assertEquals(0, c.getWaitQueueLength()); + assertFalse(lock.hasWaiters(c)); + assertTrue(lock.getWaitingThreads(c).isEmpty()); lock.unlock(); t1.join(SHORT_DELAY_MS); t2.join(SHORT_DELAY_MS); @@ -550,6 +743,8 @@ public class ReentrantLockTest extends J } } + + /** * awaitUninterruptibly doesn't abort on interrupt */