-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeLimiterTest.java
executable file
·60 lines (45 loc) · 1.59 KB
/
TimeLimiterTest.java
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
57
58
59
60
package pl.ts;
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import io.vavr.control.Try;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class TimeLimiterTest {
private TimeLimiter timeLimiter;
@BeforeEach
void setUp() {
timeLimiter = TimeLimiter.of(TimeLimiterConfig.custom()
.timeoutDuration(Duration.ofMillis(100))
.build());
}
@Test
void less() {
Callable<Integer> wait42 = TimeLimiter.decorateFutureSupplier(timeLimiter, () -> sleep(42));
Try<Integer> dream = Try.ofCallable(wait42);
assertThat(dream).contains(42);
}
@Test
void more() {
Callable<Integer> wait666 = TimeLimiter.decorateFutureSupplier(timeLimiter, () -> sleep(666));
Try<Integer> dream = Try.ofCallable(wait666);
assertThat(dream).isEmpty();
assertThat(dream.getCause()).isInstanceOf(TimeoutException.class);
}
private Future<Integer> sleep(int millis) {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(millis);
return millis;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
});
}
}