Find randomly bound port of RSocket server in Spring
Taking advantage of randomly assigned ports in Spring Boot tests that exercise a RSocket server can be somewhat convoluted. It’s not obvious how to connect a RSocket client when the server is bound to any available port via the property-value of spring.rsocket.server.port=0
.
Ultimately it boils down to registering an application event listener that is notified of RSocketServerInitializedEvent
, from which the address and port of the RSocket server can be derived.
The RSocket client bean should be initialized lazily so that the transport is configured after the port is identified after server startup.