Repository navigation
Expand file tree
/
Copy pathFluxErrorTest.java
More file actions
95 lines (78 loc) · 3.33 KB
/
Copy pathFluxErrorTest.java
File metadata and controls
95 lines (78 loc) · 3.33 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package flux;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import reactor.util.retry.Retry;
import java.time.Duration;
public class FluxErrorTest {
@Test
public void fluxErrorHandling() {
Flux<String> stringFlux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception Occurred")))
.concatWith(Flux.just("D")) // this will not print
.onErrorResume((e) -> { // this block gets executed
System.out.println("Exception is : " + e);
return Flux.just("default", "default1");
});
StepVerifier.create(stringFlux.log())
.expectSubscription()
.expectNext("A", "B", "C", "default", "default1")
//.expectError(RuntimeException.class)
//.verify();
.verifyComplete();
}
@Test
public void fluxErrorHandling_OnErrorReturn() {
Flux<String> stringFlux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception Occurred")))
.concatWith(Flux.just("D")) // this will not execute
.onErrorReturn("default");
StepVerifier.create(stringFlux.log())
.expectSubscription()
.expectNext("A", "B", "C", "default")
.verifyComplete();
}
@Test
public void fluxErrorHandling_OnErrorMap() {
Flux<String> stringFlux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception Occurred")))
.concatWith(Flux.just("D")) // will not execute
.onErrorMap((e) -> new CustomException(e));
StepVerifier.create(stringFlux.log())
.expectSubscription()
.expectNext("A", "B", "C")
.expectError(CustomException.class)
.verify();
}
@Test
public void fluxErrorHandling_OnErrorMap_withRetry() {
Flux<String> stringFlux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception Occurred")))
.concatWith(Flux.just("D"))
.onErrorMap((e) -> new CustomException(e))
.retry(2);
StepVerifier.create(stringFlux.log())
.expectSubscription()
.expectNext("A", "B", "C")
.expectNext("A", "B", "C")
.expectNext("A", "B", "C")
.expectError(CustomException.class)
.verify();
}
@Test
public void fluxErrorHandling_OnErrorMap_withRetryBackoff() {
Flux<String> stringFlux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception Occurred")))
.concatWith(Flux.just("D"))
.onErrorMap((e) -> new CustomException(e))
.retryWhen(Retry.backoff(2, Duration.ofSeconds(5)));
// retry with backoff will give different exception when exhausted
StepVerifier.create(stringFlux.log())
.expectSubscription()
.expectNext("A", "B", "C")
.expectNext("A", "B", "C")
.expectNext("A", "B", "C")
.expectError(IllegalStateException.class)
.verify();
}
}