Handle nulls. Minor refactoring.

This commit is contained in:
Olga Maciaszek-Sharma
2020-10-30 16:08:24 +01:00
parent 78ca251e96
commit ce73a587f5
2 changed files with 16 additions and 9 deletions

View File

@@ -19,12 +19,14 @@ package org.springframework.cloud.netflix.eureka;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import reactor.core.publisher.Mono;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
@@ -216,9 +218,9 @@ public class EurekaHealthCheckHandler
if (reactiveHealthIndicators != null) {
statusSet.addAll(reactiveHealthIndicators.values().stream()
.map(ReactiveHealthIndicator::health).map(health -> {
return health.block().getStatus();
}).collect(Collectors.toSet()));
.map(ReactiveHealthIndicator::health).map(Mono::block)
.filter(Objects::nonNull).map(Health::getStatus)
.collect(Collectors.toSet()));
}
status = statusAggregator.getAggregateStatus(statusSet);

View File

@@ -48,18 +48,18 @@ import static org.assertj.core.api.Assertions.assertThat;
public class EurekaHealthCheckHandlerTests {
private EurekaHealthCheckHandler healthCheckHandler;
private EurekaHealthCheckHandler healthCheckHandlerWithStatusAggregator;
@Before
public void setUp() throws Exception {
healthCheckHandler = new EurekaHealthCheckHandler(
new OrderedHealthAggregator());
public void setUp() {
healthCheckHandler = new EurekaHealthCheckHandler(new OrderedHealthAggregator());
healthCheckHandlerWithStatusAggregator = new EurekaHealthCheckHandler(
new SimpleStatusAggregator());
}
@Test
public void testNoHealthCheckRegistered() throws Exception {
public void testNoHealthCheckRegistered() {
InstanceStatus status = healthCheckHandler.getStatus(InstanceStatus.UNKNOWN);
assertThat(status).isEqualTo(InstanceStatus.UNKNOWN);
}
@@ -129,12 +129,12 @@ public class EurekaHealthCheckHandlerTests {
configurations);
healthCheckHandler.setApplicationContext(applicationContext);
healthCheckHandler.afterPropertiesSet();
healthCheckHandlerWithStatusAggregator
.setApplicationContext(applicationContext);
healthCheckHandlerWithStatusAggregator.setApplicationContext(applicationContext);
healthCheckHandlerWithStatusAggregator.afterPropertiesSet();
}
public static class UpHealthConfiguration {
@Bean
public HealthIndicator healthIndicator() {
return new AbstractHealthIndicator() {
@@ -148,6 +148,7 @@ public class EurekaHealthCheckHandlerTests {
}
public static class DownHealthConfiguration {
@Bean
public HealthIndicator healthIndicator() {
return new AbstractHealthIndicator() {
@@ -161,6 +162,7 @@ public class EurekaHealthCheckHandlerTests {
}
public static class FatalHealthConfiguration {
@Bean
public HealthIndicator healthIndicator() {
return new AbstractHealthIndicator() {
@@ -174,6 +176,7 @@ public class EurekaHealthCheckHandlerTests {
}
public static class ReactiveUpHealthConfiguration {
@Bean
public ReactiveHealthIndicator reactiveHealthIndicator() {
return new AbstractReactiveHealthIndicator() {
@@ -187,6 +190,7 @@ public class EurekaHealthCheckHandlerTests {
}
public static class ReactiveDownHealthConfiguration {
@Bean
public ReactiveHealthIndicator reactiveHealthIndicator() {
return new AbstractReactiveHealthIndicator() {
@@ -200,6 +204,7 @@ public class EurekaHealthCheckHandlerTests {
}
public static class EurekaDownHealthConfiguration {
@Bean
public DiscoveryHealthIndicator discoveryHealthIndicator() {
return new DiscoveryClientHealthIndicator(null, null) {