Search in sources :

Example 1 with AbstractListener

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

the class ClientWorkerTest method testAddListenerWithoutTenant.

@Test
public void testAddListenerWithoutTenant() throws NacosException {
    Properties prop = new Properties();
    ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties());
    ServerListManager agent = Mockito.mock(ServerListManager.class);
    ClientWorker clientWorker = new ClientWorker(filter, agent, prop);
    String dataId = "a";
    String group = "b";
    Listener listener = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    clientWorker.addListeners(dataId, group, Arrays.asList(listener));
    List<Listener> listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(1, listeners.size());
    Assert.assertEquals(listener, listeners.get(0));
    clientWorker.removeListener(dataId, group, listener);
    listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(0, listeners.size());
    CacheData cacheData = clientWorker.addCacheDataIfAbsent(dataId, group);
    Assert.assertEquals(cacheData, clientWorker.getCache(dataId, group));
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigFilterChainManager(com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with AbstractListener

use of com.alibaba.nacos.api.config.listener.AbstractListener 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 3 with AbstractListener

use of com.alibaba.nacos.api.config.listener.AbstractListener 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 4 with AbstractListener

use of com.alibaba.nacos.api.config.listener.AbstractListener 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 5 with AbstractListener

use of com.alibaba.nacos.api.config.listener.AbstractListener 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

AbstractListener (com.alibaba.nacos.api.config.listener.AbstractListener)9 Test (org.junit.Test)9 Listener (com.alibaba.nacos.api.config.listener.Listener)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Properties (java.util.Properties)3 ConfigFilterChainManager (com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager)2 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 BaseClusterTest (com.alibaba.nacos.test.base.BaseClusterTest)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1