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.58 by jsr166, Sat Apr 25 04:55:31 2015 UTC vs.
Revision 1.66 by jsr166, Mon Jul 17 21:01:30 2017 UTC

# Line 20 | Line 20 | import junit.framework.AssertionFailedEr
20   import junit.framework.Test;
21   import junit.framework.TestSuite;
22  
23 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
24   public class ReentrantLockTest extends JSR166TestCase {
25      public static void main(String[] args) {
26          main(suite(), args);
# Line 146 | Line 147 | public class ReentrantLockTest extends J
147      enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
148  
149      /**
150 <     * Awaits condition using the specified AwaitMethod.
150 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
151       */
152      void await(Condition c, AwaitMethod awaitMethod)
153              throws InterruptedException {
# Line 159 | Line 160 | public class ReentrantLockTest extends J
160              assertTrue(c.await(timeoutMillis, MILLISECONDS));
161              break;
162          case awaitNanos:
163 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
164 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
165 <            assertTrue(nanosRemaining > 0);
163 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
164 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
165 >            assertTrue(nanosRemaining > timeoutNanos / 2);
166 >            assertTrue(nanosRemaining <= timeoutNanos);
167              break;
168          case awaitUntil:
169              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
# Line 208 | Line 210 | public class ReentrantLockTest extends J
210      public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
211      public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
212      public void testUnlock_IMSE(boolean fair) {
213 <        ReentrantLock lock = new ReentrantLock(fair);
213 >        final ReentrantLock lock = new ReentrantLock(fair);
214          try {
215              lock.unlock();
216              shouldThrow();
# Line 398 | Line 400 | public class ReentrantLockTest extends J
400      public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
401      public void testTryLock_Timeout(boolean fair) {
402          final PublicReentrantLock lock = new PublicReentrantLock(fair);
403 +        final long timeoutMillis = timeoutMillis();
404          lock.lock();
405          Thread t = newStartedThread(new CheckedRunnable() {
406              public void realRun() throws InterruptedException {
407                  long startTime = System.nanoTime();
405                long timeoutMillis = 10;
408                  assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
409                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
410              }});
# Line 417 | Line 419 | public class ReentrantLockTest extends J
419      public void testGetHoldCount()      { testGetHoldCount(false); }
420      public void testGetHoldCount_fair() { testGetHoldCount(true); }
421      public void testGetHoldCount(boolean fair) {
422 <        ReentrantLock lock = new ReentrantLock(fair);
422 >        final ReentrantLock lock = new ReentrantLock(fair);
423          for (int i = 1; i <= SIZE; i++) {
424              lock.lock();
425              assertEquals(i, lock.getHoldCount());
426          }
427          for (int i = SIZE; i > 0; i--) {
428              lock.unlock();
429 <            assertEquals(i-1, lock.getHoldCount());
429 >            assertEquals(i - 1, lock.getHoldCount());
430          }
431      }
432  
# Line 434 | Line 436 | public class ReentrantLockTest extends J
436      public void testIsLocked()      { testIsLocked(false); }
437      public void testIsLocked_fair() { testIsLocked(true); }
438      public void testIsLocked(boolean fair) {
439 +        final ReentrantLock lock = new ReentrantLock(fair);
440          try {
438            final ReentrantLock lock = new ReentrantLock(fair);
441              assertFalse(lock.isLocked());
442              lock.lock();
443              assertTrue(lock.isLocked());
# Line 522 | Line 524 | public class ReentrantLockTest extends J
524      public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
525      public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
526      public void testAwaitNanos_Timeout(boolean fair) {
527 +        final ReentrantLock lock = new ReentrantLock(fair);
528 +        final Condition c = lock.newCondition();
529 +        final long timeoutMillis = timeoutMillis();
530 +        final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
531 +        lock.lock();
532 +        final long startTime = System.nanoTime();
533          try {
526            final ReentrantLock lock = new ReentrantLock(fair);
527            final Condition c = lock.newCondition();
528            lock.lock();
529            long startTime = System.nanoTime();
530            long timeoutMillis = 10;
531            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
534              long nanosRemaining = c.awaitNanos(timeoutNanos);
535              assertTrue(nanosRemaining <= 0);
534            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
535            lock.unlock();
536          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
537 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
538 +        lock.unlock();
539      }
540  
541      /**
# Line 542 | Line 544 | public class ReentrantLockTest extends J
544      public void testAwait_Timeout()      { testAwait_Timeout(false); }
545      public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
546      public void testAwait_Timeout(boolean fair) {
547 +        final ReentrantLock lock = new ReentrantLock(fair);
548 +        final Condition c = lock.newCondition();
549 +        final long timeoutMillis = timeoutMillis();
550 +        lock.lock();
551 +        final long startTime = System.nanoTime();
552          try {
546            final ReentrantLock lock = new ReentrantLock(fair);
547            final Condition c = lock.newCondition();
548            lock.lock();
549            long startTime = System.nanoTime();
550            long timeoutMillis = 10;
553              assertFalse(c.await(timeoutMillis, MILLISECONDS));
552            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
553            lock.unlock();
554          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
555 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
556 +        lock.unlock();
557      }
558  
559      /**
# Line 560 | Line 562 | public class ReentrantLockTest extends J
562      public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
563      public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
564      public void testAwaitUntil_Timeout(boolean fair) {
565 +        final ReentrantLock lock = new ReentrantLock(fair);
566 +        final Condition c = lock.newCondition();
567 +        lock.lock();
568 +        // We shouldn't assume that nanoTime and currentTimeMillis
569 +        // use the same time source, so don't use nanoTime here.
570 +        final java.util.Date delayedDate = delayedDate(timeoutMillis());
571          try {
572 <            final ReentrantLock lock = new ReentrantLock(fair);
565 <            final Condition c = lock.newCondition();
566 <            lock.lock();
567 <            long startTime = System.nanoTime();
568 <            long timeoutMillis = 10;
569 <            java.util.Date d = new java.util.Date();
570 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
571 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
572 <            lock.unlock();
572 >            assertFalse(c.awaitUntil(delayedDate));
573          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
574 +        assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
575 +        lock.unlock();
576      }
577  
578      /**
# Line 884 | Line 886 | public class ReentrantLockTest extends J
886      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
887      public void testAwaitUninterruptibly(boolean fair) {
888          final ReentrantLock lock = new ReentrantLock(fair);
889 <        final Condition c = lock.newCondition();
889 >        final Condition condition = lock.newCondition();
890          final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
891  
892          Thread t1 = newStartedThread(new CheckedRunnable() {
# Line 893 | Line 895 | public class ReentrantLockTest extends J
895                  lock.lock();
896                  pleaseInterrupt.countDown();
897                  Thread.currentThread().interrupt();
898 <                c.awaitUninterruptibly();
898 >                condition.awaitUninterruptibly();
899                  assertTrue(Thread.interrupted());
900                  lock.unlock();
901              }});
# Line 903 | Line 905 | public class ReentrantLockTest extends J
905                  // Interrupt during awaitUninterruptibly
906                  lock.lock();
907                  pleaseInterrupt.countDown();
908 <                c.awaitUninterruptibly();
908 >                condition.awaitUninterruptibly();
909                  assertTrue(Thread.interrupted());
910                  lock.unlock();
911              }});
912  
913          await(pleaseInterrupt);
914 +        t2.interrupt();
915          lock.lock();
916          lock.unlock();
917 <        t2.interrupt();
918 <
916 <        assertThreadStaysAlive(t1);
917 <        assertTrue(t2.isAlive());
917 >        assertThreadBlocks(t1, Thread.State.WAITING);
918 >        assertThreadBlocks(t2, Thread.State.WAITING);
919  
920          lock.lock();
921 <        c.signalAll();
921 >        condition.signalAll();
922          lock.unlock();
923  
924          awaitTermination(t1);
# Line 1099 | Line 1100 | public class ReentrantLockTest extends J
1100      public void testSerialization()      { testSerialization(false); }
1101      public void testSerialization_fair() { testSerialization(true); }
1102      public void testSerialization(boolean fair) {
1103 <        ReentrantLock lock = new ReentrantLock(fair);
1103 >        final ReentrantLock lock = new ReentrantLock(fair);
1104          lock.lock();
1105  
1106          ReentrantLock clone = serialClone(lock);
# Line 1125 | Line 1126 | public class ReentrantLockTest extends J
1126      public void testToString()      { testToString(false); }
1127      public void testToString_fair() { testToString(true); }
1128      public void testToString(boolean fair) {
1129 <        ReentrantLock lock = new ReentrantLock(fair);
1129 >        final ReentrantLock lock = new ReentrantLock(fair);
1130          assertTrue(lock.toString().contains("Unlocked"));
1131          lock.lock();
1132 <        assertTrue(lock.toString().contains("Locked"));
1132 >        assertTrue(lock.toString().contains("Locked by"));
1133          lock.unlock();
1134          assertTrue(lock.toString().contains("Unlocked"));
1135      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines