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 |
|
} |
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 |
< |
* tryAcquire succeeds when sufficent permits, else fails |
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 |
> |
/** |
95 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
96 |
|
*/ |
97 |
|
public void testTryAcquireInSameThread() { |
98 |
|
Semaphore s = new Semaphore(2, false); |
282 |
|
unexpectedException(); |
283 |
|
} |
284 |
|
} |
285 |
< |
|
285 |
> |
|
286 |
|
/** |
287 |
|
* A waiting timed acquire blocks interruptibly |
288 |
|
*/ |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
+ |
* hasQueuedThreads reports whether there are waiting threads |
312 |
+ |
*/ |
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)); |
317 |
+ |
try { |
318 |
+ |
assertFalse(lock.hasQueuedThreads()); |
319 |
+ |
lock.acquireUninterruptibly(); |
320 |
+ |
t1.start(); |
321 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
322 |
+ |
assertTrue(lock.hasQueuedThreads()); |
323 |
+ |
t2.start(); |
324 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
325 |
+ |
assertTrue(lock.hasQueuedThreads()); |
326 |
+ |
t1.interrupt(); |
327 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
328 |
+ |
assertTrue(lock.hasQueuedThreads()); |
329 |
+ |
lock.release(); |
330 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
331 |
+ |
assertFalse(lock.hasQueuedThreads()); |
332 |
+ |
t1.join(); |
333 |
+ |
t2.join(); |
334 |
+ |
} catch(Exception e){ |
335 |
+ |
unexpectedException(); |
336 |
+ |
} |
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)); |
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)); |
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 |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
< |
* tryAcquire succeeds when sufficent permits, else fails |
466 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
467 |
|
*/ |
468 |
|
public void testTryAcquireInSameThread_fair() { |
469 |
|
Semaphore s = new Semaphore(2, true); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
< |
* tryAcquire(n) succeeds when sufficent permits, else fails |
478 |
> |
* tryAcquire(n) succeeds when sufficient permits, else fails |
479 |
|
*/ |
480 |
|
public void testTryAcquireNInSameThread_fair() { |
481 |
|
Semaphore s = new Semaphore(2, true); |
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); |
686 |
|
|
687 |
|
|
688 |
|
|
689 |
+ |
|
690 |
+ |
|
691 |
|
/** |
692 |
|
* release in one thread enables timed acquire in another thread |
693 |
|
*/ |
794 |
|
unexpectedException(); |
795 |
|
} |
796 |
|
} |
797 |
< |
|
797 |
> |
|
798 |
|
/** |
799 |
|
* A waiting tryAcquire blocks interruptibly |
800 |
|
*/ |
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)); |
870 |
|
} catch(Exception e){ |
871 |
|
unexpectedException(); |
872 |
|
} |
873 |
< |
} |
873 |
> |
} |
874 |
|
|
875 |
|
|
876 |
|
/** |
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 |
|
} |