ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.42
Committed: Sat May 7 03:12:48 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.41: +29 -42 lines
Log Message:
use awaitTermination

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