--- jsr166/src/test/tck/ReentrantLockTest.java 2011/06/03 05:07:14 1.51 +++ jsr166/src/test/tck/ReentrantLockTest.java 2018/07/22 22:13:55 1.69 @@ -6,40 +6,48 @@ * 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.ArrayList; +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.ThreadLocalRandom; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; +import junit.framework.Test; +import junit.framework.TestSuite; + +@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom 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(); } @@ -79,11 +87,11 @@ public class ReentrantLockTest extends J long startTime = System.nanoTime(); while (!lock.hasQueuedThread(t)) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) - throw new AssertionFailedError("timed out"); + throw new AssertionError("timed out"); Thread.yield(); } assertTrue(t.isAlive()); - assertTrue(lock.getOwner() != t); + assertNotSame(t, lock.getOwner()); } /** @@ -137,10 +145,15 @@ public class ReentrantLockTest extends J lock.unlock(); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } + + static AwaitMethod randomAwaitMethod() { + AwaitMethod[] awaitMethods = AwaitMethod.values(); + return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)]; + } /** - * Awaits condition using the specified AwaitMethod. + * Awaits condition "indefinitely" using the specified AwaitMethod. */ void await(Condition c, AwaitMethod awaitMethod) throws InterruptedException { @@ -153,13 +166,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 +216,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 +406,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 +425,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 +442,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 +468,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 +480,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 +530,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 +550,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 +568,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(); } /** @@ -886,7 +892,7 @@ public class ReentrantLockTest extends J public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } public void testAwaitUninterruptibly(boolean fair) { final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); + final Condition condition = lock.newCondition(); final CountDownLatch pleaseInterrupt = new CountDownLatch(2); Thread t1 = newStartedThread(new CheckedRunnable() { @@ -895,7 +901,7 @@ public class ReentrantLockTest extends J lock.lock(); pleaseInterrupt.countDown(); Thread.currentThread().interrupt(); - c.awaitUninterruptibly(); + condition.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); @@ -905,21 +911,20 @@ public class ReentrantLockTest extends J // Interrupt during awaitUninterruptibly lock.lock(); pleaseInterrupt.countDown(); - c.awaitUninterruptibly(); + condition.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); await(pleaseInterrupt); + t2.interrupt(); lock.lock(); lock.unlock(); - t2.interrupt(); - - assertThreadStaysAlive(t1); - assertTrue(t2.isAlive()); + assertThreadBlocks(t1, Thread.State.WAITING); + assertThreadBlocks(t2, Thread.State.WAITING); lock.lock(); - c.signalAll(); + condition.signalAll(); lock.unlock(); awaitTermination(t1); @@ -1101,7 +1106,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,11 +1132,67 @@ 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")); } + + /** + * Tests scenario for JDK-8187408 + * AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock + */ + public void testBug8187408() throws InterruptedException { + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final AwaitMethod awaitMethod = randomAwaitMethod(); + final int nThreads = rnd.nextInt(2, 10); + final ReentrantLock lock = new ReentrantLock(); + final Condition cond = lock.newCondition(); + final CountDownLatch done = new CountDownLatch(nThreads); + final ArrayList threads = new ArrayList<>(); + + Runnable rogue = () -> { + while (done.getCount() > 0) { + try { + // call await without holding lock?! + await(cond, awaitMethod); + throw new AssertionError("should throw"); + } + catch (IllegalMonitorStateException success) {} + catch (Throwable fail) { threadUnexpectedException(fail); }}}; + Thread rogueThread = new Thread(rogue, "rogue"); + threads.add(rogueThread); + rogueThread.start(); + + Runnable waiter = () -> { + lock.lock(); + try { + done.countDown(); + cond.await(); + } catch (Throwable fail) { + threadUnexpectedException(fail); + } finally { + lock.unlock(); + }}; + for (int i = 0; i < nThreads; i++) { + Thread thread = new Thread(waiter, "waiter"); + threads.add(thread); + thread.start(); + } + + assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + lock.lock(); + try { + assertEquals(nThreads, lock.getWaitQueueLength(cond)); + } finally { + cond.signalAll(); + lock.unlock(); + } + for (Thread thread : threads) { + thread.join(LONG_DELAY_MS); + assertFalse(thread.isAlive()); + } + } }