SentinelLeakyBucket extends AbstractStartStoppable implements LeakyBucket { @Override public void release() { if(isOpen()) { origin.release(); } } 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 testRelease() throws InterruptedException { when(consoleConfig.isSentinelRateLimitOpen()).thenReturn(true); when(consoleConfig.getSentinelRateLimitSize()).thenReturn(3); leakyBucket = new SentinelLeakyBucket(consoleConfig, scheduled); int tasks = 100; CountDownLatch latch = new CountDownLatch(tasks); CyclicBarrier barrier = new CyclicBarrier(tasks); AtomicInteger counter = new AtomicInteger(); for(int i = 0; i < tasks; i ++) { executors.execute(new Runnable() { @Override public void run() { try { barrier.await(); } catch (Exception ignore) { } if(leakyBucket.tryAcquire()) { counter.incrementAndGet(); leakyBucket.release(); } latch.countDown(); } }); } latch.await(1500, TimeUnit.MILLISECONDS); Assert.assertTrue(leakyBucket.getTotalSize() < counter.get()); }