-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRateLimiterTest.java
More file actions
executable file
·56 lines (43 loc) · 1.82 KB
/
RateLimiterTest.java
File metadata and controls
executable file
·56 lines (43 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package pl.ts;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import io.vavr.control.Try;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;
class RateLimiterTest {
private static final Logger log = LoggerFactory.getLogger(RateLimiterTest.class);
@Test
void overLimit() {
RateLimiter rateLimiter = RateLimiter.of("name", RateLimiterConfig.custom()
.limitRefreshPeriod(Duration.ofSeconds(2))
.limitForPeriod(1)
.timeoutDuration(Duration.ZERO)
.build());
Runnable withRateLimiter = RateLimiter.decorateRunnable(rateLimiter, this::evacuate);
Try first = Try.runRunnable(withRateLimiter);
Try second = Try.runRunnable(withRateLimiter);
assertThat(first.isSuccess()).isTrue();
assertThat(second.isSuccess()).isFalse();
assertThat(second.getCause()).isInstanceOf(RequestNotPermitted.class);
}
@Test
void timeout() {
RateLimiter rateLimiter = RateLimiter.of("name", RateLimiterConfig.custom()
.limitRefreshPeriod(Duration.ofSeconds(2))
.limitForPeriod(1)
.timeoutDuration(Duration.ofSeconds(2))
.build());
Runnable withRateLimiter = RateLimiter.decorateRunnable(rateLimiter, this::evacuate);
Try first = Try.runRunnable(withRateLimiter);
Try second = Try.runRunnable(withRateLimiter);
assertThat(first.isSuccess()).isTrue();
assertThat(second.isSuccess()).isTrue();
}
private void evacuate() {
log.info("evacuated");
}
}