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.13 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.19 by dl, Sun Jan 25 13:25:28 2004 UTC

# Line 156 | Line 156 | public class ReentrantReadWriteLockTest
156       */
157      public void testWriteLockInterruptibly_Interrupted() {
158          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159        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 176 | 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 185 | Line 187 | public class ReentrantReadWriteLockTest
187                  public void run() {
188                      try {
189                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
188                        threadShouldThrow();
190                      } catch(InterruptedException success){}
191                  }
192              });
# Line 209 | Line 210 | public class ReentrantReadWriteLockTest
210                  public void run() {
211                      try {
212                          lock.readLock().lockInterruptibly();
212                        threadShouldThrow();
213                      } catch(InterruptedException success){}
214                  }
215              });
# Line 224 | 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 248 | 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 268 | 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 378 | 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 401 | 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 423 | 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 447 | 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 877 | Line 877 | public class ReentrantReadWriteLockTest
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 938 | 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() {
# Line 1181 | 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