From d40190ee24af94fa87414b88239c47693c524333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=96=87=E8=B6=85?= Date: Tue, 19 Dec 2023 14:25:54 +0800 Subject: [PATCH] [ISSUES #11499] fix address server health check error (#11508) * [ISSUES #11499] fix address server health check error Close #11499 * [ISSUES #11499] add some unit tests for HealthController --- .../server/controller/HealthController.java | 25 +++++- .../controller/HealthControllerTest.java | 86 +++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java index 7c01b94f103..7a6af68f60d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor; +import com.alibaba.nacos.core.cluster.MemberLookup; import com.alibaba.nacos.core.paramcheck.ExtractorManager; import com.alibaba.nacos.persistence.datasource.DataSourceService; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; @@ -89,9 +90,27 @@ public String getHealth() { } private boolean isAddressServerHealthy() { - Map info = memberManager.getLookup().info(); - return info != null && info.get("addressServerHealth") != null && Boolean - .parseBoolean(info.get("addressServerHealth").toString()); + final MemberLookup lookup = memberManager.getLookup(); + if (lookup == null) { + return false; + } + + final boolean useAddressServer = lookup.useAddressServer(); + if (!useAddressServer) { + return true; + } + + final Map info = lookup.info(); + if (info == null) { + return false; + } + + final Object addressServerHealth = info.get("addressServerHealth"); + if (addressServerHealth == null) { + return false; + } + + return Boolean.parseBoolean(addressServerHealth.toString()); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java index eada6af4c1b..05d6e911f9d 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java @@ -88,4 +88,90 @@ public void testGetHealth() throws Exception { Assert.assertEquals("UP", actualValue); } + + @Test + public void testGetHealthWhenTheLookUpIsNull() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(null); + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("DOWN:address server down. ", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpNotUseAddressServer() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(false); + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("UP", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpInfoIsNull() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(true); + when(memberLookup.info()).thenReturn(null); + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("DOWN:address server down. ", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpInfoIsEmpty() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(true); + when(memberLookup.info()).thenReturn(new HashMap<>()); + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("DOWN:address server down. ", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpInfoIsDown() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(true); + + final HashMap info = new HashMap<>(); + info.put("addressServerHealth", "false"); + when(memberLookup.info()).thenReturn(info); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("DOWN:address server down. ", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpInfoIsUP() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(true); + + final HashMap info = new HashMap<>(); + info.put("addressServerHealth", "true"); + when(memberLookup.info()).thenReturn(info); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("UP", actualValue); + } + + @Test + public void testGetHealthWhenTheLoopUpInfoParseError() throws Exception { + when(dataSourceService.getHealth()).thenReturn("UP"); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(memberLookup.useAddressServer()).thenReturn(true); + + final HashMap info = new HashMap<>(); + info.put("addressServerHealth", "not boolean value"); + when(memberLookup.info()).thenReturn(info); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); + String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); + Assert.assertEquals("DOWN:address server down. ", actualValue); + } }