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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8