RateControl { protected boolean checkExpired() throws DatabusException { if (!_enabled) { return false; } if (_expired) { return true; } long throttleDurationInNs = _ra.getDuration(); if (throttleDurationInNs < (_maxthrottleDurationInSecs*DbusConstants.NUM_NSECS_IN_SEC)) { if (_expired != false) { throw new DatabusException("Throttle duration has not expired. _expired must be set to false"); } } else { LOG.info("Ending throttling of events as " + _maxthrottleDurationInSecs + " have expired"); _expired = true; } return _expired; } RateControl(long numEventsPerSec, long throttleDurationInSecs); long incrementEventCount(); boolean isEnabled(); long getNumSleeps(); void resetNumSleeps(); static final String MODULE; static final Logger LOG; }
@Test public void testCheckExpired() throws Exception { RateControl rc1 = new RateControl(10,-1); Assert.assertEquals(false, rc1.checkExpired()); Assert.assertEquals(false, rc1.isEnabled()); RateControl rc2 = new RateControl(10,1); Assert.assertEquals(true, rc2.isEnabled()); boolean expired = true; Field field = rc2.getClass().getDeclaredField("_expired"); field.setAccessible(true); field.set(rc2, expired); Assert.assertEquals(expired, rc2.checkExpired()); 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 field2 = rc.getClass().getDeclaredField("_ra"); field2.setAccessible(true); field2.set(rc, mrm); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC) - 10; mrm.setNanoTime(curTime); Assert.assertEquals(rc.checkExpired(), false); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC); mrm.setNanoTime(curTime); Assert.assertEquals(rc.checkExpired(), true); curTime = (1L * DbusConstants.NUM_NSECS_IN_SEC) + 10; mrm.setNanoTime(curTime); Assert.assertEquals(rc.checkExpired(), true); }