ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.14 by dl, Sun Dec 28 22:44:59 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines