diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorServiceImpl.java index e3dfafdd5..1f197f386 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorServiceImpl.java @@ -26,10 +26,10 @@ import com.alibaba.nacos.naming.healthcheck.RsInfo; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.pojo.Subscriber; -import com.alibaba.nacos.naming.push.ClientInfo; -import com.alibaba.nacos.naming.push.DataSource; -import com.alibaba.nacos.naming.push.NamingSubscriberServiceV1Impl; -import com.alibaba.nacos.naming.push.PushClient; +import com.alibaba.nacos.naming.push.v1.ClientInfo; +import com.alibaba.nacos.naming.push.v1.DataSource; +import com.alibaba.nacos.naming.push.v1.NamingSubscriberServiceV1Impl; +import com.alibaba.nacos.naming.push.v1.PushClient; import com.alibaba.nacos.naming.push.UdpPushService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index afc1ae9d4..9f47ae0a4 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -16,26 +16,15 @@ package com.alibaba.nacos.naming.core; -import com.alibaba.nacos.api.naming.CommonParams; -import com.alibaba.nacos.common.model.RestResult; -import com.alibaba.nacos.common.utils.JacksonUtils; -import com.alibaba.nacos.core.cluster.Member; -import com.alibaba.nacos.core.cluster.ServerMemberManager; -import com.alibaba.nacos.naming.push.NamingSubscriberServiceV1Impl; -import com.alibaba.nacos.sys.env.EnvUtil; -import com.alibaba.nacos.naming.misc.HttpClient; -import com.alibaba.nacos.naming.misc.NetUtils; -import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.pojo.Subscriber; -import com.alibaba.nacos.naming.pojo.Subscribers; -import com.alibaba.nacos.naming.push.v2.NamingSubscriberServiceV2Impl; +import com.alibaba.nacos.naming.push.NamingSubscriberServiceAggregationImpl; +import com.alibaba.nacos.naming.push.NamingSubscriberServiceLocalImpl; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -48,35 +37,17 @@ import java.util.stream.Collectors; * Subscribe manager. * * @author Nicholas + * @author xiweng.yy * @since 1.0.1 */ @Service public class SubscribeManager { - private static final String SUBSCRIBER_ON_SYNC_URL = "/service/subscribers"; + @Autowired + private NamingSubscriberServiceLocalImpl localService; @Autowired - private NamingSubscriberServiceV1Impl subscriberServiceV1; - - @Autowired - private NamingSubscriberServiceV2Impl subscriberServiceV2; - - @Autowired - private ServerMemberManager memberManager; - - private List getSubscribersFuzzy(String serviceName, String namespaceId) { - List result = new LinkedList<>(); - result.addAll(subscriberServiceV1.getFuzzySubscribers(namespaceId, serviceName)); - result.addAll(subscriberServiceV2.getFuzzySubscribers(namespaceId, serviceName)); - return result; - } - - private List getSubscribers(String serviceName, String namespaceId) { - List result = new LinkedList<>(); - result.addAll(subscriberServiceV1.getSubscribers(namespaceId, serviceName)); - result.addAll(subscriberServiceV2.getSubscribers(namespaceId, serviceName)); - return result; - } + private NamingSubscriberServiceAggregationImpl aggregationService; /** * Get subscribers. @@ -85,44 +56,14 @@ public class SubscribeManager { * @param namespaceId namespace id * @param aggregation aggregation * @return list of subscriber - * @throws InterruptedException interrupted exception */ - public List getSubscribers(String serviceName, String namespaceId, boolean aggregation) - throws InterruptedException { + public List getSubscribers(String serviceName, String namespaceId, boolean aggregation) { if (aggregation) { - // size = 1 means only myself in the list, we need at least one another server alive: - if (memberManager.getServerList().size() <= 1) { - return getSubscribersFuzzy(serviceName, namespaceId); - } - - List subscriberList = new ArrayList(); - // try sync data from remote server: - for (Member server : memberManager.allMembers()) { - - Map paramValues = new HashMap<>(128); - paramValues.put(CommonParams.SERVICE_NAME, serviceName); - paramValues.put(CommonParams.NAMESPACE_ID, namespaceId); - paramValues.put("aggregation", String.valueOf(Boolean.FALSE)); - if (NetUtils.localServer().equals(server.getAddress())) { - subscriberList.addAll(getSubscribersFuzzy(serviceName, namespaceId)); - continue; - } - - RestResult result = HttpClient.httpGet( - "http://" + server.getAddress() + EnvUtil.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), - paramValues); - - if (result.ok()) { - Subscribers subscribers = JacksonUtils.toObj(result.getData(), Subscribers.class); - subscriberList.addAll(subscribers.getSubscribers()); - } - } - return CollectionUtils.isNotEmpty(subscriberList) ? subscriberList.stream() - .filter(distinctByKey(Subscriber::toString)).collect(Collectors.toList()) : Collections.EMPTY_LIST; + Collection result = aggregationService.getFuzzySubscribers(namespaceId, serviceName); + return CollectionUtils.isNotEmpty(result) ? result.stream().filter(distinctByKey(Subscriber::toString)) + .collect(Collectors.toList()) : Collections.EMPTY_LIST; } else { - // local server - return getSubscribersFuzzy(serviceName, namespaceId); + return new LinkedList<>(localService.getFuzzySubscribers(namespaceId, serviceName)); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImpl.java index 0cf5b373f..1fec0a970 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImpl.java @@ -16,11 +16,23 @@ package com.alibaba.nacos.naming.push; +import com.alibaba.nacos.api.naming.CommonParams; +import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.core.cluster.Member; +import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.misc.HttpClient; +import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.pojo.Subscriber; -import com.alibaba.nacos.naming.push.v2.NamingSubscriberServiceV2Impl; +import com.alibaba.nacos.naming.pojo.Subscribers; +import com.alibaba.nacos.sys.env.EnvUtil; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; /** * Aggregation naming subscriber service. Aggregate all implementation of {@link NamingSubscriberService} and @@ -31,33 +43,70 @@ import java.util.Collection; @org.springframework.stereotype.Service public class NamingSubscriberServiceAggregationImpl implements NamingSubscriberService { - private final NamingSubscriberServiceV1Impl subscriberServiceV1; + private static final String SUBSCRIBER_ON_SYNC_URL = "/service/subscribers"; - private final NamingSubscriberServiceV2Impl subscriberServiceV2; + private final NamingSubscriberServiceLocalImpl subscriberServiceLocal; - public NamingSubscriberServiceAggregationImpl(NamingSubscriberServiceV1Impl subscriberServiceV1, - NamingSubscriberServiceV2Impl subscriberServiceV2) { - this.subscriberServiceV1 = subscriberServiceV1; - this.subscriberServiceV2 = subscriberServiceV2; + private final ServerMemberManager memberManager; + + public NamingSubscriberServiceAggregationImpl(NamingSubscriberServiceLocalImpl subscriberServiceLocal, + ServerMemberManager serverMemberManager) { + this.subscriberServiceLocal = subscriberServiceLocal; + this.memberManager = serverMemberManager; } @Override public Collection getSubscribers(String namespaceId, String serviceName) { - return null; + Collection result = new LinkedList<>( + subscriberServiceLocal.getSubscribers(namespaceId, serviceName)); + if (memberManager.getServerList().size() > 1) { + getSubscribersFromRemotes(namespaceId, serviceName, result); + } + return result; } @Override public Collection getSubscribers(Service service) { - return null; + Collection result = new LinkedList<>(subscriberServiceLocal.getSubscribers(service)); + if (memberManager.getServerList().size() > 1) { + getSubscribersFromRemotes(service.getNamespace(), service.getGroupedServiceName(), result); + } + return result; } @Override public Collection getFuzzySubscribers(String namespaceId, String serviceName) { - return null; + Collection result = new LinkedList<>( + subscriberServiceLocal.getFuzzySubscribers(namespaceId, serviceName)); + if (memberManager.getServerList().size() > 1) { + getSubscribersFromRemotes(namespaceId, serviceName, result); + } + return result; } @Override public Collection getFuzzySubscribers(Service service) { - return null; + Collection result = new LinkedList<>(subscriberServiceLocal.getFuzzySubscribers(service)); + if (memberManager.getServerList().size() > 1) { + getSubscribersFromRemotes(service.getNamespace(), service.getGroupedServiceName(), result); + } + return result; + } + + private void getSubscribersFromRemotes(String namespaceId, String serviceName, Collection result) { + for (Member server : memberManager.allMembersWithoutSelf()) { + Map paramValues = new HashMap<>(128); + paramValues.put(CommonParams.SERVICE_NAME, serviceName); + paramValues.put(CommonParams.NAMESPACE_ID, namespaceId); + paramValues.put("aggregation", String.valueOf(Boolean.FALSE)); + // TODO replace with gRPC + RestResult response = HttpClient.httpGet( + "http://" + server.getAddress() + EnvUtil.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues); + if (response.ok()) { + Subscribers subscribers = JacksonUtils.toObj(response.getData(), Subscribers.class); + result.addAll(subscribers.getSubscribers()); + } + } } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceLocalImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceLocalImpl.java new file mode 100644 index 000000000..f748460c8 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceLocalImpl.java @@ -0,0 +1,78 @@ +/* + * Copyright 1999-2020 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.naming.push; + +import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v1.NamingSubscriberServiceV1Impl; +import com.alibaba.nacos.naming.push.v2.NamingSubscriberServiceV2Impl; + +import java.util.Collection; +import java.util.HashSet; + +/** + * Naming subscriber service for local. + * + * @author xiweng.yy + * @deprecated Will be removed with {@link com.alibaba.nacos.naming.push.v1.NamingSubscriberServiceV1Impl} + */ +@org.springframework.stereotype.Service +@Deprecated +public class NamingSubscriberServiceLocalImpl implements NamingSubscriberService { + + private final NamingSubscriberServiceV1Impl namingSubscriberServiceV1; + + private final NamingSubscriberServiceV2Impl namingSubscriberServiceV2; + + public NamingSubscriberServiceLocalImpl(NamingSubscriberServiceV1Impl namingSubscriberServiceV1, + NamingSubscriberServiceV2Impl namingSubscriberServiceV2) { + this.namingSubscriberServiceV1 = namingSubscriberServiceV1; + this.namingSubscriberServiceV2 = namingSubscriberServiceV2; + } + + @Override + public Collection getSubscribers(String namespaceId, String serviceName) { + Collection result = new HashSet<>(); + result.addAll(namingSubscriberServiceV1.getSubscribers(namespaceId, serviceName)); + result.addAll(namingSubscriberServiceV2.getSubscribers(namespaceId, serviceName)); + return result; + } + + @Override + public Collection getSubscribers(Service service) { + Collection result = new HashSet<>(); + result.addAll(namingSubscriberServiceV1.getSubscribers(service)); + result.addAll(namingSubscriberServiceV2.getSubscribers(service)); + return result; + } + + @Override + public Collection getFuzzySubscribers(String namespaceId, String serviceName) { + Collection result = new HashSet<>(); + result.addAll(namingSubscriberServiceV1.getFuzzySubscribers(namespaceId, serviceName)); + result.addAll(namingSubscriberServiceV2.getFuzzySubscribers(namespaceId, serviceName)); + return result; + } + + @Override + public Collection getFuzzySubscribers(Service service) { + Collection result = new HashSet<>(); + result.addAll(namingSubscriberServiceV1.getFuzzySubscribers(service)); + result.addAll(namingSubscriberServiceV2.getFuzzySubscribers(service)); + return result; + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/UdpPushService.java b/naming/src/main/java/com/alibaba/nacos/naming/push/UdpPushService.java index 76bcc7e54..676cc75ed 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/UdpPushService.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/UdpPushService.java @@ -26,6 +26,10 @@ import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.monitor.MetricsMonitor; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v1.ClientInfo; +import com.alibaba.nacos.naming.push.v1.NamingSubscriberServiceV1Impl; +import com.alibaba.nacos.naming.push.v1.PushClient; +import com.alibaba.nacos.naming.push.v1.ServiceChangeEvent; import com.alibaba.nacos.naming.remote.udp.AckEntry; import com.alibaba.nacos.naming.remote.udp.AckPacket; import com.alibaba.nacos.naming.remote.udp.UdpConnector; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/ClientInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/ClientInfo.java similarity index 97% rename from naming/src/main/java/com/alibaba/nacos/naming/push/ClientInfo.java rename to naming/src/main/java/com/alibaba/nacos/naming/push/v1/ClientInfo.java index fbb5f206f..1ca6883ed 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/ClientInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/ClientInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import org.apache.commons.lang3.StringUtils; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/DataSource.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/DataSource.java similarity index 90% rename from naming/src/main/java/com/alibaba/nacos/naming/push/DataSource.java rename to naming/src/main/java/com/alibaba/nacos/naming/push/v1/DataSource.java index 4594422f8..7f3f55fed 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/DataSource.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/DataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; /** * Data source for naming push. diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1Impl.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1Impl.java similarity index 97% rename from naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1Impl.java rename to naming/src/main/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1Impl.java index 9c380a429..a8435a343 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1Impl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1Impl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.naming.core.v2.pojo.Service; @@ -22,6 +22,7 @@ import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.NamingSubscriberService; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -36,8 +37,10 @@ import java.util.concurrent.TimeUnit; * Naming subscriber service for v1.x. * * @author xiweng.yy + * @deprecated Will be removed in v2.1.x version */ @org.springframework.stereotype.Service +@Deprecated public class NamingSubscriberServiceV1Impl implements NamingSubscriberService { private final ConcurrentMap> clientMap = new ConcurrentHashMap<>(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/PushClient.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/PushClient.java similarity index 99% rename from naming/src/main/java/com/alibaba/nacos/naming/push/PushClient.java rename to naming/src/main/java/com/alibaba/nacos/naming/push/v1/PushClient.java index 2ff881d5a..29bacc3ad 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/PushClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/PushClient.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.sys.utils.ApplicationUtils; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/ServiceChangeEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/ServiceChangeEvent.java similarity index 91% rename from naming/src/main/java/com/alibaba/nacos/naming/push/ServiceChangeEvent.java rename to naming/src/main/java/com/alibaba/nacos/naming/push/v1/ServiceChangeEvent.java index 04e4d72e1..d31fbc876 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/ServiceChangeEvent.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v1/ServiceChangeEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.naming.core.Service; import org.springframework.context.ApplicationEvent; diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java index 47714c182..ea984a81a 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java @@ -20,48 +20,40 @@ import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.MemberMetaDataConstants; import com.alibaba.nacos.core.cluster.NodeState; import com.alibaba.nacos.core.cluster.ServerMemberManager; -import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.pojo.Subscriber; -import com.alibaba.nacos.naming.push.NamingSubscriberServiceV1Impl; -import com.alibaba.nacos.naming.push.v2.NamingSubscriberServiceV2Impl; +import com.alibaba.nacos.naming.push.NamingSubscriberServiceAggregationImpl; +import com.alibaba.nacos.naming.push.NamingSubscriberServiceLocalImpl; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.mock.web.MockServletContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.test.util.ReflectionTestUtils; import java.util.ArrayList; import java.util.List; -@SpringBootTest -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = MockServletContext.class) -public class SubscribeManagerTest extends BaseTest { +@RunWith(MockitoJUnitRunner.class) +public class SubscribeManagerTest { - @Mock private SubscribeManager subscribeManager; @Mock - private NamingSubscriberServiceV1Impl namingSubscriberService; + private NamingSubscriberServiceAggregationImpl aggregation; @Mock - private NamingSubscriberServiceV2Impl namingSubscriberServiceV2; + private NamingSubscriberServiceLocalImpl local; @Mock private ServerMemberManager memberManager; @Before public void before() { - super.before(); subscribeManager = new SubscribeManager(); - ReflectionTestUtils.setField(subscribeManager, "subscriberServiceV1", namingSubscriberService); - ReflectionTestUtils.setField(subscribeManager, "subscriberServiceV2", namingSubscriberServiceV2); + ReflectionTestUtils.setField(subscribeManager, "aggregationService", aggregation); + ReflectionTestUtils.setField(subscribeManager, "localService", local); } @Test @@ -74,8 +66,7 @@ public class SubscribeManagerTest extends BaseTest { Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName, 0); clients.add(subscriber); - Mockito.when(namingSubscriberService.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())) - .thenReturn(clients); + Mockito.when(this.local.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())).thenReturn(clients); List list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); Assert.assertNotNull(list); Assert.assertEquals(1, list.size()); @@ -95,7 +86,7 @@ public class SubscribeManagerTest extends BaseTest { Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, "testGroupName@@test_subscriber", 0); clients.add(subscriber); - Mockito.when(namingSubscriberService.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())) + Mockito.when(this.aggregation.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())) .thenReturn(clients); List list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); Assert.assertNotNull(list); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImplTest.java new file mode 100644 index 000000000..9f32aa907 --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceAggregationImplTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 1999-2020 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.naming.push; + +import com.alibaba.nacos.core.cluster.Member; +import com.alibaba.nacos.core.cluster.ServerMemberManager; +import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.pojo.Subscriber; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.ConfigurableEnvironment; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class NamingSubscriberServiceAggregationImplTest { + + private final String namespace = "N"; + + private final String serviceName = "G@@S"; + + private final Service service = Service.newService(namespace, "G", "S"); + + @Mock + private ServerMemberManager memberManager; + + @Mock + private NamingSubscriberServiceLocalImpl local; + + @Mock + private ConfigurableEnvironment environment; + + private HashMap members; + + private NamingSubscriberServiceAggregationImpl aggregation; + + @Before + public void setUp() throws Exception { + aggregation = new NamingSubscriberServiceAggregationImpl(local, memberManager); + Subscriber subscriber = new Subscriber("local", "", "", "", namespace, serviceName, 0); + when(local.getSubscribers(namespace, serviceName)).thenReturn(Collections.singletonList(subscriber)); + when(local.getSubscribers(service)).thenReturn(Collections.singletonList(subscriber)); + when(local.getFuzzySubscribers(namespace, serviceName)).thenReturn(Collections.singletonList(subscriber)); + when(local.getFuzzySubscribers(service)).thenReturn(Collections.singletonList(subscriber)); + members = new HashMap<>(); + members.put("1", Mockito.mock(Member.class)); + when(memberManager.getServerList()).thenReturn(members); + } + + @Test + public void testGetSubscribersByStringWithLocal() { + Collection actual = aggregation.getSubscribers(namespace, serviceName); + assertEquals(1, actual.size()); + assertEquals("local", actual.iterator().next().getAddrStr()); + } + + @Test + public void testGetSubscribersByStringWithRemote() { + // TODO + } + + @Test + public void testGetSubscribersByServiceWithLocal() { + Collection actual = aggregation.getSubscribers(service); + assertEquals(1, actual.size()); + assertEquals("local", actual.iterator().next().getAddrStr()); + } + + @Test + public void testGetSubscribersByServiceWithRemote() { + // TODO + } + + @Test + public void testGetFuzzySubscribersByStringWithLocal() { + Collection actual = aggregation.getFuzzySubscribers(namespace, serviceName); + assertEquals(1, actual.size()); + assertEquals("local", actual.iterator().next().getAddrStr()); + } + + @Test + public void testGetFuzzySubscribersByServiceWithLocal() { + Collection actual = aggregation.getFuzzySubscribers(service); + assertEquals(1, actual.size()); + assertEquals("local", actual.iterator().next().getAddrStr()); + } +} diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/ClientInfoTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v1/ClientInfoTest.java similarity index 98% rename from naming/src/test/java/com/alibaba/nacos/naming/push/ClientInfoTest.java rename to naming/src/test/java/com/alibaba/nacos/naming/push/v1/ClientInfoTest.java index d489d61d6..e453dfab5 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/ClientInfoTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v1/ClientInfoTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import org.junit.Test; diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1ImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1ImplTest.java similarity index 98% rename from naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1ImplTest.java rename to naming/src/test/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1ImplTest.java index 178c7b0c5..575b5c347 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/NamingSubscriberServiceV1ImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v1/NamingSubscriberServiceV1ImplTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.naming.push; +package com.alibaba.nacos.naming.push.v1; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.pojo.Subscriber;