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 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
|
junit.textui.TestRunner.run(suite()); |
124 |
|
} |
125 |
|
|
126 |
|
/** A constant to clarify calls to checking methods below. */ |
127 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
127 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
128 |
|
|
129 |
|
/** |
130 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
951 |
|
} |
952 |
|
|
953 |
|
/** |
954 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
954 |
> |
* awaitUninterruptibly is uninterruptible |
955 |
|
*/ |
956 |
|
public void testAwaitUninterruptibly() { |
957 |
|
final Mutex sync = new Mutex(); |
958 |
|
final ConditionObject c = sync.newCondition(); |
959 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
959 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
960 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
961 |
|
public void realRun() { |
962 |
|
sync.acquire(); |
963 |
< |
assertTrue(acquired.releaseShared(0)); |
963 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
964 |
|
c.awaitUninterruptibly(); |
965 |
|
assertTrue(Thread.interrupted()); |
966 |
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
967 |
|
sync.release(); |
968 |
|
}}); |
969 |
|
|
970 |
< |
acquired.acquireShared(0); |
970 |
> |
pleaseInterrupt.acquireShared(0); |
971 |
|
sync.acquire(); |
972 |
|
assertHasWaitersLocked(sync, c, t); |
973 |
|
sync.release(); |
994 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
995 |
|
final Mutex sync = new Mutex(); |
996 |
|
final ConditionObject c = sync.newCondition(); |
997 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
997 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
998 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
999 |
|
public void realRun() throws InterruptedException { |
1000 |
|
sync.acquire(); |
1001 |
< |
assertTrue(acquired.releaseShared(0)); |
1001 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1002 |
|
await(c, awaitMethod); |
1003 |
|
}}); |
1004 |
|
|
1005 |
< |
acquired.acquireShared(0); |
1005 |
> |
pleaseInterrupt.acquireShared(0); |
1006 |
|
t.interrupt(); |
1007 |
|
awaitTermination(t); |
1008 |
|
} |
1194 |
|
*/ |
1195 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1196 |
|
final BooleanLatch l = new BooleanLatch(); |
1197 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1198 |
+ |
final long timeoutMillis = timeoutMillis(); |
1199 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1200 |
|
public void realRun() throws InterruptedException { |
1201 |
|
assertFalse(l.isSignalled()); |
1202 |
< |
long startTime = System.nanoTime(); |
1203 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1204 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1205 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1202 |
> |
for (long millis = timeoutMillis(); |
1203 |
> |
!observedQueued.isSignalled(); |
1204 |
> |
millis *= 2) { |
1205 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1206 |
> |
long startTime = System.nanoTime(); |
1207 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1208 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1209 |
> |
} |
1210 |
|
assertFalse(l.isSignalled()); |
1211 |
|
}}); |
1212 |
|
|
1213 |
|
waitForQueuedThread(l, t); |
1214 |
+ |
observedQueued.releaseShared(0); |
1215 |
|
assertFalse(l.isSignalled()); |
1216 |
|
awaitTermination(t); |
1217 |
|
assertFalse(l.isSignalled()); |
1218 |
|
} |
1219 |
|
|
1220 |
+ |
/** |
1221 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1222 |
+ |
*/ |
1223 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1224 |
+ |
final Mutex sync = new Mutex(); |
1225 |
+ |
final ConditionObject c = sync.newCondition(); |
1226 |
+ |
sync.acquire(); |
1227 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1228 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1229 |
+ |
sync.release(); |
1230 |
+ |
} |
1231 |
+ |
|
1232 |
+ |
/** |
1233 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1234 |
+ |
*/ |
1235 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1236 |
+ |
final Mutex sync = new Mutex(); |
1237 |
+ |
final ConditionObject c = sync.newCondition(); |
1238 |
+ |
sync.acquire(); |
1239 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1240 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1241 |
+ |
sync.release(); |
1242 |
+ |
} |
1243 |
+ |
|
1244 |
|
} |