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.12 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.13 by dl, Sun Dec 28 21:56:18 2003 UTC

# Line 57 | Line 57 | public class ReentrantLockTest extends J
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 +        public Collection<Thread> getWaitingThreads(Condition c) {
61 +            return super.getWaitingThreads(c);
62 +        }
63 +
64  
65      }
66  
# Line 114 | Line 118 | public class ReentrantLockTest extends J
118  
119  
120      /**
121 +     * hasQueuedThreads reports whether there are waiting threads
122 +     */
123 +    public void testhasQueuedThreads() {
124 +        final ReentrantLock lock = new ReentrantLock();
125 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
126 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
127 +        try {
128 +            assertFalse(lock.hasQueuedThreads());
129 +            lock.lock();
130 +            t1.start();
131 +            Thread.sleep(SHORT_DELAY_MS);
132 +            assertTrue(lock.hasQueuedThreads());
133 +            t2.start();
134 +            Thread.sleep(SHORT_DELAY_MS);
135 +            assertTrue(lock.hasQueuedThreads());
136 +            t1.interrupt();
137 +            Thread.sleep(SHORT_DELAY_MS);
138 +            assertTrue(lock.hasQueuedThreads());
139 +            lock.unlock();
140 +            Thread.sleep(SHORT_DELAY_MS);
141 +            assertFalse(lock.hasQueuedThreads());
142 +            t1.join();
143 +            t2.join();
144 +        } catch(Exception e){
145 +            unexpectedException();
146 +        }
147 +    }
148 +
149 +    /**
150       * getQueueLength reports number of waiting threads
151       */
152      public void testGetQueueLength() {
# Line 419 | Line 452 | public class ReentrantLockTest extends J
452       */
453      public void testAwait() {
454          final ReentrantLock lock = new ReentrantLock();
455 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
455 >        final Condition c = lock.newCondition();
456          Thread t = new Thread(new Runnable() {
457                  public void run() {
458                      try {
# Line 448 | Line 481 | public class ReentrantLockTest extends J
481      }
482  
483      /**
484 +     * hasWaiters throws IAE if not owned
485 +     */
486 +    public void testHasWaitersIAE() {
487 +        final ReentrantLock lock = new ReentrantLock();
488 +        final Condition c = (lock.newCondition());
489 +        final ReentrantLock lock2 = new ReentrantLock();
490 +        try {
491 +            lock2.hasWaiters(c);
492 +            shouldThrow();
493 +        } catch (IllegalArgumentException success) {
494 +        } catch (Exception ex) {
495 +            unexpectedException();
496 +        }
497 +    }
498 +
499 +    /**
500 +     * hasWaiters throws IMSE if not locked
501 +     */
502 +    public void testHasWaitersIMSE() {
503 +        final ReentrantLock lock = new ReentrantLock();
504 +        final Condition c = (lock.newCondition());
505 +        try {
506 +            lock.hasWaiters(c);
507 +            shouldThrow();
508 +        } catch (IllegalMonitorStateException success) {
509 +        } catch (Exception ex) {
510 +            unexpectedException();
511 +        }
512 +    }
513 +
514 +
515 +    /**
516 +     * getWaitQueueLength throws IAE if not owned
517 +     */
518 +    public void testGetWaitQueueLengthIAE() {
519 +        final ReentrantLock lock = new ReentrantLock();
520 +        final Condition c = (lock.newCondition());
521 +        final ReentrantLock lock2 = new ReentrantLock();
522 +        try {
523 +            lock2.getWaitQueueLength(c);
524 +            shouldThrow();
525 +        } catch (IllegalArgumentException success) {
526 +        } catch (Exception ex) {
527 +            unexpectedException();
528 +        }
529 +    }
530 +
531 +    /**
532 +     * getWaitQueueLength throws IMSE if not locked
533 +     */
534 +    public void testGetWaitQueueLengthIMSE() {
535 +        final ReentrantLock lock = new ReentrantLock();
536 +        final Condition c = (lock.newCondition());
537 +        try {
538 +            lock.getWaitQueueLength(c);
539 +            shouldThrow();
540 +        } catch (IllegalMonitorStateException success) {
541 +        } catch (Exception ex) {
542 +            unexpectedException();
543 +        }
544 +    }
545 +
546 +
547 +    /**
548 +     * getWaitingThreads throws IAE if not owned
549 +     */
550 +    public void testGetWaitingThreadsIAE() {
551 +        final PublicReentrantLock lock = new PublicReentrantLock();    
552 +        final Condition c = (lock.newCondition());
553 +        final PublicReentrantLock lock2 = new PublicReentrantLock();    
554 +        try {
555 +            lock2.getWaitingThreads(c);
556 +            shouldThrow();
557 +        } catch (IllegalArgumentException success) {
558 +        } catch (Exception ex) {
559 +            unexpectedException();
560 +        }
561 +    }
562 +
563 +    /**
564 +     * getWaitingThreads throws IMSE if not locked
565 +     */
566 +    public void testGetWaitingThreadsIMSE() {
567 +        final PublicReentrantLock lock = new PublicReentrantLock();    
568 +        final Condition c = (lock.newCondition());
569 +        try {
570 +            lock.getWaitingThreads(c);
571 +            shouldThrow();
572 +        } catch (IllegalMonitorStateException success) {
573 +        } catch (Exception ex) {
574 +            unexpectedException();
575 +        }
576 +    }
577 +
578 +
579 +
580 +    /**
581       * hasWaiters returns true when a thread is waiting, else false
582       */
583      public void testHasWaiters() {
584          final ReentrantLock lock = new ReentrantLock();
585 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
585 >        final Condition c = lock.newCondition();
586          Thread t = new Thread(new Runnable() {
587                  public void run() {
588                      try {
589                          lock.lock();
590 <                        threadAssertFalse(c.hasWaiters());
591 <                        threadAssertEquals(0, c.getWaitQueueLength());
590 >                        threadAssertFalse(lock.hasWaiters(c));
591 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
592                          c.await();
593                          lock.unlock();
594                      }
# Line 472 | Line 602 | public class ReentrantLockTest extends J
602              t.start();
603              Thread.sleep(SHORT_DELAY_MS);
604              lock.lock();
605 <            assertTrue(c.hasWaiters());
606 <            assertEquals(1, c.getWaitQueueLength());
605 >            assertTrue(lock.hasWaiters(c));
606 >            assertEquals(1, lock.getWaitQueueLength(c));
607              c.signal();
608              lock.unlock();
609              Thread.sleep(SHORT_DELAY_MS);
610              lock.lock();
611 <            assertFalse(c.hasWaiters());
612 <            assertEquals(0, c.getWaitQueueLength());
611 >            assertFalse(lock.hasWaiters(c));
612 >            assertEquals(0, lock.getWaitQueueLength(c));
613              lock.unlock();
614              t.join(SHORT_DELAY_MS);
615              assertFalse(t.isAlive());
# Line 494 | Line 624 | public class ReentrantLockTest extends J
624       */
625      public void testGetWaitQueueLength() {
626          final ReentrantLock lock = new ReentrantLock();
627 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
627 >        final Condition c = lock.newCondition();
628 >        Thread t1 = new Thread(new Runnable() {
629 >                public void run() {
630 >                    try {
631 >                        lock.lock();
632 >                        threadAssertFalse(lock.hasWaiters(c));
633 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
634 >                        c.await();
635 >                        lock.unlock();
636 >                    }
637 >                    catch(InterruptedException e) {
638 >                        threadUnexpectedException();
639 >                    }
640 >                }
641 >            });
642 >
643 >        Thread t2 = new Thread(new Runnable() {
644 >                public void run() {
645 >                    try {
646 >                        lock.lock();
647 >                        threadAssertTrue(lock.hasWaiters(c));
648 >                        threadAssertEquals(1, lock.getWaitQueueLength(c));
649 >                        c.await();
650 >                        lock.unlock();
651 >                    }
652 >                    catch(InterruptedException e) {
653 >                        threadUnexpectedException();
654 >                    }
655 >                }
656 >            });
657 >
658 >        try {
659 >            t1.start();
660 >            Thread.sleep(SHORT_DELAY_MS);
661 >            t2.start();
662 >            Thread.sleep(SHORT_DELAY_MS);
663 >            lock.lock();
664 >            assertTrue(lock.hasWaiters(c));
665 >            assertEquals(2, lock.getWaitQueueLength(c));
666 >            c.signalAll();
667 >            lock.unlock();
668 >            Thread.sleep(SHORT_DELAY_MS);
669 >            lock.lock();
670 >            assertFalse(lock.hasWaiters(c));
671 >            assertEquals(0, lock.getWaitQueueLength(c));
672 >            lock.unlock();
673 >            t1.join(SHORT_DELAY_MS);
674 >            t2.join(SHORT_DELAY_MS);
675 >            assertFalse(t1.isAlive());
676 >            assertFalse(t2.isAlive());
677 >        }
678 >        catch (Exception ex) {
679 >            unexpectedException();
680 >        }
681 >    }
682 >
683 >    /**
684 >     * getWaitingThreads returns only and all waiting threads
685 >     */
686 >    public void testGetWaitingThreads() {
687 >        final PublicReentrantLock lock = new PublicReentrantLock();    
688 >        final Condition c = lock.newCondition();
689          Thread t1 = new Thread(new Runnable() {
690                  public void run() {
691                      try {
692                          lock.lock();
693 <                        threadAssertFalse(c.hasWaiters());
503 <                        threadAssertEquals(0, c.getWaitQueueLength());
693 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
694                          c.await();
695                          lock.unlock();
696                      }
# Line 514 | Line 704 | public class ReentrantLockTest extends J
704                  public void run() {
705                      try {
706                          lock.lock();
707 <                        threadAssertTrue(c.hasWaiters());
518 <                        threadAssertEquals(1, c.getWaitQueueLength());
707 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
708                          c.await();
709                          lock.unlock();
710                      }
# Line 526 | Line 715 | public class ReentrantLockTest extends J
715              });
716  
717          try {
718 +            lock.lock();
719 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
720 +            lock.unlock();
721              t1.start();
722              Thread.sleep(SHORT_DELAY_MS);
723              t2.start();
724              Thread.sleep(SHORT_DELAY_MS);
725              lock.lock();
726 <            assertTrue(c.hasWaiters());
727 <            assertEquals(2, c.getWaitQueueLength());
726 >            assertTrue(lock.hasWaiters(c));
727 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
728 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
729              c.signalAll();
730              lock.unlock();
731              Thread.sleep(SHORT_DELAY_MS);
732              lock.lock();
733 <            assertFalse(c.hasWaiters());
734 <            assertEquals(0, c.getWaitQueueLength());
733 >            assertFalse(lock.hasWaiters(c));
734 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
735              lock.unlock();
736              t1.join(SHORT_DELAY_MS);
737              t2.join(SHORT_DELAY_MS);
# Line 550 | Line 743 | public class ReentrantLockTest extends J
743          }
744      }
745  
746 +
747 +
748      /**
749       * awaitUninterruptibly doesn't abort on interrupt
750       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines