@Test public void basicTest() throws Exception { RejectThrottler rejectThrottler = new RejectThrottler(-1, null); Assert.assertFalse(rejectThrottler.shouldThrottle(-1)); Assert.assertFalse(rejectThrottler.shouldThrottle(0)); Assert.assertFalse(rejectThrottler.shouldThrottle(1)); Assert.assertFalse(rejectThrottler.shouldThrottle(100)); MockClock mockClock = new MockClock(); Meter testMeter = new Meter(mockClock); rejectThrottler = new RejectThrottler(0, testMeter); mockClock.tick(5); Assert.assertTrue(rejectThrottler.shouldThrottle(0)); Assert.assertTrue(rejectThrottler.shouldThrottle(1)); Assert.assertTrue(rejectThrottler.shouldThrottle(21)); mockClock = new MockClock(); testMeter = new Meter(mockClock); rejectThrottler = new RejectThrottler(20, testMeter); Assert.assertFalse(rejectThrottler.shouldThrottle(100)); mockClock.tick(5); Assert.assertEquals("Rate should be 20", 20, testMeter.getOneMinuteRate(), 0.1); Assert.assertTrue("No more requests allowed.", rejectThrottler.shouldThrottle(1)); mockClock.tick(5); Assert.assertTrue("Rate should less than 20. New requests are welcomed.", testMeter.getOneMinuteRate() < 20); Assert.assertFalse(rejectThrottler.shouldThrottle(1)); Assert.assertFalse(rejectThrottler.shouldThrottle(200)); mockClock.tick(5); Assert.assertTrue("Rate should greater than or equal to 20", testMeter.getOneMinuteRate() >= 20); Assert.assertTrue("No more requests.", rejectThrottler.shouldThrottle(1)); }
public boolean shouldThrottle(long observed) { if (desiredRatePerSec < 0) { return false; } if (meter.getOneMinuteRate() >= desiredRatePerSec) { return true; } else { meter.mark(observed); return false; } }
RejectThrottler { public boolean shouldThrottle(long observed) { if (desiredRatePerSec < 0) { return false; } if (meter.getOneMinuteRate() >= desiredRatePerSec) { return true; } else { meter.mark(observed); return false; } } }
RejectThrottler { public boolean shouldThrottle(long observed) { if (desiredRatePerSec < 0) { return false; } if (meter.getOneMinuteRate() >= desiredRatePerSec) { return true; } else { meter.mark(observed); return false; } } RejectThrottler(long desiredRatePerSec, Meter meter);  RejectThrottler(long desiredRatePerSec); }
RejectThrottler { public boolean shouldThrottle(long observed) { if (desiredRatePerSec < 0) { return false; } if (meter.getOneMinuteRate() >= desiredRatePerSec) { return true; } else { meter.mark(observed); return false; } } RejectThrottler(long desiredRatePerSec, Meter meter);  RejectThrottler(long desiredRatePerSec); boolean shouldThrottle(long observed); }
RejectThrottler { public boolean shouldThrottle(long observed) { if (desiredRatePerSec < 0) { return false; } if (meter.getOneMinuteRate() >= desiredRatePerSec) { return true; } else { meter.mark(observed); return false; } } RejectThrottler(long desiredRatePerSec, Meter meter);  RejectThrottler(long desiredRatePerSec); boolean shouldThrottle(long observed);  }