ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.11
Committed: Thu Jan 8 01:29:46 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.10: +12 -0 lines
Log Message:
Add Atomic array constructor tests; adjust timings on other 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(null) throws NPE
159 */
160 public void testIsQueuedNPE() {
161 final Mutex lock = new Mutex();
162 try {
163 lock.isQueued(null);
164 shouldThrow();
165 } catch (NullPointerException success) {
166 }
167 }
168
169 /**
170 * isQueued reports whether a thread is queued.
171 */
172 public void testIsQueued() {
173 final Mutex lock = new Mutex();
174 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
175 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
176 try {
177 assertFalse(lock.isQueued(t1));
178 assertFalse(lock.isQueued(t2));
179 lock.lock();
180 t1.start();
181 Thread.sleep(SHORT_DELAY_MS);
182 assertTrue(lock.isQueued(t1));
183 t2.start();
184 Thread.sleep(SHORT_DELAY_MS);
185 assertTrue(lock.isQueued(t1));
186 assertTrue(lock.isQueued(t2));
187 t1.interrupt();
188 Thread.sleep(SHORT_DELAY_MS);
189 assertFalse(lock.isQueued(t1));
190 assertTrue(lock.isQueued(t2));
191 lock.unlock();
192 Thread.sleep(SHORT_DELAY_MS);
193 assertFalse(lock.isQueued(t1));
194 assertFalse(lock.isQueued(t2));
195 t1.join();
196 t2.join();
197 } catch(Exception e){
198 unexpectedException();
199 }
200 }
201
202
203 /**
204 * hasContended reports whether there has been contention
205 */
206 public void testhasContended() {
207 final Mutex lock = new Mutex();
208 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
209 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
210 try {
211 assertFalse(lock.hasContended());
212 lock.lock();
213 t1.start();
214 Thread.sleep(SHORT_DELAY_MS);
215 assertTrue(lock.hasContended());
216 t2.start();
217 Thread.sleep(SHORT_DELAY_MS);
218 assertTrue(lock.hasContended());
219 t1.interrupt();
220 Thread.sleep(SHORT_DELAY_MS);
221 assertTrue(lock.hasContended());
222 lock.unlock();
223 Thread.sleep(SHORT_DELAY_MS);
224 assertTrue(lock.hasContended());
225 t1.join();
226 t2.join();
227 } catch(Exception e){
228 unexpectedException();
229 }
230 }
231
232 /**
233 * timed tryLock is interruptible.
234 */
235 public void testInterruptedException2() {
236 final Mutex lock = new Mutex();
237 lock.lock();
238 Thread t = new Thread(new Runnable() {
239 public void run() {
240 try {
241 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
242 threadShouldThrow();
243 } catch(InterruptedException success){}
244 }
245 });
246 try {
247 t.start();
248 t.interrupt();
249 } catch(Exception e){
250 unexpectedException();
251 }
252 }
253
254
255 /**
256 * TryLock on a locked lock fails
257 */
258 public void testTryLockWhenLocked() {
259 final Mutex lock = new Mutex();
260 lock.lock();
261 Thread t = new Thread(new Runnable() {
262 public void run() {
263 threadAssertFalse(lock.tryLock());
264 }
265 });
266 try {
267 t.start();
268 t.join();
269 lock.unlock();
270 } catch(Exception e){
271 unexpectedException();
272 }
273 }
274
275 /**
276 * Timed tryLock on a locked lock times out
277 */
278 public void testTryLock_Timeout() {
279 final Mutex lock = new Mutex();
280 lock.lock();
281 Thread t = new Thread(new Runnable() {
282 public void run() {
283 try {
284 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
285 } catch (Exception ex) {
286 threadUnexpectedException();
287 }
288 }
289 });
290 try {
291 t.start();
292 t.join();
293 lock.unlock();
294 } catch(Exception e){
295 unexpectedException();
296 }
297 }
298
299
300 /**
301 * isLocked is true when locked and false when not
302 */
303 public void testIsLocked() {
304 final Mutex lock = new Mutex();
305 lock.lock();
306 assertTrue(lock.isLocked());
307 lock.unlock();
308 assertFalse(lock.isLocked());
309 Thread t = new Thread(new Runnable() {
310 public void run() {
311 lock.lock();
312 try {
313 Thread.sleep(SMALL_DELAY_MS);
314 }
315 catch(Exception e) {
316 threadUnexpectedException();
317 }
318 lock.unlock();
319 }
320 });
321 try {
322 t.start();
323 Thread.sleep(SHORT_DELAY_MS);
324 assertTrue(lock.isLocked());
325 t.join();
326 assertFalse(lock.isLocked());
327 } catch(Exception e){
328 unexpectedException();
329 }
330 }
331
332
333 /**
334 * lockInterruptibly is interruptible.
335 */
336 public void testLockInterruptibly1() {
337 final Mutex lock = new Mutex();
338 lock.lock();
339 Thread t = new Thread(new InterruptedLockRunnable(lock));
340 try {
341 t.start();
342 t.interrupt();
343 lock.unlock();
344 t.join();
345 } catch(Exception e){
346 unexpectedException();
347 }
348 }
349
350 /**
351 * lockInterruptibly succeeds when unlocked, else is interruptible
352 */
353 public void testLockInterruptibly2() {
354 final Mutex lock = new Mutex();
355 try {
356 lock.lockInterruptibly();
357 } catch(Exception e) {
358 unexpectedException();
359 }
360 Thread t = new Thread(new InterruptedLockRunnable(lock));
361 try {
362 t.start();
363 t.interrupt();
364 assertTrue(lock.isLocked());
365 t.join();
366 } catch(Exception e){
367 unexpectedException();
368 }
369 }
370
371 /**
372 * Calling await without holding lock throws IllegalMonitorStateException
373 */
374 public void testAwait_IllegalMonitor() {
375 final Mutex lock = new Mutex();
376 final Condition c = lock.newCondition();
377 try {
378 c.await();
379 shouldThrow();
380 }
381 catch (IllegalMonitorStateException success) {
382 }
383 catch (Exception ex) {
384 unexpectedException();
385 }
386 }
387
388 /**
389 * Calling signal without holding lock throws IllegalMonitorStateException
390 */
391 public void testSignal_IllegalMonitor() {
392 final Mutex lock = new Mutex();
393 final Condition c = lock.newCondition();
394 try {
395 c.signal();
396 shouldThrow();
397 }
398 catch (IllegalMonitorStateException success) {
399 }
400 catch (Exception ex) {
401 unexpectedException();
402 }
403 }
404
405 /**
406 * awaitNanos without a signal times out
407 */
408 public void testAwaitNanos_Timeout() {
409 final Mutex lock = new Mutex();
410 final Condition c = lock.newCondition();
411 try {
412 lock.lock();
413 long t = c.awaitNanos(100);
414 assertTrue(t <= 0);
415 lock.unlock();
416 }
417 catch (Exception ex) {
418 unexpectedException();
419 }
420 }
421
422 /**
423 * timed await without a signal times out
424 */
425 public void testAwait_Timeout() {
426 final Mutex lock = new Mutex();
427 final Condition c = lock.newCondition();
428 try {
429 lock.lock();
430 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
431 lock.unlock();
432 }
433 catch (Exception ex) {
434 unexpectedException();
435 }
436 }
437
438 /**
439 * awaitUntil without a signal times out
440 */
441 public void testAwaitUntil_Timeout() {
442 final Mutex lock = new Mutex();
443 final Condition c = lock.newCondition();
444 try {
445 lock.lock();
446 java.util.Date d = new java.util.Date();
447 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
448 lock.unlock();
449 }
450 catch (Exception ex) {
451 unexpectedException();
452 }
453 }
454
455 /**
456 * await returns when signalled
457 */
458 public void testAwait() {
459 final Mutex lock = new Mutex();
460 final Condition c = lock.newCondition();
461 Thread t = new Thread(new Runnable() {
462 public void run() {
463 try {
464 lock.lock();
465 c.await();
466 lock.unlock();
467 }
468 catch(InterruptedException e) {
469 threadUnexpectedException();
470 }
471 }
472 });
473
474 try {
475 t.start();
476 Thread.sleep(SHORT_DELAY_MS);
477 lock.lock();
478 c.signal();
479 lock.unlock();
480 t.join(SHORT_DELAY_MS);
481 assertFalse(t.isAlive());
482 }
483 catch (Exception ex) {
484 unexpectedException();
485 }
486 }
487
488 }