Skip to content

Commit

Permalink
Add Nacos sub try test (#11307)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ authored Jan 16, 2023
1 parent e0971ed commit 486b39f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private NacosConfigServiceWrapper buildConfigService(URL url) {
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, "", "",
Expand Down Expand Up @@ -128,6 +128,15 @@ private NacosConfigServiceWrapper buildConfigService(URL url) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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;
Expand Down Expand Up @@ -83,4 +84,39 @@ public String getServerStatus() {
}
}
}

@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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private NacosConfigServiceWrapper buildConfigService(URL url) {
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, "", "",
Expand Down Expand Up @@ -136,6 +136,14 @@ private NacosConfigServiceWrapper buildConfigService(URL url) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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;
Expand Down Expand Up @@ -82,4 +83,39 @@ public String getServerStatus() {
}
}
}

@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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static NacosNamingServiceWrapper createNamingService(URL connectionURL) {
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, "", "",
Expand Down Expand Up @@ -160,6 +160,15 @@ public static NacosNamingServiceWrapper createNamingService(URL connectionURL) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,6 +97,65 @@ public String getServerStatus() {
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);
Expand Down

0 comments on commit 486b39f

Please sign in to comment.