Skip to content

Use "non-blocking" thread for server-less tests in WebTestClient #23936

@bsideup

Description

@bsideup

Problem

As was reported on Gitter, WebTestClient uses the same thread to execute the controller's methods.
In case of Webflux, it means that Reactor's built-in blocking call detection (e.g. calling mono.block()) or more advanced checks like BlockHound will not detect anything, since the thread is not non-blocking.

Since it creates a false positive feeling that the app does not have blocking calls, it would be better if WebTestClient with Webflux would mimic the non-blocking nature of the Webflux calls and move the subscription to e.g. Schedulers.parallel()

Workaround

It is possible to customize the WebTestClient and manually move the subscription to the parallel scheduler:

@Component
public class WebTestClientConfig implements WebTestClientBuilderCustomizer {

    @Override
    public void customize(WebTestClient.Builder builder) {
        builder.filter((clientRequest, next) ->
            next.exchange(clientRequest).subscribeOn(Schedulers.parallel())
        );
    }
}

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions