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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8