ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.27
Committed: Thu May 18 10:29:23 2006 UTC (18 years ago) by dl
Branch: MAIN
Changes since 1.26: +3 -0 lines
Log Message:
Add sleeps after interrupts to force ordering

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.12 * 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 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.concurrent.locks.*;
11     import java.util.concurrent.*;
12 dl 1.2 import java.io.*;
13 dl 1.6 import java.util.*;
14 dl 1.1
15 dl 1.3 public class ReentrantReadWriteLockTest extends JSR166TestCase {
16 dl 1.1 public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19     public static Test suite() {
20     return new TestSuite(ReentrantReadWriteLockTest.class);
21     }
22    
23 dl 1.6 /**
24     * A runnable calling lockInterruptibly
25     */
26     class InterruptibleLockRunnable implements Runnable {
27     final ReentrantReadWriteLock lock;
28     InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
29     public void run() {
30     try {
31     lock.writeLock().lockInterruptibly();
32     } catch(InterruptedException success){}
33     }
34     }
35    
36    
37     /**
38     * A runnable calling lockInterruptibly that expects to be
39     * interrupted
40     */
41     class InterruptedLockRunnable implements Runnable {
42     final ReentrantReadWriteLock lock;
43     InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
44     public void run() {
45     try {
46     lock.writeLock().lockInterruptibly();
47     threadShouldThrow();
48     } catch(InterruptedException success){}
49     }
50     }
51    
52     /**
53     * Subclass to expose protected methods
54     */
55     static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
56     PublicReentrantReadWriteLock() { super(); }
57     public Collection<Thread> getQueuedThreads() {
58     return super.getQueuedThreads();
59     }
60 dl 1.13 public Collection<Thread> getWaitingThreads(Condition c) {
61     return super.getWaitingThreads(c);
62     }
63 dl 1.6 }
64    
65     /**
66     * Constructor sets given fairness, and is in unlocked state
67     */
68     public void testConstructor() {
69     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
70     assertFalse(rl.isFair());
71     assertFalse(rl.isWriteLocked());
72 dl 1.9 assertEquals(0, rl.getReadLockCount());
73 dl 1.6 ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
74     assertTrue(r2.isFair());
75     assertFalse(r2.isWriteLocked());
76 dl 1.9 assertEquals(0, r2.getReadLockCount());
77 dl 1.6 }
78 dl 1.1
79 dl 1.5 /**
80 dl 1.4 * write-locking and read-locking an unlocked lock succeed
81 dl 1.1 */
82 dl 1.4 public void testLock() {
83 dl 1.1 ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
84 dl 1.4 rl.writeLock().lock();
85 dl 1.6 assertTrue(rl.isWriteLocked());
86     assertTrue(rl.isWriteLockedByCurrentThread());
87 dl 1.25 assertTrue(rl.writeLock().isHeldByCurrentThread());
88 dl 1.9 assertEquals(0, rl.getReadLockCount());
89 dl 1.4 rl.writeLock().unlock();
90 dl 1.6 assertFalse(rl.isWriteLocked());
91     assertFalse(rl.isWriteLockedByCurrentThread());
92 dl 1.25 assertFalse(rl.writeLock().isHeldByCurrentThread());
93 dl 1.9 assertEquals(0, rl.getReadLockCount());
94 dl 1.4 rl.readLock().lock();
95 dl 1.6 assertFalse(rl.isWriteLocked());
96     assertFalse(rl.isWriteLockedByCurrentThread());
97 dl 1.9 assertEquals(1, rl.getReadLockCount());
98 dl 1.4 rl.readLock().unlock();
99 dl 1.6 assertFalse(rl.isWriteLocked());
100     assertFalse(rl.isWriteLockedByCurrentThread());
101 dl 1.9 assertEquals(0, rl.getReadLockCount());
102 dl 1.4 }
103    
104 dl 1.1
105 dl 1.5 /**
106 dl 1.4 * locking an unlocked fair lock succeeds
107     */
108     public void testFairLock() {
109     ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
110     rl.writeLock().lock();
111 dl 1.6 assertTrue(rl.isWriteLocked());
112     assertTrue(rl.isWriteLockedByCurrentThread());
113 dl 1.25 assertTrue(rl.writeLock().isHeldByCurrentThread());
114 dl 1.9 assertEquals(0, rl.getReadLockCount());
115 dl 1.4 rl.writeLock().unlock();
116 dl 1.6 assertFalse(rl.isWriteLocked());
117     assertFalse(rl.isWriteLockedByCurrentThread());
118 dl 1.25 assertFalse(rl.writeLock().isHeldByCurrentThread());
119 dl 1.9 assertEquals(0, rl.getReadLockCount());
120 dl 1.4 rl.readLock().lock();
121 dl 1.6 assertFalse(rl.isWriteLocked());
122     assertFalse(rl.isWriteLockedByCurrentThread());
123 dl 1.9 assertEquals(1, rl.getReadLockCount());
124 dl 1.4 rl.readLock().unlock();
125 dl 1.6 assertFalse(rl.isWriteLocked());
126     assertFalse(rl.isWriteLockedByCurrentThread());
127 dl 1.9 assertEquals(0, rl.getReadLockCount());
128 dl 1.2 }
129 dl 1.1
130 dl 1.4 /**
131 dl 1.6 * getWriteHoldCount returns number of recursive holds
132     */
133 dl 1.23 public void testGetWriteHoldCount() {
134 dl 1.6 ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135     for(int i = 1; i <= SIZE; i++) {
136     lock.writeLock().lock();
137     assertEquals(i,lock.getWriteHoldCount());
138     }
139     for(int i = SIZE; i > 0; i--) {
140     lock.writeLock().unlock();
141     assertEquals(i-1,lock.getWriteHoldCount());
142     }
143     }
144 dl 1.23
145     /**
146 dl 1.25 * WriteLock.getHoldCount returns number of recursive holds
147     */
148     public void testGetHoldCount() {
149     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150     for(int i = 1; i <= SIZE; i++) {
151     lock.writeLock().lock();
152     assertEquals(i,lock.writeLock().getHoldCount());
153     }
154     for(int i = SIZE; i > 0; i--) {
155     lock.writeLock().unlock();
156     assertEquals(i-1,lock.writeLock().getHoldCount());
157     }
158     }
159    
160     /**
161 dl 1.23 * getReadHoldCount returns number of recursive holds
162     */
163     public void testGetReadHoldCount() {
164     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165     for(int i = 1; i <= SIZE; i++) {
166     lock.readLock().lock();
167     assertEquals(i,lock.getReadHoldCount());
168     }
169     for(int i = SIZE; i > 0; i--) {
170     lock.readLock().unlock();
171     assertEquals(i-1,lock.getReadHoldCount());
172     }
173     }
174 dl 1.6
175    
176     /**
177 dl 1.4 * write-unlocking an unlocked lock throws IllegalMonitorStateException
178     */
179     public void testUnlock_IllegalMonitorStateException() {
180     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181     try {
182     rl.writeLock().unlock();
183     shouldThrow();
184     } catch(IllegalMonitorStateException success){}
185     }
186 dl 1.1
187    
188 dl 1.4 /**
189     * write-lockInterruptibly is interruptible
190     */
191     public void testWriteLockInterruptibly_Interrupted() {
192 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193     Thread t = new Thread(new Runnable() {
194 dl 1.4 public void run() {
195     try {
196 dl 1.1 lock.writeLock().lockInterruptibly();
197 dl 1.16 lock.writeLock().unlock();
198     lock.writeLock().lockInterruptibly();
199     lock.writeLock().unlock();
200 dl 1.4 } catch(InterruptedException success){}
201 dl 1.1 }
202     });
203 dl 1.2 try {
204 dl 1.16 lock.writeLock().lock();
205 dl 1.2 t.start();
206 dl 1.26 Thread.sleep(SHORT_DELAY_MS);
207 dl 1.2 t.interrupt();
208 dl 1.27 Thread.sleep(SHORT_DELAY_MS);
209 dl 1.2 lock.writeLock().unlock();
210     t.join();
211     } catch(Exception e){
212 dl 1.4 unexpectedException();
213 dl 1.2 }
214 dl 1.1 }
215    
216 dl 1.4 /**
217 dl 1.15 * timed write-tryLock is interruptible
218 dl 1.4 */
219     public void testWriteTryLock_Interrupted() {
220 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
221     lock.writeLock().lock();
222     Thread t = new Thread(new Runnable() {
223 dl 1.4 public void run() {
224     try {
225 dl 1.1 lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
226 dl 1.4 } catch(InterruptedException success){}
227 dl 1.2 }
228     });
229     try {
230     t.start();
231     t.interrupt();
232     lock.writeLock().unlock();
233     t.join();
234     } catch(Exception e){
235 dl 1.4 unexpectedException();
236 dl 1.2 }
237     }
238    
239 dl 1.4 /**
240     * read-lockInterruptibly is interruptible
241     */
242     public void testReadLockInterruptibly_Interrupted() {
243 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
244     lock.writeLock().lock();
245     Thread t = new Thread(new Runnable() {
246 dl 1.4 public void run() {
247     try {
248 dl 1.2 lock.readLock().lockInterruptibly();
249 dl 1.4 } catch(InterruptedException success){}
250 dl 1.2 }
251     });
252     try {
253     t.start();
254 dl 1.26 Thread.sleep(SHORT_DELAY_MS);
255 dl 1.2 t.interrupt();
256 dl 1.27 Thread.sleep(SHORT_DELAY_MS);
257 dl 1.2 lock.writeLock().unlock();
258     t.join();
259     } catch(Exception e){
260 dl 1.4 unexpectedException();
261 dl 1.2 }
262     }
263    
264 dl 1.4 /**
265 dl 1.15 * timed read-tryLock is interruptible
266 dl 1.4 */
267     public void testReadTryLock_Interrupted() {
268 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269     lock.writeLock().lock();
270     Thread t = new Thread(new Runnable() {
271 dl 1.4 public void run() {
272     try {
273 dl 1.2 lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
274 dl 1.4 threadShouldThrow();
275     } catch(InterruptedException success){}
276 dl 1.1 }
277     });
278 dl 1.2 try {
279     t.start();
280     t.interrupt();
281     t.join();
282     } catch(Exception e){
283 dl 1.4 unexpectedException();
284 dl 1.2 }
285 dl 1.1 }
286    
287    
288 dl 1.4 /**
289 dl 1.15 * write-tryLock fails if locked
290 dl 1.4 */
291     public void testWriteTryLockWhenLocked() {
292 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
293     lock.writeLock().lock();
294     Thread t = new Thread(new Runnable() {
295 dl 1.4 public void run() {
296 dl 1.3 threadAssertFalse(lock.writeLock().tryLock());
297 dl 1.2 }
298     });
299     try {
300     t.start();
301     t.join();
302     lock.writeLock().unlock();
303     } catch(Exception e){
304 dl 1.4 unexpectedException();
305 dl 1.2 }
306     }
307    
308 dl 1.4 /**
309 dl 1.15 * read-tryLock fails if locked
310 dl 1.4 */
311     public void testReadTryLockWhenLocked() {
312 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
313     lock.writeLock().lock();
314     Thread t = new Thread(new Runnable() {
315 dl 1.4 public void run() {
316 dl 1.3 threadAssertFalse(lock.readLock().tryLock());
317 dl 1.2 }
318     });
319     try {
320     t.start();
321     t.join();
322     lock.writeLock().unlock();
323     } catch(Exception e){
324 dl 1.4 unexpectedException();
325 dl 1.2 }
326     }
327    
328 dl 1.4 /**
329     * Multiple threads can hold a read lock when not write-locked
330     */
331 dl 1.2 public void testMultipleReadLocks() {
332     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
333     lock.readLock().lock();
334     Thread t = new Thread(new Runnable() {
335 dl 1.4 public void run() {
336 dl 1.3 threadAssertTrue(lock.readLock().tryLock());
337 dl 1.2 lock.readLock().unlock();
338     }
339     });
340     try {
341     t.start();
342     t.join();
343     lock.readLock().unlock();
344     } catch(Exception e){
345 dl 1.4 unexpectedException();
346 dl 1.2 }
347     }
348    
349 dl 1.4 /**
350     * A writelock succeeds after reading threads unlock
351     */
352 dl 1.2 public void testWriteAfterMultipleReadLocks() {
353     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
354     lock.readLock().lock();
355     Thread t1 = new Thread(new Runnable() {
356 dl 1.4 public void run() {
357 dl 1.2 lock.readLock().lock();
358     lock.readLock().unlock();
359     }
360     });
361     Thread t2 = new Thread(new Runnable() {
362 dl 1.4 public void run() {
363 dl 1.2 lock.writeLock().lock();
364     lock.writeLock().unlock();
365     }
366     });
367    
368     try {
369     t1.start();
370     t2.start();
371     Thread.sleep(SHORT_DELAY_MS);
372     lock.readLock().unlock();
373     t1.join(MEDIUM_DELAY_MS);
374     t2.join(MEDIUM_DELAY_MS);
375     assertTrue(!t1.isAlive());
376     assertTrue(!t2.isAlive());
377    
378     } catch(Exception e){
379 dl 1.4 unexpectedException();
380 dl 1.2 }
381     }
382    
383 dl 1.4 /**
384     * Readlocks succeed after a writing thread unlocks
385     */
386 dl 1.2 public void testReadAfterWriteLock() {
387     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
388     lock.writeLock().lock();
389     Thread t1 = new Thread(new Runnable() {
390 dl 1.4 public void run() {
391 dl 1.2 lock.readLock().lock();
392     lock.readLock().unlock();
393     }
394     });
395     Thread t2 = new Thread(new Runnable() {
396 dl 1.4 public void run() {
397 dl 1.2 lock.readLock().lock();
398     lock.readLock().unlock();
399     }
400     });
401    
402     try {
403     t1.start();
404     t2.start();
405     Thread.sleep(SHORT_DELAY_MS);
406     lock.writeLock().unlock();
407     t1.join(MEDIUM_DELAY_MS);
408     t2.join(MEDIUM_DELAY_MS);
409     assertTrue(!t1.isAlive());
410     assertTrue(!t2.isAlive());
411    
412     } catch(Exception e){
413 dl 1.4 unexpectedException();
414 dl 1.2 }
415     }
416    
417 dl 1.20 /**
418     * Read trylock succeeds if write locked by current thread
419     */
420     public void testReadHoldingWriteLock() {
421     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
422     lock.writeLock().lock();
423     assertTrue(lock.readLock().tryLock());
424     lock.readLock().unlock();
425     lock.writeLock().unlock();
426     }
427    
428     /**
429     * Read lock succeeds if write locked by current thread even if
430 dl 1.23 * other threads are waiting for readlock
431 dl 1.20 */
432     public void testReadHoldingWriteLock2() {
433     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
434     lock.writeLock().lock();
435     Thread t1 = new Thread(new Runnable() {
436     public void run() {
437     lock.readLock().lock();
438     lock.readLock().unlock();
439     }
440     });
441     Thread t2 = new Thread(new Runnable() {
442     public void run() {
443     lock.readLock().lock();
444     lock.readLock().unlock();
445     }
446     });
447    
448     try {
449     t1.start();
450     t2.start();
451     lock.readLock().lock();
452     lock.readLock().unlock();
453     Thread.sleep(SHORT_DELAY_MS);
454     lock.readLock().lock();
455     lock.readLock().unlock();
456     lock.writeLock().unlock();
457     t1.join(MEDIUM_DELAY_MS);
458     t2.join(MEDIUM_DELAY_MS);
459     assertTrue(!t1.isAlive());
460     assertTrue(!t2.isAlive());
461    
462     } catch(Exception e){
463     unexpectedException();
464     }
465     }
466    
467     /**
468 dl 1.23 * Read lock succeeds if write locked by current thread even if
469     * other threads are waiting for writelock
470     */
471     public void testReadHoldingWriteLock3() {
472     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
473     lock.writeLock().lock();
474     Thread t1 = new Thread(new Runnable() {
475     public void run() {
476     lock.writeLock().lock();
477     lock.writeLock().unlock();
478     }
479     });
480     Thread t2 = new Thread(new Runnable() {
481     public void run() {
482     lock.writeLock().lock();
483     lock.writeLock().unlock();
484     }
485     });
486    
487     try {
488     t1.start();
489     t2.start();
490     lock.readLock().lock();
491     lock.readLock().unlock();
492     Thread.sleep(SHORT_DELAY_MS);
493     lock.readLock().lock();
494     lock.readLock().unlock();
495     lock.writeLock().unlock();
496     t1.join(MEDIUM_DELAY_MS);
497     t2.join(MEDIUM_DELAY_MS);
498     assertTrue(!t1.isAlive());
499     assertTrue(!t2.isAlive());
500    
501     } catch(Exception e){
502     unexpectedException();
503     }
504     }
505    
506    
507     /**
508     * Write lock succeeds if write locked by current thread even if
509     * other threads are waiting for writelock
510     */
511     public void testWriteHoldingWriteLock4() {
512     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
513     lock.writeLock().lock();
514     Thread t1 = new Thread(new Runnable() {
515     public void run() {
516     lock.writeLock().lock();
517     lock.writeLock().unlock();
518     }
519     });
520     Thread t2 = new Thread(new Runnable() {
521     public void run() {
522     lock.writeLock().lock();
523     lock.writeLock().unlock();
524     }
525     });
526    
527     try {
528     t1.start();
529     t2.start();
530     lock.writeLock().lock();
531     lock.writeLock().unlock();
532     Thread.sleep(SHORT_DELAY_MS);
533     lock.writeLock().lock();
534     lock.writeLock().unlock();
535     lock.writeLock().unlock();
536     t1.join(MEDIUM_DELAY_MS);
537     t2.join(MEDIUM_DELAY_MS);
538     assertTrue(!t1.isAlive());
539     assertTrue(!t2.isAlive());
540    
541     } catch(Exception e){
542     unexpectedException();
543     }
544     }
545    
546    
547     /**
548 dl 1.20 * Fair Read trylock succeeds if write locked by current thread
549     */
550     public void testReadHoldingWriteLockFair() {
551     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
552     lock.writeLock().lock();
553     assertTrue(lock.readLock().tryLock());
554     lock.readLock().unlock();
555     lock.writeLock().unlock();
556     }
557    
558     /**
559     * Fair Read lock succeeds if write locked by current thread even if
560 dl 1.23 * other threads are waiting for readlock
561 dl 1.20 */
562     public void testReadHoldingWriteLockFair2() {
563     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
564     lock.writeLock().lock();
565     Thread t1 = new Thread(new Runnable() {
566     public void run() {
567     lock.readLock().lock();
568     lock.readLock().unlock();
569     }
570     });
571     Thread t2 = new Thread(new Runnable() {
572     public void run() {
573     lock.readLock().lock();
574     lock.readLock().unlock();
575     }
576     });
577    
578     try {
579     t1.start();
580     t2.start();
581     lock.readLock().lock();
582     lock.readLock().unlock();
583     Thread.sleep(SHORT_DELAY_MS);
584     lock.readLock().lock();
585     lock.readLock().unlock();
586     lock.writeLock().unlock();
587     t1.join(MEDIUM_DELAY_MS);
588     t2.join(MEDIUM_DELAY_MS);
589     assertTrue(!t1.isAlive());
590     assertTrue(!t2.isAlive());
591    
592     } catch(Exception e){
593     unexpectedException();
594     }
595     }
596    
597 dl 1.2
598 dl 1.4 /**
599 dl 1.23 * Fair Read lock succeeds if write locked by current thread even if
600     * other threads are waiting for writelock
601     */
602     public void testReadHoldingWriteLockFair3() {
603     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
604     lock.writeLock().lock();
605     Thread t1 = new Thread(new Runnable() {
606     public void run() {
607     lock.writeLock().lock();
608     lock.writeLock().unlock();
609     }
610     });
611     Thread t2 = new Thread(new Runnable() {
612     public void run() {
613     lock.writeLock().lock();
614     lock.writeLock().unlock();
615     }
616     });
617    
618     try {
619     t1.start();
620     t2.start();
621     lock.readLock().lock();
622     lock.readLock().unlock();
623     Thread.sleep(SHORT_DELAY_MS);
624     lock.readLock().lock();
625     lock.readLock().unlock();
626     lock.writeLock().unlock();
627     t1.join(MEDIUM_DELAY_MS);
628     t2.join(MEDIUM_DELAY_MS);
629     assertTrue(!t1.isAlive());
630     assertTrue(!t2.isAlive());
631    
632     } catch(Exception e){
633     unexpectedException();
634     }
635     }
636    
637    
638     /**
639     * Fair Write lock succeeds if write locked by current thread even if
640     * other threads are waiting for writelock
641     */
642     public void testWriteHoldingWriteLockFair4() {
643     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
644     lock.writeLock().lock();
645     Thread t1 = new Thread(new Runnable() {
646     public void run() {
647     lock.writeLock().lock();
648     lock.writeLock().unlock();
649     }
650     });
651     Thread t2 = new Thread(new Runnable() {
652     public void run() {
653     lock.writeLock().lock();
654     lock.writeLock().unlock();
655     }
656     });
657    
658     try {
659     t1.start();
660     t2.start();
661     Thread.sleep(SHORT_DELAY_MS);
662     assertTrue(lock.isWriteLockedByCurrentThread());
663     assertTrue(lock.getWriteHoldCount() == 1);
664     lock.writeLock().lock();
665     assertTrue(lock.getWriteHoldCount() == 2);
666     lock.writeLock().unlock();
667     lock.writeLock().lock();
668     lock.writeLock().unlock();
669     lock.writeLock().unlock();
670     t1.join(MEDIUM_DELAY_MS);
671     t2.join(MEDIUM_DELAY_MS);
672     assertTrue(!t1.isAlive());
673     assertTrue(!t2.isAlive());
674    
675     } catch(Exception e){
676     unexpectedException();
677     }
678     }
679    
680    
681     /**
682 dl 1.15 * Read tryLock succeeds if readlocked but not writelocked
683 dl 1.4 */
684 dl 1.2 public void testTryLockWhenReadLocked() {
685     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
686     lock.readLock().lock();
687     Thread t = new Thread(new Runnable() {
688 dl 1.4 public void run() {
689 dl 1.3 threadAssertTrue(lock.readLock().tryLock());
690 dl 1.2 lock.readLock().unlock();
691     }
692     });
693     try {
694     t.start();
695     t.join();
696     lock.readLock().unlock();
697     } catch(Exception e){
698 dl 1.4 unexpectedException();
699 dl 1.2 }
700     }
701    
702    
703    
704 dl 1.4 /**
705 dl 1.15 * write tryLock fails when readlocked
706 dl 1.4 */
707 dl 1.2 public void testWriteTryLockWhenReadLocked() {
708     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
709     lock.readLock().lock();
710     Thread t = new Thread(new Runnable() {
711 dl 1.4 public void run() {
712 dl 1.3 threadAssertFalse(lock.writeLock().tryLock());
713 dl 1.2 }
714     });
715     try {
716     t.start();
717     t.join();
718     lock.readLock().unlock();
719     } catch(Exception e){
720 dl 1.4 unexpectedException();
721 dl 1.2 }
722     }
723    
724 dl 1.24
725     /**
726     * Fair Read tryLock succeeds if readlocked but not writelocked
727     */
728     public void testTryLockWhenReadLockedFair() {
729     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
730     lock.readLock().lock();
731     Thread t = new Thread(new Runnable() {
732     public void run() {
733     threadAssertTrue(lock.readLock().tryLock());
734     lock.readLock().unlock();
735     }
736     });
737     try {
738     t.start();
739     t.join();
740     lock.readLock().unlock();
741     } catch(Exception e){
742     unexpectedException();
743     }
744     }
745    
746    
747    
748     /**
749     * Fair write tryLock fails when readlocked
750     */
751     public void testWriteTryLockWhenReadLockedFair() {
752     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
753     lock.readLock().lock();
754     Thread t = new Thread(new Runnable() {
755     public void run() {
756     threadAssertFalse(lock.writeLock().tryLock());
757     }
758     });
759     try {
760     t.start();
761     t.join();
762     lock.readLock().unlock();
763     } catch(Exception e){
764     unexpectedException();
765     }
766     }
767    
768 dl 1.2
769    
770 dl 1.4 /**
771 dl 1.15 * write timed tryLock times out if locked
772 dl 1.4 */
773     public void testWriteTryLock_Timeout() {
774 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
775     lock.writeLock().lock();
776     Thread t = new Thread(new Runnable() {
777 dl 1.4 public void run() {
778 dl 1.2 try {
779 dl 1.3 threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
780 dl 1.2 } catch (Exception ex) {
781 dl 1.4 threadUnexpectedException();
782 dl 1.2 }
783     }
784     });
785     try {
786     t.start();
787     t.join();
788     lock.writeLock().unlock();
789     } catch(Exception e){
790 dl 1.4 unexpectedException();
791 dl 1.2 }
792     }
793    
794 dl 1.4 /**
795 dl 1.15 * read timed tryLock times out if write-locked
796 dl 1.4 */
797     public void testReadTryLock_Timeout() {
798 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
799     lock.writeLock().lock();
800     Thread t = new Thread(new Runnable() {
801 dl 1.4 public void run() {
802 dl 1.2 try {
803 dl 1.3 threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
804 dl 1.2 } catch (Exception ex) {
805 dl 1.4 threadUnexpectedException();
806 dl 1.2 }
807     }
808     });
809     try {
810     t.start();
811     t.join();
812     lock.writeLock().unlock();
813     } catch(Exception e){
814 dl 1.4 unexpectedException();
815 dl 1.2 }
816     }
817    
818    
819 dl 1.4 /**
820     * write lockInterruptibly succeeds if lock free else is interruptible
821     */
822     public void testWriteLockInterruptibly() {
823 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
824     try {
825     lock.writeLock().lockInterruptibly();
826     } catch(Exception e) {
827 dl 1.4 unexpectedException();
828 dl 1.2 }
829     Thread t = new Thread(new Runnable() {
830     public void run() {
831     try {
832     lock.writeLock().lockInterruptibly();
833 dl 1.4 threadShouldThrow();
834 dl 1.2 }
835     catch(InterruptedException success) {
836     }
837     }
838     });
839     try {
840     t.start();
841 dl 1.26 Thread.sleep(SHORT_DELAY_MS);
842 dl 1.2 t.interrupt();
843 dl 1.27 Thread.sleep(SHORT_DELAY_MS);
844 dl 1.2 t.join();
845     lock.writeLock().unlock();
846     } catch(Exception e){
847 dl 1.4 unexpectedException();
848 dl 1.2 }
849     }
850    
851 dl 1.4 /**
852     * read lockInterruptibly succeeds if lock free else is interruptible
853     */
854     public void testReadLockInterruptibly() {
855 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
856     try {
857     lock.writeLock().lockInterruptibly();
858     } catch(Exception e) {
859 dl 1.4 unexpectedException();
860 dl 1.2 }
861     Thread t = new Thread(new Runnable() {
862     public void run() {
863     try {
864     lock.readLock().lockInterruptibly();
865 dl 1.4 threadShouldThrow();
866 dl 1.2 }
867     catch(InterruptedException success) {
868     }
869     }
870     });
871     try {
872     t.start();
873 dl 1.26 Thread.sleep(SHORT_DELAY_MS);
874 dl 1.2 t.interrupt();
875     t.join();
876     lock.writeLock().unlock();
877     } catch(Exception e){
878 dl 1.4 unexpectedException();
879 dl 1.2 }
880     }
881    
882 dl 1.4 /**
883     * Calling await without holding lock throws IllegalMonitorStateException
884     */
885 dl 1.2 public void testAwait_IllegalMonitor() {
886     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
887     final Condition c = lock.writeLock().newCondition();
888     try {
889     c.await();
890 dl 1.4 shouldThrow();
891 dl 1.2 }
892     catch (IllegalMonitorStateException success) {
893     }
894     catch (Exception ex) {
895 dl 1.4 shouldThrow();
896 dl 1.2 }
897     }
898    
899 dl 1.4 /**
900     * Calling signal without holding lock throws IllegalMonitorStateException
901     */
902 dl 1.2 public void testSignal_IllegalMonitor() {
903     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
904     final Condition c = lock.writeLock().newCondition();
905     try {
906     c.signal();
907 dl 1.4 shouldThrow();
908 dl 1.2 }
909     catch (IllegalMonitorStateException success) {
910     }
911     catch (Exception ex) {
912 dl 1.4 unexpectedException();
913 dl 1.2 }
914     }
915    
916 dl 1.4 /**
917     * awaitNanos without a signal times out
918     */
919 dl 1.2 public void testAwaitNanos_Timeout() {
920     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
921     final Condition c = lock.writeLock().newCondition();
922     try {
923     lock.writeLock().lock();
924     long t = c.awaitNanos(100);
925     assertTrue(t <= 0);
926     lock.writeLock().unlock();
927     }
928     catch (Exception ex) {
929 dl 1.4 unexpectedException();
930 dl 1.2 }
931     }
932    
933 dl 1.4
934     /**
935     * timed await without a signal times out
936     */
937 dl 1.2 public void testAwait_Timeout() {
938     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
939     final Condition c = lock.writeLock().newCondition();
940     try {
941     lock.writeLock().lock();
942     lock.writeLock().unlock();
943     }
944     catch (Exception ex) {
945 dl 1.4 unexpectedException();
946 dl 1.2 }
947     }
948    
949 dl 1.4 /**
950     * awaitUntil without a signal times out
951     */
952 dl 1.2 public void testAwaitUntil_Timeout() {
953     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
954     final Condition c = lock.writeLock().newCondition();
955     try {
956     lock.writeLock().lock();
957     java.util.Date d = new java.util.Date();
958     lock.writeLock().unlock();
959     }
960     catch (Exception ex) {
961 dl 1.4 unexpectedException();
962 dl 1.2 }
963     }
964 dl 1.1
965 dl 1.4 /**
966     * await returns when signalled
967     */
968 dl 1.2 public void testAwait() {
969     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
970     final Condition c = lock.writeLock().newCondition();
971     Thread t = new Thread(new Runnable() {
972     public void run() {
973     try {
974     lock.writeLock().lock();
975     c.await();
976     lock.writeLock().unlock();
977     }
978     catch(InterruptedException e) {
979 dl 1.4 threadUnexpectedException();
980 dl 1.2 }
981     }
982     });
983    
984     try {
985     t.start();
986     Thread.sleep(SHORT_DELAY_MS);
987     lock.writeLock().lock();
988     c.signal();
989     lock.writeLock().unlock();
990     t.join(SHORT_DELAY_MS);
991     assertFalse(t.isAlive());
992     }
993     catch (Exception ex) {
994 dl 1.4 unexpectedException();
995 dl 1.2 }
996     }
997    
998 dl 1.22 /** A helper class for uninterruptible wait tests */
999     class UninterruptableThread extends Thread {
1000     private Lock lock;
1001     private Condition c;
1002    
1003     public volatile boolean canAwake = false;
1004     public volatile boolean interrupted = false;
1005     public volatile boolean lockStarted = false;
1006    
1007     public UninterruptableThread(Lock lock, Condition c) {
1008     this.lock = lock;
1009     this.c = c;
1010     }
1011    
1012     public synchronized void run() {
1013     lock.lock();
1014     lockStarted = true;
1015    
1016     while (!canAwake) {
1017     c.awaitUninterruptibly();
1018     }
1019    
1020     interrupted = isInterrupted();
1021     lock.unlock();
1022     }
1023     }
1024    
1025 dl 1.4 /**
1026     * awaitUninterruptibly doesn't abort on interrupt
1027     */
1028 dl 1.2 public void testAwaitUninterruptibly() {
1029 dl 1.22 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1030 dl 1.2 final Condition c = lock.writeLock().newCondition();
1031 dl 1.22 UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1032 dl 1.2
1033     try {
1034 dl 1.22 thread.start();
1035    
1036     while (!thread.lockStarted) {
1037     Thread.sleep(100);
1038     }
1039    
1040 dl 1.2 lock.writeLock().lock();
1041 dl 1.22 try {
1042     thread.interrupt();
1043     thread.canAwake = true;
1044     c.signal();
1045     } finally {
1046     lock.writeLock().unlock();
1047     }
1048    
1049     thread.join();
1050     assertTrue(thread.interrupted);
1051     assertFalse(thread.isAlive());
1052     } catch (Exception ex) {
1053 dl 1.4 unexpectedException();
1054 dl 1.2 }
1055     }
1056    
1057 dl 1.4 /**
1058     * await is interruptible
1059     */
1060 dl 1.2 public void testAwait_Interrupt() {
1061     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1062     final Condition c = lock.writeLock().newCondition();
1063     Thread t = new Thread(new Runnable() {
1064     public void run() {
1065     try {
1066     lock.writeLock().lock();
1067     c.await();
1068     lock.writeLock().unlock();
1069 dl 1.4 threadShouldThrow();
1070 dl 1.2 }
1071     catch(InterruptedException success) {
1072     }
1073     }
1074     });
1075    
1076     try {
1077     t.start();
1078     Thread.sleep(SHORT_DELAY_MS);
1079     t.interrupt();
1080     t.join(SHORT_DELAY_MS);
1081     assertFalse(t.isAlive());
1082     }
1083     catch (Exception ex) {
1084 dl 1.4 unexpectedException();
1085 dl 1.2 }
1086     }
1087    
1088 dl 1.4 /**
1089     * awaitNanos is interruptible
1090     */
1091 dl 1.2 public void testAwaitNanos_Interrupt() {
1092     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1093     final Condition c = lock.writeLock().newCondition();
1094     Thread t = new Thread(new Runnable() {
1095     public void run() {
1096     try {
1097     lock.writeLock().lock();
1098     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
1099     lock.writeLock().unlock();
1100 dl 1.4 threadShouldThrow();
1101 dl 1.2 }
1102     catch(InterruptedException success) {
1103     }
1104     }
1105     });
1106    
1107     try {
1108     t.start();
1109     Thread.sleep(SHORT_DELAY_MS);
1110     t.interrupt();
1111     t.join(SHORT_DELAY_MS);
1112     assertFalse(t.isAlive());
1113     }
1114     catch (Exception ex) {
1115 dl 1.4 unexpectedException();
1116 dl 1.2 }
1117     }
1118 dl 1.1
1119 dl 1.4 /**
1120     * awaitUntil is interruptible
1121     */
1122 dl 1.2 public void testAwaitUntil_Interrupt() {
1123 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1124 dl 1.2 final Condition c = lock.writeLock().newCondition();
1125 dl 1.1 Thread t = new Thread(new Runnable() {
1126     public void run() {
1127     try {
1128 dl 1.2 lock.writeLock().lock();
1129     java.util.Date d = new java.util.Date();
1130     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1131     lock.writeLock().unlock();
1132 dl 1.4 threadShouldThrow();
1133 dl 1.2 }
1134     catch(InterruptedException success) {
1135     }
1136     }
1137     });
1138    
1139     try {
1140     t.start();
1141     Thread.sleep(SHORT_DELAY_MS);
1142     t.interrupt();
1143     t.join(SHORT_DELAY_MS);
1144     assertFalse(t.isAlive());
1145     }
1146     catch (Exception ex) {
1147 dl 1.4 unexpectedException();
1148 dl 1.2 }
1149     }
1150    
1151 dl 1.4 /**
1152     * signalAll wakes up all threads
1153     */
1154 dl 1.2 public void testSignalAll() {
1155     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1156     final Condition c = lock.writeLock().newCondition();
1157     Thread t1 = new Thread(new Runnable() {
1158     public void run() {
1159     try {
1160     lock.writeLock().lock();
1161     c.await();
1162     lock.writeLock().unlock();
1163     }
1164     catch(InterruptedException e) {
1165 dl 1.4 threadUnexpectedException();
1166 dl 1.2 }
1167     }
1168     });
1169    
1170     Thread t2 = new Thread(new Runnable() {
1171     public void run() {
1172     try {
1173     lock.writeLock().lock();
1174     c.await();
1175     lock.writeLock().unlock();
1176 dl 1.1 }
1177 dl 1.2 catch(InterruptedException e) {
1178 dl 1.4 threadUnexpectedException();
1179 dl 1.2 }
1180 dl 1.1 }
1181     });
1182 dl 1.2
1183     try {
1184     t1.start();
1185     t2.start();
1186     Thread.sleep(SHORT_DELAY_MS);
1187     lock.writeLock().lock();
1188     c.signalAll();
1189     lock.writeLock().unlock();
1190     t1.join(SHORT_DELAY_MS);
1191     t2.join(SHORT_DELAY_MS);
1192     assertFalse(t1.isAlive());
1193     assertFalse(t2.isAlive());
1194     }
1195     catch (Exception ex) {
1196 dl 1.4 unexpectedException();
1197 dl 1.2 }
1198     }
1199    
1200 dl 1.4 /**
1201     * A serialized lock deserializes as unlocked
1202     */
1203 dl 1.2 public void testSerialization() {
1204     ReentrantReadWriteLock l = new ReentrantReadWriteLock();
1205     l.readLock().lock();
1206     l.readLock().unlock();
1207    
1208     try {
1209     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1210     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1211     out.writeObject(l);
1212     out.close();
1213    
1214     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1215     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1216     ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
1217     r.readLock().lock();
1218     r.readLock().unlock();
1219     } catch(Exception e){
1220     e.printStackTrace();
1221 dl 1.4 unexpectedException();
1222 dl 1.2 }
1223 dl 1.1 }
1224 dl 1.2
1225 dl 1.6 /**
1226 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
1227     */
1228     public void testhasQueuedThreads() {
1229     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1230     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1231     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1232     try {
1233     assertFalse(lock.hasQueuedThreads());
1234     lock.writeLock().lock();
1235     t1.start();
1236     Thread.sleep(SHORT_DELAY_MS);
1237     assertTrue(lock.hasQueuedThreads());
1238     t2.start();
1239     Thread.sleep(SHORT_DELAY_MS);
1240     assertTrue(lock.hasQueuedThreads());
1241     t1.interrupt();
1242     Thread.sleep(SHORT_DELAY_MS);
1243     assertTrue(lock.hasQueuedThreads());
1244     lock.writeLock().unlock();
1245     Thread.sleep(SHORT_DELAY_MS);
1246     assertFalse(lock.hasQueuedThreads());
1247     t1.join();
1248     t2.join();
1249     } catch(Exception e){
1250     unexpectedException();
1251     }
1252     }
1253    
1254     /**
1255 dl 1.19 * hasQueuedThread(null) throws NPE
1256     */
1257     public void testHasQueuedThreadNPE() {
1258     final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1259     try {
1260     sync.hasQueuedThread(null);
1261     shouldThrow();
1262     } catch (NullPointerException success) {
1263     }
1264     }
1265    
1266     /**
1267     * hasQueuedThread reports whether a thread is queued.
1268     */
1269     public void testHasQueuedThread() {
1270     final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1271     Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1272     Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1273     try {
1274     assertFalse(sync.hasQueuedThread(t1));
1275     assertFalse(sync.hasQueuedThread(t2));
1276     sync.writeLock().lock();
1277     t1.start();
1278     Thread.sleep(SHORT_DELAY_MS);
1279     assertTrue(sync.hasQueuedThread(t1));
1280     t2.start();
1281     Thread.sleep(SHORT_DELAY_MS);
1282     assertTrue(sync.hasQueuedThread(t1));
1283     assertTrue(sync.hasQueuedThread(t2));
1284     t1.interrupt();
1285     Thread.sleep(SHORT_DELAY_MS);
1286     assertFalse(sync.hasQueuedThread(t1));
1287     assertTrue(sync.hasQueuedThread(t2));
1288     sync.writeLock().unlock();
1289     Thread.sleep(SHORT_DELAY_MS);
1290     assertFalse(sync.hasQueuedThread(t1));
1291     Thread.sleep(SHORT_DELAY_MS);
1292     assertFalse(sync.hasQueuedThread(t2));
1293     t1.join();
1294     t2.join();
1295     } catch(Exception e){
1296     unexpectedException();
1297     }
1298     }
1299    
1300    
1301     /**
1302 dl 1.6 * getQueueLength reports number of waiting threads
1303     */
1304     public void testGetQueueLength() {
1305     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1306     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1307     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1308     try {
1309     assertEquals(0, lock.getQueueLength());
1310     lock.writeLock().lock();
1311     t1.start();
1312     Thread.sleep(SHORT_DELAY_MS);
1313     assertEquals(1, lock.getQueueLength());
1314     t2.start();
1315     Thread.sleep(SHORT_DELAY_MS);
1316     assertEquals(2, lock.getQueueLength());
1317     t1.interrupt();
1318     Thread.sleep(SHORT_DELAY_MS);
1319     assertEquals(1, lock.getQueueLength());
1320     lock.writeLock().unlock();
1321     Thread.sleep(SHORT_DELAY_MS);
1322     assertEquals(0, lock.getQueueLength());
1323     t1.join();
1324     t2.join();
1325     } catch(Exception e){
1326     unexpectedException();
1327     }
1328     }
1329    
1330     /**
1331     * getQueuedThreads includes waiting threads
1332     */
1333     public void testGetQueuedThreads() {
1334     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1335     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1336     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1337     try {
1338     assertTrue(lock.getQueuedThreads().isEmpty());
1339     lock.writeLock().lock();
1340     assertTrue(lock.getQueuedThreads().isEmpty());
1341     t1.start();
1342     Thread.sleep(SHORT_DELAY_MS);
1343     assertTrue(lock.getQueuedThreads().contains(t1));
1344     t2.start();
1345     Thread.sleep(SHORT_DELAY_MS);
1346     assertTrue(lock.getQueuedThreads().contains(t1));
1347     assertTrue(lock.getQueuedThreads().contains(t2));
1348     t1.interrupt();
1349     Thread.sleep(SHORT_DELAY_MS);
1350     assertFalse(lock.getQueuedThreads().contains(t1));
1351     assertTrue(lock.getQueuedThreads().contains(t2));
1352     lock.writeLock().unlock();
1353     Thread.sleep(SHORT_DELAY_MS);
1354     assertTrue(lock.getQueuedThreads().isEmpty());
1355     t1.join();
1356     t2.join();
1357     } catch(Exception e){
1358     unexpectedException();
1359     }
1360     }
1361    
1362     /**
1363 dl 1.14 * hasWaiters throws NPE if null
1364     */
1365     public void testHasWaitersNPE() {
1366     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1367     try {
1368     lock.hasWaiters(null);
1369     shouldThrow();
1370     } catch (NullPointerException success) {
1371     } catch (Exception ex) {
1372     unexpectedException();
1373     }
1374     }
1375    
1376     /**
1377     * getWaitQueueLength throws NPE if null
1378     */
1379     public void testGetWaitQueueLengthNPE() {
1380     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1381     try {
1382     lock.getWaitQueueLength(null);
1383     shouldThrow();
1384     } catch (NullPointerException success) {
1385     } catch (Exception ex) {
1386     unexpectedException();
1387     }
1388     }
1389    
1390    
1391     /**
1392     * getWaitingThreads throws NPE if null
1393     */
1394     public void testGetWaitingThreadsNPE() {
1395     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1396     try {
1397     lock.getWaitingThreads(null);
1398     shouldThrow();
1399     } catch (NullPointerException success) {
1400     } catch (Exception ex) {
1401     unexpectedException();
1402     }
1403     }
1404    
1405     /**
1406 dl 1.13 * hasWaiters throws IAE if not owned
1407     */
1408     public void testHasWaitersIAE() {
1409     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1410     final Condition c = (lock.writeLock().newCondition());
1411     final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1412     try {
1413     lock2.hasWaiters(c);
1414     shouldThrow();
1415     } catch (IllegalArgumentException success) {
1416     } catch (Exception ex) {
1417     unexpectedException();
1418     }
1419     }
1420    
1421     /**
1422     * hasWaiters throws IMSE if not locked
1423     */
1424     public void testHasWaitersIMSE() {
1425     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1426     final Condition c = (lock.writeLock().newCondition());
1427     try {
1428     lock.hasWaiters(c);
1429     shouldThrow();
1430     } catch (IllegalMonitorStateException success) {
1431     } catch (Exception ex) {
1432     unexpectedException();
1433     }
1434     }
1435    
1436    
1437     /**
1438     * getWaitQueueLength throws IAE if not owned
1439     */
1440     public void testGetWaitQueueLengthIAE() {
1441     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1442     final Condition c = (lock.writeLock().newCondition());
1443     final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1444     try {
1445     lock2.getWaitQueueLength(c);
1446     shouldThrow();
1447     } catch (IllegalArgumentException success) {
1448     } catch (Exception ex) {
1449     unexpectedException();
1450     }
1451     }
1452    
1453     /**
1454     * getWaitQueueLength throws IMSE if not locked
1455     */
1456     public void testGetWaitQueueLengthIMSE() {
1457     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1458     final Condition c = (lock.writeLock().newCondition());
1459     try {
1460     lock.getWaitQueueLength(c);
1461     shouldThrow();
1462     } catch (IllegalMonitorStateException success) {
1463     } catch (Exception ex) {
1464     unexpectedException();
1465     }
1466     }
1467    
1468    
1469     /**
1470     * getWaitingThreads throws IAE if not owned
1471     */
1472     public void testGetWaitingThreadsIAE() {
1473     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1474     final Condition c = (lock.writeLock().newCondition());
1475     final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1476     try {
1477     lock2.getWaitingThreads(c);
1478     shouldThrow();
1479     } catch (IllegalArgumentException success) {
1480     } catch (Exception ex) {
1481     unexpectedException();
1482     }
1483     }
1484    
1485     /**
1486     * getWaitingThreads throws IMSE if not locked
1487     */
1488     public void testGetWaitingThreadsIMSE() {
1489     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1490     final Condition c = (lock.writeLock().newCondition());
1491     try {
1492     lock.getWaitingThreads(c);
1493     shouldThrow();
1494     } catch (IllegalMonitorStateException success) {
1495     } catch (Exception ex) {
1496     unexpectedException();
1497     }
1498     }
1499    
1500    
1501     /**
1502 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
1503     */
1504     public void testHasWaiters() {
1505 dl 1.13 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1506     final Condition c = (lock.writeLock().newCondition());
1507 dl 1.6 Thread t = new Thread(new Runnable() {
1508     public void run() {
1509     try {
1510     lock.writeLock().lock();
1511 dl 1.13 threadAssertFalse(lock.hasWaiters(c));
1512     threadAssertEquals(0, lock.getWaitQueueLength(c));
1513 dl 1.6 c.await();
1514     lock.writeLock().unlock();
1515     }
1516     catch(InterruptedException e) {
1517     threadUnexpectedException();
1518     }
1519     }
1520     });
1521    
1522     try {
1523     t.start();
1524     Thread.sleep(SHORT_DELAY_MS);
1525     lock.writeLock().lock();
1526 dl 1.13 assertTrue(lock.hasWaiters(c));
1527     assertEquals(1, lock.getWaitQueueLength(c));
1528 dl 1.6 c.signal();
1529     lock.writeLock().unlock();
1530     Thread.sleep(SHORT_DELAY_MS);
1531     lock.writeLock().lock();
1532 dl 1.13 assertFalse(lock.hasWaiters(c));
1533     assertEquals(0, lock.getWaitQueueLength(c));
1534 dl 1.6 lock.writeLock().unlock();
1535     t.join(SHORT_DELAY_MS);
1536     assertFalse(t.isAlive());
1537     }
1538     catch (Exception ex) {
1539     unexpectedException();
1540     }
1541     }
1542    
1543     /**
1544     * getWaitQueueLength returns number of waiting threads
1545     */
1546     public void testGetWaitQueueLength() {
1547 dl 1.13 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1548     final Condition c = (lock.writeLock().newCondition());
1549     Thread t = new Thread(new Runnable() {
1550     public void run() {
1551     try {
1552     lock.writeLock().lock();
1553     threadAssertFalse(lock.hasWaiters(c));
1554     threadAssertEquals(0, lock.getWaitQueueLength(c));
1555     c.await();
1556     lock.writeLock().unlock();
1557     }
1558     catch(InterruptedException e) {
1559     threadUnexpectedException();
1560     }
1561     }
1562     });
1563    
1564     try {
1565     t.start();
1566     Thread.sleep(SHORT_DELAY_MS);
1567     lock.writeLock().lock();
1568     assertTrue(lock.hasWaiters(c));
1569     assertEquals(1, lock.getWaitQueueLength(c));
1570     c.signal();
1571     lock.writeLock().unlock();
1572     Thread.sleep(SHORT_DELAY_MS);
1573     lock.writeLock().lock();
1574     assertFalse(lock.hasWaiters(c));
1575     assertEquals(0, lock.getWaitQueueLength(c));
1576     lock.writeLock().unlock();
1577     t.join(SHORT_DELAY_MS);
1578     assertFalse(t.isAlive());
1579     }
1580     catch (Exception ex) {
1581     unexpectedException();
1582     }
1583     }
1584    
1585    
1586     /**
1587     * getWaitingThreads returns only and all waiting threads
1588     */
1589     public void testGetWaitingThreads() {
1590     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1591     final Condition c = lock.writeLock().newCondition();
1592 dl 1.6 Thread t1 = new Thread(new Runnable() {
1593     public void run() {
1594     try {
1595     lock.writeLock().lock();
1596 dl 1.13 threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1597 dl 1.6 c.await();
1598     lock.writeLock().unlock();
1599     }
1600     catch(InterruptedException e) {
1601     threadUnexpectedException();
1602     }
1603     }
1604     });
1605    
1606     Thread t2 = new Thread(new Runnable() {
1607     public void run() {
1608     try {
1609     lock.writeLock().lock();
1610 dl 1.13 threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1611 dl 1.6 c.await();
1612     lock.writeLock().unlock();
1613     }
1614     catch(InterruptedException e) {
1615     threadUnexpectedException();
1616     }
1617     }
1618     });
1619    
1620     try {
1621 dl 1.13 lock.writeLock().lock();
1622     assertTrue(lock.getWaitingThreads(c).isEmpty());
1623     lock.writeLock().unlock();
1624 dl 1.6 t1.start();
1625     Thread.sleep(SHORT_DELAY_MS);
1626     t2.start();
1627     Thread.sleep(SHORT_DELAY_MS);
1628     lock.writeLock().lock();
1629 dl 1.13 assertTrue(lock.hasWaiters(c));
1630     assertTrue(lock.getWaitingThreads(c).contains(t1));
1631     assertTrue(lock.getWaitingThreads(c).contains(t2));
1632 dl 1.6 c.signalAll();
1633     lock.writeLock().unlock();
1634     Thread.sleep(SHORT_DELAY_MS);
1635     lock.writeLock().lock();
1636 dl 1.13 assertFalse(lock.hasWaiters(c));
1637     assertTrue(lock.getWaitingThreads(c).isEmpty());
1638 dl 1.6 lock.writeLock().unlock();
1639     t1.join(SHORT_DELAY_MS);
1640     t2.join(SHORT_DELAY_MS);
1641     assertFalse(t1.isAlive());
1642     assertFalse(t2.isAlive());
1643     }
1644     catch (Exception ex) {
1645     unexpectedException();
1646     }
1647     }
1648 dl 1.13
1649 dl 1.18 /**
1650     * toString indicates current lock state
1651     */
1652     public void testToString() {
1653     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1654     String us = lock.toString();
1655     assertTrue(us.indexOf("Write locks = 0") >= 0);
1656     assertTrue(us.indexOf("Read locks = 0") >= 0);
1657     lock.writeLock().lock();
1658     String ws = lock.toString();
1659     assertTrue(ws.indexOf("Write locks = 1") >= 0);
1660     assertTrue(ws.indexOf("Read locks = 0") >= 0);
1661     lock.writeLock().unlock();
1662     lock.readLock().lock();
1663     lock.readLock().lock();
1664     String rs = lock.toString();
1665     assertTrue(rs.indexOf("Write locks = 0") >= 0);
1666     assertTrue(rs.indexOf("Read locks = 2") >= 0);
1667     }
1668    
1669     /**
1670     * readLock.toString indicates current lock state
1671     */
1672     public void testReadLockToString() {
1673     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1674     String us = lock.readLock().toString();
1675     assertTrue(us.indexOf("Read locks = 0") >= 0);
1676     lock.readLock().lock();
1677     lock.readLock().lock();
1678     String rs = lock.readLock().toString();
1679     assertTrue(rs.indexOf("Read locks = 2") >= 0);
1680     }
1681    
1682     /**
1683     * writeLock.toString indicates current lock state
1684     */
1685     public void testWriteLockToString() {
1686     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1687     String us = lock.writeLock().toString();
1688     assertTrue(us.indexOf("Unlocked") >= 0);
1689     lock.writeLock().lock();
1690     String ls = lock.writeLock().toString();
1691     assertTrue(ls.indexOf("Locked") >= 0);
1692     }
1693    
1694 dl 1.1 }