6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.locks.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.*; |
11 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.CyclicBarrier; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.ReentrantLock; |
18 |
> |
|
19 |
> |
import junit.framework.AssertionFailedError; |
20 |
> |
import junit.framework.Test; |
21 |
> |
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); |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* A runnable calling lockInterruptibly |
32 |
> |
* A checked runnable calling lockInterruptibly |
33 |
|
*/ |
34 |
|
class InterruptibleLockRunnable extends CheckedRunnable { |
35 |
|
final ReentrantLock lock; |
36 |
< |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
36 |
> |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } |
37 |
|
public void realRun() throws InterruptedException { |
38 |
|
lock.lockInterruptibly(); |
39 |
|
} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* A runnable calling lockInterruptibly that expects to be |
43 |
> |
* A checked runnable calling lockInterruptibly that expects to be |
44 |
|
* interrupted |
45 |
|
*/ |
46 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
47 |
|
final ReentrantLock lock; |
48 |
< |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
48 |
> |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } |
49 |
|
public void realRun() throws InterruptedException { |
50 |
|
lock.lockInterruptibly(); |
51 |
|
} |
72 |
|
* Releases write lock, checking that it had a hold count of 1. |
73 |
|
*/ |
74 |
|
void releaseLock(PublicReentrantLock lock) { |
75 |
< |
assertLockedBy(lock, Thread.currentThread()); |
75 |
> |
assertLockedByMoi(lock); |
76 |
|
lock.unlock(); |
77 |
|
assertFalse(lock.isHeldByCurrentThread()); |
78 |
|
assertNotLocked(lock); |
85 |
|
long startTime = System.nanoTime(); |
86 |
|
while (!lock.hasQueuedThread(t)) { |
87 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
88 |
< |
throw new AssertionError("timed out"); |
88 |
> |
throw new AssertionFailedError("timed out"); |
89 |
|
Thread.yield(); |
90 |
|
} |
91 |
|
assertTrue(t.isAlive()); |
92 |
< |
assertTrue(lock.getOwner() != t); |
92 |
> |
assertNotSame(t, lock.getOwner()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
+ |
* Checks that lock is locked by the current thread. |
119 |
+ |
*/ |
120 |
+ |
void assertLockedByMoi(PublicReentrantLock lock) { |
121 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
/** |
125 |
|
* Checks that condition c has no waiters. |
126 |
|
*/ |
127 |
|
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { |
143 |
|
lock.unlock(); |
144 |
|
} |
145 |
|
|
146 |
+ |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
+ |
|
148 |
+ |
/** |
149 |
+ |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
150 |
+ |
*/ |
151 |
+ |
void await(Condition c, AwaitMethod awaitMethod) |
152 |
+ |
throws InterruptedException { |
153 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
154 |
+ |
switch (awaitMethod) { |
155 |
+ |
case await: |
156 |
+ |
c.await(); |
157 |
+ |
break; |
158 |
+ |
case awaitTimed: |
159 |
+ |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
160 |
+ |
break; |
161 |
+ |
case awaitNanos: |
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))); |
169 |
+ |
break; |
170 |
+ |
default: |
171 |
+ |
throw new AssertionError(); |
172 |
+ |
} |
173 |
+ |
} |
174 |
+ |
|
175 |
|
/** |
176 |
|
* Constructor sets given fairness, and is in unlocked state |
177 |
|
*/ |
194 |
|
/** |
195 |
|
* locking an unlocked lock succeeds |
196 |
|
*/ |
197 |
< |
public void testLock() { |
198 |
< |
PublicReentrantLock lock = new PublicReentrantLock(); |
197 |
> |
public void testLock() { testLock(false); } |
198 |
> |
public void testLock_fair() { testLock(true); } |
199 |
> |
public void testLock(boolean fair) { |
200 |
> |
PublicReentrantLock lock = new PublicReentrantLock(fair); |
201 |
|
lock.lock(); |
202 |
< |
assertLockedBy(lock, Thread.currentThread()); |
158 |
< |
releaseLock(lock); |
159 |
< |
} |
160 |
< |
|
161 |
< |
/** |
162 |
< |
* locking an unlocked fair lock succeeds |
163 |
< |
*/ |
164 |
< |
public void testFairLock() { |
165 |
< |
PublicReentrantLock lock = new PublicReentrantLock(true); |
166 |
< |
lock.lock(); |
167 |
< |
assertLockedBy(lock, Thread.currentThread()); |
202 |
> |
assertLockedByMoi(lock); |
203 |
|
releaseLock(lock); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
208 |
|
*/ |
209 |
< |
public void testUnlock_IllegalMonitorStateException() { |
210 |
< |
ReentrantLock lock = new ReentrantLock(); |
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 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
213 |
|
try { |
214 |
|
lock.unlock(); |
215 |
|
shouldThrow(); |
219 |
|
/** |
220 |
|
* tryLock on an unlocked lock succeeds |
221 |
|
*/ |
222 |
< |
public void testTryLock() { |
223 |
< |
PublicReentrantLock lock = new PublicReentrantLock(); |
222 |
> |
public void testTryLock() { testTryLock(false); } |
223 |
> |
public void testTryLock_fair() { testTryLock(true); } |
224 |
> |
public void testTryLock(boolean fair) { |
225 |
> |
PublicReentrantLock lock = new PublicReentrantLock(fair); |
226 |
|
assertTrue(lock.tryLock()); |
227 |
< |
assertLockedBy(lock, Thread.currentThread()); |
227 |
> |
assertLockedByMoi(lock); |
228 |
> |
assertTrue(lock.tryLock()); |
229 |
> |
assertLockedByMoi(lock); |
230 |
> |
lock.unlock(); |
231 |
|
releaseLock(lock); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* hasQueuedThreads reports whether there are waiting threads |
236 |
|
*/ |
237 |
< |
public void testHasQueuedThreads() throws InterruptedException { |
238 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
237 |
> |
public void testHasQueuedThreads() { testHasQueuedThreads(false); } |
238 |
> |
public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); } |
239 |
> |
public void testHasQueuedThreads(boolean fair) { |
240 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
241 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
242 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
243 |
|
assertFalse(lock.hasQueuedThreads()); |
260 |
|
/** |
261 |
|
* getQueueLength reports number of waiting threads |
262 |
|
*/ |
263 |
< |
public void testGetQueueLength() throws InterruptedException { |
264 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
265 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
266 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
223 |
< |
assertEquals(0, lock.getQueueLength()); |
224 |
< |
lock.lock(); |
225 |
< |
t1.start(); |
226 |
< |
waitForQueuedThread(lock, t1); |
227 |
< |
assertEquals(1, lock.getQueueLength()); |
228 |
< |
t2.start(); |
229 |
< |
waitForQueuedThread(lock, t2); |
230 |
< |
assertEquals(2, lock.getQueueLength()); |
231 |
< |
t1.interrupt(); |
232 |
< |
awaitTermination(t1); |
233 |
< |
assertEquals(1, lock.getQueueLength()); |
234 |
< |
lock.unlock(); |
235 |
< |
awaitTermination(t2); |
236 |
< |
assertEquals(0, lock.getQueueLength()); |
237 |
< |
} |
238 |
< |
|
239 |
< |
/** |
240 |
< |
* getQueueLength reports number of waiting threads |
241 |
< |
*/ |
242 |
< |
public void testGetQueueLength_fair() throws InterruptedException { |
243 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(true); |
263 |
> |
public void testGetQueueLength() { testGetQueueLength(false); } |
264 |
> |
public void testGetQueueLength_fair() { testGetQueueLength(true); } |
265 |
> |
public void testGetQueueLength(boolean fair) { |
266 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
267 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
268 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
269 |
|
assertEquals(0, lock.getQueueLength()); |
285 |
|
/** |
286 |
|
* hasQueuedThread(null) throws NPE |
287 |
|
*/ |
288 |
< |
public void testHasQueuedThreadNPE() { |
289 |
< |
final ReentrantLock lock = new ReentrantLock(); |
288 |
> |
public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); } |
289 |
> |
public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); } |
290 |
> |
public void testHasQueuedThreadNPE(boolean fair) { |
291 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
292 |
|
try { |
293 |
|
lock.hasQueuedThread(null); |
294 |
|
shouldThrow(); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* hasQueuedThread reports whether a thread is queued. |
299 |
> |
* hasQueuedThread reports whether a thread is queued |
300 |
|
*/ |
301 |
< |
public void testHasQueuedThread() throws InterruptedException { |
302 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
301 |
> |
public void testHasQueuedThread() { testHasQueuedThread(false); } |
302 |
> |
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
303 |
> |
public void testHasQueuedThread(boolean fair) { |
304 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
305 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
306 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
307 |
|
assertFalse(lock.hasQueuedThread(t1)); |
328 |
|
/** |
329 |
|
* getQueuedThreads includes waiting threads |
330 |
|
*/ |
331 |
< |
public void testGetQueuedThreads() throws InterruptedException { |
332 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
331 |
> |
public void testGetQueuedThreads() { testGetQueuedThreads(false); } |
332 |
> |
public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); } |
333 |
> |
public void testGetQueuedThreads(boolean fair) { |
334 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
335 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
336 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
337 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
< |
* timed tryLock is interruptible. |
360 |
> |
* timed tryLock is interruptible |
361 |
|
*/ |
362 |
< |
public void testTryLock_Interrupted() throws InterruptedException { |
363 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
362 |
> |
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
363 |
> |
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
364 |
> |
public void testTryLock_Interruptible(boolean fair) { |
365 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
366 |
|
lock.lock(); |
367 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
368 |
|
public void realRun() throws InterruptedException { |
378 |
|
/** |
379 |
|
* tryLock on a locked lock fails |
380 |
|
*/ |
381 |
< |
public void testTryLockWhenLocked() throws InterruptedException { |
382 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
381 |
> |
public void testTryLockWhenLocked() { testTryLockWhenLocked(false); } |
382 |
> |
public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); } |
383 |
> |
public void testTryLockWhenLocked(boolean fair) { |
384 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
385 |
|
lock.lock(); |
386 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
387 |
|
public void realRun() { |
395 |
|
/** |
396 |
|
* Timed tryLock on a locked lock times out |
397 |
|
*/ |
398 |
< |
public void testTryLock_Timeout() throws InterruptedException { |
399 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
398 |
> |
public void testTryLock_Timeout() { testTryLock_Timeout(false); } |
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(); |
371 |
– |
long timeoutMillis = 10; |
407 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
408 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
409 |
|
}}); |
415 |
|
/** |
416 |
|
* getHoldCount returns number of recursive holds |
417 |
|
*/ |
418 |
< |
public void testGetHoldCount() { |
419 |
< |
ReentrantLock lock = new ReentrantLock(); |
418 |
> |
public void testGetHoldCount() { testGetHoldCount(false); } |
419 |
> |
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
420 |
> |
public void testGetHoldCount(boolean 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 |
|
|
432 |
|
/** |
433 |
|
* isLocked is true when locked and false when not |
434 |
|
*/ |
435 |
< |
public void testIsLocked() throws Exception { |
436 |
< |
final ReentrantLock lock = new ReentrantLock(); |
437 |
< |
assertFalse(lock.isLocked()); |
438 |
< |
lock.lock(); |
439 |
< |
assertTrue(lock.isLocked()); |
440 |
< |
lock.lock(); |
441 |
< |
assertTrue(lock.isLocked()); |
442 |
< |
lock.unlock(); |
443 |
< |
assertTrue(lock.isLocked()); |
444 |
< |
lock.unlock(); |
445 |
< |
assertFalse(lock.isLocked()); |
446 |
< |
final CyclicBarrier barrier = new CyclicBarrier(2); |
447 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
448 |
< |
public void realRun() throws Exception { |
449 |
< |
lock.lock(); |
450 |
< |
assertTrue(lock.isLocked()); |
451 |
< |
barrier.await(); |
452 |
< |
barrier.await(); |
453 |
< |
lock.unlock(); |
454 |
< |
}}); |
455 |
< |
|
456 |
< |
barrier.await(); |
457 |
< |
assertTrue(lock.isLocked()); |
458 |
< |
barrier.await(); |
459 |
< |
awaitTermination(t); |
460 |
< |
assertFalse(lock.isLocked()); |
461 |
< |
} |
462 |
< |
|
463 |
< |
/** |
464 |
< |
* lockInterruptibly is interruptible. |
428 |
< |
*/ |
429 |
< |
public void testLockInterruptibly_Interrupted() throws InterruptedException { |
430 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
431 |
< |
lock.lock(); |
432 |
< |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
433 |
< |
waitForQueuedThread(lock, t); |
434 |
< |
t.interrupt(); |
435 |
< |
awaitTermination(t); |
436 |
< |
releaseLock(lock); |
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 { |
440 |
> |
assertFalse(lock.isLocked()); |
441 |
> |
lock.lock(); |
442 |
> |
assertTrue(lock.isLocked()); |
443 |
> |
lock.lock(); |
444 |
> |
assertTrue(lock.isLocked()); |
445 |
> |
lock.unlock(); |
446 |
> |
assertTrue(lock.isLocked()); |
447 |
> |
lock.unlock(); |
448 |
> |
assertFalse(lock.isLocked()); |
449 |
> |
final CyclicBarrier barrier = new CyclicBarrier(2); |
450 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
451 |
> |
public void realRun() throws Exception { |
452 |
> |
lock.lock(); |
453 |
> |
assertTrue(lock.isLocked()); |
454 |
> |
barrier.await(); |
455 |
> |
barrier.await(); |
456 |
> |
lock.unlock(); |
457 |
> |
}}); |
458 |
> |
|
459 |
> |
barrier.await(); |
460 |
> |
assertTrue(lock.isLocked()); |
461 |
> |
barrier.await(); |
462 |
> |
awaitTermination(t); |
463 |
> |
assertFalse(lock.isLocked()); |
464 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
469 |
|
*/ |
470 |
< |
public void testLockInterruptibly_Interrupted2() throws InterruptedException { |
471 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
472 |
< |
lock.lockInterruptibly(); |
470 |
> |
public void testLockInterruptibly() { testLockInterruptibly(false); } |
471 |
> |
public void testLockInterruptibly_fair() { testLockInterruptibly(true); } |
472 |
> |
public void testLockInterruptibly(boolean fair) { |
473 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
474 |
> |
try { |
475 |
> |
lock.lockInterruptibly(); |
476 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
477 |
> |
assertLockedByMoi(lock); |
478 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
479 |
|
waitForQueuedThread(lock, t); |
480 |
|
t.interrupt(); |
487 |
|
/** |
488 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
489 |
|
*/ |
490 |
< |
public void testAwait_IllegalMonitor() throws InterruptedException { |
491 |
< |
final ReentrantLock lock = new ReentrantLock(); |
492 |
< |
final Condition c = lock.newCondition(); |
493 |
< |
try { |
494 |
< |
c.await(); |
495 |
< |
shouldThrow(); |
496 |
< |
} catch (IllegalMonitorStateException success) {} |
497 |
< |
try { |
498 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
499 |
< |
shouldThrow(); |
500 |
< |
} catch (IllegalMonitorStateException success) {} |
501 |
< |
try { |
502 |
< |
c.awaitNanos(100); |
503 |
< |
shouldThrow(); |
471 |
< |
} catch (IllegalMonitorStateException success) {} |
472 |
< |
try { |
473 |
< |
c.awaitUninterruptibly(); |
474 |
< |
shouldThrow(); |
475 |
< |
} catch (IllegalMonitorStateException success) {} |
490 |
> |
public void testAwait_IMSE() { testAwait_IMSE(false); } |
491 |
> |
public void testAwait_IMSE_fair() { testAwait_IMSE(true); } |
492 |
> |
public void testAwait_IMSE(boolean fair) { |
493 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
494 |
> |
final Condition c = lock.newCondition(); |
495 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
496 |
> |
long startTime = System.nanoTime(); |
497 |
> |
try { |
498 |
> |
await(c, awaitMethod); |
499 |
> |
shouldThrow(); |
500 |
> |
} catch (IllegalMonitorStateException success) { |
501 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
502 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
503 |
> |
} |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
508 |
|
*/ |
509 |
< |
public void testSignal_IllegalMonitor() { |
510 |
< |
final ReentrantLock lock = new ReentrantLock(); |
509 |
> |
public void testSignal_IMSE() { testSignal_IMSE(false); } |
510 |
> |
public void testSignal_IMSE_fair() { testSignal_IMSE(true); } |
511 |
> |
public void testSignal_IMSE(boolean fair) { |
512 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
513 |
|
final Condition c = lock.newCondition(); |
514 |
|
try { |
515 |
|
c.signal(); |
520 |
|
/** |
521 |
|
* awaitNanos without a signal times out |
522 |
|
*/ |
523 |
< |
public void testAwaitNanos_Timeout() throws InterruptedException { |
524 |
< |
final ReentrantLock lock = new ReentrantLock(); |
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 |
< |
long startTime = System.nanoTime(); |
532 |
< |
long timeoutMillis = 10; |
533 |
< |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
534 |
< |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
535 |
< |
assertTrue(nanosRemaining <= 0); |
531 |
> |
final long startTime = System.nanoTime(); |
532 |
> |
try { |
533 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
534 |
> |
assertTrue(nanosRemaining <= 0); |
535 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
536 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
537 |
|
lock.unlock(); |
538 |
|
} |
540 |
|
/** |
541 |
|
* timed await without a signal times out |
542 |
|
*/ |
543 |
< |
public void testAwait_Timeout() throws InterruptedException { |
544 |
< |
final ReentrantLock lock = new ReentrantLock(); |
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 |
< |
long startTime = System.nanoTime(); |
551 |
< |
long timeoutMillis = 10; |
552 |
< |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
550 |
> |
final long startTime = System.nanoTime(); |
551 |
> |
try { |
552 |
> |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
553 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
554 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
555 |
|
lock.unlock(); |
556 |
|
} |
558 |
|
/** |
559 |
|
* awaitUntil without a signal times out |
560 |
|
*/ |
561 |
< |
public void testAwaitUntil_Timeout() throws InterruptedException { |
562 |
< |
final ReentrantLock lock = new ReentrantLock(); |
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 |
< |
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); |
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 |
> |
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 |
|
/** |
578 |
|
* await returns when signalled |
579 |
|
*/ |
580 |
< |
public void testAwait() throws InterruptedException { |
581 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
580 |
> |
public void testAwait() { testAwait(false); } |
581 |
> |
public void testAwait_fair() { testAwait(true); } |
582 |
> |
public void testAwait(boolean fair) { |
583 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
584 |
|
final Condition c = lock.newCondition(); |
585 |
|
final CountDownLatch locked = new CountDownLatch(1); |
586 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
591 |
|
lock.unlock(); |
592 |
|
}}); |
593 |
|
|
594 |
< |
locked.await(); |
594 |
> |
await(locked); |
595 |
|
lock.lock(); |
596 |
|
assertHasWaiters(lock, c, t); |
597 |
|
c.signal(); |
604 |
|
/** |
605 |
|
* hasWaiters throws NPE if null |
606 |
|
*/ |
607 |
< |
public void testHasWaitersNPE() { |
608 |
< |
final ReentrantLock lock = new ReentrantLock(); |
607 |
> |
public void testHasWaitersNPE() { testHasWaitersNPE(false); } |
608 |
> |
public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); } |
609 |
> |
public void testHasWaitersNPE(boolean fair) { |
610 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
611 |
|
try { |
612 |
|
lock.hasWaiters(null); |
613 |
|
shouldThrow(); |
617 |
|
/** |
618 |
|
* getWaitQueueLength throws NPE if null |
619 |
|
*/ |
620 |
< |
public void testGetWaitQueueLengthNPE() { |
621 |
< |
final ReentrantLock lock = new ReentrantLock(); |
620 |
> |
public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); } |
621 |
> |
public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); } |
622 |
> |
public void testGetWaitQueueLengthNPE(boolean fair) { |
623 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
624 |
|
try { |
625 |
|
lock.getWaitQueueLength(null); |
626 |
|
shouldThrow(); |
630 |
|
/** |
631 |
|
* getWaitingThreads throws NPE if null |
632 |
|
*/ |
633 |
< |
public void testGetWaitingThreadsNPE() { |
634 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
633 |
> |
public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); } |
634 |
> |
public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); } |
635 |
> |
public void testGetWaitingThreadsNPE(boolean fair) { |
636 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
637 |
|
try { |
638 |
|
lock.getWaitingThreads(null); |
639 |
|
shouldThrow(); |
643 |
|
/** |
644 |
|
* hasWaiters throws IllegalArgumentException if not owned |
645 |
|
*/ |
646 |
< |
public void testHasWaitersIAE() { |
647 |
< |
final ReentrantLock lock = new ReentrantLock(); |
646 |
> |
public void testHasWaitersIAE() { testHasWaitersIAE(false); } |
647 |
> |
public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); } |
648 |
> |
public void testHasWaitersIAE(boolean fair) { |
649 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
650 |
|
final Condition c = lock.newCondition(); |
651 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
651 |
> |
final ReentrantLock lock2 = new ReentrantLock(fair); |
652 |
|
try { |
653 |
|
lock2.hasWaiters(c); |
654 |
|
shouldThrow(); |
658 |
|
/** |
659 |
|
* hasWaiters throws IllegalMonitorStateException if not locked |
660 |
|
*/ |
661 |
< |
public void testHasWaitersIMSE() { |
662 |
< |
final ReentrantLock lock = new ReentrantLock(); |
661 |
> |
public void testHasWaitersIMSE() { testHasWaitersIMSE(false); } |
662 |
> |
public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); } |
663 |
> |
public void testHasWaitersIMSE(boolean fair) { |
664 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
665 |
|
final Condition c = lock.newCondition(); |
666 |
|
try { |
667 |
|
lock.hasWaiters(c); |
672 |
|
/** |
673 |
|
* getWaitQueueLength throws IllegalArgumentException if not owned |
674 |
|
*/ |
675 |
< |
public void testGetWaitQueueLengthIAE() { |
676 |
< |
final ReentrantLock lock = new ReentrantLock(); |
675 |
> |
public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); } |
676 |
> |
public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); } |
677 |
> |
public void testGetWaitQueueLengthIAE(boolean fair) { |
678 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
679 |
|
final Condition c = lock.newCondition(); |
680 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
680 |
> |
final ReentrantLock lock2 = new ReentrantLock(fair); |
681 |
|
try { |
682 |
|
lock2.getWaitQueueLength(c); |
683 |
|
shouldThrow(); |
687 |
|
/** |
688 |
|
* getWaitQueueLength throws IllegalMonitorStateException if not locked |
689 |
|
*/ |
690 |
< |
public void testGetWaitQueueLengthIMSE() { |
691 |
< |
final ReentrantLock lock = new ReentrantLock(); |
690 |
> |
public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); } |
691 |
> |
public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); } |
692 |
> |
public void testGetWaitQueueLengthIMSE(boolean fair) { |
693 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
694 |
|
final Condition c = lock.newCondition(); |
695 |
|
try { |
696 |
|
lock.getWaitQueueLength(c); |
701 |
|
/** |
702 |
|
* getWaitingThreads throws IllegalArgumentException if not owned |
703 |
|
*/ |
704 |
< |
public void testGetWaitingThreadsIAE() { |
705 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
704 |
> |
public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); } |
705 |
> |
public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); } |
706 |
> |
public void testGetWaitingThreadsIAE(boolean fair) { |
707 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
708 |
|
final Condition c = lock.newCondition(); |
709 |
< |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
709 |
> |
final PublicReentrantLock lock2 = new PublicReentrantLock(fair); |
710 |
|
try { |
711 |
|
lock2.getWaitingThreads(c); |
712 |
|
shouldThrow(); |
716 |
|
/** |
717 |
|
* getWaitingThreads throws IllegalMonitorStateException if not locked |
718 |
|
*/ |
719 |
< |
public void testGetWaitingThreadsIMSE() { |
720 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
719 |
> |
public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); } |
720 |
> |
public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); } |
721 |
> |
public void testGetWaitingThreadsIMSE(boolean fair) { |
722 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
723 |
|
final Condition c = lock.newCondition(); |
724 |
|
try { |
725 |
|
lock.getWaitingThreads(c); |
730 |
|
/** |
731 |
|
* hasWaiters returns true when a thread is waiting, else false |
732 |
|
*/ |
733 |
< |
public void testHasWaiters() throws InterruptedException { |
734 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
733 |
> |
public void testHasWaiters() { testHasWaiters(false); } |
734 |
> |
public void testHasWaiters_fair() { testHasWaiters(true); } |
735 |
> |
public void testHasWaiters(boolean fair) { |
736 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
737 |
|
final Condition c = lock.newCondition(); |
738 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
738 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
739 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
740 |
|
public void realRun() throws InterruptedException { |
741 |
|
lock.lock(); |
742 |
|
assertHasNoWaiters(lock, c); |
743 |
|
assertFalse(lock.hasWaiters(c)); |
744 |
< |
locked.countDown(); |
744 |
> |
pleaseSignal.countDown(); |
745 |
|
c.await(); |
746 |
|
assertHasNoWaiters(lock, c); |
747 |
|
assertFalse(lock.hasWaiters(c)); |
748 |
|
lock.unlock(); |
749 |
|
}}); |
750 |
|
|
751 |
< |
locked.await(); |
751 |
> |
await(pleaseSignal); |
752 |
|
lock.lock(); |
753 |
|
assertHasWaiters(lock, c, t); |
754 |
|
assertTrue(lock.hasWaiters(c)); |
763 |
|
/** |
764 |
|
* getWaitQueueLength returns number of waiting threads |
765 |
|
*/ |
766 |
< |
public void testGetWaitQueueLength() throws InterruptedException { |
767 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
766 |
> |
public void testGetWaitQueueLength() { testGetWaitQueueLength(false); } |
767 |
> |
public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); } |
768 |
> |
public void testGetWaitQueueLength(boolean fair) { |
769 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
770 |
|
final Condition c = lock.newCondition(); |
771 |
|
final CountDownLatch locked1 = new CountDownLatch(1); |
772 |
|
final CountDownLatch locked2 = new CountDownLatch(1); |
795 |
|
lock.unlock(); |
796 |
|
|
797 |
|
t1.start(); |
798 |
< |
locked1.await(); |
798 |
> |
await(locked1); |
799 |
|
|
800 |
|
lock.lock(); |
801 |
|
assertHasWaiters(lock, c, t1); |
803 |
|
lock.unlock(); |
804 |
|
|
805 |
|
t2.start(); |
806 |
< |
locked2.await(); |
806 |
> |
await(locked2); |
807 |
|
|
808 |
|
lock.lock(); |
809 |
|
assertHasWaiters(lock, c, t1, t2); |
821 |
|
/** |
822 |
|
* getWaitingThreads returns only and all waiting threads |
823 |
|
*/ |
824 |
< |
public void testGetWaitingThreads() throws InterruptedException { |
825 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
824 |
> |
public void testGetWaitingThreads() { testGetWaitingThreads(false); } |
825 |
> |
public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); } |
826 |
> |
public void testGetWaitingThreads(boolean fair) { |
827 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
828 |
|
final Condition c = lock.newCondition(); |
829 |
|
final CountDownLatch locked1 = new CountDownLatch(1); |
830 |
|
final CountDownLatch locked2 = new CountDownLatch(1); |
851 |
|
lock.unlock(); |
852 |
|
|
853 |
|
t1.start(); |
854 |
< |
locked1.await(); |
854 |
> |
await(locked1); |
855 |
|
|
856 |
|
lock.lock(); |
857 |
|
assertHasWaiters(lock, c, t1); |
861 |
|
lock.unlock(); |
862 |
|
|
863 |
|
t2.start(); |
864 |
< |
locked2.await(); |
864 |
> |
await(locked2); |
865 |
|
|
866 |
|
lock.lock(); |
867 |
|
assertHasWaiters(lock, c, t1, t2); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
882 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
882 |
> |
* awaitUninterruptibly is uninterruptible |
883 |
|
*/ |
884 |
< |
public void testAwaitUninterruptibly() throws InterruptedException { |
885 |
< |
final ReentrantLock lock = new ReentrantLock(); |
886 |
< |
final Condition c = lock.newCondition(); |
887 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
888 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
884 |
> |
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
885 |
> |
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
886 |
> |
public void testAwaitUninterruptibly(boolean fair) { |
887 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
888 |
> |
final Condition condition = lock.newCondition(); |
889 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
890 |
> |
|
891 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
892 |
|
public void realRun() { |
893 |
+ |
// Interrupt before awaitUninterruptibly |
894 |
|
lock.lock(); |
895 |
< |
locked.countDown(); |
896 |
< |
c.awaitUninterruptibly(); |
895 |
> |
pleaseInterrupt.countDown(); |
896 |
> |
Thread.currentThread().interrupt(); |
897 |
> |
condition.awaitUninterruptibly(); |
898 |
|
assertTrue(Thread.interrupted()); |
899 |
|
lock.unlock(); |
900 |
|
}}); |
901 |
|
|
902 |
< |
locked.await(); |
902 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
903 |
> |
public void realRun() { |
904 |
> |
// Interrupt during awaitUninterruptibly |
905 |
> |
lock.lock(); |
906 |
> |
pleaseInterrupt.countDown(); |
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 |
< |
t.interrupt(); |
917 |
< |
long timeoutMillis = 10; |
918 |
< |
assertThreadJoinTimesOut(t, timeoutMillis); |
916 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
917 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
918 |
> |
|
919 |
|
lock.lock(); |
920 |
< |
c.signal(); |
920 |
> |
condition.signalAll(); |
921 |
|
lock.unlock(); |
838 |
– |
awaitTermination(t); |
839 |
– |
} |
840 |
– |
|
841 |
– |
/** |
842 |
– |
* await is interruptible |
843 |
– |
*/ |
844 |
– |
public void testAwait_Interrupt() throws InterruptedException { |
845 |
– |
final PublicReentrantLock lock = new PublicReentrantLock(); |
846 |
– |
final Condition c = lock.newCondition(); |
847 |
– |
final CountDownLatch locked = new CountDownLatch(1); |
848 |
– |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
849 |
– |
public void realRun() throws InterruptedException { |
850 |
– |
lock.lock(); |
851 |
– |
assertTrue(lock.isLocked()); |
852 |
– |
assertTrue(lock.isHeldByCurrentThread()); |
853 |
– |
assertHasNoWaiters(lock, c); |
854 |
– |
locked.countDown(); |
855 |
– |
try { |
856 |
– |
c.await(); |
857 |
– |
} finally { |
858 |
– |
assertTrue(lock.isLocked()); |
859 |
– |
assertTrue(lock.isHeldByCurrentThread()); |
860 |
– |
assertHasNoWaiters(lock, c); |
861 |
– |
lock.unlock(); |
862 |
– |
assertFalse(Thread.interrupted()); |
863 |
– |
} |
864 |
– |
}}); |
922 |
|
|
923 |
< |
locked.await(); |
924 |
< |
assertHasWaiters(lock, c, t); |
868 |
< |
t.interrupt(); |
869 |
< |
awaitTermination(t); |
870 |
< |
assertFalse(lock.isLocked()); |
923 |
> |
awaitTermination(t1); |
924 |
> |
awaitTermination(t2); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
928 |
< |
* awaitNanos is interruptible |
928 |
> |
* await/awaitNanos/awaitUntil is interruptible |
929 |
|
*/ |
930 |
< |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
931 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
930 |
> |
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
931 |
> |
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
932 |
> |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
933 |
> |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
934 |
> |
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
935 |
> |
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
936 |
> |
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
937 |
> |
public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); } |
938 |
> |
public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) { |
939 |
> |
final PublicReentrantLock lock = |
940 |
> |
new PublicReentrantLock(fair); |
941 |
|
final Condition c = lock.newCondition(); |
942 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
942 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
943 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
944 |
|
public void realRun() throws InterruptedException { |
945 |
|
lock.lock(); |
946 |
< |
assertTrue(lock.isLocked()); |
884 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
946 |
> |
assertLockedByMoi(lock); |
947 |
|
assertHasNoWaiters(lock, c); |
948 |
< |
locked.countDown(); |
948 |
> |
pleaseInterrupt.countDown(); |
949 |
|
try { |
950 |
< |
c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
950 |
> |
await(c, awaitMethod); |
951 |
|
} finally { |
952 |
< |
assertTrue(lock.isLocked()); |
891 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
952 |
> |
assertLockedByMoi(lock); |
953 |
|
assertHasNoWaiters(lock, c); |
954 |
|
lock.unlock(); |
955 |
|
assertFalse(Thread.interrupted()); |
956 |
|
} |
957 |
|
}}); |
958 |
|
|
959 |
< |
locked.await(); |
959 |
> |
await(pleaseInterrupt); |
960 |
|
assertHasWaiters(lock, c, t); |
961 |
|
t.interrupt(); |
962 |
|
awaitTermination(t); |
963 |
< |
assertFalse(lock.isLocked()); |
963 |
> |
assertNotLocked(lock); |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
< |
* awaitUntil is interruptible |
967 |
> |
* signalAll wakes up all threads |
968 |
|
*/ |
969 |
< |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
970 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
969 |
> |
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
970 |
> |
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
971 |
> |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
972 |
> |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
973 |
> |
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
974 |
> |
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
975 |
> |
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
976 |
> |
public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); } |
977 |
> |
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
978 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
979 |
|
final Condition c = lock.newCondition(); |
980 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
981 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
980 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
981 |
> |
class Awaiter extends CheckedRunnable { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
|
lock.lock(); |
984 |
< |
assertTrue(lock.isLocked()); |
985 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
986 |
< |
assertHasNoWaiters(lock, c); |
987 |
< |
locked.countDown(); |
988 |
< |
java.util.Date d = new java.util.Date(); |
920 |
< |
try { |
921 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)); |
922 |
< |
} finally { |
923 |
< |
assertTrue(lock.isLocked()); |
924 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
925 |
< |
assertHasNoWaiters(lock, c); |
926 |
< |
lock.unlock(); |
927 |
< |
assertFalse(Thread.interrupted()); |
928 |
< |
} |
929 |
< |
}}); |
984 |
> |
pleaseSignal.countDown(); |
985 |
> |
await(c, awaitMethod); |
986 |
> |
lock.unlock(); |
987 |
> |
} |
988 |
> |
} |
989 |
|
|
990 |
< |
locked.await(); |
991 |
< |
assertHasWaiters(lock, c, t); |
992 |
< |
t.interrupt(); |
993 |
< |
awaitTermination(t); |
994 |
< |
assertFalse(lock.isLocked()); |
990 |
> |
Thread t1 = newStartedThread(new Awaiter()); |
991 |
> |
Thread t2 = newStartedThread(new Awaiter()); |
992 |
> |
|
993 |
> |
await(pleaseSignal); |
994 |
> |
lock.lock(); |
995 |
> |
assertHasWaiters(lock, c, t1, t2); |
996 |
> |
c.signalAll(); |
997 |
> |
assertHasNoWaiters(lock, c); |
998 |
> |
lock.unlock(); |
999 |
> |
awaitTermination(t1); |
1000 |
> |
awaitTermination(t2); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* signalAll wakes up all threads |
1004 |
> |
* signal wakes up waiting threads in FIFO order |
1005 |
|
*/ |
1006 |
< |
public void testSignalAll() throws InterruptedException { |
1007 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
1006 |
> |
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1007 |
> |
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1008 |
> |
public void testSignalWakesFifo(boolean fair) { |
1009 |
> |
final PublicReentrantLock lock = |
1010 |
> |
new PublicReentrantLock(fair); |
1011 |
|
final Condition c = lock.newCondition(); |
1012 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1012 |
> |
final CountDownLatch locked1 = new CountDownLatch(1); |
1013 |
> |
final CountDownLatch locked2 = new CountDownLatch(1); |
1014 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1015 |
|
public void realRun() throws InterruptedException { |
1016 |
|
lock.lock(); |
1017 |
< |
locked.countDown(); |
1017 |
> |
locked1.countDown(); |
1018 |
|
c.await(); |
1019 |
|
lock.unlock(); |
1020 |
|
}}); |
1021 |
|
|
1022 |
+ |
await(locked1); |
1023 |
+ |
|
1024 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
1025 |
|
public void realRun() throws InterruptedException { |
1026 |
|
lock.lock(); |
1027 |
< |
locked.countDown(); |
1027 |
> |
locked2.countDown(); |
1028 |
|
c.await(); |
1029 |
|
lock.unlock(); |
1030 |
|
}}); |
1031 |
|
|
1032 |
< |
locked.await(); |
1032 |
> |
await(locked2); |
1033 |
> |
|
1034 |
|
lock.lock(); |
1035 |
|
assertHasWaiters(lock, c, t1, t2); |
1036 |
< |
c.signalAll(); |
1036 |
> |
assertFalse(lock.hasQueuedThreads()); |
1037 |
> |
c.signal(); |
1038 |
> |
assertHasWaiters(lock, c, t2); |
1039 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1040 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1041 |
> |
c.signal(); |
1042 |
|
assertHasNoWaiters(lock, c); |
1043 |
+ |
assertTrue(lock.hasQueuedThread(t1)); |
1044 |
+ |
assertTrue(lock.hasQueuedThread(t2)); |
1045 |
|
lock.unlock(); |
1046 |
|
awaitTermination(t1); |
1047 |
|
awaitTermination(t2); |
1050 |
|
/** |
1051 |
|
* await after multiple reentrant locking preserves lock count |
1052 |
|
*/ |
1053 |
< |
public void testAwaitLockCount() throws InterruptedException { |
1054 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
1053 |
> |
public void testAwaitLockCount() { testAwaitLockCount(false); } |
1054 |
> |
public void testAwaitLockCount_fair() { testAwaitLockCount(true); } |
1055 |
> |
public void testAwaitLockCount(boolean fair) { |
1056 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1057 |
|
final Condition c = lock.newCondition(); |
1058 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1058 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1059 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
|
lock.lock(); |
1062 |
< |
assertLockedBy(lock, Thread.currentThread()); |
1062 |
> |
assertLockedByMoi(lock); |
1063 |
|
assertEquals(1, lock.getHoldCount()); |
1064 |
< |
locked.countDown(); |
1064 |
> |
pleaseSignal.countDown(); |
1065 |
|
c.await(); |
1066 |
< |
assertLockedBy(lock, Thread.currentThread()); |
1066 |
> |
assertLockedByMoi(lock); |
1067 |
|
assertEquals(1, lock.getHoldCount()); |
1068 |
|
lock.unlock(); |
1069 |
|
}}); |
1072 |
|
public void realRun() throws InterruptedException { |
1073 |
|
lock.lock(); |
1074 |
|
lock.lock(); |
1075 |
< |
assertLockedBy(lock, Thread.currentThread()); |
1075 |
> |
assertLockedByMoi(lock); |
1076 |
|
assertEquals(2, lock.getHoldCount()); |
1077 |
< |
locked.countDown(); |
1077 |
> |
pleaseSignal.countDown(); |
1078 |
|
c.await(); |
1079 |
< |
assertLockedBy(lock, Thread.currentThread()); |
1079 |
> |
assertLockedByMoi(lock); |
1080 |
|
assertEquals(2, lock.getHoldCount()); |
1081 |
|
lock.unlock(); |
1082 |
|
lock.unlock(); |
1083 |
|
}}); |
1084 |
|
|
1085 |
< |
locked.await(); |
1085 |
> |
await(pleaseSignal); |
1086 |
|
lock.lock(); |
1087 |
|
assertHasWaiters(lock, c, t1, t2); |
1088 |
|
assertEquals(1, lock.getHoldCount()); |
1096 |
|
/** |
1097 |
|
* A serialized lock deserializes as unlocked |
1098 |
|
*/ |
1099 |
< |
public void testSerialization() throws Exception { |
1100 |
< |
ReentrantLock l = new ReentrantLock(); |
1101 |
< |
l.lock(); |
1102 |
< |
l.unlock(); |
1103 |
< |
|
1104 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1105 |
< |
ObjectOutputStream out = |
1106 |
< |
new ObjectOutputStream(new BufferedOutputStream(bout)); |
1107 |
< |
out.writeObject(l); |
1108 |
< |
out.close(); |
1109 |
< |
|
1110 |
< |
ByteArrayInputStream bin = |
1111 |
< |
new ByteArrayInputStream(bout.toByteArray()); |
1112 |
< |
ObjectInputStream in = |
1113 |
< |
new ObjectInputStream(new BufferedInputStream(bin)); |
1114 |
< |
ReentrantLock r = (ReentrantLock) in.readObject(); |
1115 |
< |
r.lock(); |
1116 |
< |
r.unlock(); |
1099 |
> |
public void testSerialization() { testSerialization(false); } |
1100 |
> |
public void testSerialization_fair() { testSerialization(true); } |
1101 |
> |
public void testSerialization(boolean fair) { |
1102 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1103 |
> |
lock.lock(); |
1104 |
> |
|
1105 |
> |
ReentrantLock clone = serialClone(lock); |
1106 |
> |
assertEquals(lock.isFair(), clone.isFair()); |
1107 |
> |
assertTrue(lock.isLocked()); |
1108 |
> |
assertFalse(clone.isLocked()); |
1109 |
> |
assertEquals(1, lock.getHoldCount()); |
1110 |
> |
assertEquals(0, clone.getHoldCount()); |
1111 |
> |
clone.lock(); |
1112 |
> |
clone.lock(); |
1113 |
> |
assertTrue(clone.isLocked()); |
1114 |
> |
assertEquals(2, clone.getHoldCount()); |
1115 |
> |
assertEquals(1, lock.getHoldCount()); |
1116 |
> |
clone.unlock(); |
1117 |
> |
clone.unlock(); |
1118 |
> |
assertTrue(lock.isLocked()); |
1119 |
> |
assertFalse(clone.isLocked()); |
1120 |
|
} |
1121 |
|
|
1122 |
|
/** |
1123 |
|
* toString indicates current lock state |
1124 |
|
*/ |
1125 |
< |
public void testToString() { |
1126 |
< |
ReentrantLock lock = new ReentrantLock(); |
1127 |
< |
String us = lock.toString(); |
1128 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1125 |
> |
public void testToString() { testToString(false); } |
1126 |
> |
public void testToString_fair() { testToString(true); } |
1127 |
> |
public void testToString(boolean fair) { |
1128 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1129 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1130 |
|
lock.lock(); |
1131 |
< |
String ls = lock.toString(); |
1132 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1131 |
> |
assertTrue(lock.toString().contains("Locked by")); |
1132 |
> |
lock.unlock(); |
1133 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1134 |
|
} |
1135 |
|
} |