Skip to content

Commit a113099

Browse files
committed
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`
1 parent e921628 commit a113099

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/RateLimiterRequestHandlerAdvice.java

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public RateLimiter.Metrics getMetrics() {
117117
return this.rateLimiter.getMetrics();
118118
}
119119

120+
/**
121+
* Get a {@link RateLimiter} which is configured for this advice.
122+
* @return the {@link RateLimiter} for this advice.
123+
*/
124+
public RateLimiter getRateLimiter() {
125+
return this.rateLimiter;
126+
}
127+
120128
@Override
121129
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
122130
CheckedFunction0<Object> restrictedCall =

spring-integration-core/src/test/java/org/springframework/integration/handler/advice/RateLimiterRequestHandlerAdviceTests.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

2222
import java.time.Duration;
23+
import java.util.concurrent.TimeUnit;
2324

2425
import org.junit.jupiter.api.Test;
2526

@@ -47,12 +48,17 @@
4748
@SpringJUnitConfig
4849
public class RateLimiterRequestHandlerAdviceTests {
4950

51+
private static final Duration REFRESH_PERIOD = Duration.ofMillis(500);
52+
5053
@Autowired
5154
private MessageChannel requestChannel;
5255

5356
@Autowired
5457
private PollableChannel resultChannel;
5558

59+
@Autowired
60+
private RateLimiterRequestHandlerAdvice rateLimiterRequestHandlerAdvice;
61+
5662
@Test
5763
void testRateLimiter() throws InterruptedException {
5864
Message<?> testMessage = new GenericMessage<>("test");
@@ -63,7 +69,11 @@ void testRateLimiter() throws InterruptedException {
6369
.withCauseInstanceOf(RequestNotPermitted.class)
6470
.withMessageContaining("Rate limit exceeded for: ");
6571

66-
Thread.sleep(200);
72+
long howLongToWait =
73+
this.rateLimiterRequestHandlerAdvice.getRateLimiter()
74+
.reservePermission(Duration.ofSeconds(10));
75+
76+
TimeUnit.NANOSECONDS.sleep(howLongToWait + REFRESH_PERIOD.toNanos());
6777

6878
this.requestChannel.send(testMessage);
6979

@@ -77,11 +87,12 @@ public static class ContextConfiguration {
7787

7888
@Bean
7989
public RateLimiterRequestHandlerAdvice rateLimiterRequestHandlerAdvice() {
80-
return new RateLimiterRequestHandlerAdvice(RateLimiterConfig.custom()
81-
.timeoutDuration(Duration.ofMillis(100))
82-
.limitRefreshPeriod(Duration.ofMillis(500))
83-
.limitForPeriod(1)
84-
.build());
90+
return new RateLimiterRequestHandlerAdvice(
91+
RateLimiterConfig.custom()
92+
.timeoutDuration(Duration.ofMillis(100))
93+
.limitRefreshPeriod(REFRESH_PERIOD)
94+
.limitForPeriod(1)
95+
.build());
8596
}
8697

8798
@Bean

0 commit comments

Comments
 (0)