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.2 by dl, Sat Sep 6 19:36:05 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 TestCase {
13 <    static int HOLD_COUNT_TEST_LIMIT = 20;
14 <
14 > public class ReentrantLockTest extends JSR166TestCase {
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
18    
18      public static Test suite() {
19          return new TestSuite(ReentrantLockTest.class);
20      }
21  
22 <    private static long SHORT_DELAY_MS = 100;
23 <    private static long MEDIUM_DELAY_MS = 1000;
24 <    private static long LONG_DELAY_MS = 10000;
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
29 <     *
68 >     * Unlocking an unlocked lock throws IllegalMonitorStateException
69       */
31    
70      public void testIllegalMonitorStateException(){
71          ReentrantLock rl = new ReentrantLock();
72          try{
73              rl.unlock();
74              fail("Should of thown Illegal Monitor State Exception");
75  
76 <        }catch(IllegalMonitorStateException sucess){}
39 <
40 <
76 >        } catch(IllegalMonitorStateException success){}
77      }
78 <    
78 >
79      /*
80 <     * makes a lock, locks it, tries to aquire the lock in another thread
45 <     * interrupts that thread and waits for an interrupted Exception to
46 <     * be thrown.
80 >     * lockInterruptibly is interruptible.
81       */
48
82      public void testInterruptedException(){
83          final ReentrantLock lock = new ReentrantLock();
84          lock.lock();
85 <        Thread t = new Thread(new Runnable() {
86 <                public void run(){
87 <                    try{
88 <                        lock.lockInterruptibly();
89 <                        fail("should throw");
90 <                    }catch(InterruptedException sucess){}
91 <                }
92 <            });
93 <        t.start();
61 <        t.interrupt();
62 <        lock.unlock();
85 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
86 >        try {
87 >            t.start();
88 >            t.interrupt();
89 >            lock.unlock();
90 >            t.join();
91 >        } catch(Exception e){
92 >            fail("unexpected exception");
93 >        }
94      }
95  
96      /*
97 <     * tests for interrupted exception on a timed wait
67 <     *
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();
164          Thread t = new Thread(new Runnable() {
165                  public void run(){
166                      try{
167 <                        lock.tryLock(1000,TimeUnit.MILLISECONDS);
168 <                        fail("should throw");
169 <                    }catch(InterruptedException sucess){}
167 >                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
168 >                        threadFail("should throw");
169 >                    } catch(InterruptedException success){}
170                  }
171              });
172 <        t.start();
173 <        t.interrupt();
172 >        try {
173 >            t.start();
174 >            t.interrupt();
175 >        } catch(Exception e){
176 >            fail("unexpected exception");
177 >        }
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();
187 +        Thread t = new Thread(new Runnable() {
188 +                public void run(){
189 +                    threadAssertFalse(lock.tryLock());
190 +                }
191 +            });
192 +        try {
193 +            t.start();
194 +            t.join();
195 +            lock.unlock();
196 +        } catch(Exception e){
197 +            fail("unexpected exception");
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();
207 +        Thread t = new Thread(new Runnable() {
208 +                public void run(){
209 +                    try {
210 +                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
211 +                    } catch (Exception ex) {
212 +                        threadFail("unexpected exception");
213 +                    }
214 +                }
215 +            });
216 +        try {
217 +            t.start();
218 +            t.join();
219 +            lock.unlock();
220 +        } catch(Exception e){
221 +            fail("unexpected exception");
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 97 | Line 238 | public class ReentrantLockTest extends T
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 109 | Line 251 | public class ReentrantLockTest extends T
251                  public void run() {
252                      lock.lock();
253                      try {
254 <                        Thread.sleep(SHORT_DELAY_MS * 2);
254 >                        Thread.sleep(SMALL_DELAY_MS);
255                      }
256 <                    catch(Exception e) {}
256 >                    catch(Exception e) {
257 >                        threadFail("unexpected exception");
258 >                    }
259                      lock.unlock();
260                  }
261              });
# Line 124 | Line 268 | public class ReentrantLockTest extends T
268          } catch(Exception e){
269              fail("unexpected exception");
270          }
127
271      }
272  
130    /*
131     * 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
136     */
273  
274 <    public void testLockedInterruptibly() {
274 >    /**
275 >     * lockInterruptibly succeeds when unlocked, else is interruptible
276 >     */
277 >    public void testLockInterruptibly() {
278          final ReentrantLock lock = new ReentrantLock();
279 <        try {lock.lockInterruptibly();} catch(Exception e) {}
280 <        Thread t = new Thread(new Runnable() {
281 <                public void run() {
282 <                    try {
283 <                        lock.lockInterruptibly();
284 <                        fail("Failed to generate an Interrupted Exception");
285 <                    }
286 <                    catch(InterruptedException e) {}
287 <                }
288 <            });
289 <        t.start();
290 <        t.interrupt();
291 <        assertTrue(lock.isLocked());
292 <        assertTrue(lock.isHeldByCurrentThread());
279 >        try {
280 >            lock.lockInterruptibly();
281 >        } catch(Exception e) {
282 >            fail("unexpected exception");
283 >        }
284 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
285 >        try {
286 >            t.start();
287 >            t.interrupt();
288 >            assertTrue(lock.isLocked());
289 >            assertTrue(lock.isHeldByCurrentThread());
290 >            t.join();
291 >        } catch(Exception e){
292 >            fail("unexpected exception");
293 >        }
294      }
295  
296      public void testAwait_IllegalMonitor() {
# Line 200 | Line 340 | public class ReentrantLockTest extends T
340          final Condition c = lock.newCondition();
341          try {
342              lock.lock();
343 <            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
343 >            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
344              lock.unlock();
345          }
346          catch (Exception ex) {
# Line 224 | Line 364 | public class ReentrantLockTest extends T
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 233 | Line 373 | public class ReentrantLockTest extends T
373                          lock.unlock();
374                      }
375                      catch(InterruptedException e) {
376 <                        fail("unexpected exception");
376 >                        threadFail("unexpected exception");
377                      }
378                  }
379              });
# Line 252 | Line 392 | public class ReentrantLockTest extends T
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 +
552      public void testAwaitUninterruptibly() {
553          final ReentrantLock lock = new ReentrantLock();
554          final Condition c = lock.newCondition();
# Line 287 | Line 584 | public class ReentrantLockTest extends T
584                          lock.lock();
585                          c.await();
586                          lock.unlock();
587 <                        fail("should throw");
587 >                        threadFail("should throw");
588                      }
589                      catch(InterruptedException success) {
590                      }
# Line 315 | Line 612 | public class ReentrantLockTest extends T
612                          lock.lock();
613                          c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
614                          lock.unlock();
615 <                        fail("should throw");
615 >                        threadFail("should throw");
616                      }
617                      catch(InterruptedException success) {
618                      }
# Line 344 | Line 641 | public class ReentrantLockTest extends T
641                          java.util.Date d = new java.util.Date();
642                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
643                          lock.unlock();
644 <                        fail("should throw");
644 >                        threadFail("should throw");
645                      }
646                      catch(InterruptedException success) {
647                      }
# Line 374 | Line 671 | public class ReentrantLockTest extends T
671                          lock.unlock();
672                      }
673                      catch(InterruptedException e) {
674 <                        fail("unexpected exception");
674 >                        threadFail("unexpected exception");
675                      }
676                  }
677              });
# Line 387 | Line 684 | public class ReentrantLockTest extends T
684                          lock.unlock();
685                      }
686                      catch(InterruptedException e) {
687 <                        fail("unexpected exception");
687 >                        threadFail("unexpected exception");
688                      }
689                  }
690              });
# Line 408 | Line 705 | public class ReentrantLockTest extends T
705              fail("unexpected exception");
706          }
707      }
708 +
709 +    public void testSerialization() {
710 +        ReentrantLock l = new ReentrantLock();
711 +        l.lock();
712 +        l.unlock();
713 +
714 +        try {
715 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
716 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
717 +            out.writeObject(l);
718 +            out.close();
719 +
720 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
721 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
722 +            ReentrantLock r = (ReentrantLock) in.readObject();
723 +            r.lock();
724 +            r.unlock();
725 +        } catch(Exception e){
726 +            e.printStackTrace();
727 +            fail("unexpected exception");
728 +        }
729 +    }
730  
731   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines