ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.43
Committed: Sat Oct 9 19:30:35 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +4 -4 lines
Log Message:
whitespace

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