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.27 by jsr166, Wed Aug 10 17:05:13 2016 UTC vs.
Revision 1.36 by jsr166, Mon May 29 19:15:03 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines