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.10 by dl, Sat Dec 27 14:16:33 2003 UTC vs.
Revision 1.17 by dl, Thu Jan 8 01:29:46 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();
60 >        public Collection<Thread> getWaitingThreads(Condition c) {
61 >            return super.getWaitingThreads(c);
62          }
62
63        class PublicCondition extends ReentrantReadWriteLock.WriterConditionObject {
64            PublicCondition() { }
65            public Collection<Thread> getWaitingThreads() {
66                return super.getWaitingThreads();
67            }
68        }
69
63      }
64  
65      /**
# 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       * getQueueLength reports number of waiting threads
881       */
882      public void testGetQueueLength() {
# Line 916 | 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 ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(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 940 | 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 961 | 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 ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
1127 <        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() {
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 <                        threadAssertTrue(c.hasWaiters());
1132 <                        threadAssertEquals(1, c.getWaitQueueLength());
1131 >                        threadAssertFalse(lock.hasWaiters(c));
1132 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1133                          c.await();
1134                          lock.writeLock().unlock();
1135                      }
# Line 994 | Line 1140 | public class ReentrantReadWriteLockTest
1140              });
1141  
1142          try {
1143 <            t1.start();
998 <            Thread.sleep(SHORT_DELAY_MS);
999 <            t2.start();
1143 >            t.start();
1144              Thread.sleep(SHORT_DELAY_MS);
1145              lock.writeLock().lock();
1146 <            assertTrue(c.hasWaiters());
1147 <            assertEquals(2, c.getWaitQueueLength());
1148 <            c.signalAll();
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(c.hasWaiters());
1153 <            assertEquals(0, c.getWaitQueueLength());
1152 >            assertFalse(lock.hasWaiters(c));
1153 >            assertEquals(0, lock.getWaitQueueLength(c));
1154              lock.writeLock().unlock();
1155 <            t1.join(SHORT_DELAY_MS);
1156 <            t2.join(SHORT_DELAY_MS);
1013 <            assertFalse(t1.isAlive());
1014 <            assertFalse(t2.isAlive());
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 PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition();
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 <                        threadAssertTrue(c.getWaitingThreads().isEmpty());
1174 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1175                          c.await();
1176                          lock.writeLock().unlock();
1177                      }
# Line 1042 | Line 1185 | public class ReentrantReadWriteLockTest
1185                  public void run() {
1186                      try {
1187                          lock.writeLock().lock();
1188 <                        threadAssertFalse(c.getWaitingThreads().isEmpty());
1188 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1189                          c.await();
1190                          lock.writeLock().unlock();
1191                      }
# Line 1054 | Line 1197 | public class ReentrantReadWriteLockTest
1197  
1198          try {
1199              lock.writeLock().lock();
1200 <            assertTrue(c.getWaitingThreads().isEmpty());
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 <            assertTrue(c.getWaitingThreads().contains(t1));
1209 <            assertTrue(c.getWaitingThreads().contains(t2));
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 <            assertTrue(c.getWaitingThreads().isEmpty());
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines