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 c19af6436..5eb5a686f 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 @@ -53,12 +53,11 @@ public class SubscribeManager { private ServerListManager serverListManager; - private List getSubscribers(String serviceName, String namespaceId){ - return pushService.getClients(serviceName,namespaceId); + private List getSubscribers(String serviceName, String namespaceId) { + return pushService.getClients(serviceName, namespaceId); } /** - * * @param serviceName * @param namespaceId * @param aggregation @@ -66,10 +65,10 @@ public class SubscribeManager { * @throws InterruptedException */ public List getSubscribers(String serviceName, String namespaceId, boolean aggregation) throws InterruptedException { - if (aggregation){ + if (aggregation) { // size = 1 means only myself in the list, we need at least one another server alive: - while (serverListManager.getHealthyServers().size() <= 1) { - return getSubscribers(serviceName,namespaceId); + if (serverListManager.getHealthyServers().size() <= 1) { + return getSubscribers(serviceName, namespaceId); } List subscriberList = new ArrayList(); @@ -77,15 +76,15 @@ public class SubscribeManager { for (Server server : serverListManager.getHealthyServers()) { Map paramValues = new HashMap<>(128); - paramValues.put(CommonParams.SERVICE_NAME,serviceName); - paramValues.put(CommonParams.NAMESPACE_ID,namespaceId); - paramValues.put("aggregation",String.valueOf(Boolean.FALSE)); + paramValues.put(CommonParams.SERVICE_NAME, serviceName); + paramValues.put(CommonParams.NAMESPACE_ID, namespaceId); + paramValues.put("aggregation", String.valueOf(Boolean.FALSE)); if (NetUtils.localServer().equals(server.getKey())) { - subscriberList.addAll(getSubscribers(serviceName,namespaceId)); + subscriberList.addAll(getSubscribers(serviceName, namespaceId)); } HttpClient.HttpResult result = HttpClient.httpGet("http://" + server.getKey() + RunningConfig.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(),paramValues); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues); if (HttpURLConnection.HTTP_OK == result.code) { Subscribers subscribers = (Subscribers) JSONObject.parseObject(result.content, Subscribers.class); @@ -96,7 +95,7 @@ public class SubscribeManager { } } else { // local server - return getSubscribers(serviceName,namespaceId); + return getSubscribers(serviceName, namespaceId); } return Collections.emptyList(); } 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 new file mode 100644 index 000000000..97960f061 --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java @@ -0,0 +1,90 @@ +package com.alibaba.nacos.naming.core; + +import com.alibaba.nacos.naming.BaseTest; +import com.alibaba.nacos.naming.cluster.ServerListManager; +import com.alibaba.nacos.naming.cluster.servers.Server; +import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.PushService; +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.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Nicholas + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class SubscribeManagerTest extends BaseTest { + + @Mock + private SubscribeManager subscribeManager; + + @Mock + private PushService pushService; + + @Mock + private ServerListManager serverListManager; + + @Before + public void before() { + super.before(); + subscribeManager = new SubscribeManager(); + } + + @Test + public void getSubscribersWithFalse() { + String serviceName = "test"; + String namespaceId = "public"; + boolean aggregation = Boolean.FALSE; + try { + List clients = new ArrayList(); + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName); + clients.add(subscriber); + Mockito.when(pushService.getClients(Mockito.anyString(), Mockito.anyString())).thenReturn(clients); + subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + } catch (Exception e) { + + } + } + + @Test + public void getSubscribersWithTrue() { + String serviceName = "test"; + String namespaceId = "public"; + boolean aggregation = Boolean.TRUE; + try { + List clients = new ArrayList(); + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName); + clients.add(subscriber); + + List healthyServers = new ArrayList<>(); + + for (int i = 0; i <= 2; i++) { + Server server = new Server(); + server.setIp("127.0.0.1"); + server.setServePort(8080 + i); + server.setAlive(Boolean.TRUE); + server.setAdWeight(10); + server.setLastRefTime(System.currentTimeMillis()); + server.setLastRefTimeStr(String.valueOf(System.currentTimeMillis())); + server.setSite("site"); + server.setWeight(1); + healthyServers.add(server); + } + + Mockito.when(serverListManager.getHealthyServers()).thenReturn(healthyServers); + //Mockito.doReturn(3).when(serverListManager.getHealthyServers().size()); + subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + } catch (Exception e) { + + } + } +} + diff --git a/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java new file mode 100644 index 000000000..d41222ede --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java @@ -0,0 +1,36 @@ +package com.alibaba.nacos.naming.pojo; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Nicholas + */ +public class SubscriberTest { + + @Test + public void subscriberBeanTest() { + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "agent", "app", "127.0.0.1", "public", "test"); + subscriber.setAddrStr("127.0.0.1:8080"); + subscriber.setIp("127.0.0.1"); + subscriber.setApp("app"); + subscriber.setAgent("agent"); + subscriber.setNamespaceId("public"); + subscriber.setServiceName("test"); + + subscriber.getAddrStr(); + subscriber.getIp(); + subscriber.getAgent(); + subscriber.getApp(); + subscriber.getNamespaceId(); + subscriber.getServiceName(); + + Subscribers subscribers = new Subscribers(); + List subscriberList = new ArrayList<>(); + subscriberList.add(subscriber); + subscribers.setSubscribers(subscriberList); + subscribers.getSubscribers(); + } +}