Search in sources :

Example 16 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class NacosConfigServiceTest method testGetConfigAndSignListener.

@Test
public void testGetConfigAndSignListener() throws NacosException {
    final String dataId = "1";
    final String group = "2";
    final String tenant = "";
    final String content = "123";
    final int timeout = 3000;
    final Listener listener = new Listener() {

        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    ConfigResponse response = new ConfigResponse();
    response.setContent(content);
    response.setConfigType("bb");
    Mockito.when(mockWoker.getServerConfig(dataId, group, "", timeout, false)).thenReturn(response);
    final String config = nacosConfigService.getConfigAndSignListener(dataId, group, timeout, listener);
    Assert.assertEquals(content, config);
    Mockito.verify(mockWoker, Mockito.times(1)).getServerConfig(dataId, group, tenant, timeout, false);
    Mockito.verify(mockWoker, Mockito.times(1)).addTenantListenersWithContent(dataId, group, content, Arrays.asList(listener));
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigResponse(com.alibaba.nacos.client.config.filter.impl.ConfigResponse) Test(org.junit.Test)

Example 17 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class AbstractConfigAPI_CITCase method nacos_removeListener_3.

/**
 * @TCDescription : nacos_存在多个监听器时,删除最后一个监听器
 * @TestStep : TODO Test steps
 * @ExpectResult : TODO expect results
 * @author xiaochun.xxc
 * @since 3.6.8
 */
@Test(timeout = 5 * TIME_OUT)
public void nacos_removeListener_3() throws Exception {
    final String contentRemove = "test-abc-two";
    final AtomicInteger count = new AtomicInteger(0);
    Listener ml = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
            count.incrementAndGet();
        }
    };
    Listener ml1 = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
            // System.out.println("ml1 remove listener receive:" + configInfo);
            count.incrementAndGet();
            Assert.assertEquals(contentRemove, configInfo);
        }
    };
    iconfig.addListener(dataId, group, ml);
    iconfig.addListener(dataId, group, ml1);
    iconfig.removeListener(dataId, group, ml);
    Thread.sleep(TIME_OUT);
    boolean result = iconfig.publishConfig(dataId, group, contentRemove);
    Thread.sleep(TIME_OUT);
    Assert.assertTrue(result);
    while (count.get() == 0) {
        Thread.sleep(3000);
    }
    Assert.assertNotEquals(0, count.get());
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Test(org.junit.Test)

Example 18 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class AbstractConfigAPI_CITCase method nacos_addListener_6.

/**
 * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(进行配置参数设置)
 * @TestStep : TODO Test steps
 * @ExpectResult : TODO expect results
 * @author chuntaojun
 * @since 3.6.8
 */
@Test
public void nacos_addListener_6() throws InterruptedException, NacosException {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port);
    properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG, "true");
    properties.put(PropertyKeyConst.CONTEXT_PATH, contextPath);
    ConfigService iconfig = NacosFactory.createConfigService(properties);
    final AtomicInteger count = new AtomicInteger(0);
    final String dataId = "nacos_addListener_6";
    final String group = "nacos_addListener_6";
    final String content = "test-abc";
    final String newContent = "new-test-def";
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    Thread.sleep(2000);
    Listener ml = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
            count.incrementAndGet();
            System.out.println("Listener receive : [" + configInfo + "]");
            Assert.assertEquals(newContent, configInfo);
        }
    };
    iconfig.addListener(dataId, group, ml);
    String receiveContent = iconfig.getConfig(dataId, group, 1000);
    System.out.println(receiveContent);
    result = iconfig.publishConfig(dataId, group, newContent);
    Assert.assertTrue(result);
    Thread.sleep(2000);
    receiveContent = iconfig.getConfig(dataId, group, 1000);
    Assert.assertEquals(newContent, receiveContent);
    Assert.assertEquals(1, count.get());
    iconfig.removeListener(dataId, group, ml);
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 19 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class AbstractConfigAPI_CITCase method nacos_addListener_3.

/**
 * @TCDescription : nacos_添加对dataId的监听,修改服务端配置,正常推送并只推送一次
 * @TestStep : TODO Test steps
 * @ExpectResult : TODO expect results
 * @author xiaochun.xxc
 * @since 3.6.8
 */
@Test(timeout = Constants.CONFIG_LONG_POLL_TIMEOUT << 2)
public void nacos_addListener_3() throws InterruptedException, NacosException {
    final AtomicInteger count = new AtomicInteger(0);
    final String dataId = "nacos_addListener_3";
    final String group = "nacos_addListener_3";
    final String content = "test-abc-" + System.currentTimeMillis();
    final String newContent = "nacos_addListener_3-" + System.currentTimeMillis();
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    // Maximum assurance level notification has been performed
    ThreadUtils.sleep(5000);
    Listener ml = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
            count.incrementAndGet();
            Assert.assertEquals(newContent, configInfo);
        }
    };
    String receive = iconfig.getConfigAndSignListener(dataId, group, 5000L, ml);
    Assert.assertEquals(content, receive);
    result = iconfig.publishConfig(dataId, group, newContent);
    Assert.assertTrue(result);
    // Get enough sleep to ensure that the monitor is triggered only once
    // during the two long training sessions
    ThreadUtils.sleep(Constants.CONFIG_LONG_POLL_TIMEOUT << 1);
    Assert.assertEquals(1, count.get());
    iconfig.removeListener(dataId, group, ml);
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Test(org.junit.Test)

Example 20 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class AbstractConfigAPI_CITCase method nacos_addListener_4.

/**
 * @TCDescription : nacos_服务端无配置时,添加对dataId的监听
 * @TestStep : TODO Test steps
 * @ExpectResult : TODO expect results
 * @author xiaochun.xxc
 * @since 3.6.8
 */
@Test(timeout = 5 * TIME_OUT)
public void nacos_addListener_4() throws Exception {
    final AtomicInteger count = new AtomicInteger(0);
    iconfig.removeConfig(dataId, group);
    Thread.sleep(TIME_OUT);
    Listener ml = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
            count.incrementAndGet();
        }
    };
    iconfig.addListener(dataId, group, ml);
    Thread.sleep(TIME_OUT);
    String content = "test-abc";
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    while (count.get() == 0) {
        Thread.sleep(3000);
    }
    Assert.assertEquals(1, count.get());
    iconfig.removeListener(dataId, group, ml);
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Test(org.junit.Test)

Aggregations

Listener (com.alibaba.nacos.api.config.listener.Listener)45 Executor (java.util.concurrent.Executor)22 Test (org.junit.Test)14 Properties (java.util.Properties)13 ConfigService (com.alibaba.nacos.api.config.ConfigService)11 AbstractListener (com.alibaba.nacos.api.config.listener.AbstractListener)8 NacosException (com.alibaba.nacos.api.exception.NacosException)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ConfigFilterChainManager (com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager)4 ConnectionEventListener (com.alibaba.nacos.common.remote.client.ConnectionEventListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NacosConfigProperties (com.alibaba.cloud.nacos.NacosConfigProperties)2 AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)2 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)2 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 PostConstruct (javax.annotation.PostConstruct)2