ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.54
Committed: Mon May 2 01:07:15 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.53: +15 -14 lines
Log Message:
rename releaseLock to releaseWriteLock

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