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 |
|
|
249 |
|
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
250 |
|
long nanosRemaining = c.awaitNanos(nanosTimeout); |
251 |
|
assertTrue(nanosRemaining <= 0); |
252 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
253 |
|
break; |
254 |
|
case awaitUntil: |
255 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
1200 |
|
*/ |
1201 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1202 |
|
final BooleanLatch l = new BooleanLatch(); |
1203 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1204 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1205 |
|
public void realRun() throws InterruptedException { |
1206 |
|
assertFalse(l.isSignalled()); |
1207 |
< |
long startTime = System.nanoTime(); |
1208 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1209 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1210 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1207 |
> |
for (long millis = timeoutMillis(); |
1208 |
> |
!observedQueued.isSignalled(); |
1209 |
> |
millis *= 2) { |
1210 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1211 |
> |
long startTime = System.nanoTime(); |
1212 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1213 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1214 |
> |
} |
1215 |
|
assertFalse(l.isSignalled()); |
1216 |
|
}}); |
1217 |
|
|
1218 |
|
waitForQueuedThread(l, t); |
1219 |
+ |
observedQueued.releaseShared(0); |
1220 |
|
assertFalse(l.isSignalled()); |
1221 |
|
awaitTermination(t); |
1222 |
|
assertFalse(l.isSignalled()); |
1223 |
|
} |
1224 |
|
|
1225 |
+ |
/** |
1226 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1227 |
+ |
*/ |
1228 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1229 |
+ |
final Mutex sync = new Mutex(); |
1230 |
+ |
final ConditionObject c = sync.newCondition(); |
1231 |
+ |
sync.acquire(); |
1232 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1233 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1234 |
+ |
sync.release(); |
1235 |
+ |
} |
1236 |
+ |
|
1237 |
+ |
/** |
1238 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1239 |
+ |
*/ |
1240 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1241 |
+ |
final Mutex sync = new Mutex(); |
1242 |
+ |
final ConditionObject c = sync.newCondition(); |
1243 |
+ |
sync.acquire(); |
1244 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1245 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1246 |
+ |
sync.release(); |
1247 |
+ |
} |
1248 |
+ |
|
1249 |
|
} |