Skip to content

Commit b6fa7dc

Browse files
authored
test: add mock test for seata-discovery-consul module (apache#7227)
1 parent fe075d4 commit b6fa7dc

4 files changed

Lines changed: 95 additions & 23 deletions

File tree

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Add changes here for all PR submitted to the 2.x branch.
7676
- [[#7199](https://github.com/apache/incubator-seata/pull/7199)] add some UT cases for client processor
7777
- [[#7203](https://github.com/apache/incubator-seata/pull/7203)] Refactored tests in rm.datasource.sql.Druid and seata-sqlparser-druid module
7878
- [[#7221](https://github.com/apache/incubator-seata/pull/7221)] add UT for gRPC Encoder/Decode
79+
- [[#7227](https://github.com/apache/incubator-seata/pull/7227)] add mock test for seata-discovery-consul module
7980

8081

8182
### refactor:

changes/zh-cn/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
- [[#7199](https://github.com/apache/incubator-seata/pull/7199)] 增加 client processor 单测用例
7878
- [[#7203](https://github.com/apache/incubator-seata/pull/7203)] 重构了 rm.datasource.sql.Druid 和 seata-sqlparser-druid 模块中的测试
7979
- [[#7221](https://github.com/apache/incubator-seata/pull/7221)] 增加 gRPC Encoder/Decoder的测试用例
80+
- [[#7227](https://github.com/apache/incubator-seata/pull/7227)] 为 seata-discovery-consul 增加mock测试
8081

8182

8283
### refactor:

discovery/seata-discovery-consul/src/main/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public void register(InetSocketAddress address) throws Exception {
126126
NetUtil.validAddress(address);
127127
doRegister(address);
128128
RegistryHeartBeats.addHeartBeat(REGISTRY_TYPE, address, this::doRegister);
129-
130129
}
131130

132131
private void doRegister(InetSocketAddress address) {
@@ -170,7 +169,6 @@ public List<InetSocketAddress> lookup(String key) throws Exception {
170169
throw new ConfigNotFoundException("%s configuration item is required", missingDataId);
171170
}
172171
return lookupByCluster(cluster);
173-
174172
}
175173

176174
private List<InetSocketAddress> lookupByCluster(String cluster) throws Exception {

discovery/seata-discovery-consul/src/test/java/org/apache/seata/discovery/registry/consul/ConsulRegistryServiceImplTest.java

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,119 @@
1616
*/
1717
package org.apache.seata.discovery.registry.consul;
1818

19-
import org.apache.seata.discovery.registry.RegistryService;
19+
import com.ecwid.consul.transport.RawResponse;
20+
import com.ecwid.consul.v1.ConsulClient;
21+
import com.ecwid.consul.v1.Response;
22+
import com.ecwid.consul.v1.health.model.HealthService;
23+
import org.apache.seata.config.Configuration;
24+
import org.apache.seata.config.ConfigurationFactory;
25+
import org.apache.seata.config.exception.ConfigNotFoundException;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.BeforeEach;
2028
import org.junit.jupiter.api.Test;
29+
import org.mockito.MockedStatic;
30+
import org.mockito.Mockito;
2131

32+
import java.lang.reflect.Field;
2233
import java.net.InetSocketAddress;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
import java.util.concurrent.ConcurrentMap;
37+
import java.util.concurrent.ExecutorService;
2338

39+
import static org.mockito.Mockito.any;
2440
import static org.mockito.Mockito.mock;
2541
import static org.mockito.Mockito.verify;
26-
42+
import static org.mockito.Mockito.when;
2743

2844

2945
public class ConsulRegistryServiceImplTest {
3046

47+
final String TEST_CLUSTER_NAME = "testCluster";
48+
49+
ConsulClient client;
50+
Configuration configuration;
51+
ConsulRegistryServiceImpl service;
52+
53+
@BeforeEach
54+
public void init() throws Exception {
55+
service = (ConsulRegistryServiceImpl) new ConsulRegistryProvider().provide();
56+
client = mock(ConsulClient.class);
57+
this.setClient(service, client);
58+
59+
configuration = mock(Configuration.class);
60+
}
3161

3262
@Test
33-
public void testRegister() throws Exception {
34-
RegistryService registryService = mock(ConsulRegistryServiceImpl.class);
35-
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8091);
36-
registryService.register(inetSocketAddress);
37-
verify(registryService).register(inetSocketAddress);
63+
public void testGetInstance() {
64+
Assertions.assertEquals(ConsulRegistryServiceImpl.getInstance(), service);
3865
}
3966

4067
@Test
41-
public void testUnregister() throws Exception {
42-
RegistryService registryService = mock(ConsulRegistryServiceImpl.class);
43-
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8091);
44-
registryService.unregister(inetSocketAddress);
45-
verify(registryService).unregister(inetSocketAddress);
68+
public void testRegister() throws Exception {
69+
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
70+
71+
when(client.agentServiceRegister(any())).thenReturn(null);
72+
service.register(inetSocketAddress);
73+
verify(client).agentServiceRegister(any(), any());
74+
75+
when(client.agentServiceDeregister(any())).thenReturn(null);
76+
service.unregister(inetSocketAddress);
77+
verify(client).agentServiceDeregister(any(), any());
4678
}
4779

4880
@Test
49-
public void testSubscribe() throws Exception {
50-
RegistryService registryService = mock(ConsulRegistryServiceImpl.class);
81+
public void testSubscribeAndLookup() throws Exception {
5182
ConsulListener consulListener = mock(ConsulListener.class);
52-
registryService.subscribe("test", consulListener);
53-
verify(registryService).subscribe("test", consulListener);
83+
84+
ExecutorService executorService = mock(ExecutorService.class);
85+
setExecutorService(executorService);
86+
87+
Response<List<HealthService>> response = new Response<>(new ArrayList<>(), mock(RawResponse.class));
88+
when(client.getHealthServices(any(), any())).thenReturn(response);
89+
90+
service.subscribe(TEST_CLUSTER_NAME, consulListener);
91+
92+
Assertions.assertNotNull(getMap("listenerMap").get(TEST_CLUSTER_NAME));
93+
Assertions.assertNotNull(getMap("notifiers").get(TEST_CLUSTER_NAME));
94+
verify(executorService).submit(any(Runnable.class));
95+
96+
try (MockedStatic<ConfigurationFactory> configurationFactoryMockedStatic = Mockito.mockStatic(ConfigurationFactory.class)) {
97+
configurationFactoryMockedStatic.when(ConfigurationFactory::getInstance).thenReturn(configuration);
98+
99+
// normal condition
100+
when(configuration.getConfig(any())).thenReturn(TEST_CLUSTER_NAME);
101+
List<InetSocketAddress> addresses = new ArrayList<>();
102+
getMap("clusterAddressMap").put(TEST_CLUSTER_NAME, addresses);
103+
Assertions.assertEquals(addresses, service.lookup("testGroup"));
104+
105+
// when config exc
106+
when(configuration.getConfig(any())).thenReturn(null);
107+
Assertions.assertThrows(ConfigNotFoundException.class, () -> {
108+
service.lookup("testGroup");
109+
});
110+
}
111+
112+
service.unsubscribe(TEST_CLUSTER_NAME, consulListener);
113+
Assertions.assertNull(getMap("notifiers").get(TEST_CLUSTER_NAME));
54114
}
55115

56-
@Test
57-
public void testLookup() throws Exception {
58-
RegistryService registryService = mock(ConsulRegistryServiceImpl.class);
59-
registryService.lookup("test-key");
60-
verify(registryService).lookup("test-key");
116+
117+
private void setClient(ConsulRegistryServiceImpl service, ConsulClient client) throws Exception {
118+
Field clientField = ConsulRegistryServiceImpl.class.getDeclaredField("client");
119+
clientField.setAccessible(true);
120+
clientField.set(service, client);
121+
}
122+
123+
private void setExecutorService(ExecutorService executorService) throws Exception {
124+
Field executorServiceField = ConsulRegistryServiceImpl.class.getDeclaredField("notifierExecutor");
125+
executorServiceField.setAccessible(true);
126+
executorServiceField.set(service, executorService);
127+
}
128+
129+
private <K, V> ConcurrentMap<K, V> getMap(String name) throws Exception {
130+
Field notifiersField = ConsulRegistryServiceImpl.class.getDeclaredField(name);
131+
notifiersField.setAccessible(true);
132+
return (ConcurrentMap<K, V>) notifiersField.get(service);
61133
}
62134
}

0 commit comments

Comments
 (0)