ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.45
Committed: Sun May 1 23:49:41 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.44: +40 -81 lines
Log Message:
use newStartedThread

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