mirror of
https://github.com/apache/dubbo.git
synced 2024-10-23 07:04:37 +08:00
Add Nacos sub try test (#11307)
This commit is contained in:
parent
e0971edcd9
commit
486b39f28d
@ -99,7 +99,7 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
|
||||
try {
|
||||
for (int i = 0; i < retryTimes + 1; i++) {
|
||||
tmpConfigServices = NacosFactory.createConfigService(nacosProperties);
|
||||
if (!check || UP.equals(tmpConfigServices.getServerStatus())) {
|
||||
if (!check || (UP.equals(tmpConfigServices.getServerStatus()) && testConfigService(tmpConfigServices))) {
|
||||
break;
|
||||
} else {
|
||||
logger.warn(LoggerCodeConstants.CONFIG_ERROR_NACOS, "", "",
|
||||
@ -128,6 +128,15 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
|
||||
return new NacosConfigServiceWrapper(tmpConfigServices);
|
||||
}
|
||||
|
||||
private boolean testConfigService(ConfigService configService) {
|
||||
try {
|
||||
configService.getConfig("Dubbo-Nacos-Test", "Dubbo-Nacos-Test", DEFAULT_TIMEOUT);
|
||||
return true;
|
||||
} catch (NacosException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Properties buildNacosProperties(URL url) {
|
||||
Properties properties = new Properties();
|
||||
setServerAddr(url, properties);
|
||||
|
@ -27,6 +27,7 @@ import org.mockito.Mockito;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.DOWN;
|
||||
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.UP;
|
||||
@ -83,4 +84,39 @@ class RetryTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRequest() {
|
||||
try (MockedStatic<NacosFactory> nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class)) {
|
||||
AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||
ConfigService mock = new MockConfigService() {
|
||||
@Override
|
||||
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
|
||||
if (atomicInteger.incrementAndGet() > 10) {
|
||||
return "";
|
||||
} else {
|
||||
throw new NacosException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerStatus() {
|
||||
return UP;
|
||||
}
|
||||
};
|
||||
nacosFactoryMockedStatic.when(() -> NacosFactory.createConfigService((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 NacosDynamicConfiguration(url));
|
||||
|
||||
try {
|
||||
new NacosDynamicConfiguration(url);
|
||||
} catch (Throwable t) {
|
||||
Assertions.fail(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ public class NacosMetadataReport extends AbstractMetadataReport {
|
||||
try {
|
||||
for (int i = 0; i < retryTimes + 1; i++) {
|
||||
tmpConfigServices = NacosFactory.createConfigService(nacosProperties);
|
||||
if (!check || UP.equals(tmpConfigServices.getServerStatus())) {
|
||||
if (!check || (UP.equals(tmpConfigServices.getServerStatus()) && testConfigService(tmpConfigServices))) {
|
||||
break;
|
||||
} else {
|
||||
logger.warn(LoggerCodeConstants.CONFIG_ERROR_NACOS, "", "",
|
||||
@ -136,6 +136,14 @@ public class NacosMetadataReport extends AbstractMetadataReport {
|
||||
return new NacosConfigServiceWrapper(tmpConfigServices);
|
||||
}
|
||||
|
||||
private boolean testConfigService(ConfigService configService) {
|
||||
try {
|
||||
configService.getConfig("Dubbo-Nacos-Test", "Dubbo-Nacos-Test", 3000L);
|
||||
return true;
|
||||
} catch (NacosException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Properties buildNacosProperties(URL url) {
|
||||
Properties properties = new Properties();
|
||||
|
@ -27,6 +27,7 @@ import org.mockito.Mockito;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.DOWN;
|
||||
import static com.alibaba.nacos.client.constant.Constants.HealthCheck.UP;
|
||||
@ -82,4 +83,39 @@ class RetryTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRequest() {
|
||||
try (MockedStatic<NacosFactory> nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class)) {
|
||||
AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||
ConfigService mock = new MockConfigService() {
|
||||
@Override
|
||||
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
|
||||
if (atomicInteger.incrementAndGet() > 10) {
|
||||
return "";
|
||||
} else {
|
||||
throw new NacosException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerStatus() {
|
||||
return UP;
|
||||
}
|
||||
};
|
||||
nacosFactoryMockedStatic.when(() -> NacosFactory.createConfigService((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 NacosMetadataReport(url));
|
||||
|
||||
try {
|
||||
new NacosMetadataReport(url);
|
||||
} catch (Throwable t) {
|
||||
Assertions.fail(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ public class NacosNamingServiceUtils {
|
||||
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, "", "",
|
||||
@ -160,6 +160,15 @@ public class NacosNamingServiceUtils {
|
||||
return new NacosNamingServiceWrapper(namingService, retryTimes, sleepMsBetweenRetries);
|
||||
}
|
||||
|
||||
private static boolean testNamingService(NamingService namingService) {
|
||||
try {
|
||||
namingService.getAllInstances("Dubbo-Nacos-Test", false);
|
||||
return true;
|
||||
} catch (NacosException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static Properties buildNacosProperties(URL url) {
|
||||
Properties properties = new Properties();
|
||||
setServerAddr(url, properties);
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.apache.dubbo.registry.nacos.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -96,6 +97,65 @@ class NacosNamingServiceUtilsTest {
|
||||
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, () -> NacosNamingServiceUtils.createNamingService(url));
|
||||
|
||||
try {
|
||||
NacosNamingServiceUtils.createNamingService(url);
|
||||
} catch (Throwable t) {
|
||||
Assertions.fail(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
NacosNamingServiceUtils.createNamingService(url);
|
||||
} 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);
|
||||
|
Loading…
Reference in New Issue
Block a user