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

Comparing jsr166/src/test/tck/StampedLockTest.java (file contents):
Revision 1.25 by jsr166, Sun Jul 17 02:30:53 2016 UTC vs.
Revision 1.26 by jsr166, Sun Jul 17 03:06:50 2016 UTC

# Line 1068 | Line 1068 | public class StampedLockTest extends JSR
1068          catch (InterruptedException ex) { throw new AssertionError(ex); }
1069      }
1070  
1071 +    static long readLockInterruptiblyUninterrupted(StampedLock sl) {
1072 +        try { return sl.readLockInterruptibly(); }
1073 +        catch (InterruptedException ex) { throw new AssertionError(ex); }
1074 +    }
1075 +
1076 +    static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
1077 +        try { return sl.tryReadLock(time, unit); }
1078 +        catch (InterruptedException ex) { throw new AssertionError(ex); }
1079 +    }
1080 +
1081      /**
1082       * Invalid write stamps result in IllegalMonitorStateException
1083       */
# Line 1078 | Line 1088 | public class StampedLockTest extends JSR
1088          writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1089          writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1090  
1091 +        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1092 +        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1093 +        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1094 +        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1095 +        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1096 +
1097          List<Consumer<StampedLock>> mutaters = new ArrayList<>();
1098          mutaters.add((sl) -> {});
1099          mutaters.add((sl) -> sl.readLock());
1100          for (Function<StampedLock, Long> writeLocker : writeLockers)
1101              mutaters.add((sl) -> writeLocker.apply(sl));
1102  
1103 +        for (Function<StampedLock, Long> writeLocker : writeLockers)
1104 +        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1105 +        for (Consumer<StampedLock> mutater : mutaters) {
1106 +            final StampedLock sl = new StampedLock();
1107 +            final long stamp = writeLocker.apply(sl);
1108 +            assertTrue(stamp != 0L);
1109 +            assertThrows(IllegalMonitorStateException.class,
1110 +                         () -> sl.unlockRead(stamp));
1111 +            writeUnlocker.accept(sl, stamp);
1112 +            mutater.accept(sl);
1113 +            assertThrows(IllegalMonitorStateException.class,
1114 +                         () -> sl.unlock(stamp),
1115 +                         () -> sl.unlockRead(stamp),
1116 +                         () -> sl.unlockWrite(stamp));
1117 +        }
1118 +    }
1119 +
1120 +    /**
1121 +     * Invalid read stamps result in IllegalMonitorStateException
1122 +     */
1123 +    public void testInvalidReadStampsThrowIllegalMonitorStateException() {
1124 +        List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
1125 +        readLockers.add((sl) -> sl.readLock());
1126 +        readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl));
1127 +        readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1128 +        readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0, DAYS));
1129 +
1130 +        List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>();
1131 +        readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp));
1132 +        readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead()));
1133 +        readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock());
1134 +        readUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1135 +
1136 +        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1137 +        writeLockers.add((sl) -> sl.writeLock());
1138 +        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1139 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1140 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1141 +
1142          List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1143          writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1144          writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1145          writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1146          writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1147  
1148 +
1149 +        for (Function<StampedLock, Long> readLocker : readLockers)
1150 +        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers)
1151          for (Function<StampedLock, Long> writeLocker : writeLockers)
1152 <        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1153 <        for (Consumer<StampedLock> mutater : mutaters) {
1154 <            StampedLock sl = new StampedLock();
1097 <            long stamp = writeLocker.apply(sl);
1152 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers) {
1153 >            final StampedLock sl = new StampedLock();
1154 >            final long stamp = readLocker.apply(sl);
1155              assertTrue(stamp != 0L);
1156              assertThrows(IllegalMonitorStateException.class,
1157 <                         () -> sl.unlockRead(stamp));
1158 <            writeUnlocker.accept(sl, stamp);
1159 <            mutater.accept(sl);
1157 >                         () -> sl.unlockWrite(stamp));
1158 >            readUnlocker.accept(sl, stamp);
1159 >            assertThrows(IllegalMonitorStateException.class,
1160 >                         () -> sl.unlock(stamp),
1161 >                         () -> sl.unlockRead(stamp),
1162 >                         () -> sl.unlockWrite(stamp));
1163 >            final long writeStamp = writeLocker.apply(sl);
1164 >            assertTrue(writeStamp != 0L);
1165 >            assertTrue(writeStamp != stamp);
1166 >            assertThrows(IllegalMonitorStateException.class,
1167 >                         () -> sl.unlock(stamp),
1168 >                         () -> sl.unlockRead(stamp),
1169 >                         () -> sl.unlockWrite(stamp));
1170 >            writeUnlocker.accept(sl, writeStamp);
1171              assertThrows(IllegalMonitorStateException.class,
1172                           () -> sl.unlock(stamp),
1173                           () -> sl.unlockRead(stamp),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines