ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.18
Committed: Sat Jan 10 01:41:59 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.17: +12 -0 lines
Log Message:
Test toString

File Contents

# Content
1 /*
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/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.concurrent.locks.*;
11 import java.util.concurrent.*;
12 import java.util.*;
13 import java.io.*;
14
15 public class ReentrantLockTest extends JSR166TestCase {
16 public static void main(String[] args) {
17 junit.textui.TestRunner.run (suite());
18 }
19 public static Test suite() {
20 return new TestSuite(ReentrantLockTest.class);
21 }
22
23 /**
24 * A runnable calling lockInterruptibly
25 */
26 class InterruptibleLockRunnable implements Runnable {
27 final ReentrantLock lock;
28 InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
29 public void run() {
30 try {
31 lock.lockInterruptibly();
32 } catch(InterruptedException success){}
33 }
34 }
35
36
37 /**
38 * A runnable calling lockInterruptibly that expects to be
39 * interrupted
40 */
41 class InterruptedLockRunnable implements Runnable {
42 final ReentrantLock lock;
43 InterruptedLockRunnable(ReentrantLock l) { lock = l; }
44 public void run() {
45 try {
46 lock.lockInterruptibly();
47 threadShouldThrow();
48 } catch(InterruptedException success){}
49 }
50 }
51
52 /**
53 * Subclass to expose protected methods
54 */
55 static class PublicReentrantLock extends ReentrantLock {
56 PublicReentrantLock() { super(); }
57 public Collection<Thread> getQueuedThreads() {
58 return super.getQueuedThreads();
59 }
60 public Collection<Thread> getWaitingThreads(Condition c) {
61 return super.getWaitingThreads(c);
62 }
63
64
65 }
66
67 /**
68 * Constructor sets given fairness
69 */
70 public void testConstructor() {
71 ReentrantLock rl = new ReentrantLock();
72 assertFalse(rl.isFair());
73 ReentrantLock r2 = new ReentrantLock(true);
74 assertTrue(r2.isFair());
75 }
76
77 /**
78 * locking an unlocked lock succeeds
79 */
80 public void testLock() {
81 ReentrantLock rl = new ReentrantLock();
82 rl.lock();
83 assertTrue(rl.isLocked());
84 rl.unlock();
85 }
86
87 /**
88 * locking an unlocked fair lock succeeds
89 */
90 public void testFairLock() {
91 ReentrantLock rl = new ReentrantLock(true);
92 rl.lock();
93 assertTrue(rl.isLocked());
94 rl.unlock();
95 }
96
97 /**
98 * Unlocking an unlocked lock throws IllegalMonitorStateException
99 */
100 public void testUnlock_IllegalMonitorStateException() {
101 ReentrantLock rl = new ReentrantLock();
102 try {
103 rl.unlock();
104 shouldThrow();
105
106 } catch(IllegalMonitorStateException success){}
107 }
108
109 /**
110 * tryLock on an unlocked lock succeeds
111 */
112 public void testTryLock() {
113 ReentrantLock rl = new ReentrantLock();
114 assertTrue(rl.tryLock());
115 assertTrue(rl.isLocked());
116 rl.unlock();
117 }
118
119
120 /**
121 * hasQueuedThreads reports whether there are waiting threads
122 */
123 public void testhasQueuedThreads() {
124 final ReentrantLock lock = new ReentrantLock();
125 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
126 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
127 try {
128 assertFalse(lock.hasQueuedThreads());
129 lock.lock();
130 t1.start();
131 Thread.sleep(SHORT_DELAY_MS);
132 assertTrue(lock.hasQueuedThreads());
133 t2.start();
134 Thread.sleep(SHORT_DELAY_MS);
135 assertTrue(lock.hasQueuedThreads());
136 t1.interrupt();
137 Thread.sleep(SHORT_DELAY_MS);
138 assertTrue(lock.hasQueuedThreads());
139 lock.unlock();
140 Thread.sleep(SHORT_DELAY_MS);
141 assertFalse(lock.hasQueuedThreads());
142 t1.join();
143 t2.join();
144 } catch(Exception e){
145 unexpectedException();
146 }
147 }
148
149 /**
150 * getQueueLength reports number of waiting threads
151 */
152 public void testGetQueueLength() {
153 final ReentrantLock lock = new ReentrantLock();
154 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
155 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
156 try {
157 assertEquals(0, lock.getQueueLength());
158 lock.lock();
159 t1.start();
160 Thread.sleep(SHORT_DELAY_MS);
161 assertEquals(1, lock.getQueueLength());
162 t2.start();
163 Thread.sleep(SHORT_DELAY_MS);
164 assertEquals(2, lock.getQueueLength());
165 t1.interrupt();
166 Thread.sleep(SHORT_DELAY_MS);
167 assertEquals(1, lock.getQueueLength());
168 lock.unlock();
169 Thread.sleep(SHORT_DELAY_MS);
170 assertEquals(0, lock.getQueueLength());
171 t1.join();
172 t2.join();
173 } catch(Exception e){
174 unexpectedException();
175 }
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() {
211 final PublicReentrantLock lock = new PublicReentrantLock();
212 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
213 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
214 try {
215 assertTrue(lock.getQueuedThreads().isEmpty());
216 lock.lock();
217 assertTrue(lock.getQueuedThreads().isEmpty());
218 t1.start();
219 Thread.sleep(SHORT_DELAY_MS);
220 assertTrue(lock.getQueuedThreads().contains(t1));
221 t2.start();
222 Thread.sleep(SHORT_DELAY_MS);
223 assertTrue(lock.getQueuedThreads().contains(t1));
224 assertTrue(lock.getQueuedThreads().contains(t2));
225 t1.interrupt();
226 Thread.sleep(SHORT_DELAY_MS);
227 assertFalse(lock.getQueuedThreads().contains(t1));
228 assertTrue(lock.getQueuedThreads().contains(t2));
229 lock.unlock();
230 Thread.sleep(SHORT_DELAY_MS);
231 assertTrue(lock.getQueuedThreads().isEmpty());
232 t1.join();
233 t2.join();
234 } catch(Exception e){
235 unexpectedException();
236 }
237 }
238
239
240 /**
241 * timed tryLock is interruptible.
242 */
243 public void testInterruptedException2() {
244 final ReentrantLock lock = new ReentrantLock();
245 lock.lock();
246 Thread t = new Thread(new Runnable() {
247 public void run() {
248 try {
249 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
250 threadShouldThrow();
251 } catch(InterruptedException success){}
252 }
253 });
254 try {
255 t.start();
256 t.interrupt();
257 } catch(Exception e){
258 unexpectedException();
259 }
260 }
261
262
263 /**
264 * TryLock on a locked lock fails
265 */
266 public void testTryLockWhenLocked() {
267 final ReentrantLock lock = new ReentrantLock();
268 lock.lock();
269 Thread t = new Thread(new Runnable() {
270 public void run() {
271 threadAssertFalse(lock.tryLock());
272 }
273 });
274 try {
275 t.start();
276 t.join();
277 lock.unlock();
278 } catch(Exception e){
279 unexpectedException();
280 }
281 }
282
283 /**
284 * Timed tryLock on a locked lock times out
285 */
286 public void testTryLock_Timeout() {
287 final ReentrantLock lock = new ReentrantLock();
288 lock.lock();
289 Thread t = new Thread(new Runnable() {
290 public void run() {
291 try {
292 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
293 } catch (Exception ex) {
294 threadUnexpectedException();
295 }
296 }
297 });
298 try {
299 t.start();
300 t.join();
301 lock.unlock();
302 } catch(Exception e){
303 unexpectedException();
304 }
305 }
306
307 /**
308 * getHoldCount returns number of recursive holds
309 */
310 public void testGetHoldCount() {
311 ReentrantLock lock = new ReentrantLock();
312 for(int i = 1; i <= SIZE; i++) {
313 lock.lock();
314 assertEquals(i,lock.getHoldCount());
315 }
316 for(int i = SIZE; i > 0; i--) {
317 lock.unlock();
318 assertEquals(i-1,lock.getHoldCount());
319 }
320 }
321
322
323 /**
324 * isLocked is true when locked and false when not
325 */
326 public void testIsLocked() {
327 final ReentrantLock lock = new ReentrantLock();
328 lock.lock();
329 assertTrue(lock.isLocked());
330 lock.unlock();
331 assertFalse(lock.isLocked());
332 Thread t = new Thread(new Runnable() {
333 public void run() {
334 lock.lock();
335 try {
336 Thread.sleep(SMALL_DELAY_MS);
337 }
338 catch(Exception e) {
339 threadUnexpectedException();
340 }
341 lock.unlock();
342 }
343 });
344 try {
345 t.start();
346 Thread.sleep(SHORT_DELAY_MS);
347 assertTrue(lock.isLocked());
348 t.join();
349 assertFalse(lock.isLocked());
350 } catch(Exception e){
351 unexpectedException();
352 }
353 }
354
355
356 /**
357 * lockInterruptibly is interruptible.
358 */
359 public void testLockInterruptibly1() {
360 final ReentrantLock lock = new ReentrantLock();
361 lock.lock();
362 Thread t = new Thread(new InterruptedLockRunnable(lock));
363 try {
364 t.start();
365 t.interrupt();
366 lock.unlock();
367 t.join();
368 } catch(Exception e){
369 unexpectedException();
370 }
371 }
372
373 /**
374 * lockInterruptibly succeeds when unlocked, else is interruptible
375 */
376 public void testLockInterruptibly2() {
377 final ReentrantLock lock = new ReentrantLock();
378 try {
379 lock.lockInterruptibly();
380 } catch(Exception e) {
381 unexpectedException();
382 }
383 Thread t = new Thread(new InterruptedLockRunnable(lock));
384 try {
385 t.start();
386 t.interrupt();
387 assertTrue(lock.isLocked());
388 assertTrue(lock.isHeldByCurrentThread());
389 t.join();
390 } catch(Exception e){
391 unexpectedException();
392 }
393 }
394
395 /**
396 * Calling await without holding lock throws IllegalMonitorStateException
397 */
398 public void testAwait_IllegalMonitor() {
399 final ReentrantLock lock = new ReentrantLock();
400 final Condition c = lock.newCondition();
401 try {
402 c.await();
403 shouldThrow();
404 }
405 catch (IllegalMonitorStateException success) {
406 }
407 catch (Exception ex) {
408 unexpectedException();
409 }
410 }
411
412 /**
413 * Calling signal without holding lock throws IllegalMonitorStateException
414 */
415 public void testSignal_IllegalMonitor() {
416 final ReentrantLock lock = new ReentrantLock();
417 final Condition c = lock.newCondition();
418 try {
419 c.signal();
420 shouldThrow();
421 }
422 catch (IllegalMonitorStateException success) {
423 }
424 catch (Exception ex) {
425 unexpectedException();
426 }
427 }
428
429 /**
430 * awaitNanos without a signal times out
431 */
432 public void testAwaitNanos_Timeout() {
433 final ReentrantLock lock = new ReentrantLock();
434 final Condition c = lock.newCondition();
435 try {
436 lock.lock();
437 long t = c.awaitNanos(100);
438 assertTrue(t <= 0);
439 lock.unlock();
440 }
441 catch (Exception ex) {
442 unexpectedException();
443 }
444 }
445
446 /**
447 * timed await without a signal times out
448 */
449 public void testAwait_Timeout() {
450 final ReentrantLock lock = new ReentrantLock();
451 final Condition c = lock.newCondition();
452 try {
453 lock.lock();
454 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
455 lock.unlock();
456 }
457 catch (Exception ex) {
458 unexpectedException();
459 }
460 }
461
462 /**
463 * awaitUntil without a signal times out
464 */
465 public void testAwaitUntil_Timeout() {
466 final ReentrantLock lock = new ReentrantLock();
467 final Condition c = lock.newCondition();
468 try {
469 lock.lock();
470 java.util.Date d = new java.util.Date();
471 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
472 lock.unlock();
473 }
474 catch (Exception ex) {
475 unexpectedException();
476 }
477 }
478
479 /**
480 * await returns when signalled
481 */
482 public void testAwait() {
483 final ReentrantLock lock = new ReentrantLock();
484 final Condition c = lock.newCondition();
485 Thread t = new Thread(new Runnable() {
486 public void run() {
487 try {
488 lock.lock();
489 c.await();
490 lock.unlock();
491 }
492 catch(InterruptedException e) {
493 threadUnexpectedException();
494 }
495 }
496 });
497
498 try {
499 t.start();
500 Thread.sleep(SHORT_DELAY_MS);
501 lock.lock();
502 c.signal();
503 lock.unlock();
504 t.join(SHORT_DELAY_MS);
505 assertFalse(t.isAlive());
506 }
507 catch (Exception ex) {
508 unexpectedException();
509 }
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() {
560 final ReentrantLock lock = new ReentrantLock();
561 final Condition c = (lock.newCondition());
562 final ReentrantLock lock2 = new ReentrantLock();
563 try {
564 lock2.hasWaiters(c);
565 shouldThrow();
566 } catch (IllegalArgumentException success) {
567 } catch (Exception ex) {
568 unexpectedException();
569 }
570 }
571
572 /**
573 * hasWaiters throws IMSE if not locked
574 */
575 public void testHasWaitersIMSE() {
576 final ReentrantLock lock = new ReentrantLock();
577 final Condition c = (lock.newCondition());
578 try {
579 lock.hasWaiters(c);
580 shouldThrow();
581 } catch (IllegalMonitorStateException success) {
582 } catch (Exception ex) {
583 unexpectedException();
584 }
585 }
586
587
588 /**
589 * getWaitQueueLength throws IAE if not owned
590 */
591 public void testGetWaitQueueLengthIAE() {
592 final ReentrantLock lock = new ReentrantLock();
593 final Condition c = (lock.newCondition());
594 final ReentrantLock lock2 = new ReentrantLock();
595 try {
596 lock2.getWaitQueueLength(c);
597 shouldThrow();
598 } catch (IllegalArgumentException success) {
599 } catch (Exception ex) {
600 unexpectedException();
601 }
602 }
603
604 /**
605 * getWaitQueueLength throws IMSE if not locked
606 */
607 public void testGetWaitQueueLengthIMSE() {
608 final ReentrantLock lock = new ReentrantLock();
609 final Condition c = (lock.newCondition());
610 try {
611 lock.getWaitQueueLength(c);
612 shouldThrow();
613 } catch (IllegalMonitorStateException success) {
614 } catch (Exception ex) {
615 unexpectedException();
616 }
617 }
618
619
620 /**
621 * getWaitingThreads throws IAE if not owned
622 */
623 public void testGetWaitingThreadsIAE() {
624 final PublicReentrantLock lock = new PublicReentrantLock();
625 final Condition c = (lock.newCondition());
626 final PublicReentrantLock lock2 = new PublicReentrantLock();
627 try {
628 lock2.getWaitingThreads(c);
629 shouldThrow();
630 } catch (IllegalArgumentException success) {
631 } catch (Exception ex) {
632 unexpectedException();
633 }
634 }
635
636 /**
637 * getWaitingThreads throws IMSE if not locked
638 */
639 public void testGetWaitingThreadsIMSE() {
640 final PublicReentrantLock lock = new PublicReentrantLock();
641 final Condition c = (lock.newCondition());
642 try {
643 lock.getWaitingThreads(c);
644 shouldThrow();
645 } catch (IllegalMonitorStateException success) {
646 } catch (Exception ex) {
647 unexpectedException();
648 }
649 }
650
651
652
653 /**
654 * hasWaiters returns true when a thread is waiting, else false
655 */
656 public void testHasWaiters() {
657 final ReentrantLock lock = new ReentrantLock();
658 final Condition c = lock.newCondition();
659 Thread t = new Thread(new Runnable() {
660 public void run() {
661 try {
662 lock.lock();
663 threadAssertFalse(lock.hasWaiters(c));
664 threadAssertEquals(0, lock.getWaitQueueLength(c));
665 c.await();
666 lock.unlock();
667 }
668 catch(InterruptedException e) {
669 threadUnexpectedException();
670 }
671 }
672 });
673
674 try {
675 t.start();
676 Thread.sleep(SHORT_DELAY_MS);
677 lock.lock();
678 assertTrue(lock.hasWaiters(c));
679 assertEquals(1, lock.getWaitQueueLength(c));
680 c.signal();
681 lock.unlock();
682 Thread.sleep(SHORT_DELAY_MS);
683 lock.lock();
684 assertFalse(lock.hasWaiters(c));
685 assertEquals(0, lock.getWaitQueueLength(c));
686 lock.unlock();
687 t.join(SHORT_DELAY_MS);
688 assertFalse(t.isAlive());
689 }
690 catch (Exception ex) {
691 unexpectedException();
692 }
693 }
694
695 /**
696 * getWaitQueueLength returns number of waiting threads
697 */
698 public void testGetWaitQueueLength() {
699 final ReentrantLock lock = new ReentrantLock();
700 final Condition c = lock.newCondition();
701 Thread t1 = new Thread(new Runnable() {
702 public void run() {
703 try {
704 lock.lock();
705 threadAssertFalse(lock.hasWaiters(c));
706 threadAssertEquals(0, lock.getWaitQueueLength(c));
707 c.await();
708 lock.unlock();
709 }
710 catch(InterruptedException e) {
711 threadUnexpectedException();
712 }
713 }
714 });
715
716 Thread t2 = new Thread(new Runnable() {
717 public void run() {
718 try {
719 lock.lock();
720 threadAssertTrue(lock.hasWaiters(c));
721 threadAssertEquals(1, lock.getWaitQueueLength(c));
722 c.await();
723 lock.unlock();
724 }
725 catch(InterruptedException e) {
726 threadUnexpectedException();
727 }
728 }
729 });
730
731 try {
732 t1.start();
733 Thread.sleep(SHORT_DELAY_MS);
734 t2.start();
735 Thread.sleep(SHORT_DELAY_MS);
736 lock.lock();
737 assertTrue(lock.hasWaiters(c));
738 assertEquals(2, lock.getWaitQueueLength(c));
739 c.signalAll();
740 lock.unlock();
741 Thread.sleep(SHORT_DELAY_MS);
742 lock.lock();
743 assertFalse(lock.hasWaiters(c));
744 assertEquals(0, lock.getWaitQueueLength(c));
745 lock.unlock();
746 t1.join(SHORT_DELAY_MS);
747 t2.join(SHORT_DELAY_MS);
748 assertFalse(t1.isAlive());
749 assertFalse(t2.isAlive());
750 }
751 catch (Exception ex) {
752 unexpectedException();
753 }
754 }
755
756 /**
757 * getWaitingThreads returns only and all waiting threads
758 */
759 public void testGetWaitingThreads() {
760 final PublicReentrantLock lock = new PublicReentrantLock();
761 final Condition c = lock.newCondition();
762 Thread t1 = new Thread(new Runnable() {
763 public void run() {
764 try {
765 lock.lock();
766 threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
767 c.await();
768 lock.unlock();
769 }
770 catch(InterruptedException e) {
771 threadUnexpectedException();
772 }
773 }
774 });
775
776 Thread t2 = new Thread(new Runnable() {
777 public void run() {
778 try {
779 lock.lock();
780 threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
781 c.await();
782 lock.unlock();
783 }
784 catch(InterruptedException e) {
785 threadUnexpectedException();
786 }
787 }
788 });
789
790 try {
791 lock.lock();
792 assertTrue(lock.getWaitingThreads(c).isEmpty());
793 lock.unlock();
794 t1.start();
795 Thread.sleep(SHORT_DELAY_MS);
796 t2.start();
797 Thread.sleep(SHORT_DELAY_MS);
798 lock.lock();
799 assertTrue(lock.hasWaiters(c));
800 assertTrue(lock.getWaitingThreads(c).contains(t1));
801 assertTrue(lock.getWaitingThreads(c).contains(t2));
802 c.signalAll();
803 lock.unlock();
804 Thread.sleep(SHORT_DELAY_MS);
805 lock.lock();
806 assertFalse(lock.hasWaiters(c));
807 assertTrue(lock.getWaitingThreads(c).isEmpty());
808 lock.unlock();
809 t1.join(SHORT_DELAY_MS);
810 t2.join(SHORT_DELAY_MS);
811 assertFalse(t1.isAlive());
812 assertFalse(t2.isAlive());
813 }
814 catch (Exception ex) {
815 unexpectedException();
816 }
817 }
818
819
820
821 /**
822 * awaitUninterruptibly doesn't abort on interrupt
823 */
824 public void testAwaitUninterruptibly() {
825 final ReentrantLock lock = new ReentrantLock();
826 final Condition c = lock.newCondition();
827 Thread t = new Thread(new Runnable() {
828 public void run() {
829 lock.lock();
830 c.awaitUninterruptibly();
831 lock.unlock();
832 }
833 });
834
835 try {
836 t.start();
837 Thread.sleep(SHORT_DELAY_MS);
838 t.interrupt();
839 lock.lock();
840 c.signal();
841 lock.unlock();
842 assert(t.isInterrupted());
843 t.join(SHORT_DELAY_MS);
844 assertFalse(t.isAlive());
845 }
846 catch (Exception ex) {
847 unexpectedException();
848 }
849 }
850
851 /**
852 * await is interruptible
853 */
854 public void testAwait_Interrupt() {
855 final ReentrantLock lock = new ReentrantLock();
856 final Condition c = lock.newCondition();
857 Thread t = new Thread(new Runnable() {
858 public void run() {
859 try {
860 lock.lock();
861 c.await();
862 lock.unlock();
863 threadShouldThrow();
864 }
865 catch(InterruptedException success) {
866 }
867 }
868 });
869
870 try {
871 t.start();
872 Thread.sleep(SHORT_DELAY_MS);
873 t.interrupt();
874 t.join(SHORT_DELAY_MS);
875 assertFalse(t.isAlive());
876 }
877 catch (Exception ex) {
878 unexpectedException();
879 }
880 }
881
882 /**
883 * awaitNanos is interruptible
884 */
885 public void testAwaitNanos_Interrupt() {
886 final ReentrantLock lock = new ReentrantLock();
887 final Condition c = lock.newCondition();
888 Thread t = new Thread(new Runnable() {
889 public void run() {
890 try {
891 lock.lock();
892 c.awaitNanos(1000 * 1000 * 1000); // 1 sec
893 lock.unlock();
894 threadShouldThrow();
895 }
896 catch(InterruptedException success) {
897 }
898 }
899 });
900
901 try {
902 t.start();
903 Thread.sleep(SHORT_DELAY_MS);
904 t.interrupt();
905 t.join(SHORT_DELAY_MS);
906 assertFalse(t.isAlive());
907 }
908 catch (Exception ex) {
909 unexpectedException();
910 }
911 }
912
913 /**
914 * awaitUntil is interruptible
915 */
916 public void testAwaitUntil_Interrupt() {
917 final ReentrantLock lock = new ReentrantLock();
918 final Condition c = lock.newCondition();
919 Thread t = new Thread(new Runnable() {
920 public void run() {
921 try {
922 lock.lock();
923 java.util.Date d = new java.util.Date();
924 c.awaitUntil(new java.util.Date(d.getTime() + 10000));
925 lock.unlock();
926 threadShouldThrow();
927 }
928 catch(InterruptedException success) {
929 }
930 }
931 });
932
933 try {
934 t.start();
935 Thread.sleep(SHORT_DELAY_MS);
936 t.interrupt();
937 t.join(SHORT_DELAY_MS);
938 assertFalse(t.isAlive());
939 }
940 catch (Exception ex) {
941 unexpectedException();
942 }
943 }
944
945 /**
946 * signalAll wakes up all threads
947 */
948 public void testSignalAll() {
949 final ReentrantLock lock = new ReentrantLock();
950 final Condition c = lock.newCondition();
951 Thread t1 = new Thread(new Runnable() {
952 public void run() {
953 try {
954 lock.lock();
955 c.await();
956 lock.unlock();
957 }
958 catch(InterruptedException e) {
959 threadUnexpectedException();
960 }
961 }
962 });
963
964 Thread t2 = new Thread(new Runnable() {
965 public void run() {
966 try {
967 lock.lock();
968 c.await();
969 lock.unlock();
970 }
971 catch(InterruptedException e) {
972 threadUnexpectedException();
973 }
974 }
975 });
976
977 try {
978 t1.start();
979 t2.start();
980 Thread.sleep(SHORT_DELAY_MS);
981 lock.lock();
982 c.signalAll();
983 lock.unlock();
984 t1.join(SHORT_DELAY_MS);
985 t2.join(SHORT_DELAY_MS);
986 assertFalse(t1.isAlive());
987 assertFalse(t2.isAlive());
988 }
989 catch (Exception ex) {
990 unexpectedException();
991 }
992 }
993
994 /**
995 * A serialized lock deserializes as unlocked
996 */
997 public void testSerialization() {
998 ReentrantLock l = new ReentrantLock();
999 l.lock();
1000 l.unlock();
1001
1002 try {
1003 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1004 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1005 out.writeObject(l);
1006 out.close();
1007
1008 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1009 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1010 ReentrantLock r = (ReentrantLock) in.readObject();
1011 r.lock();
1012 r.unlock();
1013 } catch(Exception e){
1014 e.printStackTrace();
1015 unexpectedException();
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 }