ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.8
Committed: Fri Dec 19 14:44:25 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.7: +3 -3 lines
Log Message:
Adjust condition declarations

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.locks.*;
10     import java.util.concurrent.*;
11 dl 1.2 import java.io.*;
12 dl 1.6 import java.util.*;
13 dl 1.1
14 dl 1.3 public class ReentrantReadWriteLockTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(ReentrantReadWriteLockTest.class);
20     }
21    
22 dl 1.6 /**
23     * A runnable calling lockInterruptibly
24     */
25     class InterruptibleLockRunnable implements Runnable {
26     final ReentrantReadWriteLock lock;
27     InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
28     public void run() {
29     try {
30     lock.writeLock().lockInterruptibly();
31     } catch(InterruptedException success){}
32     }
33     }
34    
35    
36     /**
37     * A runnable calling lockInterruptibly that expects to be
38     * interrupted
39     */
40     class InterruptedLockRunnable implements Runnable {
41     final ReentrantReadWriteLock lock;
42     InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
43     public void run() {
44     try {
45     lock.writeLock().lockInterruptibly();
46     threadShouldThrow();
47     } catch(InterruptedException success){}
48     }
49     }
50    
51     /**
52     * Subclass to expose protected methods
53     */
54     static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
55     PublicReentrantReadWriteLock() { super(); }
56     public Collection<Thread> getQueuedThreads() {
57     return super.getQueuedThreads();
58     }
59 dl 1.7 public PublicCondition newCondition() {
60 dl 1.6 return new PublicCondition(this);
61     }
62    
63 dl 1.8 static class PublicCondition extends ReentrantReadWriteLock.WriterConditionObject {
64 dl 1.6 PublicCondition(PublicReentrantReadWriteLock l) { super(l); }
65     public Collection<Thread> getWaitingThreads() {
66     return super.getWaitingThreads();
67     }
68     }
69    
70     }
71    
72     /**
73     * Constructor sets given fairness, and is in unlocked state
74     */
75     public void testConstructor() {
76     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
77     assertFalse(rl.isFair());
78     assertFalse(rl.isWriteLocked());
79     assertEquals(0, rl.getReadLocks());
80     ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
81     assertTrue(r2.isFair());
82     assertFalse(r2.isWriteLocked());
83     assertEquals(0, r2.getReadLocks());
84     }
85 dl 1.1
86 dl 1.5 /**
87 dl 1.4 * write-locking and read-locking an unlocked lock succeed
88 dl 1.1 */
89 dl 1.4 public void testLock() {
90 dl 1.1 ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
91 dl 1.4 rl.writeLock().lock();
92 dl 1.6 assertTrue(rl.isWriteLocked());
93     assertTrue(rl.isWriteLockedByCurrentThread());
94     assertEquals(0, rl.getReadLocks());
95 dl 1.4 rl.writeLock().unlock();
96 dl 1.6 assertFalse(rl.isWriteLocked());
97     assertFalse(rl.isWriteLockedByCurrentThread());
98     assertEquals(0, rl.getReadLocks());
99 dl 1.4 rl.readLock().lock();
100 dl 1.6 assertFalse(rl.isWriteLocked());
101     assertFalse(rl.isWriteLockedByCurrentThread());
102     assertEquals(1, rl.getReadLocks());
103 dl 1.4 rl.readLock().unlock();
104 dl 1.6 assertFalse(rl.isWriteLocked());
105     assertFalse(rl.isWriteLockedByCurrentThread());
106     assertEquals(0, rl.getReadLocks());
107 dl 1.4 }
108    
109 dl 1.1
110 dl 1.5 /**
111 dl 1.4 * locking an unlocked fair lock succeeds
112     */
113     public void testFairLock() {
114     ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
115     rl.writeLock().lock();
116 dl 1.6 assertTrue(rl.isWriteLocked());
117     assertTrue(rl.isWriteLockedByCurrentThread());
118     assertEquals(0, rl.getReadLocks());
119 dl 1.4 rl.writeLock().unlock();
120 dl 1.6 assertFalse(rl.isWriteLocked());
121     assertFalse(rl.isWriteLockedByCurrentThread());
122     assertEquals(0, rl.getReadLocks());
123 dl 1.4 rl.readLock().lock();
124 dl 1.6 assertFalse(rl.isWriteLocked());
125     assertFalse(rl.isWriteLockedByCurrentThread());
126     assertEquals(1, rl.getReadLocks());
127 dl 1.4 rl.readLock().unlock();
128 dl 1.6 assertFalse(rl.isWriteLocked());
129     assertFalse(rl.isWriteLockedByCurrentThread());
130     assertEquals(0, rl.getReadLocks());
131 dl 1.2 }
132 dl 1.1
133 dl 1.4 /**
134 dl 1.6 * getWriteHoldCount returns number of recursive holds
135     */
136     public void testGetHoldCount() {
137     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
138     for(int i = 1; i <= SIZE; i++) {
139     lock.writeLock().lock();
140     assertEquals(i,lock.getWriteHoldCount());
141     }
142     for(int i = SIZE; i > 0; i--) {
143     lock.writeLock().unlock();
144     assertEquals(i-1,lock.getWriteHoldCount());
145     }
146     }
147    
148    
149     /**
150 dl 1.4 * write-unlocking an unlocked lock throws IllegalMonitorStateException
151     */
152     public void testUnlock_IllegalMonitorStateException() {
153     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
154     try {
155     rl.writeLock().unlock();
156     shouldThrow();
157     } catch(IllegalMonitorStateException success){}
158     }
159 dl 1.1
160    
161 dl 1.4 /**
162     * write-lockInterruptibly is interruptible
163     */
164     public void testWriteLockInterruptibly_Interrupted() {
165 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
166     lock.writeLock().lock();
167     Thread t = new Thread(new Runnable() {
168 dl 1.4 public void run() {
169     try {
170 dl 1.1 lock.writeLock().lockInterruptibly();
171 dl 1.4 threadShouldThrow();
172     } catch(InterruptedException success){}
173 dl 1.1 }
174     });
175 dl 1.2 try {
176     t.start();
177     t.interrupt();
178     lock.writeLock().unlock();
179     t.join();
180     } catch(Exception e){
181 dl 1.4 unexpectedException();
182 dl 1.2 }
183 dl 1.1 }
184    
185 dl 1.4 /**
186     * timed write-trylock is interruptible
187     */
188     public void testWriteTryLock_Interrupted() {
189 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
190     lock.writeLock().lock();
191     Thread t = new Thread(new Runnable() {
192 dl 1.4 public void run() {
193     try {
194 dl 1.1 lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
195 dl 1.4 threadShouldThrow();
196     } catch(InterruptedException success){}
197 dl 1.2 }
198     });
199     try {
200     t.start();
201     t.interrupt();
202     lock.writeLock().unlock();
203     t.join();
204     } catch(Exception e){
205 dl 1.4 unexpectedException();
206 dl 1.2 }
207     }
208    
209 dl 1.4 /**
210     * read-lockInterruptibly is interruptible
211     */
212     public void testReadLockInterruptibly_Interrupted() {
213 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
214     lock.writeLock().lock();
215     Thread t = new Thread(new Runnable() {
216 dl 1.4 public void run() {
217     try {
218 dl 1.2 lock.readLock().lockInterruptibly();
219 dl 1.4 threadShouldThrow();
220     } catch(InterruptedException success){}
221 dl 1.2 }
222     });
223     try {
224     t.start();
225     t.interrupt();
226     lock.writeLock().unlock();
227     t.join();
228     } catch(Exception e){
229 dl 1.4 unexpectedException();
230 dl 1.2 }
231     }
232    
233 dl 1.4 /**
234     * timed read-trylock is interruptible
235     */
236     public void testReadTryLock_Interrupted() {
237 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
238     lock.writeLock().lock();
239     Thread t = new Thread(new Runnable() {
240 dl 1.4 public void run() {
241     try {
242 dl 1.2 lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
243 dl 1.4 threadShouldThrow();
244     } catch(InterruptedException success){}
245 dl 1.1 }
246     });
247 dl 1.2 try {
248     t.start();
249     t.interrupt();
250     t.join();
251     } catch(Exception e){
252 dl 1.4 unexpectedException();
253 dl 1.2 }
254 dl 1.1 }
255    
256    
257 dl 1.4 /**
258     * write-trylock fails if locked
259     */
260     public void testWriteTryLockWhenLocked() {
261 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
262     lock.writeLock().lock();
263     Thread t = new Thread(new Runnable() {
264 dl 1.4 public void run() {
265 dl 1.3 threadAssertFalse(lock.writeLock().tryLock());
266 dl 1.2 }
267     });
268     try {
269     t.start();
270     t.join();
271     lock.writeLock().unlock();
272     } catch(Exception e){
273 dl 1.4 unexpectedException();
274 dl 1.2 }
275     }
276    
277 dl 1.4 /**
278     * read-trylock fails if locked
279     */
280     public void testReadTryLockWhenLocked() {
281 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
282     lock.writeLock().lock();
283     Thread t = new Thread(new Runnable() {
284 dl 1.4 public void run() {
285 dl 1.3 threadAssertFalse(lock.readLock().tryLock());
286 dl 1.2 }
287     });
288     try {
289     t.start();
290     t.join();
291     lock.writeLock().unlock();
292     } catch(Exception e){
293 dl 1.4 unexpectedException();
294 dl 1.2 }
295     }
296    
297 dl 1.4 /**
298     * Multiple threads can hold a read lock when not write-locked
299     */
300 dl 1.2 public void testMultipleReadLocks() {
301     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
302     lock.readLock().lock();
303     Thread t = new Thread(new Runnable() {
304 dl 1.4 public void run() {
305 dl 1.3 threadAssertTrue(lock.readLock().tryLock());
306 dl 1.2 lock.readLock().unlock();
307     }
308     });
309     try {
310     t.start();
311     t.join();
312     lock.readLock().unlock();
313     } catch(Exception e){
314 dl 1.4 unexpectedException();
315 dl 1.2 }
316     }
317    
318 dl 1.4 /**
319     * A writelock succeeds after reading threads unlock
320     */
321 dl 1.2 public void testWriteAfterMultipleReadLocks() {
322     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
323     lock.readLock().lock();
324     Thread t1 = new Thread(new Runnable() {
325 dl 1.4 public void run() {
326 dl 1.2 lock.readLock().lock();
327     lock.readLock().unlock();
328     }
329     });
330     Thread t2 = new Thread(new Runnable() {
331 dl 1.4 public void run() {
332 dl 1.2 lock.writeLock().lock();
333     lock.writeLock().unlock();
334     }
335     });
336    
337     try {
338     t1.start();
339     t2.start();
340     Thread.sleep(SHORT_DELAY_MS);
341     lock.readLock().unlock();
342     t1.join(MEDIUM_DELAY_MS);
343     t2.join(MEDIUM_DELAY_MS);
344     assertTrue(!t1.isAlive());
345     assertTrue(!t2.isAlive());
346    
347     } catch(Exception e){
348 dl 1.4 unexpectedException();
349 dl 1.2 }
350     }
351    
352 dl 1.4 /**
353     * Readlocks succeed after a writing thread unlocks
354     */
355 dl 1.2 public void testReadAfterWriteLock() {
356     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
357     lock.writeLock().lock();
358     Thread t1 = 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     Thread t2 = new Thread(new Runnable() {
365 dl 1.4 public void run() {
366 dl 1.2 lock.readLock().lock();
367     lock.readLock().unlock();
368     }
369     });
370    
371     try {
372     t1.start();
373     t2.start();
374     Thread.sleep(SHORT_DELAY_MS);
375     lock.writeLock().unlock();
376     t1.join(MEDIUM_DELAY_MS);
377     t2.join(MEDIUM_DELAY_MS);
378     assertTrue(!t1.isAlive());
379     assertTrue(!t2.isAlive());
380    
381     } catch(Exception e){
382 dl 1.4 unexpectedException();
383 dl 1.2 }
384     }
385    
386    
387 dl 1.4 /**
388     * Read trylock succeeds if readlocked but not writelocked
389     */
390 dl 1.2 public void testTryLockWhenReadLocked() {
391     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
392     lock.readLock().lock();
393     Thread t = new Thread(new Runnable() {
394 dl 1.4 public void run() {
395 dl 1.3 threadAssertTrue(lock.readLock().tryLock());
396 dl 1.2 lock.readLock().unlock();
397     }
398     });
399     try {
400     t.start();
401     t.join();
402     lock.readLock().unlock();
403     } catch(Exception e){
404 dl 1.4 unexpectedException();
405 dl 1.2 }
406     }
407    
408    
409    
410 dl 1.4 /**
411     * write trylock fails when readlocked
412     */
413 dl 1.2 public void testWriteTryLockWhenReadLocked() {
414     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
415     lock.readLock().lock();
416     Thread t = new Thread(new Runnable() {
417 dl 1.4 public void run() {
418 dl 1.3 threadAssertFalse(lock.writeLock().tryLock());
419 dl 1.2 }
420     });
421     try {
422     t.start();
423     t.join();
424     lock.readLock().unlock();
425     } catch(Exception e){
426 dl 1.4 unexpectedException();
427 dl 1.2 }
428     }
429    
430    
431    
432 dl 1.4 /**
433     * write timed trylock times out if locked
434     */
435     public void testWriteTryLock_Timeout() {
436 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
437     lock.writeLock().lock();
438     Thread t = new Thread(new Runnable() {
439 dl 1.4 public void run() {
440 dl 1.2 try {
441 dl 1.3 threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
442 dl 1.2 } catch (Exception ex) {
443 dl 1.4 threadUnexpectedException();
444 dl 1.2 }
445     }
446     });
447     try {
448     t.start();
449     t.join();
450     lock.writeLock().unlock();
451     } catch(Exception e){
452 dl 1.4 unexpectedException();
453 dl 1.2 }
454     }
455    
456 dl 1.4 /**
457     * read timed trylock times out if write-locked
458     */
459     public void testReadTryLock_Timeout() {
460 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
461     lock.writeLock().lock();
462     Thread t = new Thread(new Runnable() {
463 dl 1.4 public void run() {
464 dl 1.2 try {
465 dl 1.3 threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
466 dl 1.2 } catch (Exception ex) {
467 dl 1.4 threadUnexpectedException();
468 dl 1.2 }
469     }
470     });
471     try {
472     t.start();
473     t.join();
474     lock.writeLock().unlock();
475     } catch(Exception e){
476 dl 1.4 unexpectedException();
477 dl 1.2 }
478     }
479    
480    
481 dl 1.4 /**
482     * write lockInterruptibly succeeds if lock free else is interruptible
483     */
484     public void testWriteLockInterruptibly() {
485 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
486     try {
487     lock.writeLock().lockInterruptibly();
488     } catch(Exception e) {
489 dl 1.4 unexpectedException();
490 dl 1.2 }
491     Thread t = new Thread(new Runnable() {
492     public void run() {
493     try {
494     lock.writeLock().lockInterruptibly();
495 dl 1.4 threadShouldThrow();
496 dl 1.2 }
497     catch(InterruptedException success) {
498     }
499     }
500     });
501     try {
502     t.start();
503     t.interrupt();
504     t.join();
505     lock.writeLock().unlock();
506     } catch(Exception e){
507 dl 1.4 unexpectedException();
508 dl 1.2 }
509     }
510    
511 dl 1.4 /**
512     * read lockInterruptibly succeeds if lock free else is interruptible
513     */
514     public void testReadLockInterruptibly() {
515 dl 1.2 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
516     try {
517     lock.writeLock().lockInterruptibly();
518     } catch(Exception e) {
519 dl 1.4 unexpectedException();
520 dl 1.2 }
521     Thread t = new Thread(new Runnable() {
522     public void run() {
523     try {
524     lock.readLock().lockInterruptibly();
525 dl 1.4 threadShouldThrow();
526 dl 1.2 }
527     catch(InterruptedException success) {
528     }
529     }
530     });
531     try {
532     t.start();
533     t.interrupt();
534     t.join();
535     lock.writeLock().unlock();
536     } catch(Exception e){
537 dl 1.4 unexpectedException();
538 dl 1.2 }
539     }
540    
541 dl 1.4 /**
542     * Calling await without holding lock throws IllegalMonitorStateException
543     */
544 dl 1.2 public void testAwait_IllegalMonitor() {
545     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
546     final Condition c = lock.writeLock().newCondition();
547     try {
548     c.await();
549 dl 1.4 shouldThrow();
550 dl 1.2 }
551     catch (IllegalMonitorStateException success) {
552     }
553     catch (Exception ex) {
554 dl 1.4 shouldThrow();
555 dl 1.2 }
556     }
557    
558 dl 1.4 /**
559     * Calling signal without holding lock throws IllegalMonitorStateException
560     */
561 dl 1.2 public void testSignal_IllegalMonitor() {
562     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
563     final Condition c = lock.writeLock().newCondition();
564     try {
565     c.signal();
566 dl 1.4 shouldThrow();
567 dl 1.2 }
568     catch (IllegalMonitorStateException success) {
569     }
570     catch (Exception ex) {
571 dl 1.4 unexpectedException();
572 dl 1.2 }
573     }
574    
575 dl 1.4 /**
576     * awaitNanos without a signal times out
577     */
578 dl 1.2 public void testAwaitNanos_Timeout() {
579     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
580     final Condition c = lock.writeLock().newCondition();
581     try {
582     lock.writeLock().lock();
583     long t = c.awaitNanos(100);
584     assertTrue(t <= 0);
585     lock.writeLock().unlock();
586     }
587     catch (Exception ex) {
588 dl 1.4 unexpectedException();
589 dl 1.2 }
590     }
591    
592 dl 1.4
593     /**
594     * timed await without a signal times out
595     */
596 dl 1.2 public void testAwait_Timeout() {
597     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
598     final Condition c = lock.writeLock().newCondition();
599     try {
600     lock.writeLock().lock();
601     assertFalse(c.await(10, TimeUnit.MILLISECONDS));
602     lock.writeLock().unlock();
603     }
604     catch (Exception ex) {
605 dl 1.4 unexpectedException();
606 dl 1.2 }
607     }
608    
609 dl 1.4 /**
610     * awaitUntil without a signal times out
611     */
612 dl 1.2 public void testAwaitUntil_Timeout() {
613     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
614     final Condition c = lock.writeLock().newCondition();
615     try {
616     lock.writeLock().lock();
617     java.util.Date d = new java.util.Date();
618     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
619     lock.writeLock().unlock();
620     }
621     catch (Exception ex) {
622 dl 1.4 unexpectedException();
623 dl 1.2 }
624     }
625 dl 1.1
626 dl 1.4 /**
627     * await returns when signalled
628     */
629 dl 1.2 public void testAwait() {
630     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
631     final Condition c = lock.writeLock().newCondition();
632     Thread t = new Thread(new Runnable() {
633     public void run() {
634     try {
635     lock.writeLock().lock();
636     c.await();
637     lock.writeLock().unlock();
638     }
639     catch(InterruptedException e) {
640 dl 1.4 threadUnexpectedException();
641 dl 1.2 }
642     }
643     });
644    
645     try {
646     t.start();
647     Thread.sleep(SHORT_DELAY_MS);
648     lock.writeLock().lock();
649     c.signal();
650     lock.writeLock().unlock();
651     t.join(SHORT_DELAY_MS);
652     assertFalse(t.isAlive());
653     }
654     catch (Exception ex) {
655 dl 1.4 unexpectedException();
656 dl 1.2 }
657     }
658    
659 dl 1.4 /**
660     * awaitUninterruptibly doesn't abort on interrupt
661     */
662 dl 1.2 public void testAwaitUninterruptibly() {
663     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
664     final Condition c = lock.writeLock().newCondition();
665     Thread t = new Thread(new Runnable() {
666     public void run() {
667     lock.writeLock().lock();
668     c.awaitUninterruptibly();
669     lock.writeLock().unlock();
670     }
671     });
672    
673     try {
674     t.start();
675     Thread.sleep(SHORT_DELAY_MS);
676     t.interrupt();
677     lock.writeLock().lock();
678     c.signal();
679     lock.writeLock().unlock();
680 dl 1.4 assert(t.isInterrupted());
681 dl 1.2 t.join(SHORT_DELAY_MS);
682     assertFalse(t.isAlive());
683     }
684     catch (Exception ex) {
685 dl 1.4 unexpectedException();
686 dl 1.2 }
687     }
688    
689 dl 1.4 /**
690     * await is interruptible
691     */
692 dl 1.2 public void testAwait_Interrupt() {
693     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
694     final Condition c = lock.writeLock().newCondition();
695     Thread t = new Thread(new Runnable() {
696     public void run() {
697     try {
698     lock.writeLock().lock();
699     c.await();
700     lock.writeLock().unlock();
701 dl 1.4 threadShouldThrow();
702 dl 1.2 }
703     catch(InterruptedException success) {
704     }
705     }
706     });
707    
708     try {
709     t.start();
710     Thread.sleep(SHORT_DELAY_MS);
711     t.interrupt();
712     t.join(SHORT_DELAY_MS);
713     assertFalse(t.isAlive());
714     }
715     catch (Exception ex) {
716 dl 1.4 unexpectedException();
717 dl 1.2 }
718     }
719    
720 dl 1.4 /**
721     * awaitNanos is interruptible
722     */
723 dl 1.2 public void testAwaitNanos_Interrupt() {
724     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
725     final Condition c = lock.writeLock().newCondition();
726     Thread t = new Thread(new Runnable() {
727     public void run() {
728     try {
729     lock.writeLock().lock();
730     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
731     lock.writeLock().unlock();
732 dl 1.4 threadShouldThrow();
733 dl 1.2 }
734     catch(InterruptedException success) {
735     }
736     }
737     });
738    
739     try {
740     t.start();
741     Thread.sleep(SHORT_DELAY_MS);
742     t.interrupt();
743     t.join(SHORT_DELAY_MS);
744     assertFalse(t.isAlive());
745     }
746     catch (Exception ex) {
747 dl 1.4 unexpectedException();
748 dl 1.2 }
749     }
750 dl 1.1
751 dl 1.4 /**
752     * awaitUntil is interruptible
753     */
754 dl 1.2 public void testAwaitUntil_Interrupt() {
755 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
756 dl 1.2 final Condition c = lock.writeLock().newCondition();
757 dl 1.1 Thread t = new Thread(new Runnable() {
758     public void run() {
759     try {
760 dl 1.2 lock.writeLock().lock();
761     java.util.Date d = new java.util.Date();
762     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
763     lock.writeLock().unlock();
764 dl 1.4 threadShouldThrow();
765 dl 1.2 }
766     catch(InterruptedException success) {
767     }
768     }
769     });
770    
771     try {
772     t.start();
773     Thread.sleep(SHORT_DELAY_MS);
774     t.interrupt();
775     t.join(SHORT_DELAY_MS);
776     assertFalse(t.isAlive());
777     }
778     catch (Exception ex) {
779 dl 1.4 unexpectedException();
780 dl 1.2 }
781     }
782    
783 dl 1.4 /**
784     * signalAll wakes up all threads
785     */
786 dl 1.2 public void testSignalAll() {
787     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
788     final Condition c = lock.writeLock().newCondition();
789     Thread t1 = new Thread(new Runnable() {
790     public void run() {
791     try {
792     lock.writeLock().lock();
793     c.await();
794     lock.writeLock().unlock();
795     }
796     catch(InterruptedException e) {
797 dl 1.4 threadUnexpectedException();
798 dl 1.2 }
799     }
800     });
801    
802     Thread t2 = new Thread(new Runnable() {
803     public void run() {
804     try {
805     lock.writeLock().lock();
806     c.await();
807     lock.writeLock().unlock();
808 dl 1.1 }
809 dl 1.2 catch(InterruptedException e) {
810 dl 1.4 threadUnexpectedException();
811 dl 1.2 }
812 dl 1.1 }
813     });
814 dl 1.2
815     try {
816     t1.start();
817     t2.start();
818     Thread.sleep(SHORT_DELAY_MS);
819     lock.writeLock().lock();
820     c.signalAll();
821     lock.writeLock().unlock();
822     t1.join(SHORT_DELAY_MS);
823     t2.join(SHORT_DELAY_MS);
824     assertFalse(t1.isAlive());
825     assertFalse(t2.isAlive());
826     }
827     catch (Exception ex) {
828 dl 1.4 unexpectedException();
829 dl 1.2 }
830     }
831    
832 dl 1.4 /**
833     * A serialized lock deserializes as unlocked
834     */
835 dl 1.2 public void testSerialization() {
836     ReentrantReadWriteLock l = new ReentrantReadWriteLock();
837     l.readLock().lock();
838     l.readLock().unlock();
839    
840     try {
841     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
842     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
843     out.writeObject(l);
844     out.close();
845    
846     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
847     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
848     ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
849     r.readLock().lock();
850     r.readLock().unlock();
851     } catch(Exception e){
852     e.printStackTrace();
853 dl 1.4 unexpectedException();
854 dl 1.2 }
855 dl 1.1 }
856 dl 1.2
857 dl 1.6 /**
858     * getQueueLength reports number of waiting threads
859     */
860     public void testGetQueueLength() {
861     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
862     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
863     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
864     try {
865     assertEquals(0, lock.getQueueLength());
866     lock.writeLock().lock();
867     t1.start();
868     Thread.sleep(SHORT_DELAY_MS);
869     assertEquals(1, lock.getQueueLength());
870     t2.start();
871     Thread.sleep(SHORT_DELAY_MS);
872     assertEquals(2, lock.getQueueLength());
873     t1.interrupt();
874     Thread.sleep(SHORT_DELAY_MS);
875     assertEquals(1, lock.getQueueLength());
876     lock.writeLock().unlock();
877     Thread.sleep(SHORT_DELAY_MS);
878     assertEquals(0, lock.getQueueLength());
879     t1.join();
880     t2.join();
881     } catch(Exception e){
882     unexpectedException();
883     }
884     }
885    
886     /**
887     * getQueuedThreads includes waiting threads
888     */
889     public void testGetQueuedThreads() {
890     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
891     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
892     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
893     try {
894     assertTrue(lock.getQueuedThreads().isEmpty());
895     lock.writeLock().lock();
896     assertTrue(lock.getQueuedThreads().isEmpty());
897     t1.start();
898     Thread.sleep(SHORT_DELAY_MS);
899     assertTrue(lock.getQueuedThreads().contains(t1));
900     t2.start();
901     Thread.sleep(SHORT_DELAY_MS);
902     assertTrue(lock.getQueuedThreads().contains(t1));
903     assertTrue(lock.getQueuedThreads().contains(t2));
904     t1.interrupt();
905     Thread.sleep(SHORT_DELAY_MS);
906     assertFalse(lock.getQueuedThreads().contains(t1));
907     assertTrue(lock.getQueuedThreads().contains(t2));
908     lock.writeLock().unlock();
909     Thread.sleep(SHORT_DELAY_MS);
910     assertTrue(lock.getQueuedThreads().isEmpty());
911     t1.join();
912     t2.join();
913     } catch(Exception e){
914     unexpectedException();
915     }
916     }
917    
918     /**
919     * hasWaiters returns true when a thread is waiting, else false
920     */
921     public void testHasWaiters() {
922     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
923 dl 1.8 final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
924 dl 1.6 Thread t = new Thread(new Runnable() {
925     public void run() {
926     try {
927     lock.writeLock().lock();
928     threadAssertFalse(c.hasWaiters());
929     threadAssertEquals(0, c.getWaitQueueLength());
930     c.await();
931     lock.writeLock().unlock();
932     }
933     catch(InterruptedException e) {
934     threadUnexpectedException();
935     }
936     }
937     });
938    
939     try {
940     t.start();
941     Thread.sleep(SHORT_DELAY_MS);
942     lock.writeLock().lock();
943     assertTrue(c.hasWaiters());
944     assertEquals(1, c.getWaitQueueLength());
945     c.signal();
946     lock.writeLock().unlock();
947     Thread.sleep(SHORT_DELAY_MS);
948     lock.writeLock().lock();
949     assertFalse(c.hasWaiters());
950     assertEquals(0, c.getWaitQueueLength());
951     lock.writeLock().unlock();
952     t.join(SHORT_DELAY_MS);
953     assertFalse(t.isAlive());
954     }
955     catch (Exception ex) {
956     unexpectedException();
957     }
958     }
959    
960     /**
961     * getWaitQueueLength returns number of waiting threads
962     */
963     public void testGetWaitQueueLength() {
964     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
965 dl 1.8 final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
966 dl 1.6 Thread t1 = new Thread(new Runnable() {
967     public void run() {
968     try {
969     lock.writeLock().lock();
970     threadAssertFalse(c.hasWaiters());
971     threadAssertEquals(0, c.getWaitQueueLength());
972     c.await();
973     lock.writeLock().unlock();
974     }
975     catch(InterruptedException e) {
976     threadUnexpectedException();
977     }
978     }
979     });
980    
981     Thread t2 = new Thread(new Runnable() {
982     public void run() {
983     try {
984     lock.writeLock().lock();
985     threadAssertTrue(c.hasWaiters());
986     threadAssertEquals(1, c.getWaitQueueLength());
987     c.await();
988     lock.writeLock().unlock();
989     }
990     catch(InterruptedException e) {
991     threadUnexpectedException();
992     }
993     }
994     });
995    
996     try {
997     t1.start();
998     Thread.sleep(SHORT_DELAY_MS);
999     t2.start();
1000     Thread.sleep(SHORT_DELAY_MS);
1001     lock.writeLock().lock();
1002     assertTrue(c.hasWaiters());
1003     assertEquals(2, c.getWaitQueueLength());
1004     c.signalAll();
1005     lock.writeLock().unlock();
1006     Thread.sleep(SHORT_DELAY_MS);
1007     lock.writeLock().lock();
1008     assertFalse(c.hasWaiters());
1009     assertEquals(0, c.getWaitQueueLength());
1010     lock.writeLock().unlock();
1011     t1.join(SHORT_DELAY_MS);
1012     t2.join(SHORT_DELAY_MS);
1013     assertFalse(t1.isAlive());
1014     assertFalse(t2.isAlive());
1015     }
1016     catch (Exception ex) {
1017     unexpectedException();
1018     }
1019     }
1020    
1021     /**
1022     * getWaitingThreads returns only and all waiting threads
1023     */
1024     public void testGetWaitingThreads() {
1025     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1026     final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition();
1027     Thread t1 = new Thread(new Runnable() {
1028     public void run() {
1029     try {
1030     lock.writeLock().lock();
1031     threadAssertTrue(c.getWaitingThreads().isEmpty());
1032     c.await();
1033     lock.writeLock().unlock();
1034     }
1035     catch(InterruptedException e) {
1036     threadUnexpectedException();
1037     }
1038     }
1039     });
1040    
1041     Thread t2 = new Thread(new Runnable() {
1042     public void run() {
1043     try {
1044     lock.writeLock().lock();
1045     threadAssertFalse(c.getWaitingThreads().isEmpty());
1046     c.await();
1047     lock.writeLock().unlock();
1048     }
1049     catch(InterruptedException e) {
1050     threadUnexpectedException();
1051     }
1052     }
1053     });
1054    
1055     try {
1056     lock.writeLock().lock();
1057     assertTrue(c.getWaitingThreads().isEmpty());
1058     lock.writeLock().unlock();
1059     t1.start();
1060     Thread.sleep(SHORT_DELAY_MS);
1061     t2.start();
1062     Thread.sleep(SHORT_DELAY_MS);
1063     lock.writeLock().lock();
1064     assertTrue(c.hasWaiters());
1065     assertTrue(c.getWaitingThreads().contains(t1));
1066     assertTrue(c.getWaitingThreads().contains(t2));
1067     c.signalAll();
1068     lock.writeLock().unlock();
1069     Thread.sleep(SHORT_DELAY_MS);
1070     lock.writeLock().lock();
1071     assertFalse(c.hasWaiters());
1072     assertTrue(c.getWaitingThreads().isEmpty());
1073     lock.writeLock().unlock();
1074     t1.join(SHORT_DELAY_MS);
1075     t2.join(SHORT_DELAY_MS);
1076     assertFalse(t1.isAlive());
1077     assertFalse(t2.isAlive());
1078     }
1079     catch (Exception ex) {
1080     unexpectedException();
1081     }
1082     }
1083 dl 1.1
1084     }