/** * Invokes the latch if one is set. * * @throws java.lang.InterruptedException because it is declared that way in TimedSemaphore */ @Override public synchronized void acquire() throws InterruptedException { super.acquire(); if (latch != null) { latch.countDown(); } }
/** * Calls acquire() on the semaphore for the specified number of times. * Optionally the latch will also be triggered to synchronize with the * main test thread. */ @Override public void run() { try { for (int i = 0; i < count; i++) { semaphore.acquire(); if (i < latchCount) { latch.countDown(); } } } catch (final InterruptedException iex) { Thread.currentThread().interrupt(); } } }
/** * Tries to call acquire() after shutdown(). This should cause an exception. * * @throws java.lang.InterruptedException so we don't have to catch it */ @Test(expected = IllegalStateException.class) public void testPassAfterShutdown() throws InterruptedException { final TimedSemaphore semaphore = new TimedSemaphore(PERIOD, UNIT, LIMIT); semaphore.shutdown(); semaphore.acquire(); }
/** * Tests the methods for statistics. * * @throws java.lang.InterruptedException so we don't have to catch it */ @Test public void testGetAverageCallsPerPeriod() throws InterruptedException { final ScheduledExecutorService service = EasyMock .createMock(ScheduledExecutorService.class); final ScheduledFuture<?> future = EasyMock.createMock(ScheduledFuture.class); prepareStartTimer(service, future); EasyMock.replay(service, future); final TimedSemaphore semaphore = new TimedSemaphore(service, PERIOD, UNIT, LIMIT); semaphore.acquire(); semaphore.endOfPeriod(); assertEquals("Wrong average (1)", 1.0, semaphore .getAverageCallsPerPeriod(), .005); semaphore.acquire(); semaphore.acquire(); semaphore.endOfPeriod(); assertEquals("Wrong average (2)", 1.5, semaphore .getAverageCallsPerPeriod(), .005); EasyMock.verify(service, future); }
/** * Tests whether the available non-blocking calls can be queried. * * @throws java.lang.InterruptedException so we don't have to catch it */ @Test public void testGetAvailablePermits() throws InterruptedException { final ScheduledExecutorService service = EasyMock .createMock(ScheduledExecutorService.class); final ScheduledFuture<?> future = EasyMock.createMock(ScheduledFuture.class); prepareStartTimer(service, future); EasyMock.replay(service, future); final TimedSemaphore semaphore = new TimedSemaphore(service, PERIOD, UNIT, LIMIT); for (int i = 0; i < LIMIT; i++) { assertEquals("Wrong available count at " + i, LIMIT - i, semaphore .getAvailablePermits()); semaphore.acquire(); } semaphore.endOfPeriod(); assertEquals("Wrong available count in new period", LIMIT, semaphore .getAvailablePermits()); EasyMock.verify(service, future); }
private boolean acquire() { if(limit.getAvailablePermits() > 0) { try { // Blocking if limit is exceeded limit.acquire(); if(log.isDebugEnabled()) { log.debug(String.format("Acquired lock for %s", limit)); } return true; } catch(InterruptedException e) { log.warn(String.format("Failure acquiring lock %s", e.getMessage())); } } return false; } }