add test case

This commit is contained in:
Nicholas2015 2019-06-08 20:52:10 +08:00
parent dc61375c29
commit 7d52d47f74
3 changed files with 137 additions and 12 deletions

View File

@ -53,12 +53,11 @@ public class SubscribeManager {
private ServerListManager serverListManager;
private List<Subscriber> getSubscribers(String serviceName, String namespaceId){
return pushService.getClients(serviceName,namespaceId);
private List<Subscriber> 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<Subscriber> 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<Subscriber> subscriberList = new ArrayList<Subscriber>();
@ -77,15 +76,15 @@ public class SubscribeManager {
for (Server server : serverListManager.getHealthyServers()) {
Map<String, String> 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();
}

View File

@ -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<Subscriber> clients = new ArrayList<Subscriber>();
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<Subscriber> clients = new ArrayList<Subscriber>();
Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName);
clients.add(subscriber);
List<Server> 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) {
}
}
}

View File

@ -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<Subscriber> subscriberList = new ArrayList<>();
subscriberList.add(subscriber);
subscribers.setSubscribers(subscriberList);
subscribers.getSubscribers();
}
}