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); |
162 |
|
case awaitNanos: |
163 |
|
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
164 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
165 |
< |
assertTrue(nanosRemaining > 0); |
165 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
166 |
|
assertTrue(nanosRemaining <= timeoutNanos); |
167 |
|
break; |
168 |
|
case awaitUntil: |
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(); |
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(); |
406 |
– |
long timeoutMillis = 10; |
408 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
409 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
410 |
|
}}); |
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()); |
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 { |
439 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
441 |
|
assertFalse(lock.isLocked()); |
442 |
|
lock.lock(); |
443 |
|
assertTrue(lock.isLocked()); |
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 { |
527 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
528 |
– |
final Condition c = lock.newCondition(); |
529 |
– |
lock.lock(); |
530 |
– |
long startTime = System.nanoTime(); |
531 |
– |
long timeoutMillis = 10; |
532 |
– |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
534 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
535 |
|
assertTrue(nanosRemaining <= 0); |
535 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
536 |
– |
lock.unlock(); |
536 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
537 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
538 |
+ |
lock.unlock(); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
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 { |
547 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
548 |
– |
final Condition c = lock.newCondition(); |
549 |
– |
lock.lock(); |
550 |
– |
long startTime = System.nanoTime(); |
551 |
– |
long timeoutMillis = 10; |
553 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
553 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
554 |
– |
lock.unlock(); |
554 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
555 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
556 |
+ |
lock.unlock(); |
557 |
|
} |
558 |
|
|
559 |
|
/** |
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); |
566 |
< |
final Condition c = lock.newCondition(); |
567 |
< |
lock.lock(); |
568 |
< |
long startTime = System.nanoTime(); |
569 |
< |
long timeoutMillis = 10; |
570 |
< |
java.util.Date d = new java.util.Date(); |
571 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
572 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
573 |
< |
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 |
|
/** |
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() { |
895 |
|
lock.lock(); |
896 |
|
pleaseInterrupt.countDown(); |
897 |
|
Thread.currentThread().interrupt(); |
898 |
< |
c.awaitUninterruptibly(); |
898 |
> |
condition.awaitUninterruptibly(); |
899 |
|
assertTrue(Thread.interrupted()); |
900 |
|
lock.unlock(); |
901 |
|
}}); |
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 |
< |
|
917 |
< |
assertThreadStaysAlive(t1); |
918 |
< |
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); |
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); |
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 |
|
} |