RateControl { protected long computeSleepDuration() throws DatabusException { long numEvents = _ra.getNumTicks(); long numNSecsToHaveExpired = (numEvents * DbusConstants.NUM_NSECS_IN_SEC) / _maxEventsPerSec; long numNSecsExpiredSoFar = _ra.getDuration(); if (numNSecsToHaveExpired > numNSecsExpiredSoFar) { return (numNSecsToHaveExpired - numNSecsExpiredSoFar); } else { return 0; } } RateControl(long numEventsPerSec, long throttleDurationInSecs); long incrementEventCount(); boolean isEnabled(); long getNumSleeps(); void resetNumSleeps(); static final String MODULE; static final Logger LOG; }
@Test public void testComputeSleepDuration() throws Exception { RateControl rc = new RateControl(10,1); Assert.assertEquals(true, rc.isEnabled()); long curTime = 0L; MockRateMonitor mrm = new MockRateMonitor("mock"); mrm.setNanoTime(curTime); mrm.start(); Field field = rc.getClass().getDeclaredField("_ra"); field.setAccessible(true); field.set(rc, mrm); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC) - 10 ; mrm.ticks(10L); mrm.setNanoTime(curTime); Assert.assertEquals(10, rc.computeSleepDuration()); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC) ; mrm.setNanoTime(curTime); Assert.assertEquals(0, rc.computeSleepDuration()); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC) + 10 ; mrm.setNanoTime(curTime); Assert.assertEquals(0, rc.computeSleepDuration()); return; }