use of com.alibaba.nacos.api.config.listener.AbstractListener in project nacos by alibaba.
the class ClientWorkerTest method testListenerWithTenant.
@Test
public void testListenerWithTenant() 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);
Listener listener = new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
}
};
String dataId = "a";
String group = "b";
clientWorker.addTenantListeners(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.removeTenantListener(dataId, group, listener);
listeners = clientWorker.getCache(dataId, group).getListeners();
Assert.assertEquals(0, listeners.size());
String content = "d";
clientWorker.addTenantListenersWithContent(dataId, group, content, Arrays.asList(listener));
listeners = clientWorker.getCache(dataId, group).getListeners();
Assert.assertEquals(1, listeners.size());
Assert.assertEquals(listener, listeners.get(0));
clientWorker.removeTenantListener(dataId, group, listener);
listeners = clientWorker.getCache(dataId, group).getListeners();
Assert.assertEquals(0, listeners.size());
String tenant = "c";
CacheData cacheData = clientWorker.addCacheDataIfAbsent(dataId, group, tenant);
Assert.assertEquals(cacheData, clientWorker.getCache(dataId, group, tenant));
clientWorker.removeCache(dataId, group, tenant);
Assert.assertNull(clientWorker.getCache(dataId, group, tenant));
}
use of com.alibaba.nacos.api.config.listener.AbstractListener in project nacos by alibaba.
the class AbstractConfigAPI_CITCase method nacos_removeListener_1.
/**
* @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_1() throws Exception {
iconfig.addListener(dataId, group, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
Assert.fail();
}
});
Thread.sleep(TIME_OUT);
try {
iconfig.removeListener(dataId, group, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("remove receive:" + configInfo);
}
});
} catch (Exception e) {
}
}
use of com.alibaba.nacos.api.config.listener.AbstractListener in project nacos by alibaba.
the class AbstractConfigAPI_CITCase method nacos_addListener_5.
/**
* @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(使用特定接口)
* @TestStep : TODO Test steps
* @ExpectResult : TODO expect results
* @author chuntaojun
* @since 3.6.8
*/
@Test
public void nacos_addListener_5() throws InterruptedException, NacosException {
final AtomicInteger count = new AtomicInteger(0);
final String dataId = "nacos_addListener_5";
final String group = "nacos_addListener_5";
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();
Assert.assertEquals(newContent, configInfo);
}
};
String receiveContent = iconfig.getConfigAndSignListener(dataId, group, 1000, ml);
System.out.println(receiveContent);
result = iconfig.publishConfig(dataId, group, newContent);
Assert.assertTrue(result);
Assert.assertEquals(content, receiveContent);
Thread.sleep(2000);
Assert.assertEquals(1, count.get());
iconfig.removeListener(dataId, group, ml);
}
use of com.alibaba.nacos.api.config.listener.AbstractListener in project nacos by alibaba.
the class ConfigDerbyRaft_DITCase method test_k_config_listener.
@Test
public void test_k_config_listener() throws Exception {
String dataId = "test_h_config_listener";
String group = "test_h_config_listener";
String content = "test_h_config_listener";
CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1), new CountDownLatch(0) };
AtomicReference<String> r = new AtomicReference<>();
AtomicInteger i = new AtomicInteger(0);
iconfig7.addListener(dataId, group, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println(configInfo);
r.set(configInfo);
latch[i.getAndIncrement()].countDown();
}
});
iconfig7.publishConfig(dataId, group, content);
ThreadUtils.sleep(10_000L);
latch[0].await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(content, r.get());
Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L));
content = content + System.currentTimeMillis();
iconfig7.publishConfig(dataId, group, content);
ThreadUtils.sleep(10_000L);
latch[1].await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(content, r.get());
Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L));
}
Aggregations