|
| 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 | +} |
0 commit comments