8000 Step 2: Flux · blackbeltcoder/reactive-spring@04b2e4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 04b2e4c

Browse files
committed
Step 2: Flux
1 parent 4e8cdc5 commit 04b2e4c

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package workshop;
17+
18+
import reactor.core.publisher.Flux;
19+
import reactor.test.StepVerifier;
20+
21+
import java.time.Duration;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
import java.util.stream.Stream;
25+
26+
import org.junit.Test;
27+
28+
/**
29+
* Tests to get comfortable with {@link Flux}.
30+
*
31+
* @author Mark Paluch
32+
*/
33+
public class Step2Flux {
34+
35+
@Test
36+
public void verifyFluxEmission() {
37+
38+
// TODO: Replace this line by creating a Flux that publishes "Skyler" and "Walter" elements
39+
Flux<String> flux = Flux.empty();
40+
41+
StepVerifier.create(flux).expectNext("Skyler", "Walter").verifyComplete();
42+
}
43+
44+
@Test
45+
public void verifyInfiniteStreamEmission() {
46+
47+
Stream<Double> stream = Stream.generate(Math::random);
48+
49+
// TODO: Replace this line by creating a Flux that generates values from a Java 8 Stream
50+
Flux<Double> flux = Flux.empty();
51+
52+
StepVerifier.create(flux).expectNextCount(5).thenCancel().verify();
53+
}
54+
55+
@Test
56+
public void fluxShouldCreateHelloWorldFromIterable() {
57+
58+
List<String> strings = Arrays.asList("Saul", "Mike");
59+
60+
// TODO: Replace this line by creating a Flux from a resolved collection
61+
Flux<String> flux = Flux.empty();
62+
63+
StepVerifier.create(flux).expectNext("Saul", "Mike").verifyComplete();
64+
}
65+
66+
@Test
67+
public void fluxShouldEmitItemsOverTime() {
68+
69+
Duration duration = Duration.ofSeconds(2);
70+
71+
// TODO: Replace this line by creating a Flux publishing items over time
72+
Flux<Long> flux = Flux.empty();
73+
74+
StepVerifier.create(flux.doOnNext(System.out::println).take(4)).expectNext(0L, 1L, 2L, 3L).verifyComplete();
75+
}
76+
}

reactor/src/test/java/workshop/Verifications.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
import reactor.core.publisher.Mono;
2222
import reactor.test.StepVerifier;
2323

24+
import java.time.Duration;
25+
import java.util.function.Supplier;
26+
27+
import org.reactivestreams.Publisher;
28+
2429
/**
2530
* @author Mark Paluch
2631
*/
@@ -39,4 +44,11 @@ static void verifyDifferentThread(Mono<String> mono, String expectation) {
3944
static void verify(Mono<String> mono, String expectedValue) {
4045
StepVerifier.create(mono).expectNext(expectedValue).verifyComplete();
4146
}
47+
48+
static <T> void verifyDelayedEmission(Supplier<? extends Publisher<? extends T>> supplier) {
49+
50+
StepVerifier.withVirtualTime(supplier).thenAwait(Duration.ofSeconds(50)) //
51+
.expectNextCount(10) //
52+
.verifyComplete();
53+
}
4254
}

0 commit comments

Comments
 (0)
0