Search in sources :

Example 6 with ConfigChangeEvent

use of com.alibaba.nacos.api.config.ConfigChangeEvent in project nacos by alibaba.

the class ConfigLongPollReturnChanges_CITCase method testAdd.

@Test
public void testAdd() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String content = "config data";
    configService.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            try {
                ConfigChangeItem cci = event.getChangeItem("content");
                Assert.assertNull(cci.getOldValue());
                Assert.assertEquals(content, cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.ADDED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    boolean result = configService.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    configService.getConfig(dataId, group, 50);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with ConfigChangeEvent

use of com.alibaba.nacos.api.config.ConfigChangeEvent in project nacos by alibaba.

the class ConfigLongPollReturnChanges_CITCase method testModify.

@Test
public void testModify() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String oldData = "old data";
    final String newData = "new data";
    boolean result = configService.publishConfig(dataId, group, oldData);
    Assert.assertTrue(result);
    configService.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            try {
                ConfigChangeItem cci = event.getChangeItem("content");
                Assert.assertEquals(oldData, cci.getOldValue());
                Assert.assertEquals(newData, cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.MODIFIED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    configService.publishConfig(dataId, group, newData);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)7 AbstractConfigChangeListener (com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener)7 ConfigChangeItem (com.alibaba.nacos.api.config.ConfigChangeItem)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 NacosException (com.alibaba.nacos.api.exception.NacosException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)1 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1