6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
+ |
|
12 |
+ |
import java.util.Arrays; |
13 |
+ |
import java.util.Collection; |
14 |
+ |
import java.util.HashSet; |
15 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
17 |
|
|
18 |
+ |
import junit.framework.AssertionFailedError; |
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
+ |
|
22 |
|
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(AbstractQueuedSynchronizerTest.class); |
128 |
|
} |
129 |
|
|
130 |
|
/** A constant to clarify calls to checking methods below. */ |
131 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
131 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
132 |
|
|
133 |
|
/** |
134 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
205 |
|
new HashSet<Thread>(Arrays.asList(threads))); |
206 |
|
} |
207 |
|
|
208 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
208 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
209 |
|
|
210 |
|
/** |
211 |
|
* Awaits condition using the specified AwaitMethod. |
228 |
|
case awaitUntil: |
229 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
230 |
|
break; |
231 |
+ |
default: |
232 |
+ |
throw new AssertionError(); |
233 |
|
} |
234 |
|
} |
235 |
|
|
238 |
|
* default timeout duration). |
239 |
|
*/ |
240 |
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
241 |
< |
long timeoutMillis = timeoutMillis(); |
242 |
< |
long startTime = System.nanoTime(); |
241 |
> |
final long timeoutMillis = timeoutMillis(); |
242 |
> |
final long startTime; |
243 |
|
try { |
244 |
|
switch (awaitMethod) { |
245 |
|
case awaitTimed: |
246 |
+ |
startTime = System.nanoTime(); |
247 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
248 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
249 |
|
break; |
250 |
|
case awaitNanos: |
251 |
+ |
startTime = System.nanoTime(); |
252 |
|
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
253 |
|
long nanosRemaining = c.awaitNanos(nanosTimeout); |
254 |
|
assertTrue(nanosRemaining <= 0); |
255 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
256 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
257 |
|
break; |
258 |
|
case awaitUntil: |
259 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
260 |
+ |
// use the same time source, so don't use nanoTime here. |
261 |
+ |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
262 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
263 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
264 |
|
break; |
265 |
|
default: |
266 |
|
throw new UnsupportedOperationException(); |
267 |
|
} |
268 |
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
251 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
967 |
> |
* awaitUninterruptibly is uninterruptible |
968 |
|
*/ |
969 |
|
public void testAwaitUninterruptibly() { |
970 |
|
final Mutex sync = new Mutex(); |
971 |
< |
final ConditionObject c = sync.newCondition(); |
972 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
971 |
> |
final ConditionObject condition = sync.newCondition(); |
972 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
973 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
974 |
|
public void realRun() { |
975 |
|
sync.acquire(); |
976 |
< |
assertTrue(acquired.releaseShared(0)); |
977 |
< |
c.awaitUninterruptibly(); |
976 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
977 |
> |
condition.awaitUninterruptibly(); |
978 |
|
assertTrue(Thread.interrupted()); |
979 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
979 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
980 |
|
sync.release(); |
981 |
|
}}); |
982 |
|
|
983 |
< |
acquired.acquireShared(0); |
983 |
> |
pleaseInterrupt.acquireShared(0); |
984 |
|
sync.acquire(); |
985 |
< |
assertHasWaitersLocked(sync, c, t); |
985 |
> |
assertHasWaitersLocked(sync, condition, t); |
986 |
|
sync.release(); |
987 |
|
t.interrupt(); |
988 |
< |
assertHasWaitersUnlocked(sync, c, t); |
989 |
< |
assertThreadStaysAlive(t); |
988 |
> |
assertHasWaitersUnlocked(sync, condition, t); |
989 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
990 |
|
sync.acquire(); |
991 |
< |
assertHasWaitersLocked(sync, c, t); |
991 |
> |
assertHasWaitersLocked(sync, condition, t); |
992 |
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
993 |
< |
c.signal(); |
994 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
993 |
> |
condition.signal(); |
994 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
995 |
|
assertHasExclusiveQueuedThreads(sync, t); |
996 |
|
sync.release(); |
997 |
|
awaitTermination(t); |
1007 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
1008 |
|
final Mutex sync = new Mutex(); |
1009 |
|
final ConditionObject c = sync.newCondition(); |
1010 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
1010 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
1011 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1012 |
|
public void realRun() throws InterruptedException { |
1013 |
|
sync.acquire(); |
1014 |
< |
assertTrue(acquired.releaseShared(0)); |
1014 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1015 |
|
await(c, awaitMethod); |
1016 |
|
}}); |
1017 |
|
|
1018 |
< |
acquired.acquireShared(0); |
1018 |
> |
pleaseInterrupt.acquireShared(0); |
1019 |
|
t.interrupt(); |
1020 |
|
awaitTermination(t); |
1021 |
|
} |
1207 |
|
*/ |
1208 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1209 |
|
final BooleanLatch l = new BooleanLatch(); |
1210 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1211 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1212 |
|
public void realRun() throws InterruptedException { |
1213 |
|
assertFalse(l.isSignalled()); |
1214 |
< |
long startTime = System.nanoTime(); |
1215 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1216 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1217 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1214 |
> |
for (long millis = timeoutMillis(); |
1215 |
> |
!observedQueued.isSignalled(); |
1216 |
> |
millis *= 2) { |
1217 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1218 |
> |
long startTime = System.nanoTime(); |
1219 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1220 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1221 |
> |
} |
1222 |
|
assertFalse(l.isSignalled()); |
1223 |
|
}}); |
1224 |
|
|
1225 |
|
waitForQueuedThread(l, t); |
1226 |
+ |
observedQueued.releaseShared(0); |
1227 |
|
assertFalse(l.isSignalled()); |
1228 |
|
awaitTermination(t); |
1229 |
|
assertFalse(l.isSignalled()); |
1230 |
|
} |
1231 |
|
|
1232 |
+ |
/** |
1233 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1234 |
+ |
*/ |
1235 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1236 |
+ |
final Mutex sync = new Mutex(); |
1237 |
+ |
final ConditionObject c = sync.newCondition(); |
1238 |
+ |
sync.acquire(); |
1239 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1240 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1241 |
+ |
sync.release(); |
1242 |
+ |
} |
1243 |
+ |
|
1244 |
+ |
/** |
1245 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1246 |
+ |
*/ |
1247 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1248 |
+ |
final Mutex sync = new Mutex(); |
1249 |
+ |
final ConditionObject c = sync.newCondition(); |
1250 |
+ |
sync.acquire(); |
1251 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1252 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1253 |
+ |
sync.release(); |
1254 |
+ |
} |
1255 |
+ |
|
1256 |
|
} |