ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SemaphoreTest.java (file contents):
Revision 1.19 by jsr166, Tue Nov 17 02:22:50 2009 UTC vs.
Revision 1.22 by jsr166, Tue Dec 1 06:03:49 2009 UTC

# Line 14 | Line 14 | import java.io.*;
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      /**
# Line 49 | Line 49 | public class SemaphoreTest extends JSR16
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;
# Line 161 | Line 160 | public class SemaphoreTest extends JSR16
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();
# Line 185 | Line 184 | public class SemaphoreTest extends JSR16
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();
# Line 210 | Line 209 | public class SemaphoreTest extends JSR16
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));
# Line 232 | Line 231 | public class SemaphoreTest extends JSR16
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();
# Line 249 | Line 248 | public class SemaphoreTest extends JSR16
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();
# Line 265 | Line 264 | public class SemaphoreTest extends JSR16
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());
# Line 290 | Line 289 | public class SemaphoreTest extends JSR16
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());
# Line 315 | Line 314 | public class SemaphoreTest extends JSR16
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());
# Line 523 | Line 522 | public class SemaphoreTest extends JSR16
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();
# Line 549 | Line 548 | public class SemaphoreTest extends JSR16
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();
# Line 570 | Line 569 | public class SemaphoreTest extends JSR16
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);
# Line 593 | Line 592 | public class SemaphoreTest extends JSR16
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));
# Line 602 | Line 601 | public class SemaphoreTest extends JSR16
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();
# Line 617 | Line 616 | public class SemaphoreTest extends JSR16
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));
# Line 638 | Line 637 | public class SemaphoreTest extends JSR16
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();
# Line 655 | Line 654 | public class SemaphoreTest extends JSR16
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();
# Line 672 | Line 671 | public class SemaphoreTest extends JSR16
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();
# Line 689 | Line 688 | public class SemaphoreTest extends JSR16
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();
# Line 705 | Line 704 | public class SemaphoreTest extends JSR16
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());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines