--- jsr166/src/test/tck/ReentrantLockTest.java 2011/05/24 23:40:14 1.49 +++ jsr166/src/test/tck/ReentrantLockTest.java 2015/04/25 04:55:31 1.58 @@ -6,39 +6,46 @@ * Pat Fisher, Mike Judd. */ -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.*; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +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(); } @@ -82,7 +89,7 @@ public class ReentrantLockTest extends J Thread.yield(); } assertTrue(t.isAlive()); - assertTrue(lock.getOwner() != t); + assertNotSame(t, lock.getOwner()); } /** @@ -136,7 +143,7 @@ public class ReentrantLockTest extends J lock.unlock(); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -159,6 +166,8 @@ public class ReentrantLockTest extends J case awaitUntil: assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -451,9 +460,7 @@ public class ReentrantLockTest extends J barrier.await(); awaitTermination(t); assertFalse(lock.isLocked()); - } catch (Exception e) { - threadUnexpectedException(e); - } + } catch (Exception fail) { threadUnexpectedException(fail); } } /** @@ -465,9 +472,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); @@ -528,9 +533,7 @@ public class ReentrantLockTest extends J assertTrue(nanosRemaining <= 0); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -548,9 +551,7 @@ public class ReentrantLockTest extends J assertFalse(c.await(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -569,9 +570,7 @@ public class ReentrantLockTest extends J assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -735,20 +734,20 @@ public class ReentrantLockTest extends J public void testHasWaiters(boolean fair) { final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - final CountDownLatch locked = new CountDownLatch(1); + final CountDownLatch pleaseSignal = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); assertHasNoWaiters(lock, c); assertFalse(lock.hasWaiters(c)); - locked.countDown(); + pleaseSignal.countDown(); c.await(); assertHasNoWaiters(lock, c); assertFalse(lock.hasWaiters(c)); lock.unlock(); }}); - await(locked); + await(pleaseSignal); lock.lock(); assertHasWaiters(lock, c, t); assertTrue(lock.hasWaiters(c)); @@ -879,33 +878,50 @@ public class ReentrantLockTest extends J } /** - * awaitUninterruptibly doesn't abort on interrupt + * awaitUninterruptibly is uninterruptible */ public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } public void testAwaitUninterruptibly(boolean fair) { final ReentrantLock lock = new ReentrantLock(fair); final Condition c = lock.newCondition(); - final CountDownLatch locked = new CountDownLatch(1); - Thread t = newStartedThread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(2); + + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { + // Interrupt before awaitUninterruptibly lock.lock(); - locked.countDown(); + pleaseInterrupt.countDown(); + Thread.currentThread().interrupt(); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); - await(locked); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() { + // Interrupt during awaitUninterruptibly + lock.lock(); + pleaseInterrupt.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.unlock(); + }}); + + await(pleaseInterrupt); lock.lock(); lock.unlock(); - t.interrupt(); - long timeoutMillis = 10; - assertThreadStaysAlive(t, timeoutMillis); + t2.interrupt(); + + assertThreadStaysAlive(t1); + assertTrue(t2.isAlive()); + lock.lock(); - c.signal(); + c.signalAll(); lock.unlock(); - awaitTermination(t); + + awaitTermination(t1); + awaitTermination(t2); } /** @@ -923,13 +939,13 @@ public class ReentrantLockTest extends J final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - final CountDownLatch locked = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.lock(); assertLockedByMoi(lock); assertHasNoWaiters(lock, c); - locked.countDown(); + pleaseInterrupt.countDown(); try { await(c, awaitMethod); } finally { @@ -940,7 +956,7 @@ public class ReentrantLockTest extends J } }}); - await(locked); + await(pleaseInterrupt); assertHasWaiters(lock, c, t); t.interrupt(); awaitTermination(t); @@ -961,11 +977,11 @@ public class ReentrantLockTest extends J public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - final CountDownLatch locked = new CountDownLatch(2); + final CountDownLatch pleaseSignal = new CountDownLatch(2); class Awaiter extends CheckedRunnable { public void realRun() throws InterruptedException { lock.lock(); - locked.countDown(); + pleaseSignal.countDown(); await(c, awaitMethod); lock.unlock(); } @@ -974,7 +990,7 @@ public class ReentrantLockTest extends J Thread t1 = newStartedThread(new Awaiter()); Thread t2 = newStartedThread(new Awaiter()); - await(locked); + await(pleaseSignal); lock.lock(); assertHasWaiters(lock, c, t1, t2); c.signalAll(); @@ -1039,13 +1055,13 @@ public class ReentrantLockTest extends J public void testAwaitLockCount(boolean fair) { final PublicReentrantLock lock = new PublicReentrantLock(fair); final Condition c = lock.newCondition(); - final CountDownLatch locked = new CountDownLatch(2); + final CountDownLatch pleaseSignal = new CountDownLatch(2); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.lock(); assertLockedByMoi(lock); assertEquals(1, lock.getHoldCount()); - locked.countDown(); + pleaseSignal.countDown(); c.await(); assertLockedByMoi(lock); assertEquals(1, lock.getHoldCount()); @@ -1058,7 +1074,7 @@ public class ReentrantLockTest extends J lock.lock(); assertLockedByMoi(lock); assertEquals(2, lock.getHoldCount()); - locked.countDown(); + pleaseSignal.countDown(); c.await(); assertLockedByMoi(lock); assertEquals(2, lock.getHoldCount()); @@ -1066,7 +1082,7 @@ public class ReentrantLockTest extends J lock.unlock(); }}); - await(locked); + await(pleaseSignal); lock.lock(); assertHasWaiters(lock, c, t1, t2); assertEquals(1, lock.getHoldCount());