Search in sources :

Example 1 with NamingHttpClientProxy

use of com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy in project nacos by alibaba.

the class BeatReactorTest method testBuildBeatInfo2.

@Test
public void testBuildBeatInfo2() {
    String ip = "11.11.11.11";
    int port = 1234;
    double weight = 1.0;
    String serviceName = "service";
    String clusterName = "cluster1";
    Map<String, String> meta = new HashMap<>();
    Instance instance = new Instance();
    instance.setServiceName(serviceName);
    instance.setIp(ip);
    instance.setPort(port);
    instance.setWeight(weight);
    instance.setMetadata(meta);
    instance.setClusterName(clusterName);
    String groupedService = "group1@@service";
    BeatInfo expectInfo = new BeatInfo();
    expectInfo.setServiceName(groupedService);
    expectInfo.setIp(ip);
    expectInfo.setPort(port);
    expectInfo.setCluster(clusterName);
    expectInfo.setWeight(weight);
    expectInfo.setMetadata(meta);
    expectInfo.setScheduled(false);
    expectInfo.setPeriod(Constants.DEFAULT_HEART_BEAT_INTERVAL);
    NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
    BeatReactor beatReactor = new BeatReactor(proxy);
    assertBeatInfoEquals(expectInfo, beatReactor.buildBeatInfo(groupedService, instance));
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) HashMap(java.util.HashMap) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Test(org.junit.Test)

Example 2 with NamingHttpClientProxy

use of com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy in project nacos by alibaba.

the class BeatReactorTest method testBuildKey.

@Test
public void testBuildKey() {
    String ip = "11.11.11.11";
    int port = 1234;
    String serviceName = "serviceName1";
    NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
    BeatReactor beatReactor = new BeatReactor(proxy);
    Assert.assertEquals(serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port, beatReactor.buildKey(serviceName, ip, port));
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) Test(org.junit.Test)

Example 3 with NamingHttpClientProxy

use of com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy in project nacos by alibaba.

the class BeatReactorTest method testIntervalFromResponse.

@Test
public void testIntervalFromResponse() throws JsonProcessingException, NacosException, InterruptedException {
    BeatInfo beatInfo = new BeatInfo();
    beatInfo.setServiceName("test");
    beatInfo.setIp("11.11.11.11");
    beatInfo.setPort(1234);
    beatInfo.setCluster("clusterName");
    beatInfo.setWeight(1);
    beatInfo.setMetadata(new HashMap<String, String>());
    beatInfo.setScheduled(false);
    beatInfo.setPeriod(10L);
    NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
    String jsonString = "{\"clientBeatInterval\":20}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(jsonString);
    Mockito.when(proxy.sendBeat(beatInfo, false)).thenReturn(actualObj);
    BeatReactor beatReactor = new BeatReactor(proxy);
    String serviceName = "serviceName1";
    beatReactor.addBeatInfo(serviceName, beatInfo);
    TimeUnit.MILLISECONDS.sleep(12);
    Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
    TimeUnit.MILLISECONDS.sleep(20);
    Mockito.verify(proxy, Mockito.times(2)).sendBeat(beatInfo, false);
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with NamingHttpClientProxy

use of com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy in project nacos by alibaba.

the class BeatReactorTest method testBuildBeatInfo1.

@Test
public void testBuildBeatInfo1() {
    String ip = "11.11.11.11";
    int port = 1234;
    double weight = 1.0;
    String serviceName = "service@@group1";
    String clusterName = "cluster1";
    Map<String, String> meta = new HashMap<>();
    Instance instance = new Instance();
    instance.setServiceName(serviceName);
    instance.setIp(ip);
    instance.setPort(port);
    instance.setWeight(weight);
    instance.setMetadata(meta);
    instance.setClusterName(clusterName);
    BeatInfo expectInfo = new BeatInfo();
    expectInfo.setServiceName(serviceName);
    expectInfo.setIp(ip);
    expectInfo.setPort(port);
    expectInfo.setCluster(clusterName);
    expectInfo.setWeight(weight);
    expectInfo.setMetadata(meta);
    expectInfo.setScheduled(false);
    expectInfo.setPeriod(Constants.DEFAULT_HEART_BEAT_INTERVAL);
    NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
    BeatReactor beatReactor = new BeatReactor(proxy);
    assertBeatInfoEquals(expectInfo, beatReactor.buildBeatInfo(instance));
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) HashMap(java.util.HashMap) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Test(org.junit.Test)

Example 5 with NamingHttpClientProxy

use of com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy in project nacos by alibaba.

the class NamingClientProxyDelegateTest method testRegisterServiceByHttp.

@Test
public void testRegisterServiceByHttp() throws NacosException, NoSuchFieldException, IllegalAccessException {
    String ns = "ns1";
    ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
    Properties props = new Properties();
    props.setProperty("serverAddr", "localhost");
    InstancesChangeNotifier notifier = new InstancesChangeNotifier();
    NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier);
    NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class);
    Field mockHttpClientField = NamingClientProxyDelegate.class.getDeclaredField("httpClientProxy");
    mockHttpClientField.setAccessible(true);
    mockHttpClientField.set(delegate, mockHttpClient);
    String serviceName = "service1";
    String groupName = "group1";
    Instance instance = new Instance();
    instance.setServiceName(serviceName);
    instance.setClusterName(groupName);
    instance.setIp("1.1.1.1");
    instance.setPort(1);
    // use grpc
    instance.setEphemeral(false);
    delegate.registerService(serviceName, groupName, instance);
    verify(mockHttpClient, times(1)).registerService(serviceName, groupName, instance);
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

NamingHttpClientProxy (com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy)15 Test (org.junit.Test)13 Instance (com.alibaba.nacos.api.naming.pojo.Instance)6 Field (java.lang.reflect.Field)6 Properties (java.util.Properties)4 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)1 NamingClientProxy (com.alibaba.nacos.client.naming.remote.NamingClientProxy)1 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)1 HashSet (java.util.HashSet)1