--- jsr166/src/test/tck/ReentrantLockTest.java 2014/12/31 19:05:43 1.54 +++ jsr166/src/test/tck/ReentrantLockTest.java 2015/07/03 01:25:15 1.62 @@ -22,30 +22,30 @@ 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(); } @@ -146,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 { @@ -159,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(); } } @@ -422,7 +425,7 @@ public class ReentrantLockTest extends J } for (int i = SIZE; i > 0; i--) { lock.unlock(); - assertEquals(i-1, lock.getHoldCount()); + assertEquals(i - 1, lock.getHoldCount()); } } @@ -458,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); } } /** @@ -472,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); @@ -535,9 +534,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); } } /** @@ -555,9 +552,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); } } /** @@ -571,14 +566,10 @@ public class ReentrantLockTest extends J 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); + assertFalse(c.awaitUntil(delayedDate(timeoutMillis()))); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /**