ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.19
Committed: Tue May 3 16:02:00 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.18: +0 -1 lines
Log Message:
Fix some asserts and awaitUninterruptibly tests

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