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.25 by dl, Thu May 18 10:29:23 2006 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 333 | Line 409 | public class ReentrantLockTest extends J
409          Thread t = new Thread(new InterruptedLockRunnable(lock));
410          try {
411              t.start();
412 +            Thread.sleep(SHORT_DELAY_MS);
413              t.interrupt();
414 +            Thread.sleep(SHORT_DELAY_MS);
415              lock.unlock();
416              t.join();
417          } catch(Exception e){
# Line 422 | Line 500 | public class ReentrantLockTest extends J
500          final Condition c = lock.newCondition();
501          try {
502              lock.lock();
503 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
503 >            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
504              lock.unlock();
505          }
506          catch (Exception ex) {
# Line 439 | Line 517 | public class ReentrantLockTest extends J
517          try {
518              lock.lock();
519              java.util.Date d = new java.util.Date();
520 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
520 >            c.awaitUntil(new java.util.Date(d.getTime() + 10));
521              lock.unlock();
522          }
523          catch (Exception ex) {
# Line 787 | Line 865 | public class ReentrantLockTest extends J
865          }
866      }
867  
868 <
868 >    /** A helper class for uninterruptible wait tests */
869 >    class UninterruptableThread extends Thread {
870 >        private ReentrantLock lock;
871 >        private Condition c;
872 >        
873 >        public volatile boolean canAwake = false;
874 >        public volatile boolean interrupted = false;
875 >        public volatile boolean lockStarted = false;
876 >        
877 >        public UninterruptableThread(ReentrantLock lock, Condition c) {
878 >            this.lock = lock;
879 >            this.c = c;
880 >        }
881 >        
882 >        public synchronized void run() {
883 >            lock.lock();
884 >            lockStarted = true;
885 >            
886 >            while (!canAwake) {
887 >                c.awaitUninterruptibly();
888 >            }
889 >            
890 >            interrupted = isInterrupted();
891 >            lock.unlock();
892 >        }
893 >    }
894  
895      /**
896       * awaitUninterruptibly doesn't abort on interrupt
897       */
898      public void testAwaitUninterruptibly() {
899 <        final ReentrantLock lock = new ReentrantLock();
899 >        final ReentrantLock lock = new ReentrantLock();
900          final Condition c = lock.newCondition();
901 <        Thread t = new Thread(new Runnable() {
799 <                public void run() {
800 <                    lock.lock();
801 <                    c.awaitUninterruptibly();
802 <                    lock.unlock();
803 <                }
804 <            });
901 >        UninterruptableThread thread = new UninterruptableThread(lock, c);
902  
903          try {
904 <            t.start();
905 <            Thread.sleep(SHORT_DELAY_MS);
906 <            t.interrupt();
904 >            thread.start();
905 >
906 >            while (!thread.lockStarted) {
907 >                Thread.sleep(100);
908 >            }
909 >
910              lock.lock();
911 <            c.signal();
912 <            lock.unlock();
913 <            assert(t.isInterrupted());
914 <            t.join(SHORT_DELAY_MS);
915 <            assertFalse(t.isAlive());
916 <        }
917 <        catch (Exception ex) {
911 >            try {
912 >                thread.interrupt();
913 >                thread.canAwake = true;
914 >                c.signal();
915 >            } finally {
916 >                lock.unlock();
917 >            }
918 >
919 >            thread.join();
920 >            assertTrue(thread.interrupted);
921 >            assertFalse(thread.isAlive());
922 >        } catch (Exception ex) {
923              unexpectedException();
924          }
925      }
# Line 860 | Line 965 | public class ReentrantLockTest extends J
965                  public void run() {
966                      try {
967                          lock.lock();
968 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
968 >                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
969                          lock.unlock();
970                          threadShouldThrow();
971                      }
# Line 963 | Line 1068 | public class ReentrantLockTest extends J
1068      }
1069  
1070      /**
1071 +     * await after multiple reentrant locking preserves lock count
1072 +     */
1073 +    public void testAwaitLockCount() {
1074 +        final ReentrantLock lock = new ReentrantLock();
1075 +        final Condition c = lock.newCondition();
1076 +        Thread t1 = new Thread(new Runnable() {
1077 +                public void run() {
1078 +                    try {
1079 +                        lock.lock();
1080 +                        threadAssertEquals(1, lock.getHoldCount());
1081 +                        c.await();
1082 +                        threadAssertEquals(1, lock.getHoldCount());
1083 +                        lock.unlock();
1084 +                    }
1085 +                    catch(InterruptedException e) {
1086 +                        threadUnexpectedException();
1087 +                    }
1088 +                }
1089 +            });
1090 +
1091 +        Thread t2 = new Thread(new Runnable() {
1092 +                public void run() {
1093 +                    try {
1094 +                        lock.lock();
1095 +                        lock.lock();
1096 +                        threadAssertEquals(2, lock.getHoldCount());
1097 +                        c.await();
1098 +                        threadAssertEquals(2, lock.getHoldCount());
1099 +                        lock.unlock();
1100 +                        lock.unlock();
1101 +                    }
1102 +                    catch(InterruptedException e) {
1103 +                        threadUnexpectedException();
1104 +                    }
1105 +                }
1106 +            });
1107 +
1108 +        try {
1109 +            t1.start();
1110 +            t2.start();
1111 +            Thread.sleep(SHORT_DELAY_MS);
1112 +            lock.lock();
1113 +            c.signalAll();
1114 +            lock.unlock();
1115 +            t1.join(SHORT_DELAY_MS);
1116 +            t2.join(SHORT_DELAY_MS);
1117 +            assertFalse(t1.isAlive());
1118 +            assertFalse(t2.isAlive());
1119 +        }
1120 +        catch (Exception ex) {
1121 +            unexpectedException();
1122 +        }
1123 +    }
1124 +
1125 +    /**
1126       * A serialized lock deserializes as unlocked
1127       */
1128      public void testSerialization() {
# Line 987 | Line 1147 | public class ReentrantLockTest extends J
1147          }
1148      }
1149  
1150 +    /**
1151 +     * toString indicates current lock state
1152 +     */
1153 +    public void testToString() {
1154 +        ReentrantLock lock = new ReentrantLock();
1155 +        String us = lock.toString();
1156 +        assertTrue(us.indexOf("Unlocked") >= 0);
1157 +        lock.lock();
1158 +        String ls = lock.toString();
1159 +        assertTrue(ls.indexOf("Locked") >= 0);
1160 +    }
1161 +
1162   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines