SentinelLeakyBucket extends AbstractStartStoppable implements LeakyBucket { @Override protected void doStart() throws Exception { future = scheduled.scheduleWithFixedDelay(new Runnable() { @Override public void run() { if(!isOpen()) { return; } if(getTotalSize() != consoleConfig.getSentinelRateLimitSize()) { resize(consoleConfig.getSentinelRateLimitSize()); } } }, PERIODIC_MILLI, PERIODIC_MILLI, TimeUnit.MILLISECONDS); } 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 testDoStart() throws Exception { SentinelLeakyBucket.PERIODIC_MILLI = 10; when(consoleConfig.isSentinelRateLimitOpen()).thenReturn(true); when(consoleConfig.getSentinelRateLimitSize()).thenReturn(3); leakyBucket = new SentinelLeakyBucket(consoleConfig, scheduled); leakyBucket.start(); sleep(20); verify(consoleConfig, atLeast(2)).getSentinelRateLimitSize(); leakyBucket.stop(); }