ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.48
Committed: Mon May 2 00:06:45 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.47: +1 -2 lines
Log Message:
Improve testReadLockInterruptibly_Interrupted

File Contents

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