Merge pull request #10865 from singhkaushal01/master
BAEL-4126: what does Mono.defer() do?
This commit is contained in:
commit
36ac880555
@ -32,6 +32,12 @@
|
|||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.mono;
|
package com.baeldung.mono;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@ -13,6 +14,7 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class MonoUnitTest {
|
public class MonoUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenMonoProducesString_thenBlockAndConsume() {
|
public void whenMonoProducesString_thenBlockAndConsume() {
|
||||||
@ -80,4 +82,84 @@ public class MonoUnitTest {
|
|||||||
|
|
||||||
return Mono.just(list);
|
return Mono.just(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmptyList_thenMonoDeferExecuted() {
|
||||||
|
|
||||||
|
Mono<List<String>> emptyList = Mono.defer(() -> monoOfEmptyList());
|
||||||
|
|
||||||
|
//Empty list, hence Mono publisher in switchIfEmpty executed after condition evaluation
|
||||||
|
Flux<String> emptyListElements = emptyList.flatMapIterable(l -> l)
|
||||||
|
.switchIfEmpty(Mono.defer(() -> sampleMsg("EmptyList")))
|
||||||
|
.log();
|
||||||
|
|
||||||
|
StepVerifier.create(emptyListElements)
|
||||||
|
.expectNext("EmptyList")
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNonEmptyList_thenMonoDeferNotExecuted() {
|
||||||
|
|
||||||
|
Mono<List<String>> nonEmptyist = Mono.defer(() -> monoOfList());
|
||||||
|
|
||||||
|
//Non empty list, hence Mono publisher in switchIfEmpty won't evaluated.
|
||||||
|
Flux<String> listElements = nonEmptyist.flatMapIterable(l -> l)
|
||||||
|
.switchIfEmpty(Mono.defer(() -> sampleMsg("NonEmptyList")))
|
||||||
|
.log();
|
||||||
|
|
||||||
|
StepVerifier.create(listElements)
|
||||||
|
.expectNext("one", "two", "three", "four")
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Mono<List<String>> monoOfEmptyList() {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
return Mono.just(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingMonoJust_thenEagerEvaluation() throws InterruptedException {
|
||||||
|
|
||||||
|
Mono<String> 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<String> 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<String> sampleMsg(String str) {
|
||||||
|
log.debug("Call to Retrieve Sample Message!! --> {} at: {}", str, System.currentTimeMillis());
|
||||||
|
return Mono.just(str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user