--- jsr166/src/test/tck/ReentrantLockTest.java 2003/12/29 19:05:40 1.15 +++ jsr166/src/test/tck/ReentrantLockTest.java 2005/05/03 16:02:00 1.22 @@ -176,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() { @@ -422,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) { @@ -439,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) { @@ -787,34 +863,61 @@ public class ReentrantLockTest extends J } } - + /** A helper class for uninterruptible wait tests */ + class UninterruptableThread extends Thread { + private ReentrantLock lock; + private Condition c; + + public volatile boolean canAwake = false; + public volatile boolean interrupted = false; + public volatile boolean lockStarted = false; + + public UninterruptableThread(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(); + } + } /** * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - lock.lock(); - c.awaitUninterruptibly(); - lock.unlock(); - } - }); + UninterruptableThread thread = new UninterruptableThread(lock, c); try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); + thread.start(); + + while (!thread.lockStarted) { + Thread.sleep(100); + } + lock.lock(); - c.signal(); - lock.unlock(); - assert(t.isInterrupted()); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); - } - catch (Exception ex) { + try { + thread.interrupt(); + thread.canAwake = true; + c.signal(); + } finally { + lock.unlock(); + } + + thread.join(); + assertTrue(thread.interrupted); + assertFalse(thread.isAlive()); + } catch (Exception ex) { unexpectedException(); } } @@ -860,7 +963,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(); } @@ -987,4 +1090,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); + } + }