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); |
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); } |
257 |
– |
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); |