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.15 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.22 by dl, Tue May 3 16:02:00 2005 UTC

# Line 176 | Line 176 | public class ReentrantLockTest extends J
176      }
177  
178      /**
179 +     * getQueueLength reports number of waiting threads
180 +     */
181 +    public void testGetQueueLength_fair() {
182 +        final ReentrantLock lock = new ReentrantLock(true);
183 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
184 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
185 +        try {
186 +            assertEquals(0, lock.getQueueLength());
187 +            lock.lock();
188 +            t1.start();
189 +            Thread.sleep(SHORT_DELAY_MS);
190 +            assertEquals(1, lock.getQueueLength());
191 +            t2.start();
192 +            Thread.sleep(SHORT_DELAY_MS);
193 +            assertEquals(2, lock.getQueueLength());
194 +            t1.interrupt();
195 +            Thread.sleep(SHORT_DELAY_MS);
196 +            assertEquals(1, lock.getQueueLength());
197 +            lock.unlock();
198 +            Thread.sleep(SHORT_DELAY_MS);
199 +            assertEquals(0, lock.getQueueLength());
200 +            t1.join();
201 +            t2.join();
202 +        } catch(Exception e){
203 +            unexpectedException();
204 +        }
205 +    }
206 +
207 +    /**
208 +     * hasQueuedThread(null) throws NPE
209 +     */
210 +    public void testHasQueuedThreadNPE() {
211 +        final ReentrantLock sync = new ReentrantLock();
212 +        try {
213 +            sync.hasQueuedThread(null);
214 +            shouldThrow();
215 +        } catch (NullPointerException success) {
216 +        }
217 +    }
218 +
219 +    /**
220 +     * hasQueuedThread reports whether a thread is queued.
221 +     */
222 +    public void testHasQueuedThread() {
223 +        final ReentrantLock sync = new ReentrantLock();
224 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
225 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
226 +        try {
227 +            assertFalse(sync.hasQueuedThread(t1));
228 +            assertFalse(sync.hasQueuedThread(t2));
229 +            sync.lock();
230 +            t1.start();
231 +            Thread.sleep(SHORT_DELAY_MS);
232 +            assertTrue(sync.hasQueuedThread(t1));
233 +            t2.start();
234 +            Thread.sleep(SHORT_DELAY_MS);
235 +            assertTrue(sync.hasQueuedThread(t1));
236 +            assertTrue(sync.hasQueuedThread(t2));
237 +            t1.interrupt();
238 +            Thread.sleep(SHORT_DELAY_MS);
239 +            assertFalse(sync.hasQueuedThread(t1));
240 +            assertTrue(sync.hasQueuedThread(t2));
241 +            sync.unlock();
242 +            Thread.sleep(SHORT_DELAY_MS);
243 +            assertFalse(sync.hasQueuedThread(t1));
244 +            Thread.sleep(SHORT_DELAY_MS);
245 +            assertFalse(sync.hasQueuedThread(t2));
246 +            t1.join();
247 +            t2.join();
248 +        } catch(Exception e){
249 +            unexpectedException();
250 +        }
251 +    }
252 +
253 +
254 +    /**
255       * getQueuedThreads includes waiting threads
256       */
257      public void testGetQueuedThreads() {
# Line 422 | Line 498 | public class ReentrantLockTest extends J
498          final Condition c = lock.newCondition();
499          try {
500              lock.lock();
501 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
501 >            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
502              lock.unlock();
503          }
504          catch (Exception ex) {
# Line 439 | Line 515 | public class ReentrantLockTest extends J
515          try {
516              lock.lock();
517              java.util.Date d = new java.util.Date();
518 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
518 >            c.awaitUntil(new java.util.Date(d.getTime() + 10));
519              lock.unlock();
520          }
521          catch (Exception ex) {
# Line 787 | Line 863 | public class ReentrantLockTest extends J
863          }
864      }
865  
866 <
866 >    /** A helper class for uninterruptible wait tests */
867 >    class UninterruptableThread extends Thread {
868 >        private ReentrantLock lock;
869 >        private Condition c;
870 >        
871 >        public volatile boolean canAwake = false;
872 >        public volatile boolean interrupted = false;
873 >        public volatile boolean lockStarted = false;
874 >        
875 >        public UninterruptableThread(ReentrantLock lock, Condition c) {
876 >            this.lock = lock;
877 >            this.c = c;
878 >        }
879 >        
880 >        public synchronized void run() {
881 >            lock.lock();
882 >            lockStarted = true;
883 >            
884 >            while (!canAwake) {
885 >                c.awaitUninterruptibly();
886 >            }
887 >            
888 >            interrupted = isInterrupted();
889 >            lock.unlock();
890 >        }
891 >    }
892  
893      /**
894       * awaitUninterruptibly doesn't abort on interrupt
895       */
896      public void testAwaitUninterruptibly() {
897 <        final ReentrantLock lock = new ReentrantLock();
897 >        final ReentrantLock lock = new ReentrantLock();
898          final Condition c = lock.newCondition();
899 <        Thread t = new Thread(new Runnable() {
799 <                public void run() {
800 <                    lock.lock();
801 <                    c.awaitUninterruptibly();
802 <                    lock.unlock();
803 <                }
804 <            });
899 >        UninterruptableThread thread = new UninterruptableThread(lock, c);
900  
901          try {
902 <            t.start();
903 <            Thread.sleep(SHORT_DELAY_MS);
904 <            t.interrupt();
902 >            thread.start();
903 >
904 >            while (!thread.lockStarted) {
905 >                Thread.sleep(100);
906 >            }
907 >
908              lock.lock();
909 <            c.signal();
910 <            lock.unlock();
911 <            assert(t.isInterrupted());
912 <            t.join(SHORT_DELAY_MS);
913 <            assertFalse(t.isAlive());
914 <        }
915 <        catch (Exception ex) {
909 >            try {
910 >                thread.interrupt();
911 >                thread.canAwake = true;
912 >                c.signal();
913 >            } finally {
914 >                lock.unlock();
915 >            }
916 >
917 >            thread.join();
918 >            assertTrue(thread.interrupted);
919 >            assertFalse(thread.isAlive());
920 >        } catch (Exception ex) {
921              unexpectedException();
922          }
923      }
# Line 860 | Line 963 | public class ReentrantLockTest extends J
963                  public void run() {
964                      try {
965                          lock.lock();
966 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
966 >                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
967                          lock.unlock();
968                          threadShouldThrow();
969                      }
# Line 987 | Line 1090 | public class ReentrantLockTest extends J
1090          }
1091      }
1092  
1093 +    /**
1094 +     * toString indicates current lock state
1095 +     */
1096 +    public void testToString() {
1097 +        ReentrantLock lock = new ReentrantLock();
1098 +        String us = lock.toString();
1099 +        assertTrue(us.indexOf("Unlocked") >= 0);
1100 +        lock.lock();
1101 +        String ls = lock.toString();
1102 +        assertTrue(ls.indexOf("Locked") >= 0);
1103 +    }
1104 +
1105   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines