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.40 by jsr166, Tue Dec 1 06:03:49 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(SMALL_DELAY_MS, MILLISECONDS);
219              }});
220  
221          t.start();
222 +        Thread.sleep(SHORT_DELAY_MS);
223          t.interrupt();
224          lock.writeLock().unlock();
225          t.join();
# Line 227 | Line 229 | public class ReentrantReadWriteLockTest
229       * read-lockInterruptibly is interruptible
230       */
231      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
232 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 <        lock.writeLock().lock();
234 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
232 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 >        lock.writeLock().lock();
234 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
235              public void realRun() throws InterruptedException {
236                  lock.readLock().lockInterruptibly();
237              }});
# Line 246 | Line 248 | public class ReentrantReadWriteLockTest
248       * timed read-tryLock is interruptible
249       */
250      public void testReadTryLock_Interrupted() throws InterruptedException {
251 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 <        lock.writeLock().lock();
253 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
251 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 >        lock.writeLock().lock();
253 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
254              public void realRun() throws InterruptedException {
255 <                lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
255 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
256              }});
257  
258          t.start();
259 +        Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261          t.join();
262      }
# Line 263 | Line 266 | public class ReentrantReadWriteLockTest
266       * write-tryLock fails if locked
267       */
268      public void testWriteTryLockWhenLocked() throws InterruptedException {
269 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
270 <        lock.writeLock().lock();
271 <        Thread t = new Thread(new Runnable() {
272 <                public void run() {
273 <                    threadAssertFalse(lock.writeLock().tryLock());
274 <                }
272 <            });
269 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
270 >        lock.writeLock().lock();
271 >        Thread t = new Thread(new CheckedRunnable() {
272 >            public void realRun() {
273 >                threadAssertFalse(lock.writeLock().tryLock());
274 >            }});
275  
276          t.start();
277          t.join();
# Line 280 | Line 282 | public class ReentrantReadWriteLockTest
282       * read-tryLock fails if locked
283       */
284      public void testReadTryLockWhenLocked() throws InterruptedException {
285 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 <        lock.writeLock().lock();
287 <        Thread t = new Thread(new Runnable() {
288 <                public void run() {
289 <                    threadAssertFalse(lock.readLock().tryLock());
290 <                }
289 <            });
285 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 >        lock.writeLock().lock();
287 >        Thread t = new Thread(new CheckedRunnable() {
288 >            public void realRun() {
289 >                threadAssertFalse(lock.readLock().tryLock());
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() {
304 <                public void run() {
305 <                    threadAssertTrue(lock.readLock().tryLock());
306 <                    lock.readLock().unlock();
307 <                }
307 <            });
301 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
302 >        lock.readLock().lock();
303 >        Thread t = new Thread(new CheckedRunnable() {
304 >            public void realRun() {
305 >                threadAssertTrue(lock.readLock().tryLock());
306 >                lock.readLock().unlock();
307 >            }});
308  
309          t.start();
310          t.join();
# Line 315 | Line 315 | public class ReentrantReadWriteLockTest
315       * A writelock succeeds after reading threads unlock
316       */
317      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
318 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319 <        lock.readLock().lock();
320 <        Thread t1 = new Thread(new Runnable() {
321 <                public void run() {
322 <                    lock.readLock().lock();
323 <                    lock.readLock().unlock();
324 <                }
325 <            });
326 <        Thread t2 = new Thread(new Runnable() {
327 <                public void run() {
328 <                    lock.writeLock().lock();
329 <                    lock.writeLock().unlock();
330 <                }
331 <            });
318 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319 >        lock.readLock().lock();
320 >        Thread t1 = new Thread(new CheckedRunnable() {
321 >            public void realRun() {
322 >                lock.readLock().lock();
323 >                lock.readLock().unlock();
324 >            }});
325 >        Thread t2 = new Thread(new CheckedRunnable() {
326 >            public void realRun() {
327 >                lock.writeLock().lock();
328 >                lock.writeLock().unlock();
329 >            }});
330  
331          t1.start();
332          t2.start();
# Line 344 | Line 342 | public class ReentrantReadWriteLockTest
342       * Readlocks succeed after a writing thread unlocks
343       */
344      public void testReadAfterWriteLock() throws InterruptedException {
345 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
346 <        lock.writeLock().lock();
347 <        Thread t1 = new Thread(new Runnable() {
348 <                public void run() {
349 <                    lock.readLock().lock();
350 <                    lock.readLock().unlock();
351 <                }
352 <            });
353 <        Thread t2 = new Thread(new Runnable() {
354 <                public void run() {
355 <                    lock.readLock().lock();
356 <                    lock.readLock().unlock();
359 <                }
360 <            });
345 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
346 >        lock.writeLock().lock();
347 >        Thread t1 = new Thread(new CheckedRunnable() {
348 >            public void realRun() {
349 >                lock.readLock().lock();
350 >                lock.readLock().unlock();
351 >            }});
352 >        Thread t2 = new Thread(new CheckedRunnable() {
353 >            public void realRun() {
354 >                lock.readLock().lock();
355 >                lock.readLock().unlock();
356 >            }});
357  
358          t1.start();
359          t2.start();
# Line 373 | Line 369 | public class ReentrantReadWriteLockTest
369       * Read trylock succeeds if write locked by current thread
370       */
371      public void testReadHoldingWriteLock() {
372 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
373 <        lock.writeLock().lock();
372 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
373 >        lock.writeLock().lock();
374          assertTrue(lock.readLock().tryLock());
375          lock.readLock().unlock();
376          lock.writeLock().unlock();
# Line 385 | Line 381 | public class ReentrantReadWriteLockTest
381       * other threads are waiting for readlock
382       */
383      public void testReadHoldingWriteLock2() throws InterruptedException {
384 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
385 <        lock.writeLock().lock();
386 <        Thread t1 = new Thread(new Runnable() {
387 <                public void run() {
388 <                    lock.readLock().lock();
389 <                    lock.readLock().unlock();
390 <                }
391 <            });
392 <        Thread t2 = new Thread(new Runnable() {
393 <                public void run() {
394 <                    lock.readLock().lock();
395 <                    lock.readLock().unlock();
400 <                }
401 <            });
384 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
385 >        lock.writeLock().lock();
386 >        Thread t1 = new Thread(new CheckedRunnable() {
387 >            public void realRun() {
388 >                lock.readLock().lock();
389 >                lock.readLock().unlock();
390 >            }});
391 >        Thread t2 = new Thread(new CheckedRunnable() {
392 >            public void realRun() {
393 >                lock.readLock().lock();
394 >                lock.readLock().unlock();
395 >            }});
396  
397          t1.start();
398          t2.start();
# Line 419 | Line 413 | public class ReentrantReadWriteLockTest
413       * other threads are waiting for writelock
414       */
415      public void testReadHoldingWriteLock3() throws InterruptedException {
416 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
417 <        lock.writeLock().lock();
418 <        Thread t1 = new Thread(new Runnable() {
419 <                public void run() {
420 <                    lock.writeLock().lock();
421 <                    lock.writeLock().unlock();
422 <                }
423 <            });
424 <        Thread t2 = new Thread(new Runnable() {
425 <                public void run() {
426 <                    lock.writeLock().lock();
427 <                    lock.writeLock().unlock();
434 <                }
435 <            });
416 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
417 >        lock.writeLock().lock();
418 >        Thread t1 = new Thread(new CheckedRunnable() {
419 >            public void realRun() {
420 >                lock.writeLock().lock();
421 >                lock.writeLock().unlock();
422 >            }});
423 >        Thread t2 = new Thread(new CheckedRunnable() {
424 >            public void realRun() {
425 >                lock.writeLock().lock();
426 >                lock.writeLock().unlock();
427 >            }});
428  
429          t1.start();
430          t2.start();
# Line 454 | Line 446 | public class ReentrantReadWriteLockTest
446       * other threads are waiting for writelock
447       */
448      public void testWriteHoldingWriteLock4() throws InterruptedException {
449 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450 <        lock.writeLock().lock();
451 <        Thread t1 = new Thread(new Runnable() {
452 <                public void run() {
453 <                    lock.writeLock().lock();
454 <                    lock.writeLock().unlock();
455 <                }
456 <            });
457 <        Thread t2 = new Thread(new Runnable() {
458 <                public void run() {
459 <                    lock.writeLock().lock();
460 <                    lock.writeLock().unlock();
469 <                }
470 <            });
449 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450 >        lock.writeLock().lock();
451 >        Thread t1 = new Thread(new CheckedRunnable() {
452 >            public void realRun() {
453 >                lock.writeLock().lock();
454 >                lock.writeLock().unlock();
455 >            }});
456 >        Thread t2 = new Thread(new CheckedRunnable() {
457 >            public void realRun() {
458 >                lock.writeLock().lock();
459 >                lock.writeLock().unlock();
460 >            }});
461  
462          t1.start();
463          t2.start();
# Line 488 | Line 478 | public class ReentrantReadWriteLockTest
478       * Fair Read trylock succeeds if write locked by current thread
479       */
480      public void testReadHoldingWriteLockFair() {
481 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
482 <        lock.writeLock().lock();
481 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
482 >        lock.writeLock().lock();
483          assertTrue(lock.readLock().tryLock());
484          lock.readLock().unlock();
485          lock.writeLock().unlock();
# Line 500 | Line 490 | public class ReentrantReadWriteLockTest
490       * other threads are waiting for readlock
491       */
492      public void testReadHoldingWriteLockFair2() throws InterruptedException {
493 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
494 <        lock.writeLock().lock();
495 <        Thread t1 = new Thread(new Runnable() {
496 <                public void run() {
497 <                    lock.readLock().lock();
498 <                    lock.readLock().unlock();
499 <                }
500 <            });
501 <        Thread t2 = new Thread(new Runnable() {
502 <                public void run() {
503 <                    lock.readLock().lock();
504 <                    lock.readLock().unlock();
515 <                }
516 <            });
493 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
494 >        lock.writeLock().lock();
495 >        Thread t1 = new Thread(new CheckedRunnable() {
496 >            public void realRun() {
497 >                lock.readLock().lock();
498 >                lock.readLock().unlock();
499 >            }});
500 >        Thread t2 = new Thread(new CheckedRunnable() {
501 >            public void realRun() {
502 >                lock.readLock().lock();
503 >                lock.readLock().unlock();
504 >            }});
505  
506          t1.start();
507          t2.start();
# Line 535 | Line 523 | public class ReentrantReadWriteLockTest
523       * other threads are waiting for writelock
524       */
525      public void testReadHoldingWriteLockFair3() throws InterruptedException {
526 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
527 <        lock.writeLock().lock();
528 <        Thread t1 = new Thread(new Runnable() {
529 <                public void run() {
530 <                    lock.writeLock().lock();
531 <                    lock.writeLock().unlock();
532 <                }
533 <            });
534 <        Thread t2 = new Thread(new Runnable() {
535 <                public void run() {
536 <                    lock.writeLock().lock();
537 <                    lock.writeLock().unlock();
550 <                }
551 <            });
526 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
527 >        lock.writeLock().lock();
528 >        Thread t1 = new Thread(new CheckedRunnable() {
529 >            public void realRun() {
530 >                lock.writeLock().lock();
531 >                lock.writeLock().unlock();
532 >            }});
533 >        Thread t2 = new Thread(new CheckedRunnable() {
534 >            public void realRun() {
535 >                lock.writeLock().lock();
536 >                lock.writeLock().unlock();
537 >            }});
538  
539          t1.start();
540          t2.start();
# Line 570 | Line 556 | public class ReentrantReadWriteLockTest
556       * other threads are waiting for writelock
557       */
558      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
559 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
560 <        lock.writeLock().lock();
561 <        Thread t1 = new Thread(new Runnable() {
562 <                public void run() {
563 <                    lock.writeLock().lock();
564 <                    lock.writeLock().unlock();
565 <                }
566 <            });
567 <        Thread t2 = new Thread(new Runnable() {
568 <                public void run() {
569 <                    lock.writeLock().lock();
570 <                    lock.writeLock().unlock();
585 <                }
586 <            });
559 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
560 >        lock.writeLock().lock();
561 >        Thread t1 = new Thread(new CheckedRunnable() {
562 >            public void realRun() {
563 >                lock.writeLock().lock();
564 >                lock.writeLock().unlock();
565 >            }});
566 >        Thread t2 = new Thread(new CheckedRunnable() {
567 >            public void realRun() {
568 >                lock.writeLock().lock();
569 >                lock.writeLock().unlock();
570 >            }});
571  
572          t1.start();
573          t2.start();
# Line 607 | Line 591 | public class ReentrantReadWriteLockTest
591       * Read tryLock succeeds if readlocked but not writelocked
592       */
593      public void testTryLockWhenReadLocked() throws InterruptedException {
594 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
595 <        lock.readLock().lock();
596 <        Thread t = new Thread(new Runnable() {
597 <                public void run() {
598 <                    threadAssertTrue(lock.readLock().tryLock());
599 <                    lock.readLock().unlock();
600 <                }
617 <            });
594 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
595 >        lock.readLock().lock();
596 >        Thread t = new Thread(new CheckedRunnable() {
597 >            public void realRun() {
598 >                threadAssertTrue(lock.readLock().tryLock());
599 >                lock.readLock().unlock();
600 >            }});
601  
602          t.start();
603          t.join();
# Line 627 | Line 610 | public class ReentrantReadWriteLockTest
610       * write tryLock fails when readlocked
611       */
612      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
613 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
614 <        lock.readLock().lock();
615 <        Thread t = new Thread(new Runnable() {
616 <                public void run() {
617 <                    threadAssertFalse(lock.writeLock().tryLock());
618 <                }
636 <            });
613 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
614 >        lock.readLock().lock();
615 >        Thread t = new Thread(new CheckedRunnable() {
616 >            public void realRun() {
617 >                threadAssertFalse(lock.writeLock().tryLock());
618 >            }});
619  
620          t.start();
621          t.join();
# Line 645 | Line 627 | public class ReentrantReadWriteLockTest
627       * Fair Read tryLock succeeds if readlocked but not writelocked
628       */
629      public void testTryLockWhenReadLockedFair() throws InterruptedException {
630 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
631 <        lock.readLock().lock();
632 <        Thread t = new Thread(new Runnable() {
633 <                public void run() {
634 <                    threadAssertTrue(lock.readLock().tryLock());
635 <                    lock.readLock().unlock();
636 <                }
655 <            });
630 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
631 >        lock.readLock().lock();
632 >        Thread t = new Thread(new CheckedRunnable() {
633 >            public void realRun() {
634 >                threadAssertTrue(lock.readLock().tryLock());
635 >                lock.readLock().unlock();
636 >            }});
637  
638          t.start();
639          t.join();
# Line 665 | Line 646 | public class ReentrantReadWriteLockTest
646       * Fair write tryLock fails when readlocked
647       */
648      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
649 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
650 <        lock.readLock().lock();
651 <        Thread t = new Thread(new Runnable() {
652 <                public void run() {
653 <                    threadAssertFalse(lock.writeLock().tryLock());
654 <                }
674 <            });
649 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
650 >        lock.readLock().lock();
651 >        Thread t = new Thread(new CheckedRunnable() {
652 >            public void realRun() {
653 >                threadAssertFalse(lock.writeLock().tryLock());
654 >            }});
655  
656          t.start();
657          t.join();
# Line 684 | Line 664 | public class ReentrantReadWriteLockTest
664       * write timed tryLock times out if locked
665       */
666      public void testWriteTryLock_Timeout() throws InterruptedException {
667 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
668 <        lock.writeLock().lock();
669 <        Thread t = new Thread(new CheckedRunnable() {
667 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
668 >        lock.writeLock().lock();
669 >        Thread t = new Thread(new CheckedRunnable() {
670              public void realRun() throws InterruptedException {
671 <                threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
671 >                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
672              }});
673  
674          t.start();
675          t.join();
676 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
677          lock.writeLock().unlock();
678      }
679  
# Line 700 | Line 681 | public class ReentrantReadWriteLockTest
681       * read timed tryLock times out if write-locked
682       */
683      public void testReadTryLock_Timeout() throws InterruptedException {
684 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
685 <        lock.writeLock().lock();
686 <        Thread t = new Thread(new CheckedRunnable() {
684 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
685 >        lock.writeLock().lock();
686 >        Thread t = new Thread(new CheckedRunnable() {
687              public void realRun() throws InterruptedException {
688 <                threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
688 >                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
689              }});
690  
691          t.start();
692          t.join();
693 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
694          lock.writeLock().unlock();
695      }
696  
# Line 717 | Line 699 | public class ReentrantReadWriteLockTest
699       * write lockInterruptibly succeeds if lock free else is interruptible
700       */
701      public void testWriteLockInterruptibly() throws InterruptedException {
702 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
702 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
703          lock.writeLock().lockInterruptibly();
704 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
704 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
705              public void realRun() throws InterruptedException {
706                  lock.writeLock().lockInterruptibly();
707              }});
# Line 736 | Line 718 | public class ReentrantReadWriteLockTest
718       *  read lockInterruptibly succeeds if lock free else is interruptible
719       */
720      public void testReadLockInterruptibly() throws InterruptedException {
721 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
721 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
722          lock.writeLock().lockInterruptibly();
723 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
723 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
724              public void realRun() throws InterruptedException {
725                  lock.readLock().lockInterruptibly();
726              }});
# Line 754 | Line 736 | public class ReentrantReadWriteLockTest
736       * Calling await without holding lock throws IllegalMonitorStateException
737       */
738      public void testAwait_IllegalMonitor() throws InterruptedException {
739 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
740          final Condition c = lock.writeLock().newCondition();
741          try {
742              c.await();
# Line 766 | Line 748 | public class ReentrantReadWriteLockTest
748       * Calling signal without holding lock throws IllegalMonitorStateException
749       */
750      public void testSignal_IllegalMonitor() {
751 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
751 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
752          final Condition c = lock.writeLock().newCondition();
753          try {
754              c.signal();
# Line 778 | Line 760 | public class ReentrantReadWriteLockTest
760       * awaitNanos without a signal times out
761       */
762      public void testAwaitNanos_Timeout() throws InterruptedException {
763 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
763 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
764          final Condition c = lock.writeLock().newCondition();
765  
766          lock.writeLock().lock();
# Line 791 | Line 773 | public class ReentrantReadWriteLockTest
773      /**
774       *  timed await without a signal times out
775       */
776 <    public void testAwait_Timeout() {
777 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
776 >    public void testAwait_Timeout() throws InterruptedException {
777 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
778          final Condition c = lock.writeLock().newCondition();
779          lock.writeLock().lock();
780 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
781          lock.writeLock().unlock();
782      }
783  
784      /**
785       * awaitUntil without a signal times out
786       */
787 <    public void testAwaitUntil_Timeout() {
788 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
787 >    public void testAwaitUntil_Timeout() throws InterruptedException {
788 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
789          final Condition c = lock.writeLock().newCondition();
790          lock.writeLock().lock();
791          java.util.Date d = new java.util.Date();
792 +        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
793          lock.writeLock().unlock();
794      }
795  
# Line 813 | Line 797 | public class ReentrantReadWriteLockTest
797       * await returns when signalled
798       */
799      public void testAwait() throws InterruptedException {
800 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
800 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
801          final Condition c = lock.writeLock().newCondition();
802 <        Thread t = new Thread(new CheckedRunnable() {
802 >        Thread t = new Thread(new CheckedRunnable() {
803              public void realRun() throws InterruptedException {
804                  lock.writeLock().lock();
805                  c.await();
# Line 890 | Line 874 | public class ReentrantReadWriteLockTest
874       * await is interruptible
875       */
876      public void testAwait_Interrupt() throws InterruptedException {
877 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
877 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
878          final Condition c = lock.writeLock().newCondition();
879 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
879 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
880              public void realRun() throws InterruptedException {
881                  lock.writeLock().lock();
882                  c.await();
# Line 910 | Line 894 | public class ReentrantReadWriteLockTest
894       * awaitNanos is interruptible
895       */
896      public void testAwaitNanos_Interrupt() throws InterruptedException {
897 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
897 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
898          final Condition c = lock.writeLock().newCondition();
899 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
899 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
900              public void realRun() throws InterruptedException {
901                  lock.writeLock().lock();
902 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
902 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
903                  lock.writeLock().unlock();
904              }});
905  
# Line 930 | Line 914 | public class ReentrantReadWriteLockTest
914       * awaitUntil is interruptible
915       */
916      public void testAwaitUntil_Interrupt() throws InterruptedException {
917 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
917 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
918          final Condition c = lock.writeLock().newCondition();
919 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
919 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
920              public void realRun() throws InterruptedException {
921                  lock.writeLock().lock();
922                  java.util.Date d = new java.util.Date();
# Line 951 | Line 935 | public class ReentrantReadWriteLockTest
935       * signalAll wakes up all threads
936       */
937      public void testSignalAll() throws InterruptedException {
938 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
939          final Condition c = lock.writeLock().newCondition();
940 <        Thread t1 = new Thread(new CheckedRunnable() {
940 >        Thread t1 = new Thread(new CheckedRunnable() {
941              public void realRun() throws InterruptedException {
942                  lock.writeLock().lock();
943                  c.await();
944                  lock.writeLock().unlock();
945              }});
946  
947 <        Thread t2 = new Thread(new CheckedRunnable() {
947 >        Thread t2 = new Thread(new CheckedRunnable() {
948              public void realRun() throws InterruptedException {
949                  lock.writeLock().lock();
950                  c.await();
# Line 1003 | Line 987 | public class ReentrantReadWriteLockTest
987       * hasQueuedThreads reports whether there are waiting threads
988       */
989      public void testhasQueuedThreads() throws InterruptedException {
990 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
990 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
991          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
992          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
993          assertFalse(lock.hasQueuedThreads());
# Line 1028 | Line 1012 | public class ReentrantReadWriteLockTest
1012       * hasQueuedThread(null) throws NPE
1013       */
1014      public void testHasQueuedThreadNPE() {
1015 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1015 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1016          try {
1017              sync.hasQueuedThread(null);
1018              shouldThrow();
# Line 1039 | Line 1023 | public class ReentrantReadWriteLockTest
1023       * hasQueuedThread reports whether a thread is queued.
1024       */
1025      public void testHasQueuedThread() throws InterruptedException {
1026 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1026 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1027          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1028          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1029          assertFalse(sync.hasQueuedThread(t1));
# Line 1070 | Line 1054 | public class ReentrantReadWriteLockTest
1054       * getQueueLength reports number of waiting threads
1055       */
1056      public void testGetQueueLength() throws InterruptedException {
1057 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1057 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1058          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1059          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1060          assertEquals(0, lock.getQueueLength());
# Line 1095 | Line 1079 | public class ReentrantReadWriteLockTest
1079       * getQueuedThreads includes waiting threads
1080       */
1081      public void testGetQueuedThreads() throws InterruptedException {
1082 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1082 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1083          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1084          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1085          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1123 | Line 1107 | public class ReentrantReadWriteLockTest
1107       * hasWaiters throws NPE if null
1108       */
1109      public void testHasWaitersNPE() {
1110 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1110 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1111          try {
1112              lock.hasWaiters(null);
1113              shouldThrow();
# Line 1134 | Line 1118 | public class ReentrantReadWriteLockTest
1118       * getWaitQueueLength throws NPE if null
1119       */
1120      public void testGetWaitQueueLengthNPE() {
1121 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1121 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1122          try {
1123              lock.getWaitQueueLength(null);
1124              shouldThrow();
# Line 1146 | Line 1130 | public class ReentrantReadWriteLockTest
1130       * getWaitingThreads throws NPE if null
1131       */
1132      public void testGetWaitingThreadsNPE() {
1133 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1133 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1134          try {
1135              lock.getWaitingThreads(null);
1136              shouldThrow();
# Line 1157 | Line 1141 | public class ReentrantReadWriteLockTest
1141       * hasWaiters throws IAE if not owned
1142       */
1143      public void testHasWaitersIAE() {
1144 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1144 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1145          final Condition c = lock.writeLock().newCondition();
1146 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1146 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1147          try {
1148              lock2.hasWaiters(c);
1149              shouldThrow();
# Line 1170 | Line 1154 | public class ReentrantReadWriteLockTest
1154       * hasWaiters throws IMSE if not locked
1155       */
1156      public void testHasWaitersIMSE() {
1157 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1157 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1158          final Condition c = lock.writeLock().newCondition();
1159          try {
1160              lock.hasWaiters(c);
# Line 1183 | Line 1167 | public class ReentrantReadWriteLockTest
1167       * getWaitQueueLength throws IAE if not owned
1168       */
1169      public void testGetWaitQueueLengthIAE() {
1170 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1170 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1171          final Condition c = lock.writeLock().newCondition();
1172 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1172 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1173          try {
1174              lock2.getWaitQueueLength(c);
1175              shouldThrow();
# Line 1196 | Line 1180 | public class ReentrantReadWriteLockTest
1180       * getWaitQueueLength throws IMSE if not locked
1181       */
1182      public void testGetWaitQueueLengthIMSE() {
1183 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1183 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1184          final Condition c = lock.writeLock().newCondition();
1185          try {
1186              lock.getWaitQueueLength(c);
# Line 1209 | Line 1193 | public class ReentrantReadWriteLockTest
1193       * getWaitingThreads throws IAE if not owned
1194       */
1195      public void testGetWaitingThreadsIAE() {
1196 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1196 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1197          final Condition c = lock.writeLock().newCondition();
1198 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1198 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1199          try {
1200              lock2.getWaitingThreads(c);
1201              shouldThrow();
# Line 1222 | Line 1206 | public class ReentrantReadWriteLockTest
1206       * getWaitingThreads throws IMSE if not locked
1207       */
1208      public void testGetWaitingThreadsIMSE() {
1209 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1209 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1210          final Condition c = lock.writeLock().newCondition();
1211          try {
1212              lock.getWaitingThreads(c);
# Line 1235 | Line 1219 | public class ReentrantReadWriteLockTest
1219       * hasWaiters returns true when a thread is waiting, else false
1220       */
1221      public void testHasWaiters() throws InterruptedException {
1222 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1222 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1223          final Condition c = lock.writeLock().newCondition();
1224 <        Thread t = new Thread(new CheckedRunnable() {
1224 >        Thread t = new Thread(new CheckedRunnable() {
1225              public void realRun() throws InterruptedException {
1226                  lock.writeLock().lock();
1227                  threadAssertFalse(lock.hasWaiters(c));
# Line 1266 | Line 1250 | public class ReentrantReadWriteLockTest
1250       * getWaitQueueLength returns number of waiting threads
1251       */
1252      public void testGetWaitQueueLength() throws InterruptedException {
1253 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1253 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1254          final Condition c = lock.writeLock().newCondition();
1255 <        Thread t = new Thread(new CheckedRunnable() {
1255 >        Thread t = new Thread(new CheckedRunnable() {
1256              public void realRun() throws InterruptedException {
1257                  lock.writeLock().lock();
1258                  threadAssertFalse(lock.hasWaiters(c));
# Line 1298 | Line 1282 | public class ReentrantReadWriteLockTest
1282       * getWaitingThreads returns only and all waiting threads
1283       */
1284      public void testGetWaitingThreads() throws InterruptedException {
1285 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1285 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1286          final Condition c = lock.writeLock().newCondition();
1287 <        Thread t1 = new Thread(new CheckedRunnable() {
1287 >        Thread t1 = new Thread(new CheckedRunnable() {
1288              public void realRun() throws InterruptedException {
1289                  lock.writeLock().lock();
1290                  threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 1308 | Line 1292 | public class ReentrantReadWriteLockTest
1292                  lock.writeLock().unlock();
1293              }});
1294  
1295 <        Thread t2 = new Thread(new CheckedRunnable() {
1295 >        Thread t2 = new Thread(new CheckedRunnable() {
1296              public void realRun() throws InterruptedException {
1297                  lock.writeLock().lock();
1298                  threadAssertFalse(lock.getWaitingThreads(c).isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines