Merge pull request #1373 from IanCao/fix11

Just return empty instance list if service not found #1367
This commit is contained in:
Fury Zhu 2019-06-19 15:34:17 +08:00 committed by GitHub
commit cca2ca9752
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -373,7 +373,11 @@ public class InstanceController {
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new NacosException(NacosException.NOT_FOUND, "service not found: " + serviceName);
if (Loggers.DEBUG_LOG.isDebugEnabled()) {
Loggers.DEBUG_LOG.debug("no instance to serve for service: " + serviceName);
}
result.put("hosts", new JSONArray());
return result;
}
checkIfDisabled(service);

View File

@ -148,4 +148,20 @@ public class InstanceControllerTest extends BaseTest {
Assert.assertEquals(8888, host.getIntValue("port"));
Assert.assertEquals(2.0, host.getDoubleValue("weight"), 0.001);
}
@Test
public void getNullServiceInstances() throws Exception {
Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(null);
MockHttpServletRequestBuilder builder =
MockMvcRequestBuilders.get(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance/list")
.param("serviceName", TEST_SERVICE_NAME);
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
String actualValue = response.getContentAsString();
JSONObject result = JSON.parseObject(actualValue);
JSONArray hosts = result.getJSONArray("hosts");
Assert.assertEquals(hosts.size(), 0);
}
}