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.4 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Sat Sep 20 00:31:57 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.concurrent.locks.*;
10   import java.util.concurrent.*;
11 + import java.util.*;
12   import java.io.*;
13  
14   public class ReentrantLockTest extends JSR166TestCase {
14    static int HOLD_COUNT_TEST_LIMIT = 20;
15
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
19    
18      public static Test suite() {
19          return new TestSuite(ReentrantLockTest.class);
20      }
21  
22 +    static int HOLD_COUNT_TEST_LIMIT = 20;
23 +
24 +    class InterruptibleLockRunnable implements Runnable {
25 +        final ReentrantLock lock;
26 +        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
27 +        public void run(){
28 +            try{
29 +                lock.lockInterruptibly();
30 +            } catch(InterruptedException success){}
31 +        }
32 +    }
33 +
34 +    // Same, except must interrupt
35 +    class InterruptedLockRunnable implements Runnable {
36 +        final ReentrantLock lock;
37 +        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
38 +        public void run(){
39 +            try{
40 +                lock.lockInterruptibly();
41 +                threadFail("should throw");
42 +            } catch(InterruptedException success){}
43 +        }
44 +    }
45 +
46 +    /**
47 +     * To expose protected methods
48 +     */
49 +    static class MyReentrantLock extends ReentrantLock {
50 +        MyReentrantLock() { super(); }
51 +        public Collection<Thread> getQueuedThreads() {
52 +            return super.getQueuedThreads();
53 +        }
54 +        public ConditionObject newCondition() {
55 +            return new MyCondition(this);
56 +        }
57 +
58 +        static class MyCondition extends ReentrantLock.ConditionObject {
59 +            MyCondition(MyReentrantLock l) { super(l); }
60 +            public Collection<Thread> getWaitingThreads() {
61 +                return super.getWaitingThreads();
62 +            }
63 +        }
64 +
65 +    }
66 +
67      /*
68 <     * Unlocks an unlocked lock, throws Illegal Monitor State
26 <     *
68 >     * Unlocking an unlocked lock throws IllegalMonitorStateException
69       */
70      public void testIllegalMonitorStateException(){
71          ReentrantLock rl = new ReentrantLock();
# Line 32 | Line 74 | public class ReentrantLockTest extends J
74              fail("Should of thown Illegal Monitor State Exception");
75  
76          } catch(IllegalMonitorStateException success){}
35
36
77      }
78 <    
78 >
79      /*
80 <     * makes a lock, locks it, tries to aquire the lock in another thread
41 <     * interrupts that thread and waits for an interrupted Exception to
42 <     * be thrown.
80 >     * lockInterruptibly is interruptible.
81       */
44
82      public void testInterruptedException(){
83          final ReentrantLock lock = new ReentrantLock();
84          lock.lock();
85 <        Thread t = new Thread(new Runnable() {
49 <                public void run(){
50 <                    try{
51 <                        lock.lockInterruptibly();
52 <                        threadFail("should throw");
53 <                    } catch(InterruptedException success){}
54 <                }
55 <            });
85 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
86          try {
87              t.start();
88              t.interrupt();
# Line 64 | Line 94 | public class ReentrantLockTest extends J
94      }
95  
96      /*
97 <     * tests for interrupted exception on a timed wait
68 <     *
97 >     * getLockQueueLength reports number of waiting threads
98       */
99 <    
99 >    public void testgetLockQueueLength(){
100 >        final ReentrantLock lock = new ReentrantLock();
101 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
102 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
103 >        try {
104 >            assertEquals(0, lock.getLockQueueLength());
105 >            lock.lock();
106 >            t1.start();
107 >            Thread.sleep(SHORT_DELAY_MS);
108 >            assertEquals(1, lock.getLockQueueLength());
109 >            t2.start();
110 >            Thread.sleep(SHORT_DELAY_MS);
111 >            assertEquals(2, lock.getLockQueueLength());
112 >            t1.interrupt();
113 >            Thread.sleep(SHORT_DELAY_MS);
114 >            assertEquals(1, lock.getLockQueueLength());
115 >            lock.unlock();
116 >            Thread.sleep(SHORT_DELAY_MS);
117 >            assertEquals(0, lock.getLockQueueLength());
118 >            t1.join();
119 >            t2.join();
120 >        } catch(Exception e){
121 >            fail("unexpected exception");
122 >        }
123 >    }
124  
125 +    /*
126 +     * getQueuedThreads includes waiting threads
127 +     */
128 +    public void testGetQueuedThreads(){
129 +        final MyReentrantLock lock = new MyReentrantLock();
130 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
132 +        try {
133 +            assertTrue(lock.getQueuedThreads().isEmpty());
134 +            lock.lock();
135 +            assertTrue(lock.getQueuedThreads().isEmpty());
136 +            t1.start();
137 +            Thread.sleep(SHORT_DELAY_MS);
138 +            assertTrue(lock.getQueuedThreads().contains(t1));
139 +            t2.start();
140 +            Thread.sleep(SHORT_DELAY_MS);
141 +            assertTrue(lock.getQueuedThreads().contains(t1));
142 +            assertTrue(lock.getQueuedThreads().contains(t2));
143 +            t1.interrupt();
144 +            Thread.sleep(SHORT_DELAY_MS);
145 +            assertFalse(lock.getQueuedThreads().contains(t1));
146 +            assertTrue(lock.getQueuedThreads().contains(t2));
147 +            lock.unlock();
148 +            Thread.sleep(SHORT_DELAY_MS);
149 +            assertTrue(lock.getQueuedThreads().isEmpty());
150 +            t1.join();
151 +            t2.join();
152 +        } catch(Exception e){
153 +            fail("unexpected exception");
154 +        }
155 +    }
156 +
157 +
158 +    /*
159 +     * timed trylock is interruptible.
160 +     */
161      public void testInterruptedException2(){
162          final ReentrantLock lock = new ReentrantLock();
163          lock.lock();
# Line 89 | Line 178 | public class ReentrantLockTest extends J
178      }
179  
180  
181 +    /**
182 +     * Trylock on a locked lock fails
183 +     */
184      public void testTryLockWhenLocked() {
185          final ReentrantLock lock = new ReentrantLock();
186          lock.lock();
# Line 106 | Line 198 | public class ReentrantLockTest extends J
198          }
199      }
200  
201 +    /**
202 +     * Timed Trylock on a locked lock times out
203 +     */
204      public void testTryLock_Timeout(){
205          final ReentrantLock lock = new ReentrantLock();
206          lock.lock();
# Line 127 | Line 222 | public class ReentrantLockTest extends J
222          }
223      }
224      
225 +    /**
226 +     * getHoldCount returns number of recursive holds
227 +     */
228      public void testGetHoldCount() {
229          ReentrantLock lock = new ReentrantLock();
230          for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
# Line 140 | Line 238 | public class ReentrantLockTest extends J
238      }
239      
240    
241 <
242 <
241 >    /**
242 >     * isLocked is true when locked and false when not
243 >     */
244      public void testIsLocked() {
245          final ReentrantLock lock = new ReentrantLock();
246          lock.lock();
# Line 172 | Line 271 | public class ReentrantLockTest extends J
271      }
272  
273  
274 +    /**
275 +     * lockInterruptibly succeeds when unlocked, else is interruptible
276 +     */
277      public void testLockInterruptibly() {
278          final ReentrantLock lock = new ReentrantLock();
279          try {
# Line 179 | Line 281 | public class ReentrantLockTest extends J
281          } catch(Exception e) {
282              fail("unexpected exception");
283          }
284 <        Thread t = new Thread(new Runnable() {
183 <                public void run() {
184 <                    try {
185 <                        lock.lockInterruptibly();
186 <                        threadFail("should throw");
187 <                    }
188 <                    catch(InterruptedException e) {}
189 <                }
190 <            });
284 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
285          try {
286              t.start();
287              t.interrupt();
# Line 270 | Line 364 | public class ReentrantLockTest extends J
364  
365      public void testAwait() {
366          final ReentrantLock lock = new ReentrantLock();
367 <        final Condition c = lock.newCondition();
367 >        final ReentrantLock.ConditionObject c = lock.newCondition();
368          Thread t = new Thread(new Runnable() {
369                  public void run() {
370                      try {
# Line 295 | Line 389 | public class ReentrantLockTest extends J
389          }
390          catch (Exception ex) {
391              fail("unexpected exception");
392 +        }
393 +    }
394 +
395 +    public void testHasWaiters() {
396 +        final ReentrantLock lock = new ReentrantLock();
397 +        final ReentrantLock.ConditionObject c = lock.newCondition();
398 +        Thread t = new Thread(new Runnable() {
399 +                public void run() {
400 +                    try {
401 +                        lock.lock();
402 +                        threadAssertFalse(c.hasWaiters());
403 +                        threadAssertEquals(0, c.getWaitQueueLength());
404 +                        c.await();
405 +                        lock.unlock();
406 +                    }
407 +                    catch(InterruptedException e) {
408 +                        threadFail("unexpected exception");
409 +                    }
410 +                }
411 +            });
412 +
413 +        try {
414 +            t.start();
415 +            Thread.sleep(SHORT_DELAY_MS);
416 +            lock.lock();
417 +            assertTrue(c.hasWaiters());
418 +            assertEquals(1, c.getWaitQueueLength());
419 +            c.signal();
420 +            lock.unlock();
421 +            Thread.sleep(SHORT_DELAY_MS);
422 +            lock.lock();
423 +            assertFalse(c.hasWaiters());
424 +            assertEquals(0, c.getWaitQueueLength());
425 +            lock.unlock();
426 +            t.join(SHORT_DELAY_MS);
427 +            assertFalse(t.isAlive());
428 +        }
429 +        catch (Exception ex) {
430 +            fail("unexpected exception");
431 +        }
432 +    }
433 +
434 +    public void testGetWaitQueueLength() {
435 +        final ReentrantLock lock = new ReentrantLock();
436 +        final ReentrantLock.ConditionObject c = lock.newCondition();
437 +        Thread t1 = new Thread(new Runnable() {
438 +                public void run() {
439 +                    try {
440 +                        lock.lock();
441 +                        threadAssertFalse(c.hasWaiters());
442 +                        threadAssertEquals(0, c.getWaitQueueLength());
443 +                        c.await();
444 +                        lock.unlock();
445 +                    }
446 +                    catch(InterruptedException e) {
447 +                        threadFail("unexpected exception");
448 +                    }
449 +                }
450 +            });
451 +
452 +        Thread t2 = new Thread(new Runnable() {
453 +                public void run() {
454 +                    try {
455 +                        lock.lock();
456 +                        threadAssertTrue(c.hasWaiters());
457 +                        threadAssertEquals(1, c.getWaitQueueLength());
458 +                        c.await();
459 +                        lock.unlock();
460 +                    }
461 +                    catch(InterruptedException e) {
462 +                        threadFail("unexpected exception");
463 +                    }
464 +                }
465 +            });
466 +
467 +        try {
468 +            t1.start();
469 +            Thread.sleep(SHORT_DELAY_MS);
470 +            t2.start();
471 +            Thread.sleep(SHORT_DELAY_MS);
472 +            lock.lock();
473 +            assertTrue(c.hasWaiters());
474 +            assertEquals(2, c.getWaitQueueLength());
475 +            c.signalAll();
476 +            lock.unlock();
477 +            Thread.sleep(SHORT_DELAY_MS);
478 +            lock.lock();
479 +            assertFalse(c.hasWaiters());
480 +            assertEquals(0, c.getWaitQueueLength());
481 +            lock.unlock();
482 +            t1.join(SHORT_DELAY_MS);
483 +            t2.join(SHORT_DELAY_MS);
484 +            assertFalse(t1.isAlive());
485 +            assertFalse(t2.isAlive());
486 +        }
487 +        catch (Exception ex) {
488 +            fail("unexpected exception");
489 +        }
490 +    }
491 +
492 +    public void testGetWaitingThreads() {
493 +        final MyReentrantLock lock = new MyReentrantLock();    
494 +        final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition();
495 +        Thread t1 = new Thread(new Runnable() {
496 +                public void run() {
497 +                    try {
498 +                        lock.lock();
499 +                        threadAssertTrue(c.getWaitingThreads().isEmpty());
500 +                        c.await();
501 +                        lock.unlock();
502 +                    }
503 +                    catch(InterruptedException e) {
504 +                        threadFail("unexpected exception");
505 +                    }
506 +                }
507 +            });
508 +
509 +        Thread t2 = new Thread(new Runnable() {
510 +                public void run() {
511 +                    try {
512 +                        lock.lock();
513 +                        threadAssertFalse(c.getWaitingThreads().isEmpty());
514 +                        c.await();
515 +                        lock.unlock();
516 +                    }
517 +                    catch(InterruptedException e) {
518 +                        threadFail("unexpected exception");
519 +                    }
520 +                }
521 +            });
522 +
523 +        try {
524 +            lock.lock();
525 +            assertTrue(c.getWaitingThreads().isEmpty());
526 +            lock.unlock();
527 +            t1.start();
528 +            Thread.sleep(SHORT_DELAY_MS);
529 +            t2.start();
530 +            Thread.sleep(SHORT_DELAY_MS);
531 +            lock.lock();
532 +            assertTrue(c.hasWaiters());
533 +            assertTrue(c.getWaitingThreads().contains(t1));
534 +            assertTrue(c.getWaitingThreads().contains(t2));
535 +            c.signalAll();
536 +            lock.unlock();
537 +            Thread.sleep(SHORT_DELAY_MS);
538 +            lock.lock();
539 +            assertFalse(c.hasWaiters());
540 +            assertTrue(c.getWaitingThreads().isEmpty());
541 +            lock.unlock();
542 +            t1.join(SHORT_DELAY_MS);
543 +            t2.join(SHORT_DELAY_MS);
544 +            assertFalse(t1.isAlive());
545 +            assertFalse(t2.isAlive());
546 +        }
547 +        catch (Exception ex) {
548 +            fail("unexpected exception");
549          }
550      }
551  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines