Affects: Spring-Test-5.3.24

When using WebTestClient in my unit tests to make connections - whether mocked or bound to a real server - the tests eventually fail after a semi-random amount of tests, with each new connection dying in an infinite time-out.

Excerpt from test code:

myWebTestClient.get()
        .uri(endpoint)
        .exchange()
        .returnResult(String.class);

int statusCode = result.getRawStatusCode();
// assert based on status code

This behaviour is undocumented and unintuitive - I would expect the connection to be closed after having performed an exchange and returned & evaluated the result and I would also expect all connections to be terminated when the test execution for that test has finished. In Spring's WebClient (Spring-Webflux-5.3.24), the .exchange method is marked as deprecated with a warning for this reason:

NOTE: Unlike retrieve(), when using exchange(), it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak

It would be great to also have this deprecation & warning in the WebTestClient, or to provide a safer interface that does not leak connections by default. Unfortunately, WebTestClient does not currently offer a retrieve() method, so the workaround for now is to explicitly add a blocking call to the test.

// after .exchange().returnResult(...);
.getResponseBody().blockFirst(Duration.ofSeconds(5));
// after .exchangeToMono(clientResponse -> {})
.block(Duration.ofSeconds(5));

Comment From: rstoyanchev

I would expect the above example to fully consume the response body, so I'm not sure what leads to the behavior you're observing. We have also not run into this through our own tests, and I don't recall similar reports. Is it possible to provide something that reproduces the behavior?

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.