The following function:
private Boolean canDoIt(Parameter param) {
return myService
.getMyObjectInReactiveWay(param)
.map(myObject -> myService.checkMyObjectInImperativeWay(myObject))
.block();
}
is working fine at runtime, but when testing a flow which uses it using WebTestClient
I get the following error:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.4.1.jar:3.4.1]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.MonoFlatMap] :
reactor.core.publisher.Mono.flatMap
I know I am not supposed to use block()
but I have no other choice: the function has to return a Boolean
(not a Mono<Boolean>
). Perhaps there is an alternative way to write it which doesn't use block()
.
Is there a way I can make WebTestClient
not throw that error?
Using Reactor Core version 3.4.6
.
I validate my comment.
block()
checks if the calling thread is compatible with blocking code (thread external to reactor, or a thread of a specific reactor scheduler likeSchedulers.boundedElastic()
).There's 2 ways to handle blocking calls in the middle of a reactive stack:
block()
call to be executed on a blocking compatible scheduler usingscheduleOn
orpublishOn
. Beware, this operators should not be called on the publisher that directly callblock()
, but on the publisher that will "wrap" the block call (see example below).Some references:
And a minimal reproducible example (tested on v3.4.6) giving this output:
Here comes the code: