--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2011/05/21 06:24:33 1.41 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2014/12/02 07:23:13 1.45 @@ -9,6 +9,7 @@ import junit.framework.*; import java.util.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; @@ -121,7 +122,7 @@ public class AbstractQueuedSynchronizerT } /** A constant to clarify calls to checking methods below. */ - final static Thread[] NO_THREADS = new Thread[0]; + static final Thread[] NO_THREADS = new Thread[0]; /** * Spin-waits until sync.isQueued(t) becomes true. @@ -947,23 +948,23 @@ public class AbstractQueuedSynchronizerT } /** - * awaitUninterruptibly doesn't abort on interrupt + * awaitUninterruptibly is uninterruptible */ public void testAwaitUninterruptibly() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); - final BooleanLatch acquired = new BooleanLatch(); + final BooleanLatch pleaseInterrupt = new BooleanLatch(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { sync.acquire(); - assertTrue(acquired.releaseShared(0)); + assertTrue(pleaseInterrupt.releaseShared(0)); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); assertHasWaitersLocked(sync, c, NO_THREADS); sync.release(); }}); - acquired.acquireShared(0); + pleaseInterrupt.acquireShared(0); sync.acquire(); assertHasWaitersLocked(sync, c, t); sync.release(); @@ -990,15 +991,15 @@ public class AbstractQueuedSynchronizerT public void testInterruptible(final AwaitMethod awaitMethod) { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); - final BooleanLatch acquired = new BooleanLatch(); + final BooleanLatch pleaseInterrupt = new BooleanLatch(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { sync.acquire(); - assertTrue(acquired.releaseShared(0)); + assertTrue(pleaseInterrupt.releaseShared(0)); await(c, awaitMethod); }}); - acquired.acquireShared(0); + pleaseInterrupt.acquireShared(0); t.interrupt(); awaitTermination(t); } @@ -1190,20 +1191,51 @@ public class AbstractQueuedSynchronizerT */ public void testTryAcquireSharedNanos_Timeout() { final BooleanLatch l = new BooleanLatch(); + final BooleanLatch observedQueued = new BooleanLatch(); + final long timeoutMillis = timeoutMillis(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); - long startTime = System.nanoTime(); - long nanos = MILLISECONDS.toNanos(timeoutMillis()); - assertFalse(l.tryAcquireSharedNanos(0, nanos)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + for (long millis = timeoutMillis(); + !observedQueued.isSignalled(); + millis *= 2) { + long nanos = MILLISECONDS.toNanos(millis); + long startTime = System.nanoTime(); + assertFalse(l.tryAcquireSharedNanos(0, nanos)); + assertTrue(millisElapsedSince(startTime) >= millis); + } assertFalse(l.isSignalled()); }}); waitForQueuedThread(l, t); + observedQueued.releaseShared(0); assertFalse(l.isSignalled()); awaitTermination(t); assertFalse(l.isSignalled()); } + /** + * awaitNanos/timed await with 0 wait times out immediately + */ + public void testAwait_Zero() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(0L) <= 0); + assertFalse(c.await(0L, NANOSECONDS)); + sync.release(); + } + + /** + * awaitNanos/timed await with maximum negative wait times does not underflow + */ + public void testAwait_NegativeInfinity() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); + assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); + sync.release(); + } + }