ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.55
Committed: Mon May 2 01:15:26 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.54: +2 -0 lines
Log Message:
improve testReadAfterWriteLock

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