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

# Content
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 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
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 public boolean isHeldExclusively() {
36 return getState() == LOCKED;
37 }
38
39 public boolean tryAcquire(long acquires) {
40 return compareAndSetState(0, LOCKED);
41 }
42
43 public boolean tryRelease(long releases) {
44 if (getState() == 0) throw new IllegalMonitorStateException();
45 setState(0);
46 return true;
47 }
48
49 public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); }
50
51 }
52
53
54 /**
55 * A simple latch class, to test shared mode.
56 */
57 static class BooleanLatch extends AbstractQueuedLongSynchronizer {
58 public boolean isSignalled() { return getState() != 0; }
59
60 public long tryAcquireShared(long ignore) {
61 return isSignalled()? 1 : -1;
62 }
63
64 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 } catch (InterruptedException success) {}
80 }
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 } catch (InterruptedException success) {}
96 }
97 }
98
99 /**
100 * isHeldExclusively is false upon construction
101 */
102 public void testIsHeldExclusively() {
103 Mutex rl = new Mutex();
104 assertFalse(rl.isHeldExclusively());
105 }
106
107 /**
108 * acquiring released sync succeeds
109 */
110 public void testAcquire() {
111 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 public void testTryAcquire() {
122 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 public void testhasQueuedThreads() throws InterruptedException {
132 final Mutex sync = new Mutex();
133 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
134 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
135 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 }
152
153 /**
154 * isQueued(null) throws NPE
155 */
156 public void testIsQueuedNPE() {
157 final Mutex sync = new Mutex();
158 try {
159 sync.isQueued(null);
160 shouldThrow();
161 } catch (NullPointerException success) {}
162 }
163
164 /**
165 * isQueued reports whether a thread is queued.
166 */
167 public void testIsQueued() throws InterruptedException {
168 final Mutex sync = new Mutex();
169 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
170 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
171 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 }
193
194 /**
195 * getFirstQueuedThread returns first waiting thread or null if none
196 */
197 public void testGetFirstQueuedThread() throws InterruptedException {
198 final Mutex sync = new Mutex();
199 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
200 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
201 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 }
219
220
221 /**
222 * hasContended reports false if no thread has ever blocked, else true
223 */
224 public void testHasContended() throws InterruptedException {
225 final Mutex sync = new Mutex();
226 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
227 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
228 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 }
245
246 /**
247 * getQueuedThreads includes waiting threads
248 */
249 public void testGetQueuedThreads() throws InterruptedException {
250 final Mutex sync = new Mutex();
251 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
252 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
253 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 }
273
274 /**
275 * getExclusiveQueuedThreads includes waiting threads
276 */
277 public void testGetExclusiveQueuedThreads() throws InterruptedException {
278 final Mutex sync = new Mutex();
279 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
280 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
281 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 }
301
302 /**
303 * getSharedQueuedThreads does not include exclusively waiting threads
304 */
305 public void testGetSharedQueuedThreads() throws InterruptedException {
306 final Mutex sync = new Mutex();
307 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
308 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
309 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 }
327
328 /**
329 * tryAcquireNanos is interruptible.
330 */
331 public void testInterruptedException2() throws InterruptedException {
332 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 } catch (InterruptedException success) {}
340 }
341 });
342
343 t.start();
344 t.interrupt();
345 t.join();
346 }
347
348
349 /**
350 * TryAcquire on exclusively held sync fails
351 */
352 public void testTryAcquireWhenSynced() throws InterruptedException {
353 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
361 t.start();
362 t.join();
363 sync.release(1);
364 }
365
366 /**
367 * tryAcquireNanos on an exclusively held sync times out
368 */
369 public void testAcquireNanos_Timeout() throws InterruptedException {
370 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 threadUnexpectedException(ex);
378 }
379 }
380 });
381
382 t.start();
383 t.join();
384 sync.release(1);
385 }
386
387
388 /**
389 * getState is true when acquired and false when not
390 */
391 public void testGetState() throws InterruptedException {
392 final Mutex sync = new Mutex();
393 sync.acquire(1);
394 assertTrue(sync.isHeldExclusively());
395 sync.release(1);
396 assertFalse(sync.isHeldExclusively());
397 Thread t = new Thread(new Runnable() {
398 public void run() {
399 sync.acquire(1);
400 try {
401 Thread.sleep(SMALL_DELAY_MS);
402 }
403 catch (Exception e) {
404 threadUnexpectedException(e);
405 }
406 sync.release(1);
407 }
408 });
409
410 t.start();
411 Thread.sleep(SHORT_DELAY_MS);
412 assertTrue(sync.isHeldExclusively());
413 t.join();
414 assertFalse(sync.isHeldExclusively());
415 }
416
417
418 /**
419 * acquireInterruptibly is interruptible.
420 */
421 public void testAcquireInterruptibly1() throws InterruptedException {
422 final Mutex sync = new Mutex();
423 sync.acquire(1);
424 Thread t = new Thread(new InterruptedSyncRunnable(sync));
425 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 }
432
433 /**
434 * acquireInterruptibly succeeds when released, else is interruptible
435 */
436 public void testAcquireInterruptibly2() throws InterruptedException {
437 final Mutex sync = new Mutex();
438 sync.acquireInterruptibly(1);
439 Thread t = new Thread(new InterruptedSyncRunnable(sync));
440 t.start();
441 t.interrupt();
442 assertTrue(sync.isHeldExclusively());
443 t.join();
444 }
445
446 /**
447 * owns is true for a condition created by sync else false
448 */
449 public void testOwns() {
450 final Mutex sync = new Mutex();
451 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 public void testAwait_IllegalMonitor() throws InterruptedException {
461 final Mutex sync = new Mutex();
462 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 public void testSignal_IllegalMonitor() throws InterruptedException {
475 final Mutex sync = new Mutex();
476 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
477 try {
478 c.signal();
479 shouldThrow();
480 }
481 catch (IllegalMonitorStateException success) {}
482 }
483
484 /**
485 * awaitNanos without a signal times out
486 */
487 public void testAwaitNanos_Timeout() throws InterruptedException {
488 final Mutex sync = new Mutex();
489 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
490 sync.acquire(1);
491 long t = c.awaitNanos(100);
492 assertTrue(t <= 0);
493 sync.release(1);
494 }
495
496 /**
497 * Timed await without a signal times out
498 */
499 public void testAwait_Timeout() throws InterruptedException {
500 final Mutex sync = new Mutex();
501 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
502 sync.acquire(1);
503 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
504 sync.release(1);
505 }
506
507 /**
508 * awaitUntil without a signal times out
509 */
510 public void testAwaitUntil_Timeout() throws InterruptedException {
511 final Mutex sync = new Mutex();
512 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
513 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 }
518
519 /**
520 * await returns when signalled
521 */
522 public void testAwait() throws InterruptedException {
523 final Mutex sync = new Mutex();
524 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
525 Thread t = new Thread(new Runnable() {
526 public void run() {
527 try {
528 sync.acquire(1);
529 c.await();
530 sync.release(1);
531 }
532 catch (InterruptedException e) {
533 threadUnexpectedException(e);
534 }
535 }
536 });
537
538 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 }
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 } catch (NullPointerException success) {}
558 }
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 } catch (NullPointerException success) {}
569 }
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 } catch (NullPointerException success) {}
581 }
582
583
584 /**
585 * hasWaiters throws IAE if not owned
586 */
587 public void testHasWaitersIAE() {
588 final Mutex sync = new Mutex();
589 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
590 final Mutex sync2 = new Mutex();
591 try {
592 sync2.hasWaiters(c);
593 shouldThrow();
594 } catch (IllegalArgumentException success) {}
595 }
596
597 /**
598 * hasWaiters throws IMSE if not synced
599 */
600 public void testHasWaitersIMSE() {
601 final Mutex sync = new Mutex();
602 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
603 try {
604 sync.hasWaiters(c);
605 shouldThrow();
606 } catch (IllegalMonitorStateException success) {}
607 }
608
609
610 /**
611 * getWaitQueueLength throws IAE if not owned
612 */
613 public void testGetWaitQueueLengthIAE() {
614 final Mutex sync = new Mutex();
615 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
616 final Mutex sync2 = new Mutex();
617 try {
618 sync2.getWaitQueueLength(c);
619 shouldThrow();
620 } catch (IllegalArgumentException success) {}
621 }
622
623 /**
624 * getWaitQueueLength throws IMSE if not synced
625 */
626 public void testGetWaitQueueLengthIMSE() {
627 final Mutex sync = new Mutex();
628 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
629 try {
630 sync.getWaitQueueLength(c);
631 shouldThrow();
632 } catch (IllegalMonitorStateException success) {}
633 }
634
635
636 /**
637 * getWaitingThreads throws IAE if not owned
638 */
639 public void testGetWaitingThreadsIAE() {
640 final Mutex sync = new Mutex();
641 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
642 final Mutex sync2 = new Mutex();
643 try {
644 sync2.getWaitingThreads(c);
645 shouldThrow();
646 } catch (IllegalArgumentException success) {}
647 }
648
649 /**
650 * getWaitingThreads throws IMSE if not synced
651 */
652 public void testGetWaitingThreadsIMSE() {
653 final Mutex sync = new Mutex();
654 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
655 try {
656 sync.getWaitingThreads(c);
657 shouldThrow();
658 } catch (IllegalMonitorStateException success) {}
659 }
660
661
662
663 /**
664 * hasWaiters returns true when a thread is waiting, else false
665 */
666 public void testHasWaiters() throws InterruptedException {
667 final Mutex sync = new Mutex();
668 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
669 Thread t = new Thread(new Runnable() {
670 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 catch (InterruptedException e) {
679 threadUnexpectedException(e);
680 }
681 }
682 });
683
684 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 }
699
700 /**
701 * getWaitQueueLength returns number of waiting threads
702 */
703 public void testGetWaitQueueLength() throws InterruptedException {
704 final Mutex sync = new Mutex();
705 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
706 Thread t1 = new Thread(new Runnable() {
707 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 catch (InterruptedException e) {
716 threadUnexpectedException(e);
717 }
718 }
719 });
720
721 Thread t2 = new Thread(new Runnable() {
722 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 catch (InterruptedException e) {
731 threadUnexpectedException(e);
732 }
733 }
734 });
735
736 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 }
755
756 /**
757 * getWaitingThreads returns only and all waiting threads
758 */
759 public void testGetWaitingThreads() throws InterruptedException {
760 final Mutex sync = new Mutex();
761 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
762 Thread t1 = new Thread(new Runnable() {
763 public void run() {
764 try {
765 sync.acquire(1);
766 threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
767 c.await();
768 sync.release(1);
769 }
770 catch (InterruptedException e) {
771 threadUnexpectedException(e);
772 }
773 }
774 });
775
776 Thread t2 = new Thread(new Runnable() {
777 public void run() {
778 try {
779 sync.acquire(1);
780 threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
781 c.await();
782 sync.release(1);
783 }
784 catch (InterruptedException e) {
785 threadUnexpectedException(e);
786 }
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 public void testAwaitUninterruptibly() throws InterruptedException {
820 final Mutex sync = new Mutex();
821 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
822 Thread t = new Thread(new Runnable() {
823 public void run() {
824 sync.acquire(1);
825 c.awaitUninterruptibly();
826 sync.release(1);
827 }
828 });
829
830 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 }
839
840 /**
841 * await is interruptible
842 */
843 public void testAwait_Interrupt() throws InterruptedException {
844 final Mutex sync = new Mutex();
845 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
846 Thread t = new Thread(new Runnable() {
847 public void run() {
848 try {
849 sync.acquire(1);
850 c.await();
851 sync.release(1);
852 threadShouldThrow();
853 }
854 catch (InterruptedException success) {
855 }
856 }
857 });
858
859 t.start();
860 Thread.sleep(SHORT_DELAY_MS);
861 t.interrupt();
862 t.join(SHORT_DELAY_MS);
863 assertFalse(t.isAlive());
864 }
865
866 /**
867 * awaitNanos is interruptible
868 */
869 public void testAwaitNanos_Interrupt() throws InterruptedException {
870 final Mutex sync = new Mutex();
871 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
872 Thread t = new Thread(new Runnable() {
873 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 catch (InterruptedException success) {
881 }
882 }
883 });
884
885 t.start();
886 Thread.sleep(SHORT_DELAY_MS);
887 t.interrupt();
888 t.join(SHORT_DELAY_MS);
889 assertFalse(t.isAlive());
890 }
891
892 /**
893 * awaitUntil is interruptible
894 */
895 public void testAwaitUntil_Interrupt() throws InterruptedException {
896 final Mutex sync = new Mutex();
897 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
898 Thread t = new Thread(new Runnable() {
899 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 catch (InterruptedException success) {
908 }
909 }
910 });
911
912 t.start();
913 Thread.sleep(SHORT_DELAY_MS);
914 t.interrupt();
915 t.join(SHORT_DELAY_MS);
916 assertFalse(t.isAlive());
917 }
918
919 /**
920 * signalAll wakes up all threads
921 */
922 public void testSignalAll() throws InterruptedException {
923 final Mutex sync = new Mutex();
924 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
925 Thread t1 = new Thread(new Runnable() {
926 public void run() {
927 try {
928 sync.acquire(1);
929 c.await();
930 sync.release(1);
931 }
932 catch (InterruptedException e) {
933 threadUnexpectedException();
934 }
935 }
936 });
937
938 Thread t2 = new Thread(new Runnable() {
939 public void run() {
940 try {
941 sync.acquire(1);
942 c.await();
943 sync.release(1);
944 }
945 catch (InterruptedException e) {
946 threadUnexpectedException();
947 }
948 }
949 });
950
951 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 }
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 public void testSerialization() throws Exception {
980 Mutex l = new Mutex();
981 l.acquire(1);
982 assertTrue(l.isHeldExclusively());
983
984 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 }
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 public void testAcquireSharedInterruptibly() throws InterruptedException {
1022 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 } catch (InterruptedException e) {
1031 threadUnexpectedException();
1032 }
1033 }
1034 });
1035
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 }
1043
1044
1045 /**
1046 * acquireSharedTimed returns after release
1047 */
1048 public void testAsquireSharedTimed() throws InterruptedException {
1049 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 } catch (InterruptedException e) {
1059 threadUnexpectedException();
1060 }
1061 }
1062 });
1063
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 }
1071
1072 /**
1073 * acquireSharedInterruptibly throws IE if interrupted before released
1074 */
1075 public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
1076 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 } catch (InterruptedException success) {}
1084 }
1085 });
1086
1087 t.start();
1088 assertFalse(l.isSignalled());
1089 t.interrupt();
1090 t.join();
1091 }
1092
1093 /**
1094 * acquireSharedTimed throws IE if interrupted before released
1095 */
1096 public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
1097 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 threadShouldThrow();
1104 } catch (InterruptedException success) {}
1105 }
1106 });
1107
1108 t.start();
1109 Thread.sleep(SHORT_DELAY_MS);
1110 assertFalse(l.isSignalled());
1111 t.interrupt();
1112 t.join();
1113 }
1114
1115 /**
1116 * acquireSharedTimed times out if not released before timeout
1117 */
1118 public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1119 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 } catch (InterruptedException ie) {
1126 threadUnexpectedException();
1127 }
1128 }
1129 });
1130
1131 t.start();
1132 Thread.sleep(SHORT_DELAY_MS);
1133 assertFalse(l.isSignalled());
1134 t.join();
1135 }
1136
1137
1138 }