public void setConnectionTokenFillTime(int val) { zks.connThrottle().setFillTime(val); }
@Test public void testThrottleWithRefill() throws InterruptedException { BlueThrottle throttler = new BlueThrottle(); throttler.setMaxTokens(1); throttler.setFillTime(500); Assert.assertTrue("First request should be allowed", throttler.checkLimit(1)); Assert.assertFalse("Second request should be denied", throttler.checkLimit(1)); //wait for the bucket to be refilled Thread.sleep(750); Assert.assertTrue("Third request should be allowed since we've got a new token", throttler.checkLimit(1)); }
@Test public void testThrottleWithoutRefill() { BlueThrottle throttler = new BlueThrottle(); throttler.setMaxTokens(1); throttler.setFillTime(2000); Assert.assertTrue("First request should be allowed", throttler.checkLimit(1)); Assert.assertFalse("Second request should be denied", throttler.checkLimit(1)); }
throttler.setMaxTokens(maxTokens); throttler.setFillCount(maxTokens); throttler.setFillTime(1000); throttler.setFreezeTime(100); throttler.setDropIncrease(0.5);
@Test public void testThrottleWithoutRandomDropping() throws InterruptedException { int maxTokens = 5; BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom()); throttler.setMaxTokens(maxTokens); throttler.setFillCount(maxTokens); throttler.setFillTime(1000); for (int i=0;i<maxTokens;i++) { throttler.checkLimit(1); } Assert.assertEquals("All tokens should be used up by now", throttler.getMaxTokens(), throttler.getDeficit()); Thread.sleep(110); throttler.checkLimit(1); Assert.assertFalse("Dropping probability should still be zero", throttler.getDropChance()>0); //allow bucket to be refilled Thread.sleep(1500); for (int i=0;i<maxTokens;i++) { Assert.assertTrue("The first " + maxTokens + " requests should be allowed", throttler.checkLimit(1)); } for (int i=0;i<maxTokens;i++) { Assert.assertFalse("The latter " + maxTokens + " requests should be denied", throttler.checkLimit(1)); } }