diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java index d448fc1a68..7cc67e8114 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java @@ -128,8 +128,8 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur private ServerTransport createTransport() { if (this.transport == RSocketServer.TRANSPORT.WEBSOCKET) { if (this.resourceFactory != null) { - HttpServer httpServer = HttpServer.create() - .tcpConfiguration((tcpServer) -> tcpServer.runOn(this.resourceFactory.getLoopResources())); + HttpServer httpServer = HttpServer.create().tcpConfiguration((tcpServer) -> tcpServer + .runOn(this.resourceFactory.getLoopResources()).addressSupplier(this::getListenAddress)); return WebsocketServerTransport.create(httpServer); } else { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java index 507ff52bea..b811018c50 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java @@ -40,6 +40,7 @@ import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; import org.springframework.core.io.buffer.NettyDataBufferFactory; +import org.springframework.http.client.reactive.ReactorResourceFactory; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.util.SocketUtils; @@ -54,6 +55,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link NettyRSocketServerFactory} * * @author Brian Clozel + * @author Leo Li */ class NettyRSocketServerFactoryTests { @@ -93,6 +95,7 @@ class NettyRSocketServerFactoryTests { String payload = "test payload"; String response = this.requester.route("test").data(payload).retrieveMono(String.class).block(TIMEOUT); + assertThat(this.rSocketServer.address().getPort()).isEqualTo(specificPort); assertThat(response).isEqualTo(payload); assertThat(this.rSocketServer.address().getPort()).isEqualTo(specificPort); } @@ -106,9 +109,29 @@ class NettyRSocketServerFactoryTests { this.requester = createRSocketWebSocketClient(); String payload = "test payload"; String response = this.requester.route("test").data(payload).retrieveMono(String.class).block(TIMEOUT); + assertThat(response).isEqualTo(payload); } + @Test + void websocketTransportWithReactorResource() { + NettyRSocketServerFactory factory = getFactory(); + factory.setTransport(RSocketServer.TRANSPORT.WEBSOCKET); + ReactorResourceFactory resourceFactory = new ReactorResourceFactory(); + resourceFactory.afterPropertiesSet(); + factory.setResourceFactory(resourceFactory); + int specificPort = SocketUtils.findAvailableTcpPort(41000); + factory.setPort(specificPort); + this.rSocketServer = factory.create(new EchoRequestResponseAcceptor()); + this.rSocketServer.start(); + this.requester = createRSocketWebSocketClient(); + String payload = "test payload"; + String response = this.requester.route("test").data(payload).retrieveMono(String.class).block(TIMEOUT); + + assertThat(response).isEqualTo(payload); + assertThat(this.rSocketServer.address().getPort()).isEqualTo(specificPort); + } + @Test void serverCustomizers() { NettyRSocketServerFactory factory = getFactory();