--- jsr166/src/test/tck/StampedLockTest.java 2015/02/27 21:43:18 1.11 +++ jsr166/src/test/tck/StampedLockTest.java 2016/06/07 02:04:47 1.17 @@ -16,7 +16,7 @@ import junit.framework.TestSuite; public class StampedLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(StampedLockTest.class); @@ -166,9 +166,9 @@ public class StampedLockTest extends JSR */ public void testWriteUnlock_IMSE2() { StampedLock lock = new StampedLock(); + long s = lock.writeLock(); + lock.unlockWrite(s); try { - long s = lock.writeLock(); - lock.unlockWrite(s); lock.unlockWrite(s); shouldThrow(); } catch (IllegalMonitorStateException success) {} @@ -179,8 +179,8 @@ public class StampedLockTest extends JSR */ public void testWriteUnlock_IMSE3() { StampedLock lock = new StampedLock(); + long s = lock.readLock(); try { - long s = lock.readLock(); lock.unlockWrite(s); shouldThrow(); } catch (IllegalMonitorStateException success) {} @@ -191,9 +191,9 @@ public class StampedLockTest extends JSR */ public void testReadUnlock_IMSE() { StampedLock lock = new StampedLock(); + long s = lock.readLock(); + lock.unlockRead(s); try { - long s = lock.readLock(); - lock.unlockRead(s); lock.unlockRead(s); shouldThrow(); } catch (IllegalMonitorStateException success) {} @@ -215,8 +215,8 @@ public class StampedLockTest extends JSR */ public void testReadUnlock_IMSE3() { StampedLock lock = new StampedLock(); + long s = lock.writeLock(); try { - long s = lock.writeLock(); lock.unlockRead(s); shouldThrow(); } catch (IllegalMonitorStateException success) {} @@ -285,7 +285,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.writeLockInterruptibly(); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); t.interrupt(); @@ -305,7 +305,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); t.interrupt(); @@ -326,7 +326,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.readLockInterruptibly(); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); t.interrupt(); @@ -346,7 +346,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); t.interrupt(); @@ -378,7 +378,7 @@ public class StampedLockTest extends JSR long ws = lock.tryWriteLock(); assertTrue(ws == 0L); }}); - + awaitTermination(t); releaseWriteLock(lock, s); } @@ -394,7 +394,7 @@ public class StampedLockTest extends JSR long rs = lock.tryReadLock(); assertEquals(rs, 0L); }}); - + awaitTermination(t); releaseWriteLock(lock, s); } @@ -416,7 +416,7 @@ public class StampedLockTest extends JSR long s4 = lock.readLock(); lock.unlockRead(s4); }}); - + awaitTermination(t); lock.unlockRead(s); } @@ -434,7 +434,7 @@ public class StampedLockTest extends JSR long s = lock.writeLock(); lock.unlockWrite(s); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); assertFalse(lock.isWriteLocked()); @@ -454,7 +454,7 @@ public class StampedLockTest extends JSR long rs = lock.readLock(); lock.unlockRead(rs); }}); - + awaitTermination(t1); Thread t2 = newStartedThread(new CheckedRunnable() { @@ -462,7 +462,7 @@ public class StampedLockTest extends JSR long ws = lock.writeLock(); lock.unlockWrite(ws); }}); - + assertFalse(lock.isWriteLocked()); lock.unlockRead(s); awaitTermination(t2); @@ -599,7 +599,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.readLockInterruptibly(); }}); - + running.await(); waitForThreadToEnterWaitState(t, 100); t.interrupt(); @@ -704,7 +704,7 @@ public class StampedLockTest extends JSR running.countDown(); lock.writeLockInterruptibly(); }}); - + running.await(); assertFalse(lock.validate(p)); assertFalse((p = lock.tryOptimisticRead()) != 0L); @@ -876,4 +876,109 @@ public class StampedLockTest extends JSR assertTrue(lock.tryLock()); } + /** + * Lock.newCondition throws UnsupportedOperationException + */ + public void testLockViewsDoNotSupportConditions() { + StampedLock sl = new StampedLock(); + assertThrows(UnsupportedOperationException.class, + () -> sl.asWriteLock().newCondition(), + () -> sl.asReadLock().newCondition(), + () -> sl.asReadWriteLock().writeLock().newCondition(), + () -> sl.asReadWriteLock().readLock().newCondition()); + } + + /** + * Passing optimistic read stamps to unlock operations result in + * IllegalMonitorStateException + */ + public void testCannotUnlockOptimisticReadStamps() { + Runnable[] actions = { + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryOptimisticRead(); + assertTrue(stamp != 0); + sl.unlockRead(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryOptimisticRead(); + sl.unlock(stamp); + }, + + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryOptimisticRead(); + sl.writeLock(); + sl.unlock(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryOptimisticRead(); + sl.readLock(); + sl.unlockRead(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryOptimisticRead(); + sl.readLock(); + sl.unlock(stamp); + }, + + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); + assertTrue(stamp != 0); + sl.writeLock(); + sl.unlockWrite(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); + sl.writeLock(); + sl.unlock(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); + sl.readLock(); + sl.unlockRead(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); + sl.readLock(); + sl.unlock(stamp); + }, + + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); + assertTrue(stamp != 0); + sl.writeLock(); + sl.unlockWrite(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); + sl.writeLock(); + sl.unlock(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); + sl.readLock(); + sl.unlockRead(stamp); + }, + () -> { + StampedLock sl = new StampedLock(); + long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); + sl.readLock(); + sl.unlock(stamp); + }, + }; + + assertThrows(IllegalMonitorStateException.class, actions); + } + }