Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void register(InetSocketAddress address) throws Exception {
NetUtil.validAddress(address);
doRegister(address);
RegistryHeartBeats.addHeartBeat(REGISTRY_TYPE, address, this::doRegister);

}

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

}

private List<InetSocketAddress> lookupByCluster(String cluster) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading