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

jimin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 3e4bc61698 test: add mock test for seata-discovery-consul module 
(#7227)
3e4bc61698 is described below

commit 3e4bc616984f76db20d7a87b758bd22b1bffa77e
Author: YoWuwuuuw <2216348...@qq.com>
AuthorDate: Tue Mar 18 00:27:54 2025 +0800

    test: add mock test for seata-discovery-consul module (#7227)
---
 changes/en-us/2.x.md                               |   1 +
 changes/zh-cn/2.x.md                               |   1 +
 .../registry/consul/ConsulRegistryServiceImpl.java |   2 -
 .../consul/ConsulRegistryServiceImplTest.java      | 114 +++++++++++++++++----
 4 files changed, 95 insertions(+), 23 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index b5d7c37ece..f8cde14720 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -76,6 +76,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7199](https://github.com/apache/incubator-seata/pull/7199)] add some UT 
cases for client processor
 - [[#7203](https://github.com/apache/incubator-seata/pull/7203)] Refactored 
tests in rm.datasource.sql.Druid and seata-sqlparser-druid module
 - [[#7221](https://github.com/apache/incubator-seata/pull/7221)] add UT for 
gRPC Encoder/Decode
+- [[#7227](https://github.com/apache/incubator-seata/pull/7227)] add mock test 
for seata-discovery-consul module
 
 
 ### refactor:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index a794a5c153..bdfb2c0e67 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -77,6 +77,7 @@
 - [[#7199](https://github.com/apache/incubator-seata/pull/7199)] 增加 client 
processor 单测用例
 - [[#7203](https://github.com/apache/incubator-seata/pull/7203)] 重构了 
rm.datasource.sql.Druid 和 seata-sqlparser-druid 模块中的测试
 - [[#7221](https://github.com/apache/incubator-seata/pull/7221)] 增加 gRPC 
Encoder/Decoder的测试用例
+- [[#7227](https://github.com/apache/incubator-seata/pull/7227)] 为 
seata-discovery-consul 增加mock测试
 
 
 ### refactor:
diff --git 
a/discovery/seata-discovery-consul/src/main/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImpl.java
 
b/discovery/seata-discovery-consul/src/main/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImpl.java
index 72fa626b1f..0d27e4ef53 100644
--- 
a/discovery/seata-discovery-consul/src/main/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImpl.java
+++ 
b/discovery/seata-discovery-consul/src/main/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImpl.java
@@ -126,7 +126,6 @@ public class ConsulRegistryServiceImpl implements 
RegistryService<ConsulListener
         NetUtil.validAddress(address);
         doRegister(address);
         RegistryHeartBeats.addHeartBeat(REGISTRY_TYPE, address, 
this::doRegister);
-
     }
 
     private void doRegister(InetSocketAddress address) {
@@ -170,7 +169,6 @@ public class ConsulRegistryServiceImpl implements 
RegistryService<ConsulListener
             throw new ConfigNotFoundException("%s configuration item is 
required", missingDataId);
         }
         return lookupByCluster(cluster);
-
     }
 
     private List<InetSocketAddress> lookupByCluster(String cluster) throws 
Exception {
diff --git 
a/discovery/seata-discovery-consul/src/test/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImplTest.java
 
b/discovery/seata-discovery-consul/src/test/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImplTest.java
index 7591e718a6..955c1865a2 100644
--- 
a/discovery/seata-discovery-consul/src/test/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImplTest.java
+++ 
b/discovery/seata-discovery-consul/src/test/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImplTest.java
@@ -16,47 +16,119 @@
  */
 package org.apache.seata.discovery.registry.consul;
 
-import org.apache.seata.discovery.registry.RegistryService;
+import com.ecwid.consul.transport.RawResponse;
+import com.ecwid.consul.v1.ConsulClient;
+import com.ecwid.consul.v1.Response;
+import com.ecwid.consul.v1.health.model.HealthService;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.config.exception.ConfigNotFoundException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
 
+import java.lang.reflect.Field;
 import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
 
+import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-
+import static org.mockito.Mockito.when;
 
 
 public class ConsulRegistryServiceImplTest {
 
+    final String TEST_CLUSTER_NAME = "testCluster";
+
+    ConsulClient client;
+    Configuration configuration;
+    ConsulRegistryServiceImpl service;
+
+    @BeforeEach
+    public void init() throws Exception {
+        service = (ConsulRegistryServiceImpl) new 
ConsulRegistryProvider().provide();
+        client = mock(ConsulClient.class);
+        this.setClient(service, client);
+
+        configuration = mock(Configuration.class);
+    }
 
     @Test
-    public void testRegister() throws Exception {
-        RegistryService registryService = 
mock(ConsulRegistryServiceImpl.class);
-        InetSocketAddress inetSocketAddress = new 
InetSocketAddress("127.0.0.1", 8091);
-        registryService.register(inetSocketAddress);
-        verify(registryService).register(inetSocketAddress);
+    public void testGetInstance() {
+        Assertions.assertEquals(ConsulRegistryServiceImpl.getInstance(), 
service);
     }
 
     @Test
-    public void testUnregister() throws Exception {
-        RegistryService registryService = 
mock(ConsulRegistryServiceImpl.class);
-        InetSocketAddress inetSocketAddress = new 
InetSocketAddress("127.0.0.1", 8091);
-        registryService.unregister(inetSocketAddress);
-        verify(registryService).unregister(inetSocketAddress);
+    public void testRegister() throws Exception {
+        InetSocketAddress inetSocketAddress = new 
InetSocketAddress("127.0.0.1", 8080);
+
+        when(client.agentServiceRegister(any())).thenReturn(null);
+        service.register(inetSocketAddress);
+        verify(client).agentServiceRegister(any(), any());
+
+        when(client.agentServiceDeregister(any())).thenReturn(null);
+        service.unregister(inetSocketAddress);
+        verify(client).agentServiceDeregister(any(), any());
     }
 
     @Test
-    public void testSubscribe() throws Exception {
-        RegistryService registryService = 
mock(ConsulRegistryServiceImpl.class);
+    public void testSubscribeAndLookup() throws Exception {
         ConsulListener consulListener = mock(ConsulListener.class);
-        registryService.subscribe("test", consulListener);
-        verify(registryService).subscribe("test", consulListener);
+
+        ExecutorService executorService = mock(ExecutorService.class);
+        setExecutorService(executorService);
+
+        Response<List<HealthService>> response = new Response<>(new 
ArrayList<>(), mock(RawResponse.class));
+        when(client.getHealthServices(any(), any())).thenReturn(response);
+
+        service.subscribe(TEST_CLUSTER_NAME, consulListener);
+
+        Assertions.assertNotNull(getMap("listenerMap").get(TEST_CLUSTER_NAME));
+        Assertions.assertNotNull(getMap("notifiers").get(TEST_CLUSTER_NAME));
+        verify(executorService).submit(any(Runnable.class));
+
+        try (MockedStatic<ConfigurationFactory> 
configurationFactoryMockedStatic = 
Mockito.mockStatic(ConfigurationFactory.class)) {
+            
configurationFactoryMockedStatic.when(ConfigurationFactory::getInstance).thenReturn(configuration);
+
+            // normal condition
+            when(configuration.getConfig(any())).thenReturn(TEST_CLUSTER_NAME);
+            List<InetSocketAddress> addresses = new ArrayList<>();
+            getMap("clusterAddressMap").put(TEST_CLUSTER_NAME, addresses);
+            Assertions.assertEquals(addresses, service.lookup("testGroup"));
+
+            // when config exc
+            when(configuration.getConfig(any())).thenReturn(null);
+            Assertions.assertThrows(ConfigNotFoundException.class, () -> {
+                service.lookup("testGroup");
+            });
+        }
+
+        service.unsubscribe(TEST_CLUSTER_NAME, consulListener);
+        Assertions.assertNull(getMap("notifiers").get(TEST_CLUSTER_NAME));
     }
 
-    @Test
-    public void testLookup() throws Exception {
-        RegistryService registryService = 
mock(ConsulRegistryServiceImpl.class);
-        registryService.lookup("test-key");
-        verify(registryService).lookup("test-key");
+
+    private void setClient(ConsulRegistryServiceImpl service, ConsulClient 
client) throws Exception {
+        Field clientField = 
ConsulRegistryServiceImpl.class.getDeclaredField("client");
+        clientField.setAccessible(true);
+        clientField.set(service, client);
+    }
+
+    private void setExecutorService(ExecutorService executorService) throws 
Exception {
+        Field executorServiceField = 
ConsulRegistryServiceImpl.class.getDeclaredField("notifierExecutor");
+        executorServiceField.setAccessible(true);
+        executorServiceField.set(service, executorService);
+    }
+
+    private <K, V> ConcurrentMap<K, V> getMap(String name) throws Exception {
+        Field notifiersField = 
ConsulRegistryServiceImpl.class.getDeclaredField(name);
+        notifiersField.setAccessible(true);
+        return (ConcurrentMap<K, V>) notifiersField.get(service);
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to