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 |
+ |
* hasQueuedThread(null) throws NPE |
209 |
+ |
*/ |
210 |
+ |
public void testHasQueuedThreadNPE() { |
211 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
212 |
+ |
try { |
213 |
+ |
sync.hasQueuedThread(null); |
214 |
+ |
shouldThrow(); |
215 |
+ |
} catch (NullPointerException success) { |
216 |
+ |
} |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
/** |
220 |
+ |
* hasQueuedThread reports whether a thread is queued. |
221 |
+ |
*/ |
222 |
+ |
public void testHasQueuedThread() { |
223 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
224 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
225 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
226 |
+ |
try { |
227 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
228 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
229 |
+ |
sync.lock(); |
230 |
+ |
t1.start(); |
231 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
232 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
233 |
+ |
t2.start(); |
234 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
235 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
236 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
237 |
+ |
t1.interrupt(); |
238 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
239 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
240 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
241 |
+ |
sync.unlock(); |
242 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
243 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
244 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
245 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
246 |
+ |
t1.join(); |
247 |
+ |
t2.join(); |
248 |
+ |
} catch(Exception e){ |
249 |
+ |
unexpectedException(); |
250 |
+ |
} |
251 |
+ |
} |
252 |
+ |
|
253 |
+ |
|
254 |
+ |
/** |
255 |
|
* getQueuedThreads includes waiting threads |
256 |
|
*/ |
257 |
|
public void testGetQueuedThreads() { |
285 |
|
|
286 |
|
|
287 |
|
/** |
288 |
< |
* timed trylock is interruptible. |
288 |
> |
* timed tryLock is interruptible. |
289 |
|
*/ |
290 |
|
public void testInterruptedException2() { |
291 |
|
final ReentrantLock lock = new ReentrantLock(); |
308 |
|
|
309 |
|
|
310 |
|
/** |
311 |
< |
* Trylock on a locked lock fails |
311 |
> |
* TryLock on a locked lock fails |
312 |
|
*/ |
313 |
|
public void testTryLockWhenLocked() { |
314 |
|
final ReentrantLock lock = new ReentrantLock(); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Timed trylock on a locked lock times out |
331 |
> |
* Timed tryLock on a locked lock times out |
332 |
|
*/ |
333 |
|
public void testTryLock_Timeout() { |
334 |
|
final ReentrantLock lock = new ReentrantLock(); |
498 |
|
final Condition c = lock.newCondition(); |
499 |
|
try { |
500 |
|
lock.lock(); |
501 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
501 |
> |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
502 |
|
lock.unlock(); |
503 |
|
} |
504 |
|
catch (Exception ex) { |
515 |
|
try { |
516 |
|
lock.lock(); |
517 |
|
java.util.Date d = new java.util.Date(); |
518 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
518 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10)); |
519 |
|
lock.unlock(); |
520 |
|
} |
521 |
|
catch (Exception ex) { |
557 |
|
} |
558 |
|
|
559 |
|
/** |
560 |
+ |
* hasWaiters throws NPE if null |
561 |
+ |
*/ |
562 |
+ |
public void testHasWaitersNPE() { |
563 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
564 |
+ |
try { |
565 |
+ |
lock.hasWaiters(null); |
566 |
+ |
shouldThrow(); |
567 |
+ |
} catch (NullPointerException success) { |
568 |
+ |
} catch (Exception ex) { |
569 |
+ |
unexpectedException(); |
570 |
+ |
} |
571 |
+ |
} |
572 |
+ |
|
573 |
+ |
/** |
574 |
+ |
* getWaitQueueLength throws NPE if null |
575 |
+ |
*/ |
576 |
+ |
public void testGetWaitQueueLengthNPE() { |
577 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
578 |
+ |
try { |
579 |
+ |
lock.getWaitQueueLength(null); |
580 |
+ |
shouldThrow(); |
581 |
+ |
} catch (NullPointerException success) { |
582 |
+ |
} catch (Exception ex) { |
583 |
+ |
unexpectedException(); |
584 |
+ |
} |
585 |
+ |
} |
586 |
+ |
|
587 |
+ |
|
588 |
+ |
/** |
589 |
+ |
* getWaitingThreads throws NPE if null |
590 |
+ |
*/ |
591 |
+ |
public void testGetWaitingThreadsNPE() { |
592 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
593 |
+ |
try { |
594 |
+ |
lock.getWaitingThreads(null); |
595 |
+ |
shouldThrow(); |
596 |
+ |
} catch (NullPointerException success) { |
597 |
+ |
} catch (Exception ex) { |
598 |
+ |
unexpectedException(); |
599 |
+ |
} |
600 |
+ |
} |
601 |
+ |
|
602 |
+ |
|
603 |
+ |
/** |
604 |
|
* hasWaiters throws IAE if not owned |
605 |
|
*/ |
606 |
|
public void testHasWaitersIAE() { |
886 |
|
lock.lock(); |
887 |
|
c.signal(); |
888 |
|
lock.unlock(); |
889 |
< |
assert(t.isInterrupted()); |
889 |
> |
assertTrue(t.isInterrupted()); |
890 |
|
t.join(SHORT_DELAY_MS); |
891 |
|
assertFalse(t.isAlive()); |
892 |
|
} |
936 |
|
public void run() { |
937 |
|
try { |
938 |
|
lock.lock(); |
939 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
939 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
940 |
|
lock.unlock(); |
941 |
|
threadShouldThrow(); |
942 |
|
} |
1063 |
|
} |
1064 |
|
} |
1065 |
|
|
1066 |
+ |
/** |
1067 |
+ |
* toString indicates current lock state |
1068 |
+ |
*/ |
1069 |
+ |
public void testToString() { |
1070 |
+ |
ReentrantLock lock = new ReentrantLock(); |
1071 |
+ |
String us = lock.toString(); |
1072 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1073 |
+ |
lock.lock(); |
1074 |
+ |
String ls = lock.toString(); |
1075 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1076 |
+ |
} |
1077 |
+ |
|
1078 |
|
} |