Skip to content

Commit

Permalink
Add ConditionalOnGrpcServerEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
onobc committed Feb 2, 2025
1 parent 315d340 commit f22f6ad
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.autoconfigure.server;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;

import io.grpc.BindableService;

/**
* {@link Conditional @Conditional} that only matches when the
* {@code io.grpc.BindableService} class is on the classpath and the
* {@code spring.grpc.server.enabled} property is not explicitly set to {@code false}.
*
* @author Freeman
* @author Chris Bono
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnClass(BindableService.class)
@ConditionalOnProperty(prefix = "spring.grpc.server", name = "enabled", matchIfMissing = true)
public @interface ConditionalOnGrpcServerEnabled {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -50,9 +48,8 @@
* @author David Syer
* @author Chris Bono
*/
@AutoConfiguration
@AutoConfigureAfter(GrpcServerFactoryAutoConfiguration.class)
@ConditionalOnClass(BindableService.class)
@AutoConfiguration(after = GrpcServerFactoryAutoConfiguration.class)
@ConditionalOnGrpcServerEnabled
@ConditionalOnBean(BindableService.class)
@EnableConfigurationProperties(GrpcServerProperties.class)
@Import({ GrpcCodecConfiguration.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.grpc.server.service.GrpcServiceDiscoverer;
import org.springframework.util.unit.DataSize;

import io.grpc.BindableService;
import io.grpc.ServerServiceDefinition;
import io.grpc.servlet.jakarta.GrpcServlet;
import io.grpc.servlet.jakarta.ServletServerBuilder;

Expand All @@ -56,7 +54,7 @@
*/
@AutoConfiguration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(BindableService.class)
@ConditionalOnGrpcServerEnabled
@ConditionalOnBean(BindableService.class)
public class GrpcServerFactoryAutoConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

@AutoConfiguration(
afterName = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
@ConditionalOnGrpcServerEnabled
@ConditionalOnClass({ ObservationRegistry.class, ObservationGrpcServerInterceptor.class })
@ConditionalOnBean(ObservationRegistry.class)
@ConditionalOnProperty(name = "spring.grpc.server.observation.enabled", havingValue = "true", matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @author Haris Zujo
*/
@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class)
@ConditionalOnGrpcServerEnabled
@ConditionalOnClass(ProtoReflectionService.class)
@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true)
public class GrpcServerReflectionAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
import org.springframework.grpc.server.GlobalServerInterceptor;
import org.springframework.grpc.server.exception.CompositeGrpcExceptionHandler;
import org.springframework.grpc.server.exception.GrpcExceptionHandler;
Expand All @@ -32,6 +33,7 @@
import io.grpc.Grpc;

@AutoConfiguration
@ConditionalOnGrpcServerEnabled
@ConditionalOnClass(Grpc.class)
@ConditionalOnBean(GrpcExceptionHandler.class)
@ConditionalOnMissingBean(GrpcExceptionHandlerInterceptor.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
import org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration;
import org.springframework.grpc.autoconfigure.server.GrpcServerProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand All @@ -57,6 +58,7 @@
* @author Chris Bono
*/
@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class)
@ConditionalOnGrpcServerEnabled
@ConditionalOnClass(HealthStatusManager.class)
@ConditionalOnProperty(name = "spring.grpc.server.health.enabled", havingValue = "true", matchIfMissing = true)
public class GrpcServerHealthAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
import org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration;
import org.springframework.grpc.autoconfigure.server.exception.GrpcExceptionHandlerAutoConfiguration;
import org.springframework.grpc.server.GlobalServerInterceptor;
Expand All @@ -37,6 +38,7 @@
import io.grpc.internal.GrpcUtil;

@ConditionalOnClass(ObjectPostProcessor.class)
@ConditionalOnGrpcServerEnabled
@AutoConfiguration(before = GrpcExceptionHandlerAutoConfiguration.class, after = SecurityAutoConfiguration.class)
public class GrpcSecurityAutoConfiguration {

Expand Down Expand Up @@ -69,4 +71,4 @@ public <T extends ServerBuilder<T>> ServerBuilderCustomizer<T> securityContextEx

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() {
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.contextRunner().run((context) -> assertThat(context).hasSingleBean(GrpcServerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcServerAutoConfiguration.class));
}

@Test
void whenHasUserDefinedServerLifecycleDoesNotAutoConfigureBean() {
GrpcServerLifecycle customServerLifecycle = mock(GrpcServerLifecycle.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,32 @@ void whenObservationPropertyDisabledThenAutoConfigIsSkipped() {
.run(context -> assertThat(context).doesNotHaveBean(GrpcServerObservationAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.validContextRunner()
.withPropertyValues("spring.grpc.server.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerObservationAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.validContextRunner()
.run((context) -> assertThat(context).hasSingleBean(GrpcServerObservationAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.validContextRunner()
.withPropertyValues("spring.grpc.server.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcServerObservationAutoConfiguration.class));
}

@Test
void whenAllConditionsAreMetThenInterceptorConfiguredAsExpected() {
this.validContextRunner().run((context) -> {
assertThat(context).hasSingleBean(ObservationGrpcServerInterceptor.class)
this.validContextRunner()
.run((context) -> assertThat(context).hasSingleBean(ObservationGrpcServerInterceptor.class)
.has(new Condition<>(beans -> beans.getBeansWithAnnotation(GlobalServerInterceptor.class).size() == 1,
"One global interceptor expected"));
});
"One global interceptor expected")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ void whenReflectionDisabledThenSkipBeanCreation() {
.run((context) -> assertThat(context).doesNotHaveBean(BindableService.class));
}

@Test
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(BindableService.class));
}

@Test
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.contextRunner().run((context) -> assertThat(context).hasSingleBean(BindableService.class));
}

@Test
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(BindableService.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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.autoconfigure.server.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.api.InstanceOfAssertFactories;
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.server.exception.GrpcExceptionHandler;
import org.springframework.grpc.server.exception.GrpcExceptionHandlerInterceptor;
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;

import io.grpc.Grpc;

/**
* Tests for {@link GrpcExceptionHandlerAutoConfiguration}.
*
* @author Chris Bono
*/
class GrpcExceptionHandlerAutoConfigurationTests {

private ApplicationContextRunner contextRunner() {
// NOTE: we use noop server lifecycle to avoid startup
return new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GrpcExceptionHandlerAutoConfiguration.class))
.withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)
.withBean("mockGrpcExceptionHandler", GrpcExceptionHandler.class, Mockito::mock);
}

@Test
void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() {
this.contextRunner()
.withClassLoader(new FilteredClassLoader(Grpc.class))
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenNoGrpcExceptionHandlerRegisteredAutoConfigurationIsSkipped() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GrpcExceptionHandlerAutoConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenExceptionHandlerPropertyNotSetExceptionHandlerIsAutoConfigured() {
this.contextRunner()
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenExceptionHandlerPropertyIsTrueExceptionHandlerIsAutoConfigured() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.exception-handler.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenExceptionHandlerPropertyIsFalseAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.exception-handler.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class));
}

@Test
void whenHasUserDefinedGrpcExceptionHandlerInterceptorDoesNotAutoConfigureBean() {
GrpcExceptionHandlerInterceptor customInterceptor = Mockito.mock();
this.contextRunner()
.withBean("customInterceptor", GrpcExceptionHandlerInterceptor.class, () -> customInterceptor)
.run((context) -> assertThat(context).getBean(GrpcExceptionHandlerInterceptor.class)
.isSameAs(customInterceptor));
}

@Test
void exceptionHandlerInterceptorAutoConfiguredAsExpected() {
this.contextRunner()
.run((context) -> assertThat(context).getBean(GrpcExceptionHandlerInterceptor.class)
.extracting("exceptionHandler.exceptionHandlers",
InstanceOfAssertFactories.array(GrpcExceptionHandler[].class))
.containsExactly(context.getBean(GrpcExceptionHandler.class)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ void whenHealthPropertyIsFalseAutoConfigurationIsSkipped() {
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerHealthAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerHealthAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.run((context) -> assertThat(context).hasSingleBean(GrpcServerHealthAutoConfiguration.class));
}

@Test
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.server.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcServerHealthAutoConfiguration.class));
}

@Test
void healthIsAutoConfiguredBeforeGrpcServerFactory() {
BindableService service = mock();
Expand Down
Loading

0 comments on commit f22f6ad

Please sign in to comment.