SentinelLeakyBucket extends AbstractStartStoppable implements LeakyBucket { public void delayRelease(long time, TimeUnit timeUnit) { scheduled.schedule(new Runnable() { @Override public void run() { release(); } }, time, timeUnit); } SentinelLeakyBucket(ConsoleConfig config, ScheduledExecutorService scheduled); @Override boolean tryAcquire(); @Override void release(); void delayRelease(long time, TimeUnit timeUnit); @Override void resize(int newSize); @Override int references(); @Override int getTotalSize(); static int PERIODIC_MILLI; }
@Test public void testDelayRelease() { when(consoleConfig.isSentinelRateLimitOpen()).thenReturn(true); when(consoleConfig.getSentinelRateLimitSize()).thenReturn(3); leakyBucket = new SentinelLeakyBucket(consoleConfig, scheduled); Assert.assertTrue(leakyBucket.tryAcquire()); Assert.assertTrue(leakyBucket.tryAcquire()); Assert.assertTrue(leakyBucket.tryAcquire()); leakyBucket.delayRelease(30, TimeUnit.MILLISECONDS); Assert.assertFalse(leakyBucket.tryAcquire()); sleep(30); Assert.assertTrue(leakyBucket.tryAcquire()); }