ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.6
Committed: Mon Nov 16 04:57:09 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +35 -35 lines
Log Message:
whitespace

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