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.57 by jsr166, Fri Feb 27 21:43:18 2015 UTC vs.
Revision 1.65 by jsr166, Sun May 14 02:03:15 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines