--- jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java 2009/11/21 02:07:26 1.11 +++ jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java 2011/05/06 11:22:07 1.23 @@ -1,7 +1,7 @@ /* * 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 + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -10,12 +10,13 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.concurrent.locks.*; import java.io.*; public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(AbstractQueuedLongSynchronizerTest.class); @@ -46,7 +47,9 @@ public class AbstractQueuedLongSynchroni return true; } - public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } + public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { + return new AbstractQueuedLongSynchronizer.ConditionObject(); + } } @@ -58,7 +61,7 @@ public class AbstractQueuedLongSynchroni public boolean isSignalled() { return getState() != 0; } public long tryAcquireShared(long ignore) { - return isSignalled()? 1 : -1; + return isSignalled() ? 1 : -1; } public boolean tryReleaseShared(long ignore) { @@ -131,16 +134,16 @@ public class AbstractQueuedLongSynchroni assertFalse(sync.hasQueuedThreads()); sync.acquire(1); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasQueuedThreads()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasQueuedThreads()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasQueuedThreads()); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.hasQueuedThreads()); t1.join(); t2.join(); @@ -168,20 +171,20 @@ public class AbstractQueuedLongSynchroni assertFalse(sync.isQueued(t2)); sync.acquire(1); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.isQueued(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.isQueued(t1)); assertTrue(sync.isQueued(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.isQueued(t1)); assertTrue(sync.isQueued(t2)); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.isQueued(t1)); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.isQueued(t2)); t1.join(); t2.join(); @@ -197,17 +200,17 @@ public class AbstractQueuedLongSynchroni assertNull(sync.getFirstQueuedThread()); sync.acquire(1); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(t1, sync.getFirstQueuedThread()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(t1, sync.getFirstQueuedThread()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(t2, sync.getFirstQueuedThread()); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertNull(sync.getFirstQueuedThread()); t1.join(); t2.join(); @@ -224,16 +227,16 @@ public class AbstractQueuedLongSynchroni assertFalse(sync.hasContended()); sync.acquire(1); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasContended()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasContended()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasContended()); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasContended()); t1.join(); t2.join(); @@ -250,18 +253,18 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); assertTrue(sync.getQueuedThreads().isEmpty()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getQueuedThreads().contains(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getQueuedThreads().contains(t1)); assertTrue(sync.getQueuedThreads().contains(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.getQueuedThreads().contains(t1)); assertTrue(sync.getQueuedThreads().contains(t2)); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getQueuedThreads().isEmpty()); t1.join(); t2.join(); @@ -278,18 +281,18 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); t1.join(); t2.join(); @@ -306,16 +309,16 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); assertTrue(sync.getSharedQueuedThreads().isEmpty()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getSharedQueuedThreads().isEmpty()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getSharedQueuedThreads().isEmpty()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getSharedQueuedThreads().isEmpty()); sync.release(1); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.getSharedQueuedThreads().isEmpty()); t1.join(); t2.join(); @@ -329,10 +332,11 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); + sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); }}); t.start(); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -346,7 +350,7 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); Thread t = new Thread(new CheckedRunnable() { public void realRun() { - threadAssertFalse(sync.tryAcquire(1)); + assertFalse(sync.tryAcquire(1)); }}); t.start(); @@ -362,7 +366,8 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); + long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); + assertFalse(sync.tryAcquireNanos(1, nanos)); }}); t.start(); @@ -383,12 +388,12 @@ public class AbstractQueuedLongSynchroni Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); sync.release(1); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.isHeldExclusively()); t.join(); assertFalse(sync.isHeldExclusively()); @@ -403,9 +408,9 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); Thread t = new Thread(new InterruptedSyncRunnable(sync)); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); sync.release(1); t.join(); } @@ -418,6 +423,7 @@ public class AbstractQueuedLongSynchroni sync.acquireInterruptibly(1); Thread t = new Thread(new InterruptedSyncRunnable(sync)); t.start(); + delay(SHORT_DELAY_MS); t.interrupt(); assertTrue(sync.isHeldExclusively()); t.join(); @@ -471,13 +477,13 @@ public class AbstractQueuedLongSynchroni } /** - * Timed await without a signal times out + * Timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); sync.acquire(1); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); sync.release(1); } @@ -507,7 +513,7 @@ public class AbstractQueuedLongSynchroni }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); sync.acquire(1); c.signal(); sync.release(1); @@ -640,20 +646,20 @@ public class AbstractQueuedLongSynchroni Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - threadAssertFalse(sync.hasWaiters(c)); - threadAssertEquals(0, sync.getWaitQueueLength(c)); + assertFalse(sync.hasWaiters(c)); + assertEquals(0, sync.getWaitQueueLength(c)); c.await(); sync.release(1); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(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); + delay(SHORT_DELAY_MS); sync.acquire(1); assertFalse(sync.hasWaiters(c)); assertEquals(0, sync.getWaitQueueLength(c)); @@ -671,8 +677,8 @@ public class AbstractQueuedLongSynchroni Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - threadAssertFalse(sync.hasWaiters(c)); - threadAssertEquals(0, sync.getWaitQueueLength(c)); + assertFalse(sync.hasWaiters(c)); + assertEquals(0, sync.getWaitQueueLength(c)); c.await(); sync.release(1); }}); @@ -680,22 +686,22 @@ public class AbstractQueuedLongSynchroni Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - threadAssertTrue(sync.hasWaiters(c)); - threadAssertEquals(1, sync.getWaitQueueLength(c)); + assertTrue(sync.hasWaiters(c)); + assertEquals(1, sync.getWaitQueueLength(c)); c.await(); sync.release(1); }}); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(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); + delay(SHORT_DELAY_MS); sync.acquire(1); assertFalse(sync.hasWaiters(c)); assertEquals(0, sync.getWaitQueueLength(c)); @@ -715,7 +721,7 @@ public class AbstractQueuedLongSynchroni Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); + assertTrue(sync.getWaitingThreads(c).isEmpty()); c.await(); sync.release(1); }}); @@ -723,7 +729,7 @@ public class AbstractQueuedLongSynchroni Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); + assertFalse(sync.getWaitingThreads(c).isEmpty()); c.await(); sync.release(1); }}); @@ -732,16 +738,16 @@ public class AbstractQueuedLongSynchroni assertTrue(sync.getWaitingThreads(c).isEmpty()); sync.release(1); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(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); + delay(SHORT_DELAY_MS); sync.acquire(1); assertFalse(sync.hasWaiters(c)); assertTrue(sync.getWaitingThreads(c).isEmpty()); @@ -768,7 +774,7 @@ public class AbstractQueuedLongSynchroni }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); sync.acquire(1); c.signal(); @@ -787,11 +793,10 @@ public class AbstractQueuedLongSynchroni public void realRun() throws InterruptedException { sync.acquire(1); c.await(); - sync.release(1); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -806,12 +811,11 @@ public class AbstractQueuedLongSynchroni Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { sync.acquire(1); - c.awaitNanos(1000 * 1000 * 1000); // 1 sec - sync.release(1); + c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -828,11 +832,10 @@ public class AbstractQueuedLongSynchroni sync.acquire(1); java.util.Date d = new java.util.Date(); c.awaitUntil(new java.util.Date(d.getTime() + 10000)); - sync.release(1); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -860,7 +863,7 @@ public class AbstractQueuedLongSynchroni t1.start(); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); sync.acquire(1); c.signalAll(); sync.release(1); @@ -933,14 +936,14 @@ public class AbstractQueuedLongSynchroni Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(l.isSignalled()); + assertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); - threadAssertTrue(l.isSignalled()); + assertTrue(l.isSignalled()); }}); t.start(); assertFalse(l.isSignalled()); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); l.releaseShared(0); assertTrue(l.isSignalled()); t.join(); @@ -950,19 +953,20 @@ public class AbstractQueuedLongSynchroni /** * acquireSharedTimed returns after release */ - public void testAsquireSharedTimed() throws InterruptedException { + public void testAcquireSharedTimed() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(l.isSignalled()); - threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); - threadAssertTrue(l.isSignalled()); + assertFalse(l.isSignalled()); + long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); + assertTrue(l.tryAcquireSharedNanos(0, nanos)); + assertTrue(l.isSignalled()); }}); t.start(); assertFalse(l.isSignalled()); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); l.releaseShared(0); assertTrue(l.isSignalled()); t.join(); @@ -971,11 +975,12 @@ public class AbstractQueuedLongSynchroni /** * acquireSharedInterruptibly throws IE if interrupted before released */ - public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { + public void testAcquireSharedInterruptibly_InterruptedException() + throws InterruptedException { final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(l.isSignalled()); + assertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); }}); @@ -992,12 +997,13 @@ public class AbstractQueuedLongSynchroni final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(l.isSignalled()); - l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); + assertFalse(l.isSignalled()); + long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); + l.tryAcquireSharedNanos(0, nanos); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(l.isSignalled()); t.interrupt(); t.join(); @@ -1010,12 +1016,13 @@ public class AbstractQueuedLongSynchroni final BooleanLatch l = new BooleanLatch(); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(l.isSignalled()); - threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); + assertFalse(l.isSignalled()); + long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); + assertFalse(l.tryAcquireSharedNanos(0, nanos)); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(l.isSignalled()); t.join(); }