@Test public void testThrottleDisabled() { BlueThrottle throttler = new BlueThrottle(); Assert.assertTrue("Throttle should be disabled by default", throttler.checkLimit(1)); }
/** * Creates a ZooKeeperServer instance. It sets everything up, but doesn't * actually start listening for clients until run() is invoked. * * @param dataDir the directory to put the data */ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb) { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.txnLogFactory.setServerStats(this.serverStats); this.zkDb = zkDb; this.tickTime = tickTime; setMinSessionTimeout(minSessionTimeout); setMaxSessionTimeout(maxSessionTimeout); listener = new ZooKeeperServerListenerImpl(this); readResponseCache = new ResponseCache(); connThrottle = new BlueThrottle(); LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir()); }
@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)); }