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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
13 |
|
|
14 |
|
public class SemaphoreTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(SemaphoreTest.class); |
24 |
|
*/ |
25 |
|
static class PublicSemaphore extends Semaphore { |
26 |
|
PublicSemaphore(int p, boolean f) { super(p, f); } |
27 |
< |
public Collection<Thread> getQueuedThreads() { |
28 |
< |
return super.getQueuedThreads(); |
27 |
> |
public Collection<Thread> getQueuedThreads() { |
28 |
> |
return super.getQueuedThreads(); |
29 |
|
} |
30 |
< |
public void reducePermits(int p) { |
30 |
> |
public void reducePermits(int p) { |
31 |
|
super.reducePermits(p); |
32 |
|
} |
33 |
|
} |
41 |
|
public void run() { |
42 |
|
try { |
43 |
|
lock.acquire(); |
44 |
< |
} catch(InterruptedException success){} |
44 |
> |
} catch (InterruptedException success){} |
45 |
|
} |
46 |
|
} |
47 |
|
|
57 |
|
try { |
58 |
|
lock.acquire(); |
59 |
|
threadShouldThrow(); |
60 |
< |
} catch(InterruptedException success){} |
60 |
> |
} catch (InterruptedException success){} |
61 |
|
} |
62 |
|
} |
63 |
|
|
70 |
|
assertFalse(s0.isFair()); |
71 |
|
Semaphore s1 = new Semaphore(-1, false); |
72 |
|
assertEquals(-1, s1.availablePermits()); |
73 |
+ |
assertFalse(s1.isFair()); |
74 |
|
Semaphore s2 = new Semaphore(-1, false); |
75 |
|
assertEquals(-1, s2.availablePermits()); |
76 |
+ |
assertFalse(s2.isFair()); |
77 |
+ |
} |
78 |
+ |
|
79 |
+ |
/** |
80 |
+ |
* Constructor without fairness argument behaves as nonfair |
81 |
+ |
*/ |
82 |
+ |
public void testConstructor2() { |
83 |
+ |
Semaphore s0 = new Semaphore(0); |
84 |
+ |
assertEquals(0, s0.availablePermits()); |
85 |
+ |
assertFalse(s0.isFair()); |
86 |
+ |
Semaphore s1 = new Semaphore(-1); |
87 |
+ |
assertEquals(-1, s1.availablePermits()); |
88 |
+ |
assertFalse(s1.isFair()); |
89 |
+ |
Semaphore s2 = new Semaphore(-1); |
90 |
+ |
assertEquals(-1, s2.availablePermits()); |
91 |
+ |
assertFalse(s2.isFair()); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
120 |
|
s.acquire(); |
121 |
|
s.release(); |
122 |
|
assertEquals(1, s.availablePermits()); |
123 |
< |
} catch( InterruptedException e){ |
123 |
> |
} catch ( InterruptedException e){ |
124 |
|
unexpectedException(); |
125 |
|
} |
126 |
|
} |
165 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
166 |
|
s.release(); |
167 |
|
assertEquals(1, s.availablePermits()); |
168 |
< |
} catch( InterruptedException e){ |
168 |
> |
} catch ( InterruptedException e){ |
169 |
|
unexpectedException(); |
170 |
|
} |
171 |
|
} |
182 |
|
s.release(); |
183 |
|
s.release(); |
184 |
|
s.acquire(); |
185 |
< |
} catch(InterruptedException ie){ |
185 |
> |
} catch (InterruptedException ie){ |
186 |
|
threadUnexpectedException(); |
187 |
|
} |
188 |
|
} |
196 |
|
s.acquire(); |
197 |
|
s.release(); |
198 |
|
t.join(); |
199 |
< |
} catch( InterruptedException e){ |
199 |
> |
} catch ( InterruptedException e){ |
200 |
|
unexpectedException(); |
201 |
|
} |
202 |
|
} |
223 |
|
s.acquireUninterruptibly(); |
224 |
|
s.release(); |
225 |
|
t.join(); |
226 |
< |
} catch( InterruptedException e){ |
226 |
> |
} catch ( InterruptedException e){ |
227 |
|
unexpectedException(); |
228 |
|
} |
229 |
|
} |
242 |
|
s.release(); |
243 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
244 |
|
|
245 |
< |
} catch(InterruptedException ie){ |
245 |
> |
} catch (InterruptedException ie){ |
246 |
|
threadUnexpectedException(); |
247 |
|
} |
248 |
|
} |
255 |
|
s.release(); |
256 |
|
s.release(); |
257 |
|
t.join(); |
258 |
< |
} catch( InterruptedException e){ |
258 |
> |
} catch ( InterruptedException e){ |
259 |
|
unexpectedException(); |
260 |
|
} |
261 |
|
} |
270 |
|
try { |
271 |
|
s.acquire(); |
272 |
|
threadShouldThrow(); |
273 |
< |
} catch(InterruptedException success){} |
273 |
> |
} catch (InterruptedException success){} |
274 |
|
} |
275 |
|
}); |
276 |
|
t.start(); |
278 |
|
Thread.sleep(SHORT_DELAY_MS); |
279 |
|
t.interrupt(); |
280 |
|
t.join(); |
281 |
< |
} catch(InterruptedException e){ |
281 |
> |
} catch (InterruptedException e){ |
282 |
|
unexpectedException(); |
283 |
|
} |
284 |
|
} |
285 |
< |
|
285 |
> |
|
286 |
|
/** |
287 |
|
* A waiting timed acquire blocks interruptibly |
288 |
|
*/ |
293 |
|
try { |
294 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
295 |
|
threadShouldThrow(); |
296 |
< |
} catch(InterruptedException success){ |
296 |
> |
} catch (InterruptedException success){ |
297 |
|
} |
298 |
|
} |
299 |
|
}); |
302 |
|
Thread.sleep(SHORT_DELAY_MS); |
303 |
|
t.interrupt(); |
304 |
|
t.join(); |
305 |
< |
} catch(InterruptedException e){ |
305 |
> |
} catch (InterruptedException e){ |
306 |
|
unexpectedException(); |
307 |
|
} |
308 |
|
} |
310 |
|
/** |
311 |
|
* hasQueuedThreads reports whether there are waiting threads |
312 |
|
*/ |
313 |
< |
public void testHasQueuedThreads() { |
313 |
> |
public void testHasQueuedThreads() { |
314 |
|
final Semaphore lock = new Semaphore(1, false); |
315 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
316 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
331 |
|
assertFalse(lock.hasQueuedThreads()); |
332 |
|
t1.join(); |
333 |
|
t2.join(); |
334 |
< |
} catch(Exception e){ |
334 |
> |
} catch (Exception e){ |
335 |
|
unexpectedException(); |
336 |
|
} |
337 |
< |
} |
337 |
> |
} |
338 |
|
|
339 |
|
/** |
340 |
|
* getQueueLength reports number of waiting threads |
341 |
|
*/ |
342 |
< |
public void testGetQueueLength() { |
342 |
> |
public void testGetQueueLength() { |
343 |
|
final Semaphore lock = new Semaphore(1, false); |
344 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
345 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
360 |
|
assertEquals(0, lock.getQueueLength()); |
361 |
|
t1.join(); |
362 |
|
t2.join(); |
363 |
< |
} catch(Exception e){ |
363 |
> |
} catch (Exception e){ |
364 |
|
unexpectedException(); |
365 |
|
} |
366 |
< |
} |
366 |
> |
} |
367 |
|
|
368 |
|
/** |
369 |
|
* getQueuedThreads includes waiting threads |
370 |
|
*/ |
371 |
< |
public void testGetQueuedThreads() { |
371 |
> |
public void testGetQueuedThreads() { |
372 |
|
final PublicSemaphore lock = new PublicSemaphore(1, false); |
373 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
374 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
392 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
393 |
|
t1.join(); |
394 |
|
t2.join(); |
395 |
< |
} catch(Exception e){ |
395 |
> |
} catch (Exception e){ |
396 |
|
unexpectedException(); |
397 |
|
} |
398 |
< |
} |
398 |
> |
} |
399 |
|
|
400 |
+ |
/** |
401 |
+ |
* drainPermits reports and removes given number of permits |
402 |
+ |
*/ |
403 |
+ |
public void testDrainPermits() { |
404 |
+ |
Semaphore s = new Semaphore(0, false); |
405 |
+ |
assertEquals(0, s.availablePermits()); |
406 |
+ |
assertEquals(0, s.drainPermits()); |
407 |
+ |
s.release(10); |
408 |
+ |
assertEquals(10, s.availablePermits()); |
409 |
+ |
assertEquals(10, s.drainPermits()); |
410 |
+ |
assertEquals(0, s.availablePermits()); |
411 |
+ |
assertEquals(0, s.drainPermits()); |
412 |
+ |
} |
413 |
|
|
414 |
|
/** |
415 |
|
* reducePermits reduces number of permits |
443 |
|
assertFalse(r.isFair()); |
444 |
|
r.acquire(); |
445 |
|
r.release(); |
446 |
< |
} catch(Exception e){ |
446 |
> |
} catch (Exception e){ |
447 |
|
unexpectedException(); |
448 |
|
} |
449 |
|
} |
502 |
|
s.acquire(); |
503 |
|
s.release(); |
504 |
|
assertEquals(1, s.availablePermits()); |
505 |
< |
} catch( InterruptedException e){ |
505 |
> |
} catch ( InterruptedException e){ |
506 |
|
unexpectedException(); |
507 |
|
} |
508 |
|
} |
524 |
|
s.release(5); |
525 |
|
s.acquire(5); |
526 |
|
assertEquals(1, s.availablePermits()); |
527 |
< |
} catch( InterruptedException e){ |
527 |
> |
} catch ( InterruptedException e){ |
528 |
|
unexpectedException(); |
529 |
|
} |
530 |
|
} |
567 |
|
s.release(5); |
568 |
|
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
|
assertEquals(1, s.availablePermits()); |
570 |
< |
} catch( InterruptedException e){ |
570 |
> |
} catch ( InterruptedException e){ |
571 |
|
unexpectedException(); |
572 |
|
} |
573 |
|
} |
589 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
590 |
|
s.release(); |
591 |
|
assertEquals(1, s.availablePermits()); |
592 |
< |
} catch( InterruptedException e){ |
592 |
> |
} catch ( InterruptedException e){ |
593 |
|
unexpectedException(); |
594 |
|
} |
595 |
|
} |
606 |
|
s.acquire(); |
607 |
|
s.acquire(); |
608 |
|
s.acquire(); |
609 |
< |
} catch(InterruptedException ie){ |
609 |
> |
} catch (InterruptedException ie){ |
610 |
|
threadUnexpectedException(); |
611 |
|
} |
612 |
|
} |
622 |
|
s.release(); |
623 |
|
t.join(); |
624 |
|
assertEquals(2, s.availablePermits()); |
625 |
< |
} catch( InterruptedException e){ |
625 |
> |
} catch ( InterruptedException e){ |
626 |
|
unexpectedException(); |
627 |
|
} |
628 |
|
} |
635 |
|
Thread t = new Thread(new Runnable() { |
636 |
|
public void run() { |
637 |
|
try { |
638 |
+ |
s.acquire(); |
639 |
+ |
s.release(2); |
640 |
+ |
s.acquire(); |
641 |
+ |
} catch (InterruptedException ie){ |
642 |
+ |
threadUnexpectedException(); |
643 |
+ |
} |
644 |
+ |
} |
645 |
+ |
}); |
646 |
+ |
try { |
647 |
+ |
t.start(); |
648 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
649 |
+ |
s.release(2); |
650 |
+ |
s.acquire(2); |
651 |
+ |
s.release(1); |
652 |
+ |
t.join(); |
653 |
+ |
} catch ( InterruptedException e){ |
654 |
+ |
unexpectedException(); |
655 |
+ |
} |
656 |
+ |
} |
657 |
+ |
|
658 |
+ |
/** |
659 |
+ |
* release(n) in one thread enables acquire(n) in another thread |
660 |
+ |
*/ |
661 |
+ |
public void testAcquireReleaseNInDifferentThreads_fair2() { |
662 |
+ |
final Semaphore s = new Semaphore(0, true); |
663 |
+ |
Thread t = new Thread(new Runnable() { |
664 |
+ |
public void run() { |
665 |
+ |
try { |
666 |
|
s.acquire(2); |
667 |
|
s.acquire(2); |
668 |
|
s.release(4); |
669 |
< |
} catch(InterruptedException ie){ |
669 |
> |
} catch (InterruptedException ie){ |
670 |
|
threadUnexpectedException(); |
671 |
|
} |
672 |
|
} |
679 |
|
s.acquire(2); |
680 |
|
s.release(2); |
681 |
|
t.join(); |
682 |
< |
} catch( InterruptedException e){ |
682 |
> |
} catch ( InterruptedException e){ |
683 |
|
unexpectedException(); |
684 |
|
} |
685 |
|
} |
686 |
|
|
687 |
|
|
688 |
|
|
689 |
+ |
|
690 |
+ |
|
691 |
|
/** |
692 |
|
* release in one thread enables timed acquire in another thread |
693 |
|
*/ |
702 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
703 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
704 |
|
|
705 |
< |
} catch(InterruptedException ie){ |
705 |
> |
} catch (InterruptedException ie){ |
706 |
|
threadUnexpectedException(); |
707 |
|
} |
708 |
|
} |
715 |
|
s.release(); |
716 |
|
s.release(); |
717 |
|
t.join(); |
718 |
< |
} catch( InterruptedException e){ |
718 |
> |
} catch ( InterruptedException e){ |
719 |
|
unexpectedException(); |
720 |
|
} |
721 |
|
} |
732 |
|
s.release(2); |
733 |
|
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
734 |
|
s.release(2); |
735 |
< |
} catch(InterruptedException ie){ |
735 |
> |
} catch (InterruptedException ie){ |
736 |
|
threadUnexpectedException(); |
737 |
|
} |
738 |
|
} |
744 |
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
745 |
|
s.release(2); |
746 |
|
t.join(); |
747 |
< |
} catch( InterruptedException e){ |
747 |
> |
} catch ( InterruptedException e){ |
748 |
|
unexpectedException(); |
749 |
|
} |
750 |
|
} |
759 |
|
try { |
760 |
|
s.acquire(); |
761 |
|
threadShouldThrow(); |
762 |
< |
} catch(InterruptedException success){} |
762 |
> |
} catch (InterruptedException success){} |
763 |
|
} |
764 |
|
}); |
765 |
|
t.start(); |
767 |
|
Thread.sleep(SHORT_DELAY_MS); |
768 |
|
t.interrupt(); |
769 |
|
t.join(); |
770 |
< |
} catch(InterruptedException e){ |
770 |
> |
} catch (InterruptedException e){ |
771 |
|
unexpectedException(); |
772 |
|
} |
773 |
|
} |
782 |
|
try { |
783 |
|
s.acquire(3); |
784 |
|
threadShouldThrow(); |
785 |
< |
} catch(InterruptedException success){} |
785 |
> |
} catch (InterruptedException success){} |
786 |
|
} |
787 |
|
}); |
788 |
|
t.start(); |
790 |
|
Thread.sleep(SHORT_DELAY_MS); |
791 |
|
t.interrupt(); |
792 |
|
t.join(); |
793 |
< |
} catch(InterruptedException e){ |
793 |
> |
} catch (InterruptedException e){ |
794 |
|
unexpectedException(); |
795 |
|
} |
796 |
|
} |
797 |
< |
|
797 |
> |
|
798 |
|
/** |
799 |
|
* A waiting tryAcquire blocks interruptibly |
800 |
|
*/ |
805 |
|
try { |
806 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
807 |
|
threadShouldThrow(); |
808 |
< |
} catch(InterruptedException success){ |
808 |
> |
} catch (InterruptedException success){ |
809 |
|
} |
810 |
|
} |
811 |
|
}); |
814 |
|
Thread.sleep(SHORT_DELAY_MS); |
815 |
|
t.interrupt(); |
816 |
|
t.join(); |
817 |
< |
} catch(InterruptedException e){ |
817 |
> |
} catch (InterruptedException e){ |
818 |
|
unexpectedException(); |
819 |
|
} |
820 |
|
} |
829 |
|
try { |
830 |
|
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
831 |
|
threadShouldThrow(); |
832 |
< |
} catch(InterruptedException success){ |
832 |
> |
} catch (InterruptedException success){ |
833 |
|
} |
834 |
|
} |
835 |
|
}); |
838 |
|
Thread.sleep(SHORT_DELAY_MS); |
839 |
|
t.interrupt(); |
840 |
|
t.join(); |
841 |
< |
} catch(InterruptedException e){ |
841 |
> |
} catch (InterruptedException e){ |
842 |
|
unexpectedException(); |
843 |
|
} |
844 |
|
} |
846 |
|
/** |
847 |
|
* getQueueLength reports number of waiting threads |
848 |
|
*/ |
849 |
< |
public void testGetQueueLength_fair() { |
849 |
> |
public void testGetQueueLength_fair() { |
850 |
|
final Semaphore lock = new Semaphore(1, true); |
851 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
852 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
867 |
|
assertEquals(0, lock.getQueueLength()); |
868 |
|
t1.join(); |
869 |
|
t2.join(); |
870 |
< |
} catch(Exception e){ |
870 |
> |
} catch (Exception e){ |
871 |
|
unexpectedException(); |
872 |
|
} |
873 |
< |
} |
873 |
> |
} |
874 |
|
|
875 |
|
|
876 |
|
/** |
894 |
|
assertTrue(r.isFair()); |
895 |
|
r.acquire(); |
896 |
|
r.release(); |
897 |
< |
} catch(Exception e){ |
897 |
> |
} catch (Exception e){ |
898 |
|
unexpectedException(); |
899 |
|
} |
900 |
|
} |
901 |
|
|
902 |
+ |
/** |
903 |
+ |
* toString indicates current number of permits |
904 |
+ |
*/ |
905 |
+ |
public void testToString() { |
906 |
+ |
Semaphore s = new Semaphore(0); |
907 |
+ |
String us = s.toString(); |
908 |
+ |
assertTrue(us.indexOf("Permits = 0") >= 0); |
909 |
+ |
s.release(); |
910 |
+ |
String s1 = s.toString(); |
911 |
+ |
assertTrue(s1.indexOf("Permits = 1") >= 0); |
912 |
+ |
s.release(); |
913 |
+ |
String s2 = s.toString(); |
914 |
+ |
assertTrue(s2.indexOf("Permits = 2") >= 0); |
915 |
+ |
} |
916 |
|
|
917 |
|
} |