|
5 | 5 | import org.junit.jupiter.api.Test; |
6 | 6 | import reactor.core.publisher.Flux; |
7 | 7 | import reactor.core.publisher.Mono; |
| 8 | +import reactor.core.scheduler.Scheduler; |
| 9 | +import reactor.core.scheduler.Schedulers; |
8 | 10 | import reactor.test.StepVerifier; |
9 | 11 | import reactor.util.function.Tuple2; |
10 | 12 |
|
|
14 | 16 | import java.util.List; |
15 | 17 | import java.util.Map; |
16 | 18 | import java.util.Random; |
| 19 | +import java.util.function.Function; |
17 | 20 | import java.util.stream.Stream; |
18 | 21 |
|
19 | 22 | import static es.codeurjc.arpj.TestUtils.printSectionLine; |
@@ -91,10 +94,10 @@ void flux_from_test() { |
91 | 94 | } |
92 | 95 |
|
93 | 96 | @Test |
94 | | - @DisplayName("Test 03: Flux transform") |
95 | | - void flux_transform() { |
| 97 | + @DisplayName("Test 03: Flux map and cast") |
| 98 | + void flux_map_cast() { |
96 | 99 |
|
97 | | - System.out.println("Test 03: Flux transform"); |
| 100 | + System.out.println("Test 03: Flux map and cast"); |
98 | 101 | printTestLine(); |
99 | 102 |
|
100 | 103 | final String[] strings = {"one", "two", "three", "four", "five"}; |
@@ -310,4 +313,102 @@ void flux_aggregate_reduce() { |
310 | 313 | .expectComplete() |
311 | 314 | .verify(); |
312 | 315 | } |
| 316 | + |
| 317 | + @Test |
| 318 | + @DisplayName("Test 08: Transform") |
| 319 | + void flux_transform() { |
| 320 | + |
| 321 | + System.out.println("Test 08: Transform"); |
| 322 | + printTestLine(); |
| 323 | + |
| 324 | + final Scheduler myParallel = Schedulers.newParallel("my-parallel", 2); |
| 325 | + |
| 326 | + final Function<Flux<String>, Flux<Integer>> myTransformation = |
| 327 | + flux -> flux.publishOn(myParallel).map(String::length); |
| 328 | + |
| 329 | + final String[] strings = {"one", "two", "three", "four", "five"}; |
| 330 | + final Flux<Integer> mapToLength = Flux.fromArray(strings).transform(myTransformation).log(); |
| 331 | + |
| 332 | + StepVerifier |
| 333 | + .create(mapToLength) |
| 334 | + .expectNextCount(5) |
| 335 | + .expectComplete() |
| 336 | + .verify(); |
| 337 | + } |
| 338 | + |
| 339 | + @Test |
| 340 | + @DisplayName("Test 09: Concat map") |
| 341 | + void flux_concat_map() { |
| 342 | + |
| 343 | + System.out.println("Test 09: Concat map"); |
| 344 | + printTestLine(); |
| 345 | + |
| 346 | + final Scheduler myParallel = Schedulers.newParallel("my-parallel", 4); |
| 347 | + |
| 348 | + final Flux<Integer> numbers = Flux.range(0, 10) |
| 349 | + .window(2) |
| 350 | + .concatMap(g -> g.flatMap(this::processing).subscribeOn(myParallel)) |
| 351 | + .log(); |
| 352 | + |
| 353 | + StepVerifier |
| 354 | + .create(numbers) |
| 355 | + .expectNextCount(10) |
| 356 | + .expectComplete() |
| 357 | + .verify(); |
| 358 | + |
| 359 | + printTestLine(); |
| 360 | + |
| 361 | + final Flux<Integer> numbers2 = Flux.range(0, 10) |
| 362 | + .window(2) |
| 363 | + .flatMap(g -> g.flatMap(this::processing).subscribeOn(myParallel)) |
| 364 | + .log(); |
| 365 | + |
| 366 | + StepVerifier |
| 367 | + .create(numbers2) |
| 368 | + .expectNextCount(10) |
| 369 | + .expectComplete() |
| 370 | + .verify(); |
| 371 | + } |
| 372 | + |
| 373 | + private Mono<Integer> processing(final Integer input) { |
| 374 | + |
| 375 | + final var random = new Random(); |
| 376 | + |
| 377 | + final var millis = random.nextInt(10) * 100L; |
| 378 | + |
| 379 | + System.out.println("Sleeping [" + input + "] " + millis + " milliseconds... @ " |
| 380 | + + Thread.currentThread().getName()); |
| 381 | + |
| 382 | + try { |
| 383 | + Thread.sleep(millis); |
| 384 | + } catch (InterruptedException e) { |
| 385 | + e.printStackTrace(); |
| 386 | + } |
| 387 | + |
| 388 | + return Mono.just(input); |
| 389 | + } |
| 390 | + |
| 391 | + @Test |
| 392 | + @DisplayName("Test 10: Handle") |
| 393 | + void flux_handle() { |
| 394 | + |
| 395 | + System.out.println("Test 10: Handle"); |
| 396 | + printTestLine(); |
| 397 | + |
| 398 | + final Flux<Integer> numbers = Flux.range(0, 10) |
| 399 | + .handle((n, s) -> { |
| 400 | + if (n % 2 == 0) { |
| 401 | + System.out.println("Even!!! -> " + n); |
| 402 | + s.next(n); |
| 403 | + } else { |
| 404 | + s.next(0); |
| 405 | + } |
| 406 | + }).cast(Integer.class).log(); |
| 407 | + |
| 408 | + StepVerifier |
| 409 | + .create(numbers) |
| 410 | + .expectNextCount(10) |
| 411 | + .expectComplete() |
| 412 | + .verify(); |
| 413 | + } |
313 | 414 | } |
0 commit comments