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.12 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.21 by dl, Thu Jan 20 00:39:13 2005 UTC

# Line 57 | Line 57 | public class ReentrantReadWriteLockTest
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 +        public Collection<Thread> getWaitingThreads(Condition c) {
61 +            return super.getWaitingThreads(c);
62 +        }
63      }
64  
65      /**
# Line 153 | Line 156 | public class ReentrantReadWriteLockTest
156       */
157      public void testWriteLockInterruptibly_Interrupted() {
158          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
156        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 173 | 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 182 | Line 187 | public class ReentrantReadWriteLockTest
187                  public void run() {
188                      try {
189                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
185                        threadShouldThrow();
190                      } catch(InterruptedException success){}
191                  }
192              });
# Line 206 | Line 210 | public class ReentrantReadWriteLockTest
210                  public void run() {
211                      try {
212                          lock.readLock().lockInterruptibly();
209                        threadShouldThrow();
213                      } catch(InterruptedException success){}
214                  }
215              });
# Line 221 | 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 245 | 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 265 | 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 373 | Line 376 | public class ReentrantReadWriteLockTest
376          }
377      }
378  
379 +    /**
380 +     * Read trylock succeeds if write locked by current thread
381 +     */
382 +    public void testReadHoldingWriteLock() {
383 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 +        lock.writeLock().lock();
385 +        assertTrue(lock.readLock().tryLock());
386 +        lock.readLock().unlock();
387 +        lock.writeLock().unlock();
388 +    }
389 +
390 +    /**
391 +     * Read lock succeeds if write locked by current thread even if
392 +     * other threads are waiting
393 +     */
394 +    public void testReadHoldingWriteLock2() {
395 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
396 +        lock.writeLock().lock();
397 +        Thread t1 = new Thread(new Runnable() {
398 +                public void run() {
399 +                    lock.readLock().lock();
400 +                    lock.readLock().unlock();
401 +                }
402 +            });
403 +        Thread t2 = new Thread(new Runnable() {
404 +                public void run() {
405 +                    lock.readLock().lock();
406 +                    lock.readLock().unlock();
407 +                }
408 +            });
409 +
410 +        try {
411 +            t1.start();
412 +            t2.start();
413 +            lock.readLock().lock();
414 +            lock.readLock().unlock();
415 +            Thread.sleep(SHORT_DELAY_MS);
416 +            lock.readLock().lock();
417 +            lock.readLock().unlock();
418 +            lock.writeLock().unlock();
419 +            t1.join(MEDIUM_DELAY_MS);
420 +            t2.join(MEDIUM_DELAY_MS);
421 +            assertTrue(!t1.isAlive());
422 +            assertTrue(!t2.isAlive());
423 +          
424 +        } catch(Exception e){
425 +            unexpectedException();
426 +        }
427 +    }
428 +
429 +    /**
430 +     * Fair Read trylock succeeds if write locked by current thread
431 +     */
432 +    public void testReadHoldingWriteLockFair() {
433 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
434 +        lock.writeLock().lock();
435 +        assertTrue(lock.readLock().tryLock());
436 +        lock.readLock().unlock();
437 +        lock.writeLock().unlock();
438 +    }
439  
440      /**
441 <     * Read trylock succeeds if readlocked but not writelocked
441 >     * Fair Read lock succeeds if write locked by current thread even if
442 >     * other threads are waiting
443 >     */
444 >    public void testReadHoldingWriteLockFair2() {
445 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
446 >        lock.writeLock().lock();
447 >        Thread t1 = new Thread(new Runnable() {
448 >                public void run() {
449 >                    lock.readLock().lock();
450 >                    lock.readLock().unlock();
451 >                }
452 >            });
453 >        Thread t2 = new Thread(new Runnable() {
454 >                public void run() {
455 >                    lock.readLock().lock();
456 >                    lock.readLock().unlock();
457 >                }
458 >            });
459 >
460 >        try {
461 >            t1.start();
462 >            t2.start();
463 >            lock.readLock().lock();
464 >            lock.readLock().unlock();
465 >            Thread.sleep(SHORT_DELAY_MS);
466 >            lock.readLock().lock();
467 >            lock.readLock().unlock();
468 >            lock.writeLock().unlock();
469 >            t1.join(MEDIUM_DELAY_MS);
470 >            t2.join(MEDIUM_DELAY_MS);
471 >            assertTrue(!t1.isAlive());
472 >            assertTrue(!t2.isAlive());
473 >          
474 >        } catch(Exception e){
475 >            unexpectedException();
476 >        }
477 >    }
478 >
479 >
480 >    /**
481 >     * Read tryLock succeeds if readlocked but not writelocked
482       */
483      public void testTryLockWhenReadLocked() {
484          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 398 | Line 501 | public class ReentrantReadWriteLockTest
501      
502  
503      /**
504 <     * write trylock fails when readlocked
504 >     * write tryLock fails when readlocked
505       */
506      public void testWriteTryLockWhenReadLocked() {
507          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 420 | Line 523 | public class ReentrantReadWriteLockTest
523      
524  
525      /**
526 <     * write timed trylock times out if locked
526 >     * write timed tryLock times out if locked
527       */
528      public void testWriteTryLock_Timeout() {
529          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 444 | Line 547 | public class ReentrantReadWriteLockTest
547      }
548  
549      /**
550 <     * read timed trylock times out if write-locked
550 >     * read timed tryLock times out if write-locked
551       */
552      public void testReadTryLock_Timeout() {
553          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 588 | Line 691 | public class ReentrantReadWriteLockTest
691          final Condition c = lock.writeLock().newCondition();
692          try {
693              lock.writeLock().lock();
591            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
694              lock.writeLock().unlock();
695          }
696          catch (Exception ex) {
# Line 605 | Line 707 | public class ReentrantReadWriteLockTest
707          try {
708              lock.writeLock().lock();
709              java.util.Date d = new java.util.Date();
608            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
710              lock.writeLock().unlock();
711          }
712          catch (Exception ex) {
# Line 845 | Line 946 | public class ReentrantReadWriteLockTest
946      }
947  
948      /**
949 +     * hasQueuedThreads reports whether there are waiting threads
950 +     */
951 +    public void testhasQueuedThreads() {
952 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
953 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
954 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
955 +        try {
956 +            assertFalse(lock.hasQueuedThreads());
957 +            lock.writeLock().lock();
958 +            t1.start();
959 +            Thread.sleep(SHORT_DELAY_MS);
960 +            assertTrue(lock.hasQueuedThreads());
961 +            t2.start();
962 +            Thread.sleep(SHORT_DELAY_MS);
963 +            assertTrue(lock.hasQueuedThreads());
964 +            t1.interrupt();
965 +            Thread.sleep(SHORT_DELAY_MS);
966 +            assertTrue(lock.hasQueuedThreads());
967 +            lock.writeLock().unlock();
968 +            Thread.sleep(SHORT_DELAY_MS);
969 +            assertFalse(lock.hasQueuedThreads());
970 +            t1.join();
971 +            t2.join();
972 +        } catch(Exception e){
973 +            unexpectedException();
974 +        }
975 +    }
976 +
977 +    /**
978 +     * hasQueuedThread(null) throws NPE
979 +     */
980 +    public void testHasQueuedThreadNPE() {
981 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
982 +        try {
983 +            sync.hasQueuedThread(null);
984 +            shouldThrow();
985 +        } catch (NullPointerException success) {
986 +        }
987 +    }
988 +
989 +    /**
990 +     * hasQueuedThread reports whether a thread is queued.
991 +     */
992 +    public void testHasQueuedThread() {
993 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
994 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
995 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
996 +        try {
997 +            assertFalse(sync.hasQueuedThread(t1));
998 +            assertFalse(sync.hasQueuedThread(t2));
999 +            sync.writeLock().lock();
1000 +            t1.start();
1001 +            Thread.sleep(SHORT_DELAY_MS);
1002 +            assertTrue(sync.hasQueuedThread(t1));
1003 +            t2.start();
1004 +            Thread.sleep(SHORT_DELAY_MS);
1005 +            assertTrue(sync.hasQueuedThread(t1));
1006 +            assertTrue(sync.hasQueuedThread(t2));
1007 +            t1.interrupt();
1008 +            Thread.sleep(SHORT_DELAY_MS);
1009 +            assertFalse(sync.hasQueuedThread(t1));
1010 +            assertTrue(sync.hasQueuedThread(t2));
1011 +            sync.writeLock().unlock();
1012 +            Thread.sleep(SHORT_DELAY_MS);
1013 +            assertFalse(sync.hasQueuedThread(t1));
1014 +            Thread.sleep(SHORT_DELAY_MS);
1015 +            assertFalse(sync.hasQueuedThread(t2));
1016 +            t1.join();
1017 +            t2.join();
1018 +        } catch(Exception e){
1019 +            unexpectedException();
1020 +        }
1021 +    }
1022 +
1023 +
1024 +    /**
1025       * getQueueLength reports number of waiting threads
1026       */
1027      public void testGetQueueLength() {
# Line 906 | Line 1083 | public class ReentrantReadWriteLockTest
1083      }
1084  
1085      /**
1086 +     * hasWaiters throws NPE if null
1087 +     */
1088 +    public void testHasWaitersNPE() {
1089 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1090 +        try {
1091 +            lock.hasWaiters(null);
1092 +            shouldThrow();
1093 +        } catch (NullPointerException success) {
1094 +        } catch (Exception ex) {
1095 +            unexpectedException();
1096 +        }
1097 +    }
1098 +
1099 +    /**
1100 +     * getWaitQueueLength throws NPE if null
1101 +     */
1102 +    public void testGetWaitQueueLengthNPE() {
1103 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1104 +        try {
1105 +            lock.getWaitQueueLength(null);
1106 +            shouldThrow();
1107 +        } catch (NullPointerException success) {
1108 +        } catch (Exception ex) {
1109 +            unexpectedException();
1110 +        }
1111 +    }
1112 +
1113 +
1114 +    /**
1115 +     * getWaitingThreads throws NPE if null
1116 +     */
1117 +    public void testGetWaitingThreadsNPE() {
1118 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1119 +        try {
1120 +            lock.getWaitingThreads(null);
1121 +            shouldThrow();
1122 +        } catch (NullPointerException success) {
1123 +        } catch (Exception ex) {
1124 +            unexpectedException();
1125 +        }
1126 +    }
1127 +
1128 +    /**
1129 +     * hasWaiters throws IAE if not owned
1130 +     */
1131 +    public void testHasWaitersIAE() {
1132 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1133 +        final Condition c = (lock.writeLock().newCondition());
1134 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1135 +        try {
1136 +            lock2.hasWaiters(c);
1137 +            shouldThrow();
1138 +        } catch (IllegalArgumentException success) {
1139 +        } catch (Exception ex) {
1140 +            unexpectedException();
1141 +        }
1142 +    }
1143 +
1144 +    /**
1145 +     * hasWaiters throws IMSE if not locked
1146 +     */
1147 +    public void testHasWaitersIMSE() {
1148 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1149 +        final Condition c = (lock.writeLock().newCondition());
1150 +        try {
1151 +            lock.hasWaiters(c);
1152 +            shouldThrow();
1153 +        } catch (IllegalMonitorStateException success) {
1154 +        } catch (Exception ex) {
1155 +            unexpectedException();
1156 +        }
1157 +    }
1158 +
1159 +
1160 +    /**
1161 +     * getWaitQueueLength throws IAE if not owned
1162 +     */
1163 +    public void testGetWaitQueueLengthIAE() {
1164 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1165 +        final Condition c = (lock.writeLock().newCondition());
1166 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1167 +        try {
1168 +            lock2.getWaitQueueLength(c);
1169 +            shouldThrow();
1170 +        } catch (IllegalArgumentException success) {
1171 +        } catch (Exception ex) {
1172 +            unexpectedException();
1173 +        }
1174 +    }
1175 +
1176 +    /**
1177 +     * getWaitQueueLength throws IMSE if not locked
1178 +     */
1179 +    public void testGetWaitQueueLengthIMSE() {
1180 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1181 +        final Condition c = (lock.writeLock().newCondition());
1182 +        try {
1183 +            lock.getWaitQueueLength(c);
1184 +            shouldThrow();
1185 +        } catch (IllegalMonitorStateException success) {
1186 +        } catch (Exception ex) {
1187 +            unexpectedException();
1188 +        }
1189 +    }
1190 +
1191 +
1192 +    /**
1193 +     * getWaitingThreads throws IAE if not owned
1194 +     */
1195 +    public void testGetWaitingThreadsIAE() {
1196 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1197 +        final Condition c = (lock.writeLock().newCondition());
1198 +        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();  
1199 +        try {
1200 +            lock2.getWaitingThreads(c);
1201 +            shouldThrow();
1202 +        } catch (IllegalArgumentException success) {
1203 +        } catch (Exception ex) {
1204 +            unexpectedException();
1205 +        }
1206 +    }
1207 +
1208 +    /**
1209 +     * getWaitingThreads throws IMSE if not locked
1210 +     */
1211 +    public void testGetWaitingThreadsIMSE() {
1212 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1213 +        final Condition c = (lock.writeLock().newCondition());
1214 +        try {
1215 +            lock.getWaitingThreads(c);
1216 +            shouldThrow();
1217 +        } catch (IllegalMonitorStateException success) {
1218 +        } catch (Exception ex) {
1219 +            unexpectedException();
1220 +        }
1221 +    }
1222 +
1223 +
1224 +    /**
1225       * hasWaiters returns true when a thread is waiting, else false
1226       */
1227      public void testHasWaiters() {
1228 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1229 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition());
1228 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1229 >        final Condition c = (lock.writeLock().newCondition());
1230          Thread t = new Thread(new Runnable() {
1231                  public void run() {
1232                      try {
1233                          lock.writeLock().lock();
1234 <                        threadAssertFalse(c.hasWaiters());
1235 <                        threadAssertEquals(0, c.getWaitQueueLength());
1234 >                        threadAssertFalse(lock.hasWaiters(c));
1235 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1236                          c.await();
1237                          lock.writeLock().unlock();
1238                      }
# Line 930 | Line 1246 | public class ReentrantReadWriteLockTest
1246              t.start();
1247              Thread.sleep(SHORT_DELAY_MS);
1248              lock.writeLock().lock();
1249 <            assertTrue(c.hasWaiters());
1250 <            assertEquals(1, c.getWaitQueueLength());
1249 >            assertTrue(lock.hasWaiters(c));
1250 >            assertEquals(1, lock.getWaitQueueLength(c));
1251              c.signal();
1252              lock.writeLock().unlock();
1253              Thread.sleep(SHORT_DELAY_MS);
1254              lock.writeLock().lock();
1255 <            assertFalse(c.hasWaiters());
1256 <            assertEquals(0, c.getWaitQueueLength());
1255 >            assertFalse(lock.hasWaiters(c));
1256 >            assertEquals(0, lock.getWaitQueueLength(c));
1257              lock.writeLock().unlock();
1258              t.join(SHORT_DELAY_MS);
1259              assertFalse(t.isAlive());
# Line 951 | Line 1267 | public class ReentrantReadWriteLockTest
1267       * getWaitQueueLength returns number of waiting threads
1268       */
1269      public void testGetWaitQueueLength() {
1270 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1271 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition());
1270 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1271 >        final Condition c = (lock.writeLock().newCondition());
1272 >        Thread t = new Thread(new Runnable() {
1273 >                public void run() {
1274 >                    try {
1275 >                        lock.writeLock().lock();
1276 >                        threadAssertFalse(lock.hasWaiters(c));
1277 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1278 >                        c.await();
1279 >                        lock.writeLock().unlock();
1280 >                    }
1281 >                    catch(InterruptedException e) {
1282 >                        threadUnexpectedException();
1283 >                    }
1284 >                }
1285 >            });
1286 >
1287 >        try {
1288 >            t.start();
1289 >            Thread.sleep(SHORT_DELAY_MS);
1290 >            lock.writeLock().lock();
1291 >            assertTrue(lock.hasWaiters(c));
1292 >            assertEquals(1, lock.getWaitQueueLength(c));
1293 >            c.signal();
1294 >            lock.writeLock().unlock();
1295 >            Thread.sleep(SHORT_DELAY_MS);
1296 >            lock.writeLock().lock();
1297 >            assertFalse(lock.hasWaiters(c));
1298 >            assertEquals(0, lock.getWaitQueueLength(c));
1299 >            lock.writeLock().unlock();
1300 >            t.join(SHORT_DELAY_MS);
1301 >            assertFalse(t.isAlive());
1302 >        }
1303 >        catch (Exception ex) {
1304 >            unexpectedException();
1305 >        }
1306 >    }
1307 >
1308 >
1309 >    /**
1310 >     * getWaitingThreads returns only and all waiting threads
1311 >     */
1312 >    public void testGetWaitingThreads() {
1313 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1314 >        final Condition c = lock.writeLock().newCondition();
1315          Thread t1 = new Thread(new Runnable() {
1316                  public void run() {
1317                      try {
1318                          lock.writeLock().lock();
1319 <                        threadAssertFalse(c.hasWaiters());
961 <                        threadAssertEquals(0, c.getWaitQueueLength());
1319 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1320                          c.await();
1321                          lock.writeLock().unlock();
1322                      }
# Line 972 | Line 1330 | public class ReentrantReadWriteLockTest
1330                  public void run() {
1331                      try {
1332                          lock.writeLock().lock();
1333 <                        threadAssertTrue(c.hasWaiters());
976 <                        threadAssertEquals(1, c.getWaitQueueLength());
1333 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1334                          c.await();
1335                          lock.writeLock().unlock();
1336                      }
# Line 984 | Line 1341 | public class ReentrantReadWriteLockTest
1341              });
1342  
1343          try {
1344 +            lock.writeLock().lock();
1345 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
1346 +            lock.writeLock().unlock();
1347              t1.start();
1348              Thread.sleep(SHORT_DELAY_MS);
1349              t2.start();
1350              Thread.sleep(SHORT_DELAY_MS);
1351              lock.writeLock().lock();
1352 <            assertTrue(c.hasWaiters());
1353 <            assertEquals(2, c.getWaitQueueLength());
1352 >            assertTrue(lock.hasWaiters(c));
1353 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
1354 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
1355              c.signalAll();
1356              lock.writeLock().unlock();
1357              Thread.sleep(SHORT_DELAY_MS);
1358              lock.writeLock().lock();
1359 <            assertFalse(c.hasWaiters());
1360 <            assertEquals(0, c.getWaitQueueLength());
1359 >            assertFalse(lock.hasWaiters(c));
1360 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1361              lock.writeLock().unlock();
1362              t1.join(SHORT_DELAY_MS);
1363              t2.join(SHORT_DELAY_MS);
# Line 1007 | Line 1368 | public class ReentrantReadWriteLockTest
1368              unexpectedException();
1369          }
1370      }
1371 +
1372 +    /**
1373 +     * toString indicates current lock state
1374 +     */
1375 +    public void testToString() {
1376 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1377 +        String us = lock.toString();
1378 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1379 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1380 +        lock.writeLock().lock();
1381 +        String ws = lock.toString();
1382 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1383 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1384 +        lock.writeLock().unlock();
1385 +        lock.readLock().lock();
1386 +        lock.readLock().lock();
1387 +        String rs = lock.toString();
1388 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1389 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1390 +    }
1391 +
1392 +    /**
1393 +     * readLock.toString indicates current lock state
1394 +     */
1395 +    public void testReadLockToString() {
1396 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1397 +        String us = lock.readLock().toString();
1398 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1399 +        lock.readLock().lock();
1400 +        lock.readLock().lock();
1401 +        String rs = lock.readLock().toString();
1402 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1403 +    }
1404 +
1405 +    /**
1406 +     * writeLock.toString indicates current lock state
1407 +     */
1408 +    public void testWriteLockToString() {
1409 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1410 +        String us = lock.writeLock().toString();
1411 +        assertTrue(us.indexOf("Unlocked") >= 0);
1412 +        lock.writeLock().lock();
1413 +        String ls = lock.writeLock().toString();
1414 +        assertTrue(ls.indexOf("Locked") >= 0);
1415 +    }
1416 +
1417   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines