Search in sources :

Example 16 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class Unsubscribe_ITCase method unsubscribeCluster.

/**
 * 取消订阅,在指定cluster添加IP,不会收到通知
 * @throws Exception
 */
@Test
public void unsubscribeCluster() throws Exception {
    String serviceName = randomDomainName();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            instances = ((NamingEvent) event).getInstances();
        }
    };
    naming.subscribe(serviceName, Arrays.asList("c1"), listener);
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName)));
    naming.unsubscribe(serviceName, Arrays.asList("c1"), listener);
    instances = Collections.emptyList();
    naming.registerInstance(serviceName, "127.0.0.2", TEST_PORT, "c1");
    int i = 0;
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
        if (i++ > 10) {
            return;
        }
    }
    Assert.fail();
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class NacosNamingServiceTest method testSubscribe3.

@Test
public void testSubscribe3() throws NacosException {
    // given
    String serviceName = "service1";
    List<String> clusterList = Arrays.asList("cluster1", "cluster2");
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    };
    // when
    client.subscribe(serviceName, clusterList, listener);
    // then
    verify(changeNotifier, times(1)).registerListener(Constants.DEFAULT_GROUP, serviceName, "cluster1,cluster2", listener);
    verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 18 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class NacosNamingServiceTest method testUnSubscribe2.

@Test
public void testUnSubscribe2() throws NacosException {
    // given
    String serviceName = "service1";
    String groupName = "group1";
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    };
    when(changeNotifier.isSubscribed(serviceName, groupName, "")).thenReturn(false);
    // when
    client.unsubscribe(serviceName, groupName, listener);
    // then
    verify(changeNotifier, times(1)).deregisterListener(groupName, serviceName, "", listener);
    verify(proxy, times(1)).unsubscribe(serviceName, groupName, "");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 19 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class NacosNamingServiceTest method testUnSubscribe4.

@Test
public void testUnSubscribe4() throws NacosException {
    // given
    String serviceName = "service1";
    String groupName = "group1";
    List<String> clusterList = Arrays.asList("cluster1", "cluster2");
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    };
    when(changeNotifier.isSubscribed(serviceName, groupName, "cluster1,cluster2")).thenReturn(false);
    // when
    client.unsubscribe(serviceName, groupName, clusterList, listener);
    // then
    verify(changeNotifier, times(1)).deregisterListener(groupName, serviceName, "cluster1,cluster2", listener);
    verify(proxy, times(1)).unsubscribe(serviceName, groupName, "cluster1,cluster2");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 20 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class NacosNamingServiceTest method testUnSubscribe1.

@Test
public void testUnSubscribe1() throws NacosException {
    // given
    String serviceName = "service1";
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    };
    when(changeNotifier.isSubscribed(serviceName, Constants.DEFAULT_GROUP, "")).thenReturn(false);
    // when
    client.unsubscribe(serviceName, listener);
    // then
    verify(changeNotifier, times(1)).deregisterListener(Constants.DEFAULT_GROUP, serviceName, "", listener);
    verify(proxy, times(1)).unsubscribe(serviceName, Constants.DEFAULT_GROUP, "");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Aggregations

Event (com.alibaba.nacos.api.naming.listener.Event)29 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)28 Test (org.junit.Test)26 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 NamingService (com.alibaba.nacos.api.naming.NamingService)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 Properties (java.util.Properties)2 NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)1 NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 AbstractEventListener (com.alibaba.nacos.api.naming.listener.AbstractEventListener)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Arrays (java.util.Arrays)1