ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.38
Committed: Sun May 14 02:16:56 2017 UTC (7 years ago) by jsr166
Branch: MAIN
Changes since 1.37: +9 -9 lines
Log Message:
improve testAwaitUninterruptibly

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