-
Notifications
You must be signed in to change notification settings - Fork 123
/
RetryTest.java
61 lines (53 loc) · 2.16 KB
/
RetryTest.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
61
package guides;
import guides.extension.SystemOutCaptureExtension;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@SuppressWarnings("Convert2MethodRef")
@ExtendWith(SystemOutCaptureExtension.class)
public class RetryTest {
@Test
public void testRetryAtMost() {
Uni<String> uni = Uni.createFrom().failure(new Exception("boom"));
Multi<String> multi = Multi.createFrom().failure(new Exception("boom"));
// tag::retry-at-most[]
Uni<String> u = uni
.onFailure().retry().atMost(3);
Multi<String> m = multi
.onFailure().retry().atMost(3);
// end::retry-at-most[]
assertThatThrownBy(() -> u.await().indefinitely()).hasMessageContaining("boom");
assertThatThrownBy(() -> m.collect().asList()
.await().indefinitely()).hasMessageContaining("boom");
}
@Test
public void testRetryWithBackoff() {
Uni<String> uni = Uni.createFrom().failure(new Exception("boom"));
// tag::retry-backoff[]
Uni<String> u = uni
.onFailure().retry()
.withBackOff(Duration.ofMillis(100), Duration.ofSeconds(1))
.atMost(3);
// end::retry-backoff[]
assertThatThrownBy(() -> u.await().indefinitely())
.getCause() // Expected exception is wrapped in a java.util.concurrent.CompletionException
.hasMessageContaining("boom")
.hasSuppressedException(new IllegalStateException("Retries exhausted: 3/3"));
}
@Test
public void testRetryUntil() {
Uni<String> uni = Uni.createFrom().failure(new Exception("boom"));
// tag::retry-until[]
Uni<String> u = uni
.onFailure().retry()
.until(f -> shouldWeRetry(f));
// end::retry-until[]
assertThatThrownBy(() -> u.await().indefinitely()).hasMessageContaining("boom");
}
private boolean shouldWeRetry(Throwable ignored) {
return false;
}
}