-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathReactiveExamplesTest.java
More file actions
118 lines (80 loc) · 3.11 KB
/
ReactiveExamplesTest.java
File metadata and controls
118 lines (80 loc) · 3.11 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
package guru.springframework.reactiveexamples;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
/**
* Created by jt on 8/24/17.
*/
@Slf4j
public class ReactiveExamplesTest {
Person michael = new Person("Michael", "Weston");
Person fiona = new Person("Fiona", "Glenanne");
Person sam = new Person("Sam", "Axe");
Person jesse = new Person("Jesse", "Porter");
@Test
public void monoTests() throws Exception {
//create new person mono
Mono<Person> personMono = Mono.just(michael);
//get person object from mono publisher
Person person = personMono.block();
// output name
log.info(person.sayMyName());
}
@Test
public void monoTransform() throws Exception {
//create new person mono
Mono<Person> personMono = Mono.just(fiona);
PersonCommand command = personMono
.map(PersonCommand::new)
.block();
log.info(command.sayMyName());
}
@Test(expected = NullPointerException.class)
public void monoFilter() throws Exception {
Mono<Person> personMono = Mono.just(sam);
//filter example
Person samAxe = personMono
.filter(person -> person.getFirstName().equalsIgnoreCase("foo"))
.block();
log.info(samAxe.sayMyName()); //throws NPE
}
@Test
public void fluxTest() throws Exception {
Flux<Person> people = Flux.just(michael, fiona, sam, jesse);
people.subscribe(person -> log.info(person.sayMyName()));
}
@Test
public void fluxTestFilter() throws Exception {
Flux<Person> people = Flux.just(michael, fiona, sam, jesse);
people.filter(person -> person.getFirstName().equals(fiona.getFirstName()))
.subscribe(person -> log.info(person.sayMyName()));
}
@Test
public void fluxTestDelayNoOutput() throws Exception {
Flux<Person> people = Flux.just(michael, fiona, sam, jesse);
people.delayElements(Duration.ofSeconds(1))
.subscribe(person -> log.info(person.sayMyName()));
}
@Test
public void fluxTestDelay() throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(1);
Flux<Person> people = Flux.just(michael, fiona, sam, jesse);
people.delayElements(Duration.ofSeconds(1))
.doOnComplete(countDownLatch::countDown)
.subscribe(person -> log.info(person.sayMyName()));
countDownLatch.await();
}
@Test
public void fluxTestFilterDelay() throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(1);
Flux<Person> people = Flux.just(michael, fiona, sam, jesse);
people.delayElements(Duration.ofSeconds(1))
.filter(person -> person.getFirstName().contains("i"))
.doOnComplete(countDownLatch::countDown)
.subscribe(person -> log.info(person.sayMyName()));
countDownLatch.await();
}
}