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.7 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.11 by dl, Sat Dec 27 18:27:02 2003 UTC

# Line 56 | Line 56 | public class ReentrantLockTest extends J
56          public Collection<Thread> getQueuedThreads() {
57              return super.getQueuedThreads();
58          }
59        public ConditionObject newCondition() {
60            return new PublicCondition(this);
61        }
59  
60 <        static class PublicCondition extends ReentrantLock.ConditionObject {
64 <            PublicCondition(PublicReentrantLock l) { super(l); }
65 <            public Collection<Thread> getWaitingThreads() {
66 <                return super.getWaitingThreads();
67 <            }
68 <        }
60 >    }
61  
62 +    /**
63 +     * Constructor sets given fairness
64 +     */
65 +    public void testConstructor() {
66 +        ReentrantLock rl = new ReentrantLock();
67 +        assertFalse(rl.isFair());
68 +        ReentrantLock r2 = new ReentrantLock(true);
69 +        assertTrue(r2.isFair());
70      }
71  
72 <    /*
72 >    /**
73       * locking an unlocked lock succeeds
74       */
75      public void testLock() {
# Line 79 | Line 79 | public class ReentrantLockTest extends J
79          rl.unlock();
80      }
81  
82 <    /*
82 >    /**
83       * locking an unlocked fair lock succeeds
84       */
85      public void testFairLock() {
# Line 89 | Line 89 | public class ReentrantLockTest extends J
89          rl.unlock();
90      }
91  
92 <    /*
92 >    /**
93       * Unlocking an unlocked lock throws IllegalMonitorStateException
94       */
95      public void testUnlock_IllegalMonitorStateException() {
# Line 101 | Line 101 | public class ReentrantLockTest extends J
101          } catch(IllegalMonitorStateException success){}
102      }
103  
104 <    /*
104 >    /**
105       * trylock on an unlocked lock succeeds
106       */
107      public void testTryLock() {
# Line 112 | Line 112 | public class ReentrantLockTest extends J
112      }
113  
114  
115 <    /*
115 >    /**
116       * getQueueLength reports number of waiting threads
117       */
118      public void testGetQueueLength() {
# Line 141 | Line 141 | public class ReentrantLockTest extends J
141          }
142      }
143  
144 <    /*
144 >    /**
145       * getQueuedThreads includes waiting threads
146       */
147      public void testGetQueuedThreads() {
# Line 174 | Line 174 | public class ReentrantLockTest extends J
174      }
175  
176  
177 <    /*
177 >    /**
178       * timed trylock is interruptible.
179       */
180      public void testInterruptedException2() {
# Line 290 | Line 290 | public class ReentrantLockTest extends J
290      }
291  
292  
293 <    /*
293 >    /**
294       * lockInterruptibly is interruptible.
295       */
296      public void testLockInterruptibly1() {
# Line 418 | Line 418 | public class ReentrantLockTest extends J
418       */
419      public void testAwait() {
420          final ReentrantLock lock = new ReentrantLock();
421 <        final ReentrantLock.ConditionObject c = lock.newCondition();
421 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
422          Thread t = new Thread(new Runnable() {
423                  public void run() {
424                      try {
# Line 451 | Line 451 | public class ReentrantLockTest extends J
451       */
452      public void testHasWaiters() {
453          final ReentrantLock lock = new ReentrantLock();
454 <        final ReentrantLock.ConditionObject c = lock.newCondition();
454 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
455          Thread t = new Thread(new Runnable() {
456                  public void run() {
457                      try {
# Line 493 | Line 493 | public class ReentrantLockTest extends J
493       */
494      public void testGetWaitQueueLength() {
495          final ReentrantLock lock = new ReentrantLock();
496 <        final ReentrantLock.ConditionObject c = lock.newCondition();
496 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
497          Thread t1 = new Thread(new Runnable() {
498                  public void run() {
499                      try {
# Line 541 | Line 541 | public class ReentrantLockTest extends J
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            unexpectedException();
549        }
550    }
551
552    /**
553     * getWaitingThreads returns only and all waiting threads
554     */
555    public void testGetWaitingThreads() {
556        final PublicReentrantLock lock = new PublicReentrantLock();    
557        final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition();
558        Thread t1 = new Thread(new Runnable() {
559                public void run() {
560                    try {
561                        lock.lock();
562                        threadAssertTrue(c.getWaitingThreads().isEmpty());
563                        c.await();
564                        lock.unlock();
565                    }
566                    catch(InterruptedException e) {
567                        threadUnexpectedException();
568                    }
569                }
570            });
571
572        Thread t2 = new Thread(new Runnable() {
573                public void run() {
574                    try {
575                        lock.lock();
576                        threadAssertFalse(c.getWaitingThreads().isEmpty());
577                        c.await();
578                        lock.unlock();
579                    }
580                    catch(InterruptedException e) {
581                        threadUnexpectedException();
582                    }
583                }
584            });
585
586        try {
587            lock.lock();
588            assertTrue(c.getWaitingThreads().isEmpty());
589            lock.unlock();
590            t1.start();
591            Thread.sleep(SHORT_DELAY_MS);
592            t2.start();
593            Thread.sleep(SHORT_DELAY_MS);
594            lock.lock();
595            assertTrue(c.hasWaiters());
596            assertTrue(c.getWaitingThreads().contains(t1));
597            assertTrue(c.getWaitingThreads().contains(t2));
598            c.signalAll();
599            lock.unlock();
600            Thread.sleep(SHORT_DELAY_MS);
601            lock.lock();
602            assertFalse(c.hasWaiters());
603            assertTrue(c.getWaitingThreads().isEmpty());
604            lock.unlock();
605            t1.join(SHORT_DELAY_MS);
606            t2.join(SHORT_DELAY_MS);
544              assertFalse(t1.isAlive());
545              assertFalse(t2.isAlive());
546          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines