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(); |
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(); |
406 |
– |
long timeoutMillis = 10; |
407 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
408 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
409 |
|
}}); |
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()); |
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 { |
439 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
440 |
|
assertFalse(lock.isLocked()); |
441 |
|
lock.lock(); |
442 |
|
assertTrue(lock.isLocked()); |
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 { |
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); |
533 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
534 |
|
assertTrue(nanosRemaining <= 0); |
535 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
536 |
– |
lock.unlock(); |
535 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
536 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
537 |
+ |
lock.unlock(); |
538 |
|
} |
539 |
|
|
540 |
|
/** |
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 { |
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; |
552 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
553 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
554 |
– |
lock.unlock(); |
553 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
554 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
555 |
+ |
lock.unlock(); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
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 { |
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 |
– |
java.util.Date delayedDate = delayedDate(timeoutMillis()); |
571 |
|
assertFalse(c.awaitUntil(delayedDate)); |
572 |
– |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
573 |
– |
lock.unlock(); |
572 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
573 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
574 |
+ |
lock.unlock(); |
575 |
|
} |
576 |
|
|
577 |
|
/** |
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 |
|
} |