|
16 | 16 | */ |
17 | 17 | package org.apache.seata.discovery.registry.consul; |
18 | 18 |
|
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; |
20 | 28 | import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.MockedStatic; |
| 30 | +import org.mockito.Mockito; |
21 | 31 |
|
| 32 | +import java.lang.reflect.Field; |
22 | 33 | 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; |
23 | 38 |
|
| 39 | +import static org.mockito.Mockito.any; |
24 | 40 | import static org.mockito.Mockito.mock; |
25 | 41 | import static org.mockito.Mockito.verify; |
26 | | - |
| 42 | +import static org.mockito.Mockito.when; |
27 | 43 |
|
28 | 44 |
|
29 | 45 | public class ConsulRegistryServiceImplTest { |
30 | 46 |
|
| 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 | + } |
31 | 61 |
|
32 | 62 | @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); |
38 | 65 | } |
39 | 66 |
|
40 | 67 | @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()); |
46 | 78 | } |
47 | 79 |
|
48 | 80 | @Test |
49 | | - public void testSubscribe() throws Exception { |
50 | | - RegistryService registryService = mock(ConsulRegistryServiceImpl.class); |
| 81 | + public void testSubscribeAndLookup() throws Exception { |
51 | 82 | 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)); |
54 | 114 | } |
55 | 115 |
|
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); |
61 | 133 | } |
62 | 134 | } |
0 commit comments