--- jsr166/src/test/tck/ReentrantLockTest.java 2009/11/17 13:31:53 1.29 +++ jsr166/src/test/tck/ReentrantLockTest.java 2009/12/01 09:48:12 1.34 @@ -9,15 +9,16 @@ import junit.framework.*; import java.util.concurrent.locks.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.*; import java.io.*; public class ReentrantLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { - return new TestSuite(ReentrantLockTest.class); + return new TestSuite(ReentrantLockTest.class); } /** @@ -55,8 +56,6 @@ public class ReentrantLockTest extends J public Collection getWaitingThreads(Condition c) { return super.getWaitingThreads(c); } - - } /** @@ -72,7 +71,7 @@ public class ReentrantLockTest extends J * locking an unlocked lock succeeds */ public void testLock() { - ReentrantLock rl = new ReentrantLock(); + ReentrantLock rl = new ReentrantLock(); rl.lock(); assertTrue(rl.isLocked()); rl.unlock(); @@ -83,7 +82,7 @@ public class ReentrantLockTest extends J * locking an unlocked fair lock succeeds */ public void testFairLock() { - ReentrantLock rl = new ReentrantLock(true); + ReentrantLock rl = new ReentrantLock(true); rl.lock(); assertTrue(rl.isLocked()); rl.unlock(); @@ -93,18 +92,18 @@ public class ReentrantLockTest extends J * Unlocking an unlocked lock throws IllegalMonitorStateException */ public void testUnlock_IllegalMonitorStateException() { - ReentrantLock rl = new ReentrantLock(); - try { - rl.unlock(); - shouldThrow(); - } catch (IllegalMonitorStateException success) {} + ReentrantLock rl = new ReentrantLock(); + try { + rl.unlock(); + shouldThrow(); + } catch (IllegalMonitorStateException success) {} } /** * tryLock on an unlocked lock succeeds */ public void testTryLock() { - ReentrantLock rl = new ReentrantLock(); + ReentrantLock rl = new ReentrantLock(); assertTrue(rl.tryLock()); assertTrue(rl.isLocked()); rl.unlock(); @@ -115,7 +114,7 @@ public class ReentrantLockTest extends J * hasQueuedThreads reports whether there are waiting threads */ public void testhasQueuedThreads() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertFalse(lock.hasQueuedThreads()); @@ -140,7 +139,7 @@ public class ReentrantLockTest extends J * getQueueLength reports number of waiting threads */ public void testGetQueueLength() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertEquals(0, lock.getQueueLength()); @@ -165,7 +164,7 @@ public class ReentrantLockTest extends J * getQueueLength reports number of waiting threads */ public void testGetQueueLength_fair() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(true); + final ReentrantLock lock = new ReentrantLock(true); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertEquals(0, lock.getQueueLength()); @@ -190,7 +189,7 @@ public class ReentrantLockTest extends J * hasQueuedThread(null) throws NPE */ public void testHasQueuedThreadNPE() { - final ReentrantLock sync = new ReentrantLock(); + final ReentrantLock sync = new ReentrantLock(); try { sync.hasQueuedThread(null); shouldThrow(); @@ -201,7 +200,7 @@ public class ReentrantLockTest extends J * hasQueuedThread reports whether a thread is queued. */ public void testHasQueuedThread() throws InterruptedException { - final ReentrantLock sync = new ReentrantLock(); + final ReentrantLock sync = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(sync)); Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); assertFalse(sync.hasQueuedThread(t1)); @@ -232,7 +231,7 @@ public class ReentrantLockTest extends J * getQueuedThreads includes waiting threads */ public void testGetQueuedThreads() throws InterruptedException { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); assertTrue(lock.getQueuedThreads().isEmpty()); @@ -261,14 +260,15 @@ public class ReentrantLockTest extends J * timed tryLock is interruptible. */ public void testInterruptedException2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); + lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); }}); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -278,9 +278,9 @@ public class ReentrantLockTest extends J * TryLock on a locked lock fails */ public void testTryLockWhenLocked() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - Thread t = new Thread(new CheckedRunnable() { + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new CheckedRunnable() { public void realRun() { threadAssertFalse(lock.tryLock()); }}); @@ -294,11 +294,11 @@ public class ReentrantLockTest extends J * Timed tryLock on a locked lock times out */ public void testTryLock_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - Thread t = new Thread(new CheckedRunnable() { + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); + threadAssertFalse(lock.tryLock(1, MILLISECONDS)); }}); t.start(); @@ -310,15 +310,15 @@ public class ReentrantLockTest extends J * getHoldCount returns number of recursive holds */ public void testGetHoldCount() { - ReentrantLock lock = new ReentrantLock(); - for (int i = 1; i <= SIZE; i++) { - lock.lock(); - assertEquals(i, lock.getHoldCount()); - } - for (int i = SIZE; i > 0; i--) { - lock.unlock(); - assertEquals(i-1, lock.getHoldCount()); - } + ReentrantLock lock = new ReentrantLock(); + for (int i = 1; i <= SIZE; i++) { + lock.lock(); + assertEquals(i, lock.getHoldCount()); + } + for (int i = SIZE; i > 0; i--) { + lock.unlock(); + assertEquals(i-1, lock.getHoldCount()); + } } @@ -326,12 +326,12 @@ 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() { + 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); @@ -350,9 +350,9 @@ public class ReentrantLockTest extends J * lockInterruptibly is interruptible. */ public void testLockInterruptibly1() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); - lock.lock(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new InterruptedLockRunnable(lock)); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); @@ -365,10 +365,11 @@ public class ReentrantLockTest extends J * lockInterruptibly succeeds when unlocked, else is interruptible */ public void testLockInterruptibly2() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); lock.lockInterruptibly(); - Thread t = new Thread(new InterruptedLockRunnable(lock)); + Thread t = new Thread(new InterruptedLockRunnable(lock)); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); assertTrue(lock.isLocked()); assertTrue(lock.isHeldByCurrentThread()); @@ -379,7 +380,7 @@ public class ReentrantLockTest extends J * Calling await without holding lock throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.await(); @@ -391,7 +392,7 @@ public class ReentrantLockTest extends J * Calling signal without holding lock throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.signal(); @@ -403,7 +404,7 @@ public class ReentrantLockTest extends J * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); long t = c.awaitNanos(100); @@ -415,10 +416,10 @@ public class ReentrantLockTest extends J * timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); lock.unlock(); } @@ -426,7 +427,7 @@ public class ReentrantLockTest extends J * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); java.util.Date d = new java.util.Date(); @@ -438,9 +439,9 @@ public class ReentrantLockTest extends J * await returns when signalled */ public void testAwait() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); c.await(); @@ -460,7 +461,7 @@ public class ReentrantLockTest extends J * hasWaiters throws NPE if null */ public void testHasWaitersNPE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); try { lock.hasWaiters(null); shouldThrow(); @@ -471,7 +472,7 @@ public class ReentrantLockTest extends J * getWaitQueueLength throws NPE if null */ public void testGetWaitQueueLengthNPE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); try { lock.getWaitQueueLength(null); shouldThrow(); @@ -483,7 +484,7 @@ public class ReentrantLockTest extends J * getWaitingThreads throws NPE if null */ public void testGetWaitingThreadsNPE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); try { lock.getWaitingThreads(null); shouldThrow(); @@ -495,9 +496,9 @@ public class ReentrantLockTest extends J * hasWaiters throws IAE if not owned */ public void testHasWaitersIAE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - final ReentrantLock lock2 = new ReentrantLock(); + final ReentrantLock lock2 = new ReentrantLock(); try { lock2.hasWaiters(c); shouldThrow(); @@ -508,7 +509,7 @@ public class ReentrantLockTest extends J * hasWaiters throws IMSE if not locked */ public void testHasWaitersIMSE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.hasWaiters(c); @@ -521,9 +522,9 @@ public class ReentrantLockTest extends J * getWaitQueueLength throws IAE if not owned */ public void testGetWaitQueueLengthIAE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - final ReentrantLock lock2 = new ReentrantLock(); + final ReentrantLock lock2 = new ReentrantLock(); try { lock2.getWaitQueueLength(c); shouldThrow(); @@ -534,7 +535,7 @@ public class ReentrantLockTest extends J * getWaitQueueLength throws IMSE if not locked */ public void testGetWaitQueueLengthIMSE() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.getWaitQueueLength(c); @@ -547,9 +548,9 @@ public class ReentrantLockTest extends J * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - final PublicReentrantLock lock2 = new PublicReentrantLock(); + final PublicReentrantLock lock2 = new PublicReentrantLock(); try { lock2.getWaitingThreads(c); shouldThrow(); @@ -560,7 +561,7 @@ public class ReentrantLockTest extends J * getWaitingThreads throws IMSE if not locked */ public void testGetWaitingThreadsIMSE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); try { lock.getWaitingThreads(c); @@ -573,9 +574,9 @@ public class ReentrantLockTest extends J * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertFalse(lock.hasWaiters(c)); @@ -604,9 +605,9 @@ public class ReentrantLockTest extends J * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertFalse(lock.hasWaiters(c)); @@ -615,7 +616,7 @@ public class ReentrantLockTest extends J lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertTrue(lock.hasWaiters(c)); @@ -648,9 +649,9 @@ public class ReentrantLockTest extends J * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() throws InterruptedException { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); @@ -658,7 +659,7 @@ public class ReentrantLockTest extends J lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); @@ -749,10 +750,10 @@ public class ReentrantLockTest extends J * await is interruptible */ public void testAwait_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { lock.lock(); c.await(); }}); @@ -768,12 +769,12 @@ public class ReentrantLockTest extends J * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { lock.lock(); - c.awaitNanos(1000 * 1000 * 1000); // 1 sec + c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }}); t.start(); @@ -787,10 +788,10 @@ public class ReentrantLockTest extends J * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { lock.lock(); java.util.Date d = new java.util.Date(); c.awaitUntil(new java.util.Date(d.getTime() + 10000)); @@ -807,16 +808,16 @@ public class ReentrantLockTest extends J * signalAll wakes up all threads */ public void testSignalAll() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); c.await(); lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); c.await(); @@ -839,9 +840,9 @@ public class ReentrantLockTest extends J * await after multiple reentrant locking preserves lock count */ public void testAwaitLockCount() throws InterruptedException { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); threadAssertEquals(1, lock.getHoldCount()); @@ -850,7 +851,7 @@ public class ReentrantLockTest extends J lock.unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); lock.lock();