--- jsr166/src/test/tck/ReentrantLockTest.java 2009/11/21 02:07:27 1.30 +++ jsr166/src/test/tck/ReentrantLockTest.java 2011/05/06 11:22:07 1.40 @@ -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. */ @@ -9,12 +9,13 @@ import junit.framework.*; import java.util.concurrent.locks.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.*; import java.io.*; public class ReentrantLockTest 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(ReentrantLockTest.class); @@ -55,8 +56,6 @@ public class ReentrantLockTest extends J public Collection getWaitingThreads(Condition c) { return super.getWaitingThreads(c); } - - } /** @@ -121,16 +120,16 @@ public class ReentrantLockTest extends J assertFalse(lock.hasQueuedThreads()); lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.hasQueuedThreads()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.hasQueuedThreads()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.hasQueuedThreads()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(lock.hasQueuedThreads()); t1.join(); t2.join(); @@ -146,16 +145,16 @@ public class ReentrantLockTest extends J assertEquals(0, lock.getQueueLength()); lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, lock.getQueueLength()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(2, lock.getQueueLength()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, lock.getQueueLength()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(0, lock.getQueueLength()); t1.join(); t2.join(); @@ -171,16 +170,16 @@ public class ReentrantLockTest extends J assertEquals(0, lock.getQueueLength()); lock.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, lock.getQueueLength()); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(2, lock.getQueueLength()); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, lock.getQueueLength()); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(0, lock.getQueueLength()); t1.join(); t2.join(); @@ -208,20 +207,20 @@ public class ReentrantLockTest extends J assertFalse(sync.hasQueuedThread(t2)); sync.lock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasQueuedThread(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(sync.hasQueuedThread(t1)); assertTrue(sync.hasQueuedThread(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.hasQueuedThread(t1)); assertTrue(sync.hasQueuedThread(t2)); sync.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.hasQueuedThread(t1)); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(sync.hasQueuedThread(t2)); t1.join(); t2.join(); @@ -239,18 +238,18 @@ public class ReentrantLockTest extends J lock.lock(); assertTrue(lock.getQueuedThreads().isEmpty()); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.getQueuedThreads().contains(t1)); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.getQueuedThreads().contains(t1)); assertTrue(lock.getQueuedThreads().contains(t2)); t1.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertFalse(lock.getQueuedThreads().contains(t1)); assertTrue(lock.getQueuedThreads().contains(t2)); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.getQueuedThreads().isEmpty()); t1.join(); t2.join(); @@ -265,10 +264,11 @@ public class ReentrantLockTest extends J lock.lock(); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); + lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); }}); t.start(); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -282,7 +282,7 @@ public class ReentrantLockTest extends J lock.lock(); Thread t = new Thread(new CheckedRunnable() { public void realRun() { - threadAssertFalse(lock.tryLock()); + assertFalse(lock.tryLock()); }}); t.start(); @@ -298,7 +298,7 @@ public class ReentrantLockTest extends J lock.lock(); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); + assertFalse(lock.tryLock(1, MILLISECONDS)); }}); t.start(); @@ -334,12 +334,12 @@ public class ReentrantLockTest extends J Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); lock.unlock(); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(lock.isLocked()); t.join(); assertFalse(lock.isLocked()); @@ -354,9 +354,9 @@ public class ReentrantLockTest extends J lock.lock(); Thread t = new Thread(new InterruptedLockRunnable(lock)); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.unlock(); t.join(); } @@ -369,6 +369,7 @@ public class ReentrantLockTest extends J lock.lockInterruptibly(); Thread t = new Thread(new InterruptedLockRunnable(lock)); t.start(); + delay(SHORT_DELAY_MS); t.interrupt(); assertTrue(lock.isLocked()); assertTrue(lock.isHeldByCurrentThread()); @@ -412,13 +413,13 @@ public class ReentrantLockTest extends J } /** - * timed await without a signal times out + * timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); lock.lock(); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); lock.unlock(); } @@ -448,7 +449,7 @@ public class ReentrantLockTest extends J }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); c.signal(); lock.unlock(); @@ -578,20 +579,20 @@ public class ReentrantLockTest extends J Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertFalse(lock.hasWaiters(c)); - threadAssertEquals(0, lock.getWaitQueueLength(c)); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertTrue(lock.hasWaiters(c)); assertEquals(1, lock.getWaitQueueLength(c)); c.signal(); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); @@ -609,8 +610,8 @@ public class ReentrantLockTest extends J Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertFalse(lock.hasWaiters(c)); - threadAssertEquals(0, lock.getWaitQueueLength(c)); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); }}); @@ -618,22 +619,22 @@ public class ReentrantLockTest extends J Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertTrue(lock.hasWaiters(c)); - threadAssertEquals(1, lock.getWaitQueueLength(c)); + assertTrue(lock.hasWaiters(c)); + assertEquals(1, lock.getWaitQueueLength(c)); c.await(); lock.unlock(); }}); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertTrue(lock.hasWaiters(c)); assertEquals(2, lock.getWaitQueueLength(c)); c.signalAll(); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); @@ -653,7 +654,7 @@ public class ReentrantLockTest extends J Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); + assertTrue(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.unlock(); }}); @@ -661,7 +662,7 @@ public class ReentrantLockTest extends J Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); + assertFalse(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.unlock(); }}); @@ -670,16 +671,16 @@ public class ReentrantLockTest extends J assertTrue(lock.getWaitingThreads(c).isEmpty()); lock.unlock(); t1.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertTrue(lock.hasWaiters(c)); assertTrue(lock.getWaitingThreads(c).contains(t1)); assertTrue(lock.getWaitingThreads(c).contains(t2)); c.signalAll(); lock.unlock(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); assertFalse(lock.hasWaiters(c)); assertTrue(lock.getWaitingThreads(c).isEmpty()); @@ -728,7 +729,7 @@ public class ReentrantLockTest extends J thread.start(); while (!thread.lockStarted) { - Thread.sleep(100); + delay(100); } lock.lock(); @@ -758,7 +759,7 @@ public class ReentrantLockTest extends J }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -773,11 +774,11 @@ public class ReentrantLockTest extends J Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - c.awaitNanos(1000 * 1000 * 1000); // 1 sec + 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()); @@ -797,7 +798,7 @@ public class ReentrantLockTest extends J }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -825,7 +826,7 @@ public class ReentrantLockTest extends J t1.start(); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); c.signalAll(); lock.unlock(); @@ -844,9 +845,9 @@ public class ReentrantLockTest extends J Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); - threadAssertEquals(1, lock.getHoldCount()); + assertEquals(1, lock.getHoldCount()); c.await(); - threadAssertEquals(1, lock.getHoldCount()); + assertEquals(1, lock.getHoldCount()); lock.unlock(); }}); @@ -854,16 +855,16 @@ public class ReentrantLockTest extends J public void realRun() throws InterruptedException { lock.lock(); lock.lock(); - threadAssertEquals(2, lock.getHoldCount()); + assertEquals(2, lock.getHoldCount()); c.await(); - threadAssertEquals(2, lock.getHoldCount()); + assertEquals(2, lock.getHoldCount()); lock.unlock(); lock.unlock(); }}); t1.start(); t2.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); lock.lock(); c.signalAll(); lock.unlock(); @@ -886,7 +887,7 @@ public class ReentrantLockTest extends J new ObjectOutputStream(new BufferedOutputStream(bout)); out.writeObject(l); out.close(); - + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStream in =