ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.41
Committed: Sat May 7 02:51:33 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.40: +19 -35 lines
Log Message:
use newStartedThread

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     t1.join();
135     t2.join();
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     t1.join();
160     t2.join();
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     t1.join();
185     t2.join();
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     t1.join();
226     t2.join();
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     t1.join();
255     t2.join();
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     t.join();
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     t.join();
288     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     t.join();
303     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     t.join();
341     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     t.join();
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     t.join();
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     t.join(SHORT_DELAY_MS);
450     assertFalse(t.isAlive());
451 dl 1.5 }
452    
453 dl 1.6 /**
454 dl 1.14 * hasWaiters throws NPE if null
455     */
456     public void testHasWaitersNPE() {
457 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
458 dl 1.14 try {
459     lock.hasWaiters(null);
460     shouldThrow();
461 jsr166 1.29 } catch (NullPointerException success) {}
462 dl 1.14 }
463    
464     /**
465     * getWaitQueueLength throws NPE if null
466     */
467     public void testGetWaitQueueLengthNPE() {
468 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
469 dl 1.14 try {
470     lock.getWaitQueueLength(null);
471     shouldThrow();
472 jsr166 1.29 } catch (NullPointerException success) {}
473 dl 1.14 }
474    
475    
476     /**
477     * getWaitingThreads throws NPE if null
478     */
479     public void testGetWaitingThreadsNPE() {
480 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
481 dl 1.14 try {
482     lock.getWaitingThreads(null);
483     shouldThrow();
484 jsr166 1.29 } catch (NullPointerException success) {}
485 dl 1.14 }
486    
487    
488     /**
489 dl 1.13 * hasWaiters throws IAE if not owned
490     */
491     public void testHasWaitersIAE() {
492 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
493 jsr166 1.29 final Condition c = lock.newCondition();
494 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
495 dl 1.13 try {
496     lock2.hasWaiters(c);
497     shouldThrow();
498 jsr166 1.29 } catch (IllegalArgumentException success) {}
499 dl 1.13 }
500    
501     /**
502     * hasWaiters throws IMSE if not locked
503     */
504     public void testHasWaitersIMSE() {
505 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
506 jsr166 1.29 final Condition c = lock.newCondition();
507 dl 1.13 try {
508     lock.hasWaiters(c);
509     shouldThrow();
510 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
511 dl 1.13 }
512    
513    
514     /**
515     * getWaitQueueLength throws IAE if not owned
516     */
517     public void testGetWaitQueueLengthIAE() {
518 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
519 jsr166 1.29 final Condition c = lock.newCondition();
520 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
521 dl 1.13 try {
522     lock2.getWaitQueueLength(c);
523     shouldThrow();
524 jsr166 1.29 } catch (IllegalArgumentException success) {}
525 dl 1.13 }
526    
527     /**
528     * getWaitQueueLength throws IMSE if not locked
529     */
530     public void testGetWaitQueueLengthIMSE() {
531 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
532 jsr166 1.29 final Condition c = lock.newCondition();
533 dl 1.13 try {
534     lock.getWaitQueueLength(c);
535     shouldThrow();
536 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
537 dl 1.13 }
538    
539    
540     /**
541     * getWaitingThreads throws IAE if not owned
542     */
543     public void testGetWaitingThreadsIAE() {
544 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
545 jsr166 1.29 final Condition c = lock.newCondition();
546 jsr166 1.30 final PublicReentrantLock lock2 = new PublicReentrantLock();
547 dl 1.13 try {
548     lock2.getWaitingThreads(c);
549     shouldThrow();
550 jsr166 1.29 } catch (IllegalArgumentException success) {}
551 dl 1.13 }
552    
553     /**
554     * getWaitingThreads throws IMSE if not locked
555     */
556     public void testGetWaitingThreadsIMSE() {
557 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
558 jsr166 1.29 final Condition c = lock.newCondition();
559 dl 1.13 try {
560     lock.getWaitingThreads(c);
561     shouldThrow();
562 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
563 dl 1.13 }
564    
565    
566     /**
567 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
568     */
569 jsr166 1.29 public void testHasWaiters() throws InterruptedException {
570 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
571 dl 1.13 final Condition c = lock.newCondition();
572 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
573 jsr166 1.29 public void realRun() throws InterruptedException {
574     lock.lock();
575 jsr166 1.37 assertFalse(lock.hasWaiters(c));
576     assertEquals(0, lock.getWaitQueueLength(c));
577 jsr166 1.29 c.await();
578     lock.unlock();
579     }});
580 dl 1.2
581 dl 1.40 delay(SHORT_DELAY_MS);
582 jsr166 1.29 lock.lock();
583     assertTrue(lock.hasWaiters(c));
584     assertEquals(1, lock.getWaitQueueLength(c));
585     c.signal();
586     lock.unlock();
587 dl 1.40 delay(SHORT_DELAY_MS);
588 jsr166 1.29 lock.lock();
589     assertFalse(lock.hasWaiters(c));
590     assertEquals(0, lock.getWaitQueueLength(c));
591     lock.unlock();
592     t.join(SHORT_DELAY_MS);
593     assertFalse(t.isAlive());
594 dl 1.5 }
595    
596 dl 1.6 /**
597     * getWaitQueueLength returns number of waiting threads
598     */
599 jsr166 1.29 public void testGetWaitQueueLength() throws InterruptedException {
600 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
601 dl 1.13 final Condition c = lock.newCondition();
602 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
603 jsr166 1.29 public void realRun() throws InterruptedException {
604     lock.lock();
605 jsr166 1.37 assertFalse(lock.hasWaiters(c));
606     assertEquals(0, lock.getWaitQueueLength(c));
607 jsr166 1.29 c.await();
608     lock.unlock();
609     }});
610 dl 1.13
611 jsr166 1.41 delay(SHORT_DELAY_MS);
612    
613     Thread t2 = newStartedThread(new CheckedRunnable() {
614 jsr166 1.29 public void realRun() throws InterruptedException {
615     lock.lock();
616 jsr166 1.37 assertTrue(lock.hasWaiters(c));
617     assertEquals(1, lock.getWaitQueueLength(c));
618 jsr166 1.29 c.await();
619     lock.unlock();
620     }});
621    
622 dl 1.40 delay(SHORT_DELAY_MS);
623 jsr166 1.29 lock.lock();
624     assertTrue(lock.hasWaiters(c));
625     assertEquals(2, lock.getWaitQueueLength(c));
626     c.signalAll();
627     lock.unlock();
628 dl 1.40 delay(SHORT_DELAY_MS);
629 jsr166 1.29 lock.lock();
630     assertFalse(lock.hasWaiters(c));
631     assertEquals(0, lock.getWaitQueueLength(c));
632     lock.unlock();
633     t1.join(SHORT_DELAY_MS);
634     t2.join(SHORT_DELAY_MS);
635     assertFalse(t1.isAlive());
636     assertFalse(t2.isAlive());
637 dl 1.13 }
638    
639     /**
640     * getWaitingThreads returns only and all waiting threads
641     */
642 jsr166 1.29 public void testGetWaitingThreads() throws InterruptedException {
643 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
644 dl 1.13 final Condition c = lock.newCondition();
645 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
646 jsr166 1.29 public void realRun() throws InterruptedException {
647     lock.lock();
648 jsr166 1.37 assertTrue(lock.getWaitingThreads(c).isEmpty());
649 jsr166 1.29 c.await();
650     lock.unlock();
651     }});
652    
653 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
654 jsr166 1.29 public void realRun() throws InterruptedException {
655     lock.lock();
656 jsr166 1.37 assertFalse(lock.getWaitingThreads(c).isEmpty());
657 jsr166 1.29 c.await();
658     lock.unlock();
659     }});
660 dl 1.5
661 jsr166 1.29 lock.lock();
662     assertTrue(lock.getWaitingThreads(c).isEmpty());
663     lock.unlock();
664     t1.start();
665 dl 1.40 delay(SHORT_DELAY_MS);
666 jsr166 1.29 t2.start();
667 dl 1.40 delay(SHORT_DELAY_MS);
668 jsr166 1.29 lock.lock();
669     assertTrue(lock.hasWaiters(c));
670     assertTrue(lock.getWaitingThreads(c).contains(t1));
671     assertTrue(lock.getWaitingThreads(c).contains(t2));
672     c.signalAll();
673     lock.unlock();
674 dl 1.40 delay(SHORT_DELAY_MS);
675 jsr166 1.29 lock.lock();
676     assertFalse(lock.hasWaiters(c));
677     assertTrue(lock.getWaitingThreads(c).isEmpty());
678     lock.unlock();
679     t1.join(SHORT_DELAY_MS);
680     t2.join(SHORT_DELAY_MS);
681     assertFalse(t1.isAlive());
682     assertFalse(t2.isAlive());
683 dl 1.2 }
684 dl 1.13
685 dl 1.22 /** A helper class for uninterruptible wait tests */
686 jsr166 1.29 class UninterruptibleThread extends Thread {
687 dl 1.22 private ReentrantLock lock;
688     private Condition c;
689 jsr166 1.26
690 dl 1.22 public volatile boolean canAwake = false;
691     public volatile boolean interrupted = false;
692     public volatile boolean lockStarted = false;
693 jsr166 1.26
694 jsr166 1.29 public UninterruptibleThread(ReentrantLock lock, Condition c) {
695 dl 1.22 this.lock = lock;
696     this.c = c;
697     }
698 jsr166 1.26
699 dl 1.22 public synchronized void run() {
700     lock.lock();
701     lockStarted = true;
702 jsr166 1.26
703 dl 1.22 while (!canAwake) {
704     c.awaitUninterruptibly();
705     }
706 jsr166 1.26
707 dl 1.22 interrupted = isInterrupted();
708     lock.unlock();
709     }
710     }
711 dl 1.2
712 dl 1.6 /**
713     * awaitUninterruptibly doesn't abort on interrupt
714     */
715 jsr166 1.29 public void testAwaitUninterruptibly() throws InterruptedException {
716 dl 1.22 final ReentrantLock lock = new ReentrantLock();
717 dl 1.2 final Condition c = lock.newCondition();
718 jsr166 1.29 UninterruptibleThread thread = new UninterruptibleThread(lock, c);
719 dl 1.2
720 jsr166 1.29 thread.start();
721 dl 1.22
722 jsr166 1.29 while (!thread.lockStarted) {
723 dl 1.40 delay(100);
724 jsr166 1.29 }
725 dl 1.22
726 jsr166 1.29 lock.lock();
727     try {
728     thread.interrupt();
729     thread.canAwake = true;
730     c.signal();
731     } finally {
732     lock.unlock();
733     }
734 dl 1.22
735 jsr166 1.29 thread.join();
736     assertTrue(thread.interrupted);
737     assertFalse(thread.isAlive());
738 dl 1.2 }
739    
740 dl 1.6 /**
741     * await is interruptible
742     */
743 jsr166 1.29 public void testAwait_Interrupt() throws InterruptedException {
744 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
745 dl 1.2 final Condition c = lock.newCondition();
746 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
747 jsr166 1.30 public void realRun() throws InterruptedException {
748 jsr166 1.29 lock.lock();
749     c.await();
750     }});
751    
752 dl 1.40 delay(SHORT_DELAY_MS);
753 jsr166 1.29 t.interrupt();
754     t.join(SHORT_DELAY_MS);
755     assertFalse(t.isAlive());
756 dl 1.2 }
757    
758 dl 1.6 /**
759     * awaitNanos is interruptible
760     */
761 jsr166 1.29 public void testAwaitNanos_Interrupt() throws InterruptedException {
762 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
763 dl 1.2 final Condition c = lock.newCondition();
764 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
765 jsr166 1.30 public void realRun() throws InterruptedException {
766 jsr166 1.29 lock.lock();
767 jsr166 1.33 c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
768 jsr166 1.29 }});
769    
770 dl 1.40 delay(SHORT_DELAY_MS);
771 jsr166 1.29 t.interrupt();
772     t.join(SHORT_DELAY_MS);
773     assertFalse(t.isAlive());
774 dl 1.2 }
775    
776 dl 1.6 /**
777     * awaitUntil is interruptible
778     */
779 jsr166 1.29 public void testAwaitUntil_Interrupt() throws InterruptedException {
780 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
781 dl 1.2 final Condition c = lock.newCondition();
782 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
783 jsr166 1.30 public void realRun() throws InterruptedException {
784 jsr166 1.29 lock.lock();
785     java.util.Date d = new java.util.Date();
786     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
787     }});
788    
789 dl 1.40 delay(SHORT_DELAY_MS);
790 jsr166 1.29 t.interrupt();
791     t.join(SHORT_DELAY_MS);
792     assertFalse(t.isAlive());
793 dl 1.2 }
794    
795 dl 1.6 /**
796     * signalAll wakes up all threads
797     */
798 jsr166 1.29 public void testSignalAll() throws InterruptedException {
799 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
800 dl 1.2 final Condition c = lock.newCondition();
801 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
802 jsr166 1.29 public void realRun() throws InterruptedException {
803     lock.lock();
804     c.await();
805     lock.unlock();
806     }});
807    
808 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
809 jsr166 1.29 public void realRun() throws InterruptedException {
810     lock.lock();
811     c.await();
812     lock.unlock();
813     }});
814 dl 1.2
815 dl 1.40 delay(SHORT_DELAY_MS);
816 jsr166 1.29 lock.lock();
817     c.signalAll();
818     lock.unlock();
819     t1.join(SHORT_DELAY_MS);
820     t2.join(SHORT_DELAY_MS);
821     assertFalse(t1.isAlive());
822     assertFalse(t2.isAlive());
823 dl 1.3 }
824    
825 dl 1.6 /**
826 dl 1.23 * await after multiple reentrant locking preserves lock count
827     */
828 jsr166 1.29 public void testAwaitLockCount() throws InterruptedException {
829 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
830 dl 1.23 final Condition c = lock.newCondition();
831 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
832 jsr166 1.29 public void realRun() throws InterruptedException {
833     lock.lock();
834 jsr166 1.37 assertEquals(1, lock.getHoldCount());
835 jsr166 1.29 c.await();
836 jsr166 1.37 assertEquals(1, lock.getHoldCount());
837 jsr166 1.29 lock.unlock();
838     }});
839 dl 1.23
840 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
841 jsr166 1.29 public void realRun() throws InterruptedException {
842     lock.lock();
843     lock.lock();
844 jsr166 1.37 assertEquals(2, lock.getHoldCount());
845 jsr166 1.29 c.await();
846 jsr166 1.37 assertEquals(2, lock.getHoldCount());
847 jsr166 1.29 lock.unlock();
848     lock.unlock();
849     }});
850    
851 dl 1.40 delay(SHORT_DELAY_MS);
852 jsr166 1.29 lock.lock();
853     c.signalAll();
854     lock.unlock();
855     t1.join(SHORT_DELAY_MS);
856     t2.join(SHORT_DELAY_MS);
857     assertFalse(t1.isAlive());
858     assertFalse(t2.isAlive());
859 dl 1.23 }
860    
861     /**
862 dl 1.6 * A serialized lock deserializes as unlocked
863     */
864 jsr166 1.29 public void testSerialization() throws Exception {
865 dl 1.3 ReentrantLock l = new ReentrantLock();
866     l.lock();
867     l.unlock();
868    
869 jsr166 1.29 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
870     ObjectOutputStream out =
871     new ObjectOutputStream(new BufferedOutputStream(bout));
872     out.writeObject(l);
873     out.close();
874 jsr166 1.35
875 jsr166 1.29 ByteArrayInputStream bin =
876     new ByteArrayInputStream(bout.toByteArray());
877     ObjectInputStream in =
878     new ObjectInputStream(new BufferedInputStream(bin));
879     ReentrantLock r = (ReentrantLock) in.readObject();
880     r.lock();
881     r.unlock();
882 dl 1.2 }
883 dl 1.1
884 dl 1.18 /**
885     * toString indicates current lock state
886     */
887     public void testToString() {
888     ReentrantLock lock = new ReentrantLock();
889     String us = lock.toString();
890     assertTrue(us.indexOf("Unlocked") >= 0);
891     lock.lock();
892     String ls = lock.toString();
893     assertTrue(ls.indexOf("Locked") >= 0);
894     }
895    
896 dl 1.1 }