78 |
|
long startTime = System.nanoTime(); |
79 |
|
while (!lock.hasQueuedThread(t)) { |
80 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
81 |
< |
throw new AssertionError("timed out"); |
81 |
> |
throw new AssertionFailedError("timed out"); |
82 |
|
Thread.yield(); |
83 |
|
} |
84 |
|
assertTrue(t.isAlive()); |
136 |
|
lock.unlock(); |
137 |
|
} |
138 |
|
|
139 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
139 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
140 |
|
|
141 |
|
/** |
142 |
|
* Awaits condition using the specified AwaitMethod. |
143 |
|
*/ |
144 |
|
void await(Condition c, AwaitMethod awaitMethod) |
145 |
|
throws InterruptedException { |
146 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
147 |
|
switch (awaitMethod) { |
148 |
|
case await: |
149 |
|
c.await(); |
150 |
|
break; |
151 |
+ |
case awaitTimed: |
152 |
+ |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
153 |
+ |
break; |
154 |
|
case awaitNanos: |
155 |
< |
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
155 |
> |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
156 |
> |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
157 |
|
assertTrue(nanosRemaining > 0); |
158 |
|
break; |
159 |
|
case awaitUntil: |
160 |
< |
java.util.Date d = new java.util.Date(); |
156 |
< |
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
160 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
161 |
|
break; |
162 |
|
} |
163 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* hasQueuedThread reports whether a thread is queued. |
289 |
> |
* hasQueuedThread reports whether a thread is queued |
290 |
|
*/ |
291 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
292 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
< |
* timed tryLock is interruptible. |
350 |
> |
* timed tryLock is interruptible |
351 |
|
*/ |
352 |
|
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
353 |
|
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
486 |
|
public void testAwait_IMSE(boolean fair) { |
487 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
488 |
|
final Condition c = lock.newCondition(); |
489 |
< |
long startTime = System.nanoTime(); |
490 |
< |
try { |
487 |
< |
try { |
488 |
< |
c.await(); |
489 |
< |
shouldThrow(); |
490 |
< |
} catch (IllegalMonitorStateException success) {} |
491 |
< |
try { |
492 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
493 |
< |
shouldThrow(); |
494 |
< |
} catch (IllegalMonitorStateException success) {} |
495 |
< |
try { |
496 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
497 |
< |
shouldThrow(); |
498 |
< |
} catch (IllegalMonitorStateException success) {} |
489 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
490 |
> |
long startTime = System.nanoTime(); |
491 |
|
try { |
492 |
< |
c.awaitUninterruptibly(); |
492 |
> |
await(c, awaitMethod); |
493 |
|
shouldThrow(); |
494 |
< |
} catch (IllegalMonitorStateException success) {} |
495 |
< |
} catch (InterruptedException ie) { |
496 |
< |
threadUnexpectedException(ie); |
494 |
> |
} catch (IllegalMonitorStateException success) { |
495 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
496 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
497 |
|
} |
506 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
913 |
|
*/ |
914 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
915 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
916 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
917 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
918 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
919 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
920 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
952 |
|
*/ |
953 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
954 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
955 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
956 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
957 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
958 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
959 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
< |
* signal wakes up waiting threads in FIFO order. |
988 |
> |
* signal wakes up waiting threads in FIFO order |
989 |
|
*/ |
990 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
991 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1110 |
|
public void testToString_fair() { testToString(true); } |
1111 |
|
public void testToString(boolean fair) { |
1112 |
|
ReentrantLock lock = new ReentrantLock(fair); |
1113 |
< |
String us = lock.toString(); |
1119 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1113 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1114 |
|
lock.lock(); |
1115 |
< |
String ls = lock.toString(); |
1116 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1115 |
> |
assertTrue(lock.toString().contains("Locked")); |
1116 |
> |
lock.unlock(); |
1117 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1118 |
|
} |
1119 |
|
} |