diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 6eead97bc2..6bd725b3b5 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -32,6 +32,12 @@ ${assertj.version} test + + org.projectlombok + lombok + RELEASE + test + diff --git a/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java b/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java index 0c6e0c07ef..2738b8f069 100644 --- a/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java +++ b/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java @@ -1,10 +1,12 @@ package com.baeldung.mono; +import lombok.extern.slf4j.Slf4j; import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import java.awt.image.SampleModel; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.ArrayList; @@ -13,6 +15,7 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; +@Slf4j public class MonoUnitTest { @Test public void whenMonoProducesString_thenBlockAndConsume() { @@ -80,4 +83,84 @@ public class MonoUnitTest { return Mono.just(list); } + + @Test + public void whenEmptyList_thenMonoDeferExecuted() { + + Mono> emptyList = Mono.defer(() -> monoOfEmptyList()); + + //Empty list, hence Mono publisher in switchIfEmpty executed after condition evaluation + Flux emptyListElements = emptyList.flatMapIterable(l -> l) + .switchIfEmpty(Mono.defer(() -> sampleMsg("EmptyList"))) + .log(); + + StepVerifier.create(emptyListElements) + .expectNext("EmptyList") + .verifyComplete(); + } + + @Test + public void whenNonEmptyList_thenMonoDeferNotExecuted() { + + Mono> nonEmptyist = Mono.defer(() -> monoOfList()); + + //Non empty list, hence Mono publisher in switchIfEmpty won't evaluated. + Flux listElements = nonEmptyist.flatMapIterable(l -> l) + .switchIfEmpty(Mono.defer(() -> sampleMsg("NonEmptyList"))) + .log(); + + StepVerifier.create(listElements) + .expectNext("one", "two", "three", "four") + .verifyComplete(); + } + + private Mono> monoOfEmptyList() { + List list = new ArrayList<>(); + return Mono.just(list); + } + + @Test + public void whenUsingMonoJust_thenEagerEvaluation() throws InterruptedException { + + Mono msg = sampleMsg("Eager Publisher"); + + log.debug("Intermediate Test Message...."); + + StepVerifier.create(msg) + .expectNext("Eager Publisher") + .verifyComplete(); + + Thread.sleep(5000); + + StepVerifier.create(msg) + .expectNext("Eager Publisher") + .verifyComplete(); + } + + @Test + public void whenUsingMonoDefer_thenLazyEvaluation() throws InterruptedException { + + Mono deferMsg = Mono.defer(() -> sampleMsg("Lazy Publisher")); + + log.debug("Intermediate Test Message...."); + + StepVerifier.create(deferMsg) + .expectNext("Lazy Publisher") + .verifyComplete(); + + Thread.sleep(5000); + + StepVerifier.create(deferMsg) + .expectNext("Lazy Publisher") + .verifyComplete(); + + } + + private Mono sampleMsg(String str) { + log.debug("Call to Retrieve Sample Message!! --> {} at: {}", str, System.currentTimeMillis()); + return Mono.just(str); + } } + + +