コード例 #1
0
  @Test
  public void tryAcquire() throws Exception {
    assertTrue(s.tryAcquire());
    assertTrue(s.tryAcquire(9));
    assertEquals(0, s.availablePermits());
    assertFalse(s.tryAcquire(1, TimeUnit.SECONDS));
    assertFalse(s.tryAcquire(2, 1, TimeUnit.SECONDS));

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          if (s.tryAcquire(2, 5, TimeUnit.SECONDS)) {
            latch.countDown();
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }.start();

    s.release(2);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(0, s.availablePermits());
  }
コード例 #2
0
  @Test
  public void testAcquire() throws Exception {

    assertEquals(10, s.drainPermits());

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          s.acquire();
          latch.countDown();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }.start();
    Thread.sleep(1000);

    s.release(2);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, s.availablePermits());
  }
コード例 #3
0
 @Before
 @After
 public void clear() throws IOException {
   s.reducePermits(100);
   s.release(10);
 }