* [ISSUES #11499] fix address server health check error Close #11499 * [ISSUES #11499] add some unit tests for HealthController
This commit is contained in:
parent
ca9d55e264
commit
d40190ee24
@ -18,6 +18,7 @@ package com.alibaba.nacos.config.server.controller;
|
||||
|
||||
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 class HealthController {
|
||||
}
|
||||
|
||||
private boolean isAddressServerHealthy() {
|
||||
Map<String, Object> 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<String, Object> info = lookup.info();
|
||||
if (info == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Object addressServerHealth = info.get("addressServerHealth");
|
||||
if (addressServerHealth == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Boolean.parseBoolean(addressServerHealth.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -88,4 +88,90 @@ public class HealthControllerTest {
|
||||
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<String, Object> 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<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user