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.6 by dl, Mon Nov 3 13:50:07 2003 UTC vs.
Revision 1.11 by dl, Sun Jan 4 00:57:21 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import junit.framework.*;
# Line 74 | Line 75 | public class SemaphoreTest extends JSR16
75      }
76  
77      /**
78 <     * tryAcquire succeeds when sufficent permits, else fails
78 >     * tryAcquire succeeds when sufficient permits, else fails
79       */
80      public void testTryAcquireInSameThread() {
81          Semaphore s = new Semaphore(2, false);
# Line 290 | Line 291 | public class SemaphoreTest extends JSR16
291      }
292  
293      /**
294 +     * hasQueuedThreads reports whether there are waiting threads
295 +     */
296 +    public void testHasQueuedThreads() {
297 +        final Semaphore lock = new Semaphore(1, false);
298 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
299 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
300 +        try {
301 +            assertFalse(lock.hasQueuedThreads());
302 +            lock.acquireUninterruptibly();
303 +            t1.start();
304 +            Thread.sleep(SHORT_DELAY_MS);
305 +            assertTrue(lock.hasQueuedThreads());
306 +            t2.start();
307 +            Thread.sleep(SHORT_DELAY_MS);
308 +            assertTrue(lock.hasQueuedThreads());
309 +            t1.interrupt();
310 +            Thread.sleep(SHORT_DELAY_MS);
311 +            assertTrue(lock.hasQueuedThreads());
312 +            lock.release();
313 +            Thread.sleep(SHORT_DELAY_MS);
314 +            assertFalse(lock.hasQueuedThreads());
315 +            t1.join();
316 +            t2.join();
317 +        } catch(Exception e){
318 +            unexpectedException();
319 +        }
320 +    }
321 +
322 +    /**
323       * getQueueLength reports number of waiting threads
324       */
325      public void testGetQueueLength() {
# Line 350 | Line 380 | public class SemaphoreTest extends JSR16
380          }
381      }
382  
383 +    /**
384 +     * drainPermits reports and removes given number of permits
385 +     */
386 +    public void testDrainPermits() {
387 +        Semaphore s = new Semaphore(0, false);
388 +        assertEquals(0, s.availablePermits());
389 +        assertEquals(0, s.drainPermits());
390 +        s.release(10);
391 +        assertEquals(10, s.availablePermits());
392 +        assertEquals(10, s.drainPermits());
393 +        assertEquals(0, s.availablePermits());
394 +        assertEquals(0, s.drainPermits());
395 +    }
396  
397      /**
398       * reducePermits reduces number of permits
# Line 403 | Line 446 | public class SemaphoreTest extends JSR16
446      }
447  
448      /**
449 <     * tryAcquire succeeds when sufficent permits, else fails
449 >     * tryAcquire succeeds when sufficient permits, else fails
450       */
451      public void testTryAcquireInSameThread_fair() {
452          Semaphore s = new Semaphore(2, true);
# Line 415 | Line 458 | public class SemaphoreTest extends JSR16
458      }
459  
460      /**
461 <     * tryAcquire(n) succeeds when sufficent permits, else fails
461 >     * tryAcquire(n) succeeds when sufficient permits, else fails
462       */
463      public void testTryAcquireNInSameThread_fair() {
464          Semaphore s = new Semaphore(2, true);
# Line 575 | Line 618 | public class SemaphoreTest extends JSR16
618          Thread t = new Thread(new Runnable() {
619                  public void run() {
620                      try {
621 <                        s.acquire(2);
622 <                        s.acquire(2);
623 <                        s.release(4);
621 >                        s.acquire();
622 >                        s.release(2);
623 >                        s.acquire();
624                      } catch(InterruptedException ie){
625                          threadUnexpectedException();
626                      }
# Line 586 | Line 629 | public class SemaphoreTest extends JSR16
629          try {
630              t.start();
631              Thread.sleep(SHORT_DELAY_MS);
589            s.release(6);
590            s.acquire(2);
591            s.acquire(2);
632              s.release(2);
633 +            s.acquire(2);
634 +            s.release(1);
635              t.join();
636          } catch( InterruptedException e){
637              unexpectedException();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines