use of com.alibaba.nacos.api.config.ConfigService in project incubator-skywalking by apache.
the class NacosConfigWatcherRegisterTest method shouldReadConfigs.
@Test
public void shouldReadConfigs() throws NacosException {
final String group = "skywalking";
final String testKey1 = "agent-analyzer.default.slowDBAccessThreshold";
final String testVal1 = "test";
final String testKey2 = "testKey";
final String testVal2 = "testVal";
final NacosServerSettings mockSettings = mock(NacosServerSettings.class);
when(mockSettings.getGroup()).thenReturn(group);
when(mockSettings.getNamespace()).thenReturn("");
final NacosConfigWatcherRegister mockRegister = spy(new NacosConfigWatcherRegister(mockSettings));
final ConfigService mockConfigService = mock(ConfigService.class);
when(mockConfigService.getConfig(testKey1, group, 1000)).thenReturn(testVal1);
when(mockConfigService.getConfig(testKey2, group, 1000)).thenReturn(testVal2);
Whitebox.setInternalState(mockRegister, "configService", mockConfigService);
final ConfigTable configTable = mockRegister.readConfig(Sets.newHashSet(testKey1, testKey2)).get();
assertEquals(2, configTable.getItems().size());
Map<String, String> kvs = new HashMap<>();
for (ConfigTable.ConfigItem item : configTable.getItems()) {
kvs.put(item.getName(), item.getValue());
}
assertEquals(testVal1, kvs.get(testKey1));
assertEquals(testVal2, kvs.get(testKey2));
}
use of com.alibaba.nacos.api.config.ConfigService in project skywalking by apache.
the class ITNacosConfigurationTest method shouldReadUpdated.
@SuppressWarnings("StatementWithEmptyBody")
@Test(timeout = 20000)
public void shouldReadUpdated() throws NacosException {
assertNull(provider.watcher.value());
final Properties properties = new Properties();
final String nacosHost = System.getProperty("nacos.host");
final String nacosPort = System.getProperty("nacos.port");
log.info("nacosHost: {}, nacosPort: {}", nacosHost, nacosPort);
properties.put("serverAddr", nacosHost + ":" + nacosPort);
final ConfigService configService = NacosFactory.createConfigService(properties);
assertTrue(configService.publishConfig("test-module.default.testKey", "skywalking", "500"));
for (String v = provider.watcher.value(); v == null; v = provider.watcher.value()) {
}
assertEquals("500", provider.watcher.value());
assertTrue(configService.removeConfig("test-module.default.testKey", "skywalking"));
for (String v = provider.watcher.value(); v != null; v = provider.watcher.value()) {
}
assertNull(provider.watcher.value());
}
use of com.alibaba.nacos.api.config.ConfigService in project skywalking by apache.
the class NacosConfigWatcherRegisterTest method shouldReadConfigs.
@Test
public void shouldReadConfigs() throws NacosException {
final String group = "skywalking";
final String testKey1 = "agent-analyzer.default.slowDBAccessThreshold";
final String testVal1 = "test";
final String testKey2 = "testKey";
final String testVal2 = "testVal";
final NacosServerSettings mockSettings = mock(NacosServerSettings.class);
when(mockSettings.getGroup()).thenReturn(group);
when(mockSettings.getNamespace()).thenReturn("");
final NacosConfigWatcherRegister mockRegister = spy(new NacosConfigWatcherRegister(mockSettings));
final ConfigService mockConfigService = mock(ConfigService.class);
when(mockConfigService.getConfig(testKey1, group, 1000)).thenReturn(testVal1);
when(mockConfigService.getConfig(testKey2, group, 1000)).thenReturn(testVal2);
Whitebox.setInternalState(mockRegister, "configService", mockConfigService);
final ConfigTable configTable = mockRegister.readConfig(Sets.newHashSet(testKey1, testKey2)).get();
assertEquals(2, configTable.getItems().size());
Map<String, String> kvs = new HashMap<>();
for (ConfigTable.ConfigItem item : configTable.getItems()) {
kvs.put(item.getName(), item.getValue());
}
assertEquals(testVal1, kvs.get(testKey1));
assertEquals(testVal2, kvs.get(testKey2));
}
use of com.alibaba.nacos.api.config.ConfigService 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);
}
use of com.alibaba.nacos.api.config.ConfigService in project nacos by alibaba.
the class ConfigAuth_ITCase method readWithReadPermission.
@Test
public void readWithReadPermission() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger ai = new AtomicInteger(0);
properties.put(PropertyKeyConst.USERNAME, username1);
properties.put(PropertyKeyConst.PASSWORD, password1);
iconfig = NacosFactory.createConfigService(properties);
final String content = "test" + System.currentTimeMillis();
System.out.println(content);
iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {
@Override
public void receiveConfigChange(ConfigChangeEvent event) {
ConfigChangeItem cci = event.getChangeItem("content");
System.out.println("content:" + cci);
if (!content.equals(cci.getNewValue())) {
return;
}
latch.countDown();
}
});
TimeUnit.SECONDS.sleep(3L);
properties.put(PropertyKeyConst.USERNAME, username2);
properties.put(PropertyKeyConst.PASSWORD, password2);
ConfigService configService = NacosFactory.createConfigService(properties);
boolean result = configService.publishConfig(dataId, group, content);
Assert.assertTrue(result);
TimeUnit.SECONDS.sleep(5L);
String res = iconfig.getConfig(dataId, group, TIME_OUT);
Assert.assertEquals(content, res);
latch.await(5L, TimeUnit.SECONDS);
Assert.assertEquals(0, latch.getCount());
}
Aggregations