ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.11
Committed: Sat Nov 21 02:07:26 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +93 -93 lines
Log Message:
untabify

File Contents

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