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 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
|
public class AbstractQueuedSynchronizerTest 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(AbstractQueuedSynchronizerTest.class); |
206 |
|
new HashSet<Thread>(Arrays.asList(threads))); |
207 |
|
} |
208 |
|
|
209 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
209 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
210 |
|
|
211 |
|
/** |
212 |
|
* Awaits condition using the specified AwaitMethod. |
229 |
|
case awaitUntil: |
230 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
231 |
|
break; |
232 |
+ |
default: |
233 |
+ |
throw new AssertionError(); |
234 |
|
} |
235 |
|
} |
236 |
|
|
239 |
|
* default timeout duration). |
240 |
|
*/ |
241 |
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
242 |
< |
long timeoutMillis = timeoutMillis(); |
243 |
< |
long startTime = System.nanoTime(); |
242 |
> |
final long timeoutMillis = timeoutMillis(); |
243 |
> |
final long startTime; |
244 |
|
try { |
245 |
|
switch (awaitMethod) { |
246 |
|
case awaitTimed: |
247 |
+ |
startTime = System.nanoTime(); |
248 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
249 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
250 |
|
break; |
251 |
|
case awaitNanos: |
252 |
+ |
startTime = System.nanoTime(); |
253 |
|
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
254 |
|
long nanosRemaining = c.awaitNanos(nanosTimeout); |
255 |
|
assertTrue(nanosRemaining <= 0); |
256 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
257 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
258 |
|
break; |
259 |
|
case awaitUntil: |
260 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
261 |
+ |
// use the same time source, so don't use nanoTime here. |
262 |
+ |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
263 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
264 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
265 |
|
break; |
266 |
|
default: |
267 |
|
throw new UnsupportedOperationException(); |
268 |
|
} |
269 |
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
252 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
270 |
|
} |
271 |
|
|
272 |
|
/** |
969 |
|
*/ |
970 |
|
public void testAwaitUninterruptibly() { |
971 |
|
final Mutex sync = new Mutex(); |
972 |
< |
final ConditionObject c = sync.newCondition(); |
972 |
> |
final ConditionObject condition = sync.newCondition(); |
973 |
|
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
974 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
975 |
|
public void realRun() { |
976 |
|
sync.acquire(); |
977 |
|
assertTrue(pleaseInterrupt.releaseShared(0)); |
978 |
< |
c.awaitUninterruptibly(); |
978 |
> |
condition.awaitUninterruptibly(); |
979 |
|
assertTrue(Thread.interrupted()); |
980 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
980 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
981 |
|
sync.release(); |
982 |
|
}}); |
983 |
|
|
984 |
|
pleaseInterrupt.acquireShared(0); |
985 |
|
sync.acquire(); |
986 |
< |
assertHasWaitersLocked(sync, c, t); |
986 |
> |
assertHasWaitersLocked(sync, condition, t); |
987 |
|
sync.release(); |
988 |
|
t.interrupt(); |
989 |
< |
assertHasWaitersUnlocked(sync, c, t); |
990 |
< |
assertThreadStaysAlive(t); |
989 |
> |
assertHasWaitersUnlocked(sync, condition, t); |
990 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
991 |
|
sync.acquire(); |
992 |
< |
assertHasWaitersLocked(sync, c, t); |
992 |
> |
assertHasWaitersLocked(sync, condition, t); |
993 |
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
994 |
< |
c.signal(); |
995 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
994 |
> |
condition.signal(); |
995 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
996 |
|
assertHasExclusiveQueuedThreads(sync, t); |
997 |
|
sync.release(); |
998 |
|
awaitTermination(t); |
1135 |
|
|
1136 |
|
waitForQueuedThread(l, t); |
1137 |
|
assertFalse(l.isSignalled()); |
1138 |
< |
assertThreadStaysAlive(t); |
1138 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1139 |
|
assertHasSharedQueuedThreads(l, t); |
1140 |
|
assertTrue(l.releaseShared(0)); |
1141 |
|
assertTrue(l.isSignalled()); |
1160 |
|
|
1161 |
|
waitForQueuedThread(l, t); |
1162 |
|
assertFalse(l.isSignalled()); |
1163 |
< |
assertThreadStaysAlive(t); |
1163 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1164 |
|
assertTrue(l.releaseShared(0)); |
1165 |
|
assertTrue(l.isSignalled()); |
1166 |
|
awaitTermination(t); |
1209 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1210 |
|
final BooleanLatch l = new BooleanLatch(); |
1211 |
|
final BooleanLatch observedQueued = new BooleanLatch(); |
1195 |
– |
final long timeoutMillis = timeoutMillis(); |
1212 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1213 |
|
public void realRun() throws InterruptedException { |
1214 |
|
assertFalse(l.isSignalled()); |