--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/09 14:45:58 1.12 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2006/05/18 10:29:23 1.21 @@ -30,27 +30,20 @@ public class AbstractQueuedSynchronizerT * ReentrantReadWriteLock, and Semaphore */ static class Mutex extends AbstractQueuedSynchronizer { - public boolean isLocked() { return getState() == 1; } + public boolean isHeldExclusively() { return getState() == 1; } - public boolean tryAcquireExclusive(int acquires) { + public boolean tryAcquire(int acquires) { assertTrue(acquires == 1); return compareAndSetState(0, 1); } - public boolean tryReleaseExclusive(int releases) { + public boolean tryRelease(int releases) { + if (getState() == 0) throw new IllegalMonitorStateException(); setState(0); return true; } - public void checkConditionAccess(Thread thread) { - if (getState() == 0) throw new IllegalMonitorStateException(); - } - - public ConditionObject newCondition() { return new ConditionObject(); } - - public void lock() { - acquireExclusiveUninterruptibly(1); - } + public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } } @@ -72,76 +65,85 @@ public class AbstractQueuedSynchronizerT } /** - * A runnable calling acquireExclusiveInterruptibly + * A runnable calling acquireInterruptibly */ - class InterruptibleLockRunnable implements Runnable { - final Mutex lock; - InterruptibleLockRunnable(Mutex l) { lock = l; } + class InterruptibleSyncRunnable implements Runnable { + final Mutex sync; + InterruptibleSyncRunnable(Mutex l) { sync = l; } public void run() { try { - lock.acquireExclusiveInterruptibly(1); + sync.acquireInterruptibly(1); } catch(InterruptedException success){} } } /** - * A runnable calling acquireExclusiveInterruptibly that expects to be + * A runnable calling acquireInterruptibly that expects to be * interrupted */ - class InterruptedLockRunnable implements Runnable { - final Mutex lock; - InterruptedLockRunnable(Mutex l) { lock = l; } + class InterruptedSyncRunnable implements Runnable { + final Mutex sync; + InterruptedSyncRunnable(Mutex l) { sync = l; } public void run() { try { - lock.acquireExclusiveInterruptibly(1); + sync.acquireInterruptibly(1); threadShouldThrow(); } catch(InterruptedException success){} } } + + /** + * isHeldExclusively is false upon construction + */ + public void testIsHeldExclusively() { + Mutex rl = new Mutex(); + assertFalse(rl.isHeldExclusively()); + } /** - * acquireExclusiveUninterruptiblying an releaseExclusiveed lock succeeds + * acquiring released sync succeeds */ - public void testAcquireExclusiveUninterruptibly() { + public void testAcquire() { Mutex rl = new Mutex(); - rl.acquireExclusiveUninterruptibly(1); - assertTrue(rl.isLocked()); - rl.releaseExclusive(1); + rl.acquire(1); + assertTrue(rl.isHeldExclusively()); + rl.release(1); + assertFalse(rl.isHeldExclusively()); } /** - * tryAcquireExclusive on an releaseExclusiveed lock succeeds + * tryAcquire on an released sync succeeds */ - public void testTryAcquireExclusive() { + public void testTryAcquire() { Mutex rl = new Mutex(); - assertTrue(rl.tryAcquireExclusive(1)); - assertTrue(rl.isLocked()); - rl.releaseExclusive(1); + assertTrue(rl.tryAcquire(1)); + assertTrue(rl.isHeldExclusively()); + rl.release(1); } /** * hasQueuedThreads reports whether there are waiting threads */ public void testhasQueuedThreads() { - final Mutex lock = new Mutex(); - Thread t1 = new Thread(new InterruptedLockRunnable(lock)); - Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); try { - assertFalse(lock.hasQueuedThreads()); - lock.acquireExclusiveUninterruptibly(1); + assertFalse(sync.hasQueuedThreads()); + sync.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasQueuedThreads()); + assertTrue(sync.hasQueuedThreads()); t2.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasQueuedThreads()); + assertTrue(sync.hasQueuedThreads()); t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasQueuedThreads()); - lock.releaseExclusive(1); + assertTrue(sync.hasQueuedThreads()); + sync.release(1); Thread.sleep(SHORT_DELAY_MS); - assertFalse(lock.hasQueuedThreads()); + assertFalse(sync.hasQueuedThreads()); t1.join(); t2.join(); } catch(Exception e){ @@ -153,9 +155,9 @@ public class AbstractQueuedSynchronizerT * isQueued(null) throws NPE */ public void testIsQueuedNPE() { - final Mutex lock = new Mutex(); + final Mutex sync = new Mutex(); try { - lock.isQueued(null); + sync.isQueued(null); shouldThrow(); } catch (NullPointerException success) { } @@ -165,28 +167,29 @@ public class AbstractQueuedSynchronizerT * isQueued reports whether a thread is queued. */ public void testIsQueued() { - final Mutex lock = new Mutex(); - Thread t1 = new Thread(new InterruptedLockRunnable(lock)); - Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); - try { - assertFalse(lock.isQueued(t1)); - assertFalse(lock.isQueued(t2)); - lock.acquireExclusiveUninterruptibly(1); + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); + try { + assertFalse(sync.isQueued(t1)); + assertFalse(sync.isQueued(t2)); + sync.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.isQueued(t1)); + assertTrue(sync.isQueued(t1)); t2.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.isQueued(t1)); - assertTrue(lock.isQueued(t2)); + assertTrue(sync.isQueued(t1)); + assertTrue(sync.isQueued(t2)); t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); - assertFalse(lock.isQueued(t1)); - assertTrue(lock.isQueued(t2)); - lock.releaseExclusive(1); + assertFalse(sync.isQueued(t1)); + assertTrue(sync.isQueued(t2)); + sync.release(1); Thread.sleep(SHORT_DELAY_MS); - assertFalse(lock.isQueued(t1)); - assertFalse(lock.isQueued(t2)); + assertFalse(sync.isQueued(t1)); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(sync.isQueued(t2)); t1.join(); t2.join(); } catch(Exception e){ @@ -195,27 +198,28 @@ public class AbstractQueuedSynchronizerT } /** - * getFirstQueuedThread returns first waiting thread or null is none + * getFirstQueuedThread returns first waiting thread or null if none */ public void testGetFirstQueuedThread() { - final Mutex lock = new Mutex(); - Thread t1 = new Thread(new InterruptedLockRunnable(lock)); - Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); try { - assertNull(lock.getFirstQueuedThread()); - lock.acquireExclusiveUninterruptibly(1); + assertNull(sync.getFirstQueuedThread()); + sync.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); - assertEquals(t1, lock.getFirstQueuedThread()); + assertEquals(t1, sync.getFirstQueuedThread()); t2.start(); Thread.sleep(SHORT_DELAY_MS); - assertEquals(t1, lock.getFirstQueuedThread()); + assertEquals(t1, sync.getFirstQueuedThread()); t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); - assertEquals(t2, lock.getFirstQueuedThread()); - lock.releaseExclusive(1); Thread.sleep(SHORT_DELAY_MS); - assertNull(lock.getFirstQueuedThread()); + assertEquals(t2, sync.getFirstQueuedThread()); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertNull(sync.getFirstQueuedThread()); t1.join(); t2.join(); } catch(Exception e){ @@ -228,24 +232,88 @@ public class AbstractQueuedSynchronizerT * hasContended reports false if no thread has ever blocked, else true */ public void testHasContended() { - final Mutex lock = new Mutex(); - Thread t1 = new Thread(new InterruptedLockRunnable(lock)); - Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); + try { + assertFalse(sync.hasContended()); + sync.acquire(1); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasContended()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasContended()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasContended()); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasContended()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * getQueuedThreads includes waiting threads + */ + public void testGetQueuedThreads() { + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); + try { + assertTrue(sync.getQueuedThreads().isEmpty()); + sync.acquire(1); + assertTrue(sync.getQueuedThreads().isEmpty()); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getQueuedThreads().contains(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getQueuedThreads().contains(t1)); + assertTrue(sync.getQueuedThreads().contains(t2)); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(sync.getQueuedThreads().contains(t1)); + assertTrue(sync.getQueuedThreads().contains(t2)); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getQueuedThreads().isEmpty()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * getExclusiveQueuedThreads includes waiting threads + */ + public void testGetExclusiveQueuedThreads() { + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); try { - assertFalse(lock.hasContended()); - lock.acquireExclusiveUninterruptibly(1); + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); + sync.acquire(1); + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); t1.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasContended()); + assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); t2.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasContended()); + assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); + assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasContended()); - lock.releaseExclusive(1); + assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); + assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); + sync.release(1); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.hasContended()); + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); t1.join(); t2.join(); } catch(Exception e){ @@ -254,15 +322,45 @@ public class AbstractQueuedSynchronizerT } /** - * timed tryAcquireExclusive is interruptible. + * getSharedQueuedThreads does not include exclusively waiting threads + */ + public void testGetSharedQueuedThreads() { + final Mutex sync = new Mutex(); + Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); + Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); + try { + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + sync.acquire(1); + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getSharedQueuedThreads().isEmpty()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * tryAcquireNanos is interruptible. */ public void testInterruptedException2() { - final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + final Mutex sync = new Mutex(); + sync.acquire(1); Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); + sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); threadShouldThrow(); } catch(InterruptedException success){} } @@ -277,35 +375,35 @@ public class AbstractQueuedSynchronizerT /** - * TryAcquireExclusive on a locked lock fails + * TryAcquire on exclusively held sync fails */ - public void testTryAcquireExclusiveWhenLocked() { - final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + public void testTryAcquireWhenSynced() { + final Mutex sync = new Mutex(); + sync.acquire(1); Thread t = new Thread(new Runnable() { public void run() { - threadAssertFalse(lock.tryAcquireExclusive(1)); + threadAssertFalse(sync.tryAcquire(1)); } }); try { t.start(); t.join(); - lock.releaseExclusive(1); + sync.release(1); } catch(Exception e){ unexpectedException(); } } /** - * acquireExclusiveTimed on a locked lock times out + * tryAcquireNanos on an exclusively held sync times out */ - public void testacquireExclusiveTimed_Timeout() { - final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + public void testAcquireNanos_Timeout() { + final Mutex sync = new Mutex(); + sync.acquire(1); Thread t = new Thread(new Runnable() { public void run() { try { - threadAssertFalse(lock.acquireExclusiveNanos(1, 1000 * 1000)); + threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); } catch (Exception ex) { threadUnexpectedException(); } @@ -314,7 +412,7 @@ public class AbstractQueuedSynchronizerT try { t.start(); t.join(); - lock.releaseExclusive(1); + sync.release(1); } catch(Exception e){ unexpectedException(); } @@ -322,32 +420,32 @@ public class AbstractQueuedSynchronizerT /** - * isLocked is true when locked and false when not + * getState is true when acquired and false when not */ - public void testIsLocked() { - final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); - assertTrue(lock.isLocked()); - lock.releaseExclusive(1); - assertFalse(lock.isLocked()); + public void testGetState() { + final Mutex sync = new Mutex(); + sync.acquire(1); + assertTrue(sync.isHeldExclusively()); + sync.release(1); + assertFalse(sync.isHeldExclusively()); Thread t = new Thread(new Runnable() { public void run() { - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); try { Thread.sleep(SMALL_DELAY_MS); } catch(Exception e) { threadUnexpectedException(); } - lock.releaseExclusive(1); + sync.release(1); } }); try { t.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.isLocked()); + assertTrue(sync.isHeldExclusively()); t.join(); - assertFalse(lock.isLocked()); + assertFalse(sync.isHeldExclusively()); } catch(Exception e){ unexpectedException(); } @@ -355,16 +453,18 @@ public class AbstractQueuedSynchronizerT /** - * acquireExclusiveInterruptibly is interruptible. + * acquireInterruptibly is interruptible. */ public void testAcquireInterruptibly1() { - final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); - Thread t = new Thread(new InterruptedLockRunnable(lock)); + final Mutex sync = new Mutex(); + sync.acquire(1); + Thread t = new Thread(new InterruptedSyncRunnable(sync)); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - lock.releaseExclusive(1); + Thread.sleep(SHORT_DELAY_MS); + sync.release(1); t.join(); } catch(Exception e){ unexpectedException(); @@ -372,20 +472,20 @@ public class AbstractQueuedSynchronizerT } /** - * acquireExclusiveInterruptibly succeeds when released, else is interruptible + * acquireInterruptibly succeeds when released, else is interruptible */ public void testAcquireInterruptibly2() { - final Mutex lock = new Mutex(); + final Mutex sync = new Mutex(); try { - lock.acquireExclusiveInterruptibly(1); + sync.acquireInterruptibly(1); } catch(Exception e) { unexpectedException(); } - Thread t = new Thread(new InterruptedLockRunnable(lock)); + Thread t = new Thread(new InterruptedSyncRunnable(sync)); try { t.start(); t.interrupt(); - assertTrue(lock.isLocked()); + assertTrue(sync.isHeldExclusively()); t.join(); } catch(Exception e){ unexpectedException(); @@ -393,22 +493,22 @@ public class AbstractQueuedSynchronizerT } /** - * owns is true for a condition created by lock else false + * owns is true for a condition created by sync else false */ public void testOwns() { - final Mutex lock = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); - final Mutex lock2 = new Mutex(); - assertTrue(lock.owns(c)); - assertFalse(lock2.owns(c)); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + final Mutex sync2 = new Mutex(); + assertTrue(sync.owns(c)); + assertFalse(sync2.owns(c)); } /** - * Calling await without holding lock throws IllegalMonitorStateException + * Calling await without holding sync throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.await(); shouldThrow(); @@ -421,11 +521,11 @@ public class AbstractQueuedSynchronizerT } /** - * Calling signal without holding lock throws IllegalMonitorStateException + * Calling signal without holding sync throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.signal(); shouldThrow(); @@ -441,13 +541,13 @@ public class AbstractQueuedSynchronizerT * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); long t = c.awaitNanos(100); assertTrue(t <= 0); - lock.releaseExclusive(1); + sync.release(1); } catch (Exception ex) { unexpectedException(); @@ -455,15 +555,15 @@ public class AbstractQueuedSynchronizerT } /** - * timed await without a signal times out + * Timed await without a signal times out */ public void testAwait_Timeout() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - lock.releaseExclusive(1); + sync.release(1); } catch (Exception ex) { unexpectedException(); @@ -474,13 +574,13 @@ public class AbstractQueuedSynchronizerT * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); java.util.Date d = new java.util.Date(); assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); - lock.releaseExclusive(1); + sync.release(1); } catch (Exception ex) { unexpectedException(); @@ -491,14 +591,14 @@ public class AbstractQueuedSynchronizerT * await returns when signalled */ public void testAwait() { - final Mutex lock = new Mutex(); - final Condition c = lock.newCondition(); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); c.await(); - lock.releaseExclusive(1); + sync.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -509,9 +609,9 @@ public class AbstractQueuedSynchronizerT try { t.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + sync.acquire(1); c.signal(); - lock.releaseExclusive(1); + sync.release(1); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } @@ -521,10 +621,530 @@ public class AbstractQueuedSynchronizerT } + + /** + * hasWaiters throws NPE if null + */ + public void testHasWaitersNPE() { + final Mutex sync = new Mutex(); + try { + sync.hasWaiters(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitQueueLength throws NPE if null + */ + public void testGetWaitQueueLengthNPE() { + final Mutex sync = new Mutex(); + try { + sync.getWaitQueueLength(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** - * Latch isSignalled initially returns false, then true after release + * getWaitingThreads throws NPE if null */ - public void testLatchIsSignalled() { + public void testGetWaitingThreadsNPE() { + final Mutex sync = new Mutex(); + try { + sync.getWaitingThreads(null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * hasWaiters throws IAE if not owned + */ + public void testHasWaitersIAE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final Mutex sync2 = new Mutex(); + try { + sync2.hasWaiters(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * hasWaiters throws IMSE if not synced + */ + public void testHasWaitersIMSE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + try { + sync.hasWaiters(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * getWaitQueueLength throws IAE if not owned + */ + public void testGetWaitQueueLengthIAE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final Mutex sync2 = new Mutex(); + try { + sync2.getWaitQueueLength(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitQueueLength throws IMSE if not synced + */ + public void testGetWaitQueueLengthIMSE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + try { + sync.getWaitQueueLength(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * getWaitingThreads throws IAE if not owned + */ + public void testGetWaitingThreadsIAE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final Mutex sync2 = new Mutex(); + try { + sync2.getWaitingThreads(c); + shouldThrow(); + } catch (IllegalArgumentException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitingThreads throws IMSE if not synced + */ + public void testGetWaitingThreadsIMSE() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + try { + sync.getWaitingThreads(c); + shouldThrow(); + } catch (IllegalMonitorStateException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + + + /** + * hasWaiters returns true when a thread is waiting, else false + */ + public void testHasWaiters() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + threadAssertFalse(sync.hasWaiters(c)); + threadAssertEquals(0, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertTrue(sync.hasWaiters(c)); + assertEquals(1, sync.getWaitQueueLength(c)); + c.signal(); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertFalse(sync.hasWaiters(c)); + assertEquals(0, sync.getWaitQueueLength(c)); + sync.release(1); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * getWaitQueueLength returns number of waiting threads + */ + public void testGetWaitQueueLength() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + threadAssertFalse(sync.hasWaiters(c)); + threadAssertEquals(0, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + threadAssertTrue(sync.hasWaiters(c)); + threadAssertEquals(1, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertTrue(sync.hasWaiters(c)); + assertEquals(2, sync.getWaitQueueLength(c)); + c.signalAll(); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertFalse(sync.hasWaiters(c)); + assertEquals(0, sync.getWaitQueueLength(c)); + sync.release(1); + 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 Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + sync.acquire(1); + assertTrue(sync.getWaitingThreads(c).isEmpty()); + sync.release(1); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertTrue(sync.hasWaiters(c)); + assertTrue(sync.getWaitingThreads(c).contains(t1)); + assertTrue(sync.getWaitingThreads(c).contains(t2)); + c.signalAll(); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + assertFalse(sync.hasWaiters(c)); + assertTrue(sync.getWaitingThreads(c).isEmpty()); + sync.release(1); + 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 Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + sync.acquire(1); + c.awaitUninterruptibly(); + sync.release(1); + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + sync.acquire(1); + c.signal(); + sync.release(1); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * await is interruptible + */ + public void testAwait_Interrupt() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + c.await(); + sync.release(1); + threadShouldThrow(); + } + catch(InterruptedException success) { + } + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * awaitNanos is interruptible + */ + public void testAwaitNanos_Interrupt() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + c.awaitNanos(1000 * 1000 * 1000); // 1 sec + sync.release(1); + threadShouldThrow(); + } + catch(InterruptedException success) { + } + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * awaitUntil is interruptible + */ + public void testAwaitUntil_Interrupt() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + java.util.Date d = new java.util.Date(); + c.awaitUntil(new java.util.Date(d.getTime() + 10000)); + sync.release(1); + threadShouldThrow(); + } + catch(InterruptedException success) { + } + } + }); + + try { + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * signalAll wakes up all threads + */ + public void testSignalAll() { + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + sync.acquire(1); + c.await(); + sync.release(1); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t1.start(); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + c.signalAll(); + sync.release(1); + t1.join(SHORT_DELAY_MS); + t2.join(SHORT_DELAY_MS); + assertFalse(t1.isAlive()); + assertFalse(t2.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * toString indicates current state + */ + public void testToString() { + Mutex sync = new Mutex(); + String us = sync.toString(); + assertTrue(us.indexOf("State = 0") >= 0); + sync.acquire(1); + String ls = sync.toString(); + assertTrue(ls.indexOf("State = 1") >= 0); + } + + /** + * A serialized AQS deserializes with current state + */ + public void testSerialization() { + Mutex l = new Mutex(); + l.acquire(1); + assertTrue(l.isHeldExclusively()); + + 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)); + Mutex r = (Mutex) in.readObject(); + assertTrue(r.isHeldExclusively()); + } catch(Exception e){ + e.printStackTrace(); + unexpectedException(); + } + } + + + /** + * tryReleaseShared setting state changes getState + */ + public void testGetStateWithReleaseShared() { final BooleanLatch l = new BooleanLatch(); assertFalse(l.isSignalled()); l.releaseShared(0); @@ -532,9 +1152,9 @@ public class AbstractQueuedSynchronizerT } /** - * release and has no effect when already signalled + * releaseShared has no effect when already signalled */ - public void testLatchBoolean() { + public void testReleaseShared() { final BooleanLatch l = new BooleanLatch(); assertFalse(l.isSignalled()); l.releaseShared(0); @@ -546,7 +1166,7 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedInterruptibly returns after release, but not before */ - public void testLatchAcquireSharedInterruptibly() { + public void testAcquireSharedInterruptibly() { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { @@ -576,14 +1196,14 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedTimed returns after release */ - public void testLatchTimedacquireSharedInterruptibly() { + public void testAsquireSharedTimed() { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { try { threadAssertFalse(l.isSignalled()); - threadAssertTrue(l.acquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); + threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); threadAssertTrue(l.isSignalled()); } catch(InterruptedException e){ @@ -606,7 +1226,7 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedInterruptibly throws IE if interrupted before released */ - public void testLatchAcquireSharedInterruptibly_InterruptedException() { + public void testAcquireSharedInterruptibly_InterruptedException() { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { @@ -630,13 +1250,13 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedTimed throws IE if interrupted before released */ - public void testLatchTimedAwait_InterruptedException() { + public void testAcquireSharedNanos_InterruptedException() { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { try { threadAssertFalse(l.isSignalled()); - l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); + l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); threadShouldThrow(); } catch(InterruptedException success){} } @@ -655,13 +1275,13 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedTimed times out if not released before timeout */ - public void testLatchAwaitTimeout() { + public void testAcquireSharedNanos_Timeout() { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { try { threadAssertFalse(l.isSignalled()); - threadAssertFalse(l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); + threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); } catch(InterruptedException ie){ threadUnexpectedException(); } @@ -676,5 +1296,6 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } + }