--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/16 16:49:40 1.18 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/18 08:22:57 1.26 @@ -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; @@ -73,7 +73,7 @@ public class AbstractQueuedSynchronizerT public void run() { try { sync.acquireInterruptibly(1); - } catch(InterruptedException success){} + } catch (InterruptedException success) {} } } @@ -89,22 +89,22 @@ public class AbstractQueuedSynchronizerT try { sync.acquireInterruptibly(1); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success) {} } } /** * 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,236 +125,207 @@ public class AbstractQueuedSynchronizerT /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() { + public void testhasQueuedThreads() throws InterruptedException { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); - try { - assertFalse(sync.hasQueuedThreads()); - sync.acquire(1); - t1.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.hasQueuedThreads()); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.hasQueuedThreads()); - t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.hasQueuedThreads()); - sync.release(1); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.hasQueuedThreads()); - t1.join(); - t2.join(); - } catch(Exception e){ - unexpectedException(); - } - } + assertFalse(sync.hasQueuedThreads()); + sync.acquire(1); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasQueuedThreads()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasQueuedThreads()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.hasQueuedThreads()); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(sync.hasQueuedThreads()); + t1.join(); + t2.join(); + } /** * isQueued(null) throws NPE */ - public void testIsQueuedNPE() { + public void testIsQueuedNPE() { final Mutex sync = new Mutex(); try { sync.isQueued(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** * isQueued reports whether a thread is queued. */ - public void testIsQueued() { + public void testIsQueued() throws InterruptedException { 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(sync.isQueued(t1)); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.isQueued(t1)); - assertTrue(sync.isQueued(t2)); - t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.isQueued(t1)); - assertTrue(sync.isQueued(t2)); - 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(); - } - } + assertFalse(sync.isQueued(t1)); + assertFalse(sync.isQueued(t2)); + sync.acquire(1); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.isQueued(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.isQueued(t1)); + assertTrue(sync.isQueued(t2)); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(sync.isQueued(t1)); + assertTrue(sync.isQueued(t2)); + 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(); + } /** * getFirstQueuedThread returns first waiting thread or null if none */ - public void testGetFirstQueuedThread() { + public void testGetFirstQueuedThread() throws InterruptedException { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); - try { - assertNull(sync.getFirstQueuedThread()); - sync.acquire(1); - t1.start(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(t1, sync.getFirstQueuedThread()); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); - 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); - assertNull(sync.getFirstQueuedThread()); - t1.join(); - t2.join(); - } catch(Exception e){ - unexpectedException(); - } - } + assertNull(sync.getFirstQueuedThread()); + sync.acquire(1); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertEquals(t1, sync.getFirstQueuedThread()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + 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); + assertNull(sync.getFirstQueuedThread()); + t1.join(); + t2.join(); + } /** * hasContended reports false if no thread has ever blocked, else true */ - public void testHasContended() { + public void testHasContended() throws InterruptedException { 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(); - } - } + 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(); + } /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() { + public void testGetQueuedThreads() throws InterruptedException { 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(); - } - } + 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(); + } /** * getExclusiveQueuedThreads includes waiting threads */ - public void testGetExclusiveQueuedThreads() { + public void testGetExclusiveQueuedThreads() throws InterruptedException { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); - try { - assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); - sync.acquire(1); - assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); - t1.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); - t2.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); - assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); - t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); - assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); - sync.release(1); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); - t1.join(); - t2.join(); - } catch(Exception e){ - unexpectedException(); - } - } + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); + sync.acquire(1); + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); + assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); + assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); + sync.release(1); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); + t1.join(); + t2.join(); + } /** * getSharedQueuedThreads does not include exclusively waiting threads */ - public void testGetSharedQueuedThreads() { + public void testGetSharedQueuedThreads() throws InterruptedException { 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(); - } - } + 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(); + } /** * tryAcquireNanos is interruptible. */ - public void testInterruptedException2() { + public void testInterruptedException2() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -362,22 +333,19 @@ public class AbstractQueuedSynchronizerT try { sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success) {} } }); - try { - t.start(); - t.interrupt(); - } catch(Exception e){ - unexpectedException(); - } + + t.start(); + t.interrupt(); } /** * TryAcquire on exclusively held sync fails */ - public void testTryAcquireWhenSynced() { + public void testTryAcquireWhenSynced() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -385,19 +353,16 @@ public class AbstractQueuedSynchronizerT threadAssertFalse(sync.tryAcquire(1)); } }); - try { - t.start(); - t.join(); - sync.release(1); - } catch(Exception e){ - unexpectedException(); - } - } + + t.start(); + t.join(); + sync.release(1); + } /** * tryAcquireNanos on an exclusively held sync times out */ - public void testAcquireNanos_Timeout() { + public void testAcquireNanos_Timeout() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -409,92 +374,77 @@ public class AbstractQueuedSynchronizerT } } }); - try { - t.start(); - t.join(); - sync.release(1); - } catch(Exception e){ - unexpectedException(); - } - } - - + + t.start(); + t.join(); + sync.release(1); + } + + /** * getState is true when acquired and false when not */ - public void testGetState() { + public void testGetState() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); 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 { Thread.sleep(SMALL_DELAY_MS); } - catch(Exception e) { + catch (Exception e) { threadUnexpectedException(); } sync.release(1); } }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(sync.isHeldExclusively()); - t.join(); - assertFalse(sync.isHeldExclusively()); - } catch(Exception e){ - unexpectedException(); - } + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(sync.isHeldExclusively()); + t.join(); + assertFalse(sync.isHeldExclusively()); } /** * acquireInterruptibly is interruptible. */ - public void testAcquireInterruptibly1() { + public void testAcquireInterruptibly1() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new InterruptedSyncRunnable(sync)); - try { - t.start(); - t.interrupt(); - sync.release(1); - t.join(); - } catch(Exception e){ - unexpectedException(); - } - } + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + sync.release(1); + t.join(); + } /** * acquireInterruptibly succeeds when released, else is interruptible */ - public void testAcquireInterruptibly2() { - final Mutex sync = new Mutex(); - try { - sync.acquireInterruptibly(1); - } catch(Exception e) { - unexpectedException(); - } + public void testAcquireInterruptibly2() throws InterruptedException { + final Mutex sync = new Mutex(); + sync.acquireInterruptibly(1); Thread t = new Thread(new InterruptedSyncRunnable(sync)); - try { - t.start(); - t.interrupt(); - assertTrue(sync.isHeldExclusively()); - t.join(); - } catch(Exception e){ - unexpectedException(); - } + t.start(); + t.interrupt(); + assertTrue(sync.isHeldExclusively()); + t.join(); } /** * 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)); @@ -504,118 +454,90 @@ public class AbstractQueuedSynchronizerT /** * Calling await without holding sync throws IllegalMonitorStateException */ - public void testAwait_IllegalMonitor() { - final Mutex sync = new Mutex(); + public void testAwait_IllegalMonitor() throws InterruptedException { + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.await(); shouldThrow(); } - catch (IllegalMonitorStateException success) { - } - catch (Exception ex) { - unexpectedException(); - } + catch (IllegalMonitorStateException success) {} } /** * 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(); shouldThrow(); } - catch (IllegalMonitorStateException success) { - } - catch (Exception ex) { - unexpectedException(); - } + catch (IllegalMonitorStateException success) {} } /** * awaitNanos without a signal times out */ - public void testAwaitNanos_Timeout() { - final Mutex sync = new Mutex(); + public void testAwaitNanos_Timeout() throws InterruptedException { + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - try { - sync.acquire(1); - long t = c.awaitNanos(100); - assertTrue(t <= 0); - sync.release(1); - } - catch (Exception ex) { - unexpectedException(); - } + sync.acquire(1); + long t = c.awaitNanos(100); + assertTrue(t <= 0); + sync.release(1); } /** * Timed await without a signal times out */ - public void testAwait_Timeout() { - final Mutex sync = new Mutex(); + public void testAwait_Timeout() throws InterruptedException { + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - try { - sync.acquire(1); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - sync.release(1); - } - catch (Exception ex) { - unexpectedException(); - } + sync.acquire(1); + assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + sync.release(1); } /** * awaitUntil without a signal times out */ - public void testAwaitUntil_Timeout() { - final Mutex sync = new Mutex(); + public void testAwaitUntil_Timeout() throws InterruptedException { + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - try { - sync.acquire(1); - java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); - sync.release(1); - } - catch (Exception ex) { - unexpectedException(); - } + sync.acquire(1); + java.util.Date d = new java.util.Date(); + assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); + sync.release(1); } /** * await returns when signalled */ - public void testAwait() { - final Mutex sync = new Mutex(); + public void testAwait() throws InterruptedException { + 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); c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - sync.acquire(1); - c.signal(); - sync.release(1); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); - } - catch (Exception ex) { - unexpectedException(); - } + t.start(); + Thread.sleep(SHORT_DELAY_MS); + sync.acquire(1); + c.signal(); + sync.release(1); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); } @@ -628,10 +550,7 @@ public class AbstractQueuedSynchronizerT try { sync.hasWaiters(null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** @@ -642,10 +561,7 @@ public class AbstractQueuedSynchronizerT try { sync.getWaitQueueLength(null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (NullPointerException success) {} } @@ -657,10 +573,7 @@ public class AbstractQueuedSynchronizerT try { sync.getWaitingThreads(null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (NullPointerException success) {} } @@ -669,15 +582,12 @@ public class AbstractQueuedSynchronizerT */ public void testHasWaitersIAE() { final Mutex sync = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); try { sync2.hasWaiters(c); shouldThrow(); - } catch (IllegalArgumentException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (IllegalArgumentException success) {} } /** @@ -685,14 +595,11 @@ public class AbstractQueuedSynchronizerT */ public void testHasWaitersIMSE() { final Mutex sync = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.hasWaiters(c); shouldThrow(); - } catch (IllegalMonitorStateException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (IllegalMonitorStateException success) {} } @@ -701,15 +608,12 @@ public class AbstractQueuedSynchronizerT */ public void testGetWaitQueueLengthIAE() { final Mutex sync = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); try { sync2.getWaitQueueLength(c); shouldThrow(); - } catch (IllegalArgumentException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (IllegalArgumentException success) {} } /** @@ -717,14 +621,11 @@ public class AbstractQueuedSynchronizerT */ public void testGetWaitQueueLengthIMSE() { final Mutex sync = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.getWaitQueueLength(c); shouldThrow(); - } catch (IllegalMonitorStateException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (IllegalMonitorStateException success) {} } @@ -732,31 +633,25 @@ public class AbstractQueuedSynchronizerT * 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(); + 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(); - } + } catch (IllegalArgumentException success) {} } /** * getWaitingThreads throws IMSE if not synced */ public void testGetWaitingThreadsIMSE() { - final Mutex sync = new Mutex(); - final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); + final Mutex sync = new Mutex(); + final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.getWaitingThreads(c); shouldThrow(); - } catch (IllegalMonitorStateException success) { - } catch (Exception ex) { - unexpectedException(); - } + } catch (IllegalMonitorStateException success) {} } @@ -764,10 +659,10 @@ public class AbstractQueuedSynchronizerT /** * hasWaiters returns true when a thread is waiting, else false */ - public void testHasWaiters() { - final Mutex sync = new Mutex(); + public void testHasWaiters() throws InterruptedException { + 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); @@ -776,40 +671,35 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + 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(); - } + 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()); } /** * getWaitQueueLength returns number of waiting threads */ - public void testGetWaitQueueLength() { - final Mutex sync = new Mutex(); + public void testGetWaitQueueLength() throws InterruptedException { + 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); @@ -818,13 +708,13 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -833,44 +723,39 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + 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(); - } + 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()); } /** * getWaitingThreads returns only and all waiting threads */ - public void testGetWaitingThreads() { - final Mutex sync = new Mutex(); + public void testGetWaitingThreads() throws InterruptedException { + 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); @@ -878,13 +763,13 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -892,39 +777,34 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + 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(); - } + 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()); } @@ -932,10 +812,10 @@ public class AbstractQueuedSynchronizerT /** * awaitUninterruptibly doesn't abort on interrupt */ - public void testAwaitUninterruptibly() { - final Mutex sync = new Mutex(); + public void testAwaitUninterruptibly() throws InterruptedException { + 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(); @@ -943,29 +823,23 @@ public class AbstractQueuedSynchronizerT } }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - sync.acquire(1); - c.signal(); - sync.release(1); - assert(t.isInterrupted()); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); - } - catch (Exception ex) { - unexpectedException(); - } + 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()); } /** * await is interruptible */ - public void testAwait_Interrupt() { - final Mutex sync = new Mutex(); + public void testAwait_Interrupt() throws InterruptedException { + 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); @@ -973,30 +847,25 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + 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(); - } + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); } /** * awaitNanos is interruptible */ - public void testAwaitNanos_Interrupt() { - final Mutex sync = new Mutex(); + public void testAwaitNanos_Interrupt() throws InterruptedException { + 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); @@ -1004,30 +873,25 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + 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(); - } + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); } /** * awaitUntil is interruptible */ - public void testAwaitUntil_Interrupt() { - final Mutex sync = new Mutex(); + public void testAwaitUntil_Interrupt() throws InterruptedException { + 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); @@ -1036,70 +900,60 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + 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(); - } + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(SHORT_DELAY_MS); + assertFalse(t.isAlive()); } /** * signalAll wakes up all threads */ - public void testSignalAll() { - final Mutex sync = new Mutex(); + public void testSignalAll() throws InterruptedException { + 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); c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); c.await(); sync.release(1); } - catch(InterruptedException e) { + 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(); - } + 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()); } @@ -1118,25 +972,20 @@ public class AbstractQueuedSynchronizerT /** * A serialized AQS deserializes with current state */ - public void testSerialization() { + public void testSerialization() throws Exception { 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(); - } + 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()); } @@ -1165,7 +1014,7 @@ public class AbstractQueuedSynchronizerT /** * acquireSharedInterruptibly returns after release, but not before */ - public void testAcquireSharedInterruptibly() { + public void testAcquireSharedInterruptibly() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { @@ -1174,28 +1023,25 @@ public class AbstractQueuedSynchronizerT threadAssertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); threadAssertTrue(l.isSignalled()); - } catch(InterruptedException e){ + } catch (InterruptedException e) { threadUnexpectedException(); } } }); - try { - t.start(); - assertFalse(l.isSignalled()); - Thread.sleep(SHORT_DELAY_MS); - l.releaseShared(0); - assertTrue(l.isSignalled()); - t.join(); - } catch (InterruptedException e){ - unexpectedException(); - } + + t.start(); + assertFalse(l.isSignalled()); + Thread.sleep(SHORT_DELAY_MS); + l.releaseShared(0); + assertTrue(l.isSignalled()); + t.join(); } - + /** * acquireSharedTimed returns after release */ - public void testAsquireSharedTimed() { + public void testAsquireSharedTimed() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { @@ -1205,27 +1051,24 @@ public class AbstractQueuedSynchronizerT threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); threadAssertTrue(l.isSignalled()); - } catch(InterruptedException e){ + } catch (InterruptedException e) { threadUnexpectedException(); } } }); - try { - t.start(); - assertFalse(l.isSignalled()); - Thread.sleep(SHORT_DELAY_MS); - l.releaseShared(0); - assertTrue(l.isSignalled()); - t.join(); - } catch (InterruptedException e){ - unexpectedException(); - } + + t.start(); + assertFalse(l.isSignalled()); + Thread.sleep(SHORT_DELAY_MS); + l.releaseShared(0); + assertTrue(l.isSignalled()); + t.join(); } - + /** * acquireSharedInterruptibly throws IE if interrupted before released */ - public void testAcquireSharedInterruptibly_InterruptedException() { + public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { @@ -1233,68 +1076,56 @@ public class AbstractQueuedSynchronizerT threadAssertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success) {} } }); t.start(); - try { - assertFalse(l.isSignalled()); - t.interrupt(); - t.join(); - } catch (InterruptedException e){ - unexpectedException(); - } + assertFalse(l.isSignalled()); + t.interrupt(); + t.join(); } /** * acquireSharedTimed throws IE if interrupted before released */ - public void testAcquireSharedNanos_InterruptedException() { + public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { try { threadAssertFalse(l.isSignalled()); l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); - threadShouldThrow(); - } catch(InterruptedException success){} + threadShouldThrow(); + } catch (InterruptedException success) {} } }); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - assertFalse(l.isSignalled()); - t.interrupt(); - t.join(); - } catch (InterruptedException e){ - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + assertFalse(l.isSignalled()); + t.interrupt(); + t.join(); } /** * acquireSharedTimed times out if not released before timeout */ - public void testAcquireSharedNanos_Timeout() { + public void testAcquireSharedNanos_Timeout() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new Runnable() { public void run() { try { threadAssertFalse(l.isSignalled()); threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); - } catch(InterruptedException ie){ + } catch (InterruptedException ie) { threadUnexpectedException(); } } }); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - assertFalse(l.isSignalled()); - t.join(); - } catch (InterruptedException e){ - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + assertFalse(l.isSignalled()); + t.join(); } - + }