ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.43
Committed: Sat May 7 03:36:23 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.42: +40 -3 lines
Log Message:
improve testAwait_Interrupt

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.39 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.26 * 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.31 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.5 import java.util.*;
14 dl 1.3 import java.io.*;
15 dl 1.1
16 dl 1.4 public class ReentrantLockTest extends JSR166TestCase {
17 dl 1.1 public static void main(String[] args) {
18 jsr166 1.36 junit.textui.TestRunner.run(suite());
19 dl 1.1 }
20     public static Test suite() {
21 jsr166 1.30 return new TestSuite(ReentrantLockTest.class);
22 dl 1.1 }
23    
24 dl 1.6 /**
25     * A runnable calling lockInterruptibly
26     */
27 jsr166 1.29 class InterruptibleLockRunnable extends CheckedRunnable {
28 dl 1.5 final ReentrantLock lock;
29     InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
30 jsr166 1.29 public void realRun() throws InterruptedException {
31     lock.lockInterruptibly();
32 dl 1.5 }
33     }
34    
35 dl 1.6
36     /**
37     * A runnable calling lockInterruptibly that expects to be
38     * interrupted
39     */
40 jsr166 1.29 class InterruptedLockRunnable extends CheckedInterruptedRunnable {
41 dl 1.5 final ReentrantLock lock;
42     InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 jsr166 1.29 public void realRun() throws InterruptedException {
44     lock.lockInterruptibly();
45 dl 1.5 }
46     }
47    
48     /**
49 dl 1.6 * Subclass to expose protected methods
50 dl 1.5 */
51 dl 1.6 static class PublicReentrantLock extends ReentrantLock {
52     PublicReentrantLock() { super(); }
53 jsr166 1.26 public Collection<Thread> getQueuedThreads() {
54     return super.getQueuedThreads();
55 dl 1.5 }
56 jsr166 1.26 public Collection<Thread> getWaitingThreads(Condition c) {
57     return super.getWaitingThreads(c);
58 dl 1.13 }
59 dl 1.5 }
60    
61 dl 1.8 /**
62 jsr166 1.43 * Checks that condition c has no waiters.
63     */
64     void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
65     assertHasWaiters(lock, c, new Thread[] {});
66     }
67    
68     /**
69     * Checks that condition c has exactly the given waiter threads.
70     */
71     void assertHasWaiters(PublicReentrantLock lock, Condition c,
72     Thread... threads) {
73     lock.lock();
74     assertEquals(threads.length > 0, lock.hasWaiters(c));
75     assertEquals(threads.length, lock.getWaitQueueLength(c));
76     assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
77     assertEquals(threads.length, lock.getWaitingThreads(c).size());
78     assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
79     new HashSet<Thread>(Arrays.asList(threads)));
80     lock.unlock();
81     }
82    
83     /**
84 dl 1.9 * Constructor sets given fairness
85     */
86 jsr166 1.26 public void testConstructor() {
87 jsr166 1.29 assertFalse(new ReentrantLock().isFair());
88     assertFalse(new ReentrantLock(false).isFair());
89     assertTrue(new ReentrantLock(true).isFair());
90 dl 1.9 }
91    
92     /**
93 dl 1.6 * locking an unlocked lock succeeds
94     */
95 jsr166 1.26 public void testLock() {
96 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
97 dl 1.6 rl.lock();
98     assertTrue(rl.isLocked());
99     rl.unlock();
100 jsr166 1.29 assertFalse(rl.isLocked());
101 dl 1.6 }
102    
103 dl 1.8 /**
104 dl 1.6 * locking an unlocked fair lock succeeds
105     */
106 jsr166 1.26 public void testFairLock() {
107 jsr166 1.30 ReentrantLock rl = new ReentrantLock(true);
108 dl 1.6 rl.lock();
109     assertTrue(rl.isLocked());
110     rl.unlock();
111     }
112    
113 dl 1.8 /**
114 dl 1.5 * Unlocking an unlocked lock throws IllegalMonitorStateException
115 dl 1.1 */
116 jsr166 1.26 public void testUnlock_IllegalMonitorStateException() {
117 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
118     try {
119     rl.unlock();
120     shouldThrow();
121     } catch (IllegalMonitorStateException success) {}
122 dl 1.5 }
123 dl 1.1
124 dl 1.8 /**
125 dl 1.15 * tryLock on an unlocked lock succeeds
126 dl 1.1 */
127 jsr166 1.26 public void testTryLock() {
128 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
129 dl 1.6 assertTrue(rl.tryLock());
130     assertTrue(rl.isLocked());
131     rl.unlock();
132     }
133    
134 dl 1.1
135 dl 1.8 /**
136 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
137     */
138 jsr166 1.29 public void testhasQueuedThreads() throws InterruptedException {
139 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
140 dl 1.13 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
141     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
142 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
143     lock.lock();
144     t1.start();
145 dl 1.40 delay(SHORT_DELAY_MS);
146 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
147     t2.start();
148 dl 1.40 delay(SHORT_DELAY_MS);
149 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
150     t1.interrupt();
151 dl 1.40 delay(SHORT_DELAY_MS);
152 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
153     lock.unlock();
154 dl 1.40 delay(SHORT_DELAY_MS);
155 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
156 jsr166 1.42 awaitTermination(t1);
157     awaitTermination(t2);
158 jsr166 1.26 }
159 dl 1.13
160     /**
161 dl 1.7 * getQueueLength reports number of waiting threads
162 dl 1.1 */
163 jsr166 1.29 public void testGetQueueLength() throws InterruptedException {
164 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
165 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
166     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
167 jsr166 1.29 assertEquals(0, lock.getQueueLength());
168     lock.lock();
169     t1.start();
170 dl 1.40 delay(SHORT_DELAY_MS);
171 jsr166 1.29 assertEquals(1, lock.getQueueLength());
172     t2.start();
173 dl 1.40 delay(SHORT_DELAY_MS);
174 jsr166 1.29 assertEquals(2, lock.getQueueLength());
175     t1.interrupt();
176 dl 1.40 delay(SHORT_DELAY_MS);
177 jsr166 1.29 assertEquals(1, lock.getQueueLength());
178     lock.unlock();
179 dl 1.40 delay(SHORT_DELAY_MS);
180 jsr166 1.29 assertEquals(0, lock.getQueueLength());
181 jsr166 1.42 awaitTermination(t1);
182     awaitTermination(t2);
183 jsr166 1.26 }
184 dl 1.5
185 dl 1.8 /**
186 dl 1.16 * getQueueLength reports number of waiting threads
187     */
188 jsr166 1.29 public void testGetQueueLength_fair() throws InterruptedException {
189 jsr166 1.30 final ReentrantLock lock = new ReentrantLock(true);
190 dl 1.16 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
191     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
192 jsr166 1.29 assertEquals(0, lock.getQueueLength());
193     lock.lock();
194     t1.start();
195 dl 1.40 delay(SHORT_DELAY_MS);
196 jsr166 1.29 assertEquals(1, lock.getQueueLength());
197     t2.start();
198 dl 1.40 delay(SHORT_DELAY_MS);
199 jsr166 1.29 assertEquals(2, lock.getQueueLength());
200     t1.interrupt();
201 dl 1.40 delay(SHORT_DELAY_MS);
202 jsr166 1.29 assertEquals(1, lock.getQueueLength());
203     lock.unlock();
204 dl 1.40 delay(SHORT_DELAY_MS);
205 jsr166 1.29 assertEquals(0, lock.getQueueLength());
206 jsr166 1.42 awaitTermination(t1);
207     awaitTermination(t2);
208 jsr166 1.26 }
209 dl 1.16
210     /**
211 dl 1.19 * hasQueuedThread(null) throws NPE
212     */
213 jsr166 1.26 public void testHasQueuedThreadNPE() {
214 jsr166 1.30 final ReentrantLock sync = new ReentrantLock();
215 dl 1.19 try {
216     sync.hasQueuedThread(null);
217     shouldThrow();
218 jsr166 1.29 } catch (NullPointerException success) {}
219 dl 1.19 }
220    
221     /**
222     * hasQueuedThread reports whether a thread is queued.
223     */
224 jsr166 1.29 public void testHasQueuedThread() throws InterruptedException {
225 jsr166 1.30 final ReentrantLock sync = new ReentrantLock();
226 dl 1.19 Thread t1 = new Thread(new InterruptedLockRunnable(sync));
227     Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
228 jsr166 1.29 assertFalse(sync.hasQueuedThread(t1));
229     assertFalse(sync.hasQueuedThread(t2));
230     sync.lock();
231     t1.start();
232 dl 1.40 delay(SHORT_DELAY_MS);
233 jsr166 1.29 assertTrue(sync.hasQueuedThread(t1));
234     t2.start();
235 dl 1.40 delay(SHORT_DELAY_MS);
236 jsr166 1.29 assertTrue(sync.hasQueuedThread(t1));
237     assertTrue(sync.hasQueuedThread(t2));
238     t1.interrupt();
239 dl 1.40 delay(SHORT_DELAY_MS);
240 jsr166 1.29 assertFalse(sync.hasQueuedThread(t1));
241     assertTrue(sync.hasQueuedThread(t2));
242     sync.unlock();
243 dl 1.40 delay(SHORT_DELAY_MS);
244 jsr166 1.29 assertFalse(sync.hasQueuedThread(t1));
245 dl 1.40 delay(SHORT_DELAY_MS);
246 jsr166 1.29 assertFalse(sync.hasQueuedThread(t2));
247 jsr166 1.42 awaitTermination(t1);
248     awaitTermination(t2);
249 jsr166 1.26 }
250 dl 1.19
251    
252     /**
253 dl 1.5 * getQueuedThreads includes waiting threads
254     */
255 jsr166 1.29 public void testGetQueuedThreads() throws InterruptedException {
256 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
257 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
258     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
259 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
260     lock.lock();
261     assertTrue(lock.getQueuedThreads().isEmpty());
262     t1.start();
263 dl 1.40 delay(SHORT_DELAY_MS);
264 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
265     t2.start();
266 dl 1.40 delay(SHORT_DELAY_MS);
267 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
268     assertTrue(lock.getQueuedThreads().contains(t2));
269     t1.interrupt();
270 dl 1.40 delay(SHORT_DELAY_MS);
271 jsr166 1.29 assertFalse(lock.getQueuedThreads().contains(t1));
272     assertTrue(lock.getQueuedThreads().contains(t2));
273     lock.unlock();
274 dl 1.40 delay(SHORT_DELAY_MS);
275 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
276 jsr166 1.42 awaitTermination(t1);
277     awaitTermination(t2);
278 jsr166 1.26 }
279 dl 1.5
280 dl 1.1
281 dl 1.8 /**
282 dl 1.15 * timed tryLock is interruptible.
283 dl 1.5 */
284 jsr166 1.29 public void testInterruptedException2() throws InterruptedException {
285 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
286     lock.lock();
287 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288 jsr166 1.29 public void realRun() throws InterruptedException {
289 jsr166 1.31 lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS);
290 jsr166 1.29 }});
291    
292 dl 1.40 delay(SHORT_DELAY_MS);
293 jsr166 1.29 t.interrupt();
294 jsr166 1.42 awaitTermination(t);
295 dl 1.1 }
296    
297 dl 1.3
298 dl 1.5 /**
299 dl 1.15 * TryLock on a locked lock fails
300 dl 1.5 */
301 jsr166 1.29 public void testTryLockWhenLocked() throws InterruptedException {
302 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
303     lock.lock();
304 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
305 jsr166 1.29 public void realRun() {
306 jsr166 1.37 assertFalse(lock.tryLock());
307 jsr166 1.29 }});
308    
309 jsr166 1.42 awaitTermination(t);
310 jsr166 1.29 lock.unlock();
311 jsr166 1.26 }
312 dl 1.3
313 dl 1.5 /**
314 dl 1.15 * Timed tryLock on a locked lock times out
315 dl 1.5 */
316 jsr166 1.29 public void testTryLock_Timeout() throws InterruptedException {
317 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
318     lock.lock();
319 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
320 jsr166 1.29 public void realRun() throws InterruptedException {
321 jsr166 1.37 assertFalse(lock.tryLock(1, MILLISECONDS));
322 jsr166 1.29 }});
323    
324 jsr166 1.42 awaitTermination(t);
325 jsr166 1.29 lock.unlock();
326 jsr166 1.26 }
327    
328 dl 1.5 /**
329     * getHoldCount returns number of recursive holds
330     */
331 dl 1.1 public void testGetHoldCount() {
332 jsr166 1.30 ReentrantLock lock = new ReentrantLock();
333     for (int i = 1; i <= SIZE; i++) {
334     lock.lock();
335     assertEquals(i, lock.getHoldCount());
336     }
337     for (int i = SIZE; i > 0; i--) {
338     lock.unlock();
339     assertEquals(i-1, lock.getHoldCount());
340     }
341 dl 1.1 }
342 jsr166 1.26
343    
344 dl 1.5 /**
345     * isLocked is true when locked and false when not
346     */
347 jsr166 1.29 public void testIsLocked() throws InterruptedException {
348 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
349     lock.lock();
350     assertTrue(lock.isLocked());
351     lock.unlock();
352     assertFalse(lock.isLocked());
353 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
354 jsr166 1.29 public void realRun() throws InterruptedException {
355     lock.lock();
356 dl 1.40 delay(SMALL_DELAY_MS);
357 jsr166 1.29 lock.unlock();
358     }});
359    
360 dl 1.40 delay(SHORT_DELAY_MS);
361 jsr166 1.29 assertTrue(lock.isLocked());
362 jsr166 1.42 awaitTermination(t);
363 jsr166 1.29 assertFalse(lock.isLocked());
364 dl 1.1 }
365    
366    
367 dl 1.8 /**
368 dl 1.6 * lockInterruptibly is interruptible.
369     */
370 jsr166 1.29 public void testLockInterruptibly1() throws InterruptedException {
371 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
372     lock.lock();
373 jsr166 1.41 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
374 dl 1.40 delay(SHORT_DELAY_MS);
375 jsr166 1.29 t.interrupt();
376 dl 1.40 delay(SHORT_DELAY_MS);
377 jsr166 1.29 lock.unlock();
378 jsr166 1.42 awaitTermination(t);
379 jsr166 1.26 }
380 dl 1.6
381 dl 1.5 /**
382     * lockInterruptibly succeeds when unlocked, else is interruptible
383     */
384 jsr166 1.29 public void testLockInterruptibly2() throws InterruptedException {
385 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
386 jsr166 1.29 lock.lockInterruptibly();
387 jsr166 1.41 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
388 dl 1.40 delay(SHORT_DELAY_MS);
389 jsr166 1.29 t.interrupt();
390     assertTrue(lock.isLocked());
391     assertTrue(lock.isHeldByCurrentThread());
392 jsr166 1.42 awaitTermination(t);
393 dl 1.1 }
394 dl 1.2
395 dl 1.6 /**
396     * Calling await without holding lock throws IllegalMonitorStateException
397     */
398 jsr166 1.29 public void testAwait_IllegalMonitor() throws InterruptedException {
399 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
400 dl 1.2 final Condition c = lock.newCondition();
401     try {
402     c.await();
403 dl 1.6 shouldThrow();
404 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
405 dl 1.2 }
406    
407 dl 1.6 /**
408     * Calling signal without holding lock throws IllegalMonitorStateException
409     */
410 dl 1.2 public void testSignal_IllegalMonitor() {
411 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
412 dl 1.2 final Condition c = lock.newCondition();
413     try {
414     c.signal();
415 dl 1.6 shouldThrow();
416 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
417 dl 1.2 }
418    
419 dl 1.6 /**
420     * awaitNanos without a signal times out
421     */
422 jsr166 1.29 public void testAwaitNanos_Timeout() throws InterruptedException {
423 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
424 dl 1.2 final Condition c = lock.newCondition();
425 jsr166 1.29 lock.lock();
426     long t = c.awaitNanos(100);
427     assertTrue(t <= 0);
428     lock.unlock();
429 dl 1.2 }
430    
431 dl 1.6 /**
432 jsr166 1.38 * timed await without a signal times out
433 dl 1.6 */
434 jsr166 1.29 public void testAwait_Timeout() throws InterruptedException {
435 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
436 dl 1.2 final Condition c = lock.newCondition();
437 jsr166 1.29 lock.lock();
438 jsr166 1.31 assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
439 jsr166 1.29 lock.unlock();
440 dl 1.2 }
441    
442 dl 1.6 /**
443     * awaitUntil without a signal times out
444     */
445 jsr166 1.29 public void testAwaitUntil_Timeout() throws InterruptedException {
446 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
447 dl 1.2 final Condition c = lock.newCondition();
448 jsr166 1.29 lock.lock();
449     java.util.Date d = new java.util.Date();
450     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
451     lock.unlock();
452 dl 1.2 }
453    
454 dl 1.6 /**
455     * await returns when signalled
456     */
457 jsr166 1.29 public void testAwait() throws InterruptedException {
458 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
459 dl 1.13 final Condition c = lock.newCondition();
460 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
461 jsr166 1.29 public void realRun() throws InterruptedException {
462     lock.lock();
463     c.await();
464     lock.unlock();
465     }});
466 dl 1.5
467 dl 1.40 delay(SHORT_DELAY_MS);
468 jsr166 1.29 lock.lock();
469     c.signal();
470     lock.unlock();
471 jsr166 1.42 awaitTermination(t);
472 dl 1.5 }
473    
474 dl 1.6 /**
475 dl 1.14 * hasWaiters throws NPE if null
476     */
477     public void testHasWaitersNPE() {
478 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
479 dl 1.14 try {
480     lock.hasWaiters(null);
481     shouldThrow();
482 jsr166 1.29 } catch (NullPointerException success) {}
483 dl 1.14 }
484    
485     /**
486     * getWaitQueueLength throws NPE if null
487     */
488     public void testGetWaitQueueLengthNPE() {
489 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
490 dl 1.14 try {
491     lock.getWaitQueueLength(null);
492     shouldThrow();
493 jsr166 1.29 } catch (NullPointerException success) {}
494 dl 1.14 }
495    
496    
497     /**
498     * getWaitingThreads throws NPE if null
499     */
500     public void testGetWaitingThreadsNPE() {
501 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
502 dl 1.14 try {
503     lock.getWaitingThreads(null);
504     shouldThrow();
505 jsr166 1.29 } catch (NullPointerException success) {}
506 dl 1.14 }
507    
508    
509     /**
510 dl 1.13 * hasWaiters throws IAE if not owned
511     */
512     public void testHasWaitersIAE() {
513 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
514 jsr166 1.29 final Condition c = lock.newCondition();
515 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
516 dl 1.13 try {
517     lock2.hasWaiters(c);
518     shouldThrow();
519 jsr166 1.29 } catch (IllegalArgumentException success) {}
520 dl 1.13 }
521    
522     /**
523     * hasWaiters throws IMSE if not locked
524     */
525     public void testHasWaitersIMSE() {
526 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
527 jsr166 1.29 final Condition c = lock.newCondition();
528 dl 1.13 try {
529     lock.hasWaiters(c);
530     shouldThrow();
531 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
532 dl 1.13 }
533    
534    
535     /**
536     * getWaitQueueLength throws IAE if not owned
537     */
538     public void testGetWaitQueueLengthIAE() {
539 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
540 jsr166 1.29 final Condition c = lock.newCondition();
541 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
542 dl 1.13 try {
543     lock2.getWaitQueueLength(c);
544     shouldThrow();
545 jsr166 1.29 } catch (IllegalArgumentException success) {}
546 dl 1.13 }
547    
548     /**
549     * getWaitQueueLength throws IMSE if not locked
550     */
551     public void testGetWaitQueueLengthIMSE() {
552 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
553 jsr166 1.29 final Condition c = lock.newCondition();
554 dl 1.13 try {
555     lock.getWaitQueueLength(c);
556     shouldThrow();
557 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
558 dl 1.13 }
559    
560    
561     /**
562     * getWaitingThreads throws IAE if not owned
563     */
564     public void testGetWaitingThreadsIAE() {
565 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
566 jsr166 1.29 final Condition c = lock.newCondition();
567 jsr166 1.30 final PublicReentrantLock lock2 = new PublicReentrantLock();
568 dl 1.13 try {
569     lock2.getWaitingThreads(c);
570     shouldThrow();
571 jsr166 1.29 } catch (IllegalArgumentException success) {}
572 dl 1.13 }
573    
574     /**
575     * getWaitingThreads throws IMSE if not locked
576     */
577     public void testGetWaitingThreadsIMSE() {
578 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
579 jsr166 1.29 final Condition c = lock.newCondition();
580 dl 1.13 try {
581     lock.getWaitingThreads(c);
582     shouldThrow();
583 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
584 dl 1.13 }
585    
586    
587     /**
588 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
589     */
590 jsr166 1.29 public void testHasWaiters() throws InterruptedException {
591 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
592 dl 1.13 final Condition c = lock.newCondition();
593 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
594 jsr166 1.29 public void realRun() throws InterruptedException {
595     lock.lock();
596 jsr166 1.37 assertFalse(lock.hasWaiters(c));
597     assertEquals(0, lock.getWaitQueueLength(c));
598 jsr166 1.29 c.await();
599     lock.unlock();
600     }});
601 dl 1.2
602 dl 1.40 delay(SHORT_DELAY_MS);
603 jsr166 1.29 lock.lock();
604     assertTrue(lock.hasWaiters(c));
605     assertEquals(1, lock.getWaitQueueLength(c));
606     c.signal();
607     lock.unlock();
608 dl 1.40 delay(SHORT_DELAY_MS);
609 jsr166 1.29 lock.lock();
610     assertFalse(lock.hasWaiters(c));
611     assertEquals(0, lock.getWaitQueueLength(c));
612     lock.unlock();
613 jsr166 1.42 awaitTermination(t);
614 dl 1.5 }
615    
616 dl 1.6 /**
617     * getWaitQueueLength returns number of waiting threads
618     */
619 jsr166 1.29 public void testGetWaitQueueLength() throws InterruptedException {
620 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
621 dl 1.13 final Condition c = lock.newCondition();
622 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
623 jsr166 1.29 public void realRun() throws InterruptedException {
624     lock.lock();
625 jsr166 1.37 assertFalse(lock.hasWaiters(c));
626     assertEquals(0, lock.getWaitQueueLength(c));
627 jsr166 1.29 c.await();
628     lock.unlock();
629     }});
630 dl 1.13
631 jsr166 1.41 delay(SHORT_DELAY_MS);
632    
633     Thread t2 = newStartedThread(new CheckedRunnable() {
634 jsr166 1.29 public void realRun() throws InterruptedException {
635     lock.lock();
636 jsr166 1.37 assertTrue(lock.hasWaiters(c));
637     assertEquals(1, lock.getWaitQueueLength(c));
638 jsr166 1.29 c.await();
639     lock.unlock();
640     }});
641    
642 dl 1.40 delay(SHORT_DELAY_MS);
643 jsr166 1.29 lock.lock();
644     assertTrue(lock.hasWaiters(c));
645     assertEquals(2, lock.getWaitQueueLength(c));
646     c.signalAll();
647     lock.unlock();
648 dl 1.40 delay(SHORT_DELAY_MS);
649 jsr166 1.29 lock.lock();
650     assertFalse(lock.hasWaiters(c));
651     assertEquals(0, lock.getWaitQueueLength(c));
652     lock.unlock();
653 jsr166 1.42 awaitTermination(t1);
654     awaitTermination(t2);
655 dl 1.13 }
656    
657     /**
658     * getWaitingThreads returns only and all waiting threads
659     */
660 jsr166 1.29 public void testGetWaitingThreads() throws InterruptedException {
661 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
662 dl 1.13 final Condition c = lock.newCondition();
663 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
664 jsr166 1.29 public void realRun() throws InterruptedException {
665     lock.lock();
666 jsr166 1.37 assertTrue(lock.getWaitingThreads(c).isEmpty());
667 jsr166 1.29 c.await();
668     lock.unlock();
669     }});
670    
671 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
672 jsr166 1.29 public void realRun() throws InterruptedException {
673     lock.lock();
674 jsr166 1.37 assertFalse(lock.getWaitingThreads(c).isEmpty());
675 jsr166 1.29 c.await();
676     lock.unlock();
677     }});
678 dl 1.5
679 jsr166 1.29 lock.lock();
680     assertTrue(lock.getWaitingThreads(c).isEmpty());
681     lock.unlock();
682     t1.start();
683 dl 1.40 delay(SHORT_DELAY_MS);
684 jsr166 1.29 t2.start();
685 dl 1.40 delay(SHORT_DELAY_MS);
686 jsr166 1.29 lock.lock();
687     assertTrue(lock.hasWaiters(c));
688     assertTrue(lock.getWaitingThreads(c).contains(t1));
689     assertTrue(lock.getWaitingThreads(c).contains(t2));
690     c.signalAll();
691     lock.unlock();
692 dl 1.40 delay(SHORT_DELAY_MS);
693 jsr166 1.29 lock.lock();
694     assertFalse(lock.hasWaiters(c));
695     assertTrue(lock.getWaitingThreads(c).isEmpty());
696     lock.unlock();
697 jsr166 1.42 awaitTermination(t1);
698     awaitTermination(t2);
699 dl 1.2 }
700 dl 1.13
701 dl 1.22 /** A helper class for uninterruptible wait tests */
702 jsr166 1.29 class UninterruptibleThread extends Thread {
703 dl 1.22 private ReentrantLock lock;
704     private Condition c;
705 jsr166 1.26
706 dl 1.22 public volatile boolean canAwake = false;
707     public volatile boolean interrupted = false;
708     public volatile boolean lockStarted = false;
709 jsr166 1.26
710 jsr166 1.29 public UninterruptibleThread(ReentrantLock lock, Condition c) {
711 dl 1.22 this.lock = lock;
712     this.c = c;
713     }
714 jsr166 1.26
715 dl 1.22 public synchronized void run() {
716     lock.lock();
717     lockStarted = true;
718 jsr166 1.26
719 dl 1.22 while (!canAwake) {
720     c.awaitUninterruptibly();
721     }
722 jsr166 1.26
723 dl 1.22 interrupted = isInterrupted();
724     lock.unlock();
725     }
726     }
727 dl 1.2
728 dl 1.6 /**
729     * awaitUninterruptibly doesn't abort on interrupt
730     */
731 jsr166 1.29 public void testAwaitUninterruptibly() throws InterruptedException {
732 dl 1.22 final ReentrantLock lock = new ReentrantLock();
733 dl 1.2 final Condition c = lock.newCondition();
734 jsr166 1.29 UninterruptibleThread thread = new UninterruptibleThread(lock, c);
735 dl 1.2
736 jsr166 1.29 thread.start();
737 dl 1.22
738 jsr166 1.29 while (!thread.lockStarted) {
739 dl 1.40 delay(100);
740 jsr166 1.29 }
741 dl 1.22
742 jsr166 1.29 lock.lock();
743     try {
744     thread.interrupt();
745     thread.canAwake = true;
746     c.signal();
747     } finally {
748     lock.unlock();
749     }
750 dl 1.22
751 jsr166 1.29 thread.join();
752     assertTrue(thread.interrupted);
753     assertFalse(thread.isAlive());
754 dl 1.2 }
755    
756 dl 1.6 /**
757     * await is interruptible
758     */
759 jsr166 1.29 public void testAwait_Interrupt() throws InterruptedException {
760 jsr166 1.43 final PublicReentrantLock lock = new PublicReentrantLock();
761 dl 1.2 final Condition c = lock.newCondition();
762 jsr166 1.43 final CountDownLatch locked = new CountDownLatch(1);
763 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
764 jsr166 1.30 public void realRun() throws InterruptedException {
765 jsr166 1.29 lock.lock();
766 jsr166 1.43 assertTrue(lock.isLocked());
767     assertTrue(lock.isHeldByCurrentThread());
768     assertHasNoWaiters(lock, c);
769     locked.countDown();
770     try {
771     c.await();
772     } finally {
773     assertTrue(lock.isLocked());
774     assertTrue(lock.isHeldByCurrentThread());
775     assertHasNoWaiters(lock, c);
776     lock.unlock();
777     assertFalse(Thread.interrupted());
778     }
779 jsr166 1.29 }});
780    
781 jsr166 1.43 locked.await();
782     assertHasWaiters(lock, c, t);
783 jsr166 1.29 t.interrupt();
784 jsr166 1.42 awaitTermination(t);
785 jsr166 1.43 assertFalse(lock.isLocked());
786 dl 1.2 }
787    
788 dl 1.6 /**
789     * awaitNanos is interruptible
790     */
791 jsr166 1.29 public void testAwaitNanos_Interrupt() throws InterruptedException {
792 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
793 dl 1.2 final Condition c = lock.newCondition();
794 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
795 jsr166 1.30 public void realRun() throws InterruptedException {
796 jsr166 1.29 lock.lock();
797 jsr166 1.33 c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
798 jsr166 1.29 }});
799    
800 dl 1.40 delay(SHORT_DELAY_MS);
801 jsr166 1.29 t.interrupt();
802 jsr166 1.42 awaitTermination(t);
803 dl 1.2 }
804    
805 dl 1.6 /**
806     * awaitUntil is interruptible
807     */
808 jsr166 1.29 public void testAwaitUntil_Interrupt() throws InterruptedException {
809 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
810 dl 1.2 final Condition c = lock.newCondition();
811 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
812 jsr166 1.30 public void realRun() throws InterruptedException {
813 jsr166 1.29 lock.lock();
814     java.util.Date d = new java.util.Date();
815     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
816     }});
817    
818 dl 1.40 delay(SHORT_DELAY_MS);
819 jsr166 1.29 t.interrupt();
820 jsr166 1.42 awaitTermination(t);
821 dl 1.2 }
822    
823 dl 1.6 /**
824     * signalAll wakes up all threads
825     */
826 jsr166 1.29 public void testSignalAll() throws InterruptedException {
827 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
828 dl 1.2 final Condition c = lock.newCondition();
829 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
830 jsr166 1.29 public void realRun() throws InterruptedException {
831     lock.lock();
832     c.await();
833     lock.unlock();
834     }});
835    
836 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
837 jsr166 1.29 public void realRun() throws InterruptedException {
838     lock.lock();
839     c.await();
840     lock.unlock();
841     }});
842 dl 1.2
843 dl 1.40 delay(SHORT_DELAY_MS);
844 jsr166 1.29 lock.lock();
845     c.signalAll();
846     lock.unlock();
847 jsr166 1.42 awaitTermination(t1);
848     awaitTermination(t2);
849 dl 1.3 }
850    
851 dl 1.6 /**
852 dl 1.23 * await after multiple reentrant locking preserves lock count
853     */
854 jsr166 1.29 public void testAwaitLockCount() throws InterruptedException {
855 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
856 dl 1.23 final Condition c = lock.newCondition();
857 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
858 jsr166 1.29 public void realRun() throws InterruptedException {
859     lock.lock();
860 jsr166 1.37 assertEquals(1, lock.getHoldCount());
861 jsr166 1.29 c.await();
862 jsr166 1.37 assertEquals(1, lock.getHoldCount());
863 jsr166 1.29 lock.unlock();
864     }});
865 dl 1.23
866 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
867 jsr166 1.29 public void realRun() throws InterruptedException {
868     lock.lock();
869     lock.lock();
870 jsr166 1.37 assertEquals(2, lock.getHoldCount());
871 jsr166 1.29 c.await();
872 jsr166 1.37 assertEquals(2, lock.getHoldCount());
873 jsr166 1.29 lock.unlock();
874     lock.unlock();
875     }});
876    
877 dl 1.40 delay(SHORT_DELAY_MS);
878 jsr166 1.29 lock.lock();
879     c.signalAll();
880     lock.unlock();
881 jsr166 1.42 awaitTermination(t1);
882     awaitTermination(t2);
883 dl 1.23 }
884    
885     /**
886 dl 1.6 * A serialized lock deserializes as unlocked
887     */
888 jsr166 1.29 public void testSerialization() throws Exception {
889 dl 1.3 ReentrantLock l = new ReentrantLock();
890     l.lock();
891     l.unlock();
892    
893 jsr166 1.29 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
894     ObjectOutputStream out =
895     new ObjectOutputStream(new BufferedOutputStream(bout));
896     out.writeObject(l);
897     out.close();
898 jsr166 1.35
899 jsr166 1.29 ByteArrayInputStream bin =
900     new ByteArrayInputStream(bout.toByteArray());
901     ObjectInputStream in =
902     new ObjectInputStream(new BufferedInputStream(bin));
903     ReentrantLock r = (ReentrantLock) in.readObject();
904     r.lock();
905     r.unlock();
906 dl 1.2 }
907 dl 1.1
908 dl 1.18 /**
909     * toString indicates current lock state
910     */
911     public void testToString() {
912     ReentrantLock lock = new ReentrantLock();
913     String us = lock.toString();
914     assertTrue(us.indexOf("Unlocked") >= 0);
915     lock.lock();
916     String ls = lock.toString();
917     assertTrue(ls.indexOf("Locked") >= 0);
918     }
919    
920 dl 1.1 }