ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.14
Committed: Mon Nov 30 08:31:09 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +8 -6 lines
Log Message:
replace absolute waits with _DELAY_MS; 1000 => 1000L; short delay after starting a thread before interrupting it

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 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 jsr166 1.14 sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000L * 1000L);
334 jsr166 1.10 }});
335 jsr166 1.9
336     t.start();
337 jsr166 1.14 Thread.sleep(SHORT_DELAY_MS);
338 jsr166 1.9 t.interrupt();
339     t.join();
340 dl 1.1 }
341    
342    
343     /**
344     * TryAcquire on exclusively held sync fails
345     */
346 jsr166 1.9 public void testTryAcquireWhenSynced() throws InterruptedException {
347 jsr166 1.11 final Mutex sync = new Mutex();
348     sync.acquire(1);
349     Thread t = new Thread(new CheckedRunnable() {
350 jsr166 1.10 public void realRun() {
351     threadAssertFalse(sync.tryAcquire(1));
352     }});
353 jsr166 1.9
354     t.start();
355     t.join();
356     sync.release(1);
357 jsr166 1.5 }
358 dl 1.1
359     /**
360     * tryAcquireNanos on an exclusively held sync times out
361     */
362 jsr166 1.9 public void testAcquireNanos_Timeout() throws InterruptedException {
363 jsr166 1.11 final Mutex sync = new Mutex();
364     sync.acquire(1);
365     Thread t = new Thread(new CheckedRunnable() {
366 jsr166 1.10 public void realRun() throws InterruptedException {
367 jsr166 1.14 threadAssertFalse(sync.tryAcquireNanos(1, SHORT_DELAY_MS * 1000L * 1000L));
368 jsr166 1.10 }});
369 jsr166 1.9
370     t.start();
371     t.join();
372     sync.release(1);
373 jsr166 1.5 }
374    
375    
376 dl 1.1 /**
377     * getState is true when acquired and false when not
378     */
379 jsr166 1.9 public void testGetState() throws InterruptedException {
380 jsr166 1.11 final Mutex sync = new Mutex();
381     sync.acquire(1);
382     assertTrue(sync.isHeldExclusively());
383     sync.release(1);
384     assertFalse(sync.isHeldExclusively());
385     Thread t = new Thread(new CheckedRunnable() {
386 jsr166 1.10 public void realRun() throws InterruptedException {
387     sync.acquire(1);
388     Thread.sleep(SMALL_DELAY_MS);
389     sync.release(1);
390     }});
391 jsr166 1.9
392     t.start();
393     Thread.sleep(SHORT_DELAY_MS);
394     assertTrue(sync.isHeldExclusively());
395     t.join();
396     assertFalse(sync.isHeldExclusively());
397 dl 1.1 }
398    
399    
400     /**
401     * acquireInterruptibly is interruptible.
402     */
403 jsr166 1.9 public void testAcquireInterruptibly1() throws InterruptedException {
404 jsr166 1.11 final Mutex sync = new Mutex();
405     sync.acquire(1);
406     Thread t = new Thread(new InterruptedSyncRunnable(sync));
407 jsr166 1.9 t.start();
408     Thread.sleep(SHORT_DELAY_MS);
409     t.interrupt();
410     Thread.sleep(SHORT_DELAY_MS);
411     sync.release(1);
412     t.join();
413 jsr166 1.5 }
414 dl 1.1
415     /**
416     * acquireInterruptibly succeeds when released, else is interruptible
417     */
418 jsr166 1.9 public void testAcquireInterruptibly2() throws InterruptedException {
419 jsr166 1.11 final Mutex sync = new Mutex();
420 jsr166 1.9 sync.acquireInterruptibly(1);
421 jsr166 1.11 Thread t = new Thread(new InterruptedSyncRunnable(sync));
422 jsr166 1.9 t.start();
423 jsr166 1.14 Thread.sleep(SHORT_DELAY_MS);
424 jsr166 1.9 t.interrupt();
425     assertTrue(sync.isHeldExclusively());
426     t.join();
427 dl 1.1 }
428    
429     /**
430     * owns is true for a condition created by sync else false
431     */
432     public void testOwns() {
433 jsr166 1.11 final Mutex sync = new Mutex();
434 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
435     final Mutex sync2 = new Mutex();
436     assertTrue(sync.owns(c));
437     assertFalse(sync2.owns(c));
438     }
439    
440     /**
441     * Calling await without holding sync throws IllegalMonitorStateException
442     */
443 jsr166 1.9 public void testAwait_IllegalMonitor() throws InterruptedException {
444 jsr166 1.11 final Mutex sync = new Mutex();
445 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
446     try {
447     c.await();
448     shouldThrow();
449 jsr166 1.10 } catch (IllegalMonitorStateException success) {}
450 dl 1.1 }
451    
452     /**
453     * Calling signal without holding sync throws IllegalMonitorStateException
454     */
455 jsr166 1.9 public void testSignal_IllegalMonitor() throws InterruptedException {
456 jsr166 1.11 final Mutex sync = new Mutex();
457 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
458     try {
459     c.signal();
460     shouldThrow();
461 jsr166 1.10 } catch (IllegalMonitorStateException success) {}
462 dl 1.1 }
463    
464     /**
465     * awaitNanos without a signal times out
466     */
467 jsr166 1.9 public void testAwaitNanos_Timeout() throws InterruptedException {
468 jsr166 1.11 final Mutex sync = new Mutex();
469 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
470 jsr166 1.9 sync.acquire(1);
471     long t = c.awaitNanos(100);
472     assertTrue(t <= 0);
473     sync.release(1);
474 dl 1.1 }
475    
476     /**
477     * Timed await without a signal times out
478     */
479 jsr166 1.9 public void testAwait_Timeout() throws InterruptedException {
480 jsr166 1.11 final Mutex sync = new Mutex();
481 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
482 jsr166 1.9 sync.acquire(1);
483 jsr166 1.12 assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
484 jsr166 1.9 sync.release(1);
485 dl 1.1 }
486    
487     /**
488     * awaitUntil without a signal times out
489     */
490 jsr166 1.9 public void testAwaitUntil_Timeout() throws InterruptedException {
491 jsr166 1.11 final Mutex sync = new Mutex();
492 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
493 jsr166 1.9 sync.acquire(1);
494     java.util.Date d = new java.util.Date();
495     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
496     sync.release(1);
497 dl 1.1 }
498    
499     /**
500     * await returns when signalled
501     */
502 jsr166 1.9 public void testAwait() throws InterruptedException {
503 jsr166 1.11 final Mutex sync = new Mutex();
504 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
505 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
506 jsr166 1.10 public void realRun() throws InterruptedException {
507     sync.acquire(1);
508     c.await();
509     sync.release(1);
510     }});
511 dl 1.1
512 jsr166 1.9 t.start();
513     Thread.sleep(SHORT_DELAY_MS);
514     sync.acquire(1);
515     c.signal();
516     sync.release(1);
517     t.join(SHORT_DELAY_MS);
518     assertFalse(t.isAlive());
519 dl 1.1 }
520    
521    
522    
523     /**
524     * hasWaiters throws NPE if null
525     */
526     public void testHasWaitersNPE() {
527 jsr166 1.11 final Mutex sync = new Mutex();
528 dl 1.1 try {
529     sync.hasWaiters(null);
530     shouldThrow();
531 jsr166 1.9 } catch (NullPointerException success) {}
532 dl 1.1 }
533    
534     /**
535     * getWaitQueueLength throws NPE if null
536     */
537     public void testGetWaitQueueLengthNPE() {
538 jsr166 1.11 final Mutex sync = new Mutex();
539 dl 1.1 try {
540     sync.getWaitQueueLength(null);
541     shouldThrow();
542 jsr166 1.9 } catch (NullPointerException success) {}
543 dl 1.1 }
544    
545    
546     /**
547     * getWaitingThreads throws NPE if null
548     */
549     public void testGetWaitingThreadsNPE() {
550 jsr166 1.11 final Mutex sync = new Mutex();
551 dl 1.1 try {
552     sync.getWaitingThreads(null);
553     shouldThrow();
554 jsr166 1.9 } catch (NullPointerException success) {}
555 dl 1.1 }
556    
557    
558     /**
559     * hasWaiters throws IAE if not owned
560     */
561     public void testHasWaitersIAE() {
562 jsr166 1.11 final Mutex sync = new Mutex();
563 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
564 jsr166 1.11 final Mutex sync2 = new Mutex();
565 dl 1.1 try {
566     sync2.hasWaiters(c);
567     shouldThrow();
568 jsr166 1.9 } catch (IllegalArgumentException success) {}
569 dl 1.1 }
570    
571     /**
572     * hasWaiters throws IMSE if not synced
573     */
574     public void testHasWaitersIMSE() {
575 jsr166 1.11 final Mutex sync = new Mutex();
576 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
577 dl 1.1 try {
578     sync.hasWaiters(c);
579     shouldThrow();
580 jsr166 1.9 } catch (IllegalMonitorStateException success) {}
581 dl 1.1 }
582    
583    
584     /**
585     * getWaitQueueLength throws IAE if not owned
586     */
587     public void testGetWaitQueueLengthIAE() {
588 jsr166 1.11 final Mutex sync = new Mutex();
589 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
590 jsr166 1.11 final Mutex sync2 = new Mutex();
591 dl 1.1 try {
592     sync2.getWaitQueueLength(c);
593     shouldThrow();
594 jsr166 1.9 } catch (IllegalArgumentException success) {}
595 dl 1.1 }
596    
597     /**
598     * getWaitQueueLength throws IMSE if not synced
599     */
600     public void testGetWaitQueueLengthIMSE() {
601 jsr166 1.11 final Mutex sync = new Mutex();
602 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
603 dl 1.1 try {
604     sync.getWaitQueueLength(c);
605     shouldThrow();
606 jsr166 1.9 } catch (IllegalMonitorStateException success) {}
607 dl 1.1 }
608    
609    
610     /**
611     * getWaitingThreads throws IAE if not owned
612     */
613     public void testGetWaitingThreadsIAE() {
614 jsr166 1.11 final Mutex sync = new Mutex();
615 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
616 jsr166 1.11 final Mutex sync2 = new Mutex();
617 dl 1.1 try {
618     sync2.getWaitingThreads(c);
619     shouldThrow();
620 jsr166 1.9 } catch (IllegalArgumentException success) {}
621 dl 1.1 }
622    
623     /**
624     * getWaitingThreads throws IMSE if not synced
625     */
626     public void testGetWaitingThreadsIMSE() {
627 jsr166 1.11 final Mutex sync = new Mutex();
628 jsr166 1.8 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
629 dl 1.1 try {
630     sync.getWaitingThreads(c);
631     shouldThrow();
632 jsr166 1.9 } catch (IllegalMonitorStateException success) {}
633 dl 1.1 }
634    
635    
636    
637     /**
638     * hasWaiters returns true when a thread is waiting, else false
639     */
640 jsr166 1.9 public void testHasWaiters() throws InterruptedException {
641 jsr166 1.11 final Mutex sync = new Mutex();
642 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
643 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
644 jsr166 1.10 public void realRun() throws InterruptedException {
645     sync.acquire(1);
646     threadAssertFalse(sync.hasWaiters(c));
647     threadAssertEquals(0, sync.getWaitQueueLength(c));
648     c.await();
649     sync.release(1);
650     }});
651 dl 1.1
652 jsr166 1.9 t.start();
653     Thread.sleep(SHORT_DELAY_MS);
654     sync.acquire(1);
655     assertTrue(sync.hasWaiters(c));
656     assertEquals(1, sync.getWaitQueueLength(c));
657     c.signal();
658     sync.release(1);
659     Thread.sleep(SHORT_DELAY_MS);
660     sync.acquire(1);
661     assertFalse(sync.hasWaiters(c));
662     assertEquals(0, sync.getWaitQueueLength(c));
663     sync.release(1);
664     t.join(SHORT_DELAY_MS);
665     assertFalse(t.isAlive());
666 dl 1.1 }
667    
668     /**
669     * getWaitQueueLength returns number of waiting threads
670     */
671 jsr166 1.9 public void testGetWaitQueueLength() throws InterruptedException {
672 jsr166 1.11 final Mutex sync = new Mutex();
673 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
674 jsr166 1.11 Thread t1 = new Thread(new CheckedRunnable() {
675 jsr166 1.10 public void realRun() throws InterruptedException {
676     sync.acquire(1);
677     threadAssertFalse(sync.hasWaiters(c));
678     threadAssertEquals(0, sync.getWaitQueueLength(c));
679     c.await();
680     sync.release(1);
681     }});
682    
683 jsr166 1.11 Thread t2 = new Thread(new CheckedRunnable() {
684 jsr166 1.10 public void realRun() throws InterruptedException {
685     sync.acquire(1);
686     threadAssertTrue(sync.hasWaiters(c));
687     threadAssertEquals(1, sync.getWaitQueueLength(c));
688     c.await();
689     sync.release(1);
690     }});
691 dl 1.1
692 jsr166 1.9 t1.start();
693     Thread.sleep(SHORT_DELAY_MS);
694     t2.start();
695     Thread.sleep(SHORT_DELAY_MS);
696     sync.acquire(1);
697     assertTrue(sync.hasWaiters(c));
698     assertEquals(2, sync.getWaitQueueLength(c));
699     c.signalAll();
700     sync.release(1);
701     Thread.sleep(SHORT_DELAY_MS);
702     sync.acquire(1);
703     assertFalse(sync.hasWaiters(c));
704     assertEquals(0, sync.getWaitQueueLength(c));
705     sync.release(1);
706     t1.join(SHORT_DELAY_MS);
707     t2.join(SHORT_DELAY_MS);
708     assertFalse(t1.isAlive());
709     assertFalse(t2.isAlive());
710 dl 1.1 }
711    
712     /**
713     * getWaitingThreads returns only and all waiting threads
714     */
715 jsr166 1.9 public void testGetWaitingThreads() throws InterruptedException {
716 jsr166 1.11 final Mutex sync = new Mutex();
717 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
718 jsr166 1.11 Thread t1 = new Thread(new CheckedRunnable() {
719 jsr166 1.10 public void realRun() throws InterruptedException {
720     sync.acquire(1);
721     threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
722     c.await();
723     sync.release(1);
724     }});
725    
726 jsr166 1.11 Thread t2 = new Thread(new CheckedRunnable() {
727 jsr166 1.10 public void realRun() throws InterruptedException {
728     sync.acquire(1);
729     threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
730     c.await();
731     sync.release(1);
732     }});
733 dl 1.1
734     sync.acquire(1);
735     assertTrue(sync.getWaitingThreads(c).isEmpty());
736     sync.release(1);
737     t1.start();
738     Thread.sleep(SHORT_DELAY_MS);
739     t2.start();
740     Thread.sleep(SHORT_DELAY_MS);
741     sync.acquire(1);
742     assertTrue(sync.hasWaiters(c));
743     assertTrue(sync.getWaitingThreads(c).contains(t1));
744     assertTrue(sync.getWaitingThreads(c).contains(t2));
745     c.signalAll();
746     sync.release(1);
747     Thread.sleep(SHORT_DELAY_MS);
748     sync.acquire(1);
749     assertFalse(sync.hasWaiters(c));
750     assertTrue(sync.getWaitingThreads(c).isEmpty());
751     sync.release(1);
752     t1.join(SHORT_DELAY_MS);
753     t2.join(SHORT_DELAY_MS);
754     assertFalse(t1.isAlive());
755     assertFalse(t2.isAlive());
756     }
757    
758    
759    
760     /**
761     * awaitUninterruptibly doesn't abort on interrupt
762     */
763 jsr166 1.9 public void testAwaitUninterruptibly() throws InterruptedException {
764 jsr166 1.11 final Mutex sync = new Mutex();
765 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
766 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
767 jsr166 1.10 public void realRun() {
768     sync.acquire(1);
769     c.awaitUninterruptibly();
770     sync.release(1);
771     }});
772 dl 1.1
773 jsr166 1.9 t.start();
774     Thread.sleep(SHORT_DELAY_MS);
775     t.interrupt();
776     sync.acquire(1);
777     c.signal();
778     sync.release(1);
779     t.join(SHORT_DELAY_MS);
780     assertFalse(t.isAlive());
781 dl 1.1 }
782    
783     /**
784     * await is interruptible
785     */
786 jsr166 1.9 public void testAwait_Interrupt() throws InterruptedException {
787 jsr166 1.11 final Mutex sync = new Mutex();
788 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
789 jsr166 1.11 Thread t = new Thread(new CheckedInterruptedRunnable() {
790     public void realRun() throws InterruptedException {
791 jsr166 1.10 sync.acquire(1);
792     c.await();
793     }});
794 dl 1.1
795 jsr166 1.9 t.start();
796     Thread.sleep(SHORT_DELAY_MS);
797     t.interrupt();
798     t.join(SHORT_DELAY_MS);
799     assertFalse(t.isAlive());
800 dl 1.1 }
801    
802     /**
803     * awaitNanos is interruptible
804     */
805 jsr166 1.9 public void testAwaitNanos_Interrupt() throws InterruptedException {
806 jsr166 1.11 final Mutex sync = new Mutex();
807 dl 1.1 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
808 jsr166 1.11 Thread t = new Thread(new CheckedInterruptedRunnable() {
809     public void realRun() throws InterruptedException {
810 jsr166 1.10 sync.acquire(1);
811 jsr166 1.14 c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L);
812 jsr166 1.10 }});
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     }});
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 jsr166 1.14 threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS * 1000L * 1000L));
960 jsr166 1.10 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 jsr166 1.14 l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L);
997 jsr166 1.10 }});
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 jsr166 1.14 threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L));
1015 jsr166 1.10 }});
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     }