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.24 by jsr166, Tue Jun 7 23:28:30 2016 UTC vs.
Revision 1.26 by jsr166, Sun Jul 17 03:06:50 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 + import static java.util.concurrent.TimeUnit.DAYS;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.ArrayList;
12 + import java.util.List;
13   import java.util.concurrent.CountDownLatch;
14 + import java.util.concurrent.TimeUnit;
15   import java.util.concurrent.locks.Lock;
16   import java.util.concurrent.locks.StampedLock;
17 + import java.util.function.BiConsumer;
18 + import java.util.function.Consumer;
19 + import java.util.function.Function;
20  
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
# Line 1051 | Line 1058 | public class StampedLockTest extends JSR
1058          assertThrows(IllegalMonitorStateException.class, actions);
1059      }
1060  
1061 +    static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
1062 +        try { return sl.writeLockInterruptibly(); }
1063 +        catch (InterruptedException ex) { throw new AssertionError(ex); }
1064 +    }
1065 +
1066 +    static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
1067 +        try { return sl.tryWriteLock(time, unit); }
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 +     */
1084 +    public void testInvalidWriteStampsThrowIllegalMonitorStateException() {
1085 +        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1086 +        writeLockers.add((sl) -> sl.writeLock());
1087 +        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
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 +            final StampedLock sl = new StampedLock();
1154 +            final long stamp = readLocker.apply(sl);
1155 +            assertTrue(stamp != 0L);
1156 +            assertThrows(IllegalMonitorStateException.class,
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),
1174 +                         () -> sl.unlockWrite(stamp));
1175 +        }
1176 +    }
1177 +
1178   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines