--- jsr166/src/test/tck/StampedLockTest.java 2016/07/17 02:30:53 1.25 +++ jsr166/src/test/tck/StampedLockTest.java 2016/07/17 03:06:50 1.26 @@ -1068,6 +1068,16 @@ public class StampedLockTest extends JSR catch (InterruptedException ex) { throw new AssertionError(ex); } } + static long readLockInterruptiblyUninterrupted(StampedLock sl) { + try { return sl.readLockInterruptibly(); } + catch (InterruptedException ex) { throw new AssertionError(ex); } + } + + static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) { + try { return sl.tryReadLock(time, unit); } + catch (InterruptedException ex) { throw new AssertionError(ex); } + } + /** * Invalid write stamps result in IllegalMonitorStateException */ @@ -1078,28 +1088,86 @@ public class StampedLockTest extends JSR writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS)); + List> writeUnlockers = new ArrayList<>(); + writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp)); + writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite())); + writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock()); + writeUnlockers.add((sl, stamp) -> sl.unlock(stamp)); + List> mutaters = new ArrayList<>(); mutaters.add((sl) -> {}); mutaters.add((sl) -> sl.readLock()); for (Function writeLocker : writeLockers) mutaters.add((sl) -> writeLocker.apply(sl)); + for (Function writeLocker : writeLockers) + for (BiConsumer writeUnlocker : writeUnlockers) + for (Consumer mutater : mutaters) { + final StampedLock sl = new StampedLock(); + final long stamp = writeLocker.apply(sl); + assertTrue(stamp != 0L); + assertThrows(IllegalMonitorStateException.class, + () -> sl.unlockRead(stamp)); + writeUnlocker.accept(sl, stamp); + mutater.accept(sl); + assertThrows(IllegalMonitorStateException.class, + () -> sl.unlock(stamp), + () -> sl.unlockRead(stamp), + () -> sl.unlockWrite(stamp)); + } + } + + /** + * Invalid read stamps result in IllegalMonitorStateException + */ + public void testInvalidReadStampsThrowIllegalMonitorStateException() { + List> readLockers = new ArrayList<>(); + readLockers.add((sl) -> sl.readLock()); + readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl)); + readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); + readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0, DAYS)); + + List> readUnlockers = new ArrayList<>(); + readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp)); + readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead())); + readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock()); + readUnlockers.add((sl, stamp) -> sl.unlock(stamp)); + + List> writeLockers = new ArrayList<>(); + writeLockers.add((sl) -> sl.writeLock()); + writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl)); + writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); + writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS)); + List> writeUnlockers = new ArrayList<>(); writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp)); writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite())); writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock()); writeUnlockers.add((sl, stamp) -> sl.unlock(stamp)); + + for (Function readLocker : readLockers) + for (BiConsumer readUnlocker : readUnlockers) for (Function writeLocker : writeLockers) - for (BiConsumer writeUnlocker : writeUnlockers) - for (Consumer mutater : mutaters) { - StampedLock sl = new StampedLock(); - long stamp = writeLocker.apply(sl); + for (BiConsumer writeUnlocker : writeUnlockers) { + final StampedLock sl = new StampedLock(); + final long stamp = readLocker.apply(sl); assertTrue(stamp != 0L); assertThrows(IllegalMonitorStateException.class, - () -> sl.unlockRead(stamp)); - writeUnlocker.accept(sl, stamp); - mutater.accept(sl); + () -> sl.unlockWrite(stamp)); + readUnlocker.accept(sl, stamp); + assertThrows(IllegalMonitorStateException.class, + () -> sl.unlock(stamp), + () -> sl.unlockRead(stamp), + () -> sl.unlockWrite(stamp)); + final long writeStamp = writeLocker.apply(sl); + assertTrue(writeStamp != 0L); + assertTrue(writeStamp != stamp); + assertThrows(IllegalMonitorStateException.class, + () -> sl.unlock(stamp), + () -> sl.unlockRead(stamp), + () -> sl.unlockWrite(stamp)); + writeUnlocker.accept(sl, writeStamp); assertThrows(IllegalMonitorStateException.class, () -> sl.unlock(stamp), () -> sl.unlockRead(stamp),