ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.50
Committed: Sat Apr 25 04:55:30 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.49: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

File Contents

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