--- jsr166/src/test/tck/ReentrantLockTest.java 2014/12/31 05:04:04 1.53 +++ jsr166/src/test/tck/ReentrantLockTest.java 2017/01/01 20:34:39 1.64 @@ -6,40 +6,46 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.*; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ReentrantLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ReentrantLockTest.class); } /** - * A runnable calling lockInterruptibly + * A checked runnable calling lockInterruptibly */ class InterruptibleLockRunnable extends CheckedRunnable { final ReentrantLock lock; - InterruptibleLockRunnable(ReentrantLock l) { lock = l; } + InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } public void realRun() throws InterruptedException { lock.lockInterruptibly(); } } /** - * A runnable calling lockInterruptibly that expects to be + * A checked runnable calling lockInterruptibly that expects to be * interrupted */ class InterruptedLockRunnable extends CheckedInterruptedRunnable { final ReentrantLock lock; - InterruptedLockRunnable(ReentrantLock l) { lock = l; } + InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } public void realRun() throws InterruptedException { lock.lockInterruptibly(); } @@ -140,7 +146,7 @@ public class ReentrantLockTest extends J enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** - * Awaits condition using the specified AwaitMethod. + * Awaits condition "indefinitely" using the specified AwaitMethod. */ void await(Condition c, AwaitMethod awaitMethod) throws InterruptedException { @@ -153,13 +159,16 @@ public class ReentrantLockTest extends J assertTrue(c.await(timeoutMillis, MILLISECONDS)); break; case awaitNanos: - long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); - long nanosRemaining = c.awaitNanos(nanosTimeout); - assertTrue(nanosRemaining > 0); + long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(timeoutNanos); + assertTrue(nanosRemaining > timeoutNanos / 2); + assertTrue(nanosRemaining <= timeoutNanos); break; case awaitUntil: assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -200,7 +209,7 @@ public class ReentrantLockTest extends J public void testUnlock_IMSE() { testUnlock_IMSE(false); } public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } public void testUnlock_IMSE(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); try { lock.unlock(); shouldThrow(); @@ -390,11 +399,11 @@ public class ReentrantLockTest extends J public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } public void testTryLock_Timeout(boolean fair) { final PublicReentrantLock lock = new PublicReentrantLock(fair); + final long timeoutMillis = timeoutMillis(); lock.lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { long startTime = System.nanoTime(); - long timeoutMillis = 10; assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); }}); @@ -409,14 +418,14 @@ public class ReentrantLockTest extends J public void testGetHoldCount() { testGetHoldCount(false); } public void testGetHoldCount_fair() { testGetHoldCount(true); } public void testGetHoldCount(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); for (int i = 1; i <= SIZE; i++) { lock.lock(); assertEquals(i, lock.getHoldCount()); } for (int i = SIZE; i > 0; i--) { lock.unlock(); - assertEquals(i-1, lock.getHoldCount()); + assertEquals(i - 1, lock.getHoldCount()); } } @@ -426,8 +435,8 @@ public class ReentrantLockTest extends J public void testIsLocked() { testIsLocked(false); } public void testIsLocked_fair() { testIsLocked(true); } public void testIsLocked(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); try { - final ReentrantLock lock = new ReentrantLock(fair); assertFalse(lock.isLocked()); lock.lock(); assertTrue(lock.isLocked()); @@ -452,9 +461,7 @@ public class ReentrantLockTest extends J barrier.await(); awaitTermination(t); assertFalse(lock.isLocked()); - } catch (Exception e) { - threadUnexpectedException(e); - } + } catch (Exception fail) { threadUnexpectedException(fail); } } /** @@ -466,9 +473,7 @@ public class ReentrantLockTest extends J final PublicReentrantLock lock = new PublicReentrantLock(fair); try { lock.lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } assertLockedByMoi(lock); Thread t = newStartedThread(new InterruptedLockRunnable(lock)); waitForQueuedThread(lock, t); @@ -518,20 +523,18 @@ public class ReentrantLockTest extends J public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } public void testAwaitNanos_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + final long timeoutMillis = timeoutMillis(); + final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + lock.lock(); + final long startTime = System.nanoTime(); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); long nanosRemaining = c.awaitNanos(timeoutNanos); assertTrue(nanosRemaining <= 0); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); } /** @@ -540,18 +543,16 @@ public class ReentrantLockTest extends J public void testAwait_Timeout() { testAwait_Timeout(false); } public void testAwait_Timeout_fair() { testAwait_Timeout(true); } public void testAwait_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + final long timeoutMillis = timeoutMillis(); + lock.lock(); + final long startTime = System.nanoTime(); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; assertFalse(c.await(timeoutMillis, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); } /** @@ -560,19 +561,17 @@ public class ReentrantLockTest extends J public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } public void testAwaitUntil_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + lock.lock(); + // We shouldn't assume that nanoTime and currentTimeMillis + // use the same time source, so don't use nanoTime here. + final java.util.Date delayedDate = delayedDate(timeoutMillis()); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + assertFalse(c.awaitUntil(delayedDate)); + } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); + lock.unlock(); } /** @@ -1101,7 +1100,7 @@ public class ReentrantLockTest extends J public void testSerialization() { testSerialization(false); } public void testSerialization_fair() { testSerialization(true); } public void testSerialization(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); lock.lock(); ReentrantLock clone = serialClone(lock); @@ -1127,10 +1126,10 @@ public class ReentrantLockTest extends J public void testToString() { testToString(false); } public void testToString_fair() { testToString(true); } public void testToString(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); assertTrue(lock.toString().contains("Unlocked")); lock.lock(); - assertTrue(lock.toString().contains("Locked")); + assertTrue(lock.toString().contains("Locked by")); lock.unlock(); assertTrue(lock.toString().contains("Unlocked")); }