ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.53
Committed: Mon May 2 00:49:26 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.52: +47 -71 lines
Log Message:
replace Thread.join with awaitTermination(Thread,long)

File Contents

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