[cvs] / jsr166 / src / test / tck / AbstractQueuedLongSynchronizerTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (view) (download)

1 : dl 1.1 /*
2 :     * Written by Doug Lea with assistance from members of JCP JSR-166
3 :     * Expert Group and released to the public domain, as explained at
4 : jsr166 1.22 * http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 : jsr166 1.12 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 : jsr166 1.28 import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 : jsr166 1.29
12 :     import java.util.Arrays;
13 :     import java.util.Collection;
14 :     import java.util.HashSet;
15 : jsr166 1.24 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
16 :     import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject;
17 : dl 1.1
18 : jsr166 1.29 import junit.framework.AssertionFailedError;
19 :     import junit.framework.Test;
20 :     import junit.framework.TestSuite;
21 :    
22 : dl 1.1 public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
23 :     public static void main(String[] args) {
24 : jsr166 1.16 junit.textui.TestRunner.run(suite());
25 : dl 1.1 }
26 :     public static Test suite() {
27 :     return new TestSuite(AbstractQueuedLongSynchronizerTest.class);
28 :     }
29 :    
30 :     /**
31 : jsr166 1.24 * A simple mutex class, adapted from the class javadoc. Exclusive
32 :     * acquire tests exercise this as a sample user extension.
33 : dl 1.1 */
34 :     static class Mutex extends AbstractQueuedLongSynchronizer {
35 : jsr166 1.24 /** An eccentric value > 32 bits for locked synchronizer state. */
36 :     static final long LOCKED = (1L << 63) | (1L << 15);
37 :    
38 :     static final long UNLOCKED = 0;
39 :    
40 : jsr166 1.5 public boolean isHeldExclusively() {
41 : jsr166 1.24 long state = getState();
42 :     assertTrue(state == UNLOCKED || state == LOCKED);
43 :     return state == LOCKED;
44 : dl 1.1 }
45 : jsr166 1.5
46 : dl 1.1 public boolean tryAcquire(long acquires) {
47 : jsr166 1.24 assertEquals(LOCKED, acquires);
48 :     return compareAndSetState(UNLOCKED, LOCKED);
49 : dl 1.1 }
50 : jsr166 1.5
51 : dl 1.1 public boolean tryRelease(long releases) {
52 : jsr166 1.24 if (getState() != LOCKED) throw new IllegalMonitorStateException();
53 :     setState(UNLOCKED);
54 : dl 1.1 return true;
55 :     }
56 : jsr166 1.5
57 : jsr166 1.24 public boolean tryAcquireNanos(long nanos) throws InterruptedException {
58 :     return tryAcquireNanos(LOCKED, nanos);
59 :     }
60 :    
61 :     public boolean tryAcquire() {
62 :     return tryAcquire(LOCKED);
63 :     }
64 :    
65 :     public boolean tryRelease() {
66 :     return tryRelease(LOCKED);
67 :     }
68 :    
69 :     public void acquire() {
70 :     acquire(LOCKED);
71 :     }
72 :    
73 :     public void acquireInterruptibly() throws InterruptedException {
74 :     acquireInterruptibly(LOCKED);
75 :     }
76 :    
77 :     public void release() {
78 :     release(LOCKED);
79 : jsr166 1.17 }
80 : dl 1.1
81 : jsr166 1.24 public ConditionObject newCondition() {
82 :     return new ConditionObject();
83 :     }
84 : dl 1.1 }
85 :    
86 :     /**
87 :     * A simple latch class, to test shared mode.
88 :     */
89 : jsr166 1.5 static class BooleanLatch extends AbstractQueuedLongSynchronizer {
90 : dl 1.1 public boolean isSignalled() { return getState() != 0; }
91 :    
92 :     public long tryAcquireShared(long ignore) {
93 : jsr166 1.21 return isSignalled() ? 1 : -1;
94 : dl 1.1 }
95 : jsr166 1.5
96 : dl 1.1 public boolean tryReleaseShared(long ignore) {
97 :     setState(1 << 62);
98 :     return true;
99 :     }
100 :     }
101 :    
102 :     /**
103 : jsr166 1.10 * A runnable calling acquireInterruptibly that does not expect to
104 :     * be interrupted.
105 : dl 1.1 */
106 : jsr166 1.10 class InterruptibleSyncRunnable extends CheckedRunnable {
107 : dl 1.1 final Mutex sync;
108 : jsr166 1.24 InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; }
109 : jsr166 1.10 public void realRun() throws InterruptedException {
110 : jsr166 1.24 sync.acquireInterruptibly();
111 : dl 1.1 }
112 :     }
113 :    
114 :     /**
115 :     * A runnable calling acquireInterruptibly that expects to be
116 : jsr166 1.10 * interrupted.
117 : dl 1.1 */
118 : jsr166 1.10 class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
119 : dl 1.1 final Mutex sync;
120 : jsr166 1.24 InterruptedSyncRunnable(Mutex sync) { this.sync = sync; }
121 : jsr166 1.10 public void realRun() throws InterruptedException {
122 : jsr166 1.24 sync.acquireInterruptibly();
123 :     }
124 :     }
125 :    
126 :     /** A constant to clarify calls to checking methods below. */
127 : jsr166 1.26 static final Thread[] NO_THREADS = new Thread[0];
128 : jsr166 1.24
129 :     /**
130 :     * Spin-waits until sync.isQueued(t) becomes true.
131 :     */
132 :     void waitForQueuedThread(AbstractQueuedLongSynchronizer sync,
133 :     Thread t) {
134 :     long startTime = System.nanoTime();
135 :     while (!sync.isQueued(t)) {
136 :     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
137 :     throw new AssertionFailedError("timed out");
138 :     Thread.yield();
139 :     }
140 :     assertTrue(t.isAlive());
141 :     }
142 :    
143 :     /**
144 :     * Checks that sync has exactly the given queued threads.
145 :     */
146 :     void assertHasQueuedThreads(AbstractQueuedLongSynchronizer sync,
147 :     Thread... expected) {
148 :     Collection<Thread> actual = sync.getQueuedThreads();
149 :     assertEquals(expected.length > 0, sync.hasQueuedThreads());
150 :     assertEquals(expected.length, sync.getQueueLength());
151 :     assertEquals(expected.length, actual.size());
152 :     assertEquals(expected.length == 0, actual.isEmpty());
153 :     assertEquals(new HashSet<Thread>(actual),
154 :     new HashSet<Thread>(Arrays.asList(expected)));
155 :     }
156 :    
157 :     /**
158 :     * Checks that sync has exactly the given (exclusive) queued threads.
159 :     */
160 :     void assertHasExclusiveQueuedThreads(AbstractQueuedLongSynchronizer sync,
161 :     Thread... expected) {
162 :     assertHasQueuedThreads(sync, expected);
163 :     assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()),
164 :     new HashSet<Thread>(sync.getQueuedThreads()));
165 :     assertEquals(0, sync.getSharedQueuedThreads().size());
166 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
167 :     }
168 :    
169 :     /**
170 :     * Checks that sync has exactly the given (shared) queued threads.
171 :     */
172 :     void assertHasSharedQueuedThreads(AbstractQueuedLongSynchronizer sync,
173 :     Thread... expected) {
174 :     assertHasQueuedThreads(sync, expected);
175 :     assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()),
176 :     new HashSet<Thread>(sync.getQueuedThreads()));
177 :     assertEquals(0, sync.getExclusiveQueuedThreads().size());
178 :     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
179 :     }
180 :    
181 :     /**
182 :     * Checks that condition c has exactly the given waiter threads,
183 :     * after acquiring mutex.
184 :     */
185 :     void assertHasWaitersUnlocked(Mutex sync, ConditionObject c,
186 :     Thread... threads) {
187 :     sync.acquire();
188 :     assertHasWaitersLocked(sync, c, threads);
189 :     sync.release();
190 :     }
191 :    
192 :     /**
193 :     * Checks that condition c has exactly the given waiter threads.
194 :     */
195 :     void assertHasWaitersLocked(Mutex sync, ConditionObject c,
196 :     Thread... threads) {
197 :     assertEquals(threads.length > 0, sync.hasWaiters(c));
198 :     assertEquals(threads.length, sync.getWaitQueueLength(c));
199 :     assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty());
200 :     assertEquals(threads.length, sync.getWaitingThreads(c).size());
201 :     assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)),
202 :     new HashSet<Thread>(Arrays.asList(threads)));
203 :     }
204 :    
205 :     enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
206 :    
207 :     /**
208 :     * Awaits condition using the specified AwaitMethod.
209 :     */
210 :     void await(ConditionObject c, AwaitMethod awaitMethod)
211 :     throws InterruptedException {
212 :     long timeoutMillis = 2 * LONG_DELAY_MS;
213 :     switch (awaitMethod) {
214 :     case await:
215 :     c.await();
216 :     break;
217 :     case awaitTimed:
218 :     assertTrue(c.await(timeoutMillis, MILLISECONDS));
219 :     break;
220 :     case awaitNanos:
221 :     long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
222 :     long nanosRemaining = c.awaitNanos(nanosTimeout);
223 :     assertTrue(nanosRemaining > 0);
224 :     break;
225 :     case awaitUntil:
226 :     assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
227 :     break;
228 : dl 1.1 }
229 :     }
230 :    
231 :     /**
232 : jsr166 1.24 * Checks that awaiting the given condition times out (using the
233 :     * default timeout duration).
234 :     */
235 :     void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
236 :     long timeoutMillis = timeoutMillis();
237 :     long startTime = System.nanoTime();
238 :     try {
239 :     switch (awaitMethod) {
240 :     case awaitTimed:
241 :     assertFalse(c.await(timeoutMillis, MILLISECONDS));
242 :     break;
243 :     case awaitNanos:
244 :     long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
245 :     long nanosRemaining = c.awaitNanos(nanosTimeout);
246 :     assertTrue(nanosRemaining <= 0);
247 :     break;
248 :     case awaitUntil:
249 :     assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
250 :     break;
251 :     default:
252 :     throw new UnsupportedOperationException();
253 :     }
254 :     } catch (InterruptedException ie) { threadUnexpectedException(ie); }
255 :     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
256 :     }
257 :    
258 :     /**
259 : dl 1.1 * isHeldExclusively is false upon construction
260 :     */
261 : jsr166 1.5 public void testIsHeldExclusively() {
262 : jsr166 1.24 Mutex sync = new Mutex();
263 :     assertFalse(sync.isHeldExclusively());
264 : dl 1.1 }
265 : jsr166 1.5
266 : dl 1.1 /**
267 :     * acquiring released sync succeeds
268 :     */
269 : jsr166 1.5 public void testAcquire() {
270 : jsr166 1.24 Mutex sync = new Mutex();
271 :     sync.acquire();
272 :     assertTrue(sync.isHeldExclusively());
273 :     sync.release();
274 :     assertFalse(sync.isHeldExclusively());
275 : dl 1.1 }
276 :    
277 :     /**
278 : jsr166 1.24 * tryAcquire on a released sync succeeds
279 : dl 1.1 */
280 : jsr166 1.5 public void testTryAcquire() {
281 : jsr166 1.24 Mutex sync = new Mutex();
282 :     assertTrue(sync.tryAcquire());
283 :     assertTrue(sync.isHeldExclusively());
284 :     sync.release();
285 :     assertFalse(sync.isHeldExclusively());
286 : dl 1.1 }
287 :    
288 :     /**
289 :     * hasQueuedThreads reports whether there are waiting threads
290 :     */
291 : jsr166 1.24 public void testHasQueuedThreads() {
292 : jsr166 1.11 final Mutex sync = new Mutex();
293 : jsr166 1.9 assertFalse(sync.hasQueuedThreads());
294 : jsr166 1.24 sync.acquire();
295 :     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
296 :     waitForQueuedThread(sync, t1);
297 : jsr166 1.9 assertTrue(sync.hasQueuedThreads());
298 : jsr166 1.24 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
299 :     waitForQueuedThread(sync, t2);
300 : jsr166 1.9 assertTrue(sync.hasQueuedThreads());
301 :     t1.interrupt();
302 : jsr166 1.24 awaitTermination(t1);
303 : jsr166 1.9 assertTrue(sync.hasQueuedThreads());
304 : jsr166 1.24 sync.release();
305 :     awaitTermination(t2);
306 : jsr166 1.9 assertFalse(sync.hasQueuedThreads());
307 : jsr166 1.5 }
308 : dl 1.1
309 :     /**
310 : jsr166 1.24 * isQueued(null) throws NullPointerException
311 : dl 1.1 */
312 : jsr166 1.5 public void testIsQueuedNPE() {
313 : jsr166 1.11 final Mutex sync = new Mutex();
314 : dl 1.1 try {
315 :     sync.isQueued(null);
316 :     shouldThrow();
317 : jsr166 1.9 } catch (NullPointerException success) {}
318 : dl 1.1 }
319 :    
320 :     /**
321 : jsr166 1.24 * isQueued reports whether a thread is queued
322 : dl 1.1 */
323 : jsr166 1.24 public void testIsQueued() {
324 : jsr166 1.11 final Mutex sync = new Mutex();
325 : dl 1.1 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
326 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
327 : jsr166 1.9 assertFalse(sync.isQueued(t1));
328 :     assertFalse(sync.isQueued(t2));
329 : jsr166 1.24 sync.acquire();
330 : jsr166 1.9 t1.start();
331 : jsr166 1.24 waitForQueuedThread(sync, t1);
332 : jsr166 1.9 assertTrue(sync.isQueued(t1));
333 : jsr166 1.24 assertFalse(sync.isQueued(t2));
334 : jsr166 1.9 t2.start();
335 : jsr166 1.24 waitForQueuedThread(sync, t2);
336 : jsr166 1.9 assertTrue(sync.isQueued(t1));
337 :     assertTrue(sync.isQueued(t2));
338 :     t1.interrupt();
339 : jsr166 1.24 awaitTermination(t1);
340 : jsr166 1.9 assertFalse(sync.isQueued(t1));
341 :     assertTrue(sync.isQueued(t2));
342 : jsr166 1.24 sync.release();
343 :     awaitTermination(t2);
344 : jsr166 1.9 assertFalse(sync.isQueued(t1));
345 :     assertFalse(sync.isQueued(t2));
346 : jsr166 1.5 }
347 : dl 1.1
348 :     /**
349 :     * getFirstQueuedThread returns first waiting thread or null if none
350 :     */
351 : jsr166 1.24 public void testGetFirstQueuedThread() {
352 : jsr166 1.11 final Mutex sync = new Mutex();
353 : jsr166 1.9 assertNull(sync.getFirstQueuedThread());
354 : jsr166 1.24 sync.acquire();
355 :     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
356 :     waitForQueuedThread(sync, t1);
357 : jsr166 1.9 assertEquals(t1, sync.getFirstQueuedThread());
358 : jsr166 1.24 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
359 :     waitForQueuedThread(sync, t2);
360 : jsr166 1.9 assertEquals(t1, sync.getFirstQueuedThread());
361 :     t1.interrupt();
362 : jsr166 1.24 awaitTermination(t1);
363 : jsr166 1.9 assertEquals(t2, sync.getFirstQueuedThread());
364 : jsr166 1.24 sync.release();
365 :     awaitTermination(t2);
366 : jsr166 1.9 assertNull(sync.getFirstQueuedThread());
367 : jsr166 1.5 }
368 : dl 1.1
369 :     /**
370 :     * hasContended reports false if no thread has ever blocked, else true
371 :     */
372 : jsr166 1.24 public void testHasContended() {
373 : jsr166 1.11 final Mutex sync = new Mutex();
374 : jsr166 1.9 assertFalse(sync.hasContended());
375 : jsr166 1.24 sync.acquire();
376 :     assertFalse(sync.hasContended());
377 :     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
378 :     waitForQueuedThread(sync, t1);
379 : jsr166 1.9 assertTrue(sync.hasContended());
380 : jsr166 1.24 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
381 :     waitForQueuedThread(sync, t2);
382 : jsr166 1.9 assertTrue(sync.hasContended());
383 :     t1.interrupt();
384 : jsr166 1.24 awaitTermination(t1);
385 : jsr166 1.9 assertTrue(sync.hasContended());
386 : jsr166 1.24 sync.release();
387 :     awaitTermination(t2);
388 : jsr166 1.9 assertTrue(sync.hasContended());
389 : jsr166 1.5 }
390 : dl 1.1
391 :     /**
392 : jsr166 1.24 * getQueuedThreads returns all waiting threads
393 : dl 1.1 */
394 : jsr166 1.24 public void testGetQueuedThreads() {
395 : jsr166 1.11 final Mutex sync = new Mutex();
396 : dl 1.1 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
397 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
398 : jsr166 1.24 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
399 :     sync.acquire();
400 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
401 : jsr166 1.9 t1.start();
402 : jsr166 1.24 waitForQueuedThread(sync, t1);
403 :     assertHasExclusiveQueuedThreads(sync, t1);
404 : jsr166 1.9 assertTrue(sync.getQueuedThreads().contains(t1));
405 : jsr166 1.24 assertFalse(sync.getQueuedThreads().contains(t2));
406 : jsr166 1.9 t2.start();
407 : jsr166 1.24 waitForQueuedThread(sync, t2);
408 :     assertHasExclusiveQueuedThreads(sync, t1, t2);
409 : jsr166 1.9 assertTrue(sync.getQueuedThreads().contains(t1));
410 :     assertTrue(sync.getQueuedThreads().contains(t2));
411 :     t1.interrupt();
412 : jsr166 1.24 awaitTermination(t1);
413 :     assertHasExclusiveQueuedThreads(sync, t2);
414 :     sync.release();
415 :     awaitTermination(t2);
416 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
417 : jsr166 1.5 }
418 : dl 1.1
419 :     /**
420 : jsr166 1.24 * getExclusiveQueuedThreads returns all exclusive waiting threads
421 : dl 1.1 */
422 : jsr166 1.24 public void testGetExclusiveQueuedThreads() {
423 : jsr166 1.11 final Mutex sync = new Mutex();
424 : dl 1.1 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
425 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
426 : jsr166 1.24 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
427 :     sync.acquire();
428 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
429 : jsr166 1.9 t1.start();
430 : jsr166 1.24 waitForQueuedThread(sync, t1);
431 :     assertHasExclusiveQueuedThreads(sync, t1);
432 : jsr166 1.9 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
433 : jsr166 1.24 assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
434 : jsr166 1.9 t2.start();
435 : jsr166 1.24 waitForQueuedThread(sync, t2);
436 :     assertHasExclusiveQueuedThreads(sync, t1, t2);
437 : jsr166 1.9 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
438 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
439 :     t1.interrupt();
440 : jsr166 1.24 awaitTermination(t1);
441 :     assertHasExclusiveQueuedThreads(sync, t2);
442 :     sync.release();
443 :     awaitTermination(t2);
444 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
445 : jsr166 1.5 }
446 : dl 1.1
447 :     /**
448 :     * getSharedQueuedThreads does not include exclusively waiting threads
449 :     */
450 : jsr166 1.24 public void testGetSharedQueuedThreads_Exclusive() {
451 : jsr166 1.11 final Mutex sync = new Mutex();
452 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
453 : jsr166 1.24 sync.acquire();
454 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
455 : jsr166 1.24 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
456 :     waitForQueuedThread(sync, t1);
457 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
458 : jsr166 1.24 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
459 :     waitForQueuedThread(sync, t2);
460 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
461 :     t1.interrupt();
462 : jsr166 1.24 awaitTermination(t1);
463 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
464 : jsr166 1.24 sync.release();
465 :     awaitTermination(t2);
466 : jsr166 1.9 assertTrue(sync.getSharedQueuedThreads().isEmpty());
467 : jsr166 1.5 }
468 : dl 1.1
469 :     /**
470 : jsr166 1.24 * getSharedQueuedThreads returns all shared waiting threads
471 : dl 1.1 */
472 : jsr166 1.24 public void testGetSharedQueuedThreads_Shared() {
473 :     final BooleanLatch l = new BooleanLatch();
474 :     assertHasSharedQueuedThreads(l, NO_THREADS);
475 :     Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
476 :     public void realRun() throws InterruptedException {
477 :     l.acquireSharedInterruptibly(0);
478 :     }});
479 :     waitForQueuedThread(l, t1);
480 :     assertHasSharedQueuedThreads(l, t1);
481 :     Thread t2 = newStartedThread(new CheckedRunnable() {
482 :     public void realRun() throws InterruptedException {
483 :     l.acquireSharedInterruptibly(0);
484 :     }});
485 :     waitForQueuedThread(l, t2);
486 :     assertHasSharedQueuedThreads(l, t1, t2);
487 :     t1.interrupt();
488 :     awaitTermination(t1);
489 :     assertHasSharedQueuedThreads(l, t2);
490 :     assertTrue(l.releaseShared(0));
491 :     awaitTermination(t2);
492 :     assertHasSharedQueuedThreads(l, NO_THREADS);
493 :     }
494 :    
495 :     /**
496 :     * tryAcquireNanos is interruptible
497 :     */
498 :     public void testTryAcquireNanos_Interruptible() {
499 : jsr166 1.11 final Mutex sync = new Mutex();
500 : jsr166 1.24 sync.acquire();
501 :     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
502 : jsr166 1.10 public void realRun() throws InterruptedException {
503 : jsr166 1.24 sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
504 : jsr166 1.10 }});
505 : jsr166 1.9
506 : jsr166 1.24 waitForQueuedThread(sync, t);
507 : jsr166 1.9 t.interrupt();
508 : jsr166 1.24 awaitTermination(t);
509 : dl 1.1 }
510 :    
511 :     /**
512 : jsr166 1.24 * tryAcquire on exclusively held sync fails
513 : dl 1.1 */
514 : jsr166 1.24 public void testTryAcquireWhenSynced() {
515 : jsr166 1.11 final Mutex sync = new Mutex();
516 : jsr166 1.24 sync.acquire();
517 :     Thread t = newStartedThread(new CheckedRunnable() {
518 : jsr166 1.10 public void realRun() {
519 : jsr166 1.24 assertFalse(sync.tryAcquire());
520 : jsr166 1.10 }});
521 : jsr166 1.9
522 : jsr166 1.24 awaitTermination(t);
523 :     sync.release();
524 : jsr166 1.5 }
525 : dl 1.1
526 :     /**
527 :     * tryAcquireNanos on an exclusively held sync times out
528 :     */
529 : jsr166 1.24 public void testAcquireNanos_Timeout() {
530 : jsr166 1.11 final Mutex sync = new Mutex();
531 : jsr166 1.24 sync.acquire();
532 :     Thread t = newStartedThread(new CheckedRunnable() {
533 : jsr166 1.10 public void realRun() throws InterruptedException {
534 : jsr166 1.24 long startTime = System.nanoTime();
535 :     long nanos = MILLISECONDS.toNanos(timeoutMillis());
536 :     assertFalse(sync.tryAcquireNanos(nanos));
537 :     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
538 : jsr166 1.10 }});
539 : jsr166 1.9
540 : jsr166 1.24 awaitTermination(t);
541 :     sync.release();
542 : jsr166 1.5 }
543 :    
544 : dl 1.1 /**
545 :     * getState is true when acquired and false when not
546 :     */
547 : jsr166 1.24 public void testGetState() {
548 : jsr166 1.11 final Mutex sync = new Mutex();
549 : jsr166 1.24 sync.acquire();
550 : jsr166 1.11 assertTrue(sync.isHeldExclusively());
551 : jsr166 1.24 sync.release();
552 : jsr166 1.11 assertFalse(sync.isHeldExclusively());
553 : jsr166 1.24
554 :     final BooleanLatch acquired = new BooleanLatch();
555 :     final BooleanLatch done = new BooleanLatch();
556 :     Thread t = newStartedThread(new CheckedRunnable() {
557 : jsr166 1.10 public void realRun() throws InterruptedException {
558 : jsr166 1.24 sync.acquire();
559 :     assertTrue(acquired.releaseShared(0));
560 :     done.acquireShared(0);
561 :     sync.release();
562 : jsr166 1.10 }});
563 : jsr166 1.9
564 : jsr166 1.24 acquired.acquireShared(0);
565 : jsr166 1.9 assertTrue(sync.isHeldExclusively());
566 : jsr166 1.24 assertTrue(done.releaseShared(0));
567 :     awaitTermination(t);
568 : jsr166 1.9 assertFalse(sync.isHeldExclusively());
569 : dl 1.1 }
570 :    
571 :     /**
572 : jsr166 1.24 * acquireInterruptibly succeeds when released, else is interruptible
573 : dl 1.1 */
574 : jsr166 1.24 public void testAcquireInterruptibly() throws InterruptedException {
575 : jsr166 1.11 final Mutex sync = new Mutex();
576 : jsr166 1.24 final BooleanLatch threadStarted = new BooleanLatch();
577 :     sync.acquireInterruptibly();
578 :     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
579 :     public void realRun() throws InterruptedException {
580 :     assertTrue(threadStarted.releaseShared(0));
581 :     sync.acquireInterruptibly();
582 :     }});
583 : dl 1.1
584 : jsr166 1.24 threadStarted.acquireShared(0);
585 :     waitForQueuedThread(sync, t);
586 : jsr166 1.9 t.interrupt();
587 : jsr166 1.24 awaitTermination(t);
588 : jsr166 1.9 assertTrue(sync.isHeldExclusively());
589 : dl 1.1 }
590 :    
591 :     /**
592 :     * owns is true for a condition created by sync else false
593 :     */
594 :     public void testOwns() {
595 : jsr166 1.11 final Mutex sync = new Mutex();
596 : jsr166 1.24 final ConditionObject c = sync.newCondition();
597 : dl 1.1 final Mutex sync2 = new Mutex();
598 :     assertTrue(sync.owns(c));
599 :     assertFalse(sync2.owns(c));
600 :     }
601 :    
602 :     /**
603 :     * Calling await without holding sync throws IllegalMonitorStateException
604 :     */
605 : jsr166 1.24 public void testAwait_IMSE() {
606 : jsr166 1.11 final Mutex sync = new Mutex();
607 : jsr166 1.24 final ConditionObject c = sync.newCondition();
608 :     for (AwaitMethod awaitMethod : AwaitMethod.values()) {
609 :     long startTime = System.nanoTime();
610 :     try {
611 :     await(c, awaitMethod);
612 :     shouldThrow();
613 :     } catch (IllegalMonitorStateException success) {
614 :     } catch (InterruptedException e) { threadUnexpectedException(e); }
615 :     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
616 :     }
617 : dl 1.1 }
618 :    
619 :     /**
620 :     * Calling signal without holding sync throws IllegalMonitorStateException
621 :     */
622 : jsr166 1.24 public void testSignal_IMSE() {
623 : jsr166 1.11 final Mutex sync = new Mutex();
624 : jsr166 1.24 final ConditionObject c = sync.newCondition();
625 : dl 1.1 try {
626 :     c.signal();
627 :     shouldThrow();
628 : jsr166 1.10 } catch (IllegalMonitorStateException success) {}
629 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
630 : dl 1.1 }
631 :    
632 :     /**
633 : jsr166 1.24 * Calling signalAll without holding sync throws IllegalMonitorStateException
634 : dl 1.1 */
635 : jsr166 1.24 public void testSignalAll_IMSE() {
636 : jsr166 1.11 final Mutex sync = new Mutex();
637 : jsr166 1.24 final ConditionObject c = sync.newCondition();
638 :     try {
639 :     c.signalAll();
640 :     shouldThrow();
641 :     } catch (IllegalMonitorStateException success) {}
642 : dl 1.1 }
643 :    
644 :     /**
645 : jsr166 1.24 * await/awaitNanos/awaitUntil without a signal times out
646 : dl 1.1 */
647 : jsr166 1.24 public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); }
648 :     public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); }
649 :     public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); }
650 :     public void testAwait_Timeout(AwaitMethod awaitMethod) {
651 : jsr166 1.11 final Mutex sync = new Mutex();
652 : jsr166 1.24 final ConditionObject c = sync.newCondition();
653 :     sync.acquire();
654 :     assertAwaitTimesOut(c, awaitMethod);
655 :     sync.release();
656 : dl 1.1 }
657 :    
658 :     /**
659 : jsr166 1.24 * await/awaitNanos/awaitUntil returns when signalled
660 : dl 1.1 */
661 : jsr166 1.24 public void testSignal_await() { testSignal(AwaitMethod.await); }
662 :     public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); }
663 :     public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); }
664 :     public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); }
665 :     public void testSignal(final AwaitMethod awaitMethod) {
666 : jsr166 1.11 final Mutex sync = new Mutex();
667 : jsr166 1.24 final ConditionObject c = sync.newCondition();
668 :     final BooleanLatch acquired = new BooleanLatch();
669 :     Thread t = newStartedThread(new CheckedRunnable() {
670 : jsr166 1.10 public void realRun() throws InterruptedException {
671 : jsr166 1.24 sync.acquire();
672 :     assertTrue(acquired.releaseShared(0));
673 :     await(c, awaitMethod);
674 :     sync.release();
675 : jsr166 1.10 }});
676 : dl 1.1
677 : jsr166 1.24 acquired.acquireShared(0);
678 :     sync.acquire();
679 :     assertHasWaitersLocked(sync, c, t);
680 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
681 : jsr166 1.9 c.signal();
682 : jsr166 1.24 assertHasWaitersLocked(sync, c, NO_THREADS);
683 :     assertHasExclusiveQueuedThreads(sync, t);
684 :     sync.release();
685 :     awaitTermination(t);
686 : dl 1.1 }
687 :    
688 :     /**
689 : jsr166 1.24 * hasWaiters(null) throws NullPointerException
690 : dl 1.1 */
691 :     public void testHasWaitersNPE() {
692 : jsr166 1.11 final Mutex sync = new Mutex();
693 : dl 1.1 try {
694 :     sync.hasWaiters(null);
695 :     shouldThrow();
696 : jsr166 1.9 } catch (NullPointerException success) {}
697 : dl 1.1 }
698 :    
699 :     /**
700 : jsr166 1.24 * getWaitQueueLength(null) throws NullPointerException
701 : dl 1.1 */
702 :     public void testGetWaitQueueLengthNPE() {
703 : jsr166 1.11 final Mutex sync = new Mutex();
704 : dl 1.1 try {
705 :     sync.getWaitQueueLength(null);
706 :     shouldThrow();
707 : jsr166 1.9 } catch (NullPointerException success) {}
708 : dl 1.1 }
709 :    
710 :     /**
711 :     * getWaitingThreads throws NPE if null
712 :     */
713 :     public void testGetWaitingThreadsNPE() {
714 : jsr166 1.11 final Mutex sync = new Mutex();
715 : dl 1.1 try {
716 :     sync.getWaitingThreads(null);
717 :     shouldThrow();
718 : jsr166 1.9 } catch (NullPointerException success) {}
719 : dl 1.1 }
720 :    
721 :     /**
722 : jsr166 1.24 * hasWaiters throws IllegalArgumentException if not owned
723 : dl 1.1 */
724 :     public void testHasWaitersIAE() {
725 : jsr166 1.11 final Mutex sync = new Mutex();
726 : jsr166 1.24 final ConditionObject c = sync.newCondition();
727 : jsr166 1.11 final Mutex sync2 = new Mutex();
728 : dl 1.1 try {
729 :     sync2.hasWaiters(c);
730 :     shouldThrow();
731 : jsr166 1.9 } catch (IllegalArgumentException success) {}
732 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
733 : dl 1.1 }
734 :    
735 :     /**
736 : jsr166 1.24 * hasWaiters throws IllegalMonitorStateException if not synced
737 : dl 1.1 */
738 :     public void testHasWaitersIMSE() {
739 : jsr166 1.11 final Mutex sync = new Mutex();
740 : jsr166 1.24 final ConditionObject c = sync.newCondition();
741 : dl 1.1 try {
742 :     sync.hasWaiters(c);
743 :     shouldThrow();
744 : jsr166 1.9 } catch (IllegalMonitorStateException success) {}
745 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
746 : dl 1.1 }
747 :    
748 :     /**
749 : jsr166 1.24 * getWaitQueueLength throws IllegalArgumentException if not owned
750 : dl 1.1 */
751 :     public void testGetWaitQueueLengthIAE() {
752 : jsr166 1.11 final Mutex sync = new Mutex();
753 : jsr166 1.24 final ConditionObject c = sync.newCondition();
754 : jsr166 1.11 final Mutex sync2 = new Mutex();
755 : dl 1.1 try {
756 :     sync2.getWaitQueueLength(c);
757 :     shouldThrow();
758 : jsr166 1.9 } catch (IllegalArgumentException success) {}
759 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
760 : dl 1.1 }
761 :    
762 :     /**
763 : jsr166 1.24 * getWaitQueueLength throws IllegalMonitorStateException if not synced
764 : dl 1.1 */
765 :     public void testGetWaitQueueLengthIMSE() {
766 : jsr166 1.11 final Mutex sync = new Mutex();
767 : jsr166 1.24 final ConditionObject c = sync.newCondition();
768 : dl 1.1 try {
769 :     sync.getWaitQueueLength(c);
770 :     shouldThrow();
771 : jsr166 1.9 } catch (IllegalMonitorStateException success) {}
772 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
773 : dl 1.1 }
774 :    
775 :     /**
776 : jsr166 1.24 * getWaitingThreads throws IllegalArgumentException if not owned
777 : dl 1.1 */
778 :     public void testGetWaitingThreadsIAE() {
779 : jsr166 1.11 final Mutex sync = new Mutex();
780 : jsr166 1.24 final ConditionObject c = sync.newCondition();
781 : jsr166 1.11 final Mutex sync2 = new Mutex();
782 : dl 1.1 try {
783 :     sync2.getWaitingThreads(c);
784 :     shouldThrow();
785 : jsr166 1.9 } catch (IllegalArgumentException success) {}
786 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
787 : dl 1.1 }
788 :    
789 :     /**
790 : jsr166 1.24 * getWaitingThreads throws IllegalMonitorStateException if not synced
791 : dl 1.1 */
792 :     public void testGetWaitingThreadsIMSE() {
793 : jsr166 1.11 final Mutex sync = new Mutex();
794 : jsr166 1.24 final ConditionObject c = sync.newCondition();
795 : dl 1.1 try {
796 :     sync.getWaitingThreads(c);
797 :     shouldThrow();
798 : jsr166 1.9 } catch (IllegalMonitorStateException success) {}
799 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, NO_THREADS);
800 : dl 1.1 }
801 :    
802 :     /**
803 :     * hasWaiters returns true when a thread is waiting, else false
804 :     */
805 : jsr166 1.24 public void testHasWaiters() {
806 : jsr166 1.11 final Mutex sync = new Mutex();
807 : jsr166 1.24 final ConditionObject c = sync.newCondition();
808 :     final BooleanLatch acquired = new BooleanLatch();
809 :     Thread t = newStartedThread(new CheckedRunnable() {
810 : jsr166 1.10 public void realRun() throws InterruptedException {
811 : jsr166 1.24 sync.acquire();
812 :     assertHasWaitersLocked(sync, c, NO_THREADS);
813 : jsr166 1.17 assertFalse(sync.hasWaiters(c));
814 : jsr166 1.24 assertTrue(acquired.releaseShared(0));
815 : jsr166 1.10 c.await();
816 : jsr166 1.24 sync.release();
817 : jsr166 1.10 }});
818 : dl 1.1
819 : jsr166 1.24 acquired.acquireShared(0);
820 :     sync.acquire();
821 :     assertHasWaitersLocked(sync, c, t);
822 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
823 : jsr166 1.9 assertTrue(sync.hasWaiters(c));
824 :     c.signal();
825 : jsr166 1.24 assertHasWaitersLocked(sync, c, NO_THREADS);
826 :     assertHasExclusiveQueuedThreads(sync, t);
827 : jsr166 1.9 assertFalse(sync.hasWaiters(c));
828 : jsr166 1.24 sync.release();
829 :    
830 :     awaitTermination(t);
831 :     assertHasWaitersUnlocked(sync, c, NO_THREADS);
832 : dl 1.1 }
833 :    
834 :     /**
835 :     * getWaitQueueLength returns number of waiting threads
836 :     */
837 : jsr166 1.24 public void testGetWaitQueueLength() {
838 : jsr166 1.11 final Mutex sync = new Mutex();
839 : jsr166 1.24 final ConditionObject c = sync.newCondition();
840 :     final BooleanLatch acquired1 = new BooleanLatch();
841 :     final BooleanLatch acquired2 = new BooleanLatch();
842 :     final Thread t1 = newStartedThread(new CheckedRunnable() {
843 : jsr166 1.10 public void realRun() throws InterruptedException {
844 : jsr166 1.24 sync.acquire();
845 :     assertHasWaitersLocked(sync, c, NO_THREADS);
846 : jsr166 1.17 assertEquals(0, sync.getWaitQueueLength(c));
847 : jsr166 1.24 assertTrue(acquired1.releaseShared(0));
848 : jsr166 1.10 c.await();
849 : jsr166 1.24 sync.release();
850 : jsr166 1.10 }});
851 : jsr166 1.24 acquired1.acquireShared(0);
852 :     sync.acquire();
853 :     assertHasWaitersLocked(sync, c, t1);
854 :     assertEquals(1, sync.getWaitQueueLength(c));
855 :     sync.release();
856 : jsr166 1.10
857 : jsr166 1.24 final Thread t2 = newStartedThread(new CheckedRunnable() {
858 : jsr166 1.10 public void realRun() throws InterruptedException {
859 : jsr166 1.24 sync.acquire();
860 :     assertHasWaitersLocked(sync, c, t1);
861 : jsr166 1.17 assertEquals(1, sync.getWaitQueueLength(c));
862 : jsr166 1.24 assertTrue(acquired2.releaseShared(0));
863 : jsr166 1.10 c.await();
864 : jsr166 1.24 sync.release();
865 : jsr166 1.10 }});
866 : jsr166 1.24 acquired2.acquireShared(0);
867 :     sync.acquire();
868 :     assertHasWaitersLocked(sync, c, t1, t2);
869 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
870 : jsr166 1.9 assertEquals(2, sync.getWaitQueueLength(c));
871 :     c.signalAll();
872 : jsr166 1.24 assertHasWaitersLocked(sync, c, NO_THREADS);
873 :     assertHasExclusiveQueuedThreads(sync, t1, t2);
874 : jsr166 1.9 assertEquals(0, sync.getWaitQueueLength(c));
875 : jsr166 1.24 sync.release();
876 :    
877 :     awaitTermination(t1);
878 :     awaitTermination(t2);
879 :     assertHasWaitersUnlocked(sync, c, NO_THREADS);
880 : dl 1.1 }
881 :    
882 :     /**
883 :     * getWaitingThreads returns only and all waiting threads
884 :     */
885 : jsr166 1.24 public void testGetWaitingThreads() {
886 : jsr166 1.11 final Mutex sync = new Mutex();
887 : jsr166 1.24 final ConditionObject c = sync.newCondition();
888 :     final BooleanLatch acquired1 = new BooleanLatch();
889 :     final BooleanLatch acquired2 = new BooleanLatch();
890 :     final Thread t1 = new Thread(new CheckedRunnable() {
891 : jsr166 1.10 public void realRun() throws InterruptedException {
892 : jsr166 1.24 sync.acquire();
893 :     assertHasWaitersLocked(sync, c, NO_THREADS);
894 : jsr166 1.17 assertTrue(sync.getWaitingThreads(c).isEmpty());
895 : jsr166 1.24 assertTrue(acquired1.releaseShared(0));
896 : jsr166 1.10 c.await();
897 : jsr166 1.24 sync.release();
898 : jsr166 1.10 }});
899 :    
900 : jsr166 1.24 final Thread t2 = new Thread(new CheckedRunnable() {
901 : jsr166 1.10 public void realRun() throws InterruptedException {
902 : jsr166 1.24 sync.acquire();
903 :     assertHasWaitersLocked(sync, c, t1);
904 :     assertTrue(sync.getWaitingThreads(c).contains(t1));
905 : jsr166 1.17 assertFalse(sync.getWaitingThreads(c).isEmpty());
906 : jsr166 1.24 assertEquals(1, sync.getWaitingThreads(c).size());
907 :     assertTrue(acquired2.releaseShared(0));
908 : jsr166 1.10 c.await();
909 : jsr166 1.24 sync.release();
910 : jsr166 1.10 }});
911 : dl 1.1
912 : jsr166 1.24 sync.acquire();
913 :     assertHasWaitersLocked(sync, c, NO_THREADS);
914 :     assertFalse(sync.getWaitingThreads(c).contains(t1));
915 :     assertFalse(sync.getWaitingThreads(c).contains(t2));
916 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
917 :     assertEquals(0, sync.getWaitingThreads(c).size());
918 :     sync.release();
919 : dl 1.1
920 : jsr166 1.24 t1.start();
921 :     acquired1.acquireShared(0);
922 :     sync.acquire();
923 :     assertHasWaitersLocked(sync, c, t1);
924 :     assertTrue(sync.getWaitingThreads(c).contains(t1));
925 :     assertFalse(sync.getWaitingThreads(c).contains(t2));
926 :     assertFalse(sync.getWaitingThreads(c).isEmpty());
927 :     assertEquals(1, sync.getWaitingThreads(c).size());
928 :     sync.release();
929 : dl 1.1
930 : jsr166 1.24 t2.start();
931 :     acquired2.acquireShared(0);
932 :     sync.acquire();
933 :     assertHasWaitersLocked(sync, c, t1, t2);
934 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
935 :     assertTrue(sync.getWaitingThreads(c).contains(t1));
936 :     assertTrue(sync.getWaitingThreads(c).contains(t2));
937 :     assertFalse(sync.getWaitingThreads(c).isEmpty());
938 :     assertEquals(2, sync.getWaitingThreads(c).size());
939 :     c.signalAll();
940 :     assertHasWaitersLocked(sync, c, NO_THREADS);
941 :     assertHasExclusiveQueuedThreads(sync, t1, t2);
942 :     assertFalse(sync.getWaitingThreads(c).contains(t1));
943 :     assertFalse(sync.getWaitingThreads(c).contains(t2));
944 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
945 :     assertEquals(0, sync.getWaitingThreads(c).size());
946 :     sync.release();
947 :    
948 :     awaitTermination(t1);
949 :     awaitTermination(t2);
950 :     assertHasWaitersUnlocked(sync, c, NO_THREADS);
951 :     }
952 : dl 1.1
953 :     /**
954 : jsr166 1.25 * awaitUninterruptibly is uninterruptible
955 : dl 1.1 */
956 : jsr166 1.24 public void testAwaitUninterruptibly() {
957 : jsr166 1.11 final Mutex sync = new Mutex();
958 : jsr166 1.24 final ConditionObject c = sync.newCondition();
959 : jsr166 1.25 final BooleanLatch pleaseInterrupt = new BooleanLatch();
960 : jsr166 1.24 Thread t = newStartedThread(new CheckedRunnable() {
961 : jsr166 1.10 public void realRun() {
962 : jsr166 1.24 sync.acquire();
963 : jsr166 1.25 assertTrue(pleaseInterrupt.releaseShared(0));
964 : jsr166 1.10 c.awaitUninterruptibly();
965 : jsr166 1.24 assertTrue(Thread.interrupted());
966 :     assertHasWaitersLocked(sync, c, NO_THREADS);
967 :     sync.release();
968 : jsr166 1.10 }});
969 : dl 1.1
970 : jsr166 1.25 pleaseInterrupt.acquireShared(0);
971 : jsr166 1.24 sync.acquire();
972 :     assertHasWaitersLocked(sync, c, t);
973 :     sync.release();
974 : jsr166 1.9 t.interrupt();
975 : jsr166 1.24 assertHasWaitersUnlocked(sync, c, t);
976 :     assertThreadStaysAlive(t);
977 :     sync.acquire();
978 :     assertHasWaitersLocked(sync, c, t);
979 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
980 : jsr166 1.9 c.signal();
981 : jsr166 1.24 assertHasWaitersLocked(sync, c, NO_THREADS);
982 :     assertHasExclusiveQueuedThreads(sync, t);
983 :     sync.release();
984 :     awaitTermination(t);
985 : dl 1.1 }
986 :    
987 :     /**
988 : jsr166 1.24 * await/awaitNanos/awaitUntil is interruptible
989 : dl 1.1 */
990 : jsr166 1.24 public void testInterruptible_await() { testInterruptible(AwaitMethod.await); }
991 :     public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); }
992 :     public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); }
993 :     public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); }
994 :     public void testInterruptible(final AwaitMethod awaitMethod) {
995 : jsr166 1.11 final Mutex sync = new Mutex();
996 : jsr166 1.24 final ConditionObject c = sync.newCondition();
997 : jsr166 1.25 final BooleanLatch pleaseInterrupt = new BooleanLatch();
998 : jsr166 1.24 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
999 : jsr166 1.11 public void realRun() throws InterruptedException {
1000 : jsr166 1.24 sync.acquire();
1001 : jsr166 1.25 assertTrue(pleaseInterrupt.releaseShared(0));
1002 : jsr166 1.24 await(c, awaitMethod);
1003 : jsr166 1.10 }});
1004 : dl 1.1
1005 : jsr166 1.25 pleaseInterrupt.acquireShared(0);
1006 : jsr166 1.9 t.interrupt();
1007 : jsr166 1.24 awaitTermination(t);
1008 : dl 1.1 }
1009 :    
1010 :     /**
1011 :     * signalAll wakes up all threads
1012 :     */
1013 : jsr166 1.24 public void testSignalAll_await() { testSignalAll(AwaitMethod.await); }
1014 :     public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); }
1015 :     public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); }
1016 :     public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); }
1017 :     public void testSignalAll(final AwaitMethod awaitMethod) {
1018 :     final Mutex sync = new Mutex();
1019 :     final ConditionObject c = sync.newCondition();
1020 :     final BooleanLatch acquired1 = new BooleanLatch();
1021 :     final BooleanLatch acquired2 = new BooleanLatch();
1022 :     Thread t1 = newStartedThread(new CheckedRunnable() {
1023 : jsr166 1.10 public void realRun() throws InterruptedException {
1024 : jsr166 1.24 sync.acquire();
1025 :     acquired1.releaseShared(0);
1026 :     await(c, awaitMethod);
1027 :     sync.release();
1028 : jsr166 1.10 }});
1029 :    
1030 : jsr166 1.24 Thread t2 = newStartedThread(new CheckedRunnable() {
1031 : jsr166 1.10 public void realRun() throws InterruptedException {
1032 : jsr166 1.24 sync.acquire();
1033 :     acquired2.releaseShared(0);
1034 :     await(c, awaitMethod);
1035 :     sync.release();
1036 : jsr166 1.10 }});
1037 : dl 1.1
1038 : jsr166 1.24 acquired1.acquireShared(0);
1039 :     acquired2.acquireShared(0);
1040 :     sync.acquire();
1041 :     assertHasWaitersLocked(sync, c, t1, t2);
1042 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1043 : jsr166 1.9 c.signalAll();
1044 : jsr166 1.24 assertHasWaitersLocked(sync, c, NO_THREADS);
1045 :     assertHasExclusiveQueuedThreads(sync, t1, t2);
1046 :     sync.release();
1047 :     awaitTermination(t1);
1048 :     awaitTermination(t2);
1049 : dl 1.1 }
1050 :    
1051 :     /**
1052 :     * toString indicates current state
1053 :     */
1054 :     public void testToString() {
1055 :     Mutex sync = new Mutex();
1056 : jsr166 1.24 assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1057 :     sync.acquire();
1058 :     assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
1059 : dl 1.1 }
1060 :    
1061 :     /**
1062 : jsr166 1.24 * A serialized AQS deserializes with current state, but no queued threads
1063 :     */
1064 :     public void testSerialization() {
1065 :     Mutex sync = new Mutex();
1066 :     assertFalse(serialClone(sync).isHeldExclusively());
1067 :     sync.acquire();
1068 :     Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1069 :     waitForQueuedThread(sync, t);
1070 :     assertTrue(sync.isHeldExclusively());
1071 :    
1072 :     Mutex clone = serialClone(sync);
1073 :     assertTrue(clone.isHeldExclusively());
1074 :     assertHasExclusiveQueuedThreads(sync, t);
1075 :     assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1076 :     t.interrupt();
1077 :     awaitTermination(t);
1078 :     sync.release();
1079 :     assertFalse(sync.isHeldExclusively());
1080 :     assertTrue(clone.isHeldExclusively());
1081 :     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1082 :     assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1083 : dl 1.1 }
1084 :    
1085 :     /**
1086 :     * tryReleaseShared setting state changes getState
1087 :     */
1088 :     public void testGetStateWithReleaseShared() {
1089 : jsr166 1.11 final BooleanLatch l = new BooleanLatch();
1090 :     assertFalse(l.isSignalled());
1091 : jsr166 1.24 assertTrue(l.releaseShared(0));
1092 : jsr166 1.11 assertTrue(l.isSignalled());
1093 : dl 1.1 }
1094 :    
1095 :     /**
1096 :     * releaseShared has no effect when already signalled
1097 :     */
1098 :     public void testReleaseShared() {
1099 : jsr166 1.11 final BooleanLatch l = new BooleanLatch();
1100 :     assertFalse(l.isSignalled());
1101 : jsr166 1.24 assertTrue(l.releaseShared(0));
1102 : jsr166 1.11 assertTrue(l.isSignalled());
1103 : jsr166 1.24 assertTrue(l.releaseShared(0));
1104 : jsr166 1.11 assertTrue(l.isSignalled());
1105 : dl 1.1 }
1106 :    
1107 :     /**
1108 :     * acquireSharedInterruptibly returns after release, but not before
1109 :     */
1110 : jsr166 1.24 public void testAcquireSharedInterruptibly() {
1111 : jsr166 1.11 final BooleanLatch l = new BooleanLatch();
1112 : dl 1.1
1113 : jsr166 1.24 Thread t = newStartedThread(new CheckedRunnable() {
1114 : jsr166 1.10 public void realRun() throws InterruptedException {
1115 : jsr166 1.17 assertFalse(l.isSignalled());
1116 : jsr166 1.10 l.acquireSharedInterruptibly(0);
1117 : jsr166 1.17 assertTrue(l.isSignalled());
1118 : jsr166 1.24 l.acquireSharedInterruptibly(0);
1119 :     assertTrue(l.isSignalled());
1120 : jsr166 1.10 }});
1121 : jsr166 1.9
1122 : jsr166 1.24 waitForQueuedThread(l, t);
1123 : jsr166 1.9 assertFalse(l.isSignalled());
1124 : jsr166 1.24 assertThreadStaysAlive(t);
1125 :     assertHasSharedQueuedThreads(l, t);
1126 :     assertTrue(l.releaseShared(0));
1127 : jsr166 1.9 assertTrue(l.isSignalled());
1128 : jsr166 1.24 awaitTermination(t);
1129 : dl 1.1 }
1130 : jsr166 1.5
1131 : dl 1.1 /**
1132 : jsr166 1.24 * tryAcquireSharedNanos returns after release, but not before
1133 : dl 1.1 */
1134 : jsr166 1.24 public void testTryAcquireSharedNanos() {
1135 : jsr166 1.11 final BooleanLatch l = new BooleanLatch();
1136 : dl 1.1
1137 : jsr166 1.24 Thread t = newStartedThread(new CheckedRunnable() {
1138 : jsr166 1.10 public void realRun() throws InterruptedException {
1139 : jsr166 1.15 assertFalse(l.isSignalled());
1140 : jsr166 1.24 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1141 :     assertTrue(l.tryAcquireSharedNanos(0, nanos));
1142 :     assertTrue(l.isSignalled());
1143 : jsr166 1.15 assertTrue(l.tryAcquireSharedNanos(0, nanos));
1144 :     assertTrue(l.isSignalled());
1145 : jsr166 1.10 }});
1146 : jsr166 1.9
1147 : jsr166 1.24 waitForQueuedThread(l, t);
1148 : jsr166 1.9 assertFalse(l.isSignalled());
1149 : jsr166 1.24 assertThreadStaysAlive(t);
1150 :     assertTrue(l.releaseShared(0));
1151 : jsr166 1.9 assertTrue(l.isSignalled());
1152 : jsr166 1.24 awaitTermination(t);
1153 : dl 1.1 }
1154 : jsr166 1.5
1155 : dl 1.1 /**
1156 : jsr166 1.24 * acquireSharedInterruptibly is interruptible
1157 : dl 1.1 */
1158 : jsr166 1.24 public void testAcquireSharedInterruptibly_Interruptible() {
1159 : dl 1.1 final BooleanLatch l = new BooleanLatch();
1160 : jsr166 1.24 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1161 : jsr166 1.11 public void realRun() throws InterruptedException {
1162 : jsr166 1.17 assertFalse(l.isSignalled());
1163 : jsr166 1.10 l.acquireSharedInterruptibly(0);
1164 :     }});
1165 : jsr166 1.9
1166 : jsr166 1.24 waitForQueuedThread(l, t);
1167 : jsr166 1.9 assertFalse(l.isSignalled());
1168 :     t.interrupt();
1169 : jsr166 1.24 awaitTermination(t);
1170 :     assertFalse(l.isSignalled());
1171 : dl 1.1 }
1172 :    
1173 :     /**
1174 : jsr166 1.24 * tryAcquireSharedNanos is interruptible
1175 : dl 1.1 */
1176 : jsr166 1.24 public void testTryAcquireSharedNanos_Interruptible() {
1177 : dl 1.1 final BooleanLatch l = new BooleanLatch();
1178 : jsr166 1.24 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1179 : jsr166 1.11 public void realRun() throws InterruptedException {
1180 : jsr166 1.15 assertFalse(l.isSignalled());
1181 : jsr166 1.24 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1182 : jsr166 1.15 l.tryAcquireSharedNanos(0, nanos);
1183 : jsr166 1.10 }});
1184 : jsr166 1.9
1185 : jsr166 1.24 waitForQueuedThread(l, t);
1186 : jsr166 1.9 assertFalse(l.isSignalled());
1187 :     t.interrupt();
1188 : jsr166 1.24 awaitTermination(t);
1189 :     assertFalse(l.isSignalled());
1190 : dl 1.1 }
1191 :    
1192 :     /**
1193 : jsr166 1.24 * tryAcquireSharedNanos times out if not released before timeout
1194 : dl 1.1 */
1195 : jsr166 1.24 public void testTryAcquireSharedNanos_Timeout() {
1196 : dl 1.1 final BooleanLatch l = new BooleanLatch();
1197 : jsr166 1.27 final BooleanLatch observedQueued = new BooleanLatch();
1198 :     final long timeoutMillis = timeoutMillis();
1199 : jsr166 1.24 Thread t = newStartedThread(new CheckedRunnable() {
1200 : jsr166 1.10 public void realRun() throws InterruptedException {
1201 : jsr166 1.15 assertFalse(l.isSignalled());
1202 : jsr166 1.27 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 : jsr166 1.24 assertFalse(l.isSignalled());
1211 : jsr166 1.10 }});
1212 : jsr166 1.9
1213 : jsr166 1.24 waitForQueuedThread(l, t);
1214 : jsr166 1.27 observedQueued.releaseShared(0);
1215 : jsr166 1.24 assertFalse(l.isSignalled());
1216 :     awaitTermination(t);
1217 : jsr166 1.9 assertFalse(l.isSignalled());
1218 : dl 1.1 }
1219 :    
1220 : jsr166 1.28 /**
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 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8