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

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.29 by jsr166, Tue Nov 17 13:31:53 2009 UTC vs.
Revision 1.30 by jsr166, Sat Nov 21 02:07:27 2009 UTC

# Line 14 | Line 14 | import java.io.*;
14  
15   public class ReentrantLockTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ReentrantLockTest.class);
20 >        return new TestSuite(ReentrantLockTest.class);
21      }
22  
23      /**
# Line 72 | Line 72 | public class ReentrantLockTest extends J
72       * locking an unlocked lock succeeds
73       */
74      public void testLock() {
75 <        ReentrantLock rl = new ReentrantLock();
75 >        ReentrantLock rl = new ReentrantLock();
76          rl.lock();
77          assertTrue(rl.isLocked());
78          rl.unlock();
# Line 83 | Line 83 | public class ReentrantLockTest extends J
83       * locking an unlocked fair lock succeeds
84       */
85      public void testFairLock() {
86 <        ReentrantLock rl = new ReentrantLock(true);
86 >        ReentrantLock rl = new ReentrantLock(true);
87          rl.lock();
88          assertTrue(rl.isLocked());
89          rl.unlock();
# Line 93 | Line 93 | public class ReentrantLockTest extends J
93       * Unlocking an unlocked lock throws IllegalMonitorStateException
94       */
95      public void testUnlock_IllegalMonitorStateException() {
96 <        ReentrantLock rl = new ReentrantLock();
97 <        try {
98 <            rl.unlock();
99 <            shouldThrow();
100 <        } catch (IllegalMonitorStateException success) {}
96 >        ReentrantLock rl = new ReentrantLock();
97 >        try {
98 >            rl.unlock();
99 >            shouldThrow();
100 >        } catch (IllegalMonitorStateException success) {}
101      }
102  
103      /**
104       * tryLock on an unlocked lock succeeds
105       */
106      public void testTryLock() {
107 <        ReentrantLock rl = new ReentrantLock();
107 >        ReentrantLock rl = new ReentrantLock();
108          assertTrue(rl.tryLock());
109          assertTrue(rl.isLocked());
110          rl.unlock();
# Line 115 | Line 115 | public class ReentrantLockTest extends J
115       * hasQueuedThreads reports whether there are waiting threads
116       */
117      public void testhasQueuedThreads() throws InterruptedException {
118 <        final ReentrantLock lock = new ReentrantLock();
118 >        final ReentrantLock lock = new ReentrantLock();
119          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
120          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
121          assertFalse(lock.hasQueuedThreads());
# Line 140 | Line 140 | public class ReentrantLockTest extends J
140       * getQueueLength reports number of waiting threads
141       */
142      public void testGetQueueLength() throws InterruptedException {
143 <        final ReentrantLock lock = new ReentrantLock();
143 >        final ReentrantLock lock = new ReentrantLock();
144          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
145          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
146          assertEquals(0, lock.getQueueLength());
# Line 165 | Line 165 | public class ReentrantLockTest extends J
165       * getQueueLength reports number of waiting threads
166       */
167      public void testGetQueueLength_fair() throws InterruptedException {
168 <        final ReentrantLock lock = new ReentrantLock(true);
168 >        final ReentrantLock lock = new ReentrantLock(true);
169          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
170          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
171          assertEquals(0, lock.getQueueLength());
# Line 190 | Line 190 | public class ReentrantLockTest extends J
190       * hasQueuedThread(null) throws NPE
191       */
192      public void testHasQueuedThreadNPE() {
193 <        final ReentrantLock sync = new ReentrantLock();
193 >        final ReentrantLock sync = new ReentrantLock();
194          try {
195              sync.hasQueuedThread(null);
196              shouldThrow();
# Line 201 | Line 201 | public class ReentrantLockTest extends J
201       * hasQueuedThread reports whether a thread is queued.
202       */
203      public void testHasQueuedThread() throws InterruptedException {
204 <        final ReentrantLock sync = new ReentrantLock();
204 >        final ReentrantLock sync = new ReentrantLock();
205          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
206          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
207          assertFalse(sync.hasQueuedThread(t1));
# Line 232 | Line 232 | public class ReentrantLockTest extends J
232       * getQueuedThreads includes waiting threads
233       */
234      public void testGetQueuedThreads() throws InterruptedException {
235 <        final PublicReentrantLock lock = new PublicReentrantLock();
235 >        final PublicReentrantLock lock = new PublicReentrantLock();
236          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
237          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
238          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 261 | Line 261 | public class ReentrantLockTest extends J
261       * timed tryLock is interruptible.
262       */
263      public void testInterruptedException2() throws InterruptedException {
264 <        final ReentrantLock lock = new ReentrantLock();
265 <        lock.lock();
266 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
264 >        final ReentrantLock lock = new ReentrantLock();
265 >        lock.lock();
266 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
267              public void realRun() throws InterruptedException {
268                  lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
269              }});
# Line 278 | Line 278 | public class ReentrantLockTest extends J
278       * TryLock on a locked lock fails
279       */
280      public void testTryLockWhenLocked() throws InterruptedException {
281 <        final ReentrantLock lock = new ReentrantLock();
282 <        lock.lock();
283 <        Thread t = new Thread(new CheckedRunnable() {
281 >        final ReentrantLock lock = new ReentrantLock();
282 >        lock.lock();
283 >        Thread t = new Thread(new CheckedRunnable() {
284              public void realRun() {
285                  threadAssertFalse(lock.tryLock());
286              }});
# Line 294 | Line 294 | public class ReentrantLockTest extends J
294       * Timed tryLock on a locked lock times out
295       */
296      public void testTryLock_Timeout() throws InterruptedException {
297 <        final ReentrantLock lock = new ReentrantLock();
298 <        lock.lock();
299 <        Thread t = new Thread(new CheckedRunnable() {
297 >        final ReentrantLock lock = new ReentrantLock();
298 >        lock.lock();
299 >        Thread t = new Thread(new CheckedRunnable() {
300              public void realRun() throws InterruptedException {
301                  threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
302              }});
# Line 310 | Line 310 | public class ReentrantLockTest extends J
310       * getHoldCount returns number of recursive holds
311       */
312      public void testGetHoldCount() {
313 <        ReentrantLock lock = new ReentrantLock();
314 <        for (int i = 1; i <= SIZE; i++) {
315 <            lock.lock();
316 <            assertEquals(i, lock.getHoldCount());
317 <        }
318 <        for (int i = SIZE; i > 0; i--) {
319 <            lock.unlock();
320 <            assertEquals(i-1, lock.getHoldCount());
321 <        }
313 >        ReentrantLock lock = new ReentrantLock();
314 >        for (int i = 1; i <= SIZE; i++) {
315 >            lock.lock();
316 >            assertEquals(i, lock.getHoldCount());
317 >        }
318 >        for (int i = SIZE; i > 0; i--) {
319 >            lock.unlock();
320 >            assertEquals(i-1, lock.getHoldCount());
321 >        }
322      }
323  
324  
# Line 326 | Line 326 | public class ReentrantLockTest extends J
326       * isLocked is true when locked and false when not
327       */
328      public void testIsLocked() throws InterruptedException {
329 <        final ReentrantLock lock = new ReentrantLock();
330 <        lock.lock();
331 <        assertTrue(lock.isLocked());
332 <        lock.unlock();
333 <        assertFalse(lock.isLocked());
334 <        Thread t = new Thread(new CheckedRunnable() {
329 >        final ReentrantLock lock = new ReentrantLock();
330 >        lock.lock();
331 >        assertTrue(lock.isLocked());
332 >        lock.unlock();
333 >        assertFalse(lock.isLocked());
334 >        Thread t = new Thread(new CheckedRunnable() {
335              public void realRun() throws InterruptedException {
336                  lock.lock();
337                  Thread.sleep(SMALL_DELAY_MS);
# Line 350 | Line 350 | public class ReentrantLockTest extends J
350       * lockInterruptibly is interruptible.
351       */
352      public void testLockInterruptibly1() throws InterruptedException {
353 <        final ReentrantLock lock = new ReentrantLock();
354 <        lock.lock();
355 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
353 >        final ReentrantLock lock = new ReentrantLock();
354 >        lock.lock();
355 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
356          t.start();
357          Thread.sleep(SHORT_DELAY_MS);
358          t.interrupt();
# Line 365 | Line 365 | public class ReentrantLockTest extends J
365       * lockInterruptibly succeeds when unlocked, else is interruptible
366       */
367      public void testLockInterruptibly2() throws InterruptedException {
368 <        final ReentrantLock lock = new ReentrantLock();
368 >        final ReentrantLock lock = new ReentrantLock();
369          lock.lockInterruptibly();
370 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
370 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
371          t.start();
372          t.interrupt();
373          assertTrue(lock.isLocked());
# Line 379 | Line 379 | public class ReentrantLockTest extends J
379       * Calling await without holding lock throws IllegalMonitorStateException
380       */
381      public void testAwait_IllegalMonitor() throws InterruptedException {
382 <        final ReentrantLock lock = new ReentrantLock();
382 >        final ReentrantLock lock = new ReentrantLock();
383          final Condition c = lock.newCondition();
384          try {
385              c.await();
# Line 391 | Line 391 | public class ReentrantLockTest extends J
391       * Calling signal without holding lock throws IllegalMonitorStateException
392       */
393      public void testSignal_IllegalMonitor() {
394 <        final ReentrantLock lock = new ReentrantLock();
394 >        final ReentrantLock lock = new ReentrantLock();
395          final Condition c = lock.newCondition();
396          try {
397              c.signal();
# Line 403 | Line 403 | public class ReentrantLockTest extends J
403       * awaitNanos without a signal times out
404       */
405      public void testAwaitNanos_Timeout() throws InterruptedException {
406 <        final ReentrantLock lock = new ReentrantLock();
406 >        final ReentrantLock lock = new ReentrantLock();
407          final Condition c = lock.newCondition();
408          lock.lock();
409          long t = c.awaitNanos(100);
# Line 415 | Line 415 | public class ReentrantLockTest extends J
415       *  timed await without a signal times out
416       */
417      public void testAwait_Timeout() throws InterruptedException {
418 <        final ReentrantLock lock = new ReentrantLock();
418 >        final ReentrantLock lock = new ReentrantLock();
419          final Condition c = lock.newCondition();
420          lock.lock();
421          assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 426 | Line 426 | public class ReentrantLockTest extends J
426       * awaitUntil without a signal times out
427       */
428      public void testAwaitUntil_Timeout() throws InterruptedException {
429 <        final ReentrantLock lock = new ReentrantLock();
429 >        final ReentrantLock lock = new ReentrantLock();
430          final Condition c = lock.newCondition();
431          lock.lock();
432          java.util.Date d = new java.util.Date();
# Line 438 | Line 438 | public class ReentrantLockTest extends J
438       * await returns when signalled
439       */
440      public void testAwait() throws InterruptedException {
441 <        final ReentrantLock lock = new ReentrantLock();
441 >        final ReentrantLock lock = new ReentrantLock();
442          final Condition c = lock.newCondition();
443 <        Thread t = new Thread(new CheckedRunnable() {
443 >        Thread t = new Thread(new CheckedRunnable() {
444              public void realRun() throws InterruptedException {
445                  lock.lock();
446                  c.await();
# Line 460 | Line 460 | public class ReentrantLockTest extends J
460       * hasWaiters throws NPE if null
461       */
462      public void testHasWaitersNPE() {
463 <        final ReentrantLock lock = new ReentrantLock();
463 >        final ReentrantLock lock = new ReentrantLock();
464          try {
465              lock.hasWaiters(null);
466              shouldThrow();
# Line 471 | Line 471 | public class ReentrantLockTest extends J
471       * getWaitQueueLength throws NPE if null
472       */
473      public void testGetWaitQueueLengthNPE() {
474 <        final ReentrantLock lock = new ReentrantLock();
474 >        final ReentrantLock lock = new ReentrantLock();
475          try {
476              lock.getWaitQueueLength(null);
477              shouldThrow();
# Line 483 | Line 483 | public class ReentrantLockTest extends J
483       * getWaitingThreads throws NPE if null
484       */
485      public void testGetWaitingThreadsNPE() {
486 <        final PublicReentrantLock lock = new PublicReentrantLock();
486 >        final PublicReentrantLock lock = new PublicReentrantLock();
487          try {
488              lock.getWaitingThreads(null);
489              shouldThrow();
# Line 495 | Line 495 | public class ReentrantLockTest extends J
495       * hasWaiters throws IAE if not owned
496       */
497      public void testHasWaitersIAE() {
498 <        final ReentrantLock lock = new ReentrantLock();
498 >        final ReentrantLock lock = new ReentrantLock();
499          final Condition c = lock.newCondition();
500 <        final ReentrantLock lock2 = new ReentrantLock();
500 >        final ReentrantLock lock2 = new ReentrantLock();
501          try {
502              lock2.hasWaiters(c);
503              shouldThrow();
# Line 508 | Line 508 | public class ReentrantLockTest extends J
508       * hasWaiters throws IMSE if not locked
509       */
510      public void testHasWaitersIMSE() {
511 <        final ReentrantLock lock = new ReentrantLock();
511 >        final ReentrantLock lock = new ReentrantLock();
512          final Condition c = lock.newCondition();
513          try {
514              lock.hasWaiters(c);
# Line 521 | Line 521 | public class ReentrantLockTest extends J
521       * getWaitQueueLength throws IAE if not owned
522       */
523      public void testGetWaitQueueLengthIAE() {
524 <        final ReentrantLock lock = new ReentrantLock();
524 >        final ReentrantLock lock = new ReentrantLock();
525          final Condition c = lock.newCondition();
526 <        final ReentrantLock lock2 = new ReentrantLock();
526 >        final ReentrantLock lock2 = new ReentrantLock();
527          try {
528              lock2.getWaitQueueLength(c);
529              shouldThrow();
# Line 534 | Line 534 | public class ReentrantLockTest extends J
534       * getWaitQueueLength throws IMSE if not locked
535       */
536      public void testGetWaitQueueLengthIMSE() {
537 <        final ReentrantLock lock = new ReentrantLock();
537 >        final ReentrantLock lock = new ReentrantLock();
538          final Condition c = lock.newCondition();
539          try {
540              lock.getWaitQueueLength(c);
# Line 547 | Line 547 | public class ReentrantLockTest extends J
547       * getWaitingThreads throws IAE if not owned
548       */
549      public void testGetWaitingThreadsIAE() {
550 <        final PublicReentrantLock lock = new PublicReentrantLock();
550 >        final PublicReentrantLock lock = new PublicReentrantLock();
551          final Condition c = lock.newCondition();
552 <        final PublicReentrantLock lock2 = new PublicReentrantLock();
552 >        final PublicReentrantLock lock2 = new PublicReentrantLock();
553          try {
554              lock2.getWaitingThreads(c);
555              shouldThrow();
# Line 560 | Line 560 | public class ReentrantLockTest extends J
560       * getWaitingThreads throws IMSE if not locked
561       */
562      public void testGetWaitingThreadsIMSE() {
563 <        final PublicReentrantLock lock = new PublicReentrantLock();
563 >        final PublicReentrantLock lock = new PublicReentrantLock();
564          final Condition c = lock.newCondition();
565          try {
566              lock.getWaitingThreads(c);
# Line 573 | Line 573 | public class ReentrantLockTest extends J
573       * hasWaiters returns true when a thread is waiting, else false
574       */
575      public void testHasWaiters() throws InterruptedException {
576 <        final ReentrantLock lock = new ReentrantLock();
576 >        final ReentrantLock lock = new ReentrantLock();
577          final Condition c = lock.newCondition();
578 <        Thread t = new Thread(new CheckedRunnable() {
578 >        Thread t = new Thread(new CheckedRunnable() {
579              public void realRun() throws InterruptedException {
580                  lock.lock();
581                  threadAssertFalse(lock.hasWaiters(c));
# Line 604 | Line 604 | public class ReentrantLockTest extends J
604       * getWaitQueueLength returns number of waiting threads
605       */
606      public void testGetWaitQueueLength() throws InterruptedException {
607 <        final ReentrantLock lock = new ReentrantLock();
607 >        final ReentrantLock lock = new ReentrantLock();
608          final Condition c = lock.newCondition();
609 <        Thread t1 = new Thread(new CheckedRunnable() {
609 >        Thread t1 = new Thread(new CheckedRunnable() {
610              public void realRun() throws InterruptedException {
611                  lock.lock();
612                  threadAssertFalse(lock.hasWaiters(c));
# Line 615 | Line 615 | public class ReentrantLockTest extends J
615                  lock.unlock();
616              }});
617  
618 <        Thread t2 = new Thread(new CheckedRunnable() {
618 >        Thread t2 = new Thread(new CheckedRunnable() {
619              public void realRun() throws InterruptedException {
620                  lock.lock();
621                  threadAssertTrue(lock.hasWaiters(c));
# Line 648 | Line 648 | public class ReentrantLockTest extends J
648       * getWaitingThreads returns only and all waiting threads
649       */
650      public void testGetWaitingThreads() throws InterruptedException {
651 <        final PublicReentrantLock lock = new PublicReentrantLock();
651 >        final PublicReentrantLock lock = new PublicReentrantLock();
652          final Condition c = lock.newCondition();
653 <        Thread t1 = new Thread(new CheckedRunnable() {
653 >        Thread t1 = new Thread(new CheckedRunnable() {
654              public void realRun() throws InterruptedException {
655                  lock.lock();
656                  threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 658 | Line 658 | public class ReentrantLockTest extends J
658                  lock.unlock();
659              }});
660  
661 <        Thread t2 = new Thread(new CheckedRunnable() {
661 >        Thread t2 = new Thread(new CheckedRunnable() {
662              public void realRun() throws InterruptedException {
663                  lock.lock();
664                  threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
# Line 749 | Line 749 | public class ReentrantLockTest extends J
749       * await is interruptible
750       */
751      public void testAwait_Interrupt() throws InterruptedException {
752 <        final ReentrantLock lock = new ReentrantLock();
752 >        final ReentrantLock lock = new ReentrantLock();
753          final Condition c = lock.newCondition();
754 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
755 <            public void realRun() throws InterruptedException {
754 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
755 >            public void realRun() throws InterruptedException {
756                  lock.lock();
757                  c.await();
758              }});
# Line 768 | Line 768 | public class ReentrantLockTest extends J
768       * awaitNanos is interruptible
769       */
770      public void testAwaitNanos_Interrupt() throws InterruptedException {
771 <        final ReentrantLock lock = new ReentrantLock();
771 >        final ReentrantLock lock = new ReentrantLock();
772          final Condition c = lock.newCondition();
773 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
774 <            public void realRun() throws InterruptedException {
773 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
774 >            public void realRun() throws InterruptedException {
775                  lock.lock();
776                  c.awaitNanos(1000 * 1000 * 1000); // 1 sec
777              }});
# Line 787 | Line 787 | public class ReentrantLockTest extends J
787       * awaitUntil is interruptible
788       */
789      public void testAwaitUntil_Interrupt() throws InterruptedException {
790 <        final ReentrantLock lock = new ReentrantLock();
790 >        final ReentrantLock lock = new ReentrantLock();
791          final Condition c = lock.newCondition();
792 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
793 <            public void realRun() throws InterruptedException {
792 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
793 >            public void realRun() throws InterruptedException {
794                  lock.lock();
795                  java.util.Date d = new java.util.Date();
796                  c.awaitUntil(new java.util.Date(d.getTime() + 10000));
# Line 807 | Line 807 | public class ReentrantLockTest extends J
807       * signalAll wakes up all threads
808       */
809      public void testSignalAll() throws InterruptedException {
810 <        final ReentrantLock lock = new ReentrantLock();
810 >        final ReentrantLock lock = new ReentrantLock();
811          final Condition c = lock.newCondition();
812 <        Thread t1 = new Thread(new CheckedRunnable() {
812 >        Thread t1 = new Thread(new CheckedRunnable() {
813              public void realRun() throws InterruptedException {
814                  lock.lock();
815                  c.await();
816                  lock.unlock();
817              }});
818  
819 <        Thread t2 = new Thread(new CheckedRunnable() {
819 >        Thread t2 = new Thread(new CheckedRunnable() {
820              public void realRun() throws InterruptedException {
821                  lock.lock();
822                  c.await();
# Line 839 | Line 839 | public class ReentrantLockTest extends J
839       * await after multiple reentrant locking preserves lock count
840       */
841      public void testAwaitLockCount() throws InterruptedException {
842 <        final ReentrantLock lock = new ReentrantLock();
842 >        final ReentrantLock lock = new ReentrantLock();
843          final Condition c = lock.newCondition();
844 <        Thread t1 = new Thread(new CheckedRunnable() {
844 >        Thread t1 = new Thread(new CheckedRunnable() {
845              public void realRun() throws InterruptedException {
846                  lock.lock();
847                  threadAssertEquals(1, lock.getHoldCount());
# Line 850 | Line 850 | public class ReentrantLockTest extends J
850                  lock.unlock();
851              }});
852  
853 <        Thread t2 = new Thread(new CheckedRunnable() {
853 >        Thread t2 = new Thread(new CheckedRunnable() {
854              public void realRun() throws InterruptedException {
855                  lock.lock();
856                  lock.lock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines