ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.10
Committed: Wed Jan 7 20:49:53 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.9: +34 -0 lines
Log Message:
Add tests

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
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 public boolean isQueued(Thread t) { return sync.isQueued(t); }
77 }
78
79 /**
80 * A runnable calling lockInterruptibly
81 */
82 class InterruptibleLockRunnable implements Runnable {
83 final Mutex lock;
84 InterruptibleLockRunnable(Mutex l) { lock = l; }
85 public void run() {
86 try {
87 lock.lockInterruptibly();
88 } catch(InterruptedException success){}
89 }
90 }
91
92
93 /**
94 * A runnable calling lockInterruptibly that expects to be
95 * interrupted
96 */
97 class InterruptedLockRunnable implements Runnable {
98 final Mutex lock;
99 InterruptedLockRunnable(Mutex l) { lock = l; }
100 public void run() {
101 try {
102 lock.lockInterruptibly();
103 threadShouldThrow();
104 } catch(InterruptedException success){}
105 }
106 }
107
108 /**
109 * locking an unlocked lock succeeds
110 */
111 public void testLock() {
112 Mutex rl = new Mutex();
113 rl.lock();
114 assertTrue(rl.isLocked());
115 rl.unlock();
116 }
117
118 /**
119 * tryLock on an unlocked lock succeeds
120 */
121 public void testTryLock() {
122 Mutex rl = new Mutex();
123 assertTrue(rl.tryLock());
124 assertTrue(rl.isLocked());
125 rl.unlock();
126 }
127
128 /**
129 * hasQueuedThreads reports whether there are waiting threads
130 */
131 public void testhasQueuedThreads() {
132 final Mutex lock = new Mutex();
133 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
134 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
135 try {
136 assertFalse(lock.hasQueuedThreads());
137 lock.lock();
138 t1.start();
139 Thread.sleep(SHORT_DELAY_MS);
140 assertTrue(lock.hasQueuedThreads());
141 t2.start();
142 Thread.sleep(SHORT_DELAY_MS);
143 assertTrue(lock.hasQueuedThreads());
144 t1.interrupt();
145 Thread.sleep(SHORT_DELAY_MS);
146 assertTrue(lock.hasQueuedThreads());
147 lock.unlock();
148 Thread.sleep(SHORT_DELAY_MS);
149 assertFalse(lock.hasQueuedThreads());
150 t1.join();
151 t2.join();
152 } catch(Exception e){
153 unexpectedException();
154 }
155 }
156
157 /**
158 * isQueued reports whether a thread is queued.
159 */
160 public void testIsQueued() {
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.isQueued(t1));
166 assertFalse(lock.isQueued(t2));
167 lock.lock();
168 t1.start();
169 Thread.sleep(SHORT_DELAY_MS);
170 assertTrue(lock.isQueued(t1));
171 t2.start();
172 Thread.sleep(SHORT_DELAY_MS);
173 assertTrue(lock.isQueued(t1));
174 assertTrue(lock.isQueued(t2));
175 t1.interrupt();
176 Thread.sleep(SHORT_DELAY_MS);
177 assertFalse(lock.isQueued(t1));
178 assertTrue(lock.isQueued(t2));
179 lock.unlock();
180 Thread.sleep(SHORT_DELAY_MS);
181 assertFalse(lock.isQueued(t1));
182 assertFalse(lock.isQueued(t2));
183 t1.join();
184 t2.join();
185 } catch(Exception e){
186 unexpectedException();
187 }
188 }
189
190
191 /**
192 * hasContended reports whether there has been contention
193 */
194 public void testhasContended() {
195 final Mutex lock = new Mutex();
196 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
197 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
198 try {
199 assertFalse(lock.hasContended());
200 lock.lock();
201 t1.start();
202 Thread.sleep(SHORT_DELAY_MS);
203 assertTrue(lock.hasContended());
204 t2.start();
205 Thread.sleep(SHORT_DELAY_MS);
206 assertTrue(lock.hasContended());
207 t1.interrupt();
208 Thread.sleep(SHORT_DELAY_MS);
209 assertTrue(lock.hasContended());
210 lock.unlock();
211 Thread.sleep(SHORT_DELAY_MS);
212 assertTrue(lock.hasContended());
213 t1.join();
214 t2.join();
215 } catch(Exception e){
216 unexpectedException();
217 }
218 }
219
220 /**
221 * timed tryLock is interruptible.
222 */
223 public void testInterruptedException2() {
224 final Mutex lock = new Mutex();
225 lock.lock();
226 Thread t = new Thread(new Runnable() {
227 public void run() {
228 try {
229 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
230 threadShouldThrow();
231 } catch(InterruptedException success){}
232 }
233 });
234 try {
235 t.start();
236 t.interrupt();
237 } catch(Exception e){
238 unexpectedException();
239 }
240 }
241
242
243 /**
244 * TryLock on a locked lock fails
245 */
246 public void testTryLockWhenLocked() {
247 final Mutex lock = new Mutex();
248 lock.lock();
249 Thread t = new Thread(new Runnable() {
250 public void run() {
251 threadAssertFalse(lock.tryLock());
252 }
253 });
254 try {
255 t.start();
256 t.join();
257 lock.unlock();
258 } catch(Exception e){
259 unexpectedException();
260 }
261 }
262
263 /**
264 * Timed tryLock on a locked lock times out
265 */
266 public void testTryLock_Timeout() {
267 final Mutex lock = new Mutex();
268 lock.lock();
269 Thread t = new Thread(new Runnable() {
270 public void run() {
271 try {
272 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
273 } catch (Exception ex) {
274 threadUnexpectedException();
275 }
276 }
277 });
278 try {
279 t.start();
280 t.join();
281 lock.unlock();
282 } catch(Exception e){
283 unexpectedException();
284 }
285 }
286
287
288 /**
289 * isLocked is true when locked and false when not
290 */
291 public void testIsLocked() {
292 final Mutex lock = new Mutex();
293 lock.lock();
294 assertTrue(lock.isLocked());
295 lock.unlock();
296 assertFalse(lock.isLocked());
297 Thread t = new Thread(new Runnable() {
298 public void run() {
299 lock.lock();
300 try {
301 Thread.sleep(SMALL_DELAY_MS);
302 }
303 catch(Exception e) {
304 threadUnexpectedException();
305 }
306 lock.unlock();
307 }
308 });
309 try {
310 t.start();
311 Thread.sleep(SHORT_DELAY_MS);
312 assertTrue(lock.isLocked());
313 t.join();
314 assertFalse(lock.isLocked());
315 } catch(Exception e){
316 unexpectedException();
317 }
318 }
319
320
321 /**
322 * lockInterruptibly is interruptible.
323 */
324 public void testLockInterruptibly1() {
325 final Mutex lock = new Mutex();
326 lock.lock();
327 Thread t = new Thread(new InterruptedLockRunnable(lock));
328 try {
329 t.start();
330 t.interrupt();
331 lock.unlock();
332 t.join();
333 } catch(Exception e){
334 unexpectedException();
335 }
336 }
337
338 /**
339 * lockInterruptibly succeeds when unlocked, else is interruptible
340 */
341 public void testLockInterruptibly2() {
342 final Mutex lock = new Mutex();
343 try {
344 lock.lockInterruptibly();
345 } catch(Exception e) {
346 unexpectedException();
347 }
348 Thread t = new Thread(new InterruptedLockRunnable(lock));
349 try {
350 t.start();
351 t.interrupt();
352 assertTrue(lock.isLocked());
353 t.join();
354 } catch(Exception e){
355 unexpectedException();
356 }
357 }
358
359 /**
360 * Calling await without holding lock throws IllegalMonitorStateException
361 */
362 public void testAwait_IllegalMonitor() {
363 final Mutex lock = new Mutex();
364 final Condition c = lock.newCondition();
365 try {
366 c.await();
367 shouldThrow();
368 }
369 catch (IllegalMonitorStateException success) {
370 }
371 catch (Exception ex) {
372 unexpectedException();
373 }
374 }
375
376 /**
377 * Calling signal without holding lock throws IllegalMonitorStateException
378 */
379 public void testSignal_IllegalMonitor() {
380 final Mutex lock = new Mutex();
381 final Condition c = lock.newCondition();
382 try {
383 c.signal();
384 shouldThrow();
385 }
386 catch (IllegalMonitorStateException success) {
387 }
388 catch (Exception ex) {
389 unexpectedException();
390 }
391 }
392
393 /**
394 * awaitNanos without a signal times out
395 */
396 public void testAwaitNanos_Timeout() {
397 final Mutex lock = new Mutex();
398 final Condition c = lock.newCondition();
399 try {
400 lock.lock();
401 long t = c.awaitNanos(100);
402 assertTrue(t <= 0);
403 lock.unlock();
404 }
405 catch (Exception ex) {
406 unexpectedException();
407 }
408 }
409
410 /**
411 * timed await without a signal times out
412 */
413 public void testAwait_Timeout() {
414 final Mutex lock = new Mutex();
415 final Condition c = lock.newCondition();
416 try {
417 lock.lock();
418 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
419 lock.unlock();
420 }
421 catch (Exception ex) {
422 unexpectedException();
423 }
424 }
425
426 /**
427 * awaitUntil without a signal times out
428 */
429 public void testAwaitUntil_Timeout() {
430 final Mutex lock = new Mutex();
431 final Condition c = lock.newCondition();
432 try {
433 lock.lock();
434 java.util.Date d = new java.util.Date();
435 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
436 lock.unlock();
437 }
438 catch (Exception ex) {
439 unexpectedException();
440 }
441 }
442
443 /**
444 * await returns when signalled
445 */
446 public void testAwait() {
447 final Mutex lock = new Mutex();
448 final Condition c = lock.newCondition();
449 Thread t = new Thread(new Runnable() {
450 public void run() {
451 try {
452 lock.lock();
453 c.await();
454 lock.unlock();
455 }
456 catch(InterruptedException e) {
457 threadUnexpectedException();
458 }
459 }
460 });
461
462 try {
463 t.start();
464 Thread.sleep(SHORT_DELAY_MS);
465 lock.lock();
466 c.signal();
467 lock.unlock();
468 t.join(SHORT_DELAY_MS);
469 assertFalse(t.isAlive());
470 }
471 catch (Exception ex) {
472 unexpectedException();
473 }
474 }
475
476 }