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 |
|
10 |
import junit.framework.*; |
11 |
import java.util.*; |
12 |
import java.util.concurrent.*; |
13 |
import java.util.concurrent.locks.*; |
14 |
import java.io.*; |
15 |
|
16 |
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
17 |
public static void main(String[] args) { |
18 |
junit.textui.TestRunner.run (suite()); |
19 |
} |
20 |
public static Test suite() { |
21 |
return new TestSuite(AbstractQueuedSynchronizerTest.class); |
22 |
} |
23 |
|
24 |
/** |
25 |
* A simple mutex class, from the AbstractQueuedSynchronizer |
26 |
* javadoc. All tests exercise this as a sample user extension. |
27 |
* Other methods/features of AbstractQueuedSynchronizerTest are |
28 |
* tested implicitly in ReentrantLock, ReentrantReadWriteLock, and |
29 |
* Semaphore test classes |
30 |
*/ |
31 |
static class Mutex implements Lock, java.io.Serializable { |
32 |
private static class Sync extends AbstractQueuedSynchronizer { |
33 |
boolean isLocked() { return getState() == 1; } |
34 |
|
35 |
public boolean tryAcquireExclusive(int acquires) { |
36 |
assert acquires == 1; // Does not use multiple acquires |
37 |
return compareAndSetState(0, 1); |
38 |
} |
39 |
|
40 |
public boolean tryReleaseExclusive(int releases) { |
41 |
setState(0); |
42 |
return true; |
43 |
} |
44 |
|
45 |
public void checkConditionAccess(Thread thread) { |
46 |
if (getState() == 0) throw new IllegalMonitorStateException(); |
47 |
} |
48 |
|
49 |
Condition newCondition() { return new ConditionObject(); } |
50 |
|
51 |
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { |
52 |
s.defaultReadObject(); |
53 |
setState(0); // reset to unlocked state |
54 |
} |
55 |
|
56 |
} |
57 |
|
58 |
private final Sync sync = new Sync(); |
59 |
public boolean tryLock() { |
60 |
return sync.tryAcquireExclusive(1); |
61 |
} |
62 |
public void lock() { |
63 |
sync.acquireExclusiveUninterruptibly(1); |
64 |
} |
65 |
public void lockInterruptibly() throws InterruptedException { |
66 |
sync.acquireExclusiveInterruptibly(1); |
67 |
} |
68 |
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { |
69 |
return sync.acquireExclusiveTimed(1, unit.toNanos(timeout)); |
70 |
} |
71 |
public void unlock() { sync.releaseExclusive(1); } |
72 |
public Condition newCondition() { return sync.newCondition(); } |
73 |
public boolean isLocked() { return sync.isLocked(); } |
74 |
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } |
75 |
public boolean hasContended() { return sync.hasContended(); } |
76 |
} |
77 |
|
78 |
/** |
79 |
* A runnable calling lockInterruptibly |
80 |
*/ |
81 |
class InterruptibleLockRunnable implements Runnable { |
82 |
final Mutex lock; |
83 |
InterruptibleLockRunnable(Mutex l) { lock = l; } |
84 |
public void run() { |
85 |
try { |
86 |
lock.lockInterruptibly(); |
87 |
} catch(InterruptedException success){} |
88 |
} |
89 |
} |
90 |
|
91 |
|
92 |
/** |
93 |
* A runnable calling lockInterruptibly that expects to be |
94 |
* interrupted |
95 |
*/ |
96 |
class InterruptedLockRunnable implements Runnable { |
97 |
final Mutex lock; |
98 |
InterruptedLockRunnable(Mutex l) { lock = l; } |
99 |
public void run() { |
100 |
try { |
101 |
lock.lockInterruptibly(); |
102 |
threadShouldThrow(); |
103 |
} catch(InterruptedException success){} |
104 |
} |
105 |
} |
106 |
|
107 |
/** |
108 |
* locking an unlocked lock succeeds |
109 |
*/ |
110 |
public void testLock() { |
111 |
Mutex rl = new Mutex(); |
112 |
rl.lock(); |
113 |
assertTrue(rl.isLocked()); |
114 |
rl.unlock(); |
115 |
} |
116 |
|
117 |
/** |
118 |
* tryLock on an unlocked lock succeeds |
119 |
*/ |
120 |
public void testTryLock() { |
121 |
Mutex rl = new Mutex(); |
122 |
assertTrue(rl.tryLock()); |
123 |
assertTrue(rl.isLocked()); |
124 |
rl.unlock(); |
125 |
} |
126 |
|
127 |
/** |
128 |
* hasQueuedThreads reports whether there are waiting threads |
129 |
*/ |
130 |
public void testhasQueuedThreads() { |
131 |
final Mutex lock = new Mutex(); |
132 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
133 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
134 |
try { |
135 |
assertFalse(lock.hasQueuedThreads()); |
136 |
lock.lock(); |
137 |
t1.start(); |
138 |
Thread.sleep(SHORT_DELAY_MS); |
139 |
assertTrue(lock.hasQueuedThreads()); |
140 |
t2.start(); |
141 |
Thread.sleep(SHORT_DELAY_MS); |
142 |
assertTrue(lock.hasQueuedThreads()); |
143 |
t1.interrupt(); |
144 |
Thread.sleep(SHORT_DELAY_MS); |
145 |
assertTrue(lock.hasQueuedThreads()); |
146 |
lock.unlock(); |
147 |
Thread.sleep(SHORT_DELAY_MS); |
148 |
assertFalse(lock.hasQueuedThreads()); |
149 |
t1.join(); |
150 |
t2.join(); |
151 |
} catch(Exception e){ |
152 |
unexpectedException(); |
153 |
} |
154 |
} |
155 |
|
156 |
|
157 |
/** |
158 |
* hasContended reports whether there has been contention |
159 |
*/ |
160 |
public void testhasContended() { |
161 |
final Mutex lock = new Mutex(); |
162 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
163 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
164 |
try { |
165 |
assertFalse(lock.hasContended()); |
166 |
lock.lock(); |
167 |
t1.start(); |
168 |
Thread.sleep(SHORT_DELAY_MS); |
169 |
assertTrue(lock.hasContended()); |
170 |
t2.start(); |
171 |
Thread.sleep(SHORT_DELAY_MS); |
172 |
assertTrue(lock.hasContended()); |
173 |
t1.interrupt(); |
174 |
Thread.sleep(SHORT_DELAY_MS); |
175 |
assertTrue(lock.hasContended()); |
176 |
lock.unlock(); |
177 |
Thread.sleep(SHORT_DELAY_MS); |
178 |
assertTrue(lock.hasContended()); |
179 |
t1.join(); |
180 |
t2.join(); |
181 |
} catch(Exception e){ |
182 |
unexpectedException(); |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* timed tryLock is interruptible. |
188 |
*/ |
189 |
public void testInterruptedException2() { |
190 |
final Mutex lock = new Mutex(); |
191 |
lock.lock(); |
192 |
Thread t = new Thread(new Runnable() { |
193 |
public void run() { |
194 |
try { |
195 |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
196 |
threadShouldThrow(); |
197 |
} catch(InterruptedException success){} |
198 |
} |
199 |
}); |
200 |
try { |
201 |
t.start(); |
202 |
t.interrupt(); |
203 |
} catch(Exception e){ |
204 |
unexpectedException(); |
205 |
} |
206 |
} |
207 |
|
208 |
|
209 |
/** |
210 |
* TryLock on a locked lock fails |
211 |
*/ |
212 |
public void testTryLockWhenLocked() { |
213 |
final Mutex lock = new Mutex(); |
214 |
lock.lock(); |
215 |
Thread t = new Thread(new Runnable() { |
216 |
public void run() { |
217 |
threadAssertFalse(lock.tryLock()); |
218 |
} |
219 |
}); |
220 |
try { |
221 |
t.start(); |
222 |
t.join(); |
223 |
lock.unlock(); |
224 |
} catch(Exception e){ |
225 |
unexpectedException(); |
226 |
} |
227 |
} |
228 |
|
229 |
/** |
230 |
* Timed tryLock on a locked lock times out |
231 |
*/ |
232 |
public void testTryLock_Timeout() { |
233 |
final Mutex lock = new Mutex(); |
234 |
lock.lock(); |
235 |
Thread t = new Thread(new Runnable() { |
236 |
public void run() { |
237 |
try { |
238 |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
239 |
} catch (Exception ex) { |
240 |
threadUnexpectedException(); |
241 |
} |
242 |
} |
243 |
}); |
244 |
try { |
245 |
t.start(); |
246 |
t.join(); |
247 |
lock.unlock(); |
248 |
} catch(Exception e){ |
249 |
unexpectedException(); |
250 |
} |
251 |
} |
252 |
|
253 |
|
254 |
/** |
255 |
* isLocked is true when locked and false when not |
256 |
*/ |
257 |
public void testIsLocked() { |
258 |
final Mutex lock = new Mutex(); |
259 |
lock.lock(); |
260 |
assertTrue(lock.isLocked()); |
261 |
lock.unlock(); |
262 |
assertFalse(lock.isLocked()); |
263 |
Thread t = new Thread(new Runnable() { |
264 |
public void run() { |
265 |
lock.lock(); |
266 |
try { |
267 |
Thread.sleep(SMALL_DELAY_MS); |
268 |
} |
269 |
catch(Exception e) { |
270 |
threadUnexpectedException(); |
271 |
} |
272 |
lock.unlock(); |
273 |
} |
274 |
}); |
275 |
try { |
276 |
t.start(); |
277 |
Thread.sleep(SHORT_DELAY_MS); |
278 |
assertTrue(lock.isLocked()); |
279 |
t.join(); |
280 |
assertFalse(lock.isLocked()); |
281 |
} catch(Exception e){ |
282 |
unexpectedException(); |
283 |
} |
284 |
} |
285 |
|
286 |
|
287 |
/** |
288 |
* lockInterruptibly is interruptible. |
289 |
*/ |
290 |
public void testLockInterruptibly1() { |
291 |
final Mutex lock = new Mutex(); |
292 |
lock.lock(); |
293 |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
294 |
try { |
295 |
t.start(); |
296 |
t.interrupt(); |
297 |
lock.unlock(); |
298 |
t.join(); |
299 |
} catch(Exception e){ |
300 |
unexpectedException(); |
301 |
} |
302 |
} |
303 |
|
304 |
/** |
305 |
* lockInterruptibly succeeds when unlocked, else is interruptible |
306 |
*/ |
307 |
public void testLockInterruptibly2() { |
308 |
final Mutex lock = new Mutex(); |
309 |
try { |
310 |
lock.lockInterruptibly(); |
311 |
} catch(Exception e) { |
312 |
unexpectedException(); |
313 |
} |
314 |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
315 |
try { |
316 |
t.start(); |
317 |
t.interrupt(); |
318 |
assertTrue(lock.isLocked()); |
319 |
t.join(); |
320 |
} catch(Exception e){ |
321 |
unexpectedException(); |
322 |
} |
323 |
} |
324 |
|
325 |
/** |
326 |
* Calling await without holding lock throws IllegalMonitorStateException |
327 |
*/ |
328 |
public void testAwait_IllegalMonitor() { |
329 |
final Mutex lock = new Mutex(); |
330 |
final Condition c = lock.newCondition(); |
331 |
try { |
332 |
c.await(); |
333 |
shouldThrow(); |
334 |
} |
335 |
catch (IllegalMonitorStateException success) { |
336 |
} |
337 |
catch (Exception ex) { |
338 |
unexpectedException(); |
339 |
} |
340 |
} |
341 |
|
342 |
/** |
343 |
* Calling signal without holding lock throws IllegalMonitorStateException |
344 |
*/ |
345 |
public void testSignal_IllegalMonitor() { |
346 |
final Mutex lock = new Mutex(); |
347 |
final Condition c = lock.newCondition(); |
348 |
try { |
349 |
c.signal(); |
350 |
shouldThrow(); |
351 |
} |
352 |
catch (IllegalMonitorStateException success) { |
353 |
} |
354 |
catch (Exception ex) { |
355 |
unexpectedException(); |
356 |
} |
357 |
} |
358 |
|
359 |
/** |
360 |
* awaitNanos without a signal times out |
361 |
*/ |
362 |
public void testAwaitNanos_Timeout() { |
363 |
final Mutex lock = new Mutex(); |
364 |
final Condition c = lock.newCondition(); |
365 |
try { |
366 |
lock.lock(); |
367 |
long t = c.awaitNanos(100); |
368 |
assertTrue(t <= 0); |
369 |
lock.unlock(); |
370 |
} |
371 |
catch (Exception ex) { |
372 |
unexpectedException(); |
373 |
} |
374 |
} |
375 |
|
376 |
/** |
377 |
* timed await without a signal times out |
378 |
*/ |
379 |
public void testAwait_Timeout() { |
380 |
final Mutex lock = new Mutex(); |
381 |
final Condition c = lock.newCondition(); |
382 |
try { |
383 |
lock.lock(); |
384 |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
385 |
lock.unlock(); |
386 |
} |
387 |
catch (Exception ex) { |
388 |
unexpectedException(); |
389 |
} |
390 |
} |
391 |
|
392 |
/** |
393 |
* awaitUntil without a signal times out |
394 |
*/ |
395 |
public void testAwaitUntil_Timeout() { |
396 |
final Mutex lock = new Mutex(); |
397 |
final Condition c = lock.newCondition(); |
398 |
try { |
399 |
lock.lock(); |
400 |
java.util.Date d = new java.util.Date(); |
401 |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
402 |
lock.unlock(); |
403 |
} |
404 |
catch (Exception ex) { |
405 |
unexpectedException(); |
406 |
} |
407 |
} |
408 |
|
409 |
/** |
410 |
* await returns when signalled |
411 |
*/ |
412 |
public void testAwait() { |
413 |
final Mutex lock = new Mutex(); |
414 |
final Condition c = lock.newCondition(); |
415 |
Thread t = new Thread(new Runnable() { |
416 |
public void run() { |
417 |
try { |
418 |
lock.lock(); |
419 |
c.await(); |
420 |
lock.unlock(); |
421 |
} |
422 |
catch(InterruptedException e) { |
423 |
threadUnexpectedException(); |
424 |
} |
425 |
} |
426 |
}); |
427 |
|
428 |
try { |
429 |
t.start(); |
430 |
Thread.sleep(SHORT_DELAY_MS); |
431 |
lock.lock(); |
432 |
c.signal(); |
433 |
lock.unlock(); |
434 |
t.join(SHORT_DELAY_MS); |
435 |
assertFalse(t.isAlive()); |
436 |
} |
437 |
catch (Exception ex) { |
438 |
unexpectedException(); |
439 |
} |
440 |
} |
441 |
|
442 |
} |