Skip to content

Commit

Permalink
[ISSUES alibaba#11499] fix address server health check error (alibaba…
Browse files Browse the repository at this point in the history
…#11508)

* [ISSUES alibaba#11499] fix address server health check error

Close alibaba#11499

* [ISSUES alibaba#11499] add some unit tests for HealthController
  • Loading branch information
onewe authored Dec 19, 2023
1 parent ca9d55e commit d40190e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,9 +90,27 @@ public String getHealth() {
}

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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}
}

0 comments on commit d40190e

Please sign in to comment.