This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.1 by this push:
     new 486b39f28d Add Nacos sub try test (#11307)
486b39f28d is described below

commit 486b39f28d4ca30005c15e1fde852ec057a1148d
Author: Albumen Kevin <[email protected]>
AuthorDate: Mon Jan 16 11:07:20 2023 +0800

    Add Nacos sub try test (#11307)
---
 .../support/nacos/NacosDynamicConfiguration.java   | 11 +++-
 .../configcenter/support/nacos/RetryTest.java      | 36 +++++++++++++
 .../metadata/store/nacos/NacosMetadataReport.java  | 10 +++-
 .../dubbo/metadata/store/nacos/RetryTest.java      | 36 +++++++++++++
 .../nacos/util/NacosNamingServiceUtils.java        | 11 +++-
 .../nacos/util/NacosNamingServiceUtilsTest.java    | 60 ++++++++++++++++++++++
 6 files changed, 161 insertions(+), 3 deletions(-)

diff --git 
a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
 
b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
index beb2400860..5a09e03d06 100644
--- 
a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
+++ 
b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
@@ -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);
diff --git 
a/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/RetryTest.java
 
b/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/RetryTest.java
index e90a5cb016..3be9037380 100644
--- 
a/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/RetryTest.java
+++ 
b/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/RetryTest.java
@@ -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);
+            }
+        }
+    }
 }
diff --git 
a/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
 
b/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
index 9d2a93c24a..70a9a1210c 100644
--- 
a/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
+++ 
b/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
@@ -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();
diff --git 
a/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/RetryTest.java
 
b/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/RetryTest.java
index 9159e076fc..8ff2d826f3 100644
--- 
a/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/RetryTest.java
+++ 
b/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/RetryTest.java
@@ -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);
+            }
+        }
+    }
 }
diff --git 
a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtils.java
 
b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtils.java
index b7809911a2..32c61034a6 100644
--- 
a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtils.java
+++ 
b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtils.java
@@ -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);
diff --git 
a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtilsTest.java
 
b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtilsTest.java
index 35c13d6a41..b2e0b4e515 100644
--- 
a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtilsTest.java
+++ 
b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosNamingServiceUtilsTest.java
@@ -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);

Reply via email to