Add @LocalRSocketServerPort support

Add an appication context initializer to detect and store the
active RSocket port in the Environment under
`local.rsocket.server.port`.

Additionally add a `@LocalServerPort` that provides a convenient
alternative to `@Value`.

See gh-18287

Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
This commit is contained in:
Verónica Vásquez
2019-09-19 19:36:32 -05:00
committed by Phillip Webb
parent ec747d6b2e
commit 3c8fa3bbd0
6 changed files with 200 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ import org.springframework.boot.rsocket.server.RSocketServerFactory;
import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -39,6 +40,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link RSocketServerAutoConfiguration}.
*
* @author Brian Clozel
* @author Verónica Vásquez
*/
class RSocketServerAutoConfigurationTests {
@@ -80,6 +82,17 @@ class RSocketServerAutoConfigurationTests {
.hasSingleBean(ServerRSocketFactoryCustomizer.class));
}
@Test
void shouldSetLocalServerPortWhenRSocketServerPortIsSet() {
reactiveWebContextRunner().withPropertyValues("spring.rsocket.server.port=0")
.withInitializer(new RSocketPortInfoApplicationContextInitializer()).run((context) -> {
assertThat(context).hasSingleBean(RSocketServerFactory.class)
.hasSingleBean(RSocketServerBootstrap.class)
.hasSingleBean(ServerRSocketFactoryCustomizer.class);
assertThat(context.getEnvironment().getProperty("local.rsocket.server.port")).isNotNull();
});
}
@Test
void shouldUseCustomServerBootstrap() {
contextRunner().withUserConfiguration(CustomServerBootstrapConfig.class).run((context) -> assertThat(context)