ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.16
Committed: Wed Jan 7 01:02:17 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.15: +4 -3 lines
Log Message:
Added and fised testcases

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