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.21 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.27 by dl, Fri May 6 11:22:07 2011 UTC

# Line 1 | Line 1
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# 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);
# Line 169 | Line 169 | public class SemaphoreTest extends JSR16
169              }});
170  
171          t.start();
172 <        Thread.sleep(SHORT_DELAY_MS);
172 >        delay(SHORT_DELAY_MS);
173          s.release();
174          s.release();
175          s.acquire();
# Line 193 | Line 193 | public class SemaphoreTest extends JSR16
193              }});
194  
195          t.start();
196 <        Thread.sleep(SHORT_DELAY_MS);
196 >        delay(SHORT_DELAY_MS);
197          s.release();
198          s.release();
199          s.acquireUninterruptibly();
# Line 204 | Line 204 | public class SemaphoreTest extends JSR16
204  
205  
206      /**
207 <     *  A release in one thread enables a timed acquire in another thread
207 >     * A release in one thread enables a timed acquire in another thread
208       */
209      public void testTimedAcquireReleaseInDifferentThreads()
210          throws InterruptedException {
# Line 212 | Line 212 | public class SemaphoreTest extends JSR16
212          Thread t = new Thread(new CheckedRunnable() {
213              public void realRun() throws InterruptedException {
214                  s.release();
215 <                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
215 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
216                  s.release();
217 <                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
217 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
218              }});
219  
220          t.start();
# Line 238 | Line 238 | public class SemaphoreTest extends JSR16
238              }});
239  
240          t.start();
241 <        Thread.sleep(SHORT_DELAY_MS);
241 >        delay(SHORT_DELAY_MS);
242          t.interrupt();
243          t.join();
244      }
245  
246      /**
247 <     *  A waiting timed acquire blocks interruptibly
247 >     * A waiting timed acquire blocks interruptibly
248       */
249      public void testTryAcquire_InterruptedException()
250          throws InterruptedException {
# Line 255 | Line 255 | public class SemaphoreTest extends JSR16
255              }});
256  
257          t.start();
258 <        Thread.sleep(SHORT_DELAY_MS);
258 >        delay(SHORT_DELAY_MS);
259          t.interrupt();
260          t.join();
261      }
# Line 270 | Line 270 | public class SemaphoreTest extends JSR16
270          assertFalse(lock.hasQueuedThreads());
271          lock.acquireUninterruptibly();
272          t1.start();
273 <        Thread.sleep(SHORT_DELAY_MS);
273 >        delay(SHORT_DELAY_MS);
274          assertTrue(lock.hasQueuedThreads());
275          t2.start();
276 <        Thread.sleep(SHORT_DELAY_MS);
276 >        delay(SHORT_DELAY_MS);
277          assertTrue(lock.hasQueuedThreads());
278          t1.interrupt();
279 <        Thread.sleep(SHORT_DELAY_MS);
279 >        delay(SHORT_DELAY_MS);
280          assertTrue(lock.hasQueuedThreads());
281          lock.release();
282 <        Thread.sleep(SHORT_DELAY_MS);
282 >        delay(SHORT_DELAY_MS);
283          assertFalse(lock.hasQueuedThreads());
284          t1.join();
285          t2.join();
# Line 295 | Line 295 | public class SemaphoreTest extends JSR16
295          assertEquals(0, lock.getQueueLength());
296          lock.acquireUninterruptibly();
297          t1.start();
298 <        Thread.sleep(SHORT_DELAY_MS);
298 >        delay(SHORT_DELAY_MS);
299          assertEquals(1, lock.getQueueLength());
300          t2.start();
301 <        Thread.sleep(SHORT_DELAY_MS);
301 >        delay(SHORT_DELAY_MS);
302          assertEquals(2, lock.getQueueLength());
303          t1.interrupt();
304 <        Thread.sleep(SHORT_DELAY_MS);
304 >        delay(SHORT_DELAY_MS);
305          assertEquals(1, lock.getQueueLength());
306          lock.release();
307 <        Thread.sleep(SHORT_DELAY_MS);
307 >        delay(SHORT_DELAY_MS);
308          assertEquals(0, lock.getQueueLength());
309          t1.join();
310          t2.join();
# Line 321 | Line 321 | public class SemaphoreTest extends JSR16
321          lock.acquireUninterruptibly();
322          assertTrue(lock.getQueuedThreads().isEmpty());
323          t1.start();
324 <        Thread.sleep(SHORT_DELAY_MS);
324 >        delay(SHORT_DELAY_MS);
325          assertTrue(lock.getQueuedThreads().contains(t1));
326          t2.start();
327 <        Thread.sleep(SHORT_DELAY_MS);
327 >        delay(SHORT_DELAY_MS);
328          assertTrue(lock.getQueuedThreads().contains(t1));
329          assertTrue(lock.getQueuedThreads().contains(t2));
330          t1.interrupt();
331 <        Thread.sleep(SHORT_DELAY_MS);
331 >        delay(SHORT_DELAY_MS);
332          assertFalse(lock.getQueuedThreads().contains(t1));
333          assertTrue(lock.getQueuedThreads().contains(t2));
334          lock.release();
335 <        Thread.sleep(SHORT_DELAY_MS);
335 >        delay(SHORT_DELAY_MS);
336          assertTrue(lock.getQueuedThreads().isEmpty());
337          t1.join();
338          t2.join();
# Line 531 | Line 531 | public class SemaphoreTest extends JSR16
531              }});
532  
533          t.start();
534 <        Thread.sleep(SHORT_DELAY_MS);
534 >        delay(SHORT_DELAY_MS);
535          s.release();
536          s.release();
537          s.release();
# Line 556 | Line 556 | public class SemaphoreTest extends JSR16
556              }});
557  
558          t.start();
559 <        Thread.sleep(SHORT_DELAY_MS);
559 >        delay(SHORT_DELAY_MS);
560          s.release(2);
561          s.acquire(2);
562          s.release(1);
# Line 577 | Line 577 | public class SemaphoreTest extends JSR16
577              }});
578  
579          t.start();
580 <        Thread.sleep(SHORT_DELAY_MS);
580 >        delay(SHORT_DELAY_MS);
581          s.release(6);
582          s.acquire(2);
583          s.acquire(2);
# Line 594 | Line 594 | public class SemaphoreTest extends JSR16
594          final Semaphore s = new Semaphore(1, true);
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));
600 <                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
601 <                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
597 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
598 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
599 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
600 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
601 >                assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
602              }});
603  
604          t.start();
# Line 618 | Line 618 | public class SemaphoreTest extends JSR16
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));
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  
# Line 644 | Line 644 | public class SemaphoreTest extends JSR16
644              }});
645  
646          t.start();
647 <        Thread.sleep(SHORT_DELAY_MS);
647 >        delay(SHORT_DELAY_MS);
648          t.interrupt();
649          t.join();
650      }
# Line 661 | Line 661 | public class SemaphoreTest extends JSR16
661              }});
662  
663          t.start();
664 <        Thread.sleep(SHORT_DELAY_MS);
664 >        delay(SHORT_DELAY_MS);
665          t.interrupt();
666          t.join();
667      }
668  
669      /**
670 <     *  A waiting tryAcquire blocks interruptibly
670 >     * A waiting tryAcquire blocks interruptibly
671       */
672      public void testTryAcquire_InterruptedException_fair()
673          throws InterruptedException {
# Line 678 | Line 678 | public class SemaphoreTest extends JSR16
678              }});
679  
680          t.start();
681 <        Thread.sleep(SHORT_DELAY_MS);
681 >        delay(SHORT_DELAY_MS);
682          t.interrupt();
683          t.join();
684      }
685  
686      /**
687 <     *  A waiting tryAcquire(n) blocks interruptibly
687 >     * A waiting tryAcquire(n) blocks interruptibly
688       */
689      public void testTryAcquireN_InterruptedException_fair()
690          throws InterruptedException {
# Line 695 | Line 695 | public class SemaphoreTest extends JSR16
695              }});
696  
697          t.start();
698 <        Thread.sleep(SHORT_DELAY_MS);
698 >        delay(SHORT_DELAY_MS);
699          t.interrupt();
700          t.join();
701      }
# Line 710 | Line 710 | public class SemaphoreTest extends JSR16
710          assertEquals(0, lock.getQueueLength());
711          lock.acquireUninterruptibly();
712          t1.start();
713 <        Thread.sleep(SHORT_DELAY_MS);
713 >        delay(SHORT_DELAY_MS);
714          assertEquals(1, lock.getQueueLength());
715          t2.start();
716 <        Thread.sleep(SHORT_DELAY_MS);
716 >        delay(SHORT_DELAY_MS);
717          assertEquals(2, lock.getQueueLength());
718          t1.interrupt();
719 <        Thread.sleep(SHORT_DELAY_MS);
719 >        delay(SHORT_DELAY_MS);
720          assertEquals(1, lock.getQueueLength());
721          lock.release();
722 <        Thread.sleep(SHORT_DELAY_MS);
722 >        delay(SHORT_DELAY_MS);
723          assertEquals(0, lock.getQueueLength());
724          t1.join();
725          t2.join();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines