6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.locks.Condition; |
11 |
< |
import java.util.concurrent.locks.ReentrantLock; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
< |
import java.util.*; |
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 |
|
} |
89 |
|
Thread.yield(); |
90 |
|
} |
91 |
|
assertTrue(t.isAlive()); |
92 |
< |
assertTrue(lock.getOwner() != t); |
92 |
> |
assertNotSame(t, lock.getOwner()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
143 |
|
lock.unlock(); |
144 |
|
} |
145 |
|
|
146 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
146 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
|
|
148 |
|
/** |
149 |
|
* Awaits condition using the specified AwaitMethod. |
166 |
|
case awaitUntil: |
167 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
168 |
|
break; |
169 |
+ |
default: |
170 |
+ |
throw new AssertionError(); |
171 |
|
} |
172 |
|
} |
173 |
|
|
460 |
|
barrier.await(); |
461 |
|
awaitTermination(t); |
462 |
|
assertFalse(lock.isLocked()); |
463 |
< |
} catch (Exception e) { |
456 |
< |
threadUnexpectedException(e); |
457 |
< |
} |
463 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
464 |
|
} |
465 |
|
|
466 |
|
/** |
472 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
473 |
|
try { |
474 |
|
lock.lockInterruptibly(); |
475 |
< |
} catch (InterruptedException ie) { |
470 |
< |
threadUnexpectedException(ie); |
471 |
< |
} |
475 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
476 |
|
assertLockedByMoi(lock); |
477 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
478 |
|
waitForQueuedThread(lock, t); |
533 |
|
assertTrue(nanosRemaining <= 0); |
534 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
535 |
|
lock.unlock(); |
536 |
< |
} catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(e); |
534 |
< |
} |
536 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
537 |
|
} |
538 |
|
|
539 |
|
/** |
551 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
552 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
553 |
|
lock.unlock(); |
554 |
< |
} catch (InterruptedException e) { |
553 |
< |
threadUnexpectedException(e); |
554 |
< |
} |
554 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
555 |
|
} |
556 |
|
|
557 |
|
/** |
570 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
571 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
572 |
|
lock.unlock(); |
573 |
< |
} catch (InterruptedException e) { |
574 |
< |
threadUnexpectedException(e); |
575 |
< |
} |
573 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
574 |
|
} |
575 |
|
|
576 |
|
/** |
734 |
|
public void testHasWaiters(boolean fair) { |
735 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
736 |
|
final Condition c = lock.newCondition(); |
737 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
737 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
738 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
739 |
|
public void realRun() throws InterruptedException { |
740 |
|
lock.lock(); |
741 |
|
assertHasNoWaiters(lock, c); |
742 |
|
assertFalse(lock.hasWaiters(c)); |
743 |
< |
locked.countDown(); |
743 |
> |
pleaseSignal.countDown(); |
744 |
|
c.await(); |
745 |
|
assertHasNoWaiters(lock, c); |
746 |
|
assertFalse(lock.hasWaiters(c)); |
747 |
|
lock.unlock(); |
748 |
|
}}); |
749 |
|
|
750 |
< |
await(locked); |
750 |
> |
await(pleaseSignal); |
751 |
|
lock.lock(); |
752 |
|
assertHasWaiters(lock, c, t); |
753 |
|
assertTrue(lock.hasWaiters(c)); |
878 |
|
} |
879 |
|
|
880 |
|
/** |
881 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
881 |
> |
* awaitUninterruptibly is uninterruptible |
882 |
|
*/ |
883 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
884 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
885 |
|
public void testAwaitUninterruptibly(boolean fair) { |
886 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
887 |
|
final Condition c = lock.newCondition(); |
888 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
889 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
888 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
889 |
> |
|
890 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
891 |
|
public void realRun() { |
892 |
+ |
// Interrupt before awaitUninterruptibly |
893 |
|
lock.lock(); |
894 |
< |
locked.countDown(); |
894 |
> |
pleaseInterrupt.countDown(); |
895 |
> |
Thread.currentThread().interrupt(); |
896 |
|
c.awaitUninterruptibly(); |
897 |
|
assertTrue(Thread.interrupted()); |
898 |
|
lock.unlock(); |
899 |
|
}}); |
900 |
|
|
901 |
< |
await(locked); |
901 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
902 |
> |
public void realRun() { |
903 |
> |
// Interrupt during awaitUninterruptibly |
904 |
> |
lock.lock(); |
905 |
> |
pleaseInterrupt.countDown(); |
906 |
> |
c.awaitUninterruptibly(); |
907 |
> |
assertTrue(Thread.interrupted()); |
908 |
> |
lock.unlock(); |
909 |
> |
}}); |
910 |
> |
|
911 |
> |
await(pleaseInterrupt); |
912 |
|
lock.lock(); |
913 |
|
lock.unlock(); |
914 |
< |
t.interrupt(); |
915 |
< |
long timeoutMillis = 10; |
916 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
914 |
> |
t2.interrupt(); |
915 |
> |
|
916 |
> |
assertThreadStaysAlive(t1); |
917 |
> |
assertTrue(t2.isAlive()); |
918 |
> |
|
919 |
|
lock.lock(); |
920 |
< |
c.signal(); |
920 |
> |
c.signalAll(); |
921 |
|
lock.unlock(); |
922 |
< |
awaitTermination(t); |
922 |
> |
|
923 |
> |
awaitTermination(t1); |
924 |
> |
awaitTermination(t2); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
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 |
|
assertLockedByMoi(lock); |
947 |
|
assertHasNoWaiters(lock, c); |
948 |
< |
locked.countDown(); |
948 |
> |
pleaseInterrupt.countDown(); |
949 |
|
try { |
950 |
|
await(c, awaitMethod); |
951 |
|
} finally { |
956 |
|
} |
957 |
|
}}); |
958 |
|
|
959 |
< |
await(locked); |
959 |
> |
await(pleaseInterrupt); |
960 |
|
assertHasWaiters(lock, c, t); |
961 |
|
t.interrupt(); |
962 |
|
awaitTermination(t); |
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(2); |
980 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
981 |
|
class Awaiter extends CheckedRunnable { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
|
lock.lock(); |
984 |
< |
locked.countDown(); |
984 |
> |
pleaseSignal.countDown(); |
985 |
|
await(c, awaitMethod); |
986 |
|
lock.unlock(); |
987 |
|
} |
990 |
|
Thread t1 = newStartedThread(new Awaiter()); |
991 |
|
Thread t2 = newStartedThread(new Awaiter()); |
992 |
|
|
993 |
< |
await(locked); |
993 |
> |
await(pleaseSignal); |
994 |
|
lock.lock(); |
995 |
|
assertHasWaiters(lock, c, t1, t2); |
996 |
|
c.signalAll(); |
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 |
|
assertLockedByMoi(lock); |
1063 |
|
assertEquals(1, lock.getHoldCount()); |
1064 |
< |
locked.countDown(); |
1064 |
> |
pleaseSignal.countDown(); |
1065 |
|
c.await(); |
1066 |
|
assertLockedByMoi(lock); |
1067 |
|
assertEquals(1, lock.getHoldCount()); |
1074 |
|
lock.lock(); |
1075 |
|
assertLockedByMoi(lock); |
1076 |
|
assertEquals(2, lock.getHoldCount()); |
1077 |
< |
locked.countDown(); |
1077 |
> |
pleaseSignal.countDown(); |
1078 |
|
c.await(); |
1079 |
|
assertLockedByMoi(lock); |
1080 |
|
assertEquals(2, lock.getHoldCount()); |
1082 |
|
lock.unlock(); |
1083 |
|
}}); |
1084 |
|
|
1085 |
< |
await(locked); |
1085 |
> |
await(pleaseSignal); |
1086 |
|
lock.lock(); |
1087 |
|
assertHasWaiters(lock, c, t1, t2); |
1088 |
|
assertEquals(1, lock.getHoldCount()); |