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.28 by jsr166, Wed Aug 24 22:22:39 2016 UTC vs.
Revision 1.37 by jsr166, Mon Jul 3 19:36:24 2017 UTC

# Line 7 | Line 7
7  
8   import static java.util.concurrent.TimeUnit.DAYS;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 import static java.util.concurrent.TimeUnit.SECONDS;
10  
11   import java.util.ArrayList;
12   import java.util.List;
# Line 83 | Line 82 | public class StampedLockTest extends JSR
82  
83      List<Function<StampedLock, Long>> readLockers() {
84          List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
85 <        readLockers.add((sl) -> sl.readLock());
86 <        readLockers.add((sl) -> sl.tryReadLock());
87 <        readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl));
88 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
89 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0L, DAYS));
90 <        readLockers.add((sl) -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
85 >        readLockers.add(sl -> sl.readLock());
86 >        readLockers.add(sl -> sl.tryReadLock());
87 >        readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl));
88 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
89 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS));
90 >        readLockers.add(sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
91          return readLockers;
92      }
93  
# Line 104 | Line 103 | public class StampedLockTest extends JSR
103  
104      List<Function<StampedLock, Long>> writeLockers() {
105          List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
106 <        writeLockers.add((sl) -> sl.writeLock());
107 <        writeLockers.add((sl) -> sl.tryWriteLock());
108 <        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
109 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
110 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0L, DAYS));
111 <        writeLockers.add((sl) -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
106 >        writeLockers.add(sl -> sl.writeLock());
107 >        writeLockers.add(sl -> sl.tryWriteLock());
108 >        writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl));
109 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
110 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS));
111 >        writeLockers.add(sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
112          return writeLockers;
113      }
114  
# Line 248 | Line 247 | public class StampedLockTest extends JSR
247          long s = assertNonZero(lock.writeLock());
248          assertTrue(lock.validate(s));
249          assertFalse(lock.validate(lock.tryWriteLock()));
250 <        assertFalse(lock.validate(lock.tryWriteLock(0L, SECONDS)));
250 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
251 >                                                    randomTimeUnit())));
252          assertFalse(lock.validate(lock.tryReadLock()));
253 <        assertFalse(lock.validate(lock.tryReadLock(0L, SECONDS)));
253 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
254 >                                                    randomTimeUnit())));
255          assertFalse(lock.validate(lock.tryOptimisticRead()));
256          lock.unlockWrite(s);
257      }
# Line 272 | Line 273 | public class StampedLockTest extends JSR
273       * interruptible operations throw InterruptedException when pre-interrupted
274       */
275      public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
275        final CountDownLatch running = new CountDownLatch(1);
276          final StampedLock lock = new StampedLock();
277  
278          Action[] interruptibleLockActions = {
# Line 337 | Line 337 | public class StampedLockTest extends JSR
337       * interruptible operations throw InterruptedException when write locked and interrupted
338       */
339      public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
340        final CountDownLatch running = new CountDownLatch(1);
340          final StampedLock lock = new StampedLock();
341          long s = lock.writeLock();
342  
# Line 360 | Line 359 | public class StampedLockTest extends JSR
359       * interruptible operations throw InterruptedException when read locked and interrupted
360       */
361      public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
363        final CountDownLatch running = new CountDownLatch(1);
362          final StampedLock lock = new StampedLock();
363          long s = lock.readLock();
364  
# Line 479 | Line 477 | public class StampedLockTest extends JSR
477      }
478  
479      /**
480 <     * A writelock succeeds only after a reading thread unlocks
480 >     * writeLock() succeeds only after a reading thread unlocks
481       */
482      public void testWriteAfterReadLock() throws InterruptedException {
483 <        final CountDownLatch running = new CountDownLatch(1);
483 >        final CountDownLatch aboutToLock = new CountDownLatch(1);
484          final StampedLock lock = new StampedLock();
485          long rs = lock.readLock();
486          Thread t = newStartedThread(new CheckedRunnable() {
487              public void realRun() {
488 <                running.countDown();
488 >                aboutToLock.countDown();
489                  long s = lock.writeLock();
490 +                assertTrue(lock.isWriteLocked());
491 +                assertFalse(lock.isReadLocked());
492                  lock.unlockWrite(s);
493              }});
494  
495 <        running.await();
496 <        waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS);
495 >        await(aboutToLock);
496 >        assertThreadBlocks(t, Thread.State.WAITING);
497          assertFalse(lock.isWriteLocked());
498 +        assertTrue(lock.isReadLocked());
499          lock.unlockRead(rs);
500          awaitTermination(t);
501 <        assertFalse(lock.isWriteLocked());
501 >        assertUnlocked(lock);
502      }
503  
504      /**
505 <     * A writelock succeeds only after reading threads unlock
505 >     * writeLock() succeeds only after reading threads unlock
506       */
507      public void testWriteAfterMultipleReadLocks() {
508          final StampedLock lock = new StampedLock();
# Line 524 | Line 525 | public class StampedLockTest extends JSR
525          assertFalse(lock.isWriteLocked());
526          lock.unlockRead(s);
527          awaitTermination(t2);
528 <        assertFalse(lock.isWriteLocked());
528 >        assertUnlocked(lock);
529      }
530  
531      /**
532 <     * Readlocks succeed only after a writing thread unlocks
532 >     * readLock() succeed only after a writing thread unlocks
533       */
534      public void testReadAfterWriteLock() {
535          final StampedLock lock = new StampedLock();
536          final CountDownLatch threadsStarted = new CountDownLatch(2);
537          final long s = lock.writeLock();
538 <        Thread t1 = newStartedThread(new CheckedRunnable() {
538 <            public void realRun() {
539 <                threadsStarted.countDown();
540 <                long rs = lock.readLock();
541 <                lock.unlockRead(rs);
542 <            }});
543 <        Thread t2 = newStartedThread(new CheckedRunnable() {
538 >        final Runnable acquireReleaseReadLock = new CheckedRunnable() {
539              public void realRun() {
540                  threadsStarted.countDown();
541                  long rs = lock.readLock();
542 +                assertTrue(lock.isReadLocked());
543 +                assertFalse(lock.isWriteLocked());
544                  lock.unlockRead(rs);
545 <            }});
545 >            }};
546 >        Thread t1 = newStartedThread(acquireReleaseReadLock);
547 >        Thread t2 = newStartedThread(acquireReleaseReadLock);
548  
549          await(threadsStarted);
550 <        waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS);
551 <        waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS);
550 >        assertThreadBlocks(t1, Thread.State.WAITING);
551 >        assertThreadBlocks(t2, Thread.State.WAITING);
552 >        assertTrue(lock.isWriteLocked());
553 >        assertFalse(lock.isReadLocked());
554          releaseWriteLock(lock, s);
555          awaitTermination(t1);
556          awaitTermination(t2);
557 +        assertUnlocked(lock);
558      }
559  
560      /**
# Line 738 | Line 740 | public class StampedLockTest extends JSR
740       */
741      public void testValidateOptimisticWriteLocked2()
742              throws InterruptedException {
743 <        final CountDownLatch running = new CountDownLatch(1);
743 >        final CountDownLatch locked = new CountDownLatch(1);
744          final StampedLock lock = new StampedLock();
745          final long p = assertValid(lock, lock.tryOptimisticRead());
746  
747          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
748              public void realRun() throws InterruptedException {
749                  lock.writeLockInterruptibly();
750 <                running.countDown();
750 >                locked.countDown();
751                  lock.writeLockInterruptibly();
752              }});
753  
754 <        running.await();
754 >        await(locked);
755          assertFalse(lock.validate(p));
756          assertEquals(0L, lock.tryOptimisticRead());
757 +        assertThreadBlocks(t, Thread.State.WAITING);
758          t.interrupt();
759          awaitTermination(t);
760 +        assertTrue(lock.isWriteLocked());
761      }
762  
763      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines