14 |
|
|
15 |
|
public class SemaphoreTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(SemaphoreTest.class); |
20 |
> |
return new TestSuite(SemaphoreTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
49 |
|
|
50 |
|
|
51 |
|
/** |
52 |
< |
* A runnable calling acquire that expects to be |
53 |
< |
* interrupted |
52 |
> |
* A runnable calling acquire that expects to be interrupted |
53 |
|
*/ |
54 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
55 |
|
final Semaphore lock; |
160 |
|
public void testAcquireReleaseInDifferentThreads() |
161 |
|
throws InterruptedException { |
162 |
|
final Semaphore s = new Semaphore(0, false); |
163 |
< |
Thread t = new Thread(new CheckedRunnable() { |
164 |
< |
public void realRun() throws InterruptedException { |
163 |
> |
Thread t = new Thread(new CheckedRunnable() { |
164 |
> |
public void realRun() throws InterruptedException { |
165 |
|
s.acquire(); |
166 |
|
s.release(); |
167 |
|
s.release(); |
184 |
|
public void testUninterruptibleAcquireReleaseInDifferentThreads() |
185 |
|
throws InterruptedException { |
186 |
|
final Semaphore s = new Semaphore(0, false); |
187 |
< |
Thread t = new Thread(new CheckedRunnable() { |
187 |
> |
Thread t = new Thread(new CheckedRunnable() { |
188 |
|
public void realRun() throws InterruptedException { |
189 |
|
s.acquireUninterruptibly(); |
190 |
|
s.release(); |
209 |
|
public void testTimedAcquireReleaseInDifferentThreads() |
210 |
|
throws InterruptedException { |
211 |
|
final Semaphore s = new Semaphore(1, false); |
212 |
< |
Thread t = new Thread(new CheckedRunnable() { |
212 |
> |
Thread t = new Thread(new CheckedRunnable() { |
213 |
|
public void realRun() throws InterruptedException { |
214 |
|
s.release(); |
215 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
231 |
|
*/ |
232 |
|
public void testAcquire_InterruptedException() |
233 |
|
throws InterruptedException { |
234 |
< |
final Semaphore s = new Semaphore(0, false); |
235 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
236 |
< |
public void realRun() throws InterruptedException { |
234 |
> |
final Semaphore s = new Semaphore(0, false); |
235 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
236 |
> |
public void realRun() throws InterruptedException { |
237 |
|
s.acquire(); |
238 |
|
}}); |
239 |
|
|
240 |
< |
t.start(); |
240 |
> |
t.start(); |
241 |
|
Thread.sleep(SHORT_DELAY_MS); |
242 |
|
t.interrupt(); |
243 |
|
t.join(); |
248 |
|
*/ |
249 |
|
public void testTryAcquire_InterruptedException() |
250 |
|
throws InterruptedException { |
251 |
< |
final Semaphore s = new Semaphore(0, false); |
252 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
253 |
< |
public void realRun() throws InterruptedException { |
251 |
> |
final Semaphore s = new Semaphore(0, false); |
252 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
253 |
> |
public void realRun() throws InterruptedException { |
254 |
|
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
255 |
|
}}); |
256 |
|
|
257 |
< |
t.start(); |
257 |
> |
t.start(); |
258 |
|
Thread.sleep(SHORT_DELAY_MS); |
259 |
|
t.interrupt(); |
260 |
|
t.join(); |
264 |
|
* hasQueuedThreads reports whether there are waiting threads |
265 |
|
*/ |
266 |
|
public void testHasQueuedThreads() throws InterruptedException { |
267 |
< |
final Semaphore lock = new Semaphore(1, false); |
267 |
> |
final Semaphore lock = new Semaphore(1, false); |
268 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
269 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
270 |
|
assertFalse(lock.hasQueuedThreads()); |
289 |
|
* getQueueLength reports number of waiting threads |
290 |
|
*/ |
291 |
|
public void testGetQueueLength() throws InterruptedException { |
292 |
< |
final Semaphore lock = new Semaphore(1, false); |
292 |
> |
final Semaphore lock = new Semaphore(1, false); |
293 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
294 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
295 |
|
assertEquals(0, lock.getQueueLength()); |
314 |
|
* getQueuedThreads includes waiting threads |
315 |
|
*/ |
316 |
|
public void testGetQueuedThreads() throws InterruptedException { |
317 |
< |
final PublicSemaphore lock = new PublicSemaphore(1, false); |
317 |
> |
final PublicSemaphore lock = new PublicSemaphore(1, false); |
318 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
319 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
320 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
522 |
|
public void testAcquireReleaseInDifferentThreads_fair() |
523 |
|
throws InterruptedException { |
524 |
|
final Semaphore s = new Semaphore(0, true); |
525 |
< |
Thread t = new Thread(new CheckedRunnable() { |
526 |
< |
public void realRun() throws InterruptedException { |
525 |
> |
Thread t = new Thread(new CheckedRunnable() { |
526 |
> |
public void realRun() throws InterruptedException { |
527 |
|
s.acquire(); |
528 |
|
s.acquire(); |
529 |
|
s.acquire(); |
548 |
|
public void testAcquireReleaseNInDifferentThreads_fair() |
549 |
|
throws InterruptedException { |
550 |
|
final Semaphore s = new Semaphore(0, true); |
551 |
< |
Thread t = new Thread(new CheckedRunnable() { |
552 |
< |
public void realRun() throws InterruptedException { |
551 |
> |
Thread t = new Thread(new CheckedRunnable() { |
552 |
> |
public void realRun() throws InterruptedException { |
553 |
|
s.acquire(); |
554 |
|
s.release(2); |
555 |
|
s.acquire(); |
569 |
|
public void testAcquireReleaseNInDifferentThreads_fair2() |
570 |
|
throws InterruptedException { |
571 |
|
final Semaphore s = new Semaphore(0, true); |
572 |
< |
Thread t = new Thread(new CheckedRunnable() { |
573 |
< |
public void realRun() throws InterruptedException { |
572 |
> |
Thread t = new Thread(new CheckedRunnable() { |
573 |
> |
public void realRun() throws InterruptedException { |
574 |
|
s.acquire(2); |
575 |
|
s.acquire(2); |
576 |
|
s.release(4); |
592 |
|
public void testTimedAcquireReleaseInDifferentThreads_fair() |
593 |
|
throws InterruptedException { |
594 |
|
final Semaphore s = new Semaphore(1, true); |
595 |
< |
Thread t = new Thread(new CheckedRunnable() { |
596 |
< |
public void realRun() throws InterruptedException { |
595 |
> |
Thread t = new Thread(new CheckedRunnable() { |
596 |
> |
public void realRun() throws InterruptedException { |
597 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
598 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
599 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
601 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
602 |
|
}}); |
603 |
|
|
604 |
< |
t.start(); |
604 |
> |
t.start(); |
605 |
|
s.release(); |
606 |
|
s.release(); |
607 |
|
s.release(); |
616 |
|
public void testTimedAcquireReleaseNInDifferentThreads_fair() |
617 |
|
throws InterruptedException { |
618 |
|
final Semaphore s = new Semaphore(2, true); |
619 |
< |
Thread t = new Thread(new CheckedRunnable() { |
620 |
< |
public void realRun() throws InterruptedException { |
621 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
619 |
> |
Thread t = new Thread(new CheckedRunnable() { |
620 |
> |
public void realRun() throws InterruptedException { |
621 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
622 |
|
s.release(2); |
623 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
623 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
624 |
|
s.release(2); |
625 |
|
}}); |
626 |
|
|
627 |
< |
t.start(); |
627 |
> |
t.start(); |
628 |
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
629 |
|
s.release(2); |
630 |
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
637 |
|
*/ |
638 |
|
public void testAcquire_InterruptedException_fair() |
639 |
|
throws InterruptedException { |
640 |
< |
final Semaphore s = new Semaphore(0, true); |
641 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
642 |
< |
public void realRun() throws InterruptedException { |
640 |
> |
final Semaphore s = new Semaphore(0, true); |
641 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
642 |
> |
public void realRun() throws InterruptedException { |
643 |
|
s.acquire(); |
644 |
|
}}); |
645 |
|
|
646 |
< |
t.start(); |
646 |
> |
t.start(); |
647 |
|
Thread.sleep(SHORT_DELAY_MS); |
648 |
|
t.interrupt(); |
649 |
|
t.join(); |
654 |
|
*/ |
655 |
|
public void testAcquireN_InterruptedException_fair() |
656 |
|
throws InterruptedException { |
657 |
< |
final Semaphore s = new Semaphore(2, true); |
658 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
659 |
< |
public void realRun() throws InterruptedException { |
657 |
> |
final Semaphore s = new Semaphore(2, true); |
658 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
659 |
> |
public void realRun() throws InterruptedException { |
660 |
|
s.acquire(3); |
661 |
|
}}); |
662 |
|
|
663 |
< |
t.start(); |
663 |
> |
t.start(); |
664 |
|
Thread.sleep(SHORT_DELAY_MS); |
665 |
|
t.interrupt(); |
666 |
|
t.join(); |
671 |
|
*/ |
672 |
|
public void testTryAcquire_InterruptedException_fair() |
673 |
|
throws InterruptedException { |
674 |
< |
final Semaphore s = new Semaphore(0, true); |
675 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
676 |
< |
public void realRun() throws InterruptedException { |
674 |
> |
final Semaphore s = new Semaphore(0, true); |
675 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
676 |
> |
public void realRun() throws InterruptedException { |
677 |
|
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
678 |
|
}}); |
679 |
|
|
680 |
< |
t.start(); |
680 |
> |
t.start(); |
681 |
|
Thread.sleep(SHORT_DELAY_MS); |
682 |
|
t.interrupt(); |
683 |
|
t.join(); |
688 |
|
*/ |
689 |
|
public void testTryAcquireN_InterruptedException_fair() |
690 |
|
throws InterruptedException { |
691 |
< |
final Semaphore s = new Semaphore(1, true); |
692 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
693 |
< |
public void realRun() throws InterruptedException { |
691 |
> |
final Semaphore s = new Semaphore(1, true); |
692 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
693 |
> |
public void realRun() throws InterruptedException { |
694 |
|
s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS); |
695 |
|
}}); |
696 |
|
|
697 |
< |
t.start(); |
697 |
> |
t.start(); |
698 |
|
Thread.sleep(SHORT_DELAY_MS); |
699 |
|
t.interrupt(); |
700 |
|
t.join(); |
704 |
|
* getQueueLength reports number of waiting threads |
705 |
|
*/ |
706 |
|
public void testGetQueueLength_fair() throws InterruptedException { |
707 |
< |
final Semaphore lock = new Semaphore(1, true); |
707 |
> |
final Semaphore lock = new Semaphore(1, true); |
708 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
709 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
710 |
|
assertEquals(0, lock.getQueueLength()); |