Search in sources :

Example 1 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NacosNamingMaintainServiceTest method setUp.

@Before
public void setUp() throws Exception {
    Properties prop = new Properties();
    prop.setProperty(PropertyKeyConst.NAMESPACE, "public");
    prop.setProperty("serverAddr", "localhost");
    nacosNamingMaintainService = new NacosNamingMaintainService(prop);
    serverProxy = mock(NamingHttpClientProxy.class);
    serverListManager = mock(ServerListManager.class);
    securityProxy = mock(SecurityProxy.class);
    executorService = mock(ScheduledExecutorService.class);
    Field serverProxyField = NacosNamingMaintainService.class.getDeclaredField("serverProxy");
    serverProxyField.setAccessible(true);
    serverProxyField.set(nacosNamingMaintainService, serverProxy);
    Field serverListManagerField = NacosNamingMaintainService.class.getDeclaredField("serverListManager");
    serverListManagerField.setAccessible(true);
    serverListManagerField.set(nacosNamingMaintainService, serverListManager);
    Field securityProxyFiled = NacosNamingMaintainService.class.getDeclaredField("securityProxy");
    securityProxyFiled.setAccessible(true);
    securityProxyFiled.set(nacosNamingMaintainService, securityProxy);
    Field executorServiceField = NacosNamingMaintainService.class.getDeclaredField("executorService");
    executorServiceField.setAccessible(true);
    executorServiceField.set(nacosNamingMaintainService, executorService);
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) Field(java.lang.reflect.Field) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) Properties(java.util.Properties) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Before(org.junit.Before)

Example 2 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testServerHealthy.

@Test
public void testServerHealthy() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("{\"status\":\"UP\"}");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String serviceName = "service1";
    String groupName = "group1";
    // when
    boolean serverHealthy = clientProxy.serverHealthy();
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/operator/metrics"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertTrue(serverHealthy);
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 3 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testRegisterService.

@Test
public void testRegisterService() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("127.0.0.1:8848");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String serviceName = "service1";
    String groupName = "group1";
    Instance instance = new Instance();
    // when
    clientProxy.registerService(serviceName, groupName, instance);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(any(), any(), any(), any(), any(), any());
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) Instance(com.alibaba.nacos.api.naming.pojo.Instance) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 4 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testGetNamespaceId.

@Test
public void testGetNamespaceId() {
    // given
    // NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    // HttpRestResult<Object> a = new HttpRestResult<Object>();
    // a.setData("");
    // a.setCode(200);
    // when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    String namespaceId = "aaa";
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy(namespaceId, proxy, mgr, props, holder);
    // final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    // nacosRestTemplateField.setAccessible(true);
    // nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    // when
    String actualNamespaceId = clientProxy.getNamespaceId();
    Assert.assertEquals(namespaceId, actualNamespaceId);
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testGetBeatReactor.

@Test
public void testGetBeatReactor() throws Exception {
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    String namespaceId = "aaa";
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy(namespaceId, proxy, mgr, props, holder);
    BeatReactor mockBeatReactor = mock(BeatReactor.class);
    final Field mockBeatReactorField = NamingHttpClientProxy.class.getDeclaredField("beatReactor");
    mockBeatReactorField.setAccessible(true);
    mockBeatReactorField.set(clientProxy, mockBeatReactor);
    // when
    BeatReactor beatReactor = clientProxy.getBeatReactor();
    // then
    Assert.assertEquals(mockBeatReactor, beatReactor);
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) BeatReactor(com.alibaba.nacos.client.naming.beat.BeatReactor) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)25 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)25 Properties (java.util.Properties)24 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)23 Test (org.junit.Test)23 Field (java.lang.reflect.Field)22 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)18 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashMap (java.util.HashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)4 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 BeatReactor (com.alibaba.nacos.client.naming.beat.BeatReactor)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)2 NamingHttpClientProxy (com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy)2 HashSet (java.util.HashSet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Before (org.junit.Before)1