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.48 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.59 by jsr166, Sun May 24 01:42:14 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.locks.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.*;
11 < import java.io.*;
10 >
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.HashSet;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.CyclicBarrier;
16 > import java.util.concurrent.locks.Condition;
17 > import java.util.concurrent.locks.ReentrantLock;
18 >
19 > import junit.framework.AssertionFailedError;
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ReentrantLockTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(ReentrantLockTest.class);
29      }
30  
31      /**
32 <     * A runnable calling lockInterruptibly
32 >     * A checked runnable calling lockInterruptibly
33       */
34      class InterruptibleLockRunnable extends CheckedRunnable {
35          final ReentrantLock lock;
36 <        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
36 >        InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
37          public void realRun() throws InterruptedException {
38              lock.lockInterruptibly();
39          }
40      }
41  
42      /**
43 <     * A runnable calling lockInterruptibly that expects to be
43 >     * A checked runnable calling lockInterruptibly that expects to be
44       * interrupted
45       */
46      class InterruptedLockRunnable extends CheckedInterruptedRunnable {
47          final ReentrantLock lock;
48 <        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
48 >        InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
49          public void realRun() throws InterruptedException {
50              lock.lockInterruptibly();
51          }
# Line 82 | Line 89 | public class ReentrantLockTest extends J
89              Thread.yield();
90          }
91          assertTrue(t.isAlive());
92 <        assertTrue(lock.getOwner() != t);
92 >        assertNotSame(t, lock.getOwner());
93      }
94  
95      /**
# Line 136 | Line 143 | public class ReentrantLockTest extends J
143          lock.unlock();
144      }
145  
146 <    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
146 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147  
148      /**
149       * Awaits condition using the specified AwaitMethod.
# Line 159 | Line 166 | public class ReentrantLockTest extends J
166          case awaitUntil:
167              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
168              break;
169 +        default:
170 +            throw new AssertionError();
171          }
172      }
173  
# Line 286 | Line 295 | public class ReentrantLockTest extends J
295      }
296  
297      /**
298 <     * hasQueuedThread reports whether a thread is queued.
298 >     * hasQueuedThread reports whether a thread is queued
299       */
300      public void testHasQueuedThread()      { testHasQueuedThread(false); }
301      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 347 | Line 356 | public class ReentrantLockTest extends J
356      }
357  
358      /**
359 <     * timed tryLock is interruptible.
359 >     * timed tryLock is interruptible
360       */
361      public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
362      public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
# Line 415 | Line 424 | public class ReentrantLockTest extends J
424          }
425          for (int i = SIZE; i > 0; i--) {
426              lock.unlock();
427 <            assertEquals(i-1, lock.getHoldCount());
427 >            assertEquals(i - 1, lock.getHoldCount());
428          }
429      }
430  
# Line 451 | Line 460 | public class ReentrantLockTest extends J
460              barrier.await();
461              awaitTermination(t);
462              assertFalse(lock.isLocked());
463 <        } catch (Exception e) {
455 <            threadUnexpectedException(e);
456 <        }
463 >        } catch (Exception fail) { threadUnexpectedException(fail); }
464      }
465  
466      /**
# Line 465 | Line 472 | public class ReentrantLockTest extends J
472          final PublicReentrantLock lock = new PublicReentrantLock(fair);
473          try {
474              lock.lockInterruptibly();
475 <        } catch (InterruptedException ie) {
469 <            threadUnexpectedException(ie);
470 <        }
475 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
476          assertLockedByMoi(lock);
477          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
478          waitForQueuedThread(lock, t);
# Line 528 | Line 533 | public class ReentrantLockTest extends J
533              assertTrue(nanosRemaining <= 0);
534              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
535              lock.unlock();
536 <        } catch (InterruptedException e) {
532 <            threadUnexpectedException(e);
533 <        }
536 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
537      }
538  
539      /**
# Line 548 | Line 551 | public class ReentrantLockTest extends J
551              assertFalse(c.await(timeoutMillis, MILLISECONDS));
552              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
553              lock.unlock();
554 <        } catch (InterruptedException e) {
552 <            threadUnexpectedException(e);
553 <        }
554 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
555      }
556  
557      /**
# Line 569 | Line 570 | public class ReentrantLockTest extends J
570              assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
571              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
572              lock.unlock();
573 <        } catch (InterruptedException e) {
573 <            threadUnexpectedException(e);
574 <        }
573 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
574      }
575  
576      /**
# Line 735 | Line 734 | public class ReentrantLockTest extends J
734      public void testHasWaiters(boolean fair) {
735          final PublicReentrantLock lock = new PublicReentrantLock(fair);
736          final Condition c = lock.newCondition();
737 <        final CountDownLatch locked = new CountDownLatch(1);
737 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
738          Thread t = newStartedThread(new CheckedRunnable() {
739              public void realRun() throws InterruptedException {
740                  lock.lock();
741                  assertHasNoWaiters(lock, c);
742                  assertFalse(lock.hasWaiters(c));
743 <                locked.countDown();
743 >                pleaseSignal.countDown();
744                  c.await();
745                  assertHasNoWaiters(lock, c);
746                  assertFalse(lock.hasWaiters(c));
747                  lock.unlock();
748              }});
749  
750 <        await(locked);
750 >        await(pleaseSignal);
751          lock.lock();
752          assertHasWaiters(lock, c, t);
753          assertTrue(lock.hasWaiters(c));
# Line 879 | Line 878 | public class ReentrantLockTest extends J
878      }
879  
880      /**
881 <     * awaitUninterruptibly doesn't abort on interrupt
881 >     * awaitUninterruptibly is uninterruptible
882       */
883      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
884      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
885      public void testAwaitUninterruptibly(boolean fair) {
886          final ReentrantLock lock = new ReentrantLock(fair);
887          final Condition c = lock.newCondition();
888 <        final CountDownLatch locked = new CountDownLatch(1);
889 <        Thread t = newStartedThread(new CheckedRunnable() {
888 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
889 >
890 >        Thread t1 = newStartedThread(new CheckedRunnable() {
891              public void realRun() {
892 +                // Interrupt before awaitUninterruptibly
893                  lock.lock();
894 <                locked.countDown();
894 >                pleaseInterrupt.countDown();
895 >                Thread.currentThread().interrupt();
896                  c.awaitUninterruptibly();
897                  assertTrue(Thread.interrupted());
898                  lock.unlock();
899              }});
900  
901 <        await(locked);
901 >        Thread t2 = newStartedThread(new CheckedRunnable() {
902 >            public void realRun() {
903 >                // Interrupt during awaitUninterruptibly
904 >                lock.lock();
905 >                pleaseInterrupt.countDown();
906 >                c.awaitUninterruptibly();
907 >                assertTrue(Thread.interrupted());
908 >                lock.unlock();
909 >            }});
910 >
911 >        await(pleaseInterrupt);
912          lock.lock();
913          lock.unlock();
914 <        t.interrupt();
915 <        long timeoutMillis = 10;
916 <        assertThreadStaysAlive(t, timeoutMillis);
914 >        t2.interrupt();
915 >
916 >        assertThreadStaysAlive(t1);
917 >        assertTrue(t2.isAlive());
918 >
919          lock.lock();
920 <        c.signal();
920 >        c.signalAll();
921          lock.unlock();
922 <        awaitTermination(t);
922 >
923 >        awaitTermination(t1);
924 >        awaitTermination(t2);
925      }
926  
927      /**
# Line 923 | Line 939 | public class ReentrantLockTest extends J
939          final PublicReentrantLock lock =
940              new PublicReentrantLock(fair);
941          final Condition c = lock.newCondition();
942 <        final CountDownLatch locked = new CountDownLatch(1);
942 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
943          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
944              public void realRun() throws InterruptedException {
945                  lock.lock();
946                  assertLockedByMoi(lock);
947                  assertHasNoWaiters(lock, c);
948 <                locked.countDown();
948 >                pleaseInterrupt.countDown();
949                  try {
950                      await(c, awaitMethod);
951                  } finally {
# Line 940 | Line 956 | public class ReentrantLockTest extends J
956                  }
957              }});
958  
959 <        await(locked);
959 >        await(pleaseInterrupt);
960          assertHasWaiters(lock, c, t);
961          t.interrupt();
962          awaitTermination(t);
# Line 961 | Line 977 | public class ReentrantLockTest extends J
977      public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
978          final PublicReentrantLock lock = new PublicReentrantLock(fair);
979          final Condition c = lock.newCondition();
980 <        final CountDownLatch locked = new CountDownLatch(2);
980 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
981          class Awaiter extends CheckedRunnable {
982              public void realRun() throws InterruptedException {
983                  lock.lock();
984 <                locked.countDown();
984 >                pleaseSignal.countDown();
985                  await(c, awaitMethod);
986                  lock.unlock();
987              }
# Line 974 | Line 990 | public class ReentrantLockTest extends J
990          Thread t1 = newStartedThread(new Awaiter());
991          Thread t2 = newStartedThread(new Awaiter());
992  
993 <        await(locked);
993 >        await(pleaseSignal);
994          lock.lock();
995          assertHasWaiters(lock, c, t1, t2);
996          c.signalAll();
# Line 985 | Line 1001 | public class ReentrantLockTest extends J
1001      }
1002  
1003      /**
1004 <     * signal wakes up waiting threads in FIFO order.
1004 >     * signal wakes up waiting threads in FIFO order
1005       */
1006      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1007      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1039 | Line 1055 | public class ReentrantLockTest extends J
1055      public void testAwaitLockCount(boolean fair) {
1056          final PublicReentrantLock lock = new PublicReentrantLock(fair);
1057          final Condition c = lock.newCondition();
1058 <        final CountDownLatch locked = new CountDownLatch(2);
1058 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1059          Thread t1 = newStartedThread(new CheckedRunnable() {
1060              public void realRun() throws InterruptedException {
1061                  lock.lock();
1062                  assertLockedByMoi(lock);
1063                  assertEquals(1, lock.getHoldCount());
1064 <                locked.countDown();
1064 >                pleaseSignal.countDown();
1065                  c.await();
1066                  assertLockedByMoi(lock);
1067                  assertEquals(1, lock.getHoldCount());
# Line 1058 | Line 1074 | public class ReentrantLockTest extends J
1074                  lock.lock();
1075                  assertLockedByMoi(lock);
1076                  assertEquals(2, lock.getHoldCount());
1077 <                locked.countDown();
1077 >                pleaseSignal.countDown();
1078                  c.await();
1079                  assertLockedByMoi(lock);
1080                  assertEquals(2, lock.getHoldCount());
# Line 1066 | Line 1082 | public class ReentrantLockTest extends J
1082                  lock.unlock();
1083              }});
1084  
1085 <        await(locked);
1085 >        await(pleaseSignal);
1086          lock.lock();
1087          assertHasWaiters(lock, c, t1, t2);
1088          assertEquals(1, lock.getHoldCount());
# Line 1110 | Line 1126 | public class ReentrantLockTest extends J
1126      public void testToString_fair() { testToString(true); }
1127      public void testToString(boolean fair) {
1128          ReentrantLock lock = new ReentrantLock(fair);
1129 <        String us = lock.toString();
1114 <        assertTrue(us.indexOf("Unlocked") >= 0);
1129 >        assertTrue(lock.toString().contains("Unlocked"));
1130          lock.lock();
1131 <        String ls = lock.toString();
1132 <        assertTrue(ls.indexOf("Locked") >= 0);
1131 >        assertTrue(lock.toString().contains("Locked"));
1132 >        lock.unlock();
1133 >        assertTrue(lock.toString().contains("Unlocked"));
1134      }
1135   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines