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 |
|
|
956 |
|
} |
957 |
|
|
958 |
|
/** |
959 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
959 |
> |
* awaitUninterruptibly is uninterruptible |
960 |
|
*/ |
961 |
|
public void testAwaitUninterruptibly() { |
962 |
|
final Mutex sync = new Mutex(); |
963 |
|
final ConditionObject c = sync.newCondition(); |
964 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
964 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
965 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
966 |
|
public void realRun() { |
967 |
|
sync.acquire(); |
968 |
< |
assertTrue(acquired.releaseShared(0)); |
968 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
969 |
|
c.awaitUninterruptibly(); |
970 |
|
assertTrue(Thread.interrupted()); |
971 |
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
972 |
|
sync.release(); |
973 |
|
}}); |
974 |
|
|
975 |
< |
acquired.acquireShared(0); |
975 |
> |
pleaseInterrupt.acquireShared(0); |
976 |
|
sync.acquire(); |
977 |
|
assertHasWaitersLocked(sync, c, t); |
978 |
|
sync.release(); |
999 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
1000 |
|
final Mutex sync = new Mutex(); |
1001 |
|
final ConditionObject c = sync.newCondition(); |
1002 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
1002 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
1003 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1004 |
|
public void realRun() throws InterruptedException { |
1005 |
|
sync.acquire(); |
1006 |
< |
assertTrue(acquired.releaseShared(0)); |
1006 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1007 |
|
await(c, awaitMethod); |
1008 |
|
}}); |
1009 |
|
|
1010 |
< |
acquired.acquireShared(0); |
1010 |
> |
pleaseInterrupt.acquireShared(0); |
1011 |
|
t.interrupt(); |
1012 |
|
awaitTermination(t); |
1013 |
|
} |
1199 |
|
*/ |
1200 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1201 |
|
final BooleanLatch l = new BooleanLatch(); |
1202 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1203 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1204 |
|
public void realRun() throws InterruptedException { |
1205 |
|
assertFalse(l.isSignalled()); |
1206 |
< |
long startTime = System.nanoTime(); |
1207 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1208 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1209 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1206 |
> |
for (long millis = timeoutMillis(); |
1207 |
> |
!observedQueued.isSignalled(); |
1208 |
> |
millis *= 2) { |
1209 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1210 |
> |
long startTime = System.nanoTime(); |
1211 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1212 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1213 |
> |
} |
1214 |
|
assertFalse(l.isSignalled()); |
1215 |
|
}}); |
1216 |
|
|
1217 |
|
waitForQueuedThread(l, t); |
1218 |
+ |
observedQueued.releaseShared(0); |
1219 |
|
assertFalse(l.isSignalled()); |
1220 |
|
awaitTermination(t); |
1221 |
|
assertFalse(l.isSignalled()); |
1222 |
|
} |
1223 |
|
|
1224 |
+ |
/** |
1225 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1226 |
+ |
*/ |
1227 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1228 |
+ |
final Mutex sync = new Mutex(); |
1229 |
+ |
final ConditionObject c = sync.newCondition(); |
1230 |
+ |
sync.acquire(); |
1231 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1232 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1233 |
+ |
sync.release(); |
1234 |
+ |
} |
1235 |
+ |
|
1236 |
+ |
/** |
1237 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1238 |
+ |
*/ |
1239 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1240 |
+ |
final Mutex sync = new Mutex(); |
1241 |
+ |
final ConditionObject c = sync.newCondition(); |
1242 |
+ |
sync.acquire(); |
1243 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1244 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1245 |
+ |
sync.release(); |
1246 |
+ |
} |
1247 |
+ |
|
1248 |
|
} |