ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.57 by dl, Sat May 7 11:15:04 2011 UTC vs.
Revision 1.59 by jsr166, Sat May 7 19:03:26 2011 UTC

# Line 50 | Line 50 | public class ReentrantReadWriteLockTest
50       */
51      static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
52          PublicReentrantReadWriteLock() { super(); }
53 +        PublicReentrantReadWriteLock(boolean fair) { super(fair); }
54 +        public Thread getOwner() {
55 +            return super.getOwner();
56 +        }
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
# Line 61 | Line 65 | public class ReentrantReadWriteLockTest
65      /**
66       * Releases write lock, checking that it had a hold count of 1.
67       */
68 <    void releaseWriteLock(ReentrantReadWriteLock lock) {
68 >    void releaseWriteLock(PublicReentrantReadWriteLock lock) {
69          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
70 <        assertTrue(writeLock.isHeldByCurrentThread());
70 >        assertWriteLockedBy(lock, Thread.currentThread());
71 >        assertEquals(1, lock.getWriteHoldCount());
72          writeLock.unlock();
73 <        assertFalse(writeLock.isHeldByCurrentThread());
73 >        assertNotWriteLocked(lock);
74      }
75  
76      /**
77       * Spin-waits until lock.hasQueuedThread(t) becomes true.
78       */
79 <    void waitForQueuedThread(ReentrantReadWriteLock lock, Thread t) {
79 >    void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
80          long startTime = System.nanoTime();
81          while (!lock.hasQueuedThread(t)) {
82              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
83                  throw new AssertionError("timed out");
84              Thread.yield();
85          }
86 +        assertTrue(t.isAlive());
87 +        assertTrue(lock.getOwner() != t);
88      }
89  
90      /**
91       * Checks that lock is not write-locked.
92       */
93 <    void assertNotWriteLocked(ReentrantReadWriteLock lock) {
93 >    void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
94          assertFalse(lock.isWriteLocked());
95          assertFalse(lock.isWriteLockedByCurrentThread());
96 +        assertFalse(lock.writeLock().isHeldByCurrentThread());
97 +        assertNull(lock.getOwner());
98          assertEquals(0, lock.getWriteHoldCount());
99      }
100  
101      /**
102 +     * Checks that lock is write-locked by the given thread.
103 +     */
104 +    void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
105 +        assertTrue(lock.isWriteLocked());
106 +        assertSame(t, lock.getOwner());
107 +        assertEquals(t == Thread.currentThread(),
108 +                     lock.isWriteLockedByCurrentThread());
109 +        assertEquals(t == Thread.currentThread(),
110 +                     lock.writeLock().isHeldByCurrentThread());
111 +        assertEquals(t == Thread.currentThread(),
112 +                     lock.getWriteHoldCount() > 0);
113 +        assertEquals(0, lock.getReadLockCount());
114 +    }
115 +
116 +    /**
117       * Checks that condition c has no waiters.
118       */
119      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 115 | Line 139 | public class ReentrantReadWriteLockTest
139       * Constructor sets given fairness, and is in unlocked state
140       */
141      public void testConstructor() {
142 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
143 <        assertFalse(rl.isFair());
144 <        assertFalse(rl.isWriteLocked());
145 <        assertEquals(0, rl.getReadLockCount());
146 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
147 <        assertTrue(r2.isFair());
148 <        assertFalse(r2.isWriteLocked());
149 <        assertEquals(0, r2.getReadLockCount());
150 <        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
151 <        assertFalse(r3.isFair());
152 <        assertFalse(r3.isWriteLocked());
153 <        assertEquals(0, r3.getReadLockCount());
142 >        PublicReentrantReadWriteLock lock;
143 >
144 >        lock = new PublicReentrantReadWriteLock();
145 >        assertFalse(lock.isFair());
146 >        assertNotWriteLocked(lock);
147 >        assertEquals(0, lock.getReadLockCount());
148 >
149 >        lock = new PublicReentrantReadWriteLock(true);
150 >        assertTrue(lock.isFair());
151 >        assertNotWriteLocked(lock);
152 >        assertEquals(0, lock.getReadLockCount());
153 >
154 >        lock = new PublicReentrantReadWriteLock(false);
155 >        assertFalse(lock.isFair());
156 >        assertNotWriteLocked(lock);
157 >        assertEquals(0, lock.getReadLockCount());
158      }
159  
160      /**
161       * write-locking and read-locking an unlocked lock succeed
162       */
163      public void testLock() {
164 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
165 <        rl.writeLock().lock();
166 <        assertTrue(rl.isWriteLocked());
167 <        assertTrue(rl.isWriteLockedByCurrentThread());
168 <        assertTrue(rl.writeLock().isHeldByCurrentThread());
169 <        assertEquals(0, rl.getReadLockCount());
170 <        rl.writeLock().unlock();
171 <        assertFalse(rl.isWriteLocked());
172 <        assertFalse(rl.isWriteLockedByCurrentThread());
173 <        assertFalse(rl.writeLock().isHeldByCurrentThread());
174 <        assertEquals(0, rl.getReadLockCount());
175 <        rl.readLock().lock();
176 <        assertFalse(rl.isWriteLocked());
149 <        assertFalse(rl.isWriteLockedByCurrentThread());
150 <        assertEquals(1, rl.getReadLockCount());
151 <        rl.readLock().unlock();
152 <        assertFalse(rl.isWriteLocked());
153 <        assertFalse(rl.isWriteLockedByCurrentThread());
154 <        assertEquals(0, rl.getReadLockCount());
164 >        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
165 >        assertNotWriteLocked(lock);
166 >        lock.writeLock().lock();
167 >        assertWriteLockedBy(lock, Thread.currentThread());
168 >        lock.writeLock().unlock();
169 >        assertNotWriteLocked(lock);
170 >        assertEquals(0, lock.getReadLockCount());
171 >        lock.readLock().lock();
172 >        assertNotWriteLocked(lock);
173 >        assertEquals(1, lock.getReadLockCount());
174 >        lock.readLock().unlock();
175 >        assertNotWriteLocked(lock);
176 >        assertEquals(0, lock.getReadLockCount());
177      }
178  
179      /**
180       * locking an unlocked fair lock succeeds
181       */
182      public void testFairLock() {
183 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
184 <        rl.writeLock().lock();
185 <        assertTrue(rl.isWriteLocked());
186 <        assertTrue(rl.isWriteLockedByCurrentThread());
187 <        assertTrue(rl.writeLock().isHeldByCurrentThread());
188 <        assertEquals(0, rl.getReadLockCount());
189 <        rl.writeLock().unlock();
190 <        assertFalse(rl.isWriteLocked());
191 <        assertFalse(rl.isWriteLockedByCurrentThread());
192 <        assertFalse(rl.writeLock().isHeldByCurrentThread());
193 <        assertEquals(0, rl.getReadLockCount());
194 <        rl.readLock().lock();
195 <        assertFalse(rl.isWriteLocked());
174 <        assertFalse(rl.isWriteLockedByCurrentThread());
175 <        assertEquals(1, rl.getReadLockCount());
176 <        rl.readLock().unlock();
177 <        assertFalse(rl.isWriteLocked());
178 <        assertFalse(rl.isWriteLockedByCurrentThread());
179 <        assertEquals(0, rl.getReadLockCount());
183 >        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
184 >        assertNotWriteLocked(lock);
185 >        lock.writeLock().lock();
186 >        assertWriteLockedBy(lock, Thread.currentThread());
187 >        lock.writeLock().unlock();
188 >        assertNotWriteLocked(lock);
189 >        assertEquals(0, lock.getReadLockCount());
190 >        lock.readLock().lock();
191 >        assertNotWriteLocked(lock);
192 >        assertEquals(1, lock.getReadLockCount());
193 >        lock.readLock().unlock();
194 >        assertNotWriteLocked(lock);
195 >        assertEquals(0, lock.getReadLockCount());
196      }
197  
198      /**
# Line 228 | Line 244 | public class ReentrantReadWriteLockTest
244       * write-unlocking an unlocked lock throws IllegalMonitorStateException
245       */
246      public void testWriteUnlock_IllegalMonitorStateException() {
247 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
247 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
248          try {
249 <            rl.writeLock().unlock();
249 >            lock.writeLock().unlock();
250              shouldThrow();
251          } catch (IllegalMonitorStateException success) {}
252      }
# Line 239 | Line 255 | public class ReentrantReadWriteLockTest
255       * read-unlocking an unlocked lock throws IllegalMonitorStateException
256       */
257      public void testReadUnlock_IllegalMonitorStateException() {
258 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
258 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
259          try {
260 <            rl.readLock().unlock();
260 >            lock.readLock().unlock();
261              shouldThrow();
262          } catch (IllegalMonitorStateException success) {}
263      }
# Line 250 | Line 266 | public class ReentrantReadWriteLockTest
266       * write-lockInterruptibly is interruptible
267       */
268      public void testWriteLockInterruptibly_Interrupted() throws Exception {
269 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
270          lock.writeLock().lock();
271          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
272              public void realRun() throws InterruptedException {
# Line 267 | Line 283 | public class ReentrantReadWriteLockTest
283       * timed write-tryLock is interruptible
284       */
285      public void testWriteTryLock_Interrupted() throws InterruptedException {
286 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
287          lock.writeLock().lock();
288          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
289              public void realRun() throws InterruptedException {
290 <                lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
290 >                lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
291              }});
292  
293          waitForQueuedThread(lock, t);
# Line 284 | Line 300 | public class ReentrantReadWriteLockTest
300       * read-lockInterruptibly is interruptible
301       */
302      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
303 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
303 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
304          lock.writeLock().lock();
305          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
306              public void realRun() throws InterruptedException {
# Line 301 | Line 317 | public class ReentrantReadWriteLockTest
317       * timed read-tryLock is interruptible
318       */
319      public void testReadTryLock_Interrupted() throws InterruptedException {
320 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
320 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
321          lock.writeLock().lock();
322          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
323              public void realRun() throws InterruptedException {
324 <                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
324 >                lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
325              }});
326  
327          waitForQueuedThread(lock, t);
# Line 318 | Line 334 | public class ReentrantReadWriteLockTest
334       * write-tryLock fails if locked
335       */
336      public void testWriteTryLockWhenLocked() throws InterruptedException {
337 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
337 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
338          lock.writeLock().lock();
339          Thread t = newStartedThread(new CheckedRunnable() {
340              public void realRun() {
# Line 333 | Line 349 | public class ReentrantReadWriteLockTest
349       * read-tryLock fails if locked
350       */
351      public void testReadTryLockWhenLocked() throws InterruptedException {
352 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
352 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
353          lock.writeLock().lock();
354          Thread t = newStartedThread(new CheckedRunnable() {
355              public void realRun() {
# Line 361 | Line 377 | public class ReentrantReadWriteLockTest
377      }
378  
379      /**
380 +     * A writelock succeeds only after a reading thread unlocks
381 +     */
382 +    public void testWriteAfterReadLock() throws InterruptedException {
383 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
384 +        lock.readLock().lock();
385 +
386 +        Thread t = newStartedThread(new CheckedRunnable() {
387 +            public void realRun() {
388 +                assertEquals(1, lock.getReadLockCount());
389 +                lock.writeLock().lock();
390 +                assertEquals(0, lock.getReadLockCount());
391 +                lock.writeLock().unlock();
392 +            }});
393 +        waitForQueuedThread(lock, t);
394 +        assertNotWriteLocked(lock);
395 +        assertEquals(1, lock.getReadLockCount());
396 +        lock.readLock().unlock();
397 +        assertEquals(0, lock.getReadLockCount());
398 +        awaitTermination(t);
399 +        assertNotWriteLocked(lock);
400 +    }
401 +
402 +    /**
403       * A writelock succeeds only after reading threads unlock
404       */
405      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
406 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
406 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
407 >        lock.readLock().lock();
408          lock.readLock().lock();
409          Thread t1 = newStartedThread(new CheckedRunnable() {
410              public void realRun() {
411                  lock.readLock().lock();
412 +                assertEquals(3, lock.getReadLockCount());
413                  lock.readLock().unlock();
414              }});
415 +        awaitTermination(t1);
416 +
417          Thread t2 = newStartedThread(new CheckedRunnable() {
418              public void realRun() {
419 +                assertEquals(2, lock.getReadLockCount());
420                  lock.writeLock().lock();
421 +                assertEquals(0, lock.getReadLockCount());
422                  lock.writeLock().unlock();
423              }});
424 <        delay(SHORT_DELAY_MS);
424 >        waitForQueuedThread(lock, t2);
425 >        assertNotWriteLocked(lock);
426 >        assertEquals(2, lock.getReadLockCount());
427 >        lock.readLock().unlock();
428 >        lock.readLock().unlock();
429 >        assertEquals(0, lock.getReadLockCount());
430 >        awaitTermination(t2);
431 >        assertNotWriteLocked(lock);
432 >    }
433 >
434 >    /**
435 >     * A thread that tries to acquire a fair read lock (non-reentrantly)
436 >     * will block if there is a waiting writer thread.
437 >     */
438 >    public void testReaderWriterReaderFairFifo() throws InterruptedException {
439 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
440 >        final AtomicBoolean t1GotLock = new AtomicBoolean(false);
441 >
442 >        lock.readLock().lock();
443 >        Thread t1 = newStartedThread(new CheckedRunnable() {
444 >            public void realRun() {
445 >                assertEquals(1, lock.getReadLockCount());
446 >                lock.writeLock().lock();
447 >                assertEquals(0, lock.getReadLockCount());
448 >                t1GotLock.set(true);
449 >                lock.writeLock().unlock();
450 >            }});
451 >        waitForQueuedThread(lock, t1);
452 >
453 >        Thread t2 = newStartedThread(new CheckedRunnable() {
454 >            public void realRun() {
455 >                assertEquals(1, lock.getReadLockCount());
456 >                lock.readLock().lock();
457 >                assertEquals(1, lock.getReadLockCount());
458 >                assertTrue(t1GotLock.get());
459 >                lock.readLock().unlock();
460 >            }});
461 >        waitForQueuedThread(lock, t2);
462 >        assertTrue(t1.isAlive());
463 >        assertNotWriteLocked(lock);
464 >        assertEquals(1, lock.getReadLockCount());
465          lock.readLock().unlock();
466          awaitTermination(t1);
467          awaitTermination(t2);
# Line 387 | Line 472 | public class ReentrantReadWriteLockTest
472       * Readlocks succeed only after a writing thread unlocks
473       */
474      public void testReadAfterWriteLock() throws InterruptedException {
475 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
475 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
476          lock.writeLock().lock();
477          Thread t1 = newStartedThread(new CheckedRunnable() {
478              public void realRun() {
# Line 423 | Line 508 | public class ReentrantReadWriteLockTest
508       * other threads are waiting for readlock
509       */
510      public void testReadHoldingWriteLock2() throws InterruptedException {
511 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
511 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
512          lock.writeLock().lock();
513          lock.readLock().lock();
514          lock.readLock().unlock();
# Line 441 | Line 526 | public class ReentrantReadWriteLockTest
526  
527          waitForQueuedThread(lock, t1);
528          waitForQueuedThread(lock, t2);
529 <        assertTrue(lock.isWriteLockedByCurrentThread());
529 >        assertWriteLockedBy(lock, Thread.currentThread());
530          lock.readLock().lock();
531          lock.readLock().unlock();
532          releaseWriteLock(lock);
# Line 454 | Line 539 | public class ReentrantReadWriteLockTest
539       * other threads are waiting for writelock
540       */
541      public void testReadHoldingWriteLock3() throws InterruptedException {
542 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
542 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
543          lock.writeLock().lock();
544          lock.readLock().lock();
545          lock.readLock().unlock();
# Line 472 | Line 557 | public class ReentrantReadWriteLockTest
557  
558          waitForQueuedThread(lock, t1);
559          waitForQueuedThread(lock, t2);
560 <        assertTrue(lock.isWriteLockedByCurrentThread());
560 >        assertWriteLockedBy(lock, Thread.currentThread());
561          lock.readLock().lock();
562          lock.readLock().unlock();
563          releaseWriteLock(lock);
# Line 485 | Line 570 | public class ReentrantReadWriteLockTest
570       * other threads are waiting for writelock
571       */
572      public void testWriteHoldingWriteLock4() throws InterruptedException {
573 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
573 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
574          lock.writeLock().lock();
575          lock.writeLock().lock();
576          lock.writeLock().unlock();
# Line 503 | Line 588 | public class ReentrantReadWriteLockTest
588  
589          waitForQueuedThread(lock, t1);
590          waitForQueuedThread(lock, t2);
591 <        assertTrue(lock.isWriteLockedByCurrentThread());
591 >        assertWriteLockedBy(lock, Thread.currentThread());
592          assertEquals(1, lock.getWriteHoldCount());
593          lock.writeLock().lock();
594 <        assertTrue(lock.isWriteLockedByCurrentThread());
594 >        assertWriteLockedBy(lock, Thread.currentThread());
595          assertEquals(2, lock.getWriteHoldCount());
596          lock.writeLock().unlock();
597          releaseWriteLock(lock);
# Line 530 | Line 615 | public class ReentrantReadWriteLockTest
615       * other threads are waiting for readlock
616       */
617      public void testReadHoldingWriteLockFair2() throws InterruptedException {
618 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
618 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
619          lock.writeLock().lock();
620          lock.readLock().lock();
621          lock.readLock().unlock();
# Line 548 | Line 633 | public class ReentrantReadWriteLockTest
633  
634          waitForQueuedThread(lock, t1);
635          waitForQueuedThread(lock, t2);
636 <        assertTrue(lock.isWriteLockedByCurrentThread());
636 >        assertWriteLockedBy(lock, Thread.currentThread());
637          lock.readLock().lock();
638          lock.readLock().unlock();
639          releaseWriteLock(lock);
# Line 561 | Line 646 | public class ReentrantReadWriteLockTest
646       * other threads are waiting for writelock
647       */
648      public void testReadHoldingWriteLockFair3() throws InterruptedException {
649 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
650          lock.writeLock().lock();
651          lock.readLock().lock();
652          lock.readLock().unlock();
# Line 579 | Line 664 | public class ReentrantReadWriteLockTest
664  
665          waitForQueuedThread(lock, t1);
666          waitForQueuedThread(lock, t2);
667 <        assertTrue(lock.isWriteLockedByCurrentThread());
667 >        assertWriteLockedBy(lock, Thread.currentThread());
668          lock.readLock().lock();
669          lock.readLock().unlock();
670          releaseWriteLock(lock);
# Line 592 | Line 677 | public class ReentrantReadWriteLockTest
677       * other threads are waiting for writelock
678       */
679      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
680 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
680 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
681          lock.writeLock().lock();
682          Thread t1 = newStartedThread(new CheckedRunnable() {
683              public void realRun() {
# Line 607 | Line 692 | public class ReentrantReadWriteLockTest
692  
693          waitForQueuedThread(lock, t1);
694          waitForQueuedThread(lock, t2);
695 <        assertTrue(lock.isWriteLockedByCurrentThread());
695 >        assertWriteLockedBy(lock, Thread.currentThread());
696          assertEquals(1, lock.getWriteHoldCount());
697          lock.writeLock().lock();
698          assertEquals(2, lock.getWriteHoldCount());
# Line 689 | Line 774 | public class ReentrantReadWriteLockTest
774          lock.writeLock().lock();
775          Thread t = newStartedThread(new CheckedRunnable() {
776              public void realRun() throws InterruptedException {
777 <                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
777 >                long startTime = System.nanoTime();
778 >                long timeoutMillis = 10;
779 >                assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
780 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
781              }});
782  
783          awaitTermination(t);
# Line 705 | Line 793 | public class ReentrantReadWriteLockTest
793          lock.writeLock().lock();
794          Thread t = newStartedThread(new CheckedRunnable() {
795              public void realRun() throws InterruptedException {
796 <                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
796 >                long startTime = System.nanoTime();
797 >                long timeoutMillis = 10;
798 >                assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
799 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
800              }});
801  
802          awaitTermination(t);
# Line 717 | Line 808 | public class ReentrantReadWriteLockTest
808       * write lockInterruptibly succeeds if lock free else is interruptible
809       */
810      public void testWriteLockInterruptibly() throws InterruptedException {
811 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
811 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
812          lock.writeLock().lockInterruptibly();
813          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
814              public void realRun() throws InterruptedException {
# Line 734 | Line 825 | public class ReentrantReadWriteLockTest
825       * read lockInterruptibly succeeds if lock free else is interruptible
826       */
827      public void testReadLockInterruptibly() throws InterruptedException {
828 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
828 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
829          lock.writeLock().lockInterruptibly();
830          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
831              public void realRun() throws InterruptedException {
# Line 832 | Line 923 | public class ReentrantReadWriteLockTest
923          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
924          final Condition c = lock.writeLock().newCondition();
925          lock.writeLock().lock();
926 +        long startTime = System.nanoTime();
927 +        long timeoutMillis = 10;
928          java.util.Date d = new java.util.Date();
929 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
929 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
930 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
931          lock.writeLock().unlock();
932      }
933  
# Line 841 | Line 935 | public class ReentrantReadWriteLockTest
935       * await returns when signalled
936       */
937      public void testAwait() throws InterruptedException {
938 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
939          final Condition c = lock.writeLock().newCondition();
940          final CountDownLatch locked = new CountDownLatch(1);
941          Thread t = newStartedThread(new CheckedRunnable() {
# Line 854 | Line 948 | public class ReentrantReadWriteLockTest
948  
949          locked.await();
950          lock.writeLock().lock();
951 +        assertHasWaiters(lock, c, t);
952          c.signal();
953 +        assertHasNoWaiters(lock, c);
954          assertTrue(t.isAlive());
955          lock.writeLock().unlock();
956          awaitTermination(t);
# Line 867 | Line 963 | public class ReentrantReadWriteLockTest
963          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
964          final Condition c = lock.writeLock().newCondition();
965          final CountDownLatch locked = new CountDownLatch(1);
870        final AtomicBoolean canAwake = new AtomicBoolean(false);
966          Thread t = newStartedThread(new CheckedRunnable() {
967              public void realRun() {
968                  lock.writeLock().lock();
# Line 881 | Line 976 | public class ReentrantReadWriteLockTest
976          lock.writeLock().lock();
977          lock.writeLock().unlock();
978          t.interrupt();
979 <        t.join(10);
980 <        assertTrue(t.isAlive());
979 >        long timeoutMillis = 10;
980 >        assertThreadJoinTimesOut(t, timeoutMillis);
981          lock.writeLock().lock();
982          c.signal();
983          lock.writeLock().unlock();
# Line 899 | Line 994 | public class ReentrantReadWriteLockTest
994          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
995              public void realRun() throws InterruptedException {
996                  lock.writeLock().lock();
997 <                assertTrue(lock.isWriteLocked());
903 <                assertTrue(lock.isWriteLockedByCurrentThread());
997 >                assertWriteLockedBy(lock, Thread.currentThread());
998                  assertHasNoWaiters(lock, c);
999                  locked.countDown();
1000                  try {
1001                      c.await();
1002                  } finally {
1003 <                    assertTrue(lock.isWriteLocked());
910 <                    assertTrue(lock.isWriteLockedByCurrentThread());
1003 >                    assertWriteLockedBy(lock, Thread.currentThread());
1004                      assertHasNoWaiters(lock, c);
1005                      lock.writeLock().unlock();
1006                      assertFalse(Thread.interrupted());
# Line 918 | Line 1011 | public class ReentrantReadWriteLockTest
1011          assertHasWaiters(lock, c, t);
1012          t.interrupt();
1013          awaitTermination(t);
1014 <        assertFalse(lock.isWriteLocked());
1014 >        assertNotWriteLocked(lock);
1015      }
1016  
1017      /**
# Line 931 | Line 1024 | public class ReentrantReadWriteLockTest
1024          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1025              public void realRun() throws InterruptedException {
1026                  lock.writeLock().lock();
1027 <                assertTrue(lock.isWriteLocked());
935 <                assertTrue(lock.isWriteLockedByCurrentThread());
1027 >                assertWriteLockedBy(lock, Thread.currentThread());
1028                  assertHasNoWaiters(lock, c);
1029                  locked.countDown();
1030                  try {
1031 <                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
1031 >                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
1032                  } finally {
1033 <                    assertTrue(lock.isWriteLocked());
942 <                    assertTrue(lock.isWriteLockedByCurrentThread());
1033 >                    assertWriteLockedBy(lock, Thread.currentThread());
1034                      assertHasNoWaiters(lock, c);
1035                      lock.writeLock().unlock();
1036                      assertFalse(Thread.interrupted());
# Line 950 | Line 1041 | public class ReentrantReadWriteLockTest
1041          assertHasWaiters(lock, c, t);
1042          t.interrupt();
1043          awaitTermination(t);
1044 <        assertFalse(lock.isWriteLocked());
1044 >        assertNotWriteLocked(lock);
1045      }
1046  
1047      /**
# Line 963 | Line 1054 | public class ReentrantReadWriteLockTest
1054          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1055              public void realRun() throws InterruptedException {
1056                  lock.writeLock().lock();
1057 <                assertTrue(lock.isWriteLocked());
967 <                assertTrue(lock.isWriteLockedByCurrentThread());
1057 >                assertWriteLockedBy(lock, Thread.currentThread());
1058                  assertHasNoWaiters(lock, c);
1059                  locked.countDown();
1060                  java.util.Date d = new java.util.Date();
1061                  try {
1062 <                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1062 >                    c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS));
1063                  } finally {
1064 <                    assertTrue(lock.isWriteLocked());
975 <                    assertTrue(lock.isWriteLockedByCurrentThread());
1064 >                    assertWriteLockedBy(lock, Thread.currentThread());
1065                      assertHasNoWaiters(lock, c);
1066                      lock.writeLock().unlock();
1067                      assertFalse(Thread.interrupted());
# Line 983 | Line 1072 | public class ReentrantReadWriteLockTest
1072          assertHasWaiters(lock, c, t);
1073          t.interrupt();
1074          awaitTermination(t);
1075 <        assertFalse(lock.isWriteLocked());
1075 >        assertNotWriteLocked(lock);
1076      }
1077  
1078      /**
# Line 1066 | Line 1155 | public class ReentrantReadWriteLockTest
1155      }
1156  
1157      /**
1158 +     * await after multiple reentrant locking preserves lock count
1159 +     */
1160 +    public void testAwaitLockCount() throws InterruptedException {
1161 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1162 +        final Condition c = lock.writeLock().newCondition();
1163 +        final CountDownLatch locked = new CountDownLatch(2);
1164 +        Thread t1 = newStartedThread(new CheckedRunnable() {
1165 +            public void realRun() throws InterruptedException {
1166 +                lock.writeLock().lock();
1167 +                assertWriteLockedBy(lock, Thread.currentThread());
1168 +                assertEquals(1, lock.writeLock().getHoldCount());
1169 +                locked.countDown();
1170 +                c.await();
1171 +                assertWriteLockedBy(lock, Thread.currentThread());
1172 +                assertEquals(1, lock.writeLock().getHoldCount());
1173 +                lock.writeLock().unlock();
1174 +            }});
1175 +
1176 +        Thread t2 = newStartedThread(new CheckedRunnable() {
1177 +            public void realRun() throws InterruptedException {
1178 +                lock.writeLock().lock();
1179 +                lock.writeLock().lock();
1180 +                assertWriteLockedBy(lock, Thread.currentThread());
1181 +                assertEquals(2, lock.writeLock().getHoldCount());
1182 +                locked.countDown();
1183 +                c.await();
1184 +                assertWriteLockedBy(lock, Thread.currentThread());
1185 +                assertEquals(2, lock.writeLock().getHoldCount());
1186 +                lock.writeLock().unlock();
1187 +                lock.writeLock().unlock();
1188 +            }});
1189 +
1190 +        locked.await();
1191 +        lock.writeLock().lock();
1192 +        assertHasWaiters(lock, c, t1, t2);
1193 +        c.signalAll();
1194 +        assertHasNoWaiters(lock, c);
1195 +        lock.writeLock().unlock();
1196 +        awaitTermination(t1);
1197 +        awaitTermination(t2);
1198 +    }
1199 +
1200 +    /**
1201       * A serialized lock deserializes as unlocked
1202       */
1203      public void testSerialization() throws Exception {
# Line 1088 | Line 1220 | public class ReentrantReadWriteLockTest
1220      /**
1221       * hasQueuedThreads reports whether there are waiting threads
1222       */
1223 <    public void testhasQueuedThreads() throws InterruptedException {
1224 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1223 >    public void testHasQueuedThreads() throws InterruptedException {
1224 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1225          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1226          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1227          assertFalse(lock.hasQueuedThreads());
1228          lock.writeLock().lock();
1229          assertFalse(lock.hasQueuedThreads());
1098        long startTime = System.nanoTime();
1230          t1.start();
1231          waitForQueuedThread(lock, t1);
1232 +        assertTrue(lock.hasQueuedThreads());
1233          t2.start();
1234          waitForQueuedThread(lock, t2);
1235          assertTrue(lock.hasQueuedThreads());
# Line 1113 | Line 1245 | public class ReentrantReadWriteLockTest
1245       * hasQueuedThread(null) throws NPE
1246       */
1247      public void testHasQueuedThreadNPE() {
1248 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1248 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1249          try {
1250 <            sync.hasQueuedThread(null);
1250 >            lock.hasQueuedThread(null);
1251              shouldThrow();
1252          } catch (NullPointerException success) {}
1253      }
# Line 1124 | Line 1256 | public class ReentrantReadWriteLockTest
1256       * hasQueuedThread reports whether a thread is queued.
1257       */
1258      public void testHasQueuedThread() throws InterruptedException {
1259 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1259 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1260          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1261          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1262          assertFalse(lock.hasQueuedThread(t1));
1263          assertFalse(lock.hasQueuedThread(t2));
1264          lock.writeLock().lock();
1133        long startTime = System.nanoTime();
1265          t1.start();
1266          waitForQueuedThread(lock, t1);
1267          assertTrue(lock.hasQueuedThread(t1));
# Line 1153 | Line 1284 | public class ReentrantReadWriteLockTest
1284       * getQueueLength reports number of waiting threads
1285       */
1286      public void testGetQueueLength() throws InterruptedException {
1287 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1287 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1288          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1289          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1290          assertEquals(0, lock.getQueueLength());
1291          lock.writeLock().lock();
1161        long startTime = System.nanoTime();
1292          t1.start();
1293          waitForQueuedThread(lock, t1);
1294          assertEquals(1, lock.getQueueLength());
# Line 1182 | Line 1312 | public class ReentrantReadWriteLockTest
1312          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1313          assertTrue(lock.getQueuedThreads().isEmpty());
1314          lock.writeLock().lock();
1185        long startTime = System.nanoTime();
1315          assertTrue(lock.getQueuedThreads().isEmpty());
1316          t1.start();
1317          waitForQueuedThread(lock, t1);
# Line 1237 | Line 1366 | public class ReentrantReadWriteLockTest
1366      }
1367  
1368      /**
1369 <     * hasWaiters throws IAE if not owned
1369 >     * hasWaiters throws IllegalArgumentException if not owned
1370       */
1371      public void testHasWaitersIAE() {
1372          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1250 | Line 1379 | public class ReentrantReadWriteLockTest
1379      }
1380  
1381      /**
1382 <     * hasWaiters throws IMSE if not locked
1382 >     * hasWaiters throws IllegalMonitorStateException if not locked
1383       */
1384      public void testHasWaitersIMSE() {
1385          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1262 | Line 1391 | public class ReentrantReadWriteLockTest
1391      }
1392  
1393      /**
1394 <     * getWaitQueueLength throws IAE if not owned
1394 >     * getWaitQueueLength throws IllegalArgumentException if not owned
1395       */
1396      public void testGetWaitQueueLengthIAE() {
1397          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1275 | Line 1404 | public class ReentrantReadWriteLockTest
1404      }
1405  
1406      /**
1407 <     * getWaitQueueLength throws IMSE if not locked
1407 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
1408       */
1409      public void testGetWaitQueueLengthIMSE() {
1410          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1287 | Line 1416 | public class ReentrantReadWriteLockTest
1416      }
1417  
1418      /**
1419 <     * getWaitingThreads throws IAE if not owned
1419 >     * getWaitingThreads throws IllegalArgumentException if not owned
1420       */
1421      public void testGetWaitingThreadsIAE() {
1422          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
# Line 1300 | Line 1429 | public class ReentrantReadWriteLockTest
1429      }
1430  
1431      /**
1432 <     * getWaitingThreads throws IMSE if not locked
1432 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
1433       */
1434      public void testGetWaitingThreadsIMSE() {
1435          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
# Line 1321 | Line 1450 | public class ReentrantReadWriteLockTest
1450          Thread t = newStartedThread(new CheckedRunnable() {
1451              public void realRun() throws InterruptedException {
1452                  lock.writeLock().lock();
1453 +                assertHasNoWaiters(lock, c);
1454                  assertFalse(lock.hasWaiters(c));
1455                  locked.countDown();
1326                assertEquals(0, lock.getWaitQueueLength(c));
1456                  c.await();
1457 +                assertHasNoWaiters(lock, c);
1458 +                assertFalse(lock.hasWaiters(c));
1459                  lock.writeLock().unlock();
1460              }});
1461  
1462          locked.await();
1463          lock.writeLock().lock();
1464 +        assertHasWaiters(lock, c, t);
1465          assertTrue(lock.hasWaiters(c));
1334        assertEquals(1, lock.getWaitQueueLength(c));
1466          c.signal();
1467          assertHasNoWaiters(lock, c);
1468 +        assertFalse(lock.hasWaiters(c));
1469          lock.writeLock().unlock();
1470          awaitTermination(t);
1471          assertHasNoWaiters(lock, c);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines