[cvs] / jsr166 / src / test / tck / AbstractQueuedSynchronizerTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (view) (download)

1 : dl 1.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 : jsr166 1.22 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 :    
10 :     import junit.framework.*;
11 :     import java.util.*;
12 :     import java.util.concurrent.*;
13 : jsr166 1.30 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 : dl 1.1 import java.util.concurrent.locks.*;
15 :     import java.io.*;
16 :    
17 :     public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
18 :     public static void main(String[] args) {
19 : jsr166 1.34 junit.textui.TestRunner.run(suite());
20 : dl 1.1 }
21 :     public static Test suite() {
22 :     return new TestSuite(AbstractQueuedSynchronizerTest.class);
23 :     }
24 :    
25 :     /**
26 : dl 1.12 * A simple mutex class, adapted from the
27 :     * AbstractQueuedSynchronizer javadoc. Exclusive acquire tests
28 :     * exercise this as a sample user extension. Other
29 :     * methods/features of AbstractQueuedSynchronizerTest are tested
30 :     * via other test classes, including those for ReentrantLock,
31 :     * ReentrantReadWriteLock, and Semaphore
32 :     */
33 :     static class Mutex extends AbstractQueuedSynchronizer {
34 : dl 1.16 public boolean isHeldExclusively() { return getState() == 1; }
35 : jsr166 1.22
36 : dl 1.16 public boolean tryAcquire(int acquires) {
37 : jsr166 1.35 assertEquals(1, acquires);
38 : dl 1.12 return compareAndSetState(0, 1);
39 :     }
40 : jsr166 1.22
41 : dl 1.16 public boolean tryRelease(int releases) {
42 :     if (getState() == 0) throw new IllegalMonitorStateException();
43 : dl 1.12 setState(0);
44 :     return true;
45 :     }
46 : jsr166 1.22
47 : jsr166 1.35 public AbstractQueuedSynchronizer.ConditionObject newCondition() {
48 :     return new AbstractQueuedSynchronizer.ConditionObject();
49 :     }
50 : dl 1.12
51 :     }
52 :    
53 : jsr166 1.22
54 : dl 1.12 /**
55 :     * A simple latch class, to test shared mode.
56 :     */
57 : jsr166 1.22 static class BooleanLatch extends AbstractQueuedSynchronizer {
58 : dl 1.12 public boolean isSignalled() { return getState() != 0; }
59 :    
60 :     public int tryAcquireShared(int ignore) {
61 : jsr166 1.38 return isSignalled() ? 1 : -1;
62 : dl 1.1 }
63 : jsr166 1.22
64 : dl 1.12 public boolean tryReleaseShared(int ignore) {
65 :     setState(1);
66 :     return true;
67 : dl 1.1 }
68 :     }
69 :    
70 :     /**
71 : jsr166 1.28 * A runnable calling acquireInterruptibly that does not expect to
72 :     * be interrupted.
73 : dl 1.1 */
74 : jsr166 1.28 class InterruptibleSyncRunnable extends CheckedRunnable {
75 : dl 1.17 final Mutex sync;
76 :     InterruptibleSyncRunnable(Mutex l) { sync = l; }
77 : jsr166 1.28 public void realRun() throws InterruptedException {
78 :     sync.acquireInterruptibly(1);
79 : dl 1.1 }
80 :     }
81 :    
82 :    
83 :     /**
84 : dl 1.16 * A runnable calling acquireInterruptibly that expects to be
85 : jsr166 1.28 * interrupted.
86 : dl 1.1 */
87 : jsr166 1.28 class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
88 : dl 1.17 final Mutex sync;
89 :     InterruptedSyncRunnable(Mutex l) { sync = l; }
90 : jsr166 1.28 public void realRun() throws InterruptedException {
91 :     sync.acquireInterruptibly(1);
92 : dl 1.1 }
93 :     }
94 : dl 1.16
95 :     /**
96 :     * isHeldExclusively is false upon construction
97 :     */
98 : jsr166 1.22 public void testIsHeldExclusively() {
99 : jsr166 1.29 Mutex rl = new Mutex();
100 : dl 1.16 assertFalse(rl.isHeldExclusively());
101 :     }
102 : jsr166 1.22
103 : dl 1.1 /**
104 : dl 1.17 * acquiring released sync succeeds
105 : dl 1.1 */
106 : jsr166 1.22 public void testAcquire() {
107 : jsr166 1.29 Mutex rl = new Mutex();
108 : dl 1.16 rl.acquire(1);
109 :     assertTrue(rl.isHeldExclusively());
110 :     rl.release(1);
111 : dl 1.17 assertFalse(rl.isHeldExclusively());
112 : dl 1.1 }
113 :    
114 :     /**
115 : dl 1.17 * tryAcquire on an released sync succeeds
116 : dl 1.1 */
117 : jsr166 1.22 public void testTryAcquire() {
118 : jsr166 1.29 Mutex rl = new Mutex();
119 : dl 1.16 assertTrue(rl.tryAcquire(1));
120 :     assertTrue(rl.isHeldExclusively());
121 :     rl.release(1);
122 : dl 1.1 }
123 :    
124 :     /**
125 :     * hasQueuedThreads reports whether there are waiting threads
126 :     */
127 : jsr166 1.26 public void testhasQueuedThreads() throws InterruptedException {
128 : jsr166 1.29 final Mutex sync = new Mutex();
129 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
130 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
131 : jsr166 1.26 assertFalse(sync.hasQueuedThreads());
132 :     sync.acquire(1);
133 :     t1.start();
134 :     Thread.sleep(SHORT_DELAY_MS);
135 :     assertTrue(sync.hasQueuedThreads());
136 :     t2.start();
137 :     Thread.sleep(SHORT_DELAY_MS);
138 :     assertTrue(sync.hasQueuedThreads());
139 :     t1.interrupt();
140 :     Thread.sleep(SHORT_DELAY_MS);
141 :     assertTrue(sync.hasQueuedThreads());
142 :     sync.release(1);
143 :     Thread.sleep(SHORT_DELAY_MS);
144 :     assertFalse(sync.hasQueuedThreads());
145 :     t1.join();
146 :     t2.join();
147 : jsr166 1.22 }
148 : dl 1.10
149 :     /**
150 : dl 1.11 * isQueued(null) throws NPE
151 :     */
152 : jsr166 1.22 public void testIsQueuedNPE() {
153 : jsr166 1.29 final Mutex sync = new Mutex();
154 : dl 1.11 try {
155 : dl 1.17 sync.isQueued(null);
156 : dl 1.11 shouldThrow();
157 : jsr166 1.26 } catch (NullPointerException success) {}
158 : dl 1.11 }
159 :    
160 :     /**
161 : dl 1.10 * isQueued reports whether a thread is queued.
162 :     */
163 : jsr166 1.26 public void testIsQueued() throws InterruptedException {
164 : jsr166 1.29 final Mutex sync = new Mutex();
165 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
166 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
167 : jsr166 1.26 assertFalse(sync.isQueued(t1));
168 :     assertFalse(sync.isQueued(t2));
169 :     sync.acquire(1);
170 :     t1.start();
171 :     Thread.sleep(SHORT_DELAY_MS);
172 :     assertTrue(sync.isQueued(t1));
173 :     t2.start();
174 :     Thread.sleep(SHORT_DELAY_MS);
175 :     assertTrue(sync.isQueued(t1));
176 :     assertTrue(sync.isQueued(t2));
177 :     t1.interrupt();
178 :     Thread.sleep(SHORT_DELAY_MS);
179 :     assertFalse(sync.isQueued(t1));
180 :     assertTrue(sync.isQueued(t2));
181 :     sync.release(1);
182 :     Thread.sleep(SHORT_DELAY_MS);
183 :     assertFalse(sync.isQueued(t1));
184 :     Thread.sleep(SHORT_DELAY_MS);
185 :     assertFalse(sync.isQueued(t2));
186 :     t1.join();
187 :     t2.join();
188 : jsr166 1.22 }
189 : dl 1.8
190 : dl 1.12 /**
191 : dl 1.17 * getFirstQueuedThread returns first waiting thread or null if none
192 : dl 1.12 */
193 : jsr166 1.26 public void testGetFirstQueuedThread() throws InterruptedException {
194 : jsr166 1.29 final Mutex sync = new Mutex();
195 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
196 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
197 : jsr166 1.26 assertNull(sync.getFirstQueuedThread());
198 :     sync.acquire(1);
199 :     t1.start();
200 :     Thread.sleep(SHORT_DELAY_MS);
201 :     assertEquals(t1, sync.getFirstQueuedThread());
202 :     t2.start();
203 :     Thread.sleep(SHORT_DELAY_MS);
204 :     assertEquals(t1, sync.getFirstQueuedThread());
205 :     t1.interrupt();
206 :     Thread.sleep(SHORT_DELAY_MS);
207 :     Thread.sleep(SHORT_DELAY_MS);
208 :     assertEquals(t2, sync.getFirstQueuedThread());
209 :     sync.release(1);
210 :     Thread.sleep(SHORT_DELAY_MS);
211 :     assertNull(sync.getFirstQueuedThread());
212 :     t1.join();
213 :     t2.join();
214 : jsr166 1.22 }
215 : dl 1.12
216 : dl 1.8
217 :     /**
218 : dl 1.12 * hasContended reports false if no thread has ever blocked, else true
219 : dl 1.8 */
220 : jsr166 1.26 public void testHasContended() throws InterruptedException {
221 : jsr166 1.29 final Mutex sync = new Mutex();
222 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
223 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
224 : jsr166 1.26 assertFalse(sync.hasContended());
225 :     sync.acquire(1);
226 :     t1.start();
227 :     Thread.sleep(SHORT_DELAY_MS);
228 :     assertTrue(sync.hasContended());
229 :     t2.start();
230 :     Thread.sleep(SHORT_DELAY_MS);
231 :     assertTrue(sync.hasContended());
232 :     t1.interrupt();
233 :     Thread.sleep(SHORT_DELAY_MS);
234 :     assertTrue(sync.hasContended());
235 :     sync.release(1);
236 :     Thread.sleep(SHORT_DELAY_MS);
237 :     assertTrue(sync.hasContended());
238 :     t1.join();
239 :     t2.join();
240 : jsr166 1.22 }
241 : dl 1.17
242 :     /**
243 :     * getQueuedThreads includes waiting threads
244 :     */
245 : jsr166 1.26 public void testGetQueuedThreads() throws InterruptedException {
246 : jsr166 1.29 final Mutex sync = new Mutex();
247 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
248 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
249 : jsr166 1.26 assertTrue(sync.getQueuedThreads().isEmpty());
250 :     sync.acquire(1);
251 :     assertTrue(sync.getQueuedThreads().isEmpty());
252 :     t1.start();
253 :     Thread.sleep(SHORT_DELAY_MS);
254 :     assertTrue(sync.getQueuedThreads().contains(t1));
255 :     t2.start();
256 :     Thread.sleep(SHORT_DELAY_MS);
257 :     assertTrue(sync.getQueuedThreads().contains(t1));
258 :     assertTrue(sync.getQueuedThreads().contains(t2));
259 :     t1.interrupt();
260 :     Thread.sleep(SHORT_DELAY_MS);
261 :     assertFalse(sync.getQueuedThreads().contains(t1));
262 :     assertTrue(sync.getQueuedThreads().contains(t2));
263 :     sync.release(1);
264 :     Thread.sleep(SHORT_DELAY_MS);
265 :     assertTrue(sync.getQueuedThreads().isEmpty());
266 :     t1.join();
267 :     t2.join();
268 : jsr166 1.22 }
269 : dl 1.17
270 :     /**
271 :     * getExclusiveQueuedThreads includes waiting threads
272 :     */
273 : jsr166 1.26 public void testGetExclusiveQueuedThreads() throws InterruptedException {
274 : jsr166 1.29 final Mutex sync = new Mutex();
275 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
276 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
277 : jsr166 1.26 assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
278 :     sync.acquire(1);
279 :     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
280 :     t1.start();
281 :     Thread.sleep(SHORT_DELAY_MS);
282 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
283 :     t2.start();
284 :     Thread.sleep(SHORT_DELAY_MS);
285 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
286 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
287 :     t1.interrupt();
288 :     Thread.sleep(SHORT_DELAY_MS);
289 :     assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
290 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
291 :     sync.release(1);
292 :     Thread.sleep(SHORT_DELAY_MS);
293 :     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
294 :     t1.join();
295 :     t2.join();
296 : jsr166 1.22 }
297 : dl 1.17
298 :     /**
299 :     * getSharedQueuedThreads does not include exclusively waiting threads
300 :     */
301 : jsr166 1.26 public void testGetSharedQueuedThreads() throws InterruptedException {
302 : jsr166 1.29 final Mutex sync = new Mutex();
303 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
304 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
305 : jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
306 :     sync.acquire(1);
307 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
308 :     t1.start();
309 :     Thread.sleep(SHORT_DELAY_MS);
310 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
311 :     t2.start();
312 :     Thread.sleep(SHORT_DELAY_MS);
313 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
314 :     t1.interrupt();
315 :     Thread.sleep(SHORT_DELAY_MS);
316 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
317 :     sync.release(1);
318 :     Thread.sleep(SHORT_DELAY_MS);
319 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
320 :     t1.join();
321 :     t2.join();
322 : jsr166 1.22 }
323 : dl 1.1
324 :     /**
325 : dl 1.16 * tryAcquireNanos is interruptible.
326 : dl 1.1 */
327 : jsr166 1.27 public void testInterruptedException2() throws InterruptedException {
328 : jsr166 1.29 final Mutex sync = new Mutex();
329 :     sync.acquire(1);
330 :     Thread t = new Thread(new CheckedInterruptedRunnable() {
331 : jsr166 1.28 public void realRun() throws InterruptedException {
332 : jsr166 1.33 sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS));
333 : jsr166 1.28 }});
334 : jsr166 1.26
335 :     t.start();
336 : jsr166 1.32 Thread.sleep(SHORT_DELAY_MS);
337 : jsr166 1.26 t.interrupt();
338 : jsr166 1.27 t.join();
339 : dl 1.1 }
340 :    
341 :    
342 :     /**
343 : dl 1.17 * TryAcquire on exclusively held sync fails
344 : dl 1.1 */
345 : jsr166 1.26 public void testTryAcquireWhenSynced() throws InterruptedException {
346 : jsr166 1.29 final Mutex sync = new Mutex();
347 :     sync.acquire(1);
348 :     Thread t = new Thread(new CheckedRunnable() {
349 : jsr166 1.28 public void realRun() {
350 : jsr166 1.35 assertFalse(sync.tryAcquire(1));
351 : jsr166 1.28 }});
352 : jsr166 1.26
353 :     t.start();
354 :     t.join();
355 :     sync.release(1);
356 : jsr166 1.22 }
357 : dl 1.1
358 :     /**
359 : dl 1.17 * tryAcquireNanos on an exclusively held sync times out
360 : dl 1.1 */
361 : jsr166 1.26 public void testAcquireNanos_Timeout() throws InterruptedException {
362 : jsr166 1.29 final Mutex sync = new Mutex();
363 :     sync.acquire(1);
364 :     Thread t = new Thread(new CheckedRunnable() {
365 : jsr166 1.28 public void realRun() throws InterruptedException {
366 : jsr166 1.33 long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS);
367 :     assertFalse(sync.tryAcquireNanos(1, nanos));
368 : jsr166 1.28 }});
369 : jsr166 1.26
370 :     t.start();
371 :     t.join();
372 :     sync.release(1);
373 : jsr166 1.22 }
374 :    
375 :    
376 : dl 1.1 /**
377 : dl 1.13 * getState is true when acquired and false when not
378 : dl 1.1 */
379 : jsr166 1.26 public void testGetState() throws InterruptedException {
380 : jsr166 1.29 final Mutex sync = new Mutex();
381 :     sync.acquire(1);
382 :     assertTrue(sync.isHeldExclusively());
383 :     sync.release(1);
384 :     assertFalse(sync.isHeldExclusively());
385 :     Thread t = new Thread(new CheckedRunnable() {
386 : jsr166 1.28 public void realRun() throws InterruptedException {
387 :     sync.acquire(1);
388 :     Thread.sleep(SMALL_DELAY_MS);
389 :     sync.release(1);
390 :     }});
391 : jsr166 1.26
392 :     t.start();
393 :     Thread.sleep(SHORT_DELAY_MS);
394 :     assertTrue(sync.isHeldExclusively());
395 :     t.join();
396 :     assertFalse(sync.isHeldExclusively());
397 : dl 1.1 }
398 :    
399 :    
400 :     /**
401 : dl 1.16 * acquireInterruptibly is interruptible.
402 : dl 1.1 */
403 : jsr166 1.26 public void testAcquireInterruptibly1() throws InterruptedException {
404 : jsr166 1.29 final Mutex sync = new Mutex();
405 :     sync.acquire(1);
406 :     Thread t = new Thread(new InterruptedSyncRunnable(sync));
407 : jsr166 1.26
408 :     t.start();
409 :     Thread.sleep(SHORT_DELAY_MS);
410 :     t.interrupt();
411 :     Thread.sleep(SHORT_DELAY_MS);
412 :     sync.release(1);
413 :     t.join();
414 : jsr166 1.22 }
415 : dl 1.1
416 :     /**
417 : dl 1.16 * acquireInterruptibly succeeds when released, else is interruptible
418 : dl 1.1 */
419 : jsr166 1.26 public void testAcquireInterruptibly2() throws InterruptedException {
420 : jsr166 1.29 final Mutex sync = new Mutex();
421 : jsr166 1.26 sync.acquireInterruptibly(1);
422 : jsr166 1.29 Thread t = new Thread(new InterruptedSyncRunnable(sync));
423 : jsr166 1.26 t.start();
424 : jsr166 1.32 Thread.sleep(SHORT_DELAY_MS);
425 : jsr166 1.26 t.interrupt();
426 :     assertTrue(sync.isHeldExclusively());
427 :     t.join();
428 : dl 1.1 }
429 :    
430 :     /**
431 : dl 1.17 * owns is true for a condition created by sync else false
432 : dl 1.12 */
433 :     public void testOwns() {
434 : jsr166 1.29 final Mutex sync = new Mutex();
435 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
436 :     final Mutex sync2 = new Mutex();
437 :     assertTrue(sync.owns(c));
438 :     assertFalse(sync2.owns(c));
439 : dl 1.12 }
440 :    
441 :     /**
442 : dl 1.17 * Calling await without holding sync throws IllegalMonitorStateException
443 : dl 1.1 */
444 : jsr166 1.26 public void testAwait_IllegalMonitor() throws InterruptedException {
445 : jsr166 1.29 final Mutex sync = new Mutex();
446 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
447 : dl 1.1 try {
448 :     c.await();
449 :     shouldThrow();
450 : jsr166 1.28 } catch (IllegalMonitorStateException success) {}
451 : dl 1.1 }
452 :    
453 :     /**
454 : dl 1.17 * Calling signal without holding sync throws IllegalMonitorStateException
455 : dl 1.1 */
456 :     public void testSignal_IllegalMonitor() {
457 : jsr166 1.29 final Mutex sync = new Mutex();
458 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
459 : dl 1.1 try {
460 :     c.signal();
461 :     shouldThrow();
462 : jsr166 1.28 } catch (IllegalMonitorStateException success) {}
463 : dl 1.1 }
464 :    
465 :     /**
466 :     * awaitNanos without a signal times out
467 :     */
468 : jsr166 1.26 public void testAwaitNanos_Timeout() throws InterruptedException {
469 : jsr166 1.29 final Mutex sync = new Mutex();
470 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
471 : jsr166 1.26 sync.acquire(1);
472 :     long t = c.awaitNanos(100);
473 :     assertTrue(t <= 0);
474 :     sync.release(1);
475 : dl 1.1 }
476 :    
477 :     /**
478 : jsr166 1.36 * Timed await without a signal times out
479 : dl 1.1 */
480 : jsr166 1.26 public void testAwait_Timeout() throws InterruptedException {
481 : jsr166 1.29 final Mutex sync = new Mutex();
482 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
483 : jsr166 1.26 sync.acquire(1);
484 : jsr166 1.30 assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
485 : jsr166 1.26 sync.release(1);
486 : dl 1.1 }
487 :    
488 :     /**
489 :     * awaitUntil without a signal times out
490 :     */
491 : jsr166 1.26 public void testAwaitUntil_Timeout() throws InterruptedException {
492 : jsr166 1.29 final Mutex sync = new Mutex();
493 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
494 : jsr166 1.26 sync.acquire(1);
495 :     java.util.Date d = new java.util.Date();
496 :     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
497 :     sync.release(1);
498 : dl 1.1 }
499 :    
500 :     /**
501 :     * await returns when signalled
502 :     */
503 : jsr166 1.26 public void testAwait() throws InterruptedException {
504 : jsr166 1.29 final Mutex sync = new Mutex();
505 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
506 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
507 : jsr166 1.28 public void realRun() throws InterruptedException {
508 :     sync.acquire(1);
509 :     c.await();
510 :     sync.release(1);
511 :     }});
512 : dl 1.15
513 : jsr166 1.26 t.start();
514 :     Thread.sleep(SHORT_DELAY_MS);
515 :     sync.acquire(1);
516 :     c.signal();
517 :     sync.release(1);
518 :     t.join(SHORT_DELAY_MS);
519 :     assertFalse(t.isAlive());
520 : dl 1.15 }
521 :    
522 :    
523 :    
524 :     /**
525 :     * hasWaiters throws NPE if null
526 :     */
527 :     public void testHasWaitersNPE() {
528 : jsr166 1.29 final Mutex sync = new Mutex();
529 : dl 1.15 try {
530 : dl 1.17 sync.hasWaiters(null);
531 : dl 1.15 shouldThrow();
532 : jsr166 1.26 } catch (NullPointerException success) {}
533 : dl 1.15 }
534 :    
535 :     /**
536 :     * getWaitQueueLength throws NPE if null
537 :     */
538 :     public void testGetWaitQueueLengthNPE() {
539 : jsr166 1.29 final Mutex sync = new Mutex();
540 : dl 1.15 try {
541 : dl 1.17 sync.getWaitQueueLength(null);
542 : dl 1.15 shouldThrow();
543 : jsr166 1.26 } catch (NullPointerException success) {}
544 : dl 1.15 }
545 :    
546 :    
547 :     /**
548 :     * getWaitingThreads throws NPE if null
549 :     */
550 :     public void testGetWaitingThreadsNPE() {
551 : jsr166 1.29 final Mutex sync = new Mutex();
552 : dl 1.15 try {
553 : dl 1.17 sync.getWaitingThreads(null);
554 : dl 1.15 shouldThrow();
555 : jsr166 1.26 } catch (NullPointerException success) {}
556 : dl 1.15 }
557 :    
558 :    
559 :     /**
560 :     * hasWaiters throws IAE if not owned
561 :     */
562 :     public void testHasWaitersIAE() {
563 : jsr166 1.29 final Mutex sync = new Mutex();
564 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
565 : jsr166 1.29 final Mutex sync2 = new Mutex();
566 : dl 1.15 try {
567 : dl 1.17 sync2.hasWaiters(c);
568 : dl 1.15 shouldThrow();
569 : jsr166 1.26 } catch (IllegalArgumentException success) {}
570 : dl 1.15 }
571 :    
572 :     /**
573 : dl 1.17 * hasWaiters throws IMSE if not synced
574 : dl 1.15 */
575 :     public void testHasWaitersIMSE() {
576 : jsr166 1.29 final Mutex sync = new Mutex();
577 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
578 : dl 1.15 try {
579 : dl 1.17 sync.hasWaiters(c);
580 : dl 1.15 shouldThrow();
581 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
582 : dl 1.15 }
583 :    
584 :    
585 :     /**
586 :     * getWaitQueueLength throws IAE if not owned
587 :     */
588 :     public void testGetWaitQueueLengthIAE() {
589 : jsr166 1.29 final Mutex sync = new Mutex();
590 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
591 : jsr166 1.29 final Mutex sync2 = new Mutex();
592 : dl 1.15 try {
593 : dl 1.17 sync2.getWaitQueueLength(c);
594 : dl 1.15 shouldThrow();
595 : jsr166 1.26 } catch (IllegalArgumentException success) {}
596 : dl 1.15 }
597 :    
598 :     /**
599 : dl 1.17 * getWaitQueueLength throws IMSE if not synced
600 : dl 1.15 */
601 :     public void testGetWaitQueueLengthIMSE() {
602 : jsr166 1.29 final Mutex sync = new Mutex();
603 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
604 : dl 1.15 try {
605 : dl 1.17 sync.getWaitQueueLength(c);
606 : dl 1.15 shouldThrow();
607 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
608 : dl 1.15 }
609 :    
610 :    
611 :     /**
612 :     * getWaitingThreads throws IAE if not owned
613 :     */
614 :     public void testGetWaitingThreadsIAE() {
615 : jsr166 1.29 final Mutex sync = new Mutex();
616 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
617 : jsr166 1.29 final Mutex sync2 = new Mutex();
618 : dl 1.15 try {
619 : dl 1.17 sync2.getWaitingThreads(c);
620 : dl 1.15 shouldThrow();
621 : jsr166 1.26 } catch (IllegalArgumentException success) {}
622 : dl 1.15 }
623 :    
624 :     /**
625 : dl 1.17 * getWaitingThreads throws IMSE if not synced
626 : dl 1.15 */
627 :     public void testGetWaitingThreadsIMSE() {
628 : jsr166 1.29 final Mutex sync = new Mutex();
629 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
630 : dl 1.15 try {
631 : dl 1.17 sync.getWaitingThreads(c);
632 : dl 1.15 shouldThrow();
633 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
634 : dl 1.15 }
635 :    
636 :    
637 :    
638 :     /**
639 :     * hasWaiters returns true when a thread is waiting, else false
640 :     */
641 : jsr166 1.26 public void testHasWaiters() throws InterruptedException {
642 : jsr166 1.29 final Mutex sync = new Mutex();
643 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
644 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
645 : jsr166 1.28 public void realRun() throws InterruptedException {
646 :     sync.acquire(1);
647 : jsr166 1.35 assertFalse(sync.hasWaiters(c));
648 :     assertEquals(0, sync.getWaitQueueLength(c));
649 : jsr166 1.28 c.await();
650 :     sync.release(1);
651 :     }});
652 : dl 1.15
653 : jsr166 1.26 t.start();
654 :     Thread.sleep(SHORT_DELAY_MS);
655 :     sync.acquire(1);
656 :     assertTrue(sync.hasWaiters(c));
657 :     assertEquals(1, sync.getWaitQueueLength(c));
658 :     c.signal();
659 :     sync.release(1);
660 :     Thread.sleep(SHORT_DELAY_MS);
661 :     sync.acquire(1);
662 :     assertFalse(sync.hasWaiters(c));
663 :     assertEquals(0, sync.getWaitQueueLength(c));
664 :     sync.release(1);
665 :     t.join(SHORT_DELAY_MS);
666 :     assertFalse(t.isAlive());
667 : dl 1.15 }
668 :    
669 :     /**
670 :     * getWaitQueueLength returns number of waiting threads
671 :     */
672 : jsr166 1.26 public void testGetWaitQueueLength() throws InterruptedException {
673 : jsr166 1.29 final Mutex sync = new Mutex();
674 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
675 : jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
676 : jsr166 1.28 public void realRun() throws InterruptedException {
677 :     sync.acquire(1);
678 : jsr166 1.35 assertFalse(sync.hasWaiters(c));
679 :     assertEquals(0, sync.getWaitQueueLength(c));
680 : jsr166 1.28 c.await();
681 :     sync.release(1);
682 :     }});
683 :    
684 : jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
685 : jsr166 1.28 public void realRun() throws InterruptedException {
686 :     sync.acquire(1);
687 : jsr166 1.35 assertTrue(sync.hasWaiters(c));
688 :     assertEquals(1, sync.getWaitQueueLength(c));
689 : jsr166 1.28 c.await();
690 :     sync.release(1);
691 :     }});
692 : dl 1.15
693 : jsr166 1.26 t1.start();
694 :     Thread.sleep(SHORT_DELAY_MS);
695 :     t2.start();
696 :     Thread.sleep(SHORT_DELAY_MS);
697 :     sync.acquire(1);
698 :     assertTrue(sync.hasWaiters(c));
699 :     assertEquals(2, sync.getWaitQueueLength(c));
700 :     c.signalAll();
701 :     sync.release(1);
702 :     Thread.sleep(SHORT_DELAY_MS);
703 :     sync.acquire(1);
704 :     assertFalse(sync.hasWaiters(c));
705 :     assertEquals(0, sync.getWaitQueueLength(c));
706 :     sync.release(1);
707 :     t1.join(SHORT_DELAY_MS);
708 :     t2.join(SHORT_DELAY_MS);
709 :     assertFalse(t1.isAlive());
710 :     assertFalse(t2.isAlive());
711 : dl 1.15 }
712 :    
713 :     /**
714 :     * getWaitingThreads returns only and all waiting threads
715 :     */
716 : jsr166 1.26 public void testGetWaitingThreads() throws InterruptedException {
717 : jsr166 1.29 final Mutex sync = new Mutex();
718 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
719 : jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
720 : jsr166 1.28 public void realRun() throws InterruptedException {
721 :     sync.acquire(1);
722 : jsr166 1.35 assertTrue(sync.getWaitingThreads(c).isEmpty());
723 : jsr166 1.28 c.await();
724 :     sync.release(1);
725 :     }});
726 :    
727 : jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
728 : jsr166 1.28 public void realRun() throws InterruptedException {
729 :     sync.acquire(1);
730 : jsr166 1.35 assertFalse(sync.getWaitingThreads(c).isEmpty());
731 : jsr166 1.28 c.await();
732 :     sync.release(1);
733 :     }});
734 : dl 1.1
735 : jsr166 1.26 sync.acquire(1);
736 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
737 :     sync.release(1);
738 :     t1.start();
739 :     Thread.sleep(SHORT_DELAY_MS);
740 :     t2.start();
741 :     Thread.sleep(SHORT_DELAY_MS);
742 :     sync.acquire(1);
743 :     assertTrue(sync.hasWaiters(c));
744 :     assertTrue(sync.getWaitingThreads(c).contains(t1));
745 :     assertTrue(sync.getWaitingThreads(c).contains(t2));
746 :     c.signalAll();
747 :     sync.release(1);
748 :     Thread.sleep(SHORT_DELAY_MS);
749 :     sync.acquire(1);
750 :     assertFalse(sync.hasWaiters(c));
751 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
752 :     sync.release(1);
753 :     t1.join(SHORT_DELAY_MS);
754 :     t2.join(SHORT_DELAY_MS);
755 :     assertFalse(t1.isAlive());
756 :     assertFalse(t2.isAlive());
757 : dl 1.15 }
758 :    
759 :    
760 :    
761 :     /**
762 :     * awaitUninterruptibly doesn't abort on interrupt
763 :     */
764 : jsr166 1.26 public void testAwaitUninterruptibly() throws InterruptedException {
765 : jsr166 1.29 final Mutex sync = new Mutex();
766 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
767 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
768 : jsr166 1.28 public void realRun() {
769 :     sync.acquire(1);
770 :     c.awaitUninterruptibly();
771 :     sync.release(1);
772 :     }});
773 : dl 1.15
774 : jsr166 1.26 t.start();
775 :     Thread.sleep(SHORT_DELAY_MS);
776 :     t.interrupt();
777 :     sync.acquire(1);
778 :     c.signal();
779 :     sync.release(1);
780 :     t.join(SHORT_DELAY_MS);
781 :     assertFalse(t.isAlive());
782 : dl 1.15 }
783 :    
784 :     /**
785 :     * await is interruptible
786 :     */
787 : jsr166 1.26 public void testAwait_Interrupt() throws InterruptedException {
788 : jsr166 1.29 final Mutex sync = new Mutex();
789 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
790 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
791 :     public void realRun() throws InterruptedException {
792 : jsr166 1.28 sync.acquire(1);
793 :     c.await();
794 :     }});
795 : dl 1.15
796 : jsr166 1.26 t.start();
797 :     Thread.sleep(SHORT_DELAY_MS);
798 :     t.interrupt();
799 :     t.join(SHORT_DELAY_MS);
800 :     assertFalse(t.isAlive());
801 : dl 1.15 }
802 :    
803 :     /**
804 :     * awaitNanos is interruptible
805 :     */
806 : jsr166 1.26 public void testAwaitNanos_Interrupt() throws InterruptedException {
807 : jsr166 1.29 final Mutex sync = new Mutex();
808 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
809 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
810 :     public void realRun() throws InterruptedException {
811 : jsr166 1.28 sync.acquire(1);
812 : jsr166 1.33 c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
813 : jsr166 1.28 }});
814 : dl 1.15
815 : jsr166 1.26 t.start();
816 :     Thread.sleep(SHORT_DELAY_MS);
817 :     t.interrupt();
818 :     t.join(SHORT_DELAY_MS);
819 :     assertFalse(t.isAlive());
820 : dl 1.15 }
821 :    
822 :     /**
823 :     * awaitUntil is interruptible
824 :     */
825 : jsr166 1.26 public void testAwaitUntil_Interrupt() throws InterruptedException {
826 : jsr166 1.29 final Mutex sync = new Mutex();
827 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
828 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
829 :     public void realRun() throws InterruptedException {
830 : jsr166 1.28 sync.acquire(1);
831 :     java.util.Date d = new java.util.Date();
832 :     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
833 :     }});
834 : dl 1.15
835 : jsr166 1.26 t.start();
836 :     Thread.sleep(SHORT_DELAY_MS);
837 :     t.interrupt();
838 :     t.join(SHORT_DELAY_MS);
839 :     assertFalse(t.isAlive());
840 : dl 1.12 }
841 :    
842 : dl 1.14 /**
843 : dl 1.15 * signalAll wakes up all threads
844 :     */
845 : jsr166 1.26 public void testSignalAll() throws InterruptedException {
846 : jsr166 1.29 final Mutex sync = new Mutex();
847 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
848 : jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
849 : jsr166 1.28 public void realRun() throws InterruptedException {
850 :     sync.acquire(1);
851 :     c.await();
852 :     sync.release(1);
853 :     }});
854 :    
855 : jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
856 : jsr166 1.28 public void realRun() throws InterruptedException {
857 :     sync.acquire(1);
858 :     c.await();
859 :     sync.release(1);
860 :     }});
861 : dl 1.15
862 : jsr166 1.26 t1.start();
863 :     t2.start();
864 :     Thread.sleep(SHORT_DELAY_MS);
865 :     sync.acquire(1);
866 :     c.signalAll();
867 :     sync.release(1);
868 :     t1.join(SHORT_DELAY_MS);
869 :     t2.join(SHORT_DELAY_MS);
870 :     assertFalse(t1.isAlive());
871 :     assertFalse(t2.isAlive());
872 : dl 1.15 }
873 :    
874 :    
875 :     /**
876 : dl 1.14 * toString indicates current state
877 :     */
878 :     public void testToString() {
879 : dl 1.17 Mutex sync = new Mutex();
880 :     String us = sync.toString();
881 : dl 1.14 assertTrue(us.indexOf("State = 0") >= 0);
882 : dl 1.17 sync.acquire(1);
883 :     String ls = sync.toString();
884 : dl 1.14 assertTrue(ls.indexOf("State = 1") >= 0);
885 :     }
886 :    
887 :     /**
888 :     * A serialized AQS deserializes with current state
889 :     */
890 : jsr166 1.26 public void testSerialization() throws Exception {
891 : dl 1.14 Mutex l = new Mutex();
892 : dl 1.16 l.acquire(1);
893 :     assertTrue(l.isHeldExclusively());
894 : dl 1.14
895 : jsr166 1.26 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
896 :     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
897 :     out.writeObject(l);
898 :     out.close();
899 :    
900 :     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
901 :     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
902 :     Mutex r = (Mutex) in.readObject();
903 :     assertTrue(r.isHeldExclusively());
904 : dl 1.14 }
905 :    
906 : dl 1.12
907 :     /**
908 : dl 1.15 * tryReleaseShared setting state changes getState
909 : dl 1.12 */
910 : dl 1.15 public void testGetStateWithReleaseShared() {
911 : jsr166 1.29 final BooleanLatch l = new BooleanLatch();
912 :     assertFalse(l.isSignalled());
913 :     l.releaseShared(0);
914 :     assertTrue(l.isSignalled());
915 : dl 1.12 }
916 :    
917 :     /**
918 : dl 1.17 * releaseShared has no effect when already signalled
919 : dl 1.12 */
920 : dl 1.13 public void testReleaseShared() {
921 : jsr166 1.29 final BooleanLatch l = new BooleanLatch();
922 :     assertFalse(l.isSignalled());
923 :     l.releaseShared(0);
924 :     assertTrue(l.isSignalled());
925 :     l.releaseShared(0);
926 :     assertTrue(l.isSignalled());
927 : dl 1.12 }
928 :    
929 :     /**
930 :     * acquireSharedInterruptibly returns after release, but not before
931 :     */
932 : jsr166 1.26 public void testAcquireSharedInterruptibly() throws InterruptedException {
933 : jsr166 1.29 final BooleanLatch l = new BooleanLatch();
934 : dl 1.12
935 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
936 : jsr166 1.28 public void realRun() throws InterruptedException {
937 : jsr166 1.35 assertFalse(l.isSignalled());
938 : jsr166 1.28 l.acquireSharedInterruptibly(0);
939 : jsr166 1.35 assertTrue(l.isSignalled());
940 : jsr166 1.28 }});
941 : jsr166 1.26
942 :     t.start();
943 :     assertFalse(l.isSignalled());
944 :     Thread.sleep(SHORT_DELAY_MS);
945 :     l.releaseShared(0);
946 :     assertTrue(l.isSignalled());
947 :     t.join();
948 : dl 1.12 }
949 : jsr166 1.22
950 : dl 1.12
951 :     /**
952 :     * acquireSharedTimed returns after release
953 :     */
954 : jsr166 1.37 public void testAcquireSharedTimed() throws InterruptedException {
955 : jsr166 1.29 final BooleanLatch l = new BooleanLatch();
956 : dl 1.12
957 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
958 : jsr166 1.28 public void realRun() throws InterruptedException {
959 : jsr166 1.33 assertFalse(l.isSignalled());
960 :     long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS);
961 :     assertTrue(l.tryAcquireSharedNanos(0, nanos));
962 :     assertTrue(l.isSignalled());
963 : jsr166 1.28 }});
964 : jsr166 1.26
965 :     t.start();
966 :     assertFalse(l.isSignalled());
967 :     Thread.sleep(SHORT_DELAY_MS);
968 :     l.releaseShared(0);
969 :     assertTrue(l.isSignalled());
970 :     t.join();
971 : dl 1.12 }
972 : jsr166 1.22
973 : dl 1.12 /**
974 :     * acquireSharedInterruptibly throws IE if interrupted before released
975 :     */
976 : jsr166 1.26 public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
977 : dl 1.12 final BooleanLatch l = new BooleanLatch();
978 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
979 :     public void realRun() throws InterruptedException {
980 : jsr166 1.35 assertFalse(l.isSignalled());
981 : jsr166 1.28 l.acquireSharedInterruptibly(0);
982 :     }});
983 :    
984 : jsr166 1.29 t.start();
985 : jsr166 1.26 assertFalse(l.isSignalled());
986 :     t.interrupt();
987 :     t.join();
988 : dl 1.12 }
989 :    
990 :     /**
991 :     * acquireSharedTimed throws IE if interrupted before released
992 :     */
993 : jsr166 1.26 public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
994 : dl 1.12 final BooleanLatch l = new BooleanLatch();
995 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
996 :     public void realRun() throws InterruptedException {
997 : jsr166 1.33 assertFalse(l.isSignalled());
998 :     long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
999 :     l.tryAcquireSharedNanos(0, nanos);
1000 : jsr166 1.28 }});
1001 :    
1002 : dl 1.12 t.start();
1003 : jsr166 1.26 Thread.sleep(SHORT_DELAY_MS);
1004 :     assertFalse(l.isSignalled());
1005 :     t.interrupt();
1006 :     t.join();
1007 : dl 1.12 }
1008 :    
1009 :     /**
1010 :     * acquireSharedTimed times out if not released before timeout
1011 :     */
1012 : jsr166 1.26 public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1013 : dl 1.12 final BooleanLatch l = new BooleanLatch();
1014 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
1015 : jsr166 1.28 public void realRun() throws InterruptedException {
1016 : jsr166 1.33 assertFalse(l.isSignalled());
1017 :     long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
1018 :     assertFalse(l.tryAcquireSharedNanos(0, nanos));
1019 : jsr166 1.28 }});
1020 :    
1021 : dl 1.12 t.start();
1022 : jsr166 1.26 Thread.sleep(SHORT_DELAY_MS);
1023 :     assertFalse(l.isSignalled());
1024 :     t.join();
1025 : dl 1.1 }
1026 : dl 1.14
1027 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8