ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.36
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +6 -5 lines
Log Message:
import static TimeUnit.MILLISECONDS

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