--- jsr166/src/test/tck/ReentrantLockTest.java 2003/09/06 19:36:05 1.2 +++ jsr166/src/test/tck/ReentrantLockTest.java 2003/12/27 19:26:43 1.12 @@ -1,104 +1,266 @@ /* - * 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.*; import java.util.concurrent.locks.*; import java.util.concurrent.*; +import java.util.*; +import java.io.*; -public class ReentrantLockTest extends TestCase { - static int HOLD_COUNT_TEST_LIMIT = 20; - +public class ReentrantLockTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } - public static Test suite() { return new TestSuite(ReentrantLockTest.class); } - private static long SHORT_DELAY_MS = 100; - private static long MEDIUM_DELAY_MS = 1000; - private static long LONG_DELAY_MS = 10000; - - /* - * Unlocks an unlocked lock, throws Illegal Monitor State - * + /** + * A runnable calling lockInterruptibly */ - - public void testIllegalMonitorStateException(){ + class InterruptibleLockRunnable implements Runnable { + final ReentrantLock lock; + InterruptibleLockRunnable(ReentrantLock l) { lock = l; } + public void run() { + try { + lock.lockInterruptibly(); + } catch(InterruptedException success){} + } + } + + + /** + * A runnable calling lockInterruptibly that expects to be + * interrupted + */ + class InterruptedLockRunnable implements Runnable { + final ReentrantLock lock; + InterruptedLockRunnable(ReentrantLock l) { lock = l; } + public void run() { + try { + lock.lockInterruptibly(); + threadShouldThrow(); + } catch(InterruptedException success){} + } + } + + /** + * Subclass to expose protected methods + */ + static class PublicReentrantLock extends ReentrantLock { + PublicReentrantLock() { super(); } + public Collection getQueuedThreads() { + return super.getQueuedThreads(); + } + + } + + /** + * Constructor sets given fairness + */ + public void testConstructor() { ReentrantLock rl = new ReentrantLock(); - try{ - rl.unlock(); - fail("Should of thown Illegal Monitor State Exception"); + assertFalse(rl.isFair()); + ReentrantLock r2 = new ReentrantLock(true); + assertTrue(r2.isFair()); + } + + /** + * locking an unlocked lock succeeds + */ + public void testLock() { + ReentrantLock rl = new ReentrantLock(); + rl.lock(); + assertTrue(rl.isLocked()); + rl.unlock(); + } - }catch(IllegalMonitorStateException sucess){} + /** + * locking an unlocked fair lock succeeds + */ + public void testFairLock() { + ReentrantLock rl = new ReentrantLock(true); + rl.lock(); + assertTrue(rl.isLocked()); + rl.unlock(); + } + + /** + * Unlocking an unlocked lock throws IllegalMonitorStateException + */ + public void testUnlock_IllegalMonitorStateException() { + ReentrantLock rl = new ReentrantLock(); + try { + rl.unlock(); + shouldThrow(); + } catch(IllegalMonitorStateException success){} + } + /** + * trylock on an unlocked lock succeeds + */ + public void testTryLock() { + ReentrantLock rl = new ReentrantLock(); + assertTrue(rl.tryLock()); + assertTrue(rl.isLocked()); + rl.unlock(); } - - /* - * makes a lock, locks it, tries to aquire the lock in another thread - * interrupts that thread and waits for an interrupted Exception to - * be thrown. + + + /** + * getQueueLength reports number of waiting threads + */ + public void testGetQueueLength() { + final ReentrantLock lock = new ReentrantLock(); + 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(); + } + } + + /** + * getQueuedThreads includes waiting threads + */ + public void testGetQueuedThreads() { + final PublicReentrantLock lock = new PublicReentrantLock(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + assertTrue(lock.getQueuedThreads().isEmpty()); + lock.lock(); + assertTrue(lock.getQueuedThreads().isEmpty()); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.getQueuedThreads().contains(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.getQueuedThreads().contains(t1)); + assertTrue(lock.getQueuedThreads().contains(t2)); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(lock.getQueuedThreads().contains(t1)); + assertTrue(lock.getQueuedThreads().contains(t2)); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.getQueuedThreads().isEmpty()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * timed trylock is interruptible. */ + public void testInterruptedException2() { + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); + threadShouldThrow(); + } catch(InterruptedException success){} + } + }); + try { + t.start(); + t.interrupt(); + } catch(Exception e){ + unexpectedException(); + } + } + - public void testInterruptedException(){ + /** + * Trylock on a locked lock fails + */ + public void testTryLockWhenLocked() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { - public void run(){ - try{ - lock.lockInterruptibly(); - fail("should throw"); - }catch(InterruptedException sucess){} + public void run() { + threadAssertFalse(lock.tryLock()); } }); - t.start(); - t.interrupt(); - lock.unlock(); + try { + t.start(); + t.join(); + lock.unlock(); + } catch(Exception e){ + unexpectedException(); + } } - /* - * tests for interrupted exception on a timed wait - * + /** + * Timed trylock on a locked lock times out */ - - - public void testInterruptedException2(){ + public void testTryLock_Timeout() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { - public void run(){ - try{ - lock.tryLock(1000,TimeUnit.MILLISECONDS); - fail("should throw"); - }catch(InterruptedException sucess){} + public void run() { + try { + threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); + } catch (Exception ex) { + threadUnexpectedException(); + } } }); - t.start(); - t.interrupt(); - } - + try { + t.start(); + t.join(); + lock.unlock(); + } catch(Exception e){ + unexpectedException(); + } + } + /** + * getHoldCount returns number of recursive holds + */ public void testGetHoldCount() { ReentrantLock lock = new ReentrantLock(); - for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { + for(int i = 1; i <= SIZE; i++) { lock.lock(); assertEquals(i,lock.getHoldCount()); } - for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) { + for(int i = SIZE; i > 0; i--) { lock.unlock(); assertEquals(i-1,lock.getHoldCount()); } } - - + /** + * isLocked is true when locked and false when not + */ public void testIsLocked() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); @@ -109,78 +271,102 @@ public class ReentrantLockTest extends T public void run() { lock.lock(); try { - Thread.sleep(SHORT_DELAY_MS * 2); + Thread.sleep(SMALL_DELAY_MS); } - catch(Exception e) {} + catch(Exception e) { + threadUnexpectedException(); + } lock.unlock(); } }); - try{ + try { t.start(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.isLocked()); t.join(); assertFalse(lock.isLocked()); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } - } - /* - * current thread locks interruptibly the thread - * another thread tries to aquire the lock and blocks - * on the call. interrupt the attempted aquireLock - * assert that the first lock() call actually locked the lock - * assert that the current thread is the one holding the lock + + /** + * lockInterruptibly is interruptible. */ + public void testLockInterruptibly1() { + final ReentrantLock lock = new ReentrantLock(); + lock.lock(); + Thread t = new Thread(new InterruptedLockRunnable(lock)); + try { + t.start(); + t.interrupt(); + lock.unlock(); + t.join(); + } catch(Exception e){ + unexpectedException(); + } + } - public void testLockedInterruptibly() { + /** + * lockInterruptibly succeeds when unlocked, else is interruptible + */ + public void testLockInterruptibly2() { final ReentrantLock lock = new ReentrantLock(); - try {lock.lockInterruptibly();} catch(Exception e) {} - Thread t = new Thread(new Runnable() { - public void run() { - try { - lock.lockInterruptibly(); - fail("Failed to generate an Interrupted Exception"); - } - catch(InterruptedException e) {} - } - }); - t.start(); - t.interrupt(); - assertTrue(lock.isLocked()); - assertTrue(lock.isHeldByCurrentThread()); + try { + lock.lockInterruptibly(); + } catch(Exception e) { + unexpectedException(); + } + Thread t = new Thread(new InterruptedLockRunnable(lock)); + try { + t.start(); + t.interrupt(); + assertTrue(lock.isLocked()); + assertTrue(lock.isHeldByCurrentThread()); + t.join(); + } catch(Exception e){ + unexpectedException(); + } } + /** + * Calling await without holding lock throws IllegalMonitorStateException + */ public void testAwait_IllegalMonitor() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.await(); - fail("should throw"); + shouldThrow(); } catch (IllegalMonitorStateException success) { } catch (Exception ex) { - fail("should throw IMSE"); + unexpectedException(); } } + /** + * Calling signal without holding lock throws IllegalMonitorStateException + */ public void testSignal_IllegalMonitor() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.signal(); - fail("should throw"); + shouldThrow(); } catch (IllegalMonitorStateException success) { } catch (Exception ex) { - fail("should throw IMSE"); + unexpectedException(); } } + /** + * awaitNanos without a signal times out + */ public void testAwaitNanos_Timeout() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -191,23 +377,29 @@ public class ReentrantLockTest extends T lock.unlock(); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * timed await without a signal times out + */ public void testAwait_Timeout() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.lock(); - assertFalse(c.await(10, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); lock.unlock(); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * awaitUntil without a signal times out + */ public void testAwaitUntil_Timeout() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -218,22 +410,60 @@ public class ReentrantLockTest extends T lock.unlock(); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * await returns when signalled + */ public void testAwait() { final ReentrantLock lock = new ReentrantLock(); - final Condition c = lock.newCondition(); + final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + c.signal(); + lock.unlock(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + 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(); Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); + threadAssertFalse(c.hasWaiters()); + threadAssertEquals(0, c.getWaitQueueLength()); c.await(); lock.unlock(); } catch(InterruptedException e) { - fail("unexpected exception"); + threadUnexpectedException(); } } }); @@ -242,16 +472,87 @@ public class ReentrantLockTest extends T t.start(); Thread.sleep(SHORT_DELAY_MS); lock.lock(); + assertTrue(c.hasWaiters()); + assertEquals(1, c.getWaitQueueLength()); c.signal(); lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + assertFalse(c.hasWaiters()); + assertEquals(0, c.getWaitQueueLength()); + lock.unlock(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * getWaitQueueLength returns number of waiting threads + */ + public void testGetWaitQueueLength() { + final ReentrantLock lock = new ReentrantLock(); + final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertFalse(c.hasWaiters()); + threadAssertEquals(0, c.getWaitQueueLength()); + c.await(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertTrue(c.hasWaiters()); + threadAssertEquals(1, c.getWaitQueueLength()); + 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(c.hasWaiters()); + assertEquals(2, c.getWaitQueueLength()); + c.signalAll(); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + assertFalse(c.hasWaiters()); + assertEquals(0, c.getWaitQueueLength()); + 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() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -270,14 +571,18 @@ public class ReentrantLockTest extends T lock.lock(); c.signal(); lock.unlock(); + assert(t.isInterrupted()); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * await is interruptible + */ public void testAwait_Interrupt() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -287,7 +592,7 @@ public class ReentrantLockTest extends T lock.lock(); c.await(); lock.unlock(); - fail("should throw"); + threadShouldThrow(); } catch(InterruptedException success) { } @@ -302,10 +607,13 @@ public class ReentrantLockTest extends T assertFalse(t.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * awaitNanos is interruptible + */ public void testAwaitNanos_Interrupt() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -315,7 +623,7 @@ public class ReentrantLockTest extends T lock.lock(); c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); lock.unlock(); - fail("should throw"); + threadShouldThrow(); } catch(InterruptedException success) { } @@ -330,10 +638,13 @@ public class ReentrantLockTest extends T assertFalse(t.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * awaitUntil is interruptible + */ public void testAwaitUntil_Interrupt() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -344,7 +655,7 @@ public class ReentrantLockTest extends T java.util.Date d = new java.util.Date(); c.awaitUntil(new java.util.Date(d.getTime() + 10000)); lock.unlock(); - fail("should throw"); + threadShouldThrow(); } catch(InterruptedException success) { } @@ -359,10 +670,13 @@ public class ReentrantLockTest extends T assertFalse(t.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * signalAll wakes up all threads + */ public void testSignalAll() { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); @@ -374,7 +688,7 @@ public class ReentrantLockTest extends T lock.unlock(); } catch(InterruptedException e) { - fail("unexpected exception"); + threadUnexpectedException(); } } }); @@ -387,7 +701,7 @@ public class ReentrantLockTest extends T lock.unlock(); } catch(InterruptedException e) { - fail("unexpected exception"); + threadUnexpectedException(); } } }); @@ -405,7 +719,32 @@ public class ReentrantLockTest extends T assertFalse(t2.isAlive()); } catch (Exception ex) { - fail("unexpected exception"); + unexpectedException(); + } + } + + /** + * A serialized lock deserializes as unlocked + */ + public void testSerialization() { + ReentrantLock l = new ReentrantLock(); + l.lock(); + l.unlock(); + + try { + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(l); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + ReentrantLock r = (ReentrantLock) in.readObject(); + r.lock(); + r.unlock(); + } catch(Exception e){ + e.printStackTrace(); + unexpectedException(); } }