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

File Contents

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