From ee96df69c3d3fa0cfdd259fcf8973ab3bbc1ce5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= Date: Fri, 6 Dec 2024 16:26:31 +0800 Subject: [PATCH] [ISSUE#12644] Return cached instances when getAllInstance with subscribe exception (#12929) * when get cached all instance with `subscribe` exception, will return cached instance not throw exception. * For checkstyle --- .../client/naming/NacosNamingService.java | 74 ++- .../naming/selector/ServiceInfoContext.java | 57 ++ .../client/naming/NacosNamingServiceTest.java | 625 +++++++++++------- .../selector/ServiceInfoContextTest.java | 37 ++ 4 files changed, 531 insertions(+), 262 deletions(-) create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContext.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContextTest.java diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 7bab2fa7f60..bf382f02e27 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -24,6 +24,8 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.api.naming.selector.NamingContext; +import com.alibaba.nacos.api.naming.selector.NamingResult; import com.alibaba.nacos.api.naming.selector.NamingSelector; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; @@ -37,6 +39,7 @@ import com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate; import com.alibaba.nacos.client.naming.selector.NamingSelectorFactory; import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper; +import com.alibaba.nacos.client.naming.selector.ServiceInfoContext; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; @@ -324,38 +327,67 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) return list; } - private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, String clusterString) { - return serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); + private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) + throws NacosException { + ServiceInfo serviceInfo; + NamingSelector clusterSelector = NamingSelectorFactory.newClusterSelector(clusters); + if (serviceInfoHolder.isFailoverSwitch()) { + serviceInfo = getServiceInfoByFailover(serviceName, groupName, clusterSelector); + if (serviceInfo != null && !serviceInfo.getHosts().isEmpty()) { + NAMING_LOGGER.debug("getServiceInfo from failover,serviceName: {} data:{}", serviceName, + JacksonUtils.toJson(serviceInfo.getHosts())); + return serviceInfo; + } + } + serviceInfo = getServiceInfoBySubscribe(serviceName, groupName, clusters, clusterSelector, subscribe); + return serviceInfo; } - private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, String clusterString, - boolean subscribe) throws NacosException { + private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, NamingSelector clusterSelector) { + ServiceInfo result = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, StringUtils.EMPTY); + return doSelectInstance(result, clusterSelector); + } + + private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, List clusters, + NamingSelector selector, boolean subscribe) throws NacosException { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, StringUtils.EMPTY); + serviceInfo = tryToSubscribe(serviceName, groupName, serviceInfo); + serviceInfo = doSelectInstance(serviceInfo, selector); } else { + String clusterString = NamingSelectorFactory.getUniqueClusterString(clusters); serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } return serviceInfo; } - private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) - throws NacosException { - ServiceInfo serviceInfo; - String clusterString = StringUtils.join(clusters, ","); - if (serviceInfoHolder.isFailoverSwitch()) { - serviceInfo = getServiceInfoByFailover(serviceName, groupName, clusterString); - if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { - NAMING_LOGGER.debug("getServiceInfo from failover,serviceName: {} data:{}", serviceName, - JacksonUtils.toJson(serviceInfo.getHosts())); - return serviceInfo; - } + private ServiceInfo tryToSubscribe(String serviceName, String groupName, ServiceInfo cachedServiceInfo) throws NacosException { + // not found in cache, service never subscribed. + if (null == cachedServiceInfo) { + return clientProxy.subscribe(serviceName, groupName, StringUtils.EMPTY); } - - serviceInfo = getServiceInfoBySubscribe(serviceName, groupName, clusterString, subscribe); + // found in cache, and subscribed. + if (clientProxy.isSubscribed(serviceName, groupName, StringUtils.EMPTY)) { + return cachedServiceInfo; + } + // found in cached, but not subscribed, such as cached from local file when starting. + ServiceInfo result = cachedServiceInfo; + try { + result = clientProxy.subscribe(serviceName, groupName, StringUtils.EMPTY); + } catch (NacosException e) { + NAMING_LOGGER.warn("Subscribe from Server failed, will use local cache. fail message: ", e); + } + return result; + } + + private ServiceInfo doSelectInstance(ServiceInfo serviceInfo, NamingSelector clusterSelector) { + if (null == serviceInfo) { + return null; + } + NamingContext context = new ServiceInfoContext(serviceInfo); + NamingResult result = clusterSelector.select(context); + serviceInfo.setHosts(result.getResult()); return serviceInfo; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContext.java b/client/src/main/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContext.java new file mode 100644 index 00000000000..4cf10c89599 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContext.java @@ -0,0 +1,57 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.naming.selector; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.api.naming.selector.NamingContext; + +import java.util.List; + +/** + * Service info context. + * + * @author xiweng.yy + */ +public class ServiceInfoContext implements NamingContext { + + private final ServiceInfo serviceInfo; + + public ServiceInfoContext(ServiceInfo serviceInfo) { + this.serviceInfo = serviceInfo; + } + + @Override + public String getServiceName() { + return serviceInfo.getName(); + } + + @Override + public String getGroupName() { + return serviceInfo.getGroupName(); + } + + @Override + public String getClusters() { + return serviceInfo.getClusters(); + } + + @Override + public List getInstances() { + return serviceInfo.getHosts(); + } +} diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java index 7b1b0275bf0..b1c98ee7195 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -34,6 +34,7 @@ import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.utils.StringUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -47,9 +48,11 @@ import java.util.Collections; import java.util.List; import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; import static com.alibaba.nacos.client.naming.selector.NamingSelectorFactory.getUniqueClusterString; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -118,7 +121,7 @@ private void injectMocks(NacosNamingService client) throws NoSuchFieldException, } @Test - void testRegisterInstance1() throws NacosException { + void testRegisterInstanceSingle() throws NacosException { //given String serviceName = "service1"; String ip = "1.1.1.1"; @@ -133,87 +136,7 @@ void testRegisterInstance1() throws NacosException { } @Test - void testBatchRegisterInstance() throws NacosException { - Instance instance = new Instance(); - String serviceName = "service1"; - String ip = "1.1.1.1"; - int port = 10000; - instance.setServiceName(serviceName); - instance.setEphemeral(true); - instance.setPort(port); - instance.setIp(ip); - List instanceList = new ArrayList<>(); - instanceList.add(instance); - //when - client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); - //then - verify(proxy, times(1)).batchRegisterService(eq(serviceName), eq(Constants.DEFAULT_GROUP), - argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances))); - } - - @Test - void testBatchRegisterInstanceWithGroupNamePrefix() throws NacosException { - Instance instance = new Instance(); - String serviceName = "service1"; - String ip = "1.1.1.1"; - int port = 10000; - instance.setServiceName(Constants.DEFAULT_GROUP + "@@" + serviceName); - instance.setEphemeral(true); - instance.setPort(port); - instance.setIp(ip); - List instanceList = new ArrayList<>(); - instanceList.add(instance); - //when - client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); - //then - verify(proxy, times(1)).batchRegisterService(eq(serviceName), eq(Constants.DEFAULT_GROUP), - argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances))); - } - - @Test - void testBatchRegisterInstanceWithWrongGroupNamePrefix() throws NacosException { - Instance instance = new Instance(); - String serviceName = "service1"; - String ip = "1.1.1.1"; - int port = 10000; - instance.setServiceName("WrongGroup" + "@@" + serviceName); - instance.setEphemeral(true); - instance.setPort(port); - instance.setIp(ip); - List instanceList = new ArrayList<>(); - instanceList.add(instance); - //when - try { - client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); - } catch (Exception e) { - assertTrue(e instanceof NacosException); - assertTrue(e.getMessage().contains("wrong group name prefix of instance service name")); - } - } - - @Test - void testBatchDeRegisterInstance() throws NacosException { - Instance instance = new Instance(); - String serviceName = "service1"; - String ip = "1.1.1.1"; - int port = 10000; - instance.setServiceName(serviceName); - instance.setEphemeral(true); - instance.setPort(port); - instance.setIp(ip); - List instanceList = new ArrayList<>(); - instanceList.add(instance); - //when - try { - client.batchDeregisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); - } catch (Exception e) { - assertTrue(e instanceof NacosException); - assertTrue(e.getMessage().contains("not found")); - } - } - - @Test - void testRegisterInstance2() throws NacosException { + void testRegisterInstanceSingleWithGroup() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -229,7 +152,7 @@ void testRegisterInstance2() throws NacosException { } @Test - void testRegisterInstance3() throws NacosException { + void testRegisterInstanceSingleWithCluster() throws NacosException { //given String serviceName = "service1"; String clusterName = "cluster1"; @@ -245,7 +168,7 @@ void testRegisterInstance3() throws NacosException { } @Test - void testRegisterInstance4() throws NacosException { + void testRegisterInstanceSingleFull() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -262,7 +185,7 @@ void testRegisterInstance4() throws NacosException { } @Test - void testRegisterInstance5() throws NacosException { + void testRegisterInstanceByInstanceOnlyService() throws NacosException { //given String serviceName = "service1"; Instance instance = new Instance(); @@ -273,7 +196,7 @@ void testRegisterInstance5() throws NacosException { } @Test - void testRegisterInstance6() throws NacosException { + void testRegisterInstanceByInstanceFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -285,7 +208,7 @@ void testRegisterInstance6() throws NacosException { } @Test - void testRegisterInstance7() throws NacosException { + void testRegisterInstanceByInstanceWithCluster() throws NacosException { Throwable exception = assertThrows(NacosException.class, () -> { //given @@ -301,7 +224,87 @@ void testRegisterInstance7() throws NacosException { } @Test - void testDeregisterInstance1() throws NacosException { + void testBatchRegisterInstance() throws NacosException { + Instance instance = new Instance(); + String serviceName = "service1"; + String ip = "1.1.1.1"; + int port = 10000; + instance.setServiceName(serviceName); + instance.setEphemeral(true); + instance.setPort(port); + instance.setIp(ip); + List instanceList = new ArrayList<>(); + instanceList.add(instance); + //when + client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); + //then + verify(proxy, times(1)).batchRegisterService(eq(serviceName), eq(Constants.DEFAULT_GROUP), + argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances))); + } + + @Test + void testBatchRegisterInstanceWithGroupNamePrefix() throws NacosException { + Instance instance = new Instance(); + String serviceName = "service1"; + String ip = "1.1.1.1"; + int port = 10000; + instance.setServiceName(Constants.DEFAULT_GROUP + "@@" + serviceName); + instance.setEphemeral(true); + instance.setPort(port); + instance.setIp(ip); + List instanceList = new ArrayList<>(); + instanceList.add(instance); + //when + client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); + //then + verify(proxy, times(1)).batchRegisterService(eq(serviceName), eq(Constants.DEFAULT_GROUP), + argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances))); + } + + @Test + void testBatchRegisterInstanceWithWrongGroupNamePrefix() throws NacosException { + Instance instance = new Instance(); + String serviceName = "service1"; + String ip = "1.1.1.1"; + int port = 10000; + instance.setServiceName("WrongGroup" + "@@" + serviceName); + instance.setEphemeral(true); + instance.setPort(port); + instance.setIp(ip); + List instanceList = new ArrayList<>(); + instanceList.add(instance); + //when + try { + client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); + } catch (Exception e) { + assertTrue(e instanceof NacosException); + assertTrue(e.getMessage().contains("wrong group name prefix of instance service name")); + } + } + + @Test + void testBatchDeRegisterInstance() throws NacosException { + Instance instance = new Instance(); + String serviceName = "service1"; + String ip = "1.1.1.1"; + int port = 10000; + instance.setServiceName(serviceName); + instance.setEphemeral(true); + instance.setPort(port); + instance.setIp(ip); + List instanceList = new ArrayList<>(); + instanceList.add(instance); + //when + try { + client.batchDeregisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList); + } catch (Exception e) { + assertTrue(e instanceof NacosException); + assertTrue(e.getMessage().contains("not found")); + } + } + + @Test + void testDeregisterInstanceSingle() throws NacosException { //given String serviceName = "service1"; String ip = "1.1.1.1"; @@ -316,7 +319,7 @@ void testDeregisterInstance1() throws NacosException { } @Test - void testDeregisterInstance2() throws NacosException { + void testDeregisterInstanceSingleWithGroup() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -332,7 +335,7 @@ void testDeregisterInstance2() throws NacosException { } @Test - void testDeregisterInstance3() throws NacosException { + void testDeregisterInstanceSingleWithCluster() throws NacosException { //given String serviceName = "service1"; String clusterName = "cluster1"; @@ -348,7 +351,7 @@ void testDeregisterInstance3() throws NacosException { } @Test - void testDeregisterInstance4() throws NacosException { + void testDeregisterInstanceSingleFull() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -365,7 +368,7 @@ void testDeregisterInstance4() throws NacosException { } @Test - void testDeregisterInstance5() throws NacosException { + void testDeregisterInstanceByInstanceOnlyService() throws NacosException { //given String serviceName = "service1"; Instance instance = new Instance(); @@ -376,7 +379,7 @@ void testDeregisterInstance5() throws NacosException { } @Test - void testDeregisterInstance6() throws NacosException { + void testDeregisterInstanceByInstanceFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -388,94 +391,135 @@ void testDeregisterInstance6() throws NacosException { } @Test - void testGetAllInstances1() throws NacosException { + void testGetAllInstancesOnlyService() throws NacosException { //given String serviceName = "service1"; //when - client.getAllInstances(serviceName); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(new Instance()); + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, ""); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances2() throws NacosException { + void testGetAllInstancesFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; //when - client.getAllInstances(serviceName, groupName); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.setGroupName(groupName); + serviceInfo.addHost(new Instance()); + when(proxy.subscribe(serviceName, groupName, "")).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, groupName); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, ""); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances3() throws NacosException { + void testGetAllInstancesOnlyServiceNotSubscribe() throws NacosException { //given String serviceName = "service1"; //when - client.getAllInstances(serviceName, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(new Instance()); + when(proxy.queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", false)).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances4() throws NacosException { + void testGetAllInstancesFullNameNotSubscribe() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; //when - client.getAllInstances(serviceName, groupName, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.setGroupName(groupName); + serviceInfo.addHost(new Instance()); + when(proxy.queryInstancesOfService(serviceName, groupName, "", false)).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, groupName, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "", false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances5() throws NacosException { + void testGetAllInstancesWithServiceAndClusters() throws NacosException { //given String serviceName = "service1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.getAllInstances(serviceName, clusterList); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", false)); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, clusterList); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2"); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances6() throws NacosException { + void testGetAllInstancesWithFullNameAndClusters() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; + // when + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.setGroupName(groupName); + serviceInfo.addHost(mockInstance("cluster1", false)); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, false)); List clusterList = Arrays.asList("cluster1", "cluster2"); - //when - client.getAllInstances(serviceName, groupName, clusterList); + serviceInfo.getHosts().get(1).setClusterName(Constants.DEFAULT_CLUSTER_NAME); + when(proxy.subscribe(serviceName, groupName, "")).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, groupName, clusterList); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, "cluster1,cluster2"); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances7() throws NacosException { + void testGetAllInstancesWithServiceAndClustersNotSubscribe() throws NacosException { //given String serviceName = "service1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.getAllInstances(serviceName, clusterList, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2", + false)).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, clusterList, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2", - false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testGetAllInstances8() throws NacosException { + void testGetAllInstancesWithFullNameAndClustersNotSubscribe() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; + // when + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.setGroupName(groupName); + serviceInfo.addHost(mockInstance("cluster1", false)); List clusterList = Arrays.asList("cluster1", "cluster2"); - //when - client.getAllInstances(serviceName, groupName, clusterList, false); + when(proxy.queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", false)).thenReturn(serviceInfo); + List result = client.getAllInstances(serviceName, groupName, clusterList, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test @@ -483,7 +527,7 @@ void testGetAllInstanceFromFailover() throws NacosException { when(serviceInfoHolder.isFailoverSwitch()).thenReturn(true); ServiceInfo serviceInfo = new ServiceInfo("group1@@service1"); serviceInfo.setHosts(Collections.singletonList(new Instance())); - when(serviceInfoHolder.getFailoverServiceInfo(anyString(), anyString(), anyString())).thenReturn(serviceInfo); + when(serviceInfoHolder.getFailoverServiceInfo("service1", "group1", "")).thenReturn(serviceInfo); List actual = client.getAllInstances("service1", "group1", false); verify(proxy, never()).queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean()); assertEquals(1, actual.size()); @@ -494,101 +538,172 @@ void testGetAllInstanceFromFailover() throws NacosException { void testGetAllInstanceFromFailoverEmpty() throws NacosException { when(serviceInfoHolder.isFailoverSwitch()).thenReturn(true); ServiceInfo serviceInfo = new ServiceInfo("group1@@service1"); - when(serviceInfoHolder.getFailoverServiceInfo(anyString(), anyString(), anyString())).thenReturn(serviceInfo); + when(serviceInfoHolder.getFailoverServiceInfo("service1", "group1", "")).thenReturn(serviceInfo); List actual = client.getAllInstances("service1", "group1", false); verify(proxy).queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean()); assertEquals(0, actual.size()); } @Test - void testSelectInstances1() throws NacosException { + void testGetAllInstanceWithCacheAndSubscribeException() throws NacosException { + String serviceName = "service1"; + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(new Instance()); + when(serviceInfoHolder.getServiceInfo(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenThrow(new NacosException(500, "test")); + List result = client.getAllInstances(serviceName); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); + } + + @Test + void testGetAllInstanceWithoutCacheAndSubscribeException() throws NacosException { + String serviceName = "service1"; + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenThrow(new NacosException(500, "test")); + assertThrows(NacosException.class, () -> client.getAllInstances(serviceName)); + } + + @Test + void testGetAllInstanceWithCacheAndSubscribed() throws NacosException { + String serviceName = "service1"; + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(new Instance()); + when(serviceInfoHolder.getServiceInfo(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + when(proxy.isSubscribed(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(true); + List result = client.getAllInstances(serviceName); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); + } + + @Test + void testSelectInstancesOnlyService() throws NacosException { //given String serviceName = "service1"; //when - client.selectInstances(serviceName, true); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, true); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, ""); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances2() throws NacosException { + void testSelectInstancesFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; //when - client.selectInstances(serviceName, groupName, true); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); + when(proxy.subscribe(serviceName, groupName, "")).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, groupName, true); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, ""); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances3() throws NacosException { + void testSelectInstancesOnlyServiceNotSubscribe() throws NacosException { //given String serviceName = "service1"; //when - client.selectInstances(serviceName, true, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); + when(proxy.queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", false)).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, true, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances4() throws NacosException { + void testSelectInstancesFullNameNotSubscribe() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; //when - client.selectInstances(serviceName, groupName, true, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); + when(proxy.queryInstancesOfService(serviceName, groupName, "", false)).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, groupName, true, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "", false); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances5() throws NacosException { + void testSelectInstancesWithServiceAndClusters() throws NacosException { //given String serviceName = "service1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectInstances(serviceName, clusterList, true); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", true)); + serviceInfo.addHost(mockInstance("cluster1", false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, clusterList, true); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2"); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances6() throws NacosException { + void testSelectInstancesWithFullNameAndClusters() throws NacosException { //given String serviceName = "service1"; - String groupName = "group1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); + final String groupName = "group1"; //when - client.selectInstances(serviceName, groupName, clusterList, true); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", true)); + serviceInfo.addHost(mockInstance("cluster1", false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.subscribe(serviceName, groupName, "")).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, groupName, clusterList, true); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, "cluster1,cluster2"); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances7() throws NacosException { + void testSelectInstancesWithServiceAndClustersNotSubscribe() throws NacosException { //given String serviceName = "service1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectInstances(serviceName, clusterList, true, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", true)); + serviceInfo.addHost(mockInstance("cluster1", false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2", + false)).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, clusterList, true, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2", - false); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test - void testSelectInstances8() throws NacosException { + void testSelectInstancesWithFullNameAndClustersNotSubscribe() throws NacosException { //given String serviceName = "service1"; - String groupName = "group1"; - List clusterList = Arrays.asList("cluster1", "cluster2"); + final String groupName = "group1"; //when - client.selectInstances(serviceName, groupName, clusterList, true, false); + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setName(serviceName); + serviceInfo.addHost(mockInstance("cluster1", true)); + serviceInfo.addHost(mockInstance("cluster1", false)); + List clusterList = Arrays.asList("cluster1", "cluster2"); + when(proxy.queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", false)).thenReturn(serviceInfo); + List result = client.selectInstances(serviceName, groupName, clusterList, true, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", false); + assertEquals(1, result.size()); + assertEquals(serviceInfo.getHosts().get(0), result.get(0)); } @Test @@ -628,160 +743,123 @@ void testSelectInstancesWithHealthyFlag() throws NacosException { } @Test - void testSelectOneHealthyInstance1() throws NacosException { + void testSelectOneHealthyInstanceOnlyService() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance); String serviceName = "service1"; //when - client.selectOneHealthyInstance(serviceName); + Instance instance = client.selectOneHealthyInstance(serviceName); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, ""); + assertNotNull(instance); } @Test - void testSelectOneHealthyInstance2() throws NacosException { + void testSelectOneHealthyInstanceFullName() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance); String serviceName = "service1"; String groupName = "group1"; //when - client.selectOneHealthyInstance(serviceName, groupName); + Instance instance = client.selectOneHealthyInstance(serviceName, groupName); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, ""); + assertNotNull(instance); } @Test - void testSelectOneHealthyInstance3() throws NacosException { + void testSelectOneHealthyInstanceOnlyServiceNotSubscribe() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); when(proxy.queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean())).thenReturn( infoWithHealthyInstance); String serviceName = "service1"; //when - client.selectOneHealthyInstance(serviceName, false); + Instance instance = client.selectOneHealthyInstance(serviceName, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", false); + assertNotNull(instance); } @Test - void testSelectOneHealthyInstance4() throws NacosException { + void testSelectOneHealthyInstanceFullNameNotSubscribe() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); when(proxy.queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean())).thenReturn( infoWithHealthyInstance); String serviceName = "service1"; String groupName = "group1"; //when - client.selectOneHealthyInstance(serviceName, groupName, false); + Instance instance = client.selectOneHealthyInstance(serviceName, groupName, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "", false); + assertNotNull(instance); } @Test - void testSelectOneHealthyInstance5() throws NacosException { + void testSelectOneHealthyInstanceWithServiceAndClusters() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); - when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance); - + infoWithHealthyInstance.addHost(mockInstance("cluster1", true)); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); String serviceName = "service1"; + when(proxy.subscribe(serviceName, Constants.DEFAULT_GROUP, StringUtils.EMPTY)).thenReturn( + infoWithHealthyInstance); List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectOneHealthyInstance(serviceName, clusterList); + Instance instance = client.selectOneHealthyInstance(serviceName, clusterList); //then - verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2"); + assertNotNull(instance); + assertEquals("cluster1", instance.getClusterName()); } @Test - void testSelectOneHealthyInstance6() throws NacosException { + void testSelectOneHealthyInstanceWithFullNameAndClusters() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance("cluster1", true)); + infoWithHealthyInstance.addHost(mockInstance(Constants.DEFAULT_CLUSTER_NAME, true)); when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance); String serviceName = "service1"; String groupName = "group1"; List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectOneHealthyInstance(serviceName, groupName, clusterList); + Instance instance = client.selectOneHealthyInstance(serviceName, groupName, clusterList); //then - verify(proxy, times(1)).subscribe(serviceName, groupName, "cluster1,cluster2"); - + assertNotNull(instance); + assertEquals("cluster1", instance.getClusterName()); } @Test - void testSelectOneHealthyInstance7() throws NacosException { + void testSelectOneHealthyInstanceWithServiceAndClustersNotSubscribe() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance("cluster1", true)); when(proxy.queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean())).thenReturn( infoWithHealthyInstance); String serviceName = "service1"; List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectOneHealthyInstance(serviceName, clusterList, false); + Instance instance = client.selectOneHealthyInstance(serviceName, clusterList, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2", - false); + assertNotNull(instance); + assertEquals("cluster1", instance.getClusterName()); } @Test - void testSelectOneHealthyInstance8() throws NacosException { + void testSelectOneHealthyInstanceWithFullNameAndClustersNotSubscribe() throws NacosException { //given - Instance healthyInstance = new Instance(); - healthyInstance.setIp("1.1.1.1"); - healthyInstance.setPort(1000); - List hosts = new ArrayList<>(); - hosts.add(healthyInstance); ServiceInfo infoWithHealthyInstance = new ServiceInfo(); - infoWithHealthyInstance.setHosts(hosts); + infoWithHealthyInstance.addHost(mockInstance("cluster1", true)); when(proxy.queryInstancesOfService(anyString(), anyString(), anyString(), anyBoolean())).thenReturn( infoWithHealthyInstance); @@ -789,13 +867,14 @@ void testSelectOneHealthyInstance8() throws NacosException { String groupName = "group1"; List clusterList = Arrays.asList("cluster1", "cluster2"); //when - client.selectOneHealthyInstance(serviceName, groupName, clusterList, false); + Instance instance = client.selectOneHealthyInstance(serviceName, groupName, clusterList, false); //then - verify(proxy, times(1)).queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", false); + assertNotNull(instance); + assertEquals("cluster1", instance.getClusterName()); } @Test - void testSubscribe1() throws NacosException { + void testSubscribeOnlyService() throws NacosException { //given String serviceName = "service1"; EventListener listener = event -> { @@ -811,7 +890,7 @@ void testSubscribe1() throws NacosException { } @Test - void testSubscribe2() throws NacosException { + void testSubscribeFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -828,7 +907,7 @@ void testSubscribe2() throws NacosException { } @Test - void testSubscribe3() throws NacosException { + void testSubscribeWithServiceAndClusters() throws NacosException { //given String serviceName = "service1"; List clusterList = Arrays.asList("cluster1", "cluster2"); @@ -845,7 +924,7 @@ void testSubscribe3() throws NacosException { } @Test - void testSubscribe4() throws NacosException { + void testSubscribeWithFullNameAndClusters() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -863,7 +942,22 @@ void testSubscribe4() throws NacosException { } @Test - public void testSubscribe5() throws NacosException { + public void testSubscribeWithServiceAndCustomSelector() throws NacosException { + String serviceName = "service1"; + EventListener listener = event -> { + + }; + //when + client.subscribe(serviceName, NamingSelectorFactory.HEALTHY_SELECTOR, listener); + NamingSelectorWrapper wrapper = new NamingSelectorWrapper(serviceName, Constants.DEFAULT_GROUP, Constants.NULL, + NamingSelectorFactory.HEALTHY_SELECTOR, listener); + //then + verify(changeNotifier, times(1)).registerListener(Constants.DEFAULT_GROUP, serviceName, wrapper); + verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, Constants.NULL); + } + + @Test + public void testSubscribeWithFullNameAndCustomSelector() throws NacosException { String serviceName = "service1"; String groupName = "group1"; EventListener listener = event -> { @@ -892,7 +986,19 @@ void testSubscribeWithNullListener() throws NacosException { } @Test - void testUnSubscribe1() throws NacosException { + void testSubscribeDuplicate() throws NacosException { + String serviceName = "service1"; + when(changeNotifier.isSubscribed(Constants.DEFAULT_GROUP, serviceName)).thenReturn(true); + ServiceInfo serviceInfo = new ServiceInfo(Constants.DEFAULT_GROUP + "@@" + serviceName); + serviceInfo.addHost(new Instance()); + when(serviceInfoHolder.getServiceInfo(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(serviceInfo); + final AtomicBoolean flag = new AtomicBoolean(false); + client.subscribe(serviceName, event -> flag.set(true)); + assertTrue(flag.get()); + } + + @Test + void testUnSubscribeOnlyService() throws NacosException { //given String serviceName = "service1"; EventListener listener = event -> { @@ -909,7 +1015,7 @@ void testUnSubscribe1() throws NacosException { } @Test - void testUnSubscribe2() throws NacosException { + void testUnSubscribeFullName() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -928,7 +1034,7 @@ void testUnSubscribe2() throws NacosException { } @Test - void testUnSubscribe3() throws NacosException { + void testUnSubscribeWithServiceAndClusters() throws NacosException { //given String serviceName = "service1"; List clusterList = Arrays.asList("cluster1", "cluster2"); @@ -947,7 +1053,7 @@ void testUnSubscribe3() throws NacosException { } @Test - void testUnSubscribe4() throws NacosException { + void testUnSubscribeWithFullNameAndClusters() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -967,7 +1073,24 @@ void testUnSubscribe4() throws NacosException { } @Test - public void testUnSubscribe5() throws NacosException { + public void testUnSubscribeWithServiceAndCustomSelector() throws NacosException { + //given + String serviceName = "service1"; + EventListener listener = event -> { + + }; + when(changeNotifier.isSubscribed(Constants.DEFAULT_GROUP, serviceName)).thenReturn(false); + + //when + client.unsubscribe(serviceName, NamingSelectorFactory.HEALTHY_SELECTOR, listener); + NamingSelectorWrapper wrapper = new NamingSelectorWrapper(NamingSelectorFactory.HEALTHY_SELECTOR, listener); + //then + verify(changeNotifier, times(1)).deregisterListener(Constants.DEFAULT_GROUP, serviceName, wrapper); + verify(proxy, times(1)).unsubscribe(serviceName, Constants.DEFAULT_GROUP, Constants.NULL); + } + + @Test + public void testUnSubscribeWithFullNameAndCustomSelector() throws NacosException { //given String serviceName = "service1"; String groupName = "group1"; @@ -984,6 +1107,19 @@ public void testUnSubscribe5() throws NacosException { verify(proxy, times(1)).unsubscribe(serviceName, groupName, Constants.NULL); } + @Test + void testUnSubscribeWithNullListener() throws NacosException { + String serviceName = "service1"; + String groupName = "group1"; + //when + client.unsubscribe(serviceName, groupName, null); + //then + verify(changeNotifier, never()).deregisterListener(groupName, serviceName, + new NamingSelectorWrapper(NamingSelectorFactory.newIpSelector(""), null)); + verify(proxy, never()).unsubscribe(serviceName, groupName, ""); + + } + @Test void testGetServicesOfServer1() throws NacosException { //given @@ -1091,4 +1227,11 @@ void testConstructorWithServerList() throws NacosException, NoSuchFieldException namingService.shutDown(); } } + + private Instance mockInstance(String clusterName, boolean healthy) { + Instance instance = new Instance(); + instance.setClusterName(clusterName); + instance.setHealthy(healthy); + return instance; + } } \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContextTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContextTest.java new file mode 100644 index 00000000000..a9e41e3b52c --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/ServiceInfoContextTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.naming.selector; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ServiceInfoContextTest { + + @Test + void testGetAll() { + ServiceInfo serviceInfo = ServiceInfo.fromKey("aaa@@bbb@@ccc,ddd"); + serviceInfo.addHost(new Instance()); + ServiceInfoContext context = new ServiceInfoContext(serviceInfo); + assertEquals(1, context.getInstances().size()); + assertEquals("aaa", context.getGroupName()); + assertEquals("bbb", context.getServiceName()); + assertEquals("ccc,ddd", context.getClusters()); + } +} \ No newline at end of file