Fix RateLimiter test to depend on the permission
* Add `RateLimiterRequestHandlerAdvice.getRateLimiter()` to get access to the configured `RateLimiter` for possible low-level operations and management * Fix the `RateLimiterRequestHandlerAdviceTests` to calculate the sleep based on the `reservePermission()` plus `REFRESH_PERIOD`
This commit is contained in:
@@ -117,6 +117,14 @@ public class RateLimiterRequestHandlerAdvice extends AbstractRequestHandlerAdvic
|
||||
return this.rateLimiter.getMetrics();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@link RateLimiter} which is configured for this advice.
|
||||
* @return the {@link RateLimiter} for this advice.
|
||||
*/
|
||||
public RateLimiter getRateLimiter() {
|
||||
return this.rateLimiter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
|
||||
CheckedFunction0<Object> restrictedCall =
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -47,12 +48,17 @@ import io.github.resilience4j.ratelimiter.RequestNotPermitted;
|
||||
@SpringJUnitConfig
|
||||
public class RateLimiterRequestHandlerAdviceTests {
|
||||
|
||||
private static final Duration REFRESH_PERIOD = Duration.ofMillis(500);
|
||||
|
||||
@Autowired
|
||||
private MessageChannel requestChannel;
|
||||
|
||||
@Autowired
|
||||
private PollableChannel resultChannel;
|
||||
|
||||
@Autowired
|
||||
private RateLimiterRequestHandlerAdvice rateLimiterRequestHandlerAdvice;
|
||||
|
||||
@Test
|
||||
void testRateLimiter() throws InterruptedException {
|
||||
Message<?> testMessage = new GenericMessage<>("test");
|
||||
@@ -63,7 +69,11 @@ public class RateLimiterRequestHandlerAdviceTests {
|
||||
.withCauseInstanceOf(RequestNotPermitted.class)
|
||||
.withMessageContaining("Rate limit exceeded for: ");
|
||||
|
||||
Thread.sleep(200);
|
||||
long howLongToWait =
|
||||
this.rateLimiterRequestHandlerAdvice.getRateLimiter()
|
||||
.reservePermission(Duration.ofSeconds(10));
|
||||
|
||||
TimeUnit.NANOSECONDS.sleep(howLongToWait + REFRESH_PERIOD.toNanos());
|
||||
|
||||
this.requestChannel.send(testMessage);
|
||||
|
||||
@@ -77,11 +87,12 @@ public class RateLimiterRequestHandlerAdviceTests {
|
||||
|
||||
@Bean
|
||||
public RateLimiterRequestHandlerAdvice rateLimiterRequestHandlerAdvice() {
|
||||
return new RateLimiterRequestHandlerAdvice(RateLimiterConfig.custom()
|
||||
.timeoutDuration(Duration.ofMillis(100))
|
||||
.limitRefreshPeriod(Duration.ofMillis(500))
|
||||
.limitForPeriod(1)
|
||||
.build());
|
||||
return new RateLimiterRequestHandlerAdvice(
|
||||
RateLimiterConfig.custom()
|
||||
.timeoutDuration(Duration.ofMillis(100))
|
||||
.limitRefreshPeriod(REFRESH_PERIOD)
|
||||
.limitForPeriod(1)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user