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.29 by jsr166, Sun Nov 6 22:42:10 2016 UTC vs.
Revision 1.35 by jsr166, Sat May 13 23:05:02 2017 UTC

# Line 272 | Line 272 | public class StampedLockTest extends JSR
272       * interruptible operations throw InterruptedException when pre-interrupted
273       */
274      public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
275        final CountDownLatch running = new CountDownLatch(1);
275          final StampedLock lock = new StampedLock();
276  
277          Action[] interruptibleLockActions = {
# Line 337 | Line 336 | public class StampedLockTest extends JSR
336       * interruptible operations throw InterruptedException when write locked and interrupted
337       */
338      public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
340        final CountDownLatch running = new CountDownLatch(1);
339          final StampedLock lock = new StampedLock();
340          long s = lock.writeLock();
341  
# Line 360 | Line 358 | public class StampedLockTest extends JSR
358       * interruptible operations throw InterruptedException when read locked and interrupted
359       */
360      public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
363        final CountDownLatch running = new CountDownLatch(1);
361          final StampedLock lock = new StampedLock();
362          long s = lock.readLock();
363  
# Line 479 | Line 476 | public class StampedLockTest extends JSR
476      }
477  
478      /**
479 <     * A writelock succeeds only after a reading thread unlocks
479 >     * writeLock() succeeds only after a reading thread unlocks
480       */
481      public void testWriteAfterReadLock() throws InterruptedException {
482 <        final CountDownLatch running = new CountDownLatch(1);
482 >        final CountDownLatch aboutToLock = new CountDownLatch(1);
483          final StampedLock lock = new StampedLock();
484          long rs = lock.readLock();
485          Thread t = newStartedThread(new CheckedRunnable() {
486              public void realRun() {
487 <                running.countDown();
487 >                aboutToLock.countDown();
488                  long s = lock.writeLock();
489 +                assertTrue(lock.isWriteLocked());
490 +                assertFalse(lock.isReadLocked());
491                  lock.unlockWrite(s);
492              }});
493  
494 <        running.await();
495 <        waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS);
494 >        await(aboutToLock);
495 >        assertThreadBlocks(t, Thread.State.WAITING);
496          assertFalse(lock.isWriteLocked());
497 +        assertTrue(lock.isReadLocked());
498          lock.unlockRead(rs);
499          awaitTermination(t);
500 <        assertFalse(lock.isWriteLocked());
500 >        assertUnlocked(lock);
501      }
502  
503      /**
504 <     * A writelock succeeds only after reading threads unlock
504 >     * writeLock() succeeds only after reading threads unlock
505       */
506      public void testWriteAfterMultipleReadLocks() {
507          final StampedLock lock = new StampedLock();
# Line 524 | Line 524 | public class StampedLockTest extends JSR
524          assertFalse(lock.isWriteLocked());
525          lock.unlockRead(s);
526          awaitTermination(t2);
527 <        assertFalse(lock.isWriteLocked());
527 >        assertUnlocked(lock);
528      }
529  
530      /**
531 <     * Readlocks succeed only after a writing thread unlocks
531 >     * readLock() succeed only after a writing thread unlocks
532       */
533      public void testReadAfterWriteLock() {
534          final StampedLock lock = new StampedLock();
535          final CountDownLatch threadsStarted = new CountDownLatch(2);
536          final long s = lock.writeLock();
537 <        Thread t1 = newStartedThread(new CheckedRunnable() {
537 >        final Runnable acquireReleaseReadLock = new CheckedRunnable() {
538              public void realRun() {
539                  threadsStarted.countDown();
540                  long rs = lock.readLock();
541 +                assertTrue(lock.isReadLocked());
542 +                assertFalse(lock.isWriteLocked());
543                  lock.unlockRead(rs);
544 <            }});
545 <        Thread t2 = newStartedThread(new CheckedRunnable() {
546 <            public void realRun() {
545 <                threadsStarted.countDown();
546 <                long rs = lock.readLock();
547 <                lock.unlockRead(rs);
548 <            }});
544 >            }};
545 >        Thread t1 = newStartedThread(acquireReleaseReadLock);
546 >        Thread t2 = newStartedThread(acquireReleaseReadLock);
547  
548          await(threadsStarted);
549 <        waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS);
550 <        waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS);
549 >        assertThreadBlocks(t1, Thread.State.WAITING);
550 >        assertThreadBlocks(t2, Thread.State.WAITING);
551 >        assertTrue(lock.isWriteLocked());
552 >        assertFalse(lock.isReadLocked());
553          releaseWriteLock(lock, s);
554          awaitTermination(t1);
555          awaitTermination(t2);
556 +        assertUnlocked(lock);
557      }
558  
559      /**
# Line 738 | Line 739 | public class StampedLockTest extends JSR
739       */
740      public void testValidateOptimisticWriteLocked2()
741              throws InterruptedException {
742 <        final CountDownLatch running = new CountDownLatch(1);
742 >        final CountDownLatch locked = new CountDownLatch(1);
743          final StampedLock lock = new StampedLock();
744          final long p = assertValid(lock, lock.tryOptimisticRead());
745  
746          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
747              public void realRun() throws InterruptedException {
748                  lock.writeLockInterruptibly();
749 <                running.countDown();
749 >                locked.countDown();
750                  lock.writeLockInterruptibly();
751              }});
752  
753 <        running.await();
753 >        await(locked);
754          assertFalse(lock.validate(p));
755          assertEquals(0L, lock.tryOptimisticRead());
756 +        assertThreadBlocks(t, Thread.State.WAITING);
757          t.interrupt();
758          awaitTermination(t);
759 +        assertTrue(lock.isWriteLocked());
760      }
761  
762      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines