--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/12 16:37:40 1.17 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/02 20:28:31 1.22 @@ -2,8 +2,8 @@ * 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. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -31,33 +31,33 @@ public class AbstractQueuedSynchronizerT */ static class Mutex extends AbstractQueuedSynchronizer { public boolean isHeldExclusively() { return getState() == 1; } - + public boolean tryAcquire(int acquires) { - assertTrue(acquires == 1); + assertTrue(acquires == 1); return compareAndSetState(0, 1); } - + public boolean tryRelease(int releases) { if (getState() == 0) throw new IllegalMonitorStateException(); setState(0); return true; } - + public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } } - + /** * A simple latch class, to test shared mode. */ - static class BooleanLatch extends AbstractQueuedSynchronizer { + static class BooleanLatch extends AbstractQueuedSynchronizer { public boolean isSignalled() { return getState() != 0; } public int tryAcquireShared(int ignore) { return isSignalled()? 1 : -1; } - + public boolean tryReleaseShared(int ignore) { setState(1); return true; @@ -96,15 +96,15 @@ public class AbstractQueuedSynchronizerT /** * isHeldExclusively is false upon construction */ - public void testIsHeldExclusively() { + public void testIsHeldExclusively() { Mutex rl = new Mutex(); assertFalse(rl.isHeldExclusively()); } - + /** * acquiring released sync succeeds */ - public void testAcquire() { + public void testAcquire() { Mutex rl = new Mutex(); rl.acquire(1); assertTrue(rl.isHeldExclusively()); @@ -115,7 +115,7 @@ public class AbstractQueuedSynchronizerT /** * tryAcquire on an released sync succeeds */ - public void testTryAcquire() { + public void testTryAcquire() { Mutex rl = new Mutex(); assertTrue(rl.tryAcquire(1)); assertTrue(rl.isHeldExclusively()); @@ -125,7 +125,7 @@ public class AbstractQueuedSynchronizerT /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() { + public void testhasQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -149,12 +149,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * isQueued(null) throws NPE */ - public void testIsQueuedNPE() { + public void testIsQueuedNPE() { final Mutex sync = new Mutex(); try { sync.isQueued(null); @@ -166,7 +166,7 @@ public class AbstractQueuedSynchronizerT /** * isQueued reports whether a thread is queued. */ - public void testIsQueued() { + public void testIsQueued() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -188,18 +188,19 @@ public class AbstractQueuedSynchronizerT sync.release(1); Thread.sleep(SHORT_DELAY_MS); assertFalse(sync.isQueued(t1)); + Thread.sleep(SHORT_DELAY_MS); assertFalse(sync.isQueued(t2)); t1.join(); t2.join(); } catch(Exception e){ unexpectedException(); } - } + } /** * getFirstQueuedThread returns first waiting thread or null if none */ - public void testGetFirstQueuedThread() { + public void testGetFirstQueuedThread() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -214,6 +215,7 @@ public class AbstractQueuedSynchronizerT assertEquals(t1, sync.getFirstQueuedThread()); t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); assertEquals(t2, sync.getFirstQueuedThread()); sync.release(1); Thread.sleep(SHORT_DELAY_MS); @@ -223,13 +225,13 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * hasContended reports false if no thread has ever blocked, else true */ - public void testHasContended() { + public void testHasContended() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -253,12 +255,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() { + public void testGetQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -285,12 +287,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getExclusiveQueuedThreads includes waiting threads */ - public void testGetExclusiveQueuedThreads() { + public void testGetExclusiveQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -317,12 +319,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getSharedQueuedThreads does not include exclusively waiting threads */ - public void testGetSharedQueuedThreads() { + public void testGetSharedQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -347,12 +349,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * tryAcquireNanos is interruptible. */ - public void testInterruptedException2() { + public void testInterruptedException2() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -375,7 +377,7 @@ public class AbstractQueuedSynchronizerT /** * TryAcquire on exclusively held sync fails */ - public void testTryAcquireWhenSynced() { + public void testTryAcquireWhenSynced() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -390,12 +392,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * tryAcquireNanos on an exclusively held sync times out */ - public void testAcquireNanos_Timeout() { + public void testAcquireNanos_Timeout() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -414,9 +416,9 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } - - + } + + /** * getState is true when acquired and false when not */ @@ -426,7 +428,7 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.isHeldExclusively()); sync.release(1); assertFalse(sync.isHeldExclusively()); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { sync.acquire(1); try { @@ -453,25 +455,27 @@ public class AbstractQueuedSynchronizerT /** * acquireInterruptibly is interruptible. */ - public void testAcquireInterruptibly1() { + public void testAcquireInterruptibly1() { 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(); + Thread.sleep(SHORT_DELAY_MS); sync.release(1); t.join(); } catch(Exception e){ unexpectedException(); } - } + } /** * acquireInterruptibly succeeds when released, else is interruptible */ public void testAcquireInterruptibly2() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.acquireInterruptibly(1); } catch(Exception e) { @@ -492,7 +496,7 @@ public class AbstractQueuedSynchronizerT * owns is true for a condition created by sync else false */ public void testOwns() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); assertTrue(sync.owns(c)); @@ -503,7 +507,7 @@ public class AbstractQueuedSynchronizerT * Calling await without holding sync throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.await(); @@ -520,7 +524,7 @@ public class AbstractQueuedSynchronizerT * Calling signal without holding sync throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.signal(); @@ -537,7 +541,7 @@ public class AbstractQueuedSynchronizerT * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -554,7 +558,7 @@ public class AbstractQueuedSynchronizerT * Timed await without a signal times out */ public void testAwait_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -570,7 +574,7 @@ public class AbstractQueuedSynchronizerT * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -587,9 +591,9 @@ public class AbstractQueuedSynchronizerT * await returns when signalled */ public void testAwait() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -730,9 +734,9 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); - final Mutex sync2 = new Mutex(); + final Mutex sync2 = new Mutex(); try { sync2.getWaitingThreads(c); shouldThrow(); @@ -746,7 +750,7 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IMSE if not synced */ public void testGetWaitingThreadsIMSE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); try { sync.getWaitingThreads(c); @@ -763,9 +767,9 @@ public class AbstractQueuedSynchronizerT * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -805,9 +809,9 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -822,7 +826,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -866,9 +870,9 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -882,7 +886,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -931,9 +935,9 @@ public class AbstractQueuedSynchronizerT * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { sync.acquire(1); c.awaitUninterruptibly(); @@ -948,7 +952,6 @@ public class AbstractQueuedSynchronizerT sync.acquire(1); c.signal(); sync.release(1); - assert(t.isInterrupted()); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } @@ -961,9 +964,9 @@ public class AbstractQueuedSynchronizerT * await is interruptible */ public void testAwait_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -992,9 +995,9 @@ public class AbstractQueuedSynchronizerT * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1023,9 +1026,9 @@ public class AbstractQueuedSynchronizerT * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1055,9 +1058,9 @@ public class AbstractQueuedSynchronizerT * signalAll wakes up all threads */ public void testSignalAll() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1070,7 +1073,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1188,7 +1191,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedTimed returns after release @@ -1219,7 +1222,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedInterruptibly throws IE if interrupted before released */ @@ -1254,7 +1257,7 @@ public class AbstractQueuedSynchronizerT try { threadAssertFalse(l.isSignalled()); l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); - threadShouldThrow(); + threadShouldThrow(); } catch(InterruptedException success){} } }); @@ -1294,5 +1297,5 @@ public class AbstractQueuedSynchronizerT } } - + }