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.33 by jsr166, Tue Nov 17 14:33:55 2009 UTC vs.
Revision 1.36 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14   import java.util.*;
15  
16   public class ReentrantReadWriteLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run (suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ReentrantReadWriteLockTest.class);
21 >        return new TestSuite(ReentrantReadWriteLockTest.class);
22      }
23  
24      /**
# Line 61 | Line 62 | public class ReentrantReadWriteLockTest
62       * Constructor sets given fairness, and is in unlocked state
63       */
64      public void testConstructor() {
65 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
65 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
66          assertFalse(rl.isFair());
67          assertFalse(rl.isWriteLocked());
68          assertEquals(0, rl.getReadLockCount());
69 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
69 >        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
70          assertTrue(r2.isFair());
71          assertFalse(r2.isWriteLocked());
72          assertEquals(0, r2.getReadLockCount());
73 <        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
73 >        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
74          assertFalse(r3.isFair());
75          assertFalse(r3.isWriteLocked());
76          assertEquals(0, r3.getReadLockCount());
# Line 79 | Line 80 | public class ReentrantReadWriteLockTest
80       * write-locking and read-locking an unlocked lock succeed
81       */
82      public void testLock() {
83 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
83 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 105 | Line 106 | public class ReentrantReadWriteLockTest
106       * locking an unlocked fair lock succeeds
107       */
108      public void testFairLock() {
109 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
109 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
110          rl.writeLock().lock();
111          assertTrue(rl.isWriteLocked());
112          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 130 | Line 131 | public class ReentrantReadWriteLockTest
131       * getWriteHoldCount returns number of recursive holds
132       */
133      public void testGetWriteHoldCount() {
134 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 <        for (int i = 1; i <= SIZE; i++) {
136 <            lock.writeLock().lock();
137 <            assertEquals(i,lock.getWriteHoldCount());
138 <        }
139 <        for (int i = SIZE; i > 0; i--) {
140 <            lock.writeLock().unlock();
141 <            assertEquals(i-1,lock.getWriteHoldCount());
142 <        }
134 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 >        for (int i = 1; i <= SIZE; i++) {
136 >            lock.writeLock().lock();
137 >            assertEquals(i,lock.getWriteHoldCount());
138 >        }
139 >        for (int i = SIZE; i > 0; i--) {
140 >            lock.writeLock().unlock();
141 >            assertEquals(i-1,lock.getWriteHoldCount());
142 >        }
143      }
144  
145      /**
146       * WriteLock.getHoldCount returns number of recursive holds
147       */
148      public void testGetHoldCount() {
149 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 <        for (int i = 1; i <= SIZE; i++) {
151 <            lock.writeLock().lock();
152 <            assertEquals(i,lock.writeLock().getHoldCount());
153 <        }
154 <        for (int i = SIZE; i > 0; i--) {
155 <            lock.writeLock().unlock();
156 <            assertEquals(i-1,lock.writeLock().getHoldCount());
157 <        }
149 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 >        for (int i = 1; i <= SIZE; i++) {
151 >            lock.writeLock().lock();
152 >            assertEquals(i,lock.writeLock().getHoldCount());
153 >        }
154 >        for (int i = SIZE; i > 0; i--) {
155 >            lock.writeLock().unlock();
156 >            assertEquals(i-1,lock.writeLock().getHoldCount());
157 >        }
158      }
159  
160      /**
161       * getReadHoldCount returns number of recursive holds
162       */
163      public void testGetReadHoldCount() {
164 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 <        for (int i = 1; i <= SIZE; i++) {
166 <            lock.readLock().lock();
167 <            assertEquals(i,lock.getReadHoldCount());
168 <        }
169 <        for (int i = SIZE; i > 0; i--) {
170 <            lock.readLock().unlock();
171 <            assertEquals(i-1,lock.getReadHoldCount());
172 <        }
164 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 >        for (int i = 1; i <= SIZE; i++) {
166 >            lock.readLock().lock();
167 >            assertEquals(i,lock.getReadHoldCount());
168 >        }
169 >        for (int i = SIZE; i > 0; i--) {
170 >            lock.readLock().unlock();
171 >            assertEquals(i-1,lock.getReadHoldCount());
172 >        }
173      }
174  
175  
# Line 176 | Line 177 | public class ReentrantReadWriteLockTest
177       * write-unlocking an unlocked lock throws IllegalMonitorStateException
178       */
179      public void testUnlock_IllegalMonitorStateException() {
180 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181 <        try {
182 <            rl.writeLock().unlock();
183 <            shouldThrow();
184 <        } catch (IllegalMonitorStateException success) {}
180 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181 >        try {
182 >            rl.writeLock().unlock();
183 >            shouldThrow();
184 >        } catch (IllegalMonitorStateException success) {}
185      }
186  
187  
# Line 188 | Line 189 | public class ReentrantReadWriteLockTest
189       * write-lockInterruptibly is interruptible
190       */
191      public void testWriteLockInterruptibly_Interrupted() throws Exception {
192 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
192 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
194              public void realRun() throws InterruptedException {
195                  lock.writeLock().lockInterruptibly();
196                  lock.writeLock().unlock();
# Line 210 | Line 211 | public class ReentrantReadWriteLockTest
211       * timed write-tryLock is interruptible
212       */
213      public void testWriteTryLock_Interrupted() throws InterruptedException {
214 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 <        lock.writeLock().lock();
216 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
214 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 >        lock.writeLock().lock();
216 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
217              public void realRun() throws InterruptedException {
218 <                lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
218 >                lock.writeLock().tryLock(1000,MILLISECONDS);
219              }});
220  
221          t.start();
# Line 227 | Line 228 | public class ReentrantReadWriteLockTest
228       * read-lockInterruptibly is interruptible
229       */
230      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
231 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
232 <        lock.writeLock().lock();
233 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
231 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
232 >        lock.writeLock().lock();
233 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
234              public void realRun() throws InterruptedException {
235                  lock.readLock().lockInterruptibly();
236              }});
# Line 246 | Line 247 | public class ReentrantReadWriteLockTest
247       * timed read-tryLock is interruptible
248       */
249      public void testReadTryLock_Interrupted() throws InterruptedException {
250 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
251 <        lock.writeLock().lock();
252 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
250 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
251 >        lock.writeLock().lock();
252 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
253              public void realRun() throws InterruptedException {
254 <                lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
254 >                lock.readLock().tryLock(1000,MILLISECONDS);
255              }});
256  
257          t.start();
# Line 263 | Line 264 | public class ReentrantReadWriteLockTest
264       * write-tryLock fails if locked
265       */
266      public void testWriteTryLockWhenLocked() throws InterruptedException {
267 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
268 <        lock.writeLock().lock();
269 <        Thread t = new Thread(new Runnable() {
267 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
268 >        lock.writeLock().lock();
269 >        Thread t = new Thread(new Runnable() {
270                  public void run() {
271                      threadAssertFalse(lock.writeLock().tryLock());
272 <                }
273 <            });
272 >                }
273 >            });
274  
275          t.start();
276          t.join();
# Line 280 | Line 281 | public class ReentrantReadWriteLockTest
281       * read-tryLock fails if locked
282       */
283      public void testReadTryLockWhenLocked() throws InterruptedException {
284 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 <        lock.writeLock().lock();
286 <        Thread t = new Thread(new Runnable() {
284 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 >        lock.writeLock().lock();
286 >        Thread t = new Thread(new Runnable() {
287                  public void run() {
288                      threadAssertFalse(lock.readLock().tryLock());
289 <                }
290 <            });
289 >                }
290 >            });
291  
292          t.start();
293          t.join();
# Line 297 | Line 298 | public class ReentrantReadWriteLockTest
298       * Multiple threads can hold a read lock when not write-locked
299       */
300      public void testMultipleReadLocks() throws InterruptedException {
301 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
302 <        lock.readLock().lock();
303 <        Thread t = new Thread(new Runnable() {
301 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
302 >        lock.readLock().lock();
303 >        Thread t = new Thread(new Runnable() {
304                  public void run() {
305                      threadAssertTrue(lock.readLock().tryLock());
306                      lock.readLock().unlock();
307 <                }
308 <            });
307 >                }
308 >            });
309  
310          t.start();
311          t.join();
# Line 315 | Line 316 | public class ReentrantReadWriteLockTest
316       * A writelock succeeds after reading threads unlock
317       */
318      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
319 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
320 <        lock.readLock().lock();
321 <        Thread t1 = new Thread(new Runnable() {
319 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
320 >        lock.readLock().lock();
321 >        Thread t1 = new Thread(new Runnable() {
322                  public void run() {
323                      lock.readLock().lock();
324                      lock.readLock().unlock();
325 <                }
326 <            });
327 <        Thread t2 = new Thread(new Runnable() {
325 >                }
326 >            });
327 >        Thread t2 = new Thread(new Runnable() {
328                  public void run() {
329                      lock.writeLock().lock();
330                      lock.writeLock().unlock();
331 <                }
332 <            });
331 >                }
332 >            });
333  
334          t1.start();
335          t2.start();
# Line 344 | Line 345 | public class ReentrantReadWriteLockTest
345       * Readlocks succeed after a writing thread unlocks
346       */
347      public void testReadAfterWriteLock() throws InterruptedException {
348 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
349 <        lock.writeLock().lock();
350 <        Thread t1 = new Thread(new Runnable() {
348 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
349 >        lock.writeLock().lock();
350 >        Thread t1 = new Thread(new Runnable() {
351                  public void run() {
352                      lock.readLock().lock();
353                      lock.readLock().unlock();
354 <                }
355 <            });
356 <        Thread t2 = new Thread(new Runnable() {
354 >                }
355 >            });
356 >        Thread t2 = new Thread(new Runnable() {
357                  public void run() {
358                      lock.readLock().lock();
359                      lock.readLock().unlock();
360 <                }
361 <            });
360 >                }
361 >            });
362  
363          t1.start();
364          t2.start();
# Line 373 | Line 374 | public class ReentrantReadWriteLockTest
374       * Read trylock succeeds if write locked by current thread
375       */
376      public void testReadHoldingWriteLock() {
377 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
378 <        lock.writeLock().lock();
377 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
378 >        lock.writeLock().lock();
379          assertTrue(lock.readLock().tryLock());
380          lock.readLock().unlock();
381          lock.writeLock().unlock();
# Line 385 | Line 386 | public class ReentrantReadWriteLockTest
386       * other threads are waiting for readlock
387       */
388      public void testReadHoldingWriteLock2() throws InterruptedException {
389 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
390 <        lock.writeLock().lock();
391 <        Thread t1 = new Thread(new Runnable() {
389 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
390 >        lock.writeLock().lock();
391 >        Thread t1 = new Thread(new Runnable() {
392                  public void run() {
393                      lock.readLock().lock();
394                      lock.readLock().unlock();
395 <                }
396 <            });
397 <        Thread t2 = new Thread(new Runnable() {
395 >                }
396 >            });
397 >        Thread t2 = new Thread(new Runnable() {
398                  public void run() {
399                      lock.readLock().lock();
400                      lock.readLock().unlock();
401 <                }
402 <            });
401 >                }
402 >            });
403  
404          t1.start();
405          t2.start();
# Line 419 | Line 420 | public class ReentrantReadWriteLockTest
420       * other threads are waiting for writelock
421       */
422      public void testReadHoldingWriteLock3() throws InterruptedException {
423 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
424 <        lock.writeLock().lock();
425 <        Thread t1 = new Thread(new Runnable() {
423 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
424 >        lock.writeLock().lock();
425 >        Thread t1 = new Thread(new Runnable() {
426                  public void run() {
427                      lock.writeLock().lock();
428                      lock.writeLock().unlock();
429 <                }
430 <            });
431 <        Thread t2 = new Thread(new Runnable() {
429 >                }
430 >            });
431 >        Thread t2 = new Thread(new Runnable() {
432                  public void run() {
433                      lock.writeLock().lock();
434                      lock.writeLock().unlock();
435 <                }
436 <            });
435 >                }
436 >            });
437  
438          t1.start();
439          t2.start();
# Line 454 | Line 455 | public class ReentrantReadWriteLockTest
455       * other threads are waiting for writelock
456       */
457      public void testWriteHoldingWriteLock4() throws InterruptedException {
458 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
459 <        lock.writeLock().lock();
460 <        Thread t1 = new Thread(new Runnable() {
458 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
459 >        lock.writeLock().lock();
460 >        Thread t1 = new Thread(new Runnable() {
461                  public void run() {
462                      lock.writeLock().lock();
463                      lock.writeLock().unlock();
464 <                }
465 <            });
466 <        Thread t2 = new Thread(new Runnable() {
464 >                }
465 >            });
466 >        Thread t2 = new Thread(new Runnable() {
467                  public void run() {
468                      lock.writeLock().lock();
469                      lock.writeLock().unlock();
470 <                }
471 <            });
470 >                }
471 >            });
472  
473          t1.start();
474          t2.start();
# Line 488 | Line 489 | public class ReentrantReadWriteLockTest
489       * Fair Read trylock succeeds if write locked by current thread
490       */
491      public void testReadHoldingWriteLockFair() {
492 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
493 <        lock.writeLock().lock();
492 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
493 >        lock.writeLock().lock();
494          assertTrue(lock.readLock().tryLock());
495          lock.readLock().unlock();
496          lock.writeLock().unlock();
# Line 500 | Line 501 | public class ReentrantReadWriteLockTest
501       * other threads are waiting for readlock
502       */
503      public void testReadHoldingWriteLockFair2() throws InterruptedException {
504 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
505 <        lock.writeLock().lock();
506 <        Thread t1 = new Thread(new Runnable() {
504 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
505 >        lock.writeLock().lock();
506 >        Thread t1 = new Thread(new Runnable() {
507                  public void run() {
508                      lock.readLock().lock();
509                      lock.readLock().unlock();
510 <                }
511 <            });
512 <        Thread t2 = new Thread(new Runnable() {
510 >                }
511 >            });
512 >        Thread t2 = new Thread(new Runnable() {
513                  public void run() {
514                      lock.readLock().lock();
515                      lock.readLock().unlock();
516 <                }
517 <            });
516 >                }
517 >            });
518  
519          t1.start();
520          t2.start();
# Line 535 | Line 536 | public class ReentrantReadWriteLockTest
536       * other threads are waiting for writelock
537       */
538      public void testReadHoldingWriteLockFair3() throws InterruptedException {
539 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
540 <        lock.writeLock().lock();
541 <        Thread t1 = new Thread(new Runnable() {
539 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
540 >        lock.writeLock().lock();
541 >        Thread t1 = new Thread(new Runnable() {
542                  public void run() {
543                      lock.writeLock().lock();
544                      lock.writeLock().unlock();
545 <                }
546 <            });
547 <        Thread t2 = new Thread(new Runnable() {
545 >                }
546 >            });
547 >        Thread t2 = new Thread(new Runnable() {
548                  public void run() {
549                      lock.writeLock().lock();
550                      lock.writeLock().unlock();
551 <                }
552 <            });
551 >                }
552 >            });
553  
554          t1.start();
555          t2.start();
# Line 570 | Line 571 | public class ReentrantReadWriteLockTest
571       * other threads are waiting for writelock
572       */
573      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
574 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
575 <        lock.writeLock().lock();
576 <        Thread t1 = new Thread(new Runnable() {
574 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
575 >        lock.writeLock().lock();
576 >        Thread t1 = new Thread(new Runnable() {
577                  public void run() {
578                      lock.writeLock().lock();
579                      lock.writeLock().unlock();
580 <                }
581 <            });
582 <        Thread t2 = new Thread(new Runnable() {
580 >                }
581 >            });
582 >        Thread t2 = new Thread(new Runnable() {
583                  public void run() {
584                      lock.writeLock().lock();
585                      lock.writeLock().unlock();
586 <                }
587 <            });
586 >                }
587 >            });
588  
589          t1.start();
590          t2.start();
# Line 607 | Line 608 | public class ReentrantReadWriteLockTest
608       * Read tryLock succeeds if readlocked but not writelocked
609       */
610      public void testTryLockWhenReadLocked() throws InterruptedException {
611 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
612 <        lock.readLock().lock();
613 <        Thread t = new Thread(new Runnable() {
611 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
612 >        lock.readLock().lock();
613 >        Thread t = new Thread(new Runnable() {
614                  public void run() {
615                      threadAssertTrue(lock.readLock().tryLock());
616                      lock.readLock().unlock();
617 <                }
618 <            });
617 >                }
618 >            });
619  
620          t.start();
621          t.join();
# Line 627 | Line 628 | public class ReentrantReadWriteLockTest
628       * write tryLock fails when readlocked
629       */
630      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
631 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
632 <        lock.readLock().lock();
633 <        Thread t = new Thread(new Runnable() {
631 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
632 >        lock.readLock().lock();
633 >        Thread t = new Thread(new Runnable() {
634                  public void run() {
635                      threadAssertFalse(lock.writeLock().tryLock());
636 <                }
637 <            });
636 >                }
637 >            });
638  
639          t.start();
640          t.join();
# Line 645 | Line 646 | public class ReentrantReadWriteLockTest
646       * Fair Read tryLock succeeds if readlocked but not writelocked
647       */
648      public void testTryLockWhenReadLockedFair() throws InterruptedException {
649 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
650 <        lock.readLock().lock();
651 <        Thread t = new Thread(new Runnable() {
649 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
650 >        lock.readLock().lock();
651 >        Thread t = new Thread(new Runnable() {
652                  public void run() {
653                      threadAssertTrue(lock.readLock().tryLock());
654                      lock.readLock().unlock();
655 <                }
656 <            });
655 >                }
656 >            });
657  
658          t.start();
659          t.join();
# Line 665 | Line 666 | public class ReentrantReadWriteLockTest
666       * Fair write tryLock fails when readlocked
667       */
668      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
669 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
670 <        lock.readLock().lock();
671 <        Thread t = new Thread(new Runnable() {
669 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
670 >        lock.readLock().lock();
671 >        Thread t = new Thread(new Runnable() {
672                  public void run() {
673                      threadAssertFalse(lock.writeLock().tryLock());
674 <                }
675 <            });
674 >                }
675 >            });
676  
677          t.start();
678          t.join();
# Line 684 | Line 685 | public class ReentrantReadWriteLockTest
685       * write timed tryLock times out if locked
686       */
687      public void testWriteTryLock_Timeout() throws InterruptedException {
688 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
689 <        lock.writeLock().lock();
690 <        Thread t = new Thread(new CheckedRunnable() {
688 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
689 >        lock.writeLock().lock();
690 >        Thread t = new Thread(new CheckedRunnable() {
691              public void realRun() throws InterruptedException {
692 <                threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
692 >                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
693              }});
694  
695          t.start();
696          t.join();
697 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
698          lock.writeLock().unlock();
699      }
700  
# Line 700 | Line 702 | public class ReentrantReadWriteLockTest
702       * read timed tryLock times out if write-locked
703       */
704      public void testReadTryLock_Timeout() throws InterruptedException {
705 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
706 <        lock.writeLock().lock();
707 <        Thread t = new Thread(new CheckedRunnable() {
705 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
706 >        lock.writeLock().lock();
707 >        Thread t = new Thread(new CheckedRunnable() {
708              public void realRun() throws InterruptedException {
709 <                threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
709 >                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
710              }});
711  
712          t.start();
713          t.join();
714 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
715          lock.writeLock().unlock();
716      }
717  
# Line 717 | Line 720 | public class ReentrantReadWriteLockTest
720       * write lockInterruptibly succeeds if lock free else is interruptible
721       */
722      public void testWriteLockInterruptibly() throws InterruptedException {
723 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
723 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
724          lock.writeLock().lockInterruptibly();
725 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
725 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
726              public void realRun() throws InterruptedException {
727                  lock.writeLock().lockInterruptibly();
728              }});
# Line 736 | Line 739 | public class ReentrantReadWriteLockTest
739       *  read lockInterruptibly succeeds if lock free else is interruptible
740       */
741      public void testReadLockInterruptibly() throws InterruptedException {
742 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
742 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
743          lock.writeLock().lockInterruptibly();
744 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
744 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
745              public void realRun() throws InterruptedException {
746                  lock.readLock().lockInterruptibly();
747              }});
# Line 754 | Line 757 | public class ReentrantReadWriteLockTest
757       * Calling await without holding lock throws IllegalMonitorStateException
758       */
759      public void testAwait_IllegalMonitor() throws InterruptedException {
760 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
760 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
761          final Condition c = lock.writeLock().newCondition();
762          try {
763              c.await();
# Line 766 | Line 769 | public class ReentrantReadWriteLockTest
769       * Calling signal without holding lock throws IllegalMonitorStateException
770       */
771      public void testSignal_IllegalMonitor() {
772 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
772 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
773          final Condition c = lock.writeLock().newCondition();
774          try {
775              c.signal();
# Line 778 | Line 781 | public class ReentrantReadWriteLockTest
781       * awaitNanos without a signal times out
782       */
783      public void testAwaitNanos_Timeout() throws InterruptedException {
784 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
784 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
785          final Condition c = lock.writeLock().newCondition();
786  
787          lock.writeLock().lock();
# Line 791 | Line 794 | public class ReentrantReadWriteLockTest
794      /**
795       *  timed await without a signal times out
796       */
797 <    public void testAwait_Timeout() {
798 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
797 >    public void testAwait_Timeout() throws InterruptedException {
798 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
799          final Condition c = lock.writeLock().newCondition();
800          lock.writeLock().lock();
801 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
802          lock.writeLock().unlock();
803      }
804  
805      /**
806       * awaitUntil without a signal times out
807       */
808 <    public void testAwaitUntil_Timeout() {
809 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
808 >    public void testAwaitUntil_Timeout() throws InterruptedException {
809 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
810          final Condition c = lock.writeLock().newCondition();
811          lock.writeLock().lock();
812          java.util.Date d = new java.util.Date();
813 +        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
814          lock.writeLock().unlock();
815      }
816  
# Line 813 | Line 818 | public class ReentrantReadWriteLockTest
818       * await returns when signalled
819       */
820      public void testAwait() throws InterruptedException {
821 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
821 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
822          final Condition c = lock.writeLock().newCondition();
823 <        Thread t = new Thread(new CheckedRunnable() {
823 >        Thread t = new Thread(new CheckedRunnable() {
824              public void realRun() throws InterruptedException {
825                  lock.writeLock().lock();
826                  c.await();
# Line 890 | Line 895 | public class ReentrantReadWriteLockTest
895       * await is interruptible
896       */
897      public void testAwait_Interrupt() throws InterruptedException {
898 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
898 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
899          final Condition c = lock.writeLock().newCondition();
900 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
900 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
901              public void realRun() throws InterruptedException {
902                  lock.writeLock().lock();
903                  c.await();
# Line 910 | Line 915 | public class ReentrantReadWriteLockTest
915       * awaitNanos is interruptible
916       */
917      public void testAwaitNanos_Interrupt() throws InterruptedException {
918 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
918 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
919          final Condition c = lock.writeLock().newCondition();
920 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
920 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
921              public void realRun() throws InterruptedException {
922                  lock.writeLock().lock();
923                  c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
# Line 930 | Line 935 | public class ReentrantReadWriteLockTest
935       * awaitUntil is interruptible
936       */
937      public void testAwaitUntil_Interrupt() throws InterruptedException {
938 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
939          final Condition c = lock.writeLock().newCondition();
940 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
940 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
941              public void realRun() throws InterruptedException {
942                  lock.writeLock().lock();
943                  java.util.Date d = new java.util.Date();
# Line 951 | Line 956 | public class ReentrantReadWriteLockTest
956       * signalAll wakes up all threads
957       */
958      public void testSignalAll() throws InterruptedException {
959 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
959 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
960          final Condition c = lock.writeLock().newCondition();
961 <        Thread t1 = new Thread(new CheckedRunnable() {
961 >        Thread t1 = new Thread(new CheckedRunnable() {
962              public void realRun() throws InterruptedException {
963                  lock.writeLock().lock();
964                  c.await();
965                  lock.writeLock().unlock();
966              }});
967  
968 <        Thread t2 = new Thread(new CheckedRunnable() {
968 >        Thread t2 = new Thread(new CheckedRunnable() {
969              public void realRun() throws InterruptedException {
970                  lock.writeLock().lock();
971                  c.await();
# Line 1003 | Line 1008 | public class ReentrantReadWriteLockTest
1008       * hasQueuedThreads reports whether there are waiting threads
1009       */
1010      public void testhasQueuedThreads() throws InterruptedException {
1011 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1011 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1012          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1013          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1014          assertFalse(lock.hasQueuedThreads());
# Line 1028 | Line 1033 | public class ReentrantReadWriteLockTest
1033       * hasQueuedThread(null) throws NPE
1034       */
1035      public void testHasQueuedThreadNPE() {
1036 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1036 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1037          try {
1038              sync.hasQueuedThread(null);
1039              shouldThrow();
# Line 1039 | Line 1044 | public class ReentrantReadWriteLockTest
1044       * hasQueuedThread reports whether a thread is queued.
1045       */
1046      public void testHasQueuedThread() throws InterruptedException {
1047 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1047 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1048          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1049          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1050          assertFalse(sync.hasQueuedThread(t1));
# Line 1070 | Line 1075 | public class ReentrantReadWriteLockTest
1075       * getQueueLength reports number of waiting threads
1076       */
1077      public void testGetQueueLength() throws InterruptedException {
1078 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1078 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1079          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1080          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1081          assertEquals(0, lock.getQueueLength());
# Line 1095 | Line 1100 | public class ReentrantReadWriteLockTest
1100       * getQueuedThreads includes waiting threads
1101       */
1102      public void testGetQueuedThreads() throws InterruptedException {
1103 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1103 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1104          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1105          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1106          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1123 | Line 1128 | public class ReentrantReadWriteLockTest
1128       * hasWaiters throws NPE if null
1129       */
1130      public void testHasWaitersNPE() {
1131 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1131 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1132          try {
1133              lock.hasWaiters(null);
1134              shouldThrow();
# Line 1134 | Line 1139 | public class ReentrantReadWriteLockTest
1139       * getWaitQueueLength throws NPE if null
1140       */
1141      public void testGetWaitQueueLengthNPE() {
1142 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1142 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1143          try {
1144              lock.getWaitQueueLength(null);
1145              shouldThrow();
# Line 1146 | Line 1151 | public class ReentrantReadWriteLockTest
1151       * getWaitingThreads throws NPE if null
1152       */
1153      public void testGetWaitingThreadsNPE() {
1154 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1154 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1155          try {
1156              lock.getWaitingThreads(null);
1157              shouldThrow();
# Line 1157 | Line 1162 | public class ReentrantReadWriteLockTest
1162       * hasWaiters throws IAE if not owned
1163       */
1164      public void testHasWaitersIAE() {
1165 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1165 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1166          final Condition c = lock.writeLock().newCondition();
1167 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1167 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1168          try {
1169              lock2.hasWaiters(c);
1170              shouldThrow();
# Line 1170 | Line 1175 | public class ReentrantReadWriteLockTest
1175       * hasWaiters throws IMSE if not locked
1176       */
1177      public void testHasWaitersIMSE() {
1178 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1178 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1179          final Condition c = lock.writeLock().newCondition();
1180          try {
1181              lock.hasWaiters(c);
# Line 1183 | Line 1188 | public class ReentrantReadWriteLockTest
1188       * getWaitQueueLength throws IAE if not owned
1189       */
1190      public void testGetWaitQueueLengthIAE() {
1191 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1191 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1192          final Condition c = lock.writeLock().newCondition();
1193 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1193 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1194          try {
1195              lock2.getWaitQueueLength(c);
1196              shouldThrow();
# Line 1196 | Line 1201 | public class ReentrantReadWriteLockTest
1201       * getWaitQueueLength throws IMSE if not locked
1202       */
1203      public void testGetWaitQueueLengthIMSE() {
1204 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1204 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1205          final Condition c = lock.writeLock().newCondition();
1206          try {
1207              lock.getWaitQueueLength(c);
# Line 1209 | Line 1214 | public class ReentrantReadWriteLockTest
1214       * getWaitingThreads throws IAE if not owned
1215       */
1216      public void testGetWaitingThreadsIAE() {
1217 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1217 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1218          final Condition c = lock.writeLock().newCondition();
1219 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1219 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1220          try {
1221              lock2.getWaitingThreads(c);
1222              shouldThrow();
# Line 1222 | Line 1227 | public class ReentrantReadWriteLockTest
1227       * getWaitingThreads throws IMSE if not locked
1228       */
1229      public void testGetWaitingThreadsIMSE() {
1230 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1230 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1231          final Condition c = lock.writeLock().newCondition();
1232          try {
1233              lock.getWaitingThreads(c);
# Line 1235 | Line 1240 | public class ReentrantReadWriteLockTest
1240       * hasWaiters returns true when a thread is waiting, else false
1241       */
1242      public void testHasWaiters() throws InterruptedException {
1243 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1243 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1244          final Condition c = lock.writeLock().newCondition();
1245 <        Thread t = new Thread(new CheckedRunnable() {
1245 >        Thread t = new Thread(new CheckedRunnable() {
1246              public void realRun() throws InterruptedException {
1247                  lock.writeLock().lock();
1248                  threadAssertFalse(lock.hasWaiters(c));
# Line 1266 | Line 1271 | public class ReentrantReadWriteLockTest
1271       * getWaitQueueLength returns number of waiting threads
1272       */
1273      public void testGetWaitQueueLength() throws InterruptedException {
1274 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1274 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1275          final Condition c = lock.writeLock().newCondition();
1276 <        Thread t = new Thread(new CheckedRunnable() {
1276 >        Thread t = new Thread(new CheckedRunnable() {
1277              public void realRun() throws InterruptedException {
1278                  lock.writeLock().lock();
1279                  threadAssertFalse(lock.hasWaiters(c));
# Line 1298 | Line 1303 | public class ReentrantReadWriteLockTest
1303       * getWaitingThreads returns only and all waiting threads
1304       */
1305      public void testGetWaitingThreads() throws InterruptedException {
1306 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1306 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1307          final Condition c = lock.writeLock().newCondition();
1308 <        Thread t1 = new Thread(new CheckedRunnable() {
1308 >        Thread t1 = new Thread(new CheckedRunnable() {
1309              public void realRun() throws InterruptedException {
1310                  lock.writeLock().lock();
1311                  threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 1308 | Line 1313 | public class ReentrantReadWriteLockTest
1313                  lock.writeLock().unlock();
1314              }});
1315  
1316 <        Thread t2 = new Thread(new CheckedRunnable() {
1316 >        Thread t2 = new Thread(new CheckedRunnable() {
1317              public void realRun() throws InterruptedException {
1318                  lock.writeLock().lock();
1319                  threadAssertFalse(lock.getWaitingThreads(c).isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines