Commit 0f64685b authored by Georg Mittendorfer's avatar Georg Mittendorfer

Make test more readable.

parent 549a9018
......@@ -29,7 +29,11 @@ import java.time.Duration;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class RateLimitOperationsTest {
......@@ -142,20 +146,23 @@ public class RateLimitOperationsTest {
public void givenWaitTimeWhenApplyRateLimitWeightThenDecreaseLimitForNextPeriod() {
when(rateLimiterFactory.getListOfRateLimitedCommands()).thenReturn(Collections.singleton("some")); // needed for init
final int limit = 10;
final int weight = 11;
RateLimiter limiter = RateLimiter.of("some-rate-limiter", RateLimiterConfig.custom()
.timeoutDuration(Duration.ofSeconds(1)) // additional reservations go into next period
.limitRefreshPeriod(Duration.ofSeconds(1)) // long enough for several calls
.limitForPeriod(10).build()); // test limit
.limitForPeriod(limit).build()); // test limit
when(rateLimiterFactory.rateLimiter(anyString(), anyString())).thenReturn(limiter);
when(env.getProperty("piri.rate.ip.some.weight", Integer.class, 1)).thenReturn(11);
when(env.getProperty("piri.rate.ip.some.weight", Integer.class, 1)).thenReturn(weight);
RateLimitOperations op = new RateLimitOperations(env, rateLimiterFactory);
limiter.getPermission(Duration.ZERO); // one permission for the call
assertThat(limiter.getMetrics().getAvailablePermissions()).isEqualTo(9);
assertThat(limiter.getMetrics().getAvailablePermissions()).isEqualTo(limit - 1);
op.applySuccessRateLimitWeight("foo", "some"); // decreases by 10 (weight - 1)
assertThat(limiter.getMetrics().getAvailablePermissions()).isEqualTo(-1); // 1 reservation waiting
assertThat(limiter.getMetrics().getAvailablePermissions()).isEqualTo(limit - weight); // 1 reservation waiting
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment