blob: 17eaf76506ae3018332277b7af0e3db9e12f15d4 [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9package jsr166;
10
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010011import static java.util.concurrent.TimeUnit.MILLISECONDS;
12
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.HashSet;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010016import java.util.concurrent.CountDownLatch;
17import java.util.concurrent.CyclicBarrier;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010018import java.util.concurrent.locks.Condition;
19import java.util.concurrent.locks.ReentrantLock;
20
21import junit.framework.AssertionFailedError;
22import junit.framework.Test;
23import junit.framework.TestSuite;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010024
25public class ReentrantLockTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010026 // android-note: Removed because the CTS runner does a bad job of
27 // retrying tests that have suite() declarations.
28 //
29 // public static void main(String[] args) {
30 // main(suite(), args);
31 // }
32 // public static Test suite() {
33 // return new TestSuite(...);
34 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010035 /**
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010036 * A checked runnable calling lockInterruptibly
Calin Juravle8f0d92b2013-08-01 17:26:00 +010037 */
38 class InterruptibleLockRunnable extends CheckedRunnable {
39 final ReentrantLock lock;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010040 InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010041 public void realRun() throws InterruptedException {
42 lock.lockInterruptibly();
43 }
44 }
45
46 /**
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010047 * A checked runnable calling lockInterruptibly that expects to be
Calin Juravle8f0d92b2013-08-01 17:26:00 +010048 * interrupted
49 */
50 class InterruptedLockRunnable extends CheckedInterruptedRunnable {
51 final ReentrantLock lock;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010052 InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010053 public void realRun() throws InterruptedException {
54 lock.lockInterruptibly();
55 }
56 }
57
58 /**
59 * Subclass to expose protected methods
60 */
61 static class PublicReentrantLock extends ReentrantLock {
62 PublicReentrantLock() { super(); }
63 PublicReentrantLock(boolean fair) { super(fair); }
64 public Thread getOwner() {
65 return super.getOwner();
66 }
67 public Collection<Thread> getQueuedThreads() {
68 return super.getQueuedThreads();
69 }
70 public Collection<Thread> getWaitingThreads(Condition c) {
71 return super.getWaitingThreads(c);
72 }
73 }
74
75 /**
76 * Releases write lock, checking that it had a hold count of 1.
77 */
78 void releaseLock(PublicReentrantLock lock) {
79 assertLockedByMoi(lock);
80 lock.unlock();
81 assertFalse(lock.isHeldByCurrentThread());
82 assertNotLocked(lock);
83 }
84
85 /**
86 * Spin-waits until lock.hasQueuedThread(t) becomes true.
87 */
88 void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
89 long startTime = System.nanoTime();
90 while (!lock.hasQueuedThread(t)) {
91 if (millisElapsedSince(startTime) > LONG_DELAY_MS)
92 throw new AssertionFailedError("timed out");
93 Thread.yield();
94 }
95 assertTrue(t.isAlive());
96 assertNotSame(t, lock.getOwner());
97 }
98
99 /**
100 * Checks that lock is not locked.
101 */
102 void assertNotLocked(PublicReentrantLock lock) {
103 assertFalse(lock.isLocked());
104 assertFalse(lock.isHeldByCurrentThread());
105 assertNull(lock.getOwner());
106 assertEquals(0, lock.getHoldCount());
107 }
108
109 /**
110 * Checks that lock is locked by the given thread.
111 */
112 void assertLockedBy(PublicReentrantLock lock, Thread t) {
113 assertTrue(lock.isLocked());
114 assertSame(t, lock.getOwner());
115 assertEquals(t == Thread.currentThread(),
116 lock.isHeldByCurrentThread());
117 assertEquals(t == Thread.currentThread(),
118 lock.getHoldCount() > 0);
119 }
120
121 /**
122 * Checks that lock is locked by the current thread.
123 */
124 void assertLockedByMoi(PublicReentrantLock lock) {
125 assertLockedBy(lock, Thread.currentThread());
126 }
127
128 /**
129 * Checks that condition c has no waiters.
130 */
131 void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
132 assertHasWaiters(lock, c, new Thread[] {});
133 }
134
135 /**
136 * Checks that condition c has exactly the given waiter threads.
137 */
138 void assertHasWaiters(PublicReentrantLock lock, Condition c,
139 Thread... threads) {
140 lock.lock();
141 assertEquals(threads.length > 0, lock.hasWaiters(c));
142 assertEquals(threads.length, lock.getWaitQueueLength(c));
143 assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
144 assertEquals(threads.length, lock.getWaitingThreads(c).size());
145 assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
146 new HashSet<Thread>(Arrays.asList(threads)));
147 lock.unlock();
148 }
149
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100150 enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100151
152 /**
153 * Awaits condition using the specified AwaitMethod.
154 */
155 void await(Condition c, AwaitMethod awaitMethod)
156 throws InterruptedException {
157 long timeoutMillis = 2 * LONG_DELAY_MS;
158 switch (awaitMethod) {
159 case await:
160 c.await();
161 break;
162 case awaitTimed:
163 assertTrue(c.await(timeoutMillis, MILLISECONDS));
164 break;
165 case awaitNanos:
166 long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
167 long nanosRemaining = c.awaitNanos(nanosTimeout);
168 assertTrue(nanosRemaining > 0);
169 break;
170 case awaitUntil:
171 assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
172 break;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100173 default:
174 throw new AssertionError();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100175 }
176 }
177
178 /**
179 * Constructor sets given fairness, and is in unlocked state
180 */
181 public void testConstructor() {
182 PublicReentrantLock lock;
183
184 lock = new PublicReentrantLock();
185 assertFalse(lock.isFair());
186 assertNotLocked(lock);
187
188 lock = new PublicReentrantLock(true);
189 assertTrue(lock.isFair());
190 assertNotLocked(lock);
191
192 lock = new PublicReentrantLock(false);
193 assertFalse(lock.isFair());
194 assertNotLocked(lock);
195 }
196
197 /**
198 * locking an unlocked lock succeeds
199 */
200 public void testLock() { testLock(false); }
201 public void testLock_fair() { testLock(true); }
202 public void testLock(boolean fair) {
203 PublicReentrantLock lock = new PublicReentrantLock(fair);
204 lock.lock();
205 assertLockedByMoi(lock);
206 releaseLock(lock);
207 }
208
209 /**
210 * Unlocking an unlocked lock throws IllegalMonitorStateException
211 */
212 public void testUnlock_IMSE() { testUnlock_IMSE(false); }
213 public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
214 public void testUnlock_IMSE(boolean fair) {
215 ReentrantLock lock = new ReentrantLock(fair);
216 try {
217 lock.unlock();
218 shouldThrow();
219 } catch (IllegalMonitorStateException success) {}
220 }
221
222 /**
223 * tryLock on an unlocked lock succeeds
224 */
225 public void testTryLock() { testTryLock(false); }
226 public void testTryLock_fair() { testTryLock(true); }
227 public void testTryLock(boolean fair) {
228 PublicReentrantLock lock = new PublicReentrantLock(fair);
229 assertTrue(lock.tryLock());
230 assertLockedByMoi(lock);
231 assertTrue(lock.tryLock());
232 assertLockedByMoi(lock);
233 lock.unlock();
234 releaseLock(lock);
235 }
236
237 /**
238 * hasQueuedThreads reports whether there are waiting threads
239 */
240 public void testHasQueuedThreads() { testHasQueuedThreads(false); }
241 public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
242 public void testHasQueuedThreads(boolean fair) {
243 final PublicReentrantLock lock = new PublicReentrantLock(fair);
244 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
245 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
246 assertFalse(lock.hasQueuedThreads());
247 lock.lock();
248 assertFalse(lock.hasQueuedThreads());
249 t1.start();
250 waitForQueuedThread(lock, t1);
251 assertTrue(lock.hasQueuedThreads());
252 t2.start();
253 waitForQueuedThread(lock, t2);
254 assertTrue(lock.hasQueuedThreads());
255 t1.interrupt();
256 awaitTermination(t1);
257 assertTrue(lock.hasQueuedThreads());
258 lock.unlock();
259 awaitTermination(t2);
260 assertFalse(lock.hasQueuedThreads());
261 }
262
263 /**
264 * getQueueLength reports number of waiting threads
265 */
266 public void testGetQueueLength() { testGetQueueLength(false); }
267 public void testGetQueueLength_fair() { testGetQueueLength(true); }
268 public void testGetQueueLength(boolean fair) {
269 final PublicReentrantLock lock = new PublicReentrantLock(fair);
270 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
271 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
272 assertEquals(0, lock.getQueueLength());
273 lock.lock();
274 t1.start();
275 waitForQueuedThread(lock, t1);
276 assertEquals(1, lock.getQueueLength());
277 t2.start();
278 waitForQueuedThread(lock, t2);
279 assertEquals(2, lock.getQueueLength());
280 t1.interrupt();
281 awaitTermination(t1);
282 assertEquals(1, lock.getQueueLength());
283 lock.unlock();
284 awaitTermination(t2);
285 assertEquals(0, lock.getQueueLength());
286 }
287
288 /**
289 * hasQueuedThread(null) throws NPE
290 */
291 public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
292 public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
293 public void testHasQueuedThreadNPE(boolean fair) {
294 final ReentrantLock lock = new ReentrantLock(fair);
295 try {
296 lock.hasQueuedThread(null);
297 shouldThrow();
298 } catch (NullPointerException success) {}
299 }
300
301 /**
302 * hasQueuedThread reports whether a thread is queued
303 */
304 public void testHasQueuedThread() { testHasQueuedThread(false); }
305 public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
306 public void testHasQueuedThread(boolean fair) {
307 final PublicReentrantLock lock = new PublicReentrantLock(fair);
308 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
309 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
310 assertFalse(lock.hasQueuedThread(t1));
311 assertFalse(lock.hasQueuedThread(t2));
312 lock.lock();
313 t1.start();
314 waitForQueuedThread(lock, t1);
315 assertTrue(lock.hasQueuedThread(t1));
316 assertFalse(lock.hasQueuedThread(t2));
317 t2.start();
318 waitForQueuedThread(lock, t2);
319 assertTrue(lock.hasQueuedThread(t1));
320 assertTrue(lock.hasQueuedThread(t2));
321 t1.interrupt();
322 awaitTermination(t1);
323 assertFalse(lock.hasQueuedThread(t1));
324 assertTrue(lock.hasQueuedThread(t2));
325 lock.unlock();
326 awaitTermination(t2);
327 assertFalse(lock.hasQueuedThread(t1));
328 assertFalse(lock.hasQueuedThread(t2));
329 }
330
331 /**
332 * getQueuedThreads includes waiting threads
333 */
334 public void testGetQueuedThreads() { testGetQueuedThreads(false); }
335 public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
336 public void testGetQueuedThreads(boolean fair) {
337 final PublicReentrantLock lock = new PublicReentrantLock(fair);
338 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
339 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
340 assertTrue(lock.getQueuedThreads().isEmpty());
341 lock.lock();
342 assertTrue(lock.getQueuedThreads().isEmpty());
343 t1.start();
344 waitForQueuedThread(lock, t1);
345 assertEquals(1, lock.getQueuedThreads().size());
346 assertTrue(lock.getQueuedThreads().contains(t1));
347 t2.start();
348 waitForQueuedThread(lock, t2);
349 assertEquals(2, lock.getQueuedThreads().size());
350 assertTrue(lock.getQueuedThreads().contains(t1));
351 assertTrue(lock.getQueuedThreads().contains(t2));
352 t1.interrupt();
353 awaitTermination(t1);
354 assertFalse(lock.getQueuedThreads().contains(t1));
355 assertTrue(lock.getQueuedThreads().contains(t2));
356 assertEquals(1, lock.getQueuedThreads().size());
357 lock.unlock();
358 awaitTermination(t2);
359 assertTrue(lock.getQueuedThreads().isEmpty());
360 }
361
362 /**
363 * timed tryLock is interruptible
364 */
365 public void testTryLock_Interruptible() { testTryLock_Interruptible(false); }
366 public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
367 public void testTryLock_Interruptible(boolean fair) {
368 final PublicReentrantLock lock = new PublicReentrantLock(fair);
369 lock.lock();
370 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
371 public void realRun() throws InterruptedException {
372 lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
373 }});
374
375 waitForQueuedThread(lock, t);
376 t.interrupt();
377 awaitTermination(t);
378 releaseLock(lock);
379 }
380
381 /**
382 * tryLock on a locked lock fails
383 */
384 public void testTryLockWhenLocked() { testTryLockWhenLocked(false); }
385 public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
386 public void testTryLockWhenLocked(boolean fair) {
387 final PublicReentrantLock lock = new PublicReentrantLock(fair);
388 lock.lock();
389 Thread t = newStartedThread(new CheckedRunnable() {
390 public void realRun() {
391 assertFalse(lock.tryLock());
392 }});
393
394 awaitTermination(t);
395 releaseLock(lock);
396 }
397
398 /**
399 * Timed tryLock on a locked lock times out
400 */
401 public void testTryLock_Timeout() { testTryLock_Timeout(false); }
402 public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
403 public void testTryLock_Timeout(boolean fair) {
404 final PublicReentrantLock lock = new PublicReentrantLock(fair);
405 lock.lock();
406 Thread t = newStartedThread(new CheckedRunnable() {
407 public void realRun() throws InterruptedException {
408 long startTime = System.nanoTime();
409 long timeoutMillis = 10;
410 assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
411 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
412 }});
413
414 awaitTermination(t);
415 releaseLock(lock);
416 }
417
418 /**
419 * getHoldCount returns number of recursive holds
420 */
421 public void testGetHoldCount() { testGetHoldCount(false); }
422 public void testGetHoldCount_fair() { testGetHoldCount(true); }
423 public void testGetHoldCount(boolean fair) {
424 ReentrantLock lock = new ReentrantLock(fair);
425 for (int i = 1; i <= SIZE; i++) {
426 lock.lock();
427 assertEquals(i, lock.getHoldCount());
428 }
429 for (int i = SIZE; i > 0; i--) {
430 lock.unlock();
431 assertEquals(i-1, lock.getHoldCount());
432 }
433 }
434
435 /**
436 * isLocked is true when locked and false when not
437 */
438 public void testIsLocked() { testIsLocked(false); }
439 public void testIsLocked_fair() { testIsLocked(true); }
440 public void testIsLocked(boolean fair) {
441 try {
442 final ReentrantLock lock = new ReentrantLock(fair);
443 assertFalse(lock.isLocked());
444 lock.lock();
445 assertTrue(lock.isLocked());
446 lock.lock();
447 assertTrue(lock.isLocked());
448 lock.unlock();
449 assertTrue(lock.isLocked());
450 lock.unlock();
451 assertFalse(lock.isLocked());
452 final CyclicBarrier barrier = new CyclicBarrier(2);
453 Thread t = newStartedThread(new CheckedRunnable() {
454 public void realRun() throws Exception {
455 lock.lock();
456 assertTrue(lock.isLocked());
457 barrier.await();
458 barrier.await();
459 lock.unlock();
460 }});
461
462 barrier.await();
463 assertTrue(lock.isLocked());
464 barrier.await();
465 awaitTermination(t);
466 assertFalse(lock.isLocked());
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100467 } catch (Exception fail) { threadUnexpectedException(fail); }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100468 }
469
470 /**
471 * lockInterruptibly succeeds when unlocked, else is interruptible
472 */
473 public void testLockInterruptibly() { testLockInterruptibly(false); }
474 public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
475 public void testLockInterruptibly(boolean fair) {
476 final PublicReentrantLock lock = new PublicReentrantLock(fair);
477 try {
478 lock.lockInterruptibly();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100479 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100480 assertLockedByMoi(lock);
481 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
482 waitForQueuedThread(lock, t);
483 t.interrupt();
484 assertTrue(lock.isLocked());
485 assertTrue(lock.isHeldByCurrentThread());
486 awaitTermination(t);
487 releaseLock(lock);
488 }
489
490 /**
491 * Calling await without holding lock throws IllegalMonitorStateException
492 */
493 public void testAwait_IMSE() { testAwait_IMSE(false); }
494 public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
495 public void testAwait_IMSE(boolean fair) {
496 final ReentrantLock lock = new ReentrantLock(fair);
497 final Condition c = lock.newCondition();
498 for (AwaitMethod awaitMethod : AwaitMethod.values()) {
499 long startTime = System.nanoTime();
500 try {
501 await(c, awaitMethod);
502 shouldThrow();
503 } catch (IllegalMonitorStateException success) {
504 } catch (InterruptedException e) { threadUnexpectedException(e); }
505 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
506 }
507 }
508
509 /**
510 * Calling signal without holding lock throws IllegalMonitorStateException
511 */
512 public void testSignal_IMSE() { testSignal_IMSE(false); }
513 public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
514 public void testSignal_IMSE(boolean fair) {
515 final ReentrantLock lock = new ReentrantLock(fair);
516 final Condition c = lock.newCondition();
517 try {
518 c.signal();
519 shouldThrow();
520 } catch (IllegalMonitorStateException success) {}
521 }
522
523 /**
524 * awaitNanos without a signal times out
525 */
526 public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); }
527 public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
528 public void testAwaitNanos_Timeout(boolean fair) {
529 try {
530 final ReentrantLock lock = new ReentrantLock(fair);
531 final Condition c = lock.newCondition();
532 lock.lock();
533 long startTime = System.nanoTime();
534 long timeoutMillis = 10;
535 long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
536 long nanosRemaining = c.awaitNanos(timeoutNanos);
537 assertTrue(nanosRemaining <= 0);
538 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
539 lock.unlock();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100540 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100541 }
542
543 /**
544 * timed await without a signal times out
545 */
546 public void testAwait_Timeout() { testAwait_Timeout(false); }
547 public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
548 public void testAwait_Timeout(boolean fair) {
549 try {
550 final ReentrantLock lock = new ReentrantLock(fair);
551 final Condition c = lock.newCondition();
552 lock.lock();
553 long startTime = System.nanoTime();
554 long timeoutMillis = 10;
555 assertFalse(c.await(timeoutMillis, MILLISECONDS));
556 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
557 lock.unlock();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100558 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100559 }
560
561 /**
562 * awaitUntil without a signal times out
563 */
564 public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
565 public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
566 public void testAwaitUntil_Timeout(boolean fair) {
567 try {
568 final ReentrantLock lock = new ReentrantLock(fair);
569 final Condition c = lock.newCondition();
570 lock.lock();
571 long startTime = System.nanoTime();
572 long timeoutMillis = 10;
573 java.util.Date d = new java.util.Date();
574 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
575 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
576 lock.unlock();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100577 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100578 }
579
580 /**
581 * await returns when signalled
582 */
583 public void testAwait() { testAwait(false); }
584 public void testAwait_fair() { testAwait(true); }
585 public void testAwait(boolean fair) {
586 final PublicReentrantLock lock = new PublicReentrantLock(fair);
587 final Condition c = lock.newCondition();
588 final CountDownLatch locked = new CountDownLatch(1);
589 Thread t = newStartedThread(new CheckedRunnable() {
590 public void realRun() throws InterruptedException {
591 lock.lock();
592 locked.countDown();
593 c.await();
594 lock.unlock();
595 }});
596
597 await(locked);
598 lock.lock();
599 assertHasWaiters(lock, c, t);
600 c.signal();
601 assertHasNoWaiters(lock, c);
602 assertTrue(t.isAlive());
603 lock.unlock();
604 awaitTermination(t);
605 }
606
607 /**
608 * hasWaiters throws NPE if null
609 */
610 public void testHasWaitersNPE() { testHasWaitersNPE(false); }
611 public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
612 public void testHasWaitersNPE(boolean fair) {
613 final ReentrantLock lock = new ReentrantLock(fair);
614 try {
615 lock.hasWaiters(null);
616 shouldThrow();
617 } catch (NullPointerException success) {}
618 }
619
620 /**
621 * getWaitQueueLength throws NPE if null
622 */
623 public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
624 public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
625 public void testGetWaitQueueLengthNPE(boolean fair) {
626 final ReentrantLock lock = new ReentrantLock(fair);
627 try {
628 lock.getWaitQueueLength(null);
629 shouldThrow();
630 } catch (NullPointerException success) {}
631 }
632
633 /**
634 * getWaitingThreads throws NPE if null
635 */
636 public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
637 public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
638 public void testGetWaitingThreadsNPE(boolean fair) {
639 final PublicReentrantLock lock = new PublicReentrantLock(fair);
640 try {
641 lock.getWaitingThreads(null);
642 shouldThrow();
643 } catch (NullPointerException success) {}
644 }
645
646 /**
647 * hasWaiters throws IllegalArgumentException if not owned
648 */
649 public void testHasWaitersIAE() { testHasWaitersIAE(false); }
650 public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
651 public void testHasWaitersIAE(boolean fair) {
652 final ReentrantLock lock = new ReentrantLock(fair);
653 final Condition c = lock.newCondition();
654 final ReentrantLock lock2 = new ReentrantLock(fair);
655 try {
656 lock2.hasWaiters(c);
657 shouldThrow();
658 } catch (IllegalArgumentException success) {}
659 }
660
661 /**
662 * hasWaiters throws IllegalMonitorStateException if not locked
663 */
664 public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
665 public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
666 public void testHasWaitersIMSE(boolean fair) {
667 final ReentrantLock lock = new ReentrantLock(fair);
668 final Condition c = lock.newCondition();
669 try {
670 lock.hasWaiters(c);
671 shouldThrow();
672 } catch (IllegalMonitorStateException success) {}
673 }
674
675 /**
676 * getWaitQueueLength throws IllegalArgumentException if not owned
677 */
678 public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
679 public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
680 public void testGetWaitQueueLengthIAE(boolean fair) {
681 final ReentrantLock lock = new ReentrantLock(fair);
682 final Condition c = lock.newCondition();
683 final ReentrantLock lock2 = new ReentrantLock(fair);
684 try {
685 lock2.getWaitQueueLength(c);
686 shouldThrow();
687 } catch (IllegalArgumentException success) {}
688 }
689
690 /**
691 * getWaitQueueLength throws IllegalMonitorStateException if not locked
692 */
693 public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
694 public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
695 public void testGetWaitQueueLengthIMSE(boolean fair) {
696 final ReentrantLock lock = new ReentrantLock(fair);
697 final Condition c = lock.newCondition();
698 try {
699 lock.getWaitQueueLength(c);
700 shouldThrow();
701 } catch (IllegalMonitorStateException success) {}
702 }
703
704 /**
705 * getWaitingThreads throws IllegalArgumentException if not owned
706 */
707 public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
708 public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
709 public void testGetWaitingThreadsIAE(boolean fair) {
710 final PublicReentrantLock lock = new PublicReentrantLock(fair);
711 final Condition c = lock.newCondition();
712 final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
713 try {
714 lock2.getWaitingThreads(c);
715 shouldThrow();
716 } catch (IllegalArgumentException success) {}
717 }
718
719 /**
720 * getWaitingThreads throws IllegalMonitorStateException if not locked
721 */
722 public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
723 public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
724 public void testGetWaitingThreadsIMSE(boolean fair) {
725 final PublicReentrantLock lock = new PublicReentrantLock(fair);
726 final Condition c = lock.newCondition();
727 try {
728 lock.getWaitingThreads(c);
729 shouldThrow();
730 } catch (IllegalMonitorStateException success) {}
731 }
732
733 /**
734 * hasWaiters returns true when a thread is waiting, else false
735 */
736 public void testHasWaiters() { testHasWaiters(false); }
737 public void testHasWaiters_fair() { testHasWaiters(true); }
738 public void testHasWaiters(boolean fair) {
739 final PublicReentrantLock lock = new PublicReentrantLock(fair);
740 final Condition c = lock.newCondition();
741 final CountDownLatch pleaseSignal = new CountDownLatch(1);
742 Thread t = newStartedThread(new CheckedRunnable() {
743 public void realRun() throws InterruptedException {
744 lock.lock();
745 assertHasNoWaiters(lock, c);
746 assertFalse(lock.hasWaiters(c));
747 pleaseSignal.countDown();
748 c.await();
749 assertHasNoWaiters(lock, c);
750 assertFalse(lock.hasWaiters(c));
751 lock.unlock();
752 }});
753
754 await(pleaseSignal);
755 lock.lock();
756 assertHasWaiters(lock, c, t);
757 assertTrue(lock.hasWaiters(c));
758 c.signal();
759 assertHasNoWaiters(lock, c);
760 assertFalse(lock.hasWaiters(c));
761 lock.unlock();
762 awaitTermination(t);
763 assertHasNoWaiters(lock, c);
764 }
765
766 /**
767 * getWaitQueueLength returns number of waiting threads
768 */
769 public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
770 public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
771 public void testGetWaitQueueLength(boolean fair) {
772 final PublicReentrantLock lock = new PublicReentrantLock(fair);
773 final Condition c = lock.newCondition();
774 final CountDownLatch locked1 = new CountDownLatch(1);
775 final CountDownLatch locked2 = new CountDownLatch(1);
776 Thread t1 = new Thread(new CheckedRunnable() {
777 public void realRun() throws InterruptedException {
778 lock.lock();
779 assertFalse(lock.hasWaiters(c));
780 assertEquals(0, lock.getWaitQueueLength(c));
781 locked1.countDown();
782 c.await();
783 lock.unlock();
784 }});
785
786 Thread t2 = new Thread(new CheckedRunnable() {
787 public void realRun() throws InterruptedException {
788 lock.lock();
789 assertTrue(lock.hasWaiters(c));
790 assertEquals(1, lock.getWaitQueueLength(c));
791 locked2.countDown();
792 c.await();
793 lock.unlock();
794 }});
795
796 lock.lock();
797 assertEquals(0, lock.getWaitQueueLength(c));
798 lock.unlock();
799
800 t1.start();
801 await(locked1);
802
803 lock.lock();
804 assertHasWaiters(lock, c, t1);
805 assertEquals(1, lock.getWaitQueueLength(c));
806 lock.unlock();
807
808 t2.start();
809 await(locked2);
810
811 lock.lock();
812 assertHasWaiters(lock, c, t1, t2);
813 assertEquals(2, lock.getWaitQueueLength(c));
814 c.signalAll();
815 assertHasNoWaiters(lock, c);
816 lock.unlock();
817
818 awaitTermination(t1);
819 awaitTermination(t2);
820
821 assertHasNoWaiters(lock, c);
822 }
823
824 /**
825 * getWaitingThreads returns only and all waiting threads
826 */
827 public void testGetWaitingThreads() { testGetWaitingThreads(false); }
828 public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
829 public void testGetWaitingThreads(boolean fair) {
830 final PublicReentrantLock lock = new PublicReentrantLock(fair);
831 final Condition c = lock.newCondition();
832 final CountDownLatch locked1 = new CountDownLatch(1);
833 final CountDownLatch locked2 = new CountDownLatch(1);
834 Thread t1 = new Thread(new CheckedRunnable() {
835 public void realRun() throws InterruptedException {
836 lock.lock();
837 assertTrue(lock.getWaitingThreads(c).isEmpty());
838 locked1.countDown();
839 c.await();
840 lock.unlock();
841 }});
842
843 Thread t2 = new Thread(new CheckedRunnable() {
844 public void realRun() throws InterruptedException {
845 lock.lock();
846 assertFalse(lock.getWaitingThreads(c).isEmpty());
847 locked2.countDown();
848 c.await();
849 lock.unlock();
850 }});
851
852 lock.lock();
853 assertTrue(lock.getWaitingThreads(c).isEmpty());
854 lock.unlock();
855
856 t1.start();
857 await(locked1);
858
859 lock.lock();
860 assertHasWaiters(lock, c, t1);
861 assertTrue(lock.getWaitingThreads(c).contains(t1));
862 assertFalse(lock.getWaitingThreads(c).contains(t2));
863 assertEquals(1, lock.getWaitingThreads(c).size());
864 lock.unlock();
865
866 t2.start();
867 await(locked2);
868
869 lock.lock();
870 assertHasWaiters(lock, c, t1, t2);
871 assertTrue(lock.getWaitingThreads(c).contains(t1));
872 assertTrue(lock.getWaitingThreads(c).contains(t2));
873 assertEquals(2, lock.getWaitingThreads(c).size());
874 c.signalAll();
875 assertHasNoWaiters(lock, c);
876 lock.unlock();
877
878 awaitTermination(t1);
879 awaitTermination(t2);
880
881 assertHasNoWaiters(lock, c);
882 }
883
884 /**
885 * awaitUninterruptibly is uninterruptible
886 */
887 public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
888 public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
889 public void testAwaitUninterruptibly(boolean fair) {
890 final ReentrantLock lock = new ReentrantLock(fair);
891 final Condition c = lock.newCondition();
892 final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
893
894 Thread t1 = newStartedThread(new CheckedRunnable() {
895 public void realRun() {
896 // Interrupt before awaitUninterruptibly
897 lock.lock();
898 pleaseInterrupt.countDown();
899 Thread.currentThread().interrupt();
900 c.awaitUninterruptibly();
901 assertTrue(Thread.interrupted());
902 lock.unlock();
903 }});
904
905 Thread t2 = newStartedThread(new CheckedRunnable() {
906 public void realRun() {
907 // Interrupt during awaitUninterruptibly
908 lock.lock();
909 pleaseInterrupt.countDown();
910 c.awaitUninterruptibly();
911 assertTrue(Thread.interrupted());
912 lock.unlock();
913 }});
914
915 await(pleaseInterrupt);
916 lock.lock();
917 lock.unlock();
918 t2.interrupt();
919
920 assertThreadStaysAlive(t1);
921 assertTrue(t2.isAlive());
922
923 lock.lock();
924 c.signalAll();
925 lock.unlock();
926
927 awaitTermination(t1);
928 awaitTermination(t2);
929 }
930
931 /**
932 * await/awaitNanos/awaitUntil is interruptible
933 */
934 public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
935 public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
936 public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
937 public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
938 public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
939 public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
940 public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
941 public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
942 public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
943 final PublicReentrantLock lock =
944 new PublicReentrantLock(fair);
945 final Condition c = lock.newCondition();
946 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
947 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
948 public void realRun() throws InterruptedException {
949 lock.lock();
950 assertLockedByMoi(lock);
951 assertHasNoWaiters(lock, c);
952 pleaseInterrupt.countDown();
953 try {
954 await(c, awaitMethod);
955 } finally {
956 assertLockedByMoi(lock);
957 assertHasNoWaiters(lock, c);
958 lock.unlock();
959 assertFalse(Thread.interrupted());
960 }
961 }});
962
963 await(pleaseInterrupt);
964 assertHasWaiters(lock, c, t);
965 t.interrupt();
966 awaitTermination(t);
967 assertNotLocked(lock);
968 }
969
970 /**
971 * signalAll wakes up all threads
972 */
973 public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
974 public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
975 public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
976 public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
977 public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
978 public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
979 public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
980 public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
981 public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
982 final PublicReentrantLock lock = new PublicReentrantLock(fair);
983 final Condition c = lock.newCondition();
984 final CountDownLatch pleaseSignal = new CountDownLatch(2);
985 class Awaiter extends CheckedRunnable {
986 public void realRun() throws InterruptedException {
987 lock.lock();
988 pleaseSignal.countDown();
989 await(c, awaitMethod);
990 lock.unlock();
991 }
992 }
993
994 Thread t1 = newStartedThread(new Awaiter());
995 Thread t2 = newStartedThread(new Awaiter());
996
997 await(pleaseSignal);
998 lock.lock();
999 assertHasWaiters(lock, c, t1, t2);
1000 c.signalAll();
1001 assertHasNoWaiters(lock, c);
1002 lock.unlock();
1003 awaitTermination(t1);
1004 awaitTermination(t2);
1005 }
1006
1007 /**
1008 * signal wakes up waiting threads in FIFO order
1009 */
1010 public void testSignalWakesFifo() { testSignalWakesFifo(false); }
1011 public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1012 public void testSignalWakesFifo(boolean fair) {
1013 final PublicReentrantLock lock =
1014 new PublicReentrantLock(fair);
1015 final Condition c = lock.newCondition();
1016 final CountDownLatch locked1 = new CountDownLatch(1);
1017 final CountDownLatch locked2 = new CountDownLatch(1);
1018 Thread t1 = newStartedThread(new CheckedRunnable() {
1019 public void realRun() throws InterruptedException {
1020 lock.lock();
1021 locked1.countDown();
1022 c.await();
1023 lock.unlock();
1024 }});
1025
1026 await(locked1);
1027
1028 Thread t2 = newStartedThread(new CheckedRunnable() {
1029 public void realRun() throws InterruptedException {
1030 lock.lock();
1031 locked2.countDown();
1032 c.await();
1033 lock.unlock();
1034 }});
1035
1036 await(locked2);
1037
1038 lock.lock();
1039 assertHasWaiters(lock, c, t1, t2);
1040 assertFalse(lock.hasQueuedThreads());
1041 c.signal();
1042 assertHasWaiters(lock, c, t2);
1043 assertTrue(lock.hasQueuedThread(t1));
1044 assertFalse(lock.hasQueuedThread(t2));
1045 c.signal();
1046 assertHasNoWaiters(lock, c);
1047 assertTrue(lock.hasQueuedThread(t1));
1048 assertTrue(lock.hasQueuedThread(t2));
1049 lock.unlock();
1050 awaitTermination(t1);
1051 awaitTermination(t2);
1052 }
1053
1054 /**
1055 * await after multiple reentrant locking preserves lock count
1056 */
1057 public void testAwaitLockCount() { testAwaitLockCount(false); }
1058 public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1059 public void testAwaitLockCount(boolean fair) {
1060 final PublicReentrantLock lock = new PublicReentrantLock(fair);
1061 final Condition c = lock.newCondition();
1062 final CountDownLatch pleaseSignal = new CountDownLatch(2);
1063 Thread t1 = newStartedThread(new CheckedRunnable() {
1064 public void realRun() throws InterruptedException {
1065 lock.lock();
1066 assertLockedByMoi(lock);
1067 assertEquals(1, lock.getHoldCount());
1068 pleaseSignal.countDown();
1069 c.await();
1070 assertLockedByMoi(lock);
1071 assertEquals(1, lock.getHoldCount());
1072 lock.unlock();
1073 }});
1074
1075 Thread t2 = newStartedThread(new CheckedRunnable() {
1076 public void realRun() throws InterruptedException {
1077 lock.lock();
1078 lock.lock();
1079 assertLockedByMoi(lock);
1080 assertEquals(2, lock.getHoldCount());
1081 pleaseSignal.countDown();
1082 c.await();
1083 assertLockedByMoi(lock);
1084 assertEquals(2, lock.getHoldCount());
1085 lock.unlock();
1086 lock.unlock();
1087 }});
1088
1089 await(pleaseSignal);
1090 lock.lock();
1091 assertHasWaiters(lock, c, t1, t2);
1092 assertEquals(1, lock.getHoldCount());
1093 c.signalAll();
1094 assertHasNoWaiters(lock, c);
1095 lock.unlock();
1096 awaitTermination(t1);
1097 awaitTermination(t2);
1098 }
1099
1100 /**
1101 * A serialized lock deserializes as unlocked
1102 */
1103 public void testSerialization() { testSerialization(false); }
1104 public void testSerialization_fair() { testSerialization(true); }
1105 public void testSerialization(boolean fair) {
1106 ReentrantLock lock = new ReentrantLock(fair);
1107 lock.lock();
1108
1109 ReentrantLock clone = serialClone(lock);
1110 assertEquals(lock.isFair(), clone.isFair());
1111 assertTrue(lock.isLocked());
1112 assertFalse(clone.isLocked());
1113 assertEquals(1, lock.getHoldCount());
1114 assertEquals(0, clone.getHoldCount());
1115 clone.lock();
1116 clone.lock();
1117 assertTrue(clone.isLocked());
1118 assertEquals(2, clone.getHoldCount());
1119 assertEquals(1, lock.getHoldCount());
1120 clone.unlock();
1121 clone.unlock();
1122 assertTrue(lock.isLocked());
1123 assertFalse(clone.isLocked());
1124 }
1125
1126 /**
1127 * toString indicates current lock state
1128 */
1129 public void testToString() { testToString(false); }
1130 public void testToString_fair() { testToString(true); }
1131 public void testToString(boolean fair) {
1132 ReentrantLock lock = new ReentrantLock(fair);
1133 assertTrue(lock.toString().contains("Unlocked"));
1134 lock.lock();
1135 assertTrue(lock.toString().contains("Locked"));
1136 lock.unlock();
1137 assertTrue(lock.toString().contains("Unlocked"));
1138 }
1139}