diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicator.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicator.java index cf7db00a0..7439e3635 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicator.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicator.java @@ -73,11 +73,11 @@ private Status getStatus(Builder builder) { long lastFetch = discoveryClient.getLastSuccessfulRegistryFetchTimePeriod(); if (lastFetch < 0) { - status = new Status("UP", + status = new Status("DOWN", "Eureka discovery client has not yet successfully connected to a Eureka server"); } else if (lastFetch > clientConfig.getRegistryFetchIntervalSeconds() * 2000) { - status = new Status("UP", + status = new Status("DOWN", "Eureka discovery client is reporting failures to connect to a Eureka server"); builder.withDetail("renewalPeriod", instanceConfig.getLeaseRenewalIntervalInSeconds()); builder.withDetail("failCount", lastFetch / clientConfig.getRegistryFetchIntervalSeconds()); diff --git a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicatorTests.java b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicatorTests.java new file mode 100644 index 000000000..43600eb36 --- /dev/null +++ b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaHealthIndicatorTests.java @@ -0,0 +1,84 @@ +/* + * Copyright 2026-present 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.cloud.netflix.eureka; + +import com.netflix.appinfo.EurekaInstanceConfig; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.DiscoveryClient; +import com.netflix.discovery.EurekaClientConfig; +import com.netflix.discovery.shared.Applications; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import org.springframework.boot.health.contributor.Health; +import org.springframework.boot.health.contributor.Status; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link EurekaHealthIndicator}. + * + * @author Lucas Ma + */ +@ExtendWith(MockitoExtension.class) +class EurekaHealthIndicatorTests { + + @Mock + private DiscoveryClient eurekaClient; + + @Mock + private EurekaInstanceConfig instanceConfig; + + @Mock + private EurekaClientConfig clientConfig; + + @Test + void reportsDownWhenRegistryHasNeverBeenFetched() { + when(this.eurekaClient.getInstanceRemoteStatus()).thenReturn(InstanceInfo.InstanceStatus.UP); + when(this.eurekaClient.getLastSuccessfulRegistryFetchTimePeriod()).thenReturn(-1L); + when(this.eurekaClient.getApplications()).thenReturn(new Applications()); + when(this.clientConfig.shouldFetchRegistry()).thenReturn(true); + + Health health = healthIndicator().health(); + + assertThat(health.getStatus()).isEqualTo(Status.DOWN); + } + + @Test + void reportsDownWhenRegistryFetchIsStale() { + when(this.eurekaClient.getInstanceRemoteStatus()).thenReturn(InstanceInfo.InstanceStatus.UP); + when(this.eurekaClient.getLastSuccessfulRegistryFetchTimePeriod()).thenReturn(61_000L); + when(this.eurekaClient.getApplications()).thenReturn(new Applications()); + when(this.clientConfig.shouldFetchRegistry()).thenReturn(true); + when(this.clientConfig.getRegistryFetchIntervalSeconds()).thenReturn(30); + when(this.instanceConfig.getLeaseRenewalIntervalInSeconds()).thenReturn(30); + + Health health = healthIndicator().health(); + + assertThat(health.getStatus()).isEqualTo(Status.DOWN); + assertThat(health.getDetails()).containsEntry("renewalPeriod", 30); + assertThat(health.getDetails()).containsEntry("failCount", 2033L); + } + + private EurekaHealthIndicator healthIndicator() { + return new EurekaHealthIndicator(this.eurekaClient, this.instanceConfig, this.clientConfig); + } + +}