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.11 by dl, Sat Dec 27 18:27:02 2003 UTC vs.
Revision 1.15 by dl, Mon Dec 29 19:05:40 2003 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 Collection<Thread> getWaitingThreads(Condition c) {
61 +            return super.getWaitingThreads(c);
62 +        }
63      }
64  
65      /**
# Line 172 | Line 176 | public class ReentrantReadWriteLockTest
176      }
177  
178      /**
179 <     * timed write-trylock is interruptible
179 >     * timed write-tryLock is interruptible
180       */
181      public void testWriteTryLock_Interrupted() {
182          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 220 | 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 244 | 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 264 | 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 374 | 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 397 | 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 419 | 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 443 | 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 844 | 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       * getQueueLength reports number of waiting threads
881       */
882      public void testGetQueueLength() {
# Line 905 | Line 938 | public class ReentrantReadWriteLockTest
938      }
939  
940      /**
941 +     * hasWaiters throws NPE if null
942 +     */
943 +    public void testHasWaitersNPE() {
944 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
945 +        try {
946 +            lock.hasWaiters(null);
947 +            shouldThrow();
948 +        } catch (NullPointerException success) {
949 +        } catch (Exception ex) {
950 +            unexpectedException();
951 +        }
952 +    }
953 +
954 +    /**
955 +     * getWaitQueueLength throws NPE if null
956 +     */
957 +    public void testGetWaitQueueLengthNPE() {
958 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
959 +        try {
960 +            lock.getWaitQueueLength(null);
961 +            shouldThrow();
962 +        } catch (NullPointerException success) {
963 +        } catch (Exception ex) {
964 +            unexpectedException();
965 +        }
966 +    }
967 +
968 +
969 +    /**
970 +     * getWaitingThreads throws NPE if null
971 +     */
972 +    public void testGetWaitingThreadsNPE() {
973 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
974 +        try {
975 +            lock.getWaitingThreads(null);
976 +            shouldThrow();
977 +        } catch (NullPointerException success) {
978 +        } catch (Exception ex) {
979 +            unexpectedException();
980 +        }
981 +    }
982 +
983 +    /**
984 +     * hasWaiters throws IAE if not owned
985 +     */
986 +    public void testHasWaitersIAE() {
987 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
988 +        final Condition c = (lock.writeLock().newCondition());
989 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
990 +        try {
991 +            lock2.hasWaiters(c);
992 +            shouldThrow();
993 +        } catch (IllegalArgumentException success) {
994 +        } catch (Exception ex) {
995 +            unexpectedException();
996 +        }
997 +    }
998 +
999 +    /**
1000 +     * hasWaiters throws IMSE if not locked
1001 +     */
1002 +    public void testHasWaitersIMSE() {
1003 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1004 +        final Condition c = (lock.writeLock().newCondition());
1005 +        try {
1006 +            lock.hasWaiters(c);
1007 +            shouldThrow();
1008 +        } catch (IllegalMonitorStateException success) {
1009 +        } catch (Exception ex) {
1010 +            unexpectedException();
1011 +        }
1012 +    }
1013 +
1014 +
1015 +    /**
1016 +     * getWaitQueueLength throws IAE if not owned
1017 +     */
1018 +    public void testGetWaitQueueLengthIAE() {
1019 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1020 +        final Condition c = (lock.writeLock().newCondition());
1021 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1022 +        try {
1023 +            lock2.getWaitQueueLength(c);
1024 +            shouldThrow();
1025 +        } catch (IllegalArgumentException success) {
1026 +        } catch (Exception ex) {
1027 +            unexpectedException();
1028 +        }
1029 +    }
1030 +
1031 +    /**
1032 +     * getWaitQueueLength throws IMSE if not locked
1033 +     */
1034 +    public void testGetWaitQueueLengthIMSE() {
1035 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1036 +        final Condition c = (lock.writeLock().newCondition());
1037 +        try {
1038 +            lock.getWaitQueueLength(c);
1039 +            shouldThrow();
1040 +        } catch (IllegalMonitorStateException success) {
1041 +        } catch (Exception ex) {
1042 +            unexpectedException();
1043 +        }
1044 +    }
1045 +
1046 +
1047 +    /**
1048 +     * getWaitingThreads throws IAE if not owned
1049 +     */
1050 +    public void testGetWaitingThreadsIAE() {
1051 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1052 +        final Condition c = (lock.writeLock().newCondition());
1053 +        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();  
1054 +        try {
1055 +            lock2.getWaitingThreads(c);
1056 +            shouldThrow();
1057 +        } catch (IllegalArgumentException success) {
1058 +        } catch (Exception ex) {
1059 +            unexpectedException();
1060 +        }
1061 +    }
1062 +
1063 +    /**
1064 +     * getWaitingThreads throws IMSE if not locked
1065 +     */
1066 +    public void testGetWaitingThreadsIMSE() {
1067 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1068 +        final Condition c = (lock.writeLock().newCondition());
1069 +        try {
1070 +            lock.getWaitingThreads(c);
1071 +            shouldThrow();
1072 +        } catch (IllegalMonitorStateException success) {
1073 +        } catch (Exception ex) {
1074 +            unexpectedException();
1075 +        }
1076 +    }
1077 +
1078 +
1079 +    /**
1080       * hasWaiters returns true when a thread is waiting, else false
1081       */
1082      public void testHasWaiters() {
1083 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1084 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition());
1083 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1084 >        final Condition c = (lock.writeLock().newCondition());
1085          Thread t = new Thread(new Runnable() {
1086                  public void run() {
1087                      try {
1088                          lock.writeLock().lock();
1089 <                        threadAssertFalse(c.hasWaiters());
1090 <                        threadAssertEquals(0, c.getWaitQueueLength());
1089 >                        threadAssertFalse(lock.hasWaiters(c));
1090 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1091                          c.await();
1092                          lock.writeLock().unlock();
1093                      }
# Line 929 | Line 1101 | public class ReentrantReadWriteLockTest
1101              t.start();
1102              Thread.sleep(SHORT_DELAY_MS);
1103              lock.writeLock().lock();
1104 <            assertTrue(c.hasWaiters());
1105 <            assertEquals(1, c.getWaitQueueLength());
1104 >            assertTrue(lock.hasWaiters(c));
1105 >            assertEquals(1, lock.getWaitQueueLength(c));
1106              c.signal();
1107              lock.writeLock().unlock();
1108              Thread.sleep(SHORT_DELAY_MS);
1109              lock.writeLock().lock();
1110 <            assertFalse(c.hasWaiters());
1111 <            assertEquals(0, c.getWaitQueueLength());
1110 >            assertFalse(lock.hasWaiters(c));
1111 >            assertEquals(0, lock.getWaitQueueLength(c));
1112              lock.writeLock().unlock();
1113              t.join(SHORT_DELAY_MS);
1114              assertFalse(t.isAlive());
# Line 950 | Line 1122 | public class ReentrantReadWriteLockTest
1122       * getWaitQueueLength returns number of waiting threads
1123       */
1124      public void testGetWaitQueueLength() {
1125 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1126 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition());
1125 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1126 >        final Condition c = (lock.writeLock().newCondition());
1127 >        Thread t = new Thread(new Runnable() {
1128 >                public void run() {
1129 >                    try {
1130 >                        lock.writeLock().lock();
1131 >                        threadAssertFalse(lock.hasWaiters(c));
1132 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1133 >                        c.await();
1134 >                        lock.writeLock().unlock();
1135 >                    }
1136 >                    catch(InterruptedException e) {
1137 >                        threadUnexpectedException();
1138 >                    }
1139 >                }
1140 >            });
1141 >
1142 >        try {
1143 >            t.start();
1144 >            Thread.sleep(SHORT_DELAY_MS);
1145 >            lock.writeLock().lock();
1146 >            assertTrue(lock.hasWaiters(c));
1147 >            assertEquals(1, lock.getWaitQueueLength(c));
1148 >            c.signal();
1149 >            lock.writeLock().unlock();
1150 >            Thread.sleep(SHORT_DELAY_MS);
1151 >            lock.writeLock().lock();
1152 >            assertFalse(lock.hasWaiters(c));
1153 >            assertEquals(0, lock.getWaitQueueLength(c));
1154 >            lock.writeLock().unlock();
1155 >            t.join(SHORT_DELAY_MS);
1156 >            assertFalse(t.isAlive());
1157 >        }
1158 >        catch (Exception ex) {
1159 >            unexpectedException();
1160 >        }
1161 >    }
1162 >
1163 >
1164 >    /**
1165 >     * getWaitingThreads returns only and all waiting threads
1166 >     */
1167 >    public void testGetWaitingThreads() {
1168 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1169 >        final Condition c = lock.writeLock().newCondition();
1170          Thread t1 = new Thread(new Runnable() {
1171                  public void run() {
1172                      try {
1173                          lock.writeLock().lock();
1174 <                        threadAssertFalse(c.hasWaiters());
960 <                        threadAssertEquals(0, c.getWaitQueueLength());
1174 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1175                          c.await();
1176                          lock.writeLock().unlock();
1177                      }
# Line 971 | Line 1185 | public class ReentrantReadWriteLockTest
1185                  public void run() {
1186                      try {
1187                          lock.writeLock().lock();
1188 <                        threadAssertTrue(c.hasWaiters());
975 <                        threadAssertEquals(1, c.getWaitQueueLength());
1188 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1189                          c.await();
1190                          lock.writeLock().unlock();
1191                      }
# Line 983 | Line 1196 | public class ReentrantReadWriteLockTest
1196              });
1197  
1198          try {
1199 +            lock.writeLock().lock();
1200 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
1201 +            lock.writeLock().unlock();
1202              t1.start();
1203              Thread.sleep(SHORT_DELAY_MS);
1204              t2.start();
1205              Thread.sleep(SHORT_DELAY_MS);
1206              lock.writeLock().lock();
1207 <            assertTrue(c.hasWaiters());
1208 <            assertEquals(2, c.getWaitQueueLength());
1207 >            assertTrue(lock.hasWaiters(c));
1208 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
1209 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
1210              c.signalAll();
1211              lock.writeLock().unlock();
1212              Thread.sleep(SHORT_DELAY_MS);
1213              lock.writeLock().lock();
1214 <            assertFalse(c.hasWaiters());
1215 <            assertEquals(0, c.getWaitQueueLength());
1214 >            assertFalse(lock.hasWaiters(c));
1215 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1216              lock.writeLock().unlock();
1217              t1.join(SHORT_DELAY_MS);
1218              t2.join(SHORT_DELAY_MS);
# Line 1006 | Line 1223 | public class ReentrantReadWriteLockTest
1223              unexpectedException();
1224          }
1225      }
1226 +
1227   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines