Search in sources :

Example 16 with ConfigService

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));
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) HashMap(java.util.HashMap) ConfigTable(org.apache.skywalking.oap.server.configuration.api.ConfigTable) Test(org.junit.Test)

Example 17 with ConfigService

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());
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Properties(java.util.Properties) Test(org.junit.Test)

Example 18 with ConfigService

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));
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) HashMap(java.util.HashMap) ConfigTable(org.apache.skywalking.oap.server.configuration.api.ConfigTable) Test(org.junit.Test)

Example 19 with ConfigService

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);
}
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 20 with ConfigService

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());
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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

ConfigService (com.alibaba.nacos.api.config.ConfigService)35 Properties (java.util.Properties)19 Listener (com.alibaba.nacos.api.config.listener.Listener)11 Executor (java.util.concurrent.Executor)9 Test (org.junit.Test)8 NacosConfigProperties (com.alibaba.cloud.nacos.NacosConfigProperties)3 NacosException (com.alibaba.nacos.api.exception.NacosException)3 AbstractConfigurationTest (org.apache.shenyu.admin.AbstractConfigurationTest)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PostConstruct (javax.annotation.PostConstruct)2 ConfigTable (org.apache.skywalking.oap.server.configuration.api.ConfigTable)2 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)2 Bean (org.springframework.context.annotation.Bean)2 NacosFactory (com.alibaba.nacos.api.NacosFactory)1 ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)1 ConfigChangeItem (com.alibaba.nacos.api.config.ConfigChangeItem)1