ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.52
Committed: Fri Jul 3 05:48:30 2015 UTC (8 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.51: +9 -2 lines
Log Message:
only test Date/currentTimeMillis with Date/currentTimeMillis, not with nanoTime

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 jsr166 1.52 long startTime;
243 jsr166 1.41 try {
244     switch (awaitMethod) {
245     case awaitTimed:
246 jsr166 1.52 startTime = System.nanoTime();
247 jsr166 1.41 assertFalse(c.await(timeoutMillis, MILLISECONDS));
248 jsr166 1.52 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
249 jsr166 1.41 break;
250     case awaitNanos:
251 jsr166 1.52 startTime = System.nanoTime();
252 jsr166 1.41 long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
253     long nanosRemaining = c.awaitNanos(nanosTimeout);
254     assertTrue(nanosRemaining <= 0);
255 jsr166 1.51 assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
256 jsr166 1.52 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
257 jsr166 1.41 break;
258     case awaitUntil:
259 jsr166 1.52 // We shouldn't assume that nanoTime and currentTimeMillis
260     // use the same time source, so don't use nanoTime here.
261     java.util.Date delayedDate = delayedDate(timeoutMillis());
262 jsr166 1.41 assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
263 jsr166 1.52 assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
264 jsr166 1.41 break;
265     default:
266     throw new UnsupportedOperationException();
267     }
268     } catch (InterruptedException ie) { threadUnexpectedException(ie); }
269 dl 1.1 }
270 dl 1.16
271     /**
272     * isHeldExclusively is false upon construction
273     */
274 jsr166 1.22 public void testIsHeldExclusively() {
275 jsr166 1.41 Mutex sync = new Mutex();
276     assertFalse(sync.isHeldExclusively());
277 dl 1.16 }
278 jsr166 1.22
279 dl 1.1 /**
280 dl 1.17 * acquiring released sync succeeds
281 dl 1.1 */
282 jsr166 1.22 public void testAcquire() {
283 jsr166 1.41 Mutex sync = new Mutex();
284     sync.acquire();
285     assertTrue(sync.isHeldExclusively());
286     sync.release();
287     assertFalse(sync.isHeldExclusively());
288 dl 1.1 }
289    
290     /**
291 jsr166 1.41 * tryAcquire on a released sync succeeds
292 dl 1.1 */
293 jsr166 1.22 public void testTryAcquire() {
294 jsr166 1.41 Mutex sync = new Mutex();
295     assertTrue(sync.tryAcquire());
296     assertTrue(sync.isHeldExclusively());
297     sync.release();
298     assertFalse(sync.isHeldExclusively());
299 dl 1.1 }
300    
301     /**
302     * hasQueuedThreads reports whether there are waiting threads
303     */
304 jsr166 1.41 public void testHasQueuedThreads() {
305 jsr166 1.29 final Mutex sync = new Mutex();
306 jsr166 1.26 assertFalse(sync.hasQueuedThreads());
307 jsr166 1.41 sync.acquire();
308     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
309     waitForQueuedThread(sync, t1);
310 jsr166 1.26 assertTrue(sync.hasQueuedThreads());
311 jsr166 1.41 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
312     waitForQueuedThread(sync, t2);
313 jsr166 1.26 assertTrue(sync.hasQueuedThreads());
314     t1.interrupt();
315 jsr166 1.41 awaitTermination(t1);
316 jsr166 1.26 assertTrue(sync.hasQueuedThreads());
317 jsr166 1.41 sync.release();
318     awaitTermination(t2);
319 jsr166 1.26 assertFalse(sync.hasQueuedThreads());
320 jsr166 1.22 }
321 dl 1.10
322     /**
323 jsr166 1.41 * isQueued(null) throws NullPointerException
324 dl 1.11 */
325 jsr166 1.22 public void testIsQueuedNPE() {
326 jsr166 1.29 final Mutex sync = new Mutex();
327 dl 1.11 try {
328 dl 1.17 sync.isQueued(null);
329 dl 1.11 shouldThrow();
330 jsr166 1.26 } catch (NullPointerException success) {}
331 dl 1.11 }
332    
333     /**
334 jsr166 1.41 * isQueued reports whether a thread is queued
335 dl 1.10 */
336 jsr166 1.41 public void testIsQueued() {
337 jsr166 1.29 final Mutex sync = new Mutex();
338 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
339     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
340 jsr166 1.26 assertFalse(sync.isQueued(t1));
341     assertFalse(sync.isQueued(t2));
342 jsr166 1.41 sync.acquire();
343 jsr166 1.26 t1.start();
344 jsr166 1.41 waitForQueuedThread(sync, t1);
345 jsr166 1.26 assertTrue(sync.isQueued(t1));
346 jsr166 1.41 assertFalse(sync.isQueued(t2));
347 jsr166 1.26 t2.start();
348 jsr166 1.41 waitForQueuedThread(sync, t2);
349 jsr166 1.26 assertTrue(sync.isQueued(t1));
350     assertTrue(sync.isQueued(t2));
351     t1.interrupt();
352 jsr166 1.41 awaitTermination(t1);
353 jsr166 1.26 assertFalse(sync.isQueued(t1));
354     assertTrue(sync.isQueued(t2));
355 jsr166 1.41 sync.release();
356     awaitTermination(t2);
357 jsr166 1.26 assertFalse(sync.isQueued(t1));
358     assertFalse(sync.isQueued(t2));
359 jsr166 1.22 }
360 dl 1.8
361 dl 1.12 /**
362 dl 1.17 * getFirstQueuedThread returns first waiting thread or null if none
363 dl 1.12 */
364 jsr166 1.41 public void testGetFirstQueuedThread() {
365 jsr166 1.29 final Mutex sync = new Mutex();
366 jsr166 1.26 assertNull(sync.getFirstQueuedThread());
367 jsr166 1.41 sync.acquire();
368     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
369     waitForQueuedThread(sync, t1);
370 jsr166 1.26 assertEquals(t1, sync.getFirstQueuedThread());
371 jsr166 1.41 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
372     waitForQueuedThread(sync, t2);
373 jsr166 1.26 assertEquals(t1, sync.getFirstQueuedThread());
374     t1.interrupt();
375 jsr166 1.41 awaitTermination(t1);
376 jsr166 1.26 assertEquals(t2, sync.getFirstQueuedThread());
377 jsr166 1.41 sync.release();
378     awaitTermination(t2);
379 jsr166 1.26 assertNull(sync.getFirstQueuedThread());
380 jsr166 1.22 }
381 dl 1.12
382 dl 1.8 /**
383 dl 1.12 * hasContended reports false if no thread has ever blocked, else true
384 dl 1.8 */
385 jsr166 1.41 public void testHasContended() {
386 jsr166 1.29 final Mutex sync = new Mutex();
387 jsr166 1.26 assertFalse(sync.hasContended());
388 jsr166 1.41 sync.acquire();
389     assertFalse(sync.hasContended());
390     Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
391     waitForQueuedThread(sync, t1);
392 jsr166 1.26 assertTrue(sync.hasContended());
393 jsr166 1.41 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
394     waitForQueuedThread(sync, t2);
395 jsr166 1.26 assertTrue(sync.hasContended());
396     t1.interrupt();
397 jsr166 1.41 awaitTermination(t1);
398 jsr166 1.26 assertTrue(sync.hasContended());
399 jsr166 1.41 sync.release();
400     awaitTermination(t2);
401 jsr166 1.26 assertTrue(sync.hasContended());
402 jsr166 1.22 }
403 dl 1.17
404     /**
405 jsr166 1.41 * getQueuedThreads returns all waiting threads
406 dl 1.17 */
407 jsr166 1.41 public void testGetQueuedThreads() {
408 jsr166 1.29 final Mutex sync = new Mutex();
409 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
410     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
411 jsr166 1.41 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
412     sync.acquire();
413     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
414 jsr166 1.26 t1.start();
415 jsr166 1.41 waitForQueuedThread(sync, t1);
416     assertHasExclusiveQueuedThreads(sync, t1);
417 jsr166 1.26 assertTrue(sync.getQueuedThreads().contains(t1));
418 jsr166 1.41 assertFalse(sync.getQueuedThreads().contains(t2));
419 jsr166 1.26 t2.start();
420 jsr166 1.41 waitForQueuedThread(sync, t2);
421     assertHasExclusiveQueuedThreads(sync, t1, t2);
422 jsr166 1.26 assertTrue(sync.getQueuedThreads().contains(t1));
423     assertTrue(sync.getQueuedThreads().contains(t2));
424     t1.interrupt();
425 jsr166 1.41 awaitTermination(t1);
426     assertHasExclusiveQueuedThreads(sync, t2);
427     sync.release();
428     awaitTermination(t2);
429     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
430 jsr166 1.22 }
431 dl 1.17
432     /**
433 jsr166 1.41 * getExclusiveQueuedThreads returns all exclusive waiting threads
434 dl 1.17 */
435 jsr166 1.41 public void testGetExclusiveQueuedThreads() {
436 jsr166 1.29 final Mutex sync = new Mutex();
437 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
438     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
439 jsr166 1.41 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
440     sync.acquire();
441     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
442 jsr166 1.26 t1.start();
443 jsr166 1.41 waitForQueuedThread(sync, t1);
444     assertHasExclusiveQueuedThreads(sync, t1);
445 jsr166 1.26 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
446 jsr166 1.41 assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
447 jsr166 1.26 t2.start();
448 jsr166 1.41 waitForQueuedThread(sync, t2);
449     assertHasExclusiveQueuedThreads(sync, t1, t2);
450 jsr166 1.26 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
451     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
452     t1.interrupt();
453 jsr166 1.41 awaitTermination(t1);
454     assertHasExclusiveQueuedThreads(sync, t2);
455     sync.release();
456     awaitTermination(t2);
457     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
458 jsr166 1.22 }
459 dl 1.17
460     /**
461     * getSharedQueuedThreads does not include exclusively waiting threads
462     */
463 jsr166 1.41 public void testGetSharedQueuedThreads_Exclusive() {
464 jsr166 1.29 final Mutex sync = new Mutex();
465 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
466 jsr166 1.41 sync.acquire();
467 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
468 jsr166 1.41 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
469     waitForQueuedThread(sync, t1);
470 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
471 jsr166 1.41 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
472     waitForQueuedThread(sync, t2);
473 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
474     t1.interrupt();
475 jsr166 1.41 awaitTermination(t1);
476 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
477 jsr166 1.41 sync.release();
478     awaitTermination(t2);
479 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
480 jsr166 1.22 }
481 dl 1.1
482     /**
483 jsr166 1.41 * getSharedQueuedThreads returns all shared waiting threads
484     */
485     public void testGetSharedQueuedThreads_Shared() {
486     final BooleanLatch l = new BooleanLatch();
487     assertHasSharedQueuedThreads(l, NO_THREADS);
488     Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
489     public void realRun() throws InterruptedException {
490     l.acquireSharedInterruptibly(0);
491     }});
492     waitForQueuedThread(l, t1);
493     assertHasSharedQueuedThreads(l, t1);
494     Thread t2 = newStartedThread(new CheckedRunnable() {
495     public void realRun() throws InterruptedException {
496     l.acquireSharedInterruptibly(0);
497     }});
498     waitForQueuedThread(l, t2);
499     assertHasSharedQueuedThreads(l, t1, t2);
500     t1.interrupt();
501     awaitTermination(t1);
502     assertHasSharedQueuedThreads(l, t2);
503     assertTrue(l.releaseShared(0));
504     awaitTermination(t2);
505     assertHasSharedQueuedThreads(l, NO_THREADS);
506     }
507    
508     /**
509     * tryAcquireNanos is interruptible
510 dl 1.1 */
511 jsr166 1.41 public void testTryAcquireNanos_Interruptible() {
512 jsr166 1.29 final Mutex sync = new Mutex();
513 jsr166 1.41 sync.acquire();
514     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
515 jsr166 1.28 public void realRun() throws InterruptedException {
516 jsr166 1.41 sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
517 jsr166 1.28 }});
518 jsr166 1.26
519 jsr166 1.41 waitForQueuedThread(sync, t);
520 jsr166 1.26 t.interrupt();
521 jsr166 1.41 awaitTermination(t);
522 dl 1.1 }
523    
524     /**
525 jsr166 1.41 * tryAcquire on exclusively held sync fails
526 dl 1.1 */
527 jsr166 1.41 public void testTryAcquireWhenSynced() {
528 jsr166 1.29 final Mutex sync = new Mutex();
529 jsr166 1.41 sync.acquire();
530     Thread t = newStartedThread(new CheckedRunnable() {
531 jsr166 1.28 public void realRun() {
532 jsr166 1.41 assertFalse(sync.tryAcquire());
533 jsr166 1.28 }});
534 jsr166 1.26
535 jsr166 1.41 awaitTermination(t);
536     sync.release();
537 jsr166 1.22 }
538 dl 1.1
539     /**
540 dl 1.17 * tryAcquireNanos on an exclusively held sync times out
541 dl 1.1 */
542 jsr166 1.41 public void testAcquireNanos_Timeout() {
543 jsr166 1.29 final Mutex sync = new Mutex();
544 jsr166 1.41 sync.acquire();
545     Thread t = newStartedThread(new CheckedRunnable() {
546 jsr166 1.28 public void realRun() throws InterruptedException {
547 jsr166 1.41 long startTime = System.nanoTime();
548     long nanos = MILLISECONDS.toNanos(timeoutMillis());
549     assertFalse(sync.tryAcquireNanos(nanos));
550     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
551 jsr166 1.28 }});
552 jsr166 1.26
553 jsr166 1.41 awaitTermination(t);
554     sync.release();
555 jsr166 1.22 }
556    
557 dl 1.1 /**
558 dl 1.13 * getState is true when acquired and false when not
559 dl 1.1 */
560 jsr166 1.41 public void testGetState() {
561 jsr166 1.29 final Mutex sync = new Mutex();
562 jsr166 1.41 sync.acquire();
563 jsr166 1.29 assertTrue(sync.isHeldExclusively());
564 jsr166 1.41 sync.release();
565 jsr166 1.29 assertFalse(sync.isHeldExclusively());
566 jsr166 1.41
567     final BooleanLatch acquired = new BooleanLatch();
568     final BooleanLatch done = new BooleanLatch();
569     Thread t = newStartedThread(new CheckedRunnable() {
570 jsr166 1.28 public void realRun() throws InterruptedException {
571 jsr166 1.41 sync.acquire();
572     assertTrue(acquired.releaseShared(0));
573     done.acquireShared(0);
574     sync.release();
575 jsr166 1.28 }});
576 jsr166 1.26
577 jsr166 1.41 acquired.acquireShared(0);
578 jsr166 1.26 assertTrue(sync.isHeldExclusively());
579 jsr166 1.41 assertTrue(done.releaseShared(0));
580     awaitTermination(t);
581 jsr166 1.26 assertFalse(sync.isHeldExclusively());
582 dl 1.1 }
583    
584     /**
585 jsr166 1.41 * acquireInterruptibly succeeds when released, else is interruptible
586 dl 1.1 */
587 jsr166 1.41 public void testAcquireInterruptibly() throws InterruptedException {
588 jsr166 1.29 final Mutex sync = new Mutex();
589 jsr166 1.41 final BooleanLatch threadStarted = new BooleanLatch();
590     sync.acquireInterruptibly();
591     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
592     public void realRun() throws InterruptedException {
593     assertTrue(threadStarted.releaseShared(0));
594     sync.acquireInterruptibly();
595     }});
596 jsr166 1.26
597 jsr166 1.41 threadStarted.acquireShared(0);
598     waitForQueuedThread(sync, t);
599 jsr166 1.26 t.interrupt();
600 jsr166 1.41 awaitTermination(t);
601 jsr166 1.26 assertTrue(sync.isHeldExclusively());
602 dl 1.1 }
603    
604     /**
605 dl 1.17 * owns is true for a condition created by sync else false
606 dl 1.12 */
607     public void testOwns() {
608 jsr166 1.29 final Mutex sync = new Mutex();
609 jsr166 1.41 final ConditionObject c = sync.newCondition();
610 dl 1.17 final Mutex sync2 = new Mutex();
611     assertTrue(sync.owns(c));
612     assertFalse(sync2.owns(c));
613 dl 1.12 }
614    
615     /**
616 dl 1.17 * Calling await without holding sync throws IllegalMonitorStateException
617 dl 1.1 */
618 jsr166 1.41 public void testAwait_IMSE() {
619 jsr166 1.29 final Mutex sync = new Mutex();
620 jsr166 1.41 final ConditionObject c = sync.newCondition();
621     for (AwaitMethod awaitMethod : AwaitMethod.values()) {
622     long startTime = System.nanoTime();
623     try {
624     await(c, awaitMethod);
625     shouldThrow();
626     } catch (IllegalMonitorStateException success) {
627     } catch (InterruptedException e) { threadUnexpectedException(e); }
628     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
629     }
630 dl 1.1 }
631    
632     /**
633 dl 1.17 * Calling signal without holding sync throws IllegalMonitorStateException
634 dl 1.1 */
635 jsr166 1.41 public void testSignal_IMSE() {
636 jsr166 1.29 final Mutex sync = new Mutex();
637 jsr166 1.41 final ConditionObject c = sync.newCondition();
638 dl 1.1 try {
639     c.signal();
640     shouldThrow();
641 jsr166 1.28 } catch (IllegalMonitorStateException success) {}
642 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
643 dl 1.1 }
644    
645     /**
646 jsr166 1.41 * Calling signalAll without holding sync throws IllegalMonitorStateException
647 dl 1.1 */
648 jsr166 1.41 public void testSignalAll_IMSE() {
649 jsr166 1.29 final Mutex sync = new Mutex();
650 jsr166 1.41 final ConditionObject c = sync.newCondition();
651     try {
652     c.signalAll();
653     shouldThrow();
654     } catch (IllegalMonitorStateException success) {}
655 dl 1.1 }
656    
657     /**
658 jsr166 1.41 * await/awaitNanos/awaitUntil without a signal times out
659 dl 1.1 */
660 jsr166 1.41 public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); }
661     public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); }
662     public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); }
663     public void testAwait_Timeout(AwaitMethod awaitMethod) {
664 jsr166 1.29 final Mutex sync = new Mutex();
665 jsr166 1.41 final ConditionObject c = sync.newCondition();
666     sync.acquire();
667     assertAwaitTimesOut(c, awaitMethod);
668     sync.release();
669 dl 1.1 }
670    
671     /**
672 jsr166 1.41 * await/awaitNanos/awaitUntil returns when signalled
673 dl 1.1 */
674 jsr166 1.41 public void testSignal_await() { testSignal(AwaitMethod.await); }
675     public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); }
676     public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); }
677     public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); }
678     public void testSignal(final AwaitMethod awaitMethod) {
679 jsr166 1.29 final Mutex sync = new Mutex();
680 jsr166 1.41 final ConditionObject c = sync.newCondition();
681     final BooleanLatch acquired = new BooleanLatch();
682     Thread t = newStartedThread(new CheckedRunnable() {
683 jsr166 1.28 public void realRun() throws InterruptedException {
684 jsr166 1.41 sync.acquire();
685     assertTrue(acquired.releaseShared(0));
686     await(c, awaitMethod);
687     sync.release();
688 jsr166 1.28 }});
689 dl 1.15
690 jsr166 1.41 acquired.acquireShared(0);
691     sync.acquire();
692     assertHasWaitersLocked(sync, c, t);
693     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
694 jsr166 1.26 c.signal();
695 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
696     assertHasExclusiveQueuedThreads(sync, t);
697     sync.release();
698     awaitTermination(t);
699 dl 1.15 }
700    
701     /**
702 jsr166 1.41 * hasWaiters(null) throws NullPointerException
703 dl 1.15 */
704     public void testHasWaitersNPE() {
705 jsr166 1.29 final Mutex sync = new Mutex();
706 dl 1.15 try {
707 dl 1.17 sync.hasWaiters(null);
708 dl 1.15 shouldThrow();
709 jsr166 1.26 } catch (NullPointerException success) {}
710 dl 1.15 }
711    
712     /**
713 jsr166 1.41 * getWaitQueueLength(null) throws NullPointerException
714 dl 1.15 */
715     public void testGetWaitQueueLengthNPE() {
716 jsr166 1.29 final Mutex sync = new Mutex();
717 dl 1.15 try {
718 dl 1.17 sync.getWaitQueueLength(null);
719 dl 1.15 shouldThrow();
720 jsr166 1.26 } catch (NullPointerException success) {}
721 dl 1.15 }
722    
723     /**
724 jsr166 1.41 * getWaitingThreads(null) throws NullPointerException
725 dl 1.15 */
726     public void testGetWaitingThreadsNPE() {
727 jsr166 1.29 final Mutex sync = new Mutex();
728 dl 1.15 try {
729 dl 1.17 sync.getWaitingThreads(null);
730 dl 1.15 shouldThrow();
731 jsr166 1.26 } catch (NullPointerException success) {}
732 dl 1.15 }
733    
734     /**
735 jsr166 1.41 * hasWaiters throws IllegalArgumentException if not owned
736 dl 1.15 */
737     public void testHasWaitersIAE() {
738 jsr166 1.29 final Mutex sync = new Mutex();
739 jsr166 1.41 final ConditionObject c = sync.newCondition();
740 jsr166 1.29 final Mutex sync2 = new Mutex();
741 dl 1.15 try {
742 dl 1.17 sync2.hasWaiters(c);
743 dl 1.15 shouldThrow();
744 jsr166 1.26 } catch (IllegalArgumentException success) {}
745 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
746 dl 1.15 }
747    
748     /**
749 jsr166 1.41 * hasWaiters throws IllegalMonitorStateException if not synced
750 dl 1.15 */
751     public void testHasWaitersIMSE() {
752 jsr166 1.29 final Mutex sync = new Mutex();
753 jsr166 1.41 final ConditionObject c = sync.newCondition();
754 dl 1.15 try {
755 dl 1.17 sync.hasWaiters(c);
756 dl 1.15 shouldThrow();
757 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
758 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
759 dl 1.15 }
760    
761     /**
762 jsr166 1.41 * getWaitQueueLength throws IllegalArgumentException if not owned
763 dl 1.15 */
764     public void testGetWaitQueueLengthIAE() {
765 jsr166 1.29 final Mutex sync = new Mutex();
766 jsr166 1.41 final ConditionObject c = sync.newCondition();
767 jsr166 1.29 final Mutex sync2 = new Mutex();
768 dl 1.15 try {
769 dl 1.17 sync2.getWaitQueueLength(c);
770 dl 1.15 shouldThrow();
771 jsr166 1.26 } catch (IllegalArgumentException success) {}
772 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
773 dl 1.15 }
774    
775     /**
776 jsr166 1.41 * getWaitQueueLength throws IllegalMonitorStateException if not synced
777 dl 1.15 */
778     public void testGetWaitQueueLengthIMSE() {
779 jsr166 1.29 final Mutex sync = new Mutex();
780 jsr166 1.41 final ConditionObject c = sync.newCondition();
781 dl 1.15 try {
782 dl 1.17 sync.getWaitQueueLength(c);
783 dl 1.15 shouldThrow();
784 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
785 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
786 dl 1.15 }
787    
788     /**
789 jsr166 1.41 * getWaitingThreads throws IllegalArgumentException if not owned
790 dl 1.15 */
791     public void testGetWaitingThreadsIAE() {
792 jsr166 1.29 final Mutex sync = new Mutex();
793 jsr166 1.41 final ConditionObject c = sync.newCondition();
794 jsr166 1.29 final Mutex sync2 = new Mutex();
795 dl 1.15 try {
796 dl 1.17 sync2.getWaitingThreads(c);
797 dl 1.15 shouldThrow();
798 jsr166 1.26 } catch (IllegalArgumentException success) {}
799 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
800 dl 1.15 }
801    
802     /**
803 jsr166 1.41 * getWaitingThreads throws IllegalMonitorStateException if not synced
804 dl 1.15 */
805     public void testGetWaitingThreadsIMSE() {
806 jsr166 1.29 final Mutex sync = new Mutex();
807 jsr166 1.41 final ConditionObject c = sync.newCondition();
808 dl 1.15 try {
809 dl 1.17 sync.getWaitingThreads(c);
810 dl 1.15 shouldThrow();
811 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
812 jsr166 1.41 assertHasWaitersUnlocked(sync, c, NO_THREADS);
813 dl 1.15 }
814    
815     /**
816     * hasWaiters returns true when a thread is waiting, else false
817     */
818 jsr166 1.41 public void testHasWaiters() {
819 jsr166 1.29 final Mutex sync = new Mutex();
820 jsr166 1.41 final ConditionObject c = sync.newCondition();
821     final BooleanLatch acquired = new BooleanLatch();
822     Thread t = newStartedThread(new CheckedRunnable() {
823 jsr166 1.28 public void realRun() throws InterruptedException {
824 jsr166 1.41 sync.acquire();
825     assertHasWaitersLocked(sync, c, NO_THREADS);
826 jsr166 1.35 assertFalse(sync.hasWaiters(c));
827 jsr166 1.41 assertTrue(acquired.releaseShared(0));
828 jsr166 1.28 c.await();
829 jsr166 1.41 sync.release();
830 jsr166 1.28 }});
831 dl 1.15
832 jsr166 1.41 acquired.acquireShared(0);
833     sync.acquire();
834     assertHasWaitersLocked(sync, c, t);
835     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
836 jsr166 1.26 assertTrue(sync.hasWaiters(c));
837     c.signal();
838 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
839     assertHasExclusiveQueuedThreads(sync, t);
840 jsr166 1.26 assertFalse(sync.hasWaiters(c));
841 jsr166 1.41 sync.release();
842    
843     awaitTermination(t);
844     assertHasWaitersUnlocked(sync, c, NO_THREADS);
845 dl 1.15 }
846    
847     /**
848     * getWaitQueueLength returns number of waiting threads
849     */
850 jsr166 1.41 public void testGetWaitQueueLength() {
851 jsr166 1.29 final Mutex sync = new Mutex();
852 jsr166 1.41 final ConditionObject c = sync.newCondition();
853     final BooleanLatch acquired1 = new BooleanLatch();
854     final BooleanLatch acquired2 = new BooleanLatch();
855     final Thread t1 = newStartedThread(new CheckedRunnable() {
856 jsr166 1.28 public void realRun() throws InterruptedException {
857 jsr166 1.41 sync.acquire();
858     assertHasWaitersLocked(sync, c, NO_THREADS);
859 jsr166 1.35 assertEquals(0, sync.getWaitQueueLength(c));
860 jsr166 1.41 assertTrue(acquired1.releaseShared(0));
861 jsr166 1.28 c.await();
862 jsr166 1.41 sync.release();
863 jsr166 1.28 }});
864 jsr166 1.41 acquired1.acquireShared(0);
865     sync.acquire();
866     assertHasWaitersLocked(sync, c, t1);
867     assertEquals(1, sync.getWaitQueueLength(c));
868     sync.release();
869 jsr166 1.28
870 jsr166 1.41 final Thread t2 = newStartedThread(new CheckedRunnable() {
871 jsr166 1.28 public void realRun() throws InterruptedException {
872 jsr166 1.41 sync.acquire();
873     assertHasWaitersLocked(sync, c, t1);
874 jsr166 1.35 assertEquals(1, sync.getWaitQueueLength(c));
875 jsr166 1.41 assertTrue(acquired2.releaseShared(0));
876 jsr166 1.28 c.await();
877 jsr166 1.41 sync.release();
878 jsr166 1.28 }});
879 jsr166 1.41 acquired2.acquireShared(0);
880     sync.acquire();
881     assertHasWaitersLocked(sync, c, t1, t2);
882     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
883 jsr166 1.26 assertEquals(2, sync.getWaitQueueLength(c));
884     c.signalAll();
885 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
886     assertHasExclusiveQueuedThreads(sync, t1, t2);
887 jsr166 1.26 assertEquals(0, sync.getWaitQueueLength(c));
888 jsr166 1.41 sync.release();
889    
890     awaitTermination(t1);
891     awaitTermination(t2);
892     assertHasWaitersUnlocked(sync, c, NO_THREADS);
893 dl 1.15 }
894    
895     /**
896     * getWaitingThreads returns only and all waiting threads
897     */
898 jsr166 1.41 public void testGetWaitingThreads() {
899 jsr166 1.29 final Mutex sync = new Mutex();
900 jsr166 1.41 final ConditionObject c = sync.newCondition();
901     final BooleanLatch acquired1 = new BooleanLatch();
902     final BooleanLatch acquired2 = new BooleanLatch();
903     final Thread t1 = new Thread(new CheckedRunnable() {
904 jsr166 1.28 public void realRun() throws InterruptedException {
905 jsr166 1.41 sync.acquire();
906     assertHasWaitersLocked(sync, c, NO_THREADS);
907 jsr166 1.35 assertTrue(sync.getWaitingThreads(c).isEmpty());
908 jsr166 1.41 assertTrue(acquired1.releaseShared(0));
909 jsr166 1.28 c.await();
910 jsr166 1.41 sync.release();
911 jsr166 1.28 }});
912    
913 jsr166 1.41 final Thread t2 = new Thread(new CheckedRunnable() {
914 jsr166 1.28 public void realRun() throws InterruptedException {
915 jsr166 1.41 sync.acquire();
916     assertHasWaitersLocked(sync, c, t1);
917     assertTrue(sync.getWaitingThreads(c).contains(t1));
918 jsr166 1.35 assertFalse(sync.getWaitingThreads(c).isEmpty());
919 jsr166 1.41 assertEquals(1, sync.getWaitingThreads(c).size());
920     assertTrue(acquired2.releaseShared(0));
921 jsr166 1.28 c.await();
922 jsr166 1.41 sync.release();
923 jsr166 1.28 }});
924 dl 1.1
925 jsr166 1.41 sync.acquire();
926     assertHasWaitersLocked(sync, c, NO_THREADS);
927     assertFalse(sync.getWaitingThreads(c).contains(t1));
928     assertFalse(sync.getWaitingThreads(c).contains(t2));
929 jsr166 1.26 assertTrue(sync.getWaitingThreads(c).isEmpty());
930 jsr166 1.41 assertEquals(0, sync.getWaitingThreads(c).size());
931     sync.release();
932    
933 jsr166 1.26 t1.start();
934 jsr166 1.41 acquired1.acquireShared(0);
935     sync.acquire();
936     assertHasWaitersLocked(sync, c, t1);
937     assertTrue(sync.getWaitingThreads(c).contains(t1));
938     assertFalse(sync.getWaitingThreads(c).contains(t2));
939     assertFalse(sync.getWaitingThreads(c).isEmpty());
940     assertEquals(1, sync.getWaitingThreads(c).size());
941     sync.release();
942    
943 jsr166 1.26 t2.start();
944 jsr166 1.41 acquired2.acquireShared(0);
945     sync.acquire();
946     assertHasWaitersLocked(sync, c, t1, t2);
947     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
948 jsr166 1.26 assertTrue(sync.getWaitingThreads(c).contains(t1));
949     assertTrue(sync.getWaitingThreads(c).contains(t2));
950 jsr166 1.41 assertFalse(sync.getWaitingThreads(c).isEmpty());
951     assertEquals(2, sync.getWaitingThreads(c).size());
952 jsr166 1.26 c.signalAll();
953 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
954     assertHasExclusiveQueuedThreads(sync, t1, t2);
955     assertFalse(sync.getWaitingThreads(c).contains(t1));
956     assertFalse(sync.getWaitingThreads(c).contains(t2));
957 jsr166 1.26 assertTrue(sync.getWaitingThreads(c).isEmpty());
958 jsr166 1.41 assertEquals(0, sync.getWaitingThreads(c).size());
959     sync.release();
960    
961     awaitTermination(t1);
962     awaitTermination(t2);
963     assertHasWaitersUnlocked(sync, c, NO_THREADS);
964 dl 1.15 }
965    
966     /**
967 jsr166 1.42 * awaitUninterruptibly is uninterruptible
968 dl 1.15 */
969 jsr166 1.41 public void testAwaitUninterruptibly() {
970 jsr166 1.29 final Mutex sync = new Mutex();
971 jsr166 1.41 final ConditionObject c = sync.newCondition();
972 jsr166 1.42 final BooleanLatch pleaseInterrupt = new BooleanLatch();
973 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
974 jsr166 1.28 public void realRun() {
975 jsr166 1.41 sync.acquire();
976 jsr166 1.42 assertTrue(pleaseInterrupt.releaseShared(0));
977 jsr166 1.28 c.awaitUninterruptibly();
978 jsr166 1.41 assertTrue(Thread.interrupted());
979     assertHasWaitersLocked(sync, c, NO_THREADS);
980     sync.release();
981 jsr166 1.28 }});
982 dl 1.15
983 jsr166 1.42 pleaseInterrupt.acquireShared(0);
984 jsr166 1.41 sync.acquire();
985     assertHasWaitersLocked(sync, c, t);
986     sync.release();
987 jsr166 1.26 t.interrupt();
988 jsr166 1.41 assertHasWaitersUnlocked(sync, c, t);
989     assertThreadStaysAlive(t);
990     sync.acquire();
991     assertHasWaitersLocked(sync, c, t);
992     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
993 jsr166 1.26 c.signal();
994 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
995     assertHasExclusiveQueuedThreads(sync, t);
996     sync.release();
997     awaitTermination(t);
998 dl 1.15 }
999    
1000     /**
1001 jsr166 1.41 * await/awaitNanos/awaitUntil is interruptible
1002 dl 1.15 */
1003 jsr166 1.41 public void testInterruptible_await() { testInterruptible(AwaitMethod.await); }
1004     public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); }
1005     public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); }
1006     public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); }
1007     public void testInterruptible(final AwaitMethod awaitMethod) {
1008 jsr166 1.29 final Mutex sync = new Mutex();
1009 jsr166 1.41 final ConditionObject c = sync.newCondition();
1010 jsr166 1.42 final BooleanLatch pleaseInterrupt = new BooleanLatch();
1011 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1012 jsr166 1.29 public void realRun() throws InterruptedException {
1013 jsr166 1.41 sync.acquire();
1014 jsr166 1.42 assertTrue(pleaseInterrupt.releaseShared(0));
1015 jsr166 1.41 await(c, awaitMethod);
1016 jsr166 1.28 }});
1017 dl 1.15
1018 jsr166 1.42 pleaseInterrupt.acquireShared(0);
1019 jsr166 1.26 t.interrupt();
1020 jsr166 1.41 awaitTermination(t);
1021 dl 1.12 }
1022    
1023 dl 1.14 /**
1024 dl 1.15 * signalAll wakes up all threads
1025     */
1026 jsr166 1.41 public void testSignalAll_await() { testSignalAll(AwaitMethod.await); }
1027     public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); }
1028     public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); }
1029     public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); }
1030     public void testSignalAll(final AwaitMethod awaitMethod) {
1031     final Mutex sync = new Mutex();
1032     final ConditionObject c = sync.newCondition();
1033     final BooleanLatch acquired1 = new BooleanLatch();
1034     final BooleanLatch acquired2 = new BooleanLatch();
1035     Thread t1 = newStartedThread(new CheckedRunnable() {
1036 jsr166 1.28 public void realRun() throws InterruptedException {
1037 jsr166 1.41 sync.acquire();
1038     acquired1.releaseShared(0);
1039     await(c, awaitMethod);
1040     sync.release();
1041 jsr166 1.28 }});
1042    
1043 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
1044 jsr166 1.28 public void realRun() throws InterruptedException {
1045 jsr166 1.41 sync.acquire();
1046     acquired2.releaseShared(0);
1047     await(c, awaitMethod);
1048     sync.release();
1049 jsr166 1.28 }});
1050 dl 1.15
1051 jsr166 1.41 acquired1.acquireShared(0);
1052     acquired2.acquireShared(0);
1053     sync.acquire();
1054     assertHasWaitersLocked(sync, c, t1, t2);
1055     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1056 jsr166 1.26 c.signalAll();
1057 jsr166 1.41 assertHasWaitersLocked(sync, c, NO_THREADS);
1058     assertHasExclusiveQueuedThreads(sync, t1, t2);
1059     sync.release();
1060     awaitTermination(t1);
1061     awaitTermination(t2);
1062 dl 1.15 }
1063    
1064     /**
1065 dl 1.14 * toString indicates current state
1066     */
1067     public void testToString() {
1068 dl 1.17 Mutex sync = new Mutex();
1069 jsr166 1.41 assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1070     sync.acquire();
1071     assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
1072 dl 1.14 }
1073    
1074     /**
1075 jsr166 1.41 * A serialized AQS deserializes with current state, but no queued threads
1076     */
1077     public void testSerialization() {
1078     Mutex sync = new Mutex();
1079     assertFalse(serialClone(sync).isHeldExclusively());
1080     sync.acquire();
1081     Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1082     waitForQueuedThread(sync, t);
1083     assertTrue(sync.isHeldExclusively());
1084    
1085     Mutex clone = serialClone(sync);
1086     assertTrue(clone.isHeldExclusively());
1087     assertHasExclusiveQueuedThreads(sync, t);
1088     assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1089     t.interrupt();
1090     awaitTermination(t);
1091     sync.release();
1092     assertFalse(sync.isHeldExclusively());
1093     assertTrue(clone.isHeldExclusively());
1094     assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1095     assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1096 dl 1.14 }
1097    
1098 dl 1.12 /**
1099 dl 1.15 * tryReleaseShared setting state changes getState
1100 dl 1.12 */
1101 dl 1.15 public void testGetStateWithReleaseShared() {
1102 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
1103     assertFalse(l.isSignalled());
1104 jsr166 1.41 assertTrue(l.releaseShared(0));
1105 jsr166 1.29 assertTrue(l.isSignalled());
1106 dl 1.12 }
1107    
1108     /**
1109 dl 1.17 * releaseShared has no effect when already signalled
1110 dl 1.12 */
1111 dl 1.13 public void testReleaseShared() {
1112 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
1113     assertFalse(l.isSignalled());
1114 jsr166 1.41 assertTrue(l.releaseShared(0));
1115 jsr166 1.29 assertTrue(l.isSignalled());
1116 jsr166 1.41 assertTrue(l.releaseShared(0));
1117 jsr166 1.29 assertTrue(l.isSignalled());
1118 dl 1.12 }
1119    
1120     /**
1121     * acquireSharedInterruptibly returns after release, but not before
1122     */
1123 jsr166 1.41 public void testAcquireSharedInterruptibly() {
1124 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
1125 dl 1.12
1126 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
1127 jsr166 1.28 public void realRun() throws InterruptedException {
1128 jsr166 1.35 assertFalse(l.isSignalled());
1129 jsr166 1.28 l.acquireSharedInterruptibly(0);
1130 jsr166 1.35 assertTrue(l.isSignalled());
1131 jsr166 1.41 l.acquireSharedInterruptibly(0);
1132     assertTrue(l.isSignalled());
1133 jsr166 1.28 }});
1134 jsr166 1.26
1135 jsr166 1.41 waitForQueuedThread(l, t);
1136 jsr166 1.26 assertFalse(l.isSignalled());
1137 jsr166 1.41 assertThreadStaysAlive(t);
1138     assertHasSharedQueuedThreads(l, t);
1139     assertTrue(l.releaseShared(0));
1140 jsr166 1.26 assertTrue(l.isSignalled());
1141 jsr166 1.41 awaitTermination(t);
1142 dl 1.12 }
1143 jsr166 1.22
1144 dl 1.12 /**
1145 jsr166 1.41 * tryAcquireSharedNanos returns after release, but not before
1146 dl 1.12 */
1147 jsr166 1.41 public void testTryAcquireSharedNanos() {
1148 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
1149 dl 1.12
1150 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
1151 jsr166 1.28 public void realRun() throws InterruptedException {
1152 jsr166 1.33 assertFalse(l.isSignalled());
1153 jsr166 1.41 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1154     assertTrue(l.tryAcquireSharedNanos(0, nanos));
1155     assertTrue(l.isSignalled());
1156 jsr166 1.33 assertTrue(l.tryAcquireSharedNanos(0, nanos));
1157     assertTrue(l.isSignalled());
1158 jsr166 1.28 }});
1159 jsr166 1.26
1160 jsr166 1.41 waitForQueuedThread(l, t);
1161 jsr166 1.26 assertFalse(l.isSignalled());
1162 jsr166 1.41 assertThreadStaysAlive(t);
1163     assertTrue(l.releaseShared(0));
1164 jsr166 1.26 assertTrue(l.isSignalled());
1165 jsr166 1.41 awaitTermination(t);
1166 dl 1.12 }
1167 jsr166 1.22
1168 dl 1.12 /**
1169 jsr166 1.41 * acquireSharedInterruptibly is interruptible
1170 dl 1.12 */
1171 jsr166 1.41 public void testAcquireSharedInterruptibly_Interruptible() {
1172 dl 1.12 final BooleanLatch l = new BooleanLatch();
1173 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1174 jsr166 1.29 public void realRun() throws InterruptedException {
1175 jsr166 1.35 assertFalse(l.isSignalled());
1176 jsr166 1.28 l.acquireSharedInterruptibly(0);
1177     }});
1178    
1179 jsr166 1.41 waitForQueuedThread(l, t);
1180 jsr166 1.26 assertFalse(l.isSignalled());
1181     t.interrupt();
1182 jsr166 1.41 awaitTermination(t);
1183     assertFalse(l.isSignalled());
1184 dl 1.12 }
1185    
1186     /**
1187 jsr166 1.41 * tryAcquireSharedNanos is interruptible
1188 dl 1.12 */
1189 jsr166 1.41 public void testTryAcquireSharedNanos_Interruptible() {
1190 dl 1.12 final BooleanLatch l = new BooleanLatch();
1191 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1192 jsr166 1.29 public void realRun() throws InterruptedException {
1193 jsr166 1.33 assertFalse(l.isSignalled());
1194 jsr166 1.41 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1195 jsr166 1.33 l.tryAcquireSharedNanos(0, nanos);
1196 jsr166 1.28 }});
1197    
1198 jsr166 1.41 waitForQueuedThread(l, t);
1199 jsr166 1.26 assertFalse(l.isSignalled());
1200     t.interrupt();
1201 jsr166 1.41 awaitTermination(t);
1202     assertFalse(l.isSignalled());
1203 dl 1.12 }
1204    
1205     /**
1206 jsr166 1.41 * tryAcquireSharedNanos times out if not released before timeout
1207 dl 1.12 */
1208 jsr166 1.41 public void testTryAcquireSharedNanos_Timeout() {
1209 dl 1.12 final BooleanLatch l = new BooleanLatch();
1210 jsr166 1.44 final BooleanLatch observedQueued = new BooleanLatch();
1211 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
1212 jsr166 1.28 public void realRun() throws InterruptedException {
1213 jsr166 1.33 assertFalse(l.isSignalled());
1214 jsr166 1.44 for (long millis = timeoutMillis();
1215     !observedQueued.isSignalled();
1216     millis *= 2) {
1217     long nanos = MILLISECONDS.toNanos(millis);
1218     long startTime = System.nanoTime();
1219     assertFalse(l.tryAcquireSharedNanos(0, nanos));
1220     assertTrue(millisElapsedSince(startTime) >= millis);
1221     }
1222 jsr166 1.41 assertFalse(l.isSignalled());
1223 jsr166 1.28 }});
1224    
1225 jsr166 1.41 waitForQueuedThread(l, t);
1226 jsr166 1.44 observedQueued.releaseShared(0);
1227 jsr166 1.41 assertFalse(l.isSignalled());
1228     awaitTermination(t);
1229 jsr166 1.26 assertFalse(l.isSignalled());
1230 dl 1.1 }
1231 dl 1.14
1232 jsr166 1.45 /**
1233     * awaitNanos/timed await with 0 wait times out immediately
1234     */
1235     public void testAwait_Zero() throws InterruptedException {
1236     final Mutex sync = new Mutex();
1237     final ConditionObject c = sync.newCondition();
1238     sync.acquire();
1239     assertTrue(c.awaitNanos(0L) <= 0);
1240     assertFalse(c.await(0L, NANOSECONDS));
1241     sync.release();
1242     }
1243    
1244     /**
1245     * awaitNanos/timed await with maximum negative wait times does not underflow
1246     */
1247     public void testAwait_NegativeInfinity() throws InterruptedException {
1248     final Mutex sync = new Mutex();
1249     final ConditionObject c = sync.newCondition();
1250     sync.acquire();
1251     assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1252     assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1253     sync.release();
1254     }
1255    
1256 dl 1.1 }