Search in sources :

Example 6 with Event

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

the class MultiTenant_ITCase method multipleTenant_group_unSubscribe.

/**
 * @TCDescription : 多租户,多group下, 多个group订阅,查看服务的个数
 * @TestStep :
 * @ExpectResult :
 */
@Test
public void multipleTenant_group_unSubscribe() throws Exception {
    String serviceName = randomDomainName();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            instances = ((NamingEvent) event).getInstances();
        }
    };
    naming1.subscribe(serviceName, Constants.DEFAULT_GROUP, listener);
    naming1.subscribe(serviceName, TEST_GROUP_2, listener);
    naming1.subscribe(serviceName, TEST_GROUP_1, listener);
    naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming1.registerInstance(serviceName, TEST_GROUP_2, "33.33.33.33", TEST_PORT, "c1");
    while (instances.size() == 0) {
        TimeUnit.SECONDS.sleep(1L);
    }
    TimeUnit.SECONDS.sleep(2L);
    Assert.assertEquals(serviceName, naming1.getSubscribeServices().get(0).getName());
    Assert.assertEquals(3, naming1.getSubscribeServices().size());
    naming1.unsubscribe(serviceName, listener);
    naming1.unsubscribe(serviceName, TEST_GROUP_2, listener);
    TimeUnit.SECONDS.sleep(3L);
    Assert.assertEquals(1, naming1.getSubscribeServices().size());
    Assert.assertEquals(TEST_GROUP_1, naming1.getSubscribeServices().get(0).getGroupName());
    naming1.unsubscribe(serviceName, TEST_GROUP_1, listener);
}
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 7 with Event

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

the class MultiTenant_ITCase method multipleTenant_subscribe.

/**
 * @TCDescription : 多租户订阅服务
 * @TestStep :
 * @ExpectResult :
 */
@Test
public void multipleTenant_subscribe() throws Exception {
    String serviceName = randomDomainName();
    naming1.subscribe(serviceName, new EventListener() {

        @Override
        public void onEvent(Event event) {
            instances = ((NamingEvent) event).getInstances();
        }
    });
    naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming2.registerInstance(serviceName, "33.33.33.33", TEST_PORT, "c1");
    while (instances.size() == 0) {
        TimeUnit.SECONDS.sleep(1L);
    }
    Assert.assertEquals(1, instances.size());
    TimeUnit.SECONDS.sleep(2L);
    Assert.assertTrue(verifyInstanceList(instances, naming1.getAllInstances(serviceName)));
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) 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 8 with Event

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

the class MultiTenant_ITCase method multipleTenant_group_nosubscribe_unSubscribe.

/**
 * @TCDescription : 多租户,多group下, 没有对应的group订阅,取消订阅服务
 * @TestStep :
 * @ExpectResult :
 */
@Test
public void multipleTenant_group_nosubscribe_unSubscribe() throws Exception {
    String serviceName = randomDomainName();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            instances = ((NamingEvent) event).getInstances();
        }
    };
    naming1.subscribe(serviceName, TEST_GROUP_1, listener);
    naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming1.registerInstance(serviceName, TEST_GROUP_2, "33.33.33.33", TEST_PORT, "c1");
    TimeUnit.SECONDS.sleep(3L);
    Assert.assertEquals(serviceName, naming1.getSubscribeServices().get(0).getName());
    Assert.assertEquals(0, naming2.getSubscribeServices().size());
    // 取消订阅服务,没有订阅group
    naming1.unsubscribe(serviceName, listener);
    TimeUnit.SECONDS.sleep(3L);
    Assert.assertEquals(1, naming1.getSubscribeServices().size());
    // 取消订阅服务,有订阅group
    naming1.unsubscribe(serviceName, TEST_GROUP_1, listener);
    TimeUnit.SECONDS.sleep(3L);
    Assert.assertEquals(0, naming1.getSubscribeServices().size());
    Assert.assertEquals(0, naming2.getSubscribeServices().size());
}
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 9 with Event

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

the class MultiTenant_ITCase method multipleTenant_group_subscribe.

/**
 * @TCDescription : 多租户多group订阅服务
 * @TestStep :
 * @ExpectResult :
 */
@Test
public void multipleTenant_group_subscribe() throws Exception {
    String serviceName = randomDomainName();
    naming1.subscribe(serviceName, TEST_GROUP_1, new EventListener() {

        @Override
        public void onEvent(Event event) {
            instances = ((NamingEvent) event).getInstances();
        }
    });
    naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming1.registerInstance(serviceName, TEST_GROUP_1, "33.33.33.33", TEST_PORT, "c1");
    while (instances.size() == 0) {
        TimeUnit.SECONDS.sleep(1L);
    }
    TimeUnit.SECONDS.sleep(2L);
    Assert.assertEquals(1, instances.size());
    TimeUnit.SECONDS.sleep(2L);
    Assert.assertTrue(verifyInstanceList(instances, naming1.getAllInstances(serviceName, TEST_GROUP_1)));
    naming1.deregisterInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming1.deregisterInstance(serviceName, TEST_GROUP_1, "33.33.33.33", TEST_PORT, "c1");
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) 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 10 with Event

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

the class SubscribeCluster_ITCase method subscribeUnhealthy.

/**
 * 添加不可用IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 10000L)
public void subscribeUnhealthy() throws Exception {
    String serviceName = randomDomainName();
    naming.subscribe(serviceName, Arrays.asList("c1"), 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.registerInstance(serviceName, "1.1.1.1", TEST_PORT, "c1");
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName)));
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) 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)

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