-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
c5_CreatingSequence.java
308 lines (263 loc) · 9.8 KB
/
c5_CreatingSequence.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import org.junit.jupiter.api.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
/**
* In this chapter we are going to cover fundamentals of how to create a sequence. At the end of this
* chapter we will tackle more complex methods like generate, create, push, and we will meet them again in following
* chapters like Sinks and Backpressure.
*
* Read first:
*
* https://projectreactor.io/docs/core/release/reference/#which.create
* https://projectreactor.io/docs/core/release/reference/#producing
* https://projectreactor.io/docs/core/release/reference/#_simple_ways_to_create_a_flux_or_mono_and_subscribe_to_it
*
* Useful documentation:
*
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
*
* @author Stefan Dragisic
*/
public class c5_CreatingSequence {
/**
* Emit value that you already have.
*/
@Test
public void value_I_already_have_mono() {
String valueIAlreadyHave = "value";
Mono<String> valueIAlreadyHaveMono = null; //todo: change this line only
StepVerifier.create(valueIAlreadyHaveMono)
.expectNext("value")
.verifyComplete();
}
/**
* Emit potentially null value that you already have.
*/
@Test
public void potentially_null_mono() {
String potentiallyNull = null;
Mono<String> potentiallyNullMono = null; //todo change this line only
StepVerifier.create(potentiallyNullMono)
.verifyComplete();
}
/**
* Emit value from a optional.
*/
@Test
public void optional_value() {
Optional<String> optionalValue = Optional.of("optional");
Mono<String> optionalMono = null; //todo: change this line only
StepVerifier.create(optionalMono)
.expectNext("optional")
.verifyComplete();
}
/**
* Convert callable task to Mono.
*/
@Test
public void callable_counter() {
AtomicInteger callableCounter = new AtomicInteger(0);
Callable<Integer> callable = () -> {
System.out.println("You are incrementing a counter via Callable!");
return callableCounter.incrementAndGet();
};
Mono<Integer> callableCounterMono = null; //todo: change this line only
StepVerifier.create(callableCounterMono.repeat(2))
.expectNext(1, 2, 3)
.verifyComplete();
}
/**
* Convert Future task to Mono.
*/
@Test
public void future_counter() {
AtomicInteger futureCounter = new AtomicInteger(0);
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("You are incrementing a counter via Future!");
return futureCounter.incrementAndGet();
});
Mono<Integer> futureCounterMono = null; //todo: change this line only
StepVerifier.create(futureCounterMono)
.expectNext(1)
.verifyComplete();
}
/**
* Convert Runnable task to Mono.
*/
@Test
public void runnable_counter() {
AtomicInteger runnableCounter = new AtomicInteger(0);
Runnable runnable = () -> {
runnableCounter.incrementAndGet();
System.out.println("You are incrementing a counter via Runnable!");
};
Mono<Integer> runnableMono = null; //todo: change this line only
StepVerifier.create(runnableMono.repeat(2))
.verifyComplete();
Assertions.assertEquals(3, runnableCounter.get());
}
/**
* Create Mono that emits no value but completes successfully.
*/
@Test
public void acknowledged() {
Mono<String> acknowledged = null; //todo: change this line only
StepVerifier.create(acknowledged)
.verifyComplete();
}
/**
* Create Mono that emits no value and never completes.
*/
@Test
public void seen() {
Mono<String> seen = null; //todo: change this line only
StepVerifier.create(seen.timeout(Duration.ofSeconds(5)))
.expectSubscription()
.expectNoEvent(Duration.ofSeconds(4))
.verifyTimeout(Duration.ofSeconds(5));
}
/**
* Create Mono that completes exceptionally with exception `IllegalStateException`.
*/
@Test
public void trouble_maker() {
Mono<String> trouble = null; //todo: change this line
StepVerifier.create(trouble)
.expectError(IllegalStateException.class)
.verify();
}
/**
* Create Flux that will emit all values from the array.
*/
@Test
public void from_array() {
Integer[] array = {1, 2, 3, 4, 5};
Flux<Integer> arrayFlux = null; //todo: change this line only
StepVerifier.create(arrayFlux)
.expectNext(1, 2, 3, 4, 5)
.verifyComplete();
}
/**
* Create Flux that will emit all values from the list.
*/
@Test
public void from_list() {
List<String> list = Arrays.asList("1", "2", "3", "4", "5");
Flux<String> listFlux = null; //todo: change this line only
StepVerifier.create(listFlux)
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
}
/**
* Create Flux that will emit all values from the stream.
*/
@Test
public void from_stream() {
Stream<String> stream = Stream.of("5", "6", "7", "8", "9");
Flux<String> streamFlux = null; //todo: change this line only
StepVerifier.create(streamFlux)
.expectNext("5", "6", "7", "8", "9")
.verifyComplete();
}
/**
* Create Flux that emits number incrementing numbers at interval of 1 second.
*/
@Test
public void interval() {
Flux<Long> interval = null; //todo: change this line only
System.out.println("Interval: ");
StepVerifier.create(interval.take(3).doOnNext(System.out::println))
.expectSubscription()
.expectNext(0L)
.expectNoEvent(Duration.ofMillis(900))
.expectNext(1L)
.expectNoEvent(Duration.ofMillis(900))
.expectNext(2L)
.verifyComplete();
}
/**
* Create Flux that emits range of integers from [-5,5].
*/
@Test
public void range() {
Flux<Integer> range = null; //todo: change this line only
System.out.println("Range: ");
StepVerifier.create(range.doOnNext(System.out::println))
.expectNext(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)
.verifyComplete();
}
/**
* Create Callable that increments the counter and returns the counter value, and then use `repeat()` operator to create Flux that emits
* values from 0 to 10.
*/
@Test
public void repeat() {
AtomicInteger counter = new AtomicInteger(0);
Flux<Integer> repeated = null; //todo: change this line
System.out.println("Repeat: ");
StepVerifier.create(repeated.doOnNext(System.out::println))
.expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.verifyComplete();
}
/**
* Following example is just a basic usage of `generate,`create`,`push` sinks. We will learn how to use them in a
* more complex scenarios when we tackle backpressure.
*
* Answer:
* - What is difference between `generate` and `create`?
* - What is difference between `create` and `push`?
*/
@Test
public void generate_programmatically() {
Flux<Integer> generateFlux = Flux.generate(sink -> {
//todo: fix following code so it emits values from 0 to 5 and then completes
});
//------------------------------------------------------
Flux<Integer> createFlux = Flux.create(sink -> {
//todo: fix following code so it emits values from 0 to 5 and then completes
});
//------------------------------------------------------
Flux<Integer> pushFlux = Flux.push(sink -> {
//todo: fix following code so it emits values from 0 to 5 and then completes
});
StepVerifier.create(generateFlux)
.expectNext(0, 1, 2, 3, 4, 5)
.verifyComplete();
StepVerifier.create(createFlux)
.expectNext(0, 1, 2, 3, 4, 5)
.verifyComplete();
StepVerifier.create(pushFlux)
.expectNext(0, 1, 2, 3, 4, 5)
.verifyComplete();
}
/**
* Something is wrong with the following code. Find the bug and fix it so test passes.
*/
@Test
public void multi_threaded_producer() {
//todo: find a bug and fix it!
Flux<Integer> producer = Flux.push(sink -> {
for (int i = 0; i < 100; i++) {
int finalI = i;
new Thread(() -> sink.next(finalI)).start(); //don't change this line!
}
});
//do not change code below
StepVerifier.create(producer
.doOnNext(System.out::println)
.take(100))
.expectNextCount(100)
.verifyComplete();
}
}