--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/31 16:16:24 1.65 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2015/07/03 05:48:30 1.78 @@ -6,18 +6,24 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; +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.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.concurrent.CountDownLatch; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.*; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ReentrantReadWriteLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ReentrantReadWriteLockTest.class); @@ -85,7 +91,7 @@ public class ReentrantReadWriteLockTest Thread.yield(); } assertTrue(t.isAlive()); - assertTrue(lock.getOwner() != t); + assertNotSame(t, lock.getOwner()); } /** @@ -146,28 +152,32 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + 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 { + long timeoutMillis = 2 * LONG_DELAY_MS; switch (awaitMethod) { case await: c.await(); break; case awaitTimed: - assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); + assertTrue(c.await(timeoutMillis, MILLISECONDS)); break; case awaitNanos: - long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); - assertTrue(nanosRemaining > 0); + long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(timeoutNanos); + assertTrue(nanosRemaining > timeoutNanos / 2); + assertTrue(nanosRemaining <= timeoutNanos); break; case awaitUntil: - java.util.Date d = new java.util.Date(); - assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); + assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -228,7 +238,7 @@ public class ReentrantReadWriteLockTest } for (int i = SIZE; i > 0; i--) { lock.writeLock().unlock(); - assertEquals(i-1,lock.getWriteHoldCount()); + assertEquals(i - 1,lock.getWriteHoldCount()); } } @@ -245,7 +255,7 @@ public class ReentrantReadWriteLockTest } for (int i = SIZE; i > 0; i--) { lock.writeLock().unlock(); - assertEquals(i-1,lock.writeLock().getHoldCount()); + assertEquals(i - 1,lock.writeLock().getHoldCount()); } } @@ -262,7 +272,7 @@ public class ReentrantReadWriteLockTest } for (int i = SIZE; i > 0; i--) { lock.readLock().unlock(); - assertEquals(i-1,lock.getReadHoldCount()); + assertEquals(i - 1,lock.getReadHoldCount()); } } @@ -822,9 +832,7 @@ public class ReentrantReadWriteLockTest new PublicReentrantReadWriteLock(fair); try { lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); @@ -849,9 +857,7 @@ public class ReentrantReadWriteLockTest lock.readLock().lockInterruptibly(); lock.readLock().unlock(); lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); @@ -877,7 +883,9 @@ public class ReentrantReadWriteLockTest await(c, awaitMethod); shouldThrow(); } catch (IllegalMonitorStateException success) { - } catch (InterruptedException e) { threadUnexpectedException(e); } + } catch (InterruptedException fail) { + threadUnexpectedException(fail); + } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -928,9 +936,7 @@ public class ReentrantReadWriteLockTest assertTrue(nanosRemaining <= 0); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -949,9 +955,7 @@ public class ReentrantReadWriteLockTest assertFalse(c.await(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -965,15 +969,13 @@ public class ReentrantReadWriteLockTest new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); lock.writeLock().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); + // We shouldn't assume that nanoTime and currentTimeMillis + // use the same time source, so don't use nanoTime here. + java.util.Date delayedDate = delayedDate(timeoutMillis()); + assertFalse(c.awaitUntil(delayedDate)); + assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -1005,33 +1007,50 @@ public class ReentrantReadWriteLockTest } /** - * 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 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().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.writeLock().lock(); - locked.countDown(); + pleaseInterrupt.countDown(); + Thread.currentThread().interrupt(); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.writeLock().unlock(); }}); - await(locked); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() { + // Interrupt during awaitUninterruptibly + lock.writeLock().lock(); + pleaseInterrupt.countDown(); + c.awaitUninterruptibly(); + assertTrue(Thread.interrupted()); + lock.writeLock().unlock(); + }}); + + await(pleaseInterrupt); lock.writeLock().lock(); lock.writeLock().unlock(); - t.interrupt(); - long timeoutMillis = 10; - assertThreadStaysAlive(t, timeoutMillis); + t2.interrupt(); + + assertThreadStaysAlive(t1); + assertTrue(t2.isAlive()); + lock.writeLock().lock(); - c.signal(); + c.signalAll(); lock.writeLock().unlock(); - awaitTermination(t); + + awaitTermination(t1); + awaitTermination(t2); } /**