--- jsr166/src/test/tck/ReentrantLockTest.java 2004/01/07 01:02:17 1.16 +++ jsr166/src/test/tck/ReentrantLockTest.java 2004/12/05 21:56:41 1.20 @@ -205,6 +205,53 @@ public class ReentrantLockTest extends J } /** + * 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() { @@ -451,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) { @@ -468,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) { @@ -889,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(); } @@ -1016,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); + } + }