Repository navigation
Expand file tree
/
Copy pathFluxTransformTest.java
More file actions
127 lines (93 loc) · 3.65 KB
/
Copy pathFluxTransformTest.java
File metadata and controls
127 lines (93 loc) · 3.65 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package flux;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import static reactor.core.scheduler.Schedulers.parallel;
public class FluxTransformTest {
List<String> names = Arrays.asList("mark", "nick", "jack", "kim");
@Test
public void transformUsingMap() {
Flux<String> namesFlux = Flux.fromIterable(names)
.map(s -> s.toUpperCase())
.log();
StepVerifier.create(namesFlux)
.expectNext("MARK", "NICK", "JACK", "KIM")
.verifyComplete();
}
@Test
public void transformUsingMap_Length() {
Flux<Integer> namesFlux = Flux.fromIterable(names)
.map(s -> s.length())
.log();
StepVerifier.create(namesFlux)
.expectNext(4, 4, 4, 3)
.verifyComplete();
}
@Test
public void transformUsingMap_Length_repeat() {
Flux<Integer> namesFlux = Flux.fromIterable(names)
.map(s -> s.length())
.repeat(1)
.log();
StepVerifier.create(namesFlux)
.expectNext(4, 4, 4, 3, 4, 4, 4, 3)
.verifyComplete();
}
@Test
public void transformUsingMap_Filter() {
Flux<String> namesFlux = Flux.fromIterable(names)
.filter(s -> s.length() == 3)
.map(s -> s.toUpperCase())
.log();
StepVerifier.create(namesFlux)
.expectNext("KIM")
.verifyComplete();
}
@Test
public void transformUsingFlatMap() {
Flux<String> stringFlux = Flux.fromIterable(Arrays.asList("A", "B", "C", "D", "E", "F")) // A, B, C, D, E, F
.flatMap(s -> {
return Flux.fromIterable(convertToList(s)); // A -> List[A, newValue] , B -> List[B, newValue]
})//db or external service call that returns a flux -> s -> Flux<String>
.log();
StepVerifier.create(stringFlux)
.expectNextCount(12)
.verifyComplete();
}
private List<String> convertToList(String s) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Arrays.asList(s, "newValue");
}
@Test
public void tranformUsingFlatMap_usingparallel() {
Flux<String> stringFlux = Flux.fromIterable(Arrays.asList("A", "B", "C", "D", "E", "F")) // Flux<String>
.window(2) //Flux<Flux<String> -> (A,B), (C,D), (E,F)
.flatMap((s) ->
s.map(this::convertToList).subscribeOn(parallel())) // Flux<List<String>
.flatMap(s -> Flux.fromIterable(s)) //Flux<String>
.log();
StepVerifier.create(stringFlux)
.expectNextCount(12)
.verifyComplete();
}
@Test
public void tranformUsingFlatMap_parallel_maintain_order() {
Flux<String> stringFlux = Flux.fromIterable(Arrays.asList("A", "B", "C", "D", "E", "F")) // Flux<String>
.window(2) //Flux<Flux<String> -> (A,B), (C,D), (E,F)
/* .concatMap((s) ->
s.map(this::convertToList).`(parallel())) */// Flux<List<String>
.flatMapSequential((s) ->
s.map(this::convertToList).subscribeOn(parallel()))
.flatMap(s -> Flux.fromIterable(s)) //Flux<String>
.log();
StepVerifier.create(stringFlux)
.expectNextCount(12)
.verifyComplete();
}
}