ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.16
Committed: Sun Jan 11 16:02:33 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.15: +119 -114 lines
Log Message:
Simplify/shorten AQS method names

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