From fb0aa5abb3ffbcaec68ed74e14d9b98faba92262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 20 Jan 2023 11:19:01 +0100 Subject: [PATCH] Fix WebClientIntegrationTests flaky Jetty tests Ensure the port used by the client in malformedResponseChunksOnBodilessEntity and malformedResponseChunksOnEntityWithBody has correctly been set. Closes gh-29862 --- .../client/WebClientIntegrationTests.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index 7e1c0c7adc..880dbcc000 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -34,7 +34,6 @@ import java.time.Duration; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; @@ -50,6 +49,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.core.publisher.Sinks; import reactor.netty.http.client.HttpClient; import reactor.netty.resources.ConnectionProvider; import reactor.test.StepVerifier; @@ -1281,12 +1281,13 @@ class WebClientIntegrationTests { private Mono doMalformedChunkedResponseTest( ClientHttpConnector connector, Function> handler) { - AtomicInteger port = new AtomicInteger(); + Sinks.One portSink = Sinks.one(); Thread serverThread = new Thread(() -> { // No way to simulate a malformed chunked response through MockWebServer. try (ServerSocket serverSocket = new ServerSocket(0)) { - port.set(serverSocket.getLocalPort()); + Sinks.EmitResult result = portSink.tryEmitValue(serverSocket.getLocalPort()); + assertThat(result).isEqualTo(Sinks.EmitResult.OK); Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); @@ -1310,12 +1311,13 @@ class WebClientIntegrationTests { serverThread.start(); - WebClient client = WebClient.builder() - .clientConnector(connector) - .baseUrl("http://localhost:" + port) - .build(); - - return handler.apply(client.post().retrieve()); + return portSink.asMono().flatMap(port -> { + WebClient client = WebClient.builder() + .clientConnector(connector) + .baseUrl("http://localhost:" + port) + .build(); + return handler.apply(client.post().retrieve()); + }); } private void prepareResponse(Consumer consumer) {