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.AbstractQueuedLongSynchronizer; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
17 |
|
|
18 |
+ |
import junit.framework.AssertionFailedError; |
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
+ |
|
22 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
95 |
|
} |
96 |
|
|
97 |
|
public boolean tryReleaseShared(long ignore) { |
98 |
< |
setState(1 << 62); |
98 |
> |
setState(1L << 62); |
99 |
|
return true; |
100 |
|
} |
101 |
|
} |
125 |
|
} |
126 |
|
|
127 |
|
/** A constant to clarify calls to checking methods below. */ |
128 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
128 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
129 |
|
|
130 |
|
/** |
131 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
203 |
|
new HashSet<Thread>(Arrays.asList(threads))); |
204 |
|
} |
205 |
|
|
206 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
206 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
207 |
|
|
208 |
|
/** |
209 |
|
* Awaits condition using the specified AwaitMethod. |
226 |
|
case awaitUntil: |
227 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
228 |
|
break; |
229 |
+ |
default: |
230 |
+ |
throw new AssertionError(); |
231 |
|
} |
232 |
|
} |
233 |
|
|
236 |
|
* default timeout duration). |
237 |
|
*/ |
238 |
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
239 |
< |
long timeoutMillis = timeoutMillis(); |
240 |
< |
long startTime = System.nanoTime(); |
239 |
> |
final long timeoutMillis = timeoutMillis(); |
240 |
> |
final long startTime; |
241 |
|
try { |
242 |
|
switch (awaitMethod) { |
243 |
|
case awaitTimed: |
244 |
+ |
startTime = System.nanoTime(); |
245 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
246 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
247 |
|
break; |
248 |
|
case awaitNanos: |
249 |
+ |
startTime = System.nanoTime(); |
250 |
|
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
251 |
|
long nanosRemaining = c.awaitNanos(nanosTimeout); |
252 |
|
assertTrue(nanosRemaining <= 0); |
253 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
254 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
255 |
|
break; |
256 |
|
case awaitUntil: |
257 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
258 |
+ |
// use the same time source, so don't use nanoTime here. |
259 |
+ |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
260 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
261 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
262 |
|
break; |
263 |
|
default: |
264 |
|
throw new UnsupportedOperationException(); |
265 |
|
} |
266 |
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
248 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
966 |
|
*/ |
967 |
|
public void testAwaitUninterruptibly() { |
968 |
|
final Mutex sync = new Mutex(); |
969 |
< |
final ConditionObject c = sync.newCondition(); |
969 |
> |
final ConditionObject condition = sync.newCondition(); |
970 |
|
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
971 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
972 |
|
public void realRun() { |
973 |
|
sync.acquire(); |
974 |
|
assertTrue(pleaseInterrupt.releaseShared(0)); |
975 |
< |
c.awaitUninterruptibly(); |
975 |
> |
condition.awaitUninterruptibly(); |
976 |
|
assertTrue(Thread.interrupted()); |
977 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
977 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
978 |
|
sync.release(); |
979 |
|
}}); |
980 |
|
|
981 |
|
pleaseInterrupt.acquireShared(0); |
982 |
|
sync.acquire(); |
983 |
< |
assertHasWaitersLocked(sync, c, t); |
983 |
> |
assertHasWaitersLocked(sync, condition, t); |
984 |
|
sync.release(); |
985 |
|
t.interrupt(); |
986 |
< |
assertHasWaitersUnlocked(sync, c, t); |
987 |
< |
assertThreadStaysAlive(t); |
986 |
> |
assertHasWaitersUnlocked(sync, condition, t); |
987 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
988 |
|
sync.acquire(); |
989 |
< |
assertHasWaitersLocked(sync, c, t); |
989 |
> |
assertHasWaitersLocked(sync, condition, t); |
990 |
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
991 |
< |
c.signal(); |
992 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
991 |
> |
condition.signal(); |
992 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
993 |
|
assertHasExclusiveQueuedThreads(sync, t); |
994 |
|
sync.release(); |
995 |
|
awaitTermination(t); |
1132 |
|
|
1133 |
|
waitForQueuedThread(l, t); |
1134 |
|
assertFalse(l.isSignalled()); |
1135 |
< |
assertThreadStaysAlive(t); |
1135 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1136 |
|
assertHasSharedQueuedThreads(l, t); |
1137 |
|
assertTrue(l.releaseShared(0)); |
1138 |
|
assertTrue(l.isSignalled()); |
1157 |
|
|
1158 |
|
waitForQueuedThread(l, t); |
1159 |
|
assertFalse(l.isSignalled()); |
1160 |
< |
assertThreadStaysAlive(t); |
1160 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1161 |
|
assertTrue(l.releaseShared(0)); |
1162 |
|
assertTrue(l.isSignalled()); |
1163 |
|
awaitTermination(t); |
1205 |
|
*/ |
1206 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1207 |
|
final BooleanLatch l = new BooleanLatch(); |
1208 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1209 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1210 |
|
public void realRun() throws InterruptedException { |
1211 |
|
assertFalse(l.isSignalled()); |
1212 |
< |
long startTime = System.nanoTime(); |
1213 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1214 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1215 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1212 |
> |
for (long millis = timeoutMillis(); |
1213 |
> |
!observedQueued.isSignalled(); |
1214 |
> |
millis *= 2) { |
1215 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1216 |
> |
long startTime = System.nanoTime(); |
1217 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1218 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1219 |
> |
} |
1220 |
|
assertFalse(l.isSignalled()); |
1221 |
|
}}); |
1222 |
|
|
1223 |
|
waitForQueuedThread(l, t); |
1224 |
+ |
observedQueued.releaseShared(0); |
1225 |
|
assertFalse(l.isSignalled()); |
1226 |
|
awaitTermination(t); |
1227 |
|
assertFalse(l.isSignalled()); |
1228 |
|
} |
1229 |
|
|
1230 |
+ |
/** |
1231 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1232 |
+ |
*/ |
1233 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1234 |
+ |
final Mutex sync = new Mutex(); |
1235 |
+ |
final ConditionObject c = sync.newCondition(); |
1236 |
+ |
sync.acquire(); |
1237 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1238 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1239 |
+ |
sync.release(); |
1240 |
+ |
} |
1241 |
+ |
|
1242 |
+ |
/** |
1243 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1244 |
+ |
*/ |
1245 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1246 |
+ |
final Mutex sync = new Mutex(); |
1247 |
+ |
final ConditionObject c = sync.newCondition(); |
1248 |
+ |
sync.acquire(); |
1249 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1250 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1251 |
+ |
sync.release(); |
1252 |
+ |
} |
1253 |
+ |
|
1254 |
|
} |