107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* trylock on an unlocked lock succeeds |
110 |
> |
* tryLock on an unlocked lock succeeds |
111 |
|
*/ |
112 |
|
public void testTryLock() { |
113 |
|
ReentrantLock rl = new ReentrantLock(); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
+ |
* getQueueLength reports number of waiting threads |
180 |
+ |
*/ |
181 |
+ |
public void testGetQueueLength_fair() { |
182 |
+ |
final ReentrantLock lock = new ReentrantLock(true); |
183 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
184 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
185 |
+ |
try { |
186 |
+ |
assertEquals(0, lock.getQueueLength()); |
187 |
+ |
lock.lock(); |
188 |
+ |
t1.start(); |
189 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
190 |
+ |
assertEquals(1, lock.getQueueLength()); |
191 |
+ |
t2.start(); |
192 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
193 |
+ |
assertEquals(2, lock.getQueueLength()); |
194 |
+ |
t1.interrupt(); |
195 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
196 |
+ |
assertEquals(1, lock.getQueueLength()); |
197 |
+ |
lock.unlock(); |
198 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
199 |
+ |
assertEquals(0, lock.getQueueLength()); |
200 |
+ |
t1.join(); |
201 |
+ |
t2.join(); |
202 |
+ |
} catch(Exception e){ |
203 |
+ |
unexpectedException(); |
204 |
+ |
} |
205 |
+ |
} |
206 |
+ |
|
207 |
+ |
/** |
208 |
|
* getQueuedThreads includes waiting threads |
209 |
|
*/ |
210 |
|
public void testGetQueuedThreads() { |
238 |
|
|
239 |
|
|
240 |
|
/** |
241 |
< |
* timed trylock is interruptible. |
241 |
> |
* timed tryLock is interruptible. |
242 |
|
*/ |
243 |
|
public void testInterruptedException2() { |
244 |
|
final ReentrantLock lock = new ReentrantLock(); |
261 |
|
|
262 |
|
|
263 |
|
/** |
264 |
< |
* Trylock on a locked lock fails |
264 |
> |
* TryLock on a locked lock fails |
265 |
|
*/ |
266 |
|
public void testTryLockWhenLocked() { |
267 |
|
final ReentrantLock lock = new ReentrantLock(); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* Timed trylock on a locked lock times out |
284 |
> |
* Timed tryLock on a locked lock times out |
285 |
|
*/ |
286 |
|
public void testTryLock_Timeout() { |
287 |
|
final ReentrantLock lock = new ReentrantLock(); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
+ |
* hasWaiters throws NPE if null |
514 |
+ |
*/ |
515 |
+ |
public void testHasWaitersNPE() { |
516 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
517 |
+ |
try { |
518 |
+ |
lock.hasWaiters(null); |
519 |
+ |
shouldThrow(); |
520 |
+ |
} catch (NullPointerException success) { |
521 |
+ |
} catch (Exception ex) { |
522 |
+ |
unexpectedException(); |
523 |
+ |
} |
524 |
+ |
} |
525 |
+ |
|
526 |
+ |
/** |
527 |
+ |
* getWaitQueueLength throws NPE if null |
528 |
+ |
*/ |
529 |
+ |
public void testGetWaitQueueLengthNPE() { |
530 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
531 |
+ |
try { |
532 |
+ |
lock.getWaitQueueLength(null); |
533 |
+ |
shouldThrow(); |
534 |
+ |
} catch (NullPointerException success) { |
535 |
+ |
} catch (Exception ex) { |
536 |
+ |
unexpectedException(); |
537 |
+ |
} |
538 |
+ |
} |
539 |
+ |
|
540 |
+ |
|
541 |
+ |
/** |
542 |
+ |
* getWaitingThreads throws NPE if null |
543 |
+ |
*/ |
544 |
+ |
public void testGetWaitingThreadsNPE() { |
545 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
546 |
+ |
try { |
547 |
+ |
lock.getWaitingThreads(null); |
548 |
+ |
shouldThrow(); |
549 |
+ |
} catch (NullPointerException success) { |
550 |
+ |
} catch (Exception ex) { |
551 |
+ |
unexpectedException(); |
552 |
+ |
} |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
|
556 |
+ |
/** |
557 |
|
* hasWaiters throws IAE if not owned |
558 |
|
*/ |
559 |
|
public void testHasWaitersIAE() { |
889 |
|
public void run() { |
890 |
|
try { |
891 |
|
lock.lock(); |
892 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
892 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
893 |
|
lock.unlock(); |
894 |
|
threadShouldThrow(); |
895 |
|
} |
1016 |
|
} |
1017 |
|
} |
1018 |
|
|
1019 |
+ |
/** |
1020 |
+ |
* toString indicates current lock state |
1021 |
+ |
*/ |
1022 |
+ |
public void testToString() { |
1023 |
+ |
ReentrantLock lock = new ReentrantLock(); |
1024 |
+ |
String us = lock.toString(); |
1025 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1026 |
+ |
lock.lock(); |
1027 |
+ |
String ls = lock.toString(); |
1028 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1029 |
+ |
} |
1030 |
+ |
|
1031 |
|
} |