Fix Nacos sub try test conflict

This commit is contained in:
Albumen Kevin 2023-01-16 11:24:24 +08:00
parent a9fce6fa94
commit 012a6ed49e
2 changed files with 71 additions and 1 deletions

View File

@ -122,7 +122,7 @@ public class NacosConnectionManager {
try {
for (int i = 0; i < retryTimes + 1; i++) {
namingService = NacosFactory.createNamingService(nacosProperties);
if (!check || UP.equals(namingService.getServerStatus())) {
if (!check || (UP.equals(namingService.getServerStatus()) && testNamingService(namingService))) {
break;
} else {
logger.warn(LoggerCodeConstants.REGISTRY_NACOS_EXCEPTION, "", "",
@ -152,6 +152,16 @@ public class NacosConnectionManager {
return namingService;
}
private boolean testNamingService(NamingService namingService) {
try {
namingService.getAllInstances("Dubbo-Nacos-Test", false);
return true;
} catch (NacosException e) {
return false;
}
}
private Properties buildNacosProperties(URL url) {
Properties properties = new Properties();
setServerAddr(url, properties);

View File

@ -32,6 +32,7 @@ import org.mockito.Mockito;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.DOWN;
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.UP;
@ -133,4 +134,63 @@ public class NacosConnectionsManagerTest {
}
}
}
@Test
void testDisable() {
try (MockedStatic<NacosFactory> nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class)) {
NamingService mock = new MockNamingService() {
@Override
public String getServerStatus() {
return DOWN;
}
};
nacosFactoryMockedStatic.when(() -> NacosFactory.createNamingService((Properties) any())).thenReturn(mock);
URL url = URL.valueOf("nacos://127.0.0.1:8848")
.addParameter("nacos.retry", 5)
.addParameter("nacos.retry-wait", 10)
.addParameter("nacos.check", "false");
try {
new NacosConnectionManager(url, false, 5, 10);
} catch (Throwable t) {
Assertions.fail(t);
}
}
}
@Test
void testRequest() {
try (MockedStatic<NacosFactory> nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class)) {
AtomicInteger atomicInteger = new AtomicInteger(0);
NamingService mock = new MockNamingService() {
@Override
public List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException {
if (atomicInteger.incrementAndGet() > 10) {
return null;
} else {
throw new NacosException();
}
}
@Override
public String getServerStatus() {
return UP;
}
};
nacosFactoryMockedStatic.when(() -> NacosFactory.createNamingService((Properties) any())).thenReturn(mock);
URL url = URL.valueOf("nacos://127.0.0.1:8848")
.addParameter("nacos.retry", 5)
.addParameter("nacos.retry-wait", 10);
Assertions.assertThrows(IllegalStateException.class, () -> new NacosConnectionManager(url, true, 5, 10));
try {
new NacosConnectionManager(url, true, 5, 10);
} catch (Throwable t) {
Assertions.fail(t);
}
}
}
}