RateControl { public long incrementEventCount() throws DatabusException { if (! isEnabled()) { return Long.MIN_VALUE; } if ( checkExpired()) { if (LOG.isDebugEnabled()) { LOG.debug("Throttle duration has expired. Accepting events without rate control"); } return Long.MIN_VALUE; } _ra.tick(); if (! checkRateExceeded()) { return _ra.getNumTicks(); } try { long duration = computeSleepDuration(); sleepToMaintainRate(duration); } catch (InterruptedException ie){} return _ra.getNumTicks(); } RateControl(long numEventsPerSec, long throttleDurationInSecs); long incrementEventCount(); boolean isEnabled(); long getNumSleeps(); void resetNumSleeps(); static final String MODULE; static final Logger LOG; }
@Test public void testInvalidRateMethod() throws Exception { RateControl rc = new RateControl(Long.MIN_VALUE, Long.MIN_VALUE); Assert.assertEquals(Long.MIN_VALUE, rc.incrementEventCount()); }
@Test public void testIncrementEventCount() throws Exception { RateControl rc = new RateControl(2,1); MockRateMonitor mrm = new MockRateMonitor("mock"); mrm.setNanoTime(0L); mrm.start(); Field field = rc.getClass().getDeclaredField("_ra"); field.setAccessible(true); field.set(rc, mrm); long expWakeupTime = DbusConstants.NUM_NSECS_IN_SEC /2; long tc = rc.incrementEventCount(); Assert.assertEquals(tc, 1L); Assert.assertEquals(rc.getNumSleeps(),1); Assert.assertEquals(mrm.getNanoTime(),expWakeupTime); long curTime = expWakeupTime + 1; mrm.setNanoTime(curTime); tc = rc.incrementEventCount(); Assert.assertEquals(tc, 2L); Assert.assertEquals(rc.getNumSleeps(),2); Assert.assertEquals(DbusConstants.NUM_NSECS_IN_SEC+1, mrm.getNanoTime()); }