Merge pull request #50799 from xfocus3

* gh-50791-grpc-health-overall:
  Polish "Honor gRPC overall health setting"
  Honor gRPC overall health setting

Closes gh-50799
This commit is contained in:
Stéphane Nicoll
2026-07-23 14:23:44 +02:00
3 changed files with 23 additions and 3 deletions
@@ -51,7 +51,7 @@ import org.springframework.util.ObjectUtils;
*/
class AutoConfiguredHealthCheckedGrpcComponents implements HealthCheckedGrpcComponents {
private final HealthCheckedGrpcComponent server;
private final @Nullable HealthCheckedGrpcComponent server;
private final Map<String, HealthCheckedGrpcComponent> services;
@@ -68,8 +68,8 @@ class AutoConfiguredHealthCheckedGrpcComponents implements HealthCheckedGrpcComp
() -> StatusAggregator.of(properties.getStatus().getOrder()));
StatusMapper statusMapper = getNonQualifiedBean(beanFactory, StatusMapper.class,
() -> StatusMapper.of(properties.getStatus().getMapping()));
this.server = new AutoConfiguredHealthCheckedGrpcComponent(HealthContributorMembership.always(),
statusAggregator, statusMapper);
this.server = (properties.isIncludeOverallHealth()) ? new AutoConfiguredHealthCheckedGrpcComponent(
HealthContributorMembership.always(), statusAggregator, statusMapper) : null;
this.services = createServices(properties.getService(), beanFactory, statusAggregator, statusMapper);
}
@@ -59,6 +59,15 @@ class AutoConfiguredHealthCheckedGrpcComponentsTests {
});
}
@Test
void getServerWhenIncludeOverallHealthIsFalseReturnsNull() {
this.contextRunner.withPropertyValues("spring.grpc.server.health.include-overall-health=false")
.run((context) -> {
HealthCheckedGrpcComponents components = context.getBean(HealthCheckedGrpcComponents.class);
assertThat(components.getServer()).isNull();
});
}
@Test
void getServiceNamesReturnsServiceNames() {
this.contextRunner
@@ -302,6 +302,17 @@ class GrpcServerHealthAutoConfigurationTests {
});
}
@Test
void whenIncludeOverallHealthIsFalseDoesNotReportOverallHealth() {
this.contextRunner.withPropertyValues("spring.grpc.server.health.include-overall-health=false")
.run((context) -> {
GrpcServerHealth serverHealth = context.getBean(GrpcServerHealth.class);
Map<String, ServingStatus> result = new LinkedHashMap<>();
serverHealth.update(result::put);
assertThat(result).isEmpty();
});
}
@Test
void whenHasGrpcServerHealthBeanDoesNotCreateAdditional() {
this.contextRunner.withUserConfiguration(GrpcServerHealthConfiguration.class).run((context) -> {