Add back in @ConditionalOnBean to auto-config

Also adds auto-configuration tests for server.
This commit is contained in:
Chris Bono
2024-09-24 10:09:47 -05:00
committed by Dave Syer
parent 5bc8a8c3a2
commit 5f4fa5204d
2 changed files with 107 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import io.grpc.BindableService;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -34,12 +35,16 @@ import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
/**
* {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components.
* <p>
* gRPC must be on the classpath and at least one {@link BindableService} bean registered
* in the context in order for the auto-configuration to execute.
*
* @author David Syer
* @author Chris Bono
*/
@AutoConfiguration
@ConditionalOnClass(BindableService.class)
@ConditionalOnBean(BindableService.class)
@EnableConfigurationProperties(GrpcServerProperties.class)
public class GrpcServerAutoConfiguration {

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.grpc.server;
import java.util.List;
import io.grpc.BindableService;
import io.grpc.ServerServiceDefinition;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration;
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link GrpcServerAutoConfiguration}.
*
* @author Chris Bono
*/
class GrpcServerAutoConfigurationTests {
private ApplicationContextRunner validContextRunner() {
BindableService service = mock();
ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build();
when(service.bindService()).thenReturn(serviceDefinition);
return new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class))
.withBean(BindableService.class, () -> service);
}
@Test
void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() {
this.validContextRunner()
.withClassLoader(new FilteredClassLoader(BindableService.class))
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class));
}
@Test
void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class));
}
@Test
void whenHasUserDefinedServerFactoryDoesNotAutoConfigureBean() {
// NOTE: we use noop server lifecycle to avoid startup
GrpcServerFactory customServerFactory = mock(GrpcServerFactory.class);
this.validContextRunner()
.withBean("customServerFactory", GrpcServerFactory.class, () -> customServerFactory)
.withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)
.run((context) -> assertThat(context).getBean(GrpcServerFactory.class).isSameAs(customServerFactory));
}
@Test
void whenHasUserDefinedServerLifecycleDoesNotAutoConfigureBean() {
GrpcServerLifecycle customServerLifecycle = mock(GrpcServerLifecycle.class);
this.validContextRunner()
.withBean("customServerLifecycle", GrpcServerLifecycle.class, () -> customServerLifecycle)
.run((context) -> assertThat(context).getBean(GrpcServerLifecycle.class).isSameAs(customServerLifecycle));
}
@Test
void serverFactoryAutoConfiguredAsExpected() {
// NOTE: we use noop server lifecycle to avoid startup
this.validContextRunner()
.withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)
.withPropertyValues("spring.grpc.server.address=myhost", "spring.grpc.server.port=6160")
.run((context) -> assertThat(context).getBean(DefaultGrpcServerFactory.class)
.hasFieldOrPropertyWithValue("address", "myhost")
.hasFieldOrPropertyWithValue("port", 6160)
.hasFieldOrPropertyWithValue("serverConfigurers", List.of()));
}
@Test
void serverLifecycleAutoConfiguredAsExpected() {
this.validContextRunner()
.run((context) -> assertThat(context).getBean(GrpcServerLifecycle.class)
.hasFieldOrPropertyWithValue("factory", context.getBean(DefaultGrpcServerFactory.class)));
}
}