ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.38
Committed: Sat Nov 21 21:59:50 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +2 -1 lines
Log Message:
Fix rare failures in testWriteTryLock_Interrupted

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