9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
13 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
14 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
15 |
|
|
118 |
|
} |
119 |
|
|
120 |
|
/** A constant to clarify calls to checking methods below. */ |
121 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
121 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
122 |
|
|
123 |
|
/** |
124 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
1188 |
|
*/ |
1189 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1190 |
|
final BooleanLatch l = new BooleanLatch(); |
1191 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1192 |
+ |
final long timeoutMillis = timeoutMillis(); |
1193 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1194 |
|
public void realRun() throws InterruptedException { |
1195 |
|
assertFalse(l.isSignalled()); |
1196 |
< |
long startTime = System.nanoTime(); |
1197 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1198 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1199 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1196 |
> |
for (long millis = timeoutMillis(); |
1197 |
> |
!observedQueued.isSignalled(); |
1198 |
> |
millis *= 2) { |
1199 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1200 |
> |
long startTime = System.nanoTime(); |
1201 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1202 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1203 |
> |
} |
1204 |
|
assertFalse(l.isSignalled()); |
1205 |
|
}}); |
1206 |
|
|
1207 |
|
waitForQueuedThread(l, t); |
1208 |
+ |
observedQueued.releaseShared(0); |
1209 |
|
assertFalse(l.isSignalled()); |
1210 |
|
awaitTermination(t); |
1211 |
|
assertFalse(l.isSignalled()); |
1212 |
|
} |
1213 |
|
|
1214 |
+ |
/** |
1215 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1216 |
+ |
*/ |
1217 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1218 |
+ |
final Mutex sync = new Mutex(); |
1219 |
+ |
final ConditionObject c = sync.newCondition(); |
1220 |
+ |
sync.acquire(); |
1221 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1222 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1223 |
+ |
sync.release(); |
1224 |
+ |
} |
1225 |
+ |
|
1226 |
+ |
/** |
1227 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1228 |
+ |
*/ |
1229 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1230 |
+ |
final Mutex sync = new Mutex(); |
1231 |
+ |
final ConditionObject c = sync.newCondition(); |
1232 |
+ |
sync.acquire(); |
1233 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1234 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1235 |
+ |
sync.release(); |
1236 |
+ |
} |
1237 |
+ |
|
1238 |
|
} |