ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.9
Committed: Wed Nov 18 08:22:57 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +317 -483 lines
Log Message:
nicer exception handling

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