CommandRateLimiter extends AbstractLimiter<Intent> implements RequestLimiter<Intent> { @Override public boolean tryAcquire(final int streamId, final long requestId, final Intent context) { final Optional<Listener> acquired = acquire(context); return acquired .map( listener -> { registerListener(streamId, requestId, listener); metrics.incInflight(partitionId); return true; }) .orElse(false); } protected  CommandRateLimiter(final CommandRateLimiterBuilder builder, final int partitionId); @Override Optional<Listener> acquire(final Intent intent); @Override boolean tryAcquire(final int streamId, final long requestId, final Intent context); @Override void onResponse(final int streamId, final long requestId); @Override void onIgnore(final int streamId, final long requestId); @Override int getInflightCount(); static CommandRateLimiterBuilder builder();  }
@Test public void shouldAcquire() { assertThat(rateLimiter.tryAcquire(0, 1, context)).isTrue(); }
@Test public void shouldNotAcquireAfterLimit() { IntStream.range(0, limit.getLimit()) .forEach(i -> assertThat(rateLimiter.tryAcquire(0, 1, context)).isTrue()); assertThat(rateLimiter.tryAcquire(0, 1, context)).isFalse(); }
@Test public void shouldAcquireWhenJobCompleteCommandAfterLimit() { IntStream.range(0, limit.getLimit()) .forEach(i -> assertThat(rateLimiter.tryAcquire(0, 1, context)).isTrue()); assertThat(rateLimiter.tryAcquire(0, 1, JobIntent.COMPLETE)).isTrue(); }
@Test public void shouldAcquireWhenJobFailCommandAfterLimit() { IntStream.range(0, limit.getLimit()) .forEach(i -> assertThat(rateLimiter.tryAcquire(0, 1, context)).isTrue()); assertThat(rateLimiter.tryAcquire(0, 1, JobIntent.FAIL)).isTrue(); }