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.25 by jsr166, Sun Jul 17 02:30:53 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 +    /**
1072 +     * Invalid write stamps result in IllegalMonitorStateException
1073 +     */
1074 +    public void testInvalidWriteStampsThrowIllegalMonitorStateException() {
1075 +        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1076 +        writeLockers.add((sl) -> sl.writeLock());
1077 +        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1078 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1079 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1080 +
1081 +        List<Consumer<StampedLock>> mutaters = new ArrayList<>();
1082 +        mutaters.add((sl) -> {});
1083 +        mutaters.add((sl) -> sl.readLock());
1084 +        for (Function<StampedLock, Long> writeLocker : writeLockers)
1085 +            mutaters.add((sl) -> writeLocker.apply(sl));
1086 +
1087 +        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1088 +        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1089 +        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1090 +        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1091 +        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1092 +
1093 +        for (Function<StampedLock, Long> writeLocker : writeLockers)
1094 +        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1095 +        for (Consumer<StampedLock> mutater : mutaters) {
1096 +            StampedLock sl = new StampedLock();
1097 +            long stamp = writeLocker.apply(sl);
1098 +            assertTrue(stamp != 0L);
1099 +            assertThrows(IllegalMonitorStateException.class,
1100 +                         () -> sl.unlockRead(stamp));
1101 +            writeUnlocker.accept(sl, stamp);
1102 +            mutater.accept(sl);
1103 +            assertThrows(IllegalMonitorStateException.class,
1104 +                         () -> sl.unlock(stamp),
1105 +                         () -> sl.unlockRead(stamp),
1106 +                         () -> sl.unlockWrite(stamp));
1107 +        }
1108 +    }
1109 +
1110   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines