Search in sources :

Example 6 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileLongPollingServiceTest method testNotReceivedPushEvent.

@Test
public void testNotReceivedPushEvent() throws InterruptedException {
    // 初始化 LongPollingService
    DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
    RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
    longPollingService.addConfigFile(remoteConfigFileRepo);
    // 第一次收到变更事件,第二次也没有变化
    when(configFileConnector.watchConfigFiles(anyList())).then(invocation -> {
        return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
    }).then(invocation -> {
        TimeUnit.SECONDS.sleep(6);
        return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
    });
    // 因为LongPolling会在 5s 后开始执行
    TimeUnit.SECONDS.sleep(7);
    verify(configFileConnector, times(2)).watchConfigFiles(anyList());
    // 没有触发回调
    verify(remoteConfigFileRepo, times(0)).onLongPollNotified(anyLong());
}
Also used : ErrorCode(com.tencent.polaris.api.exception.ErrorCode) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ConfigFileTestUtils(com.tencent.polaris.configuration.client.ConfigFileTestUtils) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) RetriableException(com.tencent.polaris.api.exception.RetriableException) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) SDKContext(com.tencent.polaris.client.api.SDKContext) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) ConfigFileConnector(com.tencent.polaris.api.plugin.configuration.ConfigFileConnector) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ServerCodes(com.tencent.polaris.api.exception.ServerCodes) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 7 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileLongPollingServiceTest method testReceivedPushEvent.

@Test
public void testReceivedPushEvent() throws InterruptedException {
    // 初始化 LongPollingService
    DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
    RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
    longPollingService.addConfigFile(remoteConfigFileRepo);
    // 构造监听响应对象
    ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
    String content = "hello world";
    long version = 100;
    configFile.setContent(content);
    configFile.setVersion(version);
    ConfigFileResponse configFileResponse = new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
    // 第一次收到变更事件,第二次没有变化
    when(configFileConnector.watchConfigFiles(anyList())).thenReturn(configFileResponse).then(invocation -> {
        TimeUnit.SECONDS.sleep(30);
        return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
    });
    // 因为LongPolling会在 5s 后开始执行
    TimeUnit.SECONDS.sleep(6);
    verify(configFileConnector, times(2)).watchConfigFiles(anyList());
    // 验证触发回调
    verify(remoteConfigFileRepo).onLongPollNotified(version);
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 8 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileLongPollingServiceTest method testSecondReceivedVersionLessThanFirstReceived.

@Test
public void testSecondReceivedVersionLessThanFirstReceived() throws InterruptedException {
    // 初始化 LongPollingService
    DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
    RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
    longPollingService.addConfigFile(remoteConfigFileRepo);
    // 第一次版本号为100,第二次版本号为99
    when(configFileConnector.watchConfigFiles(anyList())).then(invocation -> {
        ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
        String content = "hello world";
        long version = 100;
        configFile.setContent(content);
        configFile.setVersion(version);
        return new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
    }).then(invocation -> {
        ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
        String content = "hello world";
        long version = 99;
        configFile.setContent(content);
        configFile.setVersion(version);
        return new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
    }).then(invocation -> {
        TimeUnit.SECONDS.sleep(30);
        return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
    });
    // 因为LongPolling会在 5s 后开始执行
    TimeUnit.SECONDS.sleep(7);
    verify(configFileConnector, times(3)).watchConfigFiles(anyList());
    // 没有触发回调
    verify(remoteConfigFileRepo, times(2)).onLongPollNotified(100);
}
Also used : ErrorCode(com.tencent.polaris.api.exception.ErrorCode) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ConfigFileTestUtils(com.tencent.polaris.configuration.client.ConfigFileTestUtils) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) RetriableException(com.tencent.polaris.api.exception.RetriableException) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) SDKContext(com.tencent.polaris.client.api.SDKContext) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) ConfigFileConnector(com.tencent.polaris.api.plugin.configuration.ConfigFileConnector) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ServerCodes(com.tencent.polaris.api.exception.ServerCodes) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 9 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileLongPollingServiceTest method testThrowRetryException.

@Test
public void testThrowRetryException() throws InterruptedException {
    // 初始化 LongPollingService
    DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
    RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
    longPollingService.addConfigFile(remoteConfigFileRepo);
    // 模拟抛异常
    when(configFileConnector.watchConfigFiles(anyList())).thenThrow(new RetriableException(ErrorCode.API_TIMEOUT, ""));
    // 因为LongPolling会在 5s 后开始执行
    TimeUnit.SECONDS.sleep(7);
    verify(configFileConnector, times(2)).watchConfigFiles(anyList());
    // 没有触发回调
    verify(remoteConfigFileRepo, times(0)).onLongPollNotified(anyLong());
}
Also used : ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) RetriableException(com.tencent.polaris.api.exception.RetriableException) Test(org.junit.Test)

Example 10 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileManagerTest method testGetConfigPropertiesFile.

@Test
public void testGetConfigPropertiesFile() {
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    ConfigKVFile mockedConfigFile = mock(ConfigKVFile.class);
    when(configFileFactoryManager.getFactory(any())).thenReturn(configFileFactory);
    when(configFileFactory.createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties)).thenReturn(mockedConfigFile);
    // 第一次获取
    ConfigKVFile configFile = defaultConfigFileManager.getConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
    Assert.assertEquals(mockedConfigFile, configFile);
    // 第二次获取,经过缓存
    ConfigKVFile configFile2 = defaultConfigFileManager.getConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
    Assert.assertEquals(mockedConfigFile, configFile2);
}
Also used : ConfigKVFile(com.tencent.polaris.configuration.api.core.ConfigKVFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Aggregations

ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)17 Test (org.junit.Test)12 ConfigFileResponse (com.tencent.polaris.api.plugin.configuration.ConfigFileResponse)8 ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)7 RetriableException (com.tencent.polaris.api.exception.RetriableException)4 DefaultConfigFileMetadata (com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata)3 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)2 ServerCodes (com.tencent.polaris.api.exception.ServerCodes)2 ConfigFileConnector (com.tencent.polaris.api.plugin.configuration.ConfigFileConnector)2 SDKContext (com.tencent.polaris.client.api.SDKContext)2 ConfigFileTestUtils (com.tencent.polaris.configuration.client.ConfigFileTestUtils)2 TimeUnit (java.util.concurrent.TimeUnit)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 Mock (org.mockito.Mock)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2