ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.2
Committed: Tue May 3 16:02:00 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.1: +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 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     t.join(SHORT_DELAY_MS);
957     assertFalse(t.isAlive());
958     }
959     catch (Exception ex) {
960     unexpectedException();
961     }
962     }
963    
964     /**
965     * await is interruptible
966     */
967     public void testAwait_Interrupt() {
968     final Mutex sync = new Mutex();
969     final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
970     Thread t = new Thread(new Runnable() {
971     public void run() {
972     try {
973     sync.acquire(1);
974     c.await();
975     sync.release(1);
976     threadShouldThrow();
977     }
978     catch(InterruptedException success) {
979     }
980     }
981     });
982    
983     try {
984     t.start();
985     Thread.sleep(SHORT_DELAY_MS);
986     t.interrupt();
987     t.join(SHORT_DELAY_MS);
988     assertFalse(t.isAlive());
989     }
990     catch (Exception ex) {
991     unexpectedException();
992     }
993     }
994    
995     /**
996     * awaitNanos is interruptible
997     */
998     public void testAwaitNanos_Interrupt() {
999     final Mutex sync = new Mutex();
1000     final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1001     Thread t = new Thread(new Runnable() {
1002     public void run() {
1003     try {
1004     sync.acquire(1);
1005     c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1006     sync.release(1);
1007     threadShouldThrow();
1008     }
1009     catch(InterruptedException success) {
1010     }
1011     }
1012     });
1013    
1014     try {
1015     t.start();
1016     Thread.sleep(SHORT_DELAY_MS);
1017     t.interrupt();
1018     t.join(SHORT_DELAY_MS);
1019     assertFalse(t.isAlive());
1020     }
1021     catch (Exception ex) {
1022     unexpectedException();
1023     }
1024     }
1025    
1026     /**
1027     * awaitUntil is interruptible
1028     */
1029     public void testAwaitUntil_Interrupt() {
1030     final Mutex sync = new Mutex();
1031     final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1032     Thread t = new Thread(new Runnable() {
1033     public void run() {
1034     try {
1035     sync.acquire(1);
1036     java.util.Date d = new java.util.Date();
1037     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1038     sync.release(1);
1039     threadShouldThrow();
1040     }
1041     catch(InterruptedException success) {
1042     }
1043     }
1044     });
1045    
1046     try {
1047     t.start();
1048     Thread.sleep(SHORT_DELAY_MS);
1049     t.interrupt();
1050     t.join(SHORT_DELAY_MS);
1051     assertFalse(t.isAlive());
1052     }
1053     catch (Exception ex) {
1054     unexpectedException();
1055     }
1056     }
1057    
1058     /**
1059     * signalAll wakes up all threads
1060     */
1061     public void testSignalAll() {
1062     final Mutex sync = new Mutex();
1063     final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1064     Thread t1 = new Thread(new Runnable() {
1065     public void run() {
1066     try {
1067     sync.acquire(1);
1068     c.await();
1069     sync.release(1);
1070     }
1071     catch(InterruptedException e) {
1072     threadUnexpectedException();
1073     }
1074     }
1075     });
1076    
1077     Thread t2 = new Thread(new Runnable() {
1078     public void run() {
1079     try {
1080     sync.acquire(1);
1081     c.await();
1082     sync.release(1);
1083     }
1084     catch(InterruptedException e) {
1085     threadUnexpectedException();
1086     }
1087     }
1088     });
1089    
1090     try {
1091     t1.start();
1092     t2.start();
1093     Thread.sleep(SHORT_DELAY_MS);
1094     sync.acquire(1);
1095     c.signalAll();
1096     sync.release(1);
1097     t1.join(SHORT_DELAY_MS);
1098     t2.join(SHORT_DELAY_MS);
1099     assertFalse(t1.isAlive());
1100     assertFalse(t2.isAlive());
1101     }
1102     catch (Exception ex) {
1103     unexpectedException();
1104     }
1105     }
1106    
1107    
1108     /**
1109     * toString indicates current state
1110     */
1111     public void testToString() {
1112     Mutex sync = new Mutex();
1113     String us = sync.toString();
1114     assertTrue(us.indexOf("State = 0") >= 0);
1115     sync.acquire(1);
1116     String ls = sync.toString();
1117     assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0);
1118     }
1119    
1120     /**
1121     * A serialized AQS deserializes with current state
1122     */
1123     public void testSerialization() {
1124     Mutex l = new Mutex();
1125     l.acquire(1);
1126     assertTrue(l.isHeldExclusively());
1127    
1128     try {
1129     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1130     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1131     out.writeObject(l);
1132     out.close();
1133    
1134     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1135     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1136     Mutex r = (Mutex) in.readObject();
1137     assertTrue(r.isHeldExclusively());
1138     } catch(Exception e){
1139     e.printStackTrace();
1140     unexpectedException();
1141     }
1142     }
1143    
1144    
1145     /**
1146     * tryReleaseShared setting state changes getState
1147     */
1148     public void testGetStateWithReleaseShared() {
1149     final BooleanLatch l = new BooleanLatch();
1150     assertFalse(l.isSignalled());
1151     l.releaseShared(0);
1152     assertTrue(l.isSignalled());
1153     }
1154    
1155     /**
1156     * releaseShared has no effect when already signalled
1157     */
1158     public void testReleaseShared() {
1159     final BooleanLatch l = new BooleanLatch();
1160     assertFalse(l.isSignalled());
1161     l.releaseShared(0);
1162     assertTrue(l.isSignalled());
1163     l.releaseShared(0);
1164     assertTrue(l.isSignalled());
1165     }
1166    
1167     /**
1168     * acquireSharedInterruptibly returns after release, but not before
1169     */
1170     public void testAcquireSharedInterruptibly() {
1171     final BooleanLatch l = new BooleanLatch();
1172    
1173     Thread t = new Thread(new Runnable() {
1174     public void run() {
1175     try {
1176     threadAssertFalse(l.isSignalled());
1177     l.acquireSharedInterruptibly(0);
1178     threadAssertTrue(l.isSignalled());
1179     } catch(InterruptedException e){
1180     threadUnexpectedException();
1181     }
1182     }
1183     });
1184     try {
1185     t.start();
1186     assertFalse(l.isSignalled());
1187     Thread.sleep(SHORT_DELAY_MS);
1188     l.releaseShared(0);
1189     assertTrue(l.isSignalled());
1190     t.join();
1191     } catch (InterruptedException e){
1192     unexpectedException();
1193     }
1194     }
1195    
1196    
1197     /**
1198     * acquireSharedTimed returns after release
1199     */
1200     public void testAsquireSharedTimed() {
1201     final BooleanLatch l = new BooleanLatch();
1202    
1203     Thread t = new Thread(new Runnable() {
1204     public void run() {
1205     try {
1206     threadAssertFalse(l.isSignalled());
1207     threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
1208     threadAssertTrue(l.isSignalled());
1209    
1210     } catch(InterruptedException e){
1211     threadUnexpectedException();
1212     }
1213     }
1214     });
1215     try {
1216     t.start();
1217     assertFalse(l.isSignalled());
1218     Thread.sleep(SHORT_DELAY_MS);
1219     l.releaseShared(0);
1220     assertTrue(l.isSignalled());
1221     t.join();
1222     } catch (InterruptedException e){
1223     unexpectedException();
1224     }
1225     }
1226    
1227     /**
1228     * acquireSharedInterruptibly throws IE if interrupted before released
1229     */
1230     public void testAcquireSharedInterruptibly_InterruptedException() {
1231     final BooleanLatch l = new BooleanLatch();
1232     Thread t = new Thread(new Runnable() {
1233     public void run() {
1234     try {
1235     threadAssertFalse(l.isSignalled());
1236     l.acquireSharedInterruptibly(0);
1237     threadShouldThrow();
1238     } catch(InterruptedException success){}
1239     }
1240     });
1241     t.start();
1242     try {
1243     assertFalse(l.isSignalled());
1244     t.interrupt();
1245     t.join();
1246     } catch (InterruptedException e){
1247     unexpectedException();
1248     }
1249     }
1250    
1251     /**
1252     * acquireSharedTimed throws IE if interrupted before released
1253     */
1254     public void testAcquireSharedNanos_InterruptedException() {
1255     final BooleanLatch l = new BooleanLatch();
1256     Thread t = new Thread(new Runnable() {
1257     public void run() {
1258     try {
1259     threadAssertFalse(l.isSignalled());
1260     l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
1261     threadShouldThrow();
1262     } catch(InterruptedException success){}
1263     }
1264     });
1265     t.start();
1266     try {
1267     Thread.sleep(SHORT_DELAY_MS);
1268     assertFalse(l.isSignalled());
1269     t.interrupt();
1270     t.join();
1271     } catch (InterruptedException e){
1272     unexpectedException();
1273     }
1274     }
1275    
1276     /**
1277     * acquireSharedTimed times out if not released before timeout
1278     */
1279     public void testAcquireSharedNanos_Timeout() {
1280     final BooleanLatch l = new BooleanLatch();
1281     Thread t = new Thread(new Runnable() {
1282     public void run() {
1283     try {
1284     threadAssertFalse(l.isSignalled());
1285     threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1286     } catch(InterruptedException ie){
1287     threadUnexpectedException();
1288     }
1289     }
1290     });
1291     t.start();
1292     try {
1293     Thread.sleep(SHORT_DELAY_MS);
1294     assertFalse(l.isSignalled());
1295     t.join();
1296     } catch (InterruptedException e){
1297     unexpectedException();
1298     }
1299     }
1300    
1301    
1302     }