[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.29 - (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 :     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 : dl 1.12 * A simple mutex class, adapted from the
26 :     * AbstractQueuedSynchronizer javadoc. Exclusive acquire tests
27 :     * exercise this as a sample user extension. Other
28 :     * methods/features of AbstractQueuedSynchronizerTest are tested
29 :     * via other test classes, including those for ReentrantLock,
30 :     * ReentrantReadWriteLock, and Semaphore
31 :     */
32 :     static class Mutex extends AbstractQueuedSynchronizer {
33 : dl 1.16 public boolean isHeldExclusively() { return getState() == 1; }
34 : jsr166 1.22
35 : dl 1.16 public boolean tryAcquire(int acquires) {
36 : jsr166 1.22 assertTrue(acquires == 1);
37 : dl 1.12 return compareAndSetState(0, 1);
38 :     }
39 : jsr166 1.22
40 : dl 1.16 public boolean tryRelease(int releases) {
41 :     if (getState() == 0) throw new IllegalMonitorStateException();
42 : dl 1.12 setState(0);
43 :     return true;
44 :     }
45 : jsr166 1.22
46 : dl 1.15 public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
47 : dl 1.12
48 :     }
49 :    
50 : jsr166 1.22
51 : dl 1.12 /**
52 :     * A simple latch class, to test shared mode.
53 :     */
54 : jsr166 1.22 static class BooleanLatch extends AbstractQueuedSynchronizer {
55 : dl 1.12 public boolean isSignalled() { return getState() != 0; }
56 :    
57 :     public int tryAcquireShared(int ignore) {
58 :     return isSignalled()? 1 : -1;
59 : dl 1.1 }
60 : jsr166 1.22
61 : dl 1.12 public boolean tryReleaseShared(int ignore) {
62 :     setState(1);
63 :     return true;
64 : dl 1.1 }
65 :     }
66 :    
67 :     /**
68 : jsr166 1.28 * A runnable calling acquireInterruptibly that does not expect to
69 :     * be interrupted.
70 : dl 1.1 */
71 : jsr166 1.28 class InterruptibleSyncRunnable extends CheckedRunnable {
72 : dl 1.17 final Mutex sync;
73 :     InterruptibleSyncRunnable(Mutex l) { sync = l; }
74 : jsr166 1.28 public void realRun() throws InterruptedException {
75 :     sync.acquireInterruptibly(1);
76 : dl 1.1 }
77 :     }
78 :    
79 :    
80 :     /**
81 : dl 1.16 * A runnable calling acquireInterruptibly that expects to be
82 : jsr166 1.28 * interrupted.
83 : dl 1.1 */
84 : jsr166 1.28 class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
85 : dl 1.17 final Mutex sync;
86 :     InterruptedSyncRunnable(Mutex l) { sync = l; }
87 : jsr166 1.28 public void realRun() throws InterruptedException {
88 :     sync.acquireInterruptibly(1);
89 : dl 1.1 }
90 :     }
91 : dl 1.16
92 :     /**
93 :     * isHeldExclusively is false upon construction
94 :     */
95 : jsr166 1.22 public void testIsHeldExclusively() {
96 : jsr166 1.29 Mutex rl = new Mutex();
97 : dl 1.16 assertFalse(rl.isHeldExclusively());
98 :     }
99 : jsr166 1.22
100 : dl 1.1 /**
101 : dl 1.17 * acquiring released sync succeeds
102 : dl 1.1 */
103 : jsr166 1.22 public void testAcquire() {
104 : jsr166 1.29 Mutex rl = new Mutex();
105 : dl 1.16 rl.acquire(1);
106 :     assertTrue(rl.isHeldExclusively());
107 :     rl.release(1);
108 : dl 1.17 assertFalse(rl.isHeldExclusively());
109 : dl 1.1 }
110 :    
111 :     /**
112 : dl 1.17 * tryAcquire on an released sync succeeds
113 : dl 1.1 */
114 : jsr166 1.22 public void testTryAcquire() {
115 : jsr166 1.29 Mutex rl = new Mutex();
116 : dl 1.16 assertTrue(rl.tryAcquire(1));
117 :     assertTrue(rl.isHeldExclusively());
118 :     rl.release(1);
119 : dl 1.1 }
120 :    
121 :     /**
122 :     * hasQueuedThreads reports whether there are waiting threads
123 :     */
124 : jsr166 1.26 public void testhasQueuedThreads() throws InterruptedException {
125 : jsr166 1.29 final Mutex sync = new Mutex();
126 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
127 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
128 : jsr166 1.26 assertFalse(sync.hasQueuedThreads());
129 :     sync.acquire(1);
130 :     t1.start();
131 :     Thread.sleep(SHORT_DELAY_MS);
132 :     assertTrue(sync.hasQueuedThreads());
133 :     t2.start();
134 :     Thread.sleep(SHORT_DELAY_MS);
135 :     assertTrue(sync.hasQueuedThreads());
136 :     t1.interrupt();
137 :     Thread.sleep(SHORT_DELAY_MS);
138 :     assertTrue(sync.hasQueuedThreads());
139 :     sync.release(1);
140 :     Thread.sleep(SHORT_DELAY_MS);
141 :     assertFalse(sync.hasQueuedThreads());
142 :     t1.join();
143 :     t2.join();
144 : jsr166 1.22 }
145 : dl 1.10
146 :     /**
147 : dl 1.11 * isQueued(null) throws NPE
148 :     */
149 : jsr166 1.22 public void testIsQueuedNPE() {
150 : jsr166 1.29 final Mutex sync = new Mutex();
151 : dl 1.11 try {
152 : dl 1.17 sync.isQueued(null);
153 : dl 1.11 shouldThrow();
154 : jsr166 1.26 } catch (NullPointerException success) {}
155 : dl 1.11 }
156 :    
157 :     /**
158 : dl 1.10 * isQueued reports whether a thread is queued.
159 :     */
160 : jsr166 1.26 public void testIsQueued() throws InterruptedException {
161 : jsr166 1.29 final Mutex sync = new Mutex();
162 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
163 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
164 : jsr166 1.26 assertFalse(sync.isQueued(t1));
165 :     assertFalse(sync.isQueued(t2));
166 :     sync.acquire(1);
167 :     t1.start();
168 :     Thread.sleep(SHORT_DELAY_MS);
169 :     assertTrue(sync.isQueued(t1));
170 :     t2.start();
171 :     Thread.sleep(SHORT_DELAY_MS);
172 :     assertTrue(sync.isQueued(t1));
173 :     assertTrue(sync.isQueued(t2));
174 :     t1.interrupt();
175 :     Thread.sleep(SHORT_DELAY_MS);
176 :     assertFalse(sync.isQueued(t1));
177 :     assertTrue(sync.isQueued(t2));
178 :     sync.release(1);
179 :     Thread.sleep(SHORT_DELAY_MS);
180 :     assertFalse(sync.isQueued(t1));
181 :     Thread.sleep(SHORT_DELAY_MS);
182 :     assertFalse(sync.isQueued(t2));
183 :     t1.join();
184 :     t2.join();
185 : jsr166 1.22 }
186 : dl 1.8
187 : dl 1.12 /**
188 : dl 1.17 * getFirstQueuedThread returns first waiting thread or null if none
189 : dl 1.12 */
190 : jsr166 1.26 public void testGetFirstQueuedThread() throws InterruptedException {
191 : jsr166 1.29 final Mutex sync = new Mutex();
192 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
193 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
194 : jsr166 1.26 assertNull(sync.getFirstQueuedThread());
195 :     sync.acquire(1);
196 :     t1.start();
197 :     Thread.sleep(SHORT_DELAY_MS);
198 :     assertEquals(t1, sync.getFirstQueuedThread());
199 :     t2.start();
200 :     Thread.sleep(SHORT_DELAY_MS);
201 :     assertEquals(t1, sync.getFirstQueuedThread());
202 :     t1.interrupt();
203 :     Thread.sleep(SHORT_DELAY_MS);
204 :     Thread.sleep(SHORT_DELAY_MS);
205 :     assertEquals(t2, sync.getFirstQueuedThread());
206 :     sync.release(1);
207 :     Thread.sleep(SHORT_DELAY_MS);
208 :     assertNull(sync.getFirstQueuedThread());
209 :     t1.join();
210 :     t2.join();
211 : jsr166 1.22 }
212 : dl 1.12
213 : dl 1.8
214 :     /**
215 : dl 1.12 * hasContended reports false if no thread has ever blocked, else true
216 : dl 1.8 */
217 : jsr166 1.26 public void testHasContended() throws InterruptedException {
218 : jsr166 1.29 final Mutex sync = new Mutex();
219 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
220 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
221 : jsr166 1.26 assertFalse(sync.hasContended());
222 :     sync.acquire(1);
223 :     t1.start();
224 :     Thread.sleep(SHORT_DELAY_MS);
225 :     assertTrue(sync.hasContended());
226 :     t2.start();
227 :     Thread.sleep(SHORT_DELAY_MS);
228 :     assertTrue(sync.hasContended());
229 :     t1.interrupt();
230 :     Thread.sleep(SHORT_DELAY_MS);
231 :     assertTrue(sync.hasContended());
232 :     sync.release(1);
233 :     Thread.sleep(SHORT_DELAY_MS);
234 :     assertTrue(sync.hasContended());
235 :     t1.join();
236 :     t2.join();
237 : jsr166 1.22 }
238 : dl 1.17
239 :     /**
240 :     * getQueuedThreads includes waiting threads
241 :     */
242 : jsr166 1.26 public void testGetQueuedThreads() throws InterruptedException {
243 : jsr166 1.29 final Mutex sync = new Mutex();
244 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
245 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
246 : jsr166 1.26 assertTrue(sync.getQueuedThreads().isEmpty());
247 :     sync.acquire(1);
248 :     assertTrue(sync.getQueuedThreads().isEmpty());
249 :     t1.start();
250 :     Thread.sleep(SHORT_DELAY_MS);
251 :     assertTrue(sync.getQueuedThreads().contains(t1));
252 :     t2.start();
253 :     Thread.sleep(SHORT_DELAY_MS);
254 :     assertTrue(sync.getQueuedThreads().contains(t1));
255 :     assertTrue(sync.getQueuedThreads().contains(t2));
256 :     t1.interrupt();
257 :     Thread.sleep(SHORT_DELAY_MS);
258 :     assertFalse(sync.getQueuedThreads().contains(t1));
259 :     assertTrue(sync.getQueuedThreads().contains(t2));
260 :     sync.release(1);
261 :     Thread.sleep(SHORT_DELAY_MS);
262 :     assertTrue(sync.getQueuedThreads().isEmpty());
263 :     t1.join();
264 :     t2.join();
265 : jsr166 1.22 }
266 : dl 1.17
267 :     /**
268 :     * getExclusiveQueuedThreads includes waiting threads
269 :     */
270 : jsr166 1.26 public void testGetExclusiveQueuedThreads() throws InterruptedException {
271 : jsr166 1.29 final Mutex sync = new Mutex();
272 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
273 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
274 : jsr166 1.26 assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
275 :     sync.acquire(1);
276 :     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
277 :     t1.start();
278 :     Thread.sleep(SHORT_DELAY_MS);
279 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
280 :     t2.start();
281 :     Thread.sleep(SHORT_DELAY_MS);
282 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
283 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
284 :     t1.interrupt();
285 :     Thread.sleep(SHORT_DELAY_MS);
286 :     assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
287 :     assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
288 :     sync.release(1);
289 :     Thread.sleep(SHORT_DELAY_MS);
290 :     assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
291 :     t1.join();
292 :     t2.join();
293 : jsr166 1.22 }
294 : dl 1.17
295 :     /**
296 :     * getSharedQueuedThreads does not include exclusively waiting threads
297 :     */
298 : jsr166 1.26 public void testGetSharedQueuedThreads() throws InterruptedException {
299 : jsr166 1.29 final Mutex sync = new Mutex();
300 : dl 1.17 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
301 :     Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
302 : jsr166 1.26 assertTrue(sync.getSharedQueuedThreads().isEmpty());
303 :     sync.acquire(1);
304 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
305 :     t1.start();
306 :     Thread.sleep(SHORT_DELAY_MS);
307 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
308 :     t2.start();
309 :     Thread.sleep(SHORT_DELAY_MS);
310 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
311 :     t1.interrupt();
312 :     Thread.sleep(SHORT_DELAY_MS);
313 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
314 :     sync.release(1);
315 :     Thread.sleep(SHORT_DELAY_MS);
316 :     assertTrue(sync.getSharedQueuedThreads().isEmpty());
317 :     t1.join();
318 :     t2.join();
319 : jsr166 1.22 }
320 : dl 1.1
321 :     /**
322 : dl 1.16 * tryAcquireNanos is interruptible.
323 : dl 1.1 */
324 : jsr166 1.27 public void testInterruptedException2() throws InterruptedException {
325 : jsr166 1.29 final Mutex sync = new Mutex();
326 :     sync.acquire(1);
327 :     Thread t = new Thread(new CheckedInterruptedRunnable() {
328 : jsr166 1.28 public void realRun() throws InterruptedException {
329 :     sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
330 :     }});
331 : jsr166 1.26
332 :     t.start();
333 :     t.interrupt();
334 : jsr166 1.27 t.join();
335 : dl 1.1 }
336 :    
337 :    
338 :     /**
339 : dl 1.17 * TryAcquire on exclusively held sync fails
340 : dl 1.1 */
341 : jsr166 1.26 public void testTryAcquireWhenSynced() throws InterruptedException {
342 : jsr166 1.29 final Mutex sync = new Mutex();
343 :     sync.acquire(1);
344 :     Thread t = new Thread(new CheckedRunnable() {
345 : jsr166 1.28 public void realRun() {
346 :     threadAssertFalse(sync.tryAcquire(1));
347 :     }});
348 : jsr166 1.26
349 :     t.start();
350 :     t.join();
351 :     sync.release(1);
352 : jsr166 1.22 }
353 : dl 1.1
354 :     /**
355 : dl 1.17 * tryAcquireNanos on an exclusively held sync times out
356 : dl 1.1 */
357 : jsr166 1.26 public void testAcquireNanos_Timeout() throws InterruptedException {
358 : jsr166 1.29 final Mutex sync = new Mutex();
359 :     sync.acquire(1);
360 :     Thread t = new Thread(new CheckedRunnable() {
361 : jsr166 1.28 public void realRun() throws InterruptedException {
362 :     threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
363 :     }});
364 : jsr166 1.26
365 :     t.start();
366 :     t.join();
367 :     sync.release(1);
368 : jsr166 1.22 }
369 :    
370 :    
371 : dl 1.1 /**
372 : dl 1.13 * getState is true when acquired and false when not
373 : dl 1.1 */
374 : jsr166 1.26 public void testGetState() throws InterruptedException {
375 : jsr166 1.29 final Mutex sync = new Mutex();
376 :     sync.acquire(1);
377 :     assertTrue(sync.isHeldExclusively());
378 :     sync.release(1);
379 :     assertFalse(sync.isHeldExclusively());
380 :     Thread t = new Thread(new CheckedRunnable() {
381 : jsr166 1.28 public void realRun() throws InterruptedException {
382 :     sync.acquire(1);
383 :     Thread.sleep(SMALL_DELAY_MS);
384 :     sync.release(1);
385 :     }});
386 : jsr166 1.26
387 :     t.start();
388 :     Thread.sleep(SHORT_DELAY_MS);
389 :     assertTrue(sync.isHeldExclusively());
390 :     t.join();
391 :     assertFalse(sync.isHeldExclusively());
392 : dl 1.1 }
393 :    
394 :    
395 :     /**
396 : dl 1.16 * acquireInterruptibly is interruptible.
397 : dl 1.1 */
398 : jsr166 1.26 public void testAcquireInterruptibly1() throws InterruptedException {
399 : jsr166 1.29 final Mutex sync = new Mutex();
400 :     sync.acquire(1);
401 :     Thread t = new Thread(new InterruptedSyncRunnable(sync));
402 : jsr166 1.26
403 :     t.start();
404 :     Thread.sleep(SHORT_DELAY_MS);
405 :     t.interrupt();
406 :     Thread.sleep(SHORT_DELAY_MS);
407 :     sync.release(1);
408 :     t.join();
409 : jsr166 1.22 }
410 : dl 1.1
411 :     /**
412 : dl 1.16 * acquireInterruptibly succeeds when released, else is interruptible
413 : dl 1.1 */
414 : jsr166 1.26 public void testAcquireInterruptibly2() throws InterruptedException {
415 : jsr166 1.29 final Mutex sync = new Mutex();
416 : jsr166 1.26 sync.acquireInterruptibly(1);
417 : jsr166 1.29 Thread t = new Thread(new InterruptedSyncRunnable(sync));
418 : jsr166 1.26 t.start();
419 :     t.interrupt();
420 :     assertTrue(sync.isHeldExclusively());
421 :     t.join();
422 : dl 1.1 }
423 :    
424 :     /**
425 : dl 1.17 * owns is true for a condition created by sync else false
426 : dl 1.12 */
427 :     public void testOwns() {
428 : jsr166 1.29 final Mutex sync = new Mutex();
429 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
430 :     final Mutex sync2 = new Mutex();
431 :     assertTrue(sync.owns(c));
432 :     assertFalse(sync2.owns(c));
433 : dl 1.12 }
434 :    
435 :     /**
436 : dl 1.17 * Calling await without holding sync throws IllegalMonitorStateException
437 : dl 1.1 */
438 : jsr166 1.26 public void testAwait_IllegalMonitor() throws InterruptedException {
439 : jsr166 1.29 final Mutex sync = new Mutex();
440 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
441 : dl 1.1 try {
442 :     c.await();
443 :     shouldThrow();
444 : jsr166 1.28 } catch (IllegalMonitorStateException success) {}
445 : dl 1.1 }
446 :    
447 :     /**
448 : dl 1.17 * Calling signal without holding sync throws IllegalMonitorStateException
449 : dl 1.1 */
450 :     public void testSignal_IllegalMonitor() {
451 : jsr166 1.29 final Mutex sync = new Mutex();
452 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
453 : dl 1.1 try {
454 :     c.signal();
455 :     shouldThrow();
456 : jsr166 1.28 } catch (IllegalMonitorStateException success) {}
457 : dl 1.1 }
458 :    
459 :     /**
460 :     * awaitNanos without a signal times out
461 :     */
462 : jsr166 1.26 public void testAwaitNanos_Timeout() throws InterruptedException {
463 : jsr166 1.29 final Mutex sync = new Mutex();
464 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
465 : jsr166 1.26 sync.acquire(1);
466 :     long t = c.awaitNanos(100);
467 :     assertTrue(t <= 0);
468 :     sync.release(1);
469 : dl 1.1 }
470 :    
471 :     /**
472 : dl 1.17 * Timed await without a signal times out
473 : dl 1.1 */
474 : jsr166 1.26 public void testAwait_Timeout() throws InterruptedException {
475 : jsr166 1.29 final Mutex sync = new Mutex();
476 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
477 : jsr166 1.26 sync.acquire(1);
478 :     assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
479 :     sync.release(1);
480 : dl 1.1 }
481 :    
482 :     /**
483 :     * awaitUntil without a signal times out
484 :     */
485 : jsr166 1.26 public void testAwaitUntil_Timeout() throws InterruptedException {
486 : jsr166 1.29 final Mutex sync = new Mutex();
487 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
488 : jsr166 1.26 sync.acquire(1);
489 :     java.util.Date d = new java.util.Date();
490 :     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
491 :     sync.release(1);
492 : dl 1.1 }
493 :    
494 :     /**
495 :     * await returns when signalled
496 :     */
497 : jsr166 1.26 public void testAwait() throws InterruptedException {
498 : jsr166 1.29 final Mutex sync = new Mutex();
499 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
500 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
501 : jsr166 1.28 public void realRun() throws InterruptedException {
502 :     sync.acquire(1);
503 :     c.await();
504 :     sync.release(1);
505 :     }});
506 : dl 1.15
507 : jsr166 1.26 t.start();
508 :     Thread.sleep(SHORT_DELAY_MS);
509 :     sync.acquire(1);
510 :     c.signal();
511 :     sync.release(1);
512 :     t.join(SHORT_DELAY_MS);
513 :     assertFalse(t.isAlive());
514 : dl 1.15 }
515 :    
516 :    
517 :    
518 :     /**
519 :     * hasWaiters throws NPE if null
520 :     */
521 :     public void testHasWaitersNPE() {
522 : jsr166 1.29 final Mutex sync = new Mutex();
523 : dl 1.15 try {
524 : dl 1.17 sync.hasWaiters(null);
525 : dl 1.15 shouldThrow();
526 : jsr166 1.26 } catch (NullPointerException success) {}
527 : dl 1.15 }
528 :    
529 :     /**
530 :     * getWaitQueueLength throws NPE if null
531 :     */
532 :     public void testGetWaitQueueLengthNPE() {
533 : jsr166 1.29 final Mutex sync = new Mutex();
534 : dl 1.15 try {
535 : dl 1.17 sync.getWaitQueueLength(null);
536 : dl 1.15 shouldThrow();
537 : jsr166 1.26 } catch (NullPointerException success) {}
538 : dl 1.15 }
539 :    
540 :    
541 :     /**
542 :     * getWaitingThreads throws NPE if null
543 :     */
544 :     public void testGetWaitingThreadsNPE() {
545 : jsr166 1.29 final Mutex sync = new Mutex();
546 : dl 1.15 try {
547 : dl 1.17 sync.getWaitingThreads(null);
548 : dl 1.15 shouldThrow();
549 : jsr166 1.26 } catch (NullPointerException success) {}
550 : dl 1.15 }
551 :    
552 :    
553 :     /**
554 :     * hasWaiters throws IAE if not owned
555 :     */
556 :     public void testHasWaitersIAE() {
557 : jsr166 1.29 final Mutex sync = new Mutex();
558 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
559 : jsr166 1.29 final Mutex sync2 = new Mutex();
560 : dl 1.15 try {
561 : dl 1.17 sync2.hasWaiters(c);
562 : dl 1.15 shouldThrow();
563 : jsr166 1.26 } catch (IllegalArgumentException success) {}
564 : dl 1.15 }
565 :    
566 :     /**
567 : dl 1.17 * hasWaiters throws IMSE if not synced
568 : dl 1.15 */
569 :     public void testHasWaitersIMSE() {
570 : jsr166 1.29 final Mutex sync = new Mutex();
571 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
572 : dl 1.15 try {
573 : dl 1.17 sync.hasWaiters(c);
574 : dl 1.15 shouldThrow();
575 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
576 : dl 1.15 }
577 :    
578 :    
579 :     /**
580 :     * getWaitQueueLength throws IAE if not owned
581 :     */
582 :     public void testGetWaitQueueLengthIAE() {
583 : jsr166 1.29 final Mutex sync = new Mutex();
584 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
585 : jsr166 1.29 final Mutex sync2 = new Mutex();
586 : dl 1.15 try {
587 : dl 1.17 sync2.getWaitQueueLength(c);
588 : dl 1.15 shouldThrow();
589 : jsr166 1.26 } catch (IllegalArgumentException success) {}
590 : dl 1.15 }
591 :    
592 :     /**
593 : dl 1.17 * getWaitQueueLength throws IMSE if not synced
594 : dl 1.15 */
595 :     public void testGetWaitQueueLengthIMSE() {
596 : jsr166 1.29 final Mutex sync = new Mutex();
597 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
598 : dl 1.15 try {
599 : dl 1.17 sync.getWaitQueueLength(c);
600 : dl 1.15 shouldThrow();
601 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
602 : dl 1.15 }
603 :    
604 :    
605 :     /**
606 :     * getWaitingThreads throws IAE if not owned
607 :     */
608 :     public void testGetWaitingThreadsIAE() {
609 : jsr166 1.29 final Mutex sync = new Mutex();
610 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
611 : jsr166 1.29 final Mutex sync2 = new Mutex();
612 : dl 1.15 try {
613 : dl 1.17 sync2.getWaitingThreads(c);
614 : dl 1.15 shouldThrow();
615 : jsr166 1.26 } catch (IllegalArgumentException success) {}
616 : dl 1.15 }
617 :    
618 :     /**
619 : dl 1.17 * getWaitingThreads throws IMSE if not synced
620 : dl 1.15 */
621 :     public void testGetWaitingThreadsIMSE() {
622 : jsr166 1.29 final Mutex sync = new Mutex();
623 : jsr166 1.25 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
624 : dl 1.15 try {
625 : dl 1.17 sync.getWaitingThreads(c);
626 : dl 1.15 shouldThrow();
627 : jsr166 1.26 } catch (IllegalMonitorStateException success) {}
628 : dl 1.15 }
629 :    
630 :    
631 :    
632 :     /**
633 :     * hasWaiters returns true when a thread is waiting, else false
634 :     */
635 : jsr166 1.26 public void testHasWaiters() throws InterruptedException {
636 : jsr166 1.29 final Mutex sync = new Mutex();
637 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
638 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
639 : jsr166 1.28 public void realRun() throws InterruptedException {
640 :     sync.acquire(1);
641 :     threadAssertFalse(sync.hasWaiters(c));
642 :     threadAssertEquals(0, sync.getWaitQueueLength(c));
643 :     c.await();
644 :     sync.release(1);
645 :     }});
646 : dl 1.15
647 : jsr166 1.26 t.start();
648 :     Thread.sleep(SHORT_DELAY_MS);
649 :     sync.acquire(1);
650 :     assertTrue(sync.hasWaiters(c));
651 :     assertEquals(1, sync.getWaitQueueLength(c));
652 :     c.signal();
653 :     sync.release(1);
654 :     Thread.sleep(SHORT_DELAY_MS);
655 :     sync.acquire(1);
656 :     assertFalse(sync.hasWaiters(c));
657 :     assertEquals(0, sync.getWaitQueueLength(c));
658 :     sync.release(1);
659 :     t.join(SHORT_DELAY_MS);
660 :     assertFalse(t.isAlive());
661 : dl 1.15 }
662 :    
663 :     /**
664 :     * getWaitQueueLength returns number of waiting threads
665 :     */
666 : jsr166 1.26 public void testGetWaitQueueLength() throws InterruptedException {
667 : jsr166 1.29 final Mutex sync = new Mutex();
668 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
669 : jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
670 : jsr166 1.28 public void realRun() throws InterruptedException {
671 :     sync.acquire(1);
672 :     threadAssertFalse(sync.hasWaiters(c));
673 :     threadAssertEquals(0, sync.getWaitQueueLength(c));
674 :     c.await();
675 :     sync.release(1);
676 :     }});
677 :    
678 : jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
679 : jsr166 1.28 public void realRun() throws InterruptedException {
680 :     sync.acquire(1);
681 :     threadAssertTrue(sync.hasWaiters(c));
682 :     threadAssertEquals(1, sync.getWaitQueueLength(c));
683 :     c.await();
684 :     sync.release(1);
685 :     }});
686 : dl 1.15
687 : jsr166 1.26 t1.start();
688 :     Thread.sleep(SHORT_DELAY_MS);
689 :     t2.start();
690 :     Thread.sleep(SHORT_DELAY_MS);
691 :     sync.acquire(1);
692 :     assertTrue(sync.hasWaiters(c));
693 :     assertEquals(2, sync.getWaitQueueLength(c));
694 :     c.signalAll();
695 :     sync.release(1);
696 :     Thread.sleep(SHORT_DELAY_MS);
697 :     sync.acquire(1);
698 :     assertFalse(sync.hasWaiters(c));
699 :     assertEquals(0, sync.getWaitQueueLength(c));
700 :     sync.release(1);
701 :     t1.join(SHORT_DELAY_MS);
702 :     t2.join(SHORT_DELAY_MS);
703 :     assertFalse(t1.isAlive());
704 :     assertFalse(t2.isAlive());
705 : dl 1.15 }
706 :    
707 :     /**
708 :     * getWaitingThreads returns only and all waiting threads
709 :     */
710 : jsr166 1.26 public void testGetWaitingThreads() throws InterruptedException {
711 : jsr166 1.29 final Mutex sync = new Mutex();
712 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
713 : jsr166 1.29 Thread t1 = new Thread(new CheckedRunnable() {
714 : jsr166 1.28 public void realRun() throws InterruptedException {
715 :     sync.acquire(1);
716 :     threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
717 :     c.await();
718 :     sync.release(1);
719 :     }});
720 :    
721 : jsr166 1.29 Thread t2 = new Thread(new CheckedRunnable() {
722 : jsr166 1.28 public void realRun() throws InterruptedException {
723 :     sync.acquire(1);
724 :     threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
725 :     c.await();
726 :     sync.release(1);
727 :     }});
728 : dl 1.1
729 : jsr166 1.26 sync.acquire(1);
730 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
731 :     sync.release(1);
732 :     t1.start();
733 :     Thread.sleep(SHORT_DELAY_MS);
734 :     t2.start();
735 :     Thread.sleep(SHORT_DELAY_MS);
736 :     sync.acquire(1);
737 :     assertTrue(sync.hasWaiters(c));
738 :     assertTrue(sync.getWaitingThreads(c).contains(t1));
739 :     assertTrue(sync.getWaitingThreads(c).contains(t2));
740 :     c.signalAll();
741 :     sync.release(1);
742 :     Thread.sleep(SHORT_DELAY_MS);
743 :     sync.acquire(1);
744 :     assertFalse(sync.hasWaiters(c));
745 :     assertTrue(sync.getWaitingThreads(c).isEmpty());
746 :     sync.release(1);
747 :     t1.join(SHORT_DELAY_MS);
748 :     t2.join(SHORT_DELAY_MS);
749 :     assertFalse(t1.isAlive());
750 :     assertFalse(t2.isAlive());
751 : dl 1.15 }
752 :    
753 :    
754 :    
755 :     /**
756 :     * awaitUninterruptibly doesn't abort on interrupt
757 :     */
758 : jsr166 1.26 public void testAwaitUninterruptibly() throws InterruptedException {
759 : jsr166 1.29 final Mutex sync = new Mutex();
760 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
761 : jsr166 1.29 Thread t = new Thread(new CheckedRunnable() {
762 : jsr166 1.28 public void realRun() {
763 :     sync.acquire(1);
764 :     c.awaitUninterruptibly();
765 :     sync.release(1);
766 :     }});
767 : dl 1.15
768 : jsr166 1.26 t.start();
769 :     Thread.sleep(SHORT_DELAY_MS);
770 :     t.interrupt();
771 :     sync.acquire(1);
772 :     c.signal();
773 :     sync.release(1);
774 :     t.join(SHORT_DELAY_MS);
775 :     assertFalse(t.isAlive());
776 : dl 1.15 }
777 :    
778 :     /**
779 :     * await is interruptible
780 :     */
781 : jsr166 1.26 public void testAwait_Interrupt() throws InterruptedException {
782 : jsr166 1.29 final Mutex sync = new Mutex();
783 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
784 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
785 :     public void realRun() throws InterruptedException {
786 : jsr166 1.28 sync.acquire(1);
787 :     c.await();
788 :     sync.release(1);
789 :     }});
790 : dl 1.15
791 : jsr166 1.26 t.start();
792 :     Thread.sleep(SHORT_DELAY_MS);
793 :     t.interrupt();
794 :     t.join(SHORT_DELAY_MS);
795 :     assertFalse(t.isAlive());
796 : dl 1.15 }
797 :    
798 :     /**
799 :     * awaitNanos is interruptible
800 :     */
801 : jsr166 1.26 public void testAwaitNanos_Interrupt() throws InterruptedException {
802 : jsr166 1.29 final Mutex sync = new Mutex();
803 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
804 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
805 :     public void realRun() throws InterruptedException {
806 : jsr166 1.28 sync.acquire(1);
807 :     c.awaitNanos(1000 * 1000 * 1000); // 1 sec
808 :     sync.release(1);
809 :     }});
810 : dl 1.15
811 : jsr166 1.26 t.start();
812 :     Thread.sleep(SHORT_DELAY_MS);
813 :     t.interrupt();
814 :     t.join(SHORT_DELAY_MS);
815 :     assertFalse(t.isAlive());
816 : dl 1.15 }
817 :    
818 :     /**
819 :     * awaitUntil is interruptible
820 :     */
821 : jsr166 1.26 public void testAwaitUntil_Interrupt() throws InterruptedException {
822 : jsr166 1.29 final Mutex sync = new Mutex();
823 : dl 1.17 final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
824 : jsr166 1.29 Thread t = new Thread(new CheckedInterruptedRunnable() {
825 :     public void realRun() throws InterruptedException {
826 : jsr166 1.28 sync.acquire(1);
827 :     java.util.Date d = new java.util.Date();
828 :     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
829 :     sync.release(1);
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 :     threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
958 :     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 :     l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
995 :     }});
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 :     threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1013 :     }});
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