ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.1
Committed: Tue Mar 1 01:32:00 2005 UTC (19 years, 2 months ago) by dl
Branch: MAIN
Log Message:
Add tests for new AQLS class

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