1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.concurrent.locks.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
|
12 |
|
|
public class ReentrantLockTest extends TestCase { |
13 |
|
|
static int HOLD_COUNT_TEST_LIMIT = 20; |
14 |
|
|
|
15 |
|
|
public static void main(String[] args) { |
16 |
|
|
junit.textui.TestRunner.run (suite()); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(ReentrantLockTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
private static long SHORT_DELAY_MS = 100; |
24 |
|
|
private static long MEDIUM_DELAY_MS = 1000; |
25 |
|
|
private static long LONG_DELAY_MS = 10000; |
26 |
|
|
|
27 |
|
|
/* |
28 |
|
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
29 |
|
|
* |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
public void testIllegalMonitorStateException(){ |
33 |
|
|
ReentrantLock rl = new ReentrantLock(); |
34 |
|
|
try{ |
35 |
|
|
rl.unlock(); |
36 |
|
|
fail("Should of thown Illegal Monitor State Exception"); |
37 |
|
|
|
38 |
|
|
}catch(IllegalMonitorStateException sucess){} |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
/* |
44 |
|
|
* makes a lock, locks it, tries to aquire the lock in another thread |
45 |
|
|
* interrupts that thread and waits for an interrupted Exception to |
46 |
|
|
* be thrown. |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
public void testInterruptedException(){ |
50 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
51 |
|
|
lock.lock(); |
52 |
|
|
Thread t = new Thread(new Runnable() { |
53 |
|
|
public void run(){ |
54 |
|
|
try{ |
55 |
|
|
lock.lockInterruptibly(); |
56 |
|
|
fail("should throw"); |
57 |
|
|
}catch(InterruptedException sucess){} |
58 |
|
|
} |
59 |
|
|
}); |
60 |
|
|
t.start(); |
61 |
|
|
t.interrupt(); |
62 |
|
|
lock.unlock(); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/* |
66 |
|
|
* tests for interrupted exception on a timed wait |
67 |
|
|
* |
68 |
|
|
*/ |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
public void testInterruptedException2(){ |
72 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
73 |
|
|
lock.lock(); |
74 |
|
|
Thread t = new Thread(new Runnable() { |
75 |
|
|
public void run(){ |
76 |
|
|
try{ |
77 |
|
|
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
78 |
|
|
fail("should throw"); |
79 |
|
|
}catch(InterruptedException sucess){} |
80 |
|
|
} |
81 |
|
|
}); |
82 |
|
|
t.start(); |
83 |
|
|
t.interrupt(); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
public void testGetHoldCount() { |
88 |
|
|
ReentrantLock lock = new ReentrantLock(); |
89 |
|
|
for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { |
90 |
|
|
lock.lock(); |
91 |
|
|
assertEquals(i,lock.getHoldCount()); |
92 |
|
|
} |
93 |
|
|
for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) { |
94 |
|
|
lock.unlock(); |
95 |
|
|
assertEquals(i-1,lock.getHoldCount()); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
public void testIsLocked() { |
103 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
104 |
|
|
lock.lock(); |
105 |
|
|
assertTrue(lock.isLocked()); |
106 |
|
|
lock.unlock(); |
107 |
|
|
assertFalse(lock.isLocked()); |
108 |
|
|
Thread t = new Thread(new Runnable() { |
109 |
|
|
public void run() { |
110 |
|
|
lock.lock(); |
111 |
|
|
try { |
112 |
|
|
Thread.sleep(SHORT_DELAY_MS * 2); |
113 |
|
|
} |
114 |
|
|
catch(Exception e) {} |
115 |
|
|
lock.unlock(); |
116 |
|
|
} |
117 |
|
|
}); |
118 |
|
|
try{ |
119 |
|
|
t.start(); |
120 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
121 |
|
|
assertTrue(lock.isLocked()); |
122 |
|
|
t.join(); |
123 |
|
|
assertFalse(lock.isLocked()); |
124 |
|
|
} catch(Exception e){ |
125 |
|
|
fail("unexpected exception"); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* |
131 |
|
|
* current thread locks interruptibly the thread |
132 |
|
|
* another thread tries to aquire the lock and blocks |
133 |
|
|
* on the call. interrupt the attempted aquireLock |
134 |
|
|
* assert that the first lock() call actually locked the lock |
135 |
|
|
* assert that the current thread is the one holding the lock |
136 |
|
|
*/ |
137 |
|
|
|
138 |
|
|
public void testLockedInterruptibly() { |
139 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
140 |
|
|
try {lock.lockInterruptibly();} catch(Exception e) {} |
141 |
|
|
Thread t = new Thread(new Runnable() { |
142 |
|
|
public void run() { |
143 |
|
|
try { |
144 |
|
|
lock.lockInterruptibly(); |
145 |
|
|
fail("Failed to generate an Interrupted Exception"); |
146 |
|
|
} |
147 |
|
|
catch(InterruptedException e) {} |
148 |
|
|
} |
149 |
|
|
}); |
150 |
|
|
t.start(); |
151 |
|
|
t.interrupt(); |
152 |
|
|
assertTrue(lock.isLocked()); |
153 |
|
|
assertTrue(lock.isHeldByCurrentThread()); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
} |