ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.19
Committed: Sun Jan 25 13:25:28 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.18: +47 -0 lines
Log Message:
test hasQueuedThread

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