@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()); }
@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()); }
@Before @After public void clear() throws IOException { s.reducePermits(100); s.release(10); }