ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.7 by dl, Thu Dec 4 20:54:46 2003 UTC vs.
Revision 1.19 by dl, Sun Jan 25 13:25:28 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 56 | Line 57 | public class ReentrantReadWriteLockTest
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 <        public PublicCondition newCondition() {
61 <            return new PublicCondition(this);
60 >        public Collection<Thread> getWaitingThreads(Condition c) {
61 >            return super.getWaitingThreads(c);
62          }
62
63        static class PublicCondition extends AbstractReentrantLock.ConditionObject {
64            PublicCondition(PublicReentrantReadWriteLock l) { super(l); }
65            public Collection<Thread> getWaitingThreads() {
66                return super.getWaitingThreads();
67            }
68        }
69
63      }
64  
65      /**
# Line 76 | Line 69 | public class ReentrantReadWriteLockTest
69          ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
70          assertFalse(rl.isFair());
71          assertFalse(rl.isWriteLocked());
72 <        assertEquals(0, rl.getReadLocks());
72 >        assertEquals(0, rl.getReadLockCount());
73          ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
74          assertTrue(r2.isFair());
75          assertFalse(r2.isWriteLocked());
76 <        assertEquals(0, r2.getReadLocks());
76 >        assertEquals(0, r2.getReadLockCount());
77      }
78  
79      /**
# Line 91 | Line 84 | public class ReentrantReadWriteLockTest
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
87 <        assertEquals(0, rl.getReadLocks());
87 >        assertEquals(0, rl.getReadLockCount());
88          rl.writeLock().unlock();
89          assertFalse(rl.isWriteLocked());
90          assertFalse(rl.isWriteLockedByCurrentThread());
91 <        assertEquals(0, rl.getReadLocks());
91 >        assertEquals(0, rl.getReadLockCount());
92          rl.readLock().lock();
93          assertFalse(rl.isWriteLocked());
94          assertFalse(rl.isWriteLockedByCurrentThread());
95 <        assertEquals(1, rl.getReadLocks());
95 >        assertEquals(1, rl.getReadLockCount());
96          rl.readLock().unlock();
97          assertFalse(rl.isWriteLocked());
98          assertFalse(rl.isWriteLockedByCurrentThread());
99 <        assertEquals(0, rl.getReadLocks());
99 >        assertEquals(0, rl.getReadLockCount());
100      }
101  
102  
# Line 115 | Line 108 | public class ReentrantReadWriteLockTest
108          rl.writeLock().lock();
109          assertTrue(rl.isWriteLocked());
110          assertTrue(rl.isWriteLockedByCurrentThread());
111 <        assertEquals(0, rl.getReadLocks());
111 >        assertEquals(0, rl.getReadLockCount());
112          rl.writeLock().unlock();
113          assertFalse(rl.isWriteLocked());
114          assertFalse(rl.isWriteLockedByCurrentThread());
115 <        assertEquals(0, rl.getReadLocks());
115 >        assertEquals(0, rl.getReadLockCount());
116          rl.readLock().lock();
117          assertFalse(rl.isWriteLocked());
118          assertFalse(rl.isWriteLockedByCurrentThread());
119 <        assertEquals(1, rl.getReadLocks());
119 >        assertEquals(1, rl.getReadLockCount());
120          rl.readLock().unlock();
121          assertFalse(rl.isWriteLocked());
122          assertFalse(rl.isWriteLockedByCurrentThread());
123 <        assertEquals(0, rl.getReadLocks());
123 >        assertEquals(0, rl.getReadLockCount());
124      }
125  
126      /**
# Line 163 | Line 156 | public class ReentrantReadWriteLockTest
156       */
157      public void testWriteLockInterruptibly_Interrupted() {
158          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
166        lock.writeLock().lock();
159          Thread t = new Thread(new Runnable() {
160                  public void run() {
161                      try {
162                          lock.writeLock().lockInterruptibly();
163 <                        threadShouldThrow();
163 >                        lock.writeLock().unlock();
164 >                        lock.writeLock().lockInterruptibly();
165 >                        lock.writeLock().unlock();
166                      } catch(InterruptedException success){}
167                  }
168              });
169          try {
170 +            lock.writeLock().lock();
171              t.start();
172              t.interrupt();
173              lock.writeLock().unlock();
# Line 183 | Line 178 | public class ReentrantReadWriteLockTest
178      }
179  
180      /**
181 <     * timed write-trylock is interruptible
181 >     * timed write-tryLock is interruptible
182       */
183      public void testWriteTryLock_Interrupted() {
184          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 192 | Line 187 | public class ReentrantReadWriteLockTest
187                  public void run() {
188                      try {
189                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
195                        threadShouldThrow();
190                      } catch(InterruptedException success){}
191                  }
192              });
# Line 216 | Line 210 | public class ReentrantReadWriteLockTest
210                  public void run() {
211                      try {
212                          lock.readLock().lockInterruptibly();
219                        threadShouldThrow();
213                      } catch(InterruptedException success){}
214                  }
215              });
# Line 231 | Line 224 | public class ReentrantReadWriteLockTest
224      }
225  
226      /**
227 <     * timed read-trylock is interruptible
227 >     * timed read-tryLock is interruptible
228       */
229      public void testReadTryLock_Interrupted() {
230          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 255 | Line 248 | public class ReentrantReadWriteLockTest
248  
249      
250      /**
251 <     * write-trylock fails if locked
251 >     * write-tryLock fails if locked
252       */
253      public void testWriteTryLockWhenLocked() {
254          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 275 | Line 268 | public class ReentrantReadWriteLockTest
268      }
269  
270      /**
271 <     * read-trylock fails if locked
271 >     * read-tryLock fails if locked
272       */
273      public void testReadTryLockWhenLocked() {
274          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 385 | Line 378 | public class ReentrantReadWriteLockTest
378  
379  
380      /**
381 <     * Read trylock succeeds if readlocked but not writelocked
381 >     * Read tryLock succeeds if readlocked but not writelocked
382       */
383      public void testTryLockWhenReadLocked() {
384          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 408 | Line 401 | public class ReentrantReadWriteLockTest
401      
402  
403      /**
404 <     * write trylock fails when readlocked
404 >     * write tryLock fails when readlocked
405       */
406      public void testWriteTryLockWhenReadLocked() {
407          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 430 | Line 423 | public class ReentrantReadWriteLockTest
423      
424  
425      /**
426 <     * write timed trylock times out if locked
426 >     * write timed tryLock times out if locked
427       */
428      public void testWriteTryLock_Timeout() {
429          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 454 | Line 447 | public class ReentrantReadWriteLockTest
447      }
448  
449      /**
450 <     * read timed trylock times out if write-locked
450 >     * read timed tryLock times out if write-locked
451       */
452      public void testReadTryLock_Timeout() {
453          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 855 | Line 848 | public class ReentrantReadWriteLockTest
848      }
849  
850      /**
851 +     * hasQueuedThreads reports whether there are waiting threads
852 +     */
853 +    public void testhasQueuedThreads() {
854 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
855 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
856 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
857 +        try {
858 +            assertFalse(lock.hasQueuedThreads());
859 +            lock.writeLock().lock();
860 +            t1.start();
861 +            Thread.sleep(SHORT_DELAY_MS);
862 +            assertTrue(lock.hasQueuedThreads());
863 +            t2.start();
864 +            Thread.sleep(SHORT_DELAY_MS);
865 +            assertTrue(lock.hasQueuedThreads());
866 +            t1.interrupt();
867 +            Thread.sleep(SHORT_DELAY_MS);
868 +            assertTrue(lock.hasQueuedThreads());
869 +            lock.writeLock().unlock();
870 +            Thread.sleep(SHORT_DELAY_MS);
871 +            assertFalse(lock.hasQueuedThreads());
872 +            t1.join();
873 +            t2.join();
874 +        } catch(Exception e){
875 +            unexpectedException();
876 +        }
877 +    }
878 +
879 +    /**
880 +     * hasQueuedThread(null) throws NPE
881 +     */
882 +    public void testHasQueuedThreadNPE() {
883 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
884 +        try {
885 +            sync.hasQueuedThread(null);
886 +            shouldThrow();
887 +        } catch (NullPointerException success) {
888 +        }
889 +    }
890 +
891 +    /**
892 +     * hasQueuedThread reports whether a thread is queued.
893 +     */
894 +    public void testHasQueuedThread() {
895 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
896 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
897 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
898 +        try {
899 +            assertFalse(sync.hasQueuedThread(t1));
900 +            assertFalse(sync.hasQueuedThread(t2));
901 +            sync.writeLock().lock();
902 +            t1.start();
903 +            Thread.sleep(SHORT_DELAY_MS);
904 +            assertTrue(sync.hasQueuedThread(t1));
905 +            t2.start();
906 +            Thread.sleep(SHORT_DELAY_MS);
907 +            assertTrue(sync.hasQueuedThread(t1));
908 +            assertTrue(sync.hasQueuedThread(t2));
909 +            t1.interrupt();
910 +            Thread.sleep(SHORT_DELAY_MS);
911 +            assertFalse(sync.hasQueuedThread(t1));
912 +            assertTrue(sync.hasQueuedThread(t2));
913 +            sync.writeLock().unlock();
914 +            Thread.sleep(SHORT_DELAY_MS);
915 +            assertFalse(sync.hasQueuedThread(t1));
916 +            Thread.sleep(SHORT_DELAY_MS);
917 +            assertFalse(sync.hasQueuedThread(t2));
918 +            t1.join();
919 +            t2.join();
920 +        } catch(Exception e){
921 +            unexpectedException();
922 +        }
923 +    }
924 +
925 +
926 +    /**
927       * getQueueLength reports number of waiting threads
928       */
929      public void testGetQueueLength() {
# Line 916 | Line 985 | public class ReentrantReadWriteLockTest
985      }
986  
987      /**
988 +     * hasWaiters throws NPE if null
989 +     */
990 +    public void testHasWaitersNPE() {
991 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
992 +        try {
993 +            lock.hasWaiters(null);
994 +            shouldThrow();
995 +        } catch (NullPointerException success) {
996 +        } catch (Exception ex) {
997 +            unexpectedException();
998 +        }
999 +    }
1000 +
1001 +    /**
1002 +     * getWaitQueueLength throws NPE if null
1003 +     */
1004 +    public void testGetWaitQueueLengthNPE() {
1005 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1006 +        try {
1007 +            lock.getWaitQueueLength(null);
1008 +            shouldThrow();
1009 +        } catch (NullPointerException success) {
1010 +        } catch (Exception ex) {
1011 +            unexpectedException();
1012 +        }
1013 +    }
1014 +
1015 +
1016 +    /**
1017 +     * getWaitingThreads throws NPE if null
1018 +     */
1019 +    public void testGetWaitingThreadsNPE() {
1020 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1021 +        try {
1022 +            lock.getWaitingThreads(null);
1023 +            shouldThrow();
1024 +        } catch (NullPointerException success) {
1025 +        } catch (Exception ex) {
1026 +            unexpectedException();
1027 +        }
1028 +    }
1029 +
1030 +    /**
1031 +     * hasWaiters throws IAE if not owned
1032 +     */
1033 +    public void testHasWaitersIAE() {
1034 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1035 +        final Condition c = (lock.writeLock().newCondition());
1036 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1037 +        try {
1038 +            lock2.hasWaiters(c);
1039 +            shouldThrow();
1040 +        } catch (IllegalArgumentException success) {
1041 +        } catch (Exception ex) {
1042 +            unexpectedException();
1043 +        }
1044 +    }
1045 +
1046 +    /**
1047 +     * hasWaiters throws IMSE if not locked
1048 +     */
1049 +    public void testHasWaitersIMSE() {
1050 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1051 +        final Condition c = (lock.writeLock().newCondition());
1052 +        try {
1053 +            lock.hasWaiters(c);
1054 +            shouldThrow();
1055 +        } catch (IllegalMonitorStateException success) {
1056 +        } catch (Exception ex) {
1057 +            unexpectedException();
1058 +        }
1059 +    }
1060 +
1061 +
1062 +    /**
1063 +     * getWaitQueueLength throws IAE if not owned
1064 +     */
1065 +    public void testGetWaitQueueLengthIAE() {
1066 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1067 +        final Condition c = (lock.writeLock().newCondition());
1068 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1069 +        try {
1070 +            lock2.getWaitQueueLength(c);
1071 +            shouldThrow();
1072 +        } catch (IllegalArgumentException success) {
1073 +        } catch (Exception ex) {
1074 +            unexpectedException();
1075 +        }
1076 +    }
1077 +
1078 +    /**
1079 +     * getWaitQueueLength throws IMSE if not locked
1080 +     */
1081 +    public void testGetWaitQueueLengthIMSE() {
1082 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1083 +        final Condition c = (lock.writeLock().newCondition());
1084 +        try {
1085 +            lock.getWaitQueueLength(c);
1086 +            shouldThrow();
1087 +        } catch (IllegalMonitorStateException success) {
1088 +        } catch (Exception ex) {
1089 +            unexpectedException();
1090 +        }
1091 +    }
1092 +
1093 +
1094 +    /**
1095 +     * getWaitingThreads throws IAE if not owned
1096 +     */
1097 +    public void testGetWaitingThreadsIAE() {
1098 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1099 +        final Condition c = (lock.writeLock().newCondition());
1100 +        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();  
1101 +        try {
1102 +            lock2.getWaitingThreads(c);
1103 +            shouldThrow();
1104 +        } catch (IllegalArgumentException success) {
1105 +        } catch (Exception ex) {
1106 +            unexpectedException();
1107 +        }
1108 +    }
1109 +
1110 +    /**
1111 +     * getWaitingThreads throws IMSE if not locked
1112 +     */
1113 +    public void testGetWaitingThreadsIMSE() {
1114 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1115 +        final Condition c = (lock.writeLock().newCondition());
1116 +        try {
1117 +            lock.getWaitingThreads(c);
1118 +            shouldThrow();
1119 +        } catch (IllegalMonitorStateException success) {
1120 +        } catch (Exception ex) {
1121 +            unexpectedException();
1122 +        }
1123 +    }
1124 +
1125 +
1126 +    /**
1127       * hasWaiters returns true when a thread is waiting, else false
1128       */
1129      public void testHasWaiters() {
1130 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1131 <        final AbstractReentrantLock.ConditionObject c = (AbstractReentrantLock.ConditionObject)(lock.writeLock().newCondition());
1130 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1131 >        final Condition c = (lock.writeLock().newCondition());
1132          Thread t = new Thread(new Runnable() {
1133                  public void run() {
1134                      try {
1135                          lock.writeLock().lock();
1136 <                        threadAssertFalse(c.hasWaiters());
1137 <                        threadAssertEquals(0, c.getWaitQueueLength());
1136 >                        threadAssertFalse(lock.hasWaiters(c));
1137 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1138                          c.await();
1139                          lock.writeLock().unlock();
1140                      }
# Line 940 | Line 1148 | public class ReentrantReadWriteLockTest
1148              t.start();
1149              Thread.sleep(SHORT_DELAY_MS);
1150              lock.writeLock().lock();
1151 <            assertTrue(c.hasWaiters());
1152 <            assertEquals(1, c.getWaitQueueLength());
1151 >            assertTrue(lock.hasWaiters(c));
1152 >            assertEquals(1, lock.getWaitQueueLength(c));
1153              c.signal();
1154              lock.writeLock().unlock();
1155              Thread.sleep(SHORT_DELAY_MS);
1156              lock.writeLock().lock();
1157 <            assertFalse(c.hasWaiters());
1158 <            assertEquals(0, c.getWaitQueueLength());
1157 >            assertFalse(lock.hasWaiters(c));
1158 >            assertEquals(0, lock.getWaitQueueLength(c));
1159              lock.writeLock().unlock();
1160              t.join(SHORT_DELAY_MS);
1161              assertFalse(t.isAlive());
# Line 961 | Line 1169 | public class ReentrantReadWriteLockTest
1169       * getWaitQueueLength returns number of waiting threads
1170       */
1171      public void testGetWaitQueueLength() {
1172 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1173 <        final AbstractReentrantLock.ConditionObject c = (AbstractReentrantLock.ConditionObject)(lock.writeLock().newCondition());
1174 <        Thread t1 = new Thread(new Runnable() {
967 <                public void run() {
968 <                    try {
969 <                        lock.writeLock().lock();
970 <                        threadAssertFalse(c.hasWaiters());
971 <                        threadAssertEquals(0, c.getWaitQueueLength());
972 <                        c.await();
973 <                        lock.writeLock().unlock();
974 <                    }
975 <                    catch(InterruptedException e) {
976 <                        threadUnexpectedException();
977 <                    }
978 <                }
979 <            });
980 <
981 <        Thread t2 = new Thread(new Runnable() {
1172 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1173 >        final Condition c = (lock.writeLock().newCondition());
1174 >        Thread t = new Thread(new Runnable() {
1175                  public void run() {
1176                      try {
1177                          lock.writeLock().lock();
1178 <                        threadAssertTrue(c.hasWaiters());
1179 <                        threadAssertEquals(1, c.getWaitQueueLength());
1178 >                        threadAssertFalse(lock.hasWaiters(c));
1179 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1180                          c.await();
1181                          lock.writeLock().unlock();
1182                      }
# Line 994 | Line 1187 | public class ReentrantReadWriteLockTest
1187              });
1188  
1189          try {
1190 <            t1.start();
998 <            Thread.sleep(SHORT_DELAY_MS);
999 <            t2.start();
1190 >            t.start();
1191              Thread.sleep(SHORT_DELAY_MS);
1192              lock.writeLock().lock();
1193 <            assertTrue(c.hasWaiters());
1194 <            assertEquals(2, c.getWaitQueueLength());
1195 <            c.signalAll();
1193 >            assertTrue(lock.hasWaiters(c));
1194 >            assertEquals(1, lock.getWaitQueueLength(c));
1195 >            c.signal();
1196              lock.writeLock().unlock();
1197              Thread.sleep(SHORT_DELAY_MS);
1198              lock.writeLock().lock();
1199 <            assertFalse(c.hasWaiters());
1200 <            assertEquals(0, c.getWaitQueueLength());
1199 >            assertFalse(lock.hasWaiters(c));
1200 >            assertEquals(0, lock.getWaitQueueLength(c));
1201              lock.writeLock().unlock();
1202 <            t1.join(SHORT_DELAY_MS);
1203 <            t2.join(SHORT_DELAY_MS);
1013 <            assertFalse(t1.isAlive());
1014 <            assertFalse(t2.isAlive());
1202 >            t.join(SHORT_DELAY_MS);
1203 >            assertFalse(t.isAlive());
1204          }
1205          catch (Exception ex) {
1206              unexpectedException();
1207          }
1208      }
1209  
1210 +
1211      /**
1212       * getWaitingThreads returns only and all waiting threads
1213       */
1214      public void testGetWaitingThreads() {
1215          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1216 <        final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition();
1216 >        final Condition c = lock.writeLock().newCondition();
1217          Thread t1 = new Thread(new Runnable() {
1218                  public void run() {
1219                      try {
1220                          lock.writeLock().lock();
1221 <                        threadAssertTrue(c.getWaitingThreads().isEmpty());
1221 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1222                          c.await();
1223                          lock.writeLock().unlock();
1224                      }
# Line 1042 | Line 1232 | public class ReentrantReadWriteLockTest
1232                  public void run() {
1233                      try {
1234                          lock.writeLock().lock();
1235 <                        threadAssertFalse(c.getWaitingThreads().isEmpty());
1235 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1236                          c.await();
1237                          lock.writeLock().unlock();
1238                      }
# Line 1054 | Line 1244 | public class ReentrantReadWriteLockTest
1244  
1245          try {
1246              lock.writeLock().lock();
1247 <            assertTrue(c.getWaitingThreads().isEmpty());
1247 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1248              lock.writeLock().unlock();
1249              t1.start();
1250              Thread.sleep(SHORT_DELAY_MS);
1251              t2.start();
1252              Thread.sleep(SHORT_DELAY_MS);
1253              lock.writeLock().lock();
1254 <            assertTrue(c.hasWaiters());
1255 <            assertTrue(c.getWaitingThreads().contains(t1));
1256 <            assertTrue(c.getWaitingThreads().contains(t2));
1254 >            assertTrue(lock.hasWaiters(c));
1255 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
1256 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
1257              c.signalAll();
1258              lock.writeLock().unlock();
1259              Thread.sleep(SHORT_DELAY_MS);
1260              lock.writeLock().lock();
1261 <            assertFalse(c.hasWaiters());
1262 <            assertTrue(c.getWaitingThreads().isEmpty());
1261 >            assertFalse(lock.hasWaiters(c));
1262 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1263              lock.writeLock().unlock();
1264              t1.join(SHORT_DELAY_MS);
1265              t2.join(SHORT_DELAY_MS);
# Line 1081 | Line 1271 | public class ReentrantReadWriteLockTest
1271          }
1272      }
1273  
1274 +    /**
1275 +     * toString indicates current lock state
1276 +     */
1277 +    public void testToString() {
1278 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1279 +        String us = lock.toString();
1280 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1281 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1282 +        lock.writeLock().lock();
1283 +        String ws = lock.toString();
1284 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1285 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1286 +        lock.writeLock().unlock();
1287 +        lock.readLock().lock();
1288 +        lock.readLock().lock();
1289 +        String rs = lock.toString();
1290 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1291 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1292 +    }
1293 +
1294 +    /**
1295 +     * readLock.toString indicates current lock state
1296 +     */
1297 +    public void testReadLockToString() {
1298 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1299 +        String us = lock.readLock().toString();
1300 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1301 +        lock.readLock().lock();
1302 +        lock.readLock().lock();
1303 +        String rs = lock.readLock().toString();
1304 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1305 +    }
1306 +
1307 +    /**
1308 +     * writeLock.toString indicates current lock state
1309 +     */
1310 +    public void testWriteLockToString() {
1311 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1312 +        String us = lock.writeLock().toString();
1313 +        assertTrue(us.indexOf("Unlocked") >= 0);
1314 +        lock.writeLock().lock();
1315 +        String ls = lock.writeLock().toString();
1316 +        assertTrue(ls.indexOf("Locked") >= 0);
1317 +    }
1318 +
1319   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines