ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.3
Committed: Wed Apr 5 11:24:55 2006 UTC (18 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.2: +1 -0 lines
Log Message:
Make acquireInterruptibly  consistent with AQS

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 sync.release(1);
470 t.join();
471 } catch(Exception e){
472 unexpectedException();
473 }
474 }
475
476 /**
477 * acquireInterruptibly succeeds when released, else is interruptible
478 */
479 public void testAcquireInterruptibly2() {
480 final Mutex sync = new Mutex();
481 try {
482 sync.acquireInterruptibly(1);
483 } catch(Exception e) {
484 unexpectedException();
485 }
486 Thread t = new Thread(new InterruptedSyncRunnable(sync));
487 try {
488 t.start();
489 t.interrupt();
490 assertTrue(sync.isHeldExclusively());
491 t.join();
492 } catch(Exception e){
493 unexpectedException();
494 }
495 }
496
497 /**
498 * owns is true for a condition created by sync else false
499 */
500 public void testOwns() {
501 final Mutex sync = new Mutex();
502 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
503 final Mutex sync2 = new Mutex();
504 assertTrue(sync.owns(c));
505 assertFalse(sync2.owns(c));
506 }
507
508 /**
509 * Calling await without holding sync throws IllegalMonitorStateException
510 */
511 public void testAwait_IllegalMonitor() {
512 final Mutex sync = new Mutex();
513 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
514 try {
515 c.await();
516 shouldThrow();
517 }
518 catch (IllegalMonitorStateException success) {
519 }
520 catch (Exception ex) {
521 unexpectedException();
522 }
523 }
524
525 /**
526 * Calling signal without holding sync throws IllegalMonitorStateException
527 */
528 public void testSignal_IllegalMonitor() {
529 final Mutex sync = new Mutex();
530 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
531 try {
532 c.signal();
533 shouldThrow();
534 }
535 catch (IllegalMonitorStateException success) {
536 }
537 catch (Exception ex) {
538 unexpectedException();
539 }
540 }
541
542 /**
543 * awaitNanos without a signal times out
544 */
545 public void testAwaitNanos_Timeout() {
546 final Mutex sync = new Mutex();
547 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
548 try {
549 sync.acquire(1);
550 long t = c.awaitNanos(100);
551 assertTrue(t <= 0);
552 sync.release(1);
553 }
554 catch (Exception ex) {
555 unexpectedException();
556 }
557 }
558
559 /**
560 * Timed await without a signal times out
561 */
562 public void testAwait_Timeout() {
563 final Mutex sync = new Mutex();
564 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
565 try {
566 sync.acquire(1);
567 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
568 sync.release(1);
569 }
570 catch (Exception ex) {
571 unexpectedException();
572 }
573 }
574
575 /**
576 * awaitUntil without a signal times out
577 */
578 public void testAwaitUntil_Timeout() {
579 final Mutex sync = new Mutex();
580 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
581 try {
582 sync.acquire(1);
583 java.util.Date d = new java.util.Date();
584 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
585 sync.release(1);
586 }
587 catch (Exception ex) {
588 unexpectedException();
589 }
590 }
591
592 /**
593 * await returns when signalled
594 */
595 public void testAwait() {
596 final Mutex sync = new Mutex();
597 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
598 Thread t = new Thread(new Runnable() {
599 public void run() {
600 try {
601 sync.acquire(1);
602 c.await();
603 sync.release(1);
604 }
605 catch(InterruptedException e) {
606 threadUnexpectedException();
607 }
608 }
609 });
610
611 try {
612 t.start();
613 Thread.sleep(SHORT_DELAY_MS);
614 sync.acquire(1);
615 c.signal();
616 sync.release(1);
617 t.join(SHORT_DELAY_MS);
618 assertFalse(t.isAlive());
619 }
620 catch (Exception ex) {
621 unexpectedException();
622 }
623 }
624
625
626
627 /**
628 * hasWaiters throws NPE if null
629 */
630 public void testHasWaitersNPE() {
631 final Mutex sync = new Mutex();
632 try {
633 sync.hasWaiters(null);
634 shouldThrow();
635 } catch (NullPointerException success) {
636 } catch (Exception ex) {
637 unexpectedException();
638 }
639 }
640
641 /**
642 * getWaitQueueLength throws NPE if null
643 */
644 public void testGetWaitQueueLengthNPE() {
645 final Mutex sync = new Mutex();
646 try {
647 sync.getWaitQueueLength(null);
648 shouldThrow();
649 } catch (NullPointerException success) {
650 } catch (Exception ex) {
651 unexpectedException();
652 }
653 }
654
655
656 /**
657 * getWaitingThreads throws NPE if null
658 */
659 public void testGetWaitingThreadsNPE() {
660 final Mutex sync = new Mutex();
661 try {
662 sync.getWaitingThreads(null);
663 shouldThrow();
664 } catch (NullPointerException success) {
665 } catch (Exception ex) {
666 unexpectedException();
667 }
668 }
669
670
671 /**
672 * hasWaiters throws IAE if not owned
673 */
674 public void testHasWaitersIAE() {
675 final Mutex sync = new Mutex();
676 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
677 final Mutex sync2 = new Mutex();
678 try {
679 sync2.hasWaiters(c);
680 shouldThrow();
681 } catch (IllegalArgumentException success) {
682 } catch (Exception ex) {
683 unexpectedException();
684 }
685 }
686
687 /**
688 * hasWaiters throws IMSE if not synced
689 */
690 public void testHasWaitersIMSE() {
691 final Mutex sync = new Mutex();
692 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
693 try {
694 sync.hasWaiters(c);
695 shouldThrow();
696 } catch (IllegalMonitorStateException success) {
697 } catch (Exception ex) {
698 unexpectedException();
699 }
700 }
701
702
703 /**
704 * getWaitQueueLength throws IAE if not owned
705 */
706 public void testGetWaitQueueLengthIAE() {
707 final Mutex sync = new Mutex();
708 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
709 final Mutex sync2 = new Mutex();
710 try {
711 sync2.getWaitQueueLength(c);
712 shouldThrow();
713 } catch (IllegalArgumentException success) {
714 } catch (Exception ex) {
715 unexpectedException();
716 }
717 }
718
719 /**
720 * getWaitQueueLength throws IMSE if not synced
721 */
722 public void testGetWaitQueueLengthIMSE() {
723 final Mutex sync = new Mutex();
724 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
725 try {
726 sync.getWaitQueueLength(c);
727 shouldThrow();
728 } catch (IllegalMonitorStateException success) {
729 } catch (Exception ex) {
730 unexpectedException();
731 }
732 }
733
734
735 /**
736 * getWaitingThreads throws IAE if not owned
737 */
738 public void testGetWaitingThreadsIAE() {
739 final Mutex sync = new Mutex();
740 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
741 final Mutex sync2 = new Mutex();
742 try {
743 sync2.getWaitingThreads(c);
744 shouldThrow();
745 } catch (IllegalArgumentException success) {
746 } catch (Exception ex) {
747 unexpectedException();
748 }
749 }
750
751 /**
752 * getWaitingThreads throws IMSE if not synced
753 */
754 public void testGetWaitingThreadsIMSE() {
755 final Mutex sync = new Mutex();
756 final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
757 try {
758 sync.getWaitingThreads(c);
759 shouldThrow();
760 } catch (IllegalMonitorStateException success) {
761 } catch (Exception ex) {
762 unexpectedException();
763 }
764 }
765
766
767
768 /**
769 * hasWaiters returns true when a thread is waiting, else false
770 */
771 public void testHasWaiters() {
772 final Mutex sync = new Mutex();
773 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
774 Thread t = new Thread(new Runnable() {
775 public void run() {
776 try {
777 sync.acquire(1);
778 threadAssertFalse(sync.hasWaiters(c));
779 threadAssertEquals(0, sync.getWaitQueueLength(c));
780 c.await();
781 sync.release(1);
782 }
783 catch(InterruptedException e) {
784 threadUnexpectedException();
785 }
786 }
787 });
788
789 try {
790 t.start();
791 Thread.sleep(SHORT_DELAY_MS);
792 sync.acquire(1);
793 assertTrue(sync.hasWaiters(c));
794 assertEquals(1, sync.getWaitQueueLength(c));
795 c.signal();
796 sync.release(1);
797 Thread.sleep(SHORT_DELAY_MS);
798 sync.acquire(1);
799 assertFalse(sync.hasWaiters(c));
800 assertEquals(0, sync.getWaitQueueLength(c));
801 sync.release(1);
802 t.join(SHORT_DELAY_MS);
803 assertFalse(t.isAlive());
804 }
805 catch (Exception ex) {
806 unexpectedException();
807 }
808 }
809
810 /**
811 * getWaitQueueLength returns number of waiting threads
812 */
813 public void testGetWaitQueueLength() {
814 final Mutex sync = new Mutex();
815 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
816 Thread t1 = new Thread(new Runnable() {
817 public void run() {
818 try {
819 sync.acquire(1);
820 threadAssertFalse(sync.hasWaiters(c));
821 threadAssertEquals(0, sync.getWaitQueueLength(c));
822 c.await();
823 sync.release(1);
824 }
825 catch(InterruptedException e) {
826 threadUnexpectedException();
827 }
828 }
829 });
830
831 Thread t2 = new Thread(new Runnable() {
832 public void run() {
833 try {
834 sync.acquire(1);
835 threadAssertTrue(sync.hasWaiters(c));
836 threadAssertEquals(1, sync.getWaitQueueLength(c));
837 c.await();
838 sync.release(1);
839 }
840 catch(InterruptedException e) {
841 threadUnexpectedException();
842 }
843 }
844 });
845
846 try {
847 t1.start();
848 Thread.sleep(SHORT_DELAY_MS);
849 t2.start();
850 Thread.sleep(SHORT_DELAY_MS);
851 sync.acquire(1);
852 assertTrue(sync.hasWaiters(c));
853 assertEquals(2, sync.getWaitQueueLength(c));
854 c.signalAll();
855 sync.release(1);
856 Thread.sleep(SHORT_DELAY_MS);
857 sync.acquire(1);
858 assertFalse(sync.hasWaiters(c));
859 assertEquals(0, sync.getWaitQueueLength(c));
860 sync.release(1);
861 t1.join(SHORT_DELAY_MS);
862 t2.join(SHORT_DELAY_MS);
863 assertFalse(t1.isAlive());
864 assertFalse(t2.isAlive());
865 }
866 catch (Exception ex) {
867 unexpectedException();
868 }
869 }
870
871 /**
872 * getWaitingThreads returns only and all waiting threads
873 */
874 public void testGetWaitingThreads() {
875 final Mutex sync = new Mutex();
876 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
877 Thread t1 = new Thread(new Runnable() {
878 public void run() {
879 try {
880 sync.acquire(1);
881 threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
882 c.await();
883 sync.release(1);
884 }
885 catch(InterruptedException e) {
886 threadUnexpectedException();
887 }
888 }
889 });
890
891 Thread t2 = new Thread(new Runnable() {
892 public void run() {
893 try {
894 sync.acquire(1);
895 threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
896 c.await();
897 sync.release(1);
898 }
899 catch(InterruptedException e) {
900 threadUnexpectedException();
901 }
902 }
903 });
904
905 try {
906 sync.acquire(1);
907 assertTrue(sync.getWaitingThreads(c).isEmpty());
908 sync.release(1);
909 t1.start();
910 Thread.sleep(SHORT_DELAY_MS);
911 t2.start();
912 Thread.sleep(SHORT_DELAY_MS);
913 sync.acquire(1);
914 assertTrue(sync.hasWaiters(c));
915 assertTrue(sync.getWaitingThreads(c).contains(t1));
916 assertTrue(sync.getWaitingThreads(c).contains(t2));
917 c.signalAll();
918 sync.release(1);
919 Thread.sleep(SHORT_DELAY_MS);
920 sync.acquire(1);
921 assertFalse(sync.hasWaiters(c));
922 assertTrue(sync.getWaitingThreads(c).isEmpty());
923 sync.release(1);
924 t1.join(SHORT_DELAY_MS);
925 t2.join(SHORT_DELAY_MS);
926 assertFalse(t1.isAlive());
927 assertFalse(t2.isAlive());
928 }
929 catch (Exception ex) {
930 unexpectedException();
931 }
932 }
933
934
935
936 /**
937 * awaitUninterruptibly doesn't abort on interrupt
938 */
939 public void testAwaitUninterruptibly() {
940 final Mutex sync = new Mutex();
941 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
942 Thread t = new Thread(new Runnable() {
943 public void run() {
944 sync.acquire(1);
945 c.awaitUninterruptibly();
946 sync.release(1);
947 }
948 });
949
950 try {
951 t.start();
952 Thread.sleep(SHORT_DELAY_MS);
953 t.interrupt();
954 sync.acquire(1);
955 c.signal();
956 sync.release(1);
957 t.join(SHORT_DELAY_MS);
958 assertFalse(t.isAlive());
959 }
960 catch (Exception ex) {
961 unexpectedException();
962 }
963 }
964
965 /**
966 * await is interruptible
967 */
968 public void testAwait_Interrupt() {
969 final Mutex sync = new Mutex();
970 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
971 Thread t = new Thread(new Runnable() {
972 public void run() {
973 try {
974 sync.acquire(1);
975 c.await();
976 sync.release(1);
977 threadShouldThrow();
978 }
979 catch(InterruptedException success) {
980 }
981 }
982 });
983
984 try {
985 t.start();
986 Thread.sleep(SHORT_DELAY_MS);
987 t.interrupt();
988 t.join(SHORT_DELAY_MS);
989 assertFalse(t.isAlive());
990 }
991 catch (Exception ex) {
992 unexpectedException();
993 }
994 }
995
996 /**
997 * awaitNanos is interruptible
998 */
999 public void testAwaitNanos_Interrupt() {
1000 final Mutex sync = new Mutex();
1001 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1002 Thread t = new Thread(new Runnable() {
1003 public void run() {
1004 try {
1005 sync.acquire(1);
1006 c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1007 sync.release(1);
1008 threadShouldThrow();
1009 }
1010 catch(InterruptedException success) {
1011 }
1012 }
1013 });
1014
1015 try {
1016 t.start();
1017 Thread.sleep(SHORT_DELAY_MS);
1018 t.interrupt();
1019 t.join(SHORT_DELAY_MS);
1020 assertFalse(t.isAlive());
1021 }
1022 catch (Exception ex) {
1023 unexpectedException();
1024 }
1025 }
1026
1027 /**
1028 * awaitUntil is interruptible
1029 */
1030 public void testAwaitUntil_Interrupt() {
1031 final Mutex sync = new Mutex();
1032 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1033 Thread t = new Thread(new Runnable() {
1034 public void run() {
1035 try {
1036 sync.acquire(1);
1037 java.util.Date d = new java.util.Date();
1038 c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1039 sync.release(1);
1040 threadShouldThrow();
1041 }
1042 catch(InterruptedException success) {
1043 }
1044 }
1045 });
1046
1047 try {
1048 t.start();
1049 Thread.sleep(SHORT_DELAY_MS);
1050 t.interrupt();
1051 t.join(SHORT_DELAY_MS);
1052 assertFalse(t.isAlive());
1053 }
1054 catch (Exception ex) {
1055 unexpectedException();
1056 }
1057 }
1058
1059 /**
1060 * signalAll wakes up all threads
1061 */
1062 public void testSignalAll() {
1063 final Mutex sync = new Mutex();
1064 final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1065 Thread t1 = new Thread(new Runnable() {
1066 public void run() {
1067 try {
1068 sync.acquire(1);
1069 c.await();
1070 sync.release(1);
1071 }
1072 catch(InterruptedException e) {
1073 threadUnexpectedException();
1074 }
1075 }
1076 });
1077
1078 Thread t2 = new Thread(new Runnable() {
1079 public void run() {
1080 try {
1081 sync.acquire(1);
1082 c.await();
1083 sync.release(1);
1084 }
1085 catch(InterruptedException e) {
1086 threadUnexpectedException();
1087 }
1088 }
1089 });
1090
1091 try {
1092 t1.start();
1093 t2.start();
1094 Thread.sleep(SHORT_DELAY_MS);
1095 sync.acquire(1);
1096 c.signalAll();
1097 sync.release(1);
1098 t1.join(SHORT_DELAY_MS);
1099 t2.join(SHORT_DELAY_MS);
1100 assertFalse(t1.isAlive());
1101 assertFalse(t2.isAlive());
1102 }
1103 catch (Exception ex) {
1104 unexpectedException();
1105 }
1106 }
1107
1108
1109 /**
1110 * toString indicates current state
1111 */
1112 public void testToString() {
1113 Mutex sync = new Mutex();
1114 String us = sync.toString();
1115 assertTrue(us.indexOf("State = 0") >= 0);
1116 sync.acquire(1);
1117 String ls = sync.toString();
1118 assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0);
1119 }
1120
1121 /**
1122 * A serialized AQS deserializes with current state
1123 */
1124 public void testSerialization() {
1125 Mutex l = new Mutex();
1126 l.acquire(1);
1127 assertTrue(l.isHeldExclusively());
1128
1129 try {
1130 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1131 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1132 out.writeObject(l);
1133 out.close();
1134
1135 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1136 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1137 Mutex r = (Mutex) in.readObject();
1138 assertTrue(r.isHeldExclusively());
1139 } catch(Exception e){
1140 e.printStackTrace();
1141 unexpectedException();
1142 }
1143 }
1144
1145
1146 /**
1147 * tryReleaseShared setting state changes getState
1148 */
1149 public void testGetStateWithReleaseShared() {
1150 final BooleanLatch l = new BooleanLatch();
1151 assertFalse(l.isSignalled());
1152 l.releaseShared(0);
1153 assertTrue(l.isSignalled());
1154 }
1155
1156 /**
1157 * releaseShared has no effect when already signalled
1158 */
1159 public void testReleaseShared() {
1160 final BooleanLatch l = new BooleanLatch();
1161 assertFalse(l.isSignalled());
1162 l.releaseShared(0);
1163 assertTrue(l.isSignalled());
1164 l.releaseShared(0);
1165 assertTrue(l.isSignalled());
1166 }
1167
1168 /**
1169 * acquireSharedInterruptibly returns after release, but not before
1170 */
1171 public void testAcquireSharedInterruptibly() {
1172 final BooleanLatch l = new BooleanLatch();
1173
1174 Thread t = new Thread(new Runnable() {
1175 public void run() {
1176 try {
1177 threadAssertFalse(l.isSignalled());
1178 l.acquireSharedInterruptibly(0);
1179 threadAssertTrue(l.isSignalled());
1180 } catch(InterruptedException e){
1181 threadUnexpectedException();
1182 }
1183 }
1184 });
1185 try {
1186 t.start();
1187 assertFalse(l.isSignalled());
1188 Thread.sleep(SHORT_DELAY_MS);
1189 l.releaseShared(0);
1190 assertTrue(l.isSignalled());
1191 t.join();
1192 } catch (InterruptedException e){
1193 unexpectedException();
1194 }
1195 }
1196
1197
1198 /**
1199 * acquireSharedTimed returns after release
1200 */
1201 public void testAsquireSharedTimed() {
1202 final BooleanLatch l = new BooleanLatch();
1203
1204 Thread t = new Thread(new Runnable() {
1205 public void run() {
1206 try {
1207 threadAssertFalse(l.isSignalled());
1208 threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
1209 threadAssertTrue(l.isSignalled());
1210
1211 } catch(InterruptedException e){
1212 threadUnexpectedException();
1213 }
1214 }
1215 });
1216 try {
1217 t.start();
1218 assertFalse(l.isSignalled());
1219 Thread.sleep(SHORT_DELAY_MS);
1220 l.releaseShared(0);
1221 assertTrue(l.isSignalled());
1222 t.join();
1223 } catch (InterruptedException e){
1224 unexpectedException();
1225 }
1226 }
1227
1228 /**
1229 * acquireSharedInterruptibly throws IE if interrupted before released
1230 */
1231 public void testAcquireSharedInterruptibly_InterruptedException() {
1232 final BooleanLatch l = new BooleanLatch();
1233 Thread t = new Thread(new Runnable() {
1234 public void run() {
1235 try {
1236 threadAssertFalse(l.isSignalled());
1237 l.acquireSharedInterruptibly(0);
1238 threadShouldThrow();
1239 } catch(InterruptedException success){}
1240 }
1241 });
1242 t.start();
1243 try {
1244 assertFalse(l.isSignalled());
1245 t.interrupt();
1246 t.join();
1247 } catch (InterruptedException e){
1248 unexpectedException();
1249 }
1250 }
1251
1252 /**
1253 * acquireSharedTimed throws IE if interrupted before released
1254 */
1255 public void testAcquireSharedNanos_InterruptedException() {
1256 final BooleanLatch l = new BooleanLatch();
1257 Thread t = new Thread(new Runnable() {
1258 public void run() {
1259 try {
1260 threadAssertFalse(l.isSignalled());
1261 l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
1262 threadShouldThrow();
1263 } catch(InterruptedException success){}
1264 }
1265 });
1266 t.start();
1267 try {
1268 Thread.sleep(SHORT_DELAY_MS);
1269 assertFalse(l.isSignalled());
1270 t.interrupt();
1271 t.join();
1272 } catch (InterruptedException e){
1273 unexpectedException();
1274 }
1275 }
1276
1277 /**
1278 * acquireSharedTimed times out if not released before timeout
1279 */
1280 public void testAcquireSharedNanos_Timeout() {
1281 final BooleanLatch l = new BooleanLatch();
1282 Thread t = new Thread(new Runnable() {
1283 public void run() {
1284 try {
1285 threadAssertFalse(l.isSignalled());
1286 threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1287 } catch(InterruptedException ie){
1288 threadUnexpectedException();
1289 }
1290 }
1291 });
1292 t.start();
1293 try {
1294 Thread.sleep(SHORT_DELAY_MS);
1295 assertFalse(l.isSignalled());
1296 t.join();
1297 } catch (InterruptedException e){
1298 unexpectedException();
1299 }
1300 }
1301
1302
1303 }