Created
September 20, 2018 17:45
-
-
Save mzakyalvan/c3a99f65881c0384b0c17096c2fe7d37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.util.ArrayList; | |
import java.util.List; | |
import reactor.core.publisher.Mono; | |
import reactor.test.StepVerifier; | |
public class CombiningSample { | |
public static void main(String[] args) { | |
List<Mono<Boolean>> sources = new ArrayList<>(); | |
/** | |
* Don't use {@link Mono#just}, if you expect execution on subscription time, | |
* instead of assembly time. | |
*/ | |
sources.add(Mono.fromCallable(() -> true)); | |
sources.add(Mono.fromCallable(() -> true)); | |
sources.add(Mono.fromCallable(() -> false)); | |
sources.add(Mono.fromCallable(() -> true)); | |
sources.add(Mono.fromCallable(() -> false)); | |
Mono<Boolean> finalMono = sources.stream() | |
.reduce(Mono.just(true), (previous, next) -> previous.flatMap(value -> value ? next : Mono.empty())); | |
/** | |
* End with complete signal, without on next. | |
*/ | |
StepVerifier.create(finalMono) | |
.expectSubscription() | |
.expectComplete() | |
.verify(); | |
/** | |
* End with false as fallback value. | |
*/ | |
StepVerifier.create(finalMono.switchIfEmpty(Mono.just(false))) | |
.expectSubscription() | |
.expectNext(false) | |
.expectComplete() | |
.verify(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment