ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.31
Committed: Sat Nov 21 17:54:04 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +0 -3 lines
Log Message:
remove dead test code

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     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.22 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9    
10     import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13 jsr166 1.30 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 dl 1.1 import java.util.concurrent.locks.*;
15     import java.io.*;
16    
17     public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
18     public static void main(String[] args) {
19     junit.textui.TestRunner.run (suite());
20     }
21     public static Test suite() {
22     return new TestSuite(AbstractQueuedSynchronizerTest.class);
23     }
24    
25     /**
26 dl 1.12 * A simple mutex class, adapted from the
27     * AbstractQueuedSynchronizer javadoc. Exclusive acquire tests
28     * exercise this as a sample user extension. Other
29     * methods/features of AbstractQueuedSynchronizerTest are tested
30     * via other test classes, including those for ReentrantLock,
31     * ReentrantReadWriteLock, and Semaphore
32     */
33     static class Mutex extends AbstractQueuedSynchronizer {
34 dl 1.16 public boolean isHeldExclusively() { return getState() == 1; }
35 jsr166 1.22
36 dl 1.16 public boolean tryAcquire(int acquires) {
37 jsr166 1.22 assertTrue(acquires == 1);
38 dl 1.12 return compareAndSetState(0, 1);
39     }
40 jsr166 1.22
41 dl 1.16 public boolean tryRelease(int releases) {
42     if (getState() == 0) throw new IllegalMonitorStateException();
43 dl 1.12 setState(0);
44     return true;
45     }
46 jsr166 1.22
47 dl 1.15 public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
48 dl 1.12
49     }
50    
51 jsr166 1.22
52 dl 1.12 /**
53     * A simple latch class, to test shared mode.
54     */
55 jsr166 1.22 static class BooleanLatch extends AbstractQueuedSynchronizer {
56 dl 1.12 public boolean isSignalled() { return getState() != 0; }
57    
58     public int tryAcquireShared(int ignore) {
59     return isSignalled()? 1 : -1;
60 dl 1.1 }
61 jsr166 1.22
62 dl 1.12 public boolean tryReleaseShared(int ignore) {
63     setState(1);
64     return true;
65 dl 1.1 }
66     }
67    
68     /**
69 jsr166 1.28 * A runnable calling acquireInterruptibly that does not expect to
70     * be interrupted.
71 dl 1.1 */
72 jsr166 1.28 class InterruptibleSyncRunnable extends CheckedRunnable {
73 dl 1.17 final Mutex sync;
74     InterruptibleSyncRunnable(Mutex l) { sync = l; }
75 jsr166 1.28 public void realRun() throws InterruptedException {
76     sync.acquireInterruptibly(1);
77 dl 1.1 }
78     }
79    
80    
81     /**
82 dl 1.16 * A runnable calling acquireInterruptibly that expects to be
83 jsr166 1.28 * interrupted.
84 dl 1.1 */
85 jsr166 1.28 class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
86 dl 1.17 final Mutex sync;
87     InterruptedSyncRunnable(Mutex l) { sync = l; }
88 jsr166 1.28 public void realRun() throws InterruptedException {
89     sync.acquireInterruptibly(1);
90 dl 1.1 }
91     }
92 dl 1.16
93     /**
94     * isHeldExclusively is false upon construction
95     */
96 jsr166 1.22 public void testIsHeldExclusively() {
97 jsr166 1.29 Mutex rl = new Mutex();
98 dl 1.16 assertFalse(rl.isHeldExclusively());
99     }
100 jsr166 1.22
101 dl 1.1 /**
102 dl 1.17 * acquiring released sync succeeds
103 dl 1.1 */
104 jsr166 1.22 public void testAcquire() {
105 jsr166 1.29 Mutex rl = new Mutex();
106 dl 1.16 rl.acquire(1);
107     assertTrue(rl.isHeldExclusively());
108     rl.release(1);
109 dl 1.17 assertFalse(rl.isHeldExclusively());
110 dl 1.1 }
111    
112     /**
113 dl 1.17 * tryAcquire on an released sync succeeds
114 dl 1.1 */
115 jsr166 1.22 public void testTryAcquire() {
116 jsr166 1.29 Mutex rl = new Mutex();
117 dl 1.16 assertTrue(rl.tryAcquire(1));
118     assertTrue(rl.isHeldExclusively());
119     rl.release(1);
120 dl 1.1 }
121    
122     /**
123     * hasQueuedThreads reports whether there are waiting threads
124     */
125 jsr166 1.26 public void testhasQueuedThreads() throws InterruptedException {
126 jsr166 1.29 final Mutex sync = new Mutex();
127 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
128     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
129 jsr166 1.26 assertFalse(sync.hasQueuedThreads());
130     sync.acquire(1);
131     t1.start();
132     Thread.sleep(SHORT_DELAY_MS);
133     assertTrue(sync.hasQueuedThreads());
134     t2.start();
135     Thread.sleep(SHORT_DELAY_MS);
136     assertTrue(sync.hasQueuedThreads());
137     t1.interrupt();
138     Thread.sleep(SHORT_DELAY_MS);
139     assertTrue(sync.hasQueuedThreads());
140     sync.release(1);
141     Thread.sleep(SHORT_DELAY_MS);
142     assertFalse(sync.hasQueuedThreads());
143     t1.join();
144     t2.join();
145 jsr166 1.22 }
146 dl 1.10
147     /**
148 dl 1.11 * isQueued(null) throws NPE
149     */
150 jsr166 1.22 public void testIsQueuedNPE() {
151 jsr166 1.29 final Mutex sync = new Mutex();
152 dl 1.11 try {
153 dl 1.17 sync.isQueued(null);
154 dl 1.11 shouldThrow();
155 jsr166 1.26 } catch (NullPointerException success) {}
156 dl 1.11 }
157    
158     /**
159 dl 1.10 * isQueued reports whether a thread is queued.
160     */
161 jsr166 1.26 public void testIsQueued() throws InterruptedException {
162 jsr166 1.29 final Mutex sync = new Mutex();
163 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
164     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
165 jsr166 1.26 assertFalse(sync.isQueued(t1));
166     assertFalse(sync.isQueued(t2));
167     sync.acquire(1);
168     t1.start();
169     Thread.sleep(SHORT_DELAY_MS);
170     assertTrue(sync.isQueued(t1));
171     t2.start();
172     Thread.sleep(SHORT_DELAY_MS);
173     assertTrue(sync.isQueued(t1));
174     assertTrue(sync.isQueued(t2));
175     t1.interrupt();
176     Thread.sleep(SHORT_DELAY_MS);
177     assertFalse(sync.isQueued(t1));
178     assertTrue(sync.isQueued(t2));
179     sync.release(1);
180     Thread.sleep(SHORT_DELAY_MS);
181     assertFalse(sync.isQueued(t1));
182     Thread.sleep(SHORT_DELAY_MS);
183     assertFalse(sync.isQueued(t2));
184     t1.join();
185     t2.join();
186 jsr166 1.22 }
187 dl 1.8
188 dl 1.12 /**
189 dl 1.17 * getFirstQueuedThread returns first waiting thread or null if none
190 dl 1.12 */
191 jsr166 1.26 public void testGetFirstQueuedThread() throws InterruptedException {
192 jsr166 1.29 final Mutex sync = new Mutex();
193 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
194     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
195 jsr166 1.26 assertNull(sync.getFirstQueuedThread());
196     sync.acquire(1);
197     t1.start();
198     Thread.sleep(SHORT_DELAY_MS);
199     assertEquals(t1, sync.getFirstQueuedThread());
200     t2.start();
201     Thread.sleep(SHORT_DELAY_MS);
202     assertEquals(t1, sync.getFirstQueuedThread());
203     t1.interrupt();
204     Thread.sleep(SHORT_DELAY_MS);
205     Thread.sleep(SHORT_DELAY_MS);
206     assertEquals(t2, sync.getFirstQueuedThread());
207     sync.release(1);
208     Thread.sleep(SHORT_DELAY_MS);
209     assertNull(sync.getFirstQueuedThread());
210     t1.join();
211     t2.join();
212 jsr166 1.22 }
213 dl 1.12
214 dl 1.8
215     /**
216 dl 1.12 * hasContended reports false if no thread has ever blocked, else true
217 dl 1.8 */
218 jsr166 1.26 public void testHasContended() throws InterruptedException {
219 jsr166 1.29 final Mutex sync = new Mutex();
220 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
221     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
222 jsr166 1.26 assertFalse(sync.hasContended());
223     sync.acquire(1);
224     t1.start();
225     Thread.sleep(SHORT_DELAY_MS);
226     assertTrue(sync.hasContended());
227     t2.start();
228     Thread.sleep(SHORT_DELAY_MS);
229     assertTrue(sync.hasContended());
230     t1.interrupt();
231     Thread.sleep(SHORT_DELAY_MS);
232     assertTrue(sync.hasContended());
233     sync.release(1);
234     Thread.sleep(SHORT_DELAY_MS);
235     assertTrue(sync.hasContended());
236     t1.join();
237     t2.join();
238 jsr166 1.22 }
239 dl 1.17
240     /**
241     * getQueuedThreads includes waiting threads
242     */
243 jsr166 1.26 public void testGetQueuedThreads() throws InterruptedException {
244 jsr166 1.29 final Mutex sync = new Mutex();
245 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
246     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
247 jsr166 1.26 assertTrue(sync.getQueuedThreads().isEmpty());
248     sync.acquire(1);
249     assertTrue(sync.getQueuedThreads().isEmpty());
250     t1.start();
251     Thread.sleep(SHORT_DELAY_MS);
252     assertTrue(sync.getQueuedThreads().contains(t1));
253     t2.start();
254     Thread.sleep(SHORT_DELAY_MS);
255     assertTrue(sync.getQueuedThreads().contains(t1));
256     assertTrue(sync.getQueuedThreads().contains(t2));
257     t1.interrupt();
258     Thread.sleep(SHORT_DELAY_MS);
259     assertFalse(sync.getQueuedThreads().contains(t1));
260     assertTrue(sync.getQueuedThreads().contains(t2));
261     sync.release(1);
262     Thread.sleep(SHORT_DELAY_MS);
263     assertTrue(sync.getQueuedThreads().isEmpty());
264     t1.join();
265     t2.join();
266 jsr166 1.22 }
267 dl 1.17
268     /**
269     * getExclusiveQueuedThreads includes waiting threads
270     */
271 jsr166 1.26 public void testGetExclusiveQueuedThreads() throws InterruptedException {
272 jsr166 1.29 final Mutex sync = new Mutex();
273 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
274     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
275 jsr166 1.26 assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
276     sync.acquire(1);
277     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
278     t1.start();
279     Thread.sleep(SHORT_DELAY_MS);
280     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
281     t2.start();
282     Thread.sleep(SHORT_DELAY_MS);
283     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
284     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
285     t1.interrupt();
286     Thread.sleep(SHORT_DELAY_MS);
287     assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
288     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
289     sync.release(1);
290     Thread.sleep(SHORT_DELAY_MS);
291     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
292     t1.join();
293     t2.join();
294 jsr166 1.22 }
295 dl 1.17
296     /**
297     * getSharedQueuedThreads does not include exclusively waiting threads
298     */
299 jsr166 1.26 public void testGetSharedQueuedThreads() throws InterruptedException {
300 jsr166 1.29 final Mutex sync = new Mutex();
301 dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
302     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
303 jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
304     sync.acquire(1);
305     assertTrue(sync.getSharedQueuedThreads().isEmpty());
306     t1.start();
307     Thread.sleep(SHORT_DELAY_MS);
308     assertTrue(sync.getSharedQueuedThreads().isEmpty());
309     t2.start();
310     Thread.sleep(SHORT_DELAY_MS);
311     assertTrue(sync.getSharedQueuedThreads().isEmpty());
312     t1.interrupt();
313     Thread.sleep(SHORT_DELAY_MS);
314     assertTrue(sync.getSharedQueuedThreads().isEmpty());
315     sync.release(1);
316     Thread.sleep(SHORT_DELAY_MS);
317     assertTrue(sync.getSharedQueuedThreads().isEmpty());
318     t1.join();
319     t2.join();
320 jsr166 1.22 }
321 dl 1.1
322     /**
323 dl 1.16 * tryAcquireNanos is interruptible.
324 dl 1.1 */
325 jsr166 1.27 public void testInterruptedException2() throws InterruptedException {
326 jsr166 1.29 final Mutex sync = new Mutex();
327     sync.acquire(1);
328     Thread t = new Thread(new CheckedInterruptedRunnable() {
329 jsr166 1.28 public void realRun() throws InterruptedException {
330     sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
331     }});
332 jsr166 1.26
333     t.start();
334     t.interrupt();
335 jsr166 1.27 t.join();
336 dl 1.1 }
337    
338    
339     /**
340 dl 1.17 * TryAcquire on exclusively held sync fails
341 dl 1.1 */
342 jsr166 1.26 public void testTryAcquireWhenSynced() throws InterruptedException {
343 jsr166 1.29 final Mutex sync = new Mutex();
344     sync.acquire(1);
345     Thread t = new Thread(new CheckedRunnable() {
346 jsr166 1.28 public void realRun() {
347     threadAssertFalse(sync.tryAcquire(1));
348     }});
349 jsr166 1.26
350     t.start();
351     t.join();
352     sync.release(1);
353 jsr166 1.22 }
354 dl 1.1
355     /**
356 dl 1.17 * tryAcquireNanos on an exclusively held sync times out
357 dl 1.1 */
358 jsr166 1.26 public void testAcquireNanos_Timeout() throws InterruptedException {
359 jsr166 1.29 final Mutex sync = new Mutex();
360     sync.acquire(1);
361     Thread t = new Thread(new CheckedRunnable() {
362 jsr166 1.28 public void realRun() throws InterruptedException {
363     threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
364     }});
365 jsr166 1.26
366     t.start();
367     t.join();
368     sync.release(1);
369 jsr166 1.22 }
370    
371    
372 dl 1.1 /**
373 dl 1.13 * getState is true when acquired and false when not
374 dl 1.1 */
375 jsr166 1.26 public void testGetState() throws InterruptedException {
376 jsr166 1.29 final Mutex sync = new Mutex();
377     sync.acquire(1);
378     assertTrue(sync.isHeldExclusively());
379     sync.release(1);
380     assertFalse(sync.isHeldExclusively());
381     Thread t = new Thread(new CheckedRunnable() {
382 jsr166 1.28 public void realRun() throws InterruptedException {
383     sync.acquire(1);
384     Thread.sleep(SMALL_DELAY_MS);
385     sync.release(1);
386     }});
387 jsr166 1.26
388     t.start();
389     Thread.sleep(SHORT_DELAY_MS);
390     assertTrue(sync.isHeldExclusively());
391     t.join();
392     assertFalse(sync.isHeldExclusively());
393 dl 1.1 }
394    
395    
396     /**
397 dl 1.16 * acquireInterruptibly is interruptible.
398 dl 1.1 */
399 jsr166 1.26 public void testAcquireInterruptibly1() throws InterruptedException {
400 jsr166 1.29 final Mutex sync = new Mutex();
401     sync.acquire(1);
402     Thread t = new Thread(new InterruptedSyncRunnable(sync));
403 jsr166 1.26
404     t.start();
405     Thread.sleep(SHORT_DELAY_MS);
406     t.interrupt();
407     Thread.sleep(SHORT_DELAY_MS);
408     sync.release(1);
409     t.join();
410 jsr166 1.22 }
411 dl 1.1
412     /**
413 dl 1.16 * acquireInterruptibly succeeds when released, else is interruptible
414 dl 1.1 */
415 jsr166 1.26 public void testAcquireInterruptibly2() throws InterruptedException {
416 jsr166 1.29 final Mutex sync = new Mutex();
417 jsr166 1.26 sync.acquireInterruptibly(1);
418 jsr166 1.29 Thread t = new Thread(new InterruptedSyncRunnable(sync));
419 jsr166 1.26 t.start();
420     t.interrupt();
421     assertTrue(sync.isHeldExclusively());
422     t.join();
423 dl 1.1 }
424    
425     /**
426 dl 1.17 * owns is true for a condition created by sync else false
427 dl 1.12 */
428     public void testOwns() {
429 jsr166 1.29 final Mutex sync = new Mutex();
430 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
431     final Mutex sync2 = new Mutex();
432     assertTrue(sync.owns(c));
433     assertFalse(sync2.owns(c));
434 dl 1.12 }
435    
436     /**
437 dl 1.17 * Calling await without holding sync throws IllegalMonitorStateException
438 dl 1.1 */
439 jsr166 1.26 public void testAwait_IllegalMonitor() throws InterruptedException {
440 jsr166 1.29 final Mutex sync = new Mutex();
441 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
442 dl 1.1 try {
443     c.await();
444     shouldThrow();
445 jsr166 1.28 } catch (IllegalMonitorStateException success) {}
446 dl 1.1 }
447    
448     /**
449 dl 1.17 * Calling signal without holding sync throws IllegalMonitorStateException
450 dl 1.1 */
451     public void testSignal_IllegalMonitor() {
452 jsr166 1.29 final Mutex sync = new Mutex();
453 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
454 dl 1.1 try {
455     c.signal();
456     shouldThrow();
457 jsr166 1.28 } catch (IllegalMonitorStateException success) {}
458 dl 1.1 }
459    
460     /**
461     * awaitNanos without a signal times out
462     */
463 jsr166 1.26 public void testAwaitNanos_Timeout() throws InterruptedException {
464 jsr166 1.29 final Mutex sync = new Mutex();
465 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
466 jsr166 1.26 sync.acquire(1);
467     long t = c.awaitNanos(100);
468     assertTrue(t <= 0);
469     sync.release(1);
470 dl 1.1 }
471    
472     /**
473 dl 1.17 * Timed await without a signal times out
474 dl 1.1 */
475 jsr166 1.26 public void testAwait_Timeout() throws InterruptedException {
476 jsr166 1.29 final Mutex sync = new Mutex();
477 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
478 jsr166 1.26 sync.acquire(1);
479 jsr166 1.30 assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
480 jsr166 1.26 sync.release(1);
481 dl 1.1 }
482    
483     /**
484     * awaitUntil without a signal times out
485     */
486 jsr166 1.26 public void testAwaitUntil_Timeout() throws InterruptedException {
487 jsr166 1.29 final Mutex sync = new Mutex();
488 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
489 jsr166 1.26 sync.acquire(1);
490     java.util.Date d = new java.util.Date();
491     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
492     sync.release(1);
493 dl 1.1 }
494    
495     /**
496     * await returns when signalled
497     */
498 jsr166 1.26 public void testAwait() throws InterruptedException {
499 jsr166 1.29 final Mutex sync = new Mutex();
500 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
501 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
502 jsr166 1.28 public void realRun() throws InterruptedException {
503     sync.acquire(1);
504     c.await();
505     sync.release(1);
506     }});
507 dl 1.15
508 jsr166 1.26 t.start();
509     Thread.sleep(SHORT_DELAY_MS);
510     sync.acquire(1);
511     c.signal();
512     sync.release(1);
513     t.join(SHORT_DELAY_MS);
514     assertFalse(t.isAlive());
515 dl 1.15 }
516    
517    
518    
519     /**
520     * hasWaiters throws NPE if null
521     */
522     public void testHasWaitersNPE() {
523 jsr166 1.29 final Mutex sync = new Mutex();
524 dl 1.15 try {
525 dl 1.17 sync.hasWaiters(null);
526 dl 1.15 shouldThrow();
527 jsr166 1.26 } catch (NullPointerException success) {}
528 dl 1.15 }
529    
530     /**
531     * getWaitQueueLength throws NPE if null
532     */
533     public void testGetWaitQueueLengthNPE() {
534 jsr166 1.29 final Mutex sync = new Mutex();
535 dl 1.15 try {
536 dl 1.17 sync.getWaitQueueLength(null);
537 dl 1.15 shouldThrow();
538 jsr166 1.26 } catch (NullPointerException success) {}
539 dl 1.15 }
540    
541    
542     /**
543     * getWaitingThreads throws NPE if null
544     */
545     public void testGetWaitingThreadsNPE() {
546 jsr166 1.29 final Mutex sync = new Mutex();
547 dl 1.15 try {
548 dl 1.17 sync.getWaitingThreads(null);
549 dl 1.15 shouldThrow();
550 jsr166 1.26 } catch (NullPointerException success) {}
551 dl 1.15 }
552    
553    
554     /**
555     * hasWaiters throws IAE if not owned
556     */
557     public void testHasWaitersIAE() {
558 jsr166 1.29 final Mutex sync = new Mutex();
559 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
560 jsr166 1.29 final Mutex sync2 = new Mutex();
561 dl 1.15 try {
562 dl 1.17 sync2.hasWaiters(c);
563 dl 1.15 shouldThrow();
564 jsr166 1.26 } catch (IllegalArgumentException success) {}
565 dl 1.15 }
566    
567     /**
568 dl 1.17 * hasWaiters throws IMSE if not synced
569 dl 1.15 */
570     public void testHasWaitersIMSE() {
571 jsr166 1.29 final Mutex sync = new Mutex();
572 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
573 dl 1.15 try {
574 dl 1.17 sync.hasWaiters(c);
575 dl 1.15 shouldThrow();
576 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
577 dl 1.15 }
578    
579    
580     /**
581     * getWaitQueueLength throws IAE if not owned
582     */
583     public void testGetWaitQueueLengthIAE() {
584 jsr166 1.29 final Mutex sync = new Mutex();
585 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
586 jsr166 1.29 final Mutex sync2 = new Mutex();
587 dl 1.15 try {
588 dl 1.17 sync2.getWaitQueueLength(c);
589 dl 1.15 shouldThrow();
590 jsr166 1.26 } catch (IllegalArgumentException success) {}
591 dl 1.15 }
592    
593     /**
594 dl 1.17 * getWaitQueueLength throws IMSE if not synced
595 dl 1.15 */
596     public void testGetWaitQueueLengthIMSE() {
597 jsr166 1.29 final Mutex sync = new Mutex();
598 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
599 dl 1.15 try {
600 dl 1.17 sync.getWaitQueueLength(c);
601 dl 1.15 shouldThrow();
602 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
603 dl 1.15 }
604    
605    
606     /**
607     * getWaitingThreads throws IAE if not owned
608     */
609     public void testGetWaitingThreadsIAE() {
610 jsr166 1.29 final Mutex sync = new Mutex();
611 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
612 jsr166 1.29 final Mutex sync2 = new Mutex();
613 dl 1.15 try {
614 dl 1.17 sync2.getWaitingThreads(c);
615 dl 1.15 shouldThrow();
616 jsr166 1.26 } catch (IllegalArgumentException success) {}
617 dl 1.15 }
618    
619     /**
620 dl 1.17 * getWaitingThreads throws IMSE if not synced
621 dl 1.15 */
622     public void testGetWaitingThreadsIMSE() {
623 jsr166 1.29 final Mutex sync = new Mutex();
624 jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
625 dl 1.15 try {
626 dl 1.17 sync.getWaitingThreads(c);
627 dl 1.15 shouldThrow();
628 jsr166 1.26 } catch (IllegalMonitorStateException success) {}
629 dl 1.15 }
630    
631    
632    
633     /**
634     * hasWaiters returns true when a thread is waiting, else false
635     */
636 jsr166 1.26 public void testHasWaiters() throws InterruptedException {
637 jsr166 1.29 final Mutex sync = new Mutex();
638 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
639 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
640 jsr166 1.28 public void realRun() throws InterruptedException {
641     sync.acquire(1);
642     threadAssertFalse(sync.hasWaiters(c));
643     threadAssertEquals(0, sync.getWaitQueueLength(c));
644     c.await();
645     sync.release(1);
646     }});
647 dl 1.15
648 jsr166 1.26 t.start();
649     Thread.sleep(SHORT_DELAY_MS);
650     sync.acquire(1);
651     assertTrue(sync.hasWaiters(c));
652     assertEquals(1, sync.getWaitQueueLength(c));
653     c.signal();
654     sync.release(1);
655     Thread.sleep(SHORT_DELAY_MS);
656     sync.acquire(1);
657     assertFalse(sync.hasWaiters(c));
658     assertEquals(0, sync.getWaitQueueLength(c));
659     sync.release(1);
660     t.join(SHORT_DELAY_MS);
661     assertFalse(t.isAlive());
662 dl 1.15 }
663    
664     /**
665     * getWaitQueueLength returns number of waiting threads
666     */
667 jsr166 1.26 public void testGetWaitQueueLength() throws InterruptedException {
668 jsr166 1.29 final Mutex sync = new Mutex();
669 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
670 jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
671 jsr166 1.28 public void realRun() throws InterruptedException {
672     sync.acquire(1);
673     threadAssertFalse(sync.hasWaiters(c));
674     threadAssertEquals(0, sync.getWaitQueueLength(c));
675     c.await();
676     sync.release(1);
677     }});
678    
679 jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
680 jsr166 1.28 public void realRun() throws InterruptedException {
681     sync.acquire(1);
682     threadAssertTrue(sync.hasWaiters(c));
683     threadAssertEquals(1, sync.getWaitQueueLength(c));
684     c.await();
685     sync.release(1);
686     }});
687 dl 1.15
688 jsr166 1.26 t1.start();
689     Thread.sleep(SHORT_DELAY_MS);
690     t2.start();
691     Thread.sleep(SHORT_DELAY_MS);
692     sync.acquire(1);
693     assertTrue(sync.hasWaiters(c));
694     assertEquals(2, sync.getWaitQueueLength(c));
695     c.signalAll();
696     sync.release(1);
697     Thread.sleep(SHORT_DELAY_MS);
698     sync.acquire(1);
699     assertFalse(sync.hasWaiters(c));
700     assertEquals(0, sync.getWaitQueueLength(c));
701     sync.release(1);
702     t1.join(SHORT_DELAY_MS);
703     t2.join(SHORT_DELAY_MS);
704     assertFalse(t1.isAlive());
705     assertFalse(t2.isAlive());
706 dl 1.15 }
707    
708     /**
709     * getWaitingThreads returns only and all waiting threads
710     */
711 jsr166 1.26 public void testGetWaitingThreads() throws InterruptedException {
712 jsr166 1.29 final Mutex sync = new Mutex();
713 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
714 jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
715 jsr166 1.28 public void realRun() throws InterruptedException {
716     sync.acquire(1);
717     threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
718     c.await();
719     sync.release(1);
720     }});
721    
722 jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
723 jsr166 1.28 public void realRun() throws InterruptedException {
724     sync.acquire(1);
725     threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
726     c.await();
727     sync.release(1);
728     }});
729 dl 1.1
730 jsr166 1.26 sync.acquire(1);
731     assertTrue(sync.getWaitingThreads(c).isEmpty());
732     sync.release(1);
733     t1.start();
734     Thread.sleep(SHORT_DELAY_MS);
735     t2.start();
736     Thread.sleep(SHORT_DELAY_MS);
737     sync.acquire(1);
738     assertTrue(sync.hasWaiters(c));
739     assertTrue(sync.getWaitingThreads(c).contains(t1));
740     assertTrue(sync.getWaitingThreads(c).contains(t2));
741     c.signalAll();
742     sync.release(1);
743     Thread.sleep(SHORT_DELAY_MS);
744     sync.acquire(1);
745     assertFalse(sync.hasWaiters(c));
746     assertTrue(sync.getWaitingThreads(c).isEmpty());
747     sync.release(1);
748     t1.join(SHORT_DELAY_MS);
749     t2.join(SHORT_DELAY_MS);
750     assertFalse(t1.isAlive());
751     assertFalse(t2.isAlive());
752 dl 1.15 }
753    
754    
755    
756     /**
757     * awaitUninterruptibly doesn't abort on interrupt
758     */
759 jsr166 1.26 public void testAwaitUninterruptibly() throws InterruptedException {
760 jsr166 1.29 final Mutex sync = new Mutex();
761 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
762 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
763 jsr166 1.28 public void realRun() {
764     sync.acquire(1);
765     c.awaitUninterruptibly();
766     sync.release(1);
767     }});
768 dl 1.15
769 jsr166 1.26 t.start();
770     Thread.sleep(SHORT_DELAY_MS);
771     t.interrupt();
772     sync.acquire(1);
773     c.signal();
774     sync.release(1);
775     t.join(SHORT_DELAY_MS);
776     assertFalse(t.isAlive());
777 dl 1.15 }
778    
779     /**
780     * await is interruptible
781     */
782 jsr166 1.26 public void testAwait_Interrupt() throws InterruptedException {
783 jsr166 1.29 final Mutex sync = new Mutex();
784 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
785 jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
786     public void realRun() throws InterruptedException {
787 jsr166 1.28 sync.acquire(1);
788     c.await();
789     }});
790 dl 1.15
791 jsr166 1.26 t.start();
792     Thread.sleep(SHORT_DELAY_MS);
793     t.interrupt();
794     t.join(SHORT_DELAY_MS);
795     assertFalse(t.isAlive());
796 dl 1.15 }
797    
798     /**
799     * awaitNanos is interruptible
800     */
801 jsr166 1.26 public void testAwaitNanos_Interrupt() throws InterruptedException {
802 jsr166 1.29 final Mutex sync = new Mutex();
803 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
804 jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
805     public void realRun() throws InterruptedException {
806 jsr166 1.28 sync.acquire(1);
807     c.awaitNanos(1000 * 1000 * 1000); // 1 sec
808     }});
809 dl 1.15
810 jsr166 1.26 t.start();
811     Thread.sleep(SHORT_DELAY_MS);
812     t.interrupt();
813     t.join(SHORT_DELAY_MS);
814     assertFalse(t.isAlive());
815 dl 1.15 }
816    
817     /**
818     * awaitUntil is interruptible
819     */
820 jsr166 1.26 public void testAwaitUntil_Interrupt() throws InterruptedException {
821 jsr166 1.29 final Mutex sync = new Mutex();
822 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
823 jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
824     public void realRun() throws InterruptedException {
825 jsr166 1.28 sync.acquire(1);
826     java.util.Date d = new java.util.Date();
827     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
828     }});
829 dl 1.15
830 jsr166 1.26 t.start();
831     Thread.sleep(SHORT_DELAY_MS);
832     t.interrupt();
833     t.join(SHORT_DELAY_MS);
834     assertFalse(t.isAlive());
835 dl 1.12 }
836    
837 dl 1.14 /**
838 dl 1.15 * signalAll wakes up all threads
839     */
840 jsr166 1.26 public void testSignalAll() throws InterruptedException {
841 jsr166 1.29 final Mutex sync = new Mutex();
842 dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
843 jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
844 jsr166 1.28 public void realRun() throws InterruptedException {
845     sync.acquire(1);
846     c.await();
847     sync.release(1);
848     }});
849    
850 jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
851 jsr166 1.28 public void realRun() throws InterruptedException {
852     sync.acquire(1);
853     c.await();
854     sync.release(1);
855     }});
856 dl 1.15
857 jsr166 1.26 t1.start();
858     t2.start();
859     Thread.sleep(SHORT_DELAY_MS);
860     sync.acquire(1);
861     c.signalAll();
862     sync.release(1);
863     t1.join(SHORT_DELAY_MS);
864     t2.join(SHORT_DELAY_MS);
865     assertFalse(t1.isAlive());
866     assertFalse(t2.isAlive());
867 dl 1.15 }
868    
869    
870     /**
871 dl 1.14 * toString indicates current state
872     */
873     public void testToString() {
874 dl 1.17 Mutex sync = new Mutex();
875     String us = sync.toString();
876 dl 1.14 assertTrue(us.indexOf("State = 0") >= 0);
877 dl 1.17 sync.acquire(1);
878     String ls = sync.toString();
879 dl 1.14 assertTrue(ls.indexOf("State = 1") >= 0);
880     }
881    
882     /**
883     * A serialized AQS deserializes with current state
884     */
885 jsr166 1.26 public void testSerialization() throws Exception {
886 dl 1.14 Mutex l = new Mutex();
887 dl 1.16 l.acquire(1);
888     assertTrue(l.isHeldExclusively());
889 dl 1.14
890 jsr166 1.26 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
891     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
892     out.writeObject(l);
893     out.close();
894    
895     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
896     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
897     Mutex r = (Mutex) in.readObject();
898     assertTrue(r.isHeldExclusively());
899 dl 1.14 }
900    
901 dl 1.12
902     /**
903 dl 1.15 * tryReleaseShared setting state changes getState
904 dl 1.12 */
905 dl 1.15 public void testGetStateWithReleaseShared() {
906 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
907     assertFalse(l.isSignalled());
908     l.releaseShared(0);
909     assertTrue(l.isSignalled());
910 dl 1.12 }
911    
912     /**
913 dl 1.17 * releaseShared has no effect when already signalled
914 dl 1.12 */
915 dl 1.13 public void testReleaseShared() {
916 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
917     assertFalse(l.isSignalled());
918     l.releaseShared(0);
919     assertTrue(l.isSignalled());
920     l.releaseShared(0);
921     assertTrue(l.isSignalled());
922 dl 1.12 }
923    
924     /**
925     * acquireSharedInterruptibly returns after release, but not before
926     */
927 jsr166 1.26 public void testAcquireSharedInterruptibly() throws InterruptedException {
928 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
929 dl 1.12
930 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
931 jsr166 1.28 public void realRun() throws InterruptedException {
932     threadAssertFalse(l.isSignalled());
933     l.acquireSharedInterruptibly(0);
934     threadAssertTrue(l.isSignalled());
935     }});
936 jsr166 1.26
937     t.start();
938     assertFalse(l.isSignalled());
939     Thread.sleep(SHORT_DELAY_MS);
940     l.releaseShared(0);
941     assertTrue(l.isSignalled());
942     t.join();
943 dl 1.12 }
944 jsr166 1.22
945 dl 1.12
946     /**
947     * acquireSharedTimed returns after release
948     */
949 jsr166 1.26 public void testAsquireSharedTimed() throws InterruptedException {
950 jsr166 1.29 final BooleanLatch l = new BooleanLatch();
951 dl 1.12
952 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
953 jsr166 1.28 public void realRun() throws InterruptedException {
954     threadAssertFalse(l.isSignalled());
955     threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
956     threadAssertTrue(l.isSignalled());
957     }});
958 jsr166 1.26
959     t.start();
960     assertFalse(l.isSignalled());
961     Thread.sleep(SHORT_DELAY_MS);
962     l.releaseShared(0);
963     assertTrue(l.isSignalled());
964     t.join();
965 dl 1.12 }
966 jsr166 1.22
967 dl 1.12 /**
968     * acquireSharedInterruptibly throws IE if interrupted before released
969     */
970 jsr166 1.26 public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
971 dl 1.12 final BooleanLatch l = new BooleanLatch();
972 jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
973     public void realRun() throws InterruptedException {
974 jsr166 1.28 threadAssertFalse(l.isSignalled());
975     l.acquireSharedInterruptibly(0);
976     }});
977    
978 jsr166 1.29 t.start();
979 jsr166 1.26 assertFalse(l.isSignalled());
980     t.interrupt();
981     t.join();
982 dl 1.12 }
983    
984     /**
985     * acquireSharedTimed throws IE if interrupted before released
986     */
987 jsr166 1.26 public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
988 dl 1.12 final BooleanLatch l = new BooleanLatch();
989 jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
990     public void realRun() throws InterruptedException {
991 jsr166 1.28 threadAssertFalse(l.isSignalled());
992     l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
993     }});
994    
995 dl 1.12 t.start();
996 jsr166 1.26 Thread.sleep(SHORT_DELAY_MS);
997     assertFalse(l.isSignalled());
998     t.interrupt();
999     t.join();
1000 dl 1.12 }
1001    
1002     /**
1003     * acquireSharedTimed times out if not released before timeout
1004     */
1005 jsr166 1.26 public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1006 dl 1.12 final BooleanLatch l = new BooleanLatch();
1007 jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
1008 jsr166 1.28 public void realRun() throws InterruptedException {
1009     threadAssertFalse(l.isSignalled());
1010     threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1011     }});
1012    
1013 dl 1.12 t.start();
1014 jsr166 1.26 Thread.sleep(SHORT_DELAY_MS);
1015     assertFalse(l.isSignalled());
1016     t.join();
1017 dl 1.1 }
1018 dl 1.14
1019 dl 1.1 }