From 3990670aedef3f1721301af169fc882fb43ac6d5 Mon Sep 17 00:00:00 2001 From: shedfreewu Date: Wed, 20 Nov 2024 20:24:45 +0800 Subject: [PATCH] fix --- .../polaris/config/ConfigurationModifier.java | 5 +- .../adapter/PolarisConfigFileLocator.java | 2 +- .../PolarisConfigPropertyAutoRefresher.java | 8 +- .../config/PolarisConfigProperties.java | 11 ++ .../adapter/PolarisConfigFileLocatorTest.java | 17 ++ .../listener/ConfigChangeListenerTest.java | 21 ++- ...hOptimizationListenerNotTriggeredTest.java | 152 ----------------- ...reshOptimizationListenerTriggeredTest.java | 155 ------------------ .../src/test/resources/application-test.yml | 2 + 9 files changed, 60 insertions(+), 313 deletions(-) delete mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java delete mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index 69025b48ed..2136bb2c4a 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -104,8 +104,9 @@ private void initDataSource(ConfigurationImpl configuration) { throw new RuntimeException("Config server address is blank. Please check your config in bootstrap.yml" + " with spring.cloud.polaris.address or spring.cloud.polaris.config.address"); } - - checkAddressAccessible(configAddresses); + if (polarisConfigProperties.isCheckAddress()) { + checkAddressAccessible(configAddresses); + } configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java index c4a25a06d2..1063b23dfb 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -69,7 +69,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { // this class provides customized logic for some customers to configure special business group files private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); - private volatile CompositePropertySource compositePropertySourceCache = null; + private volatile static CompositePropertySource compositePropertySourceCache = null; public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java index 9411e98e6a..e38324e4d1 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java @@ -30,6 +30,7 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext; import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils; +import com.tencent.polaris.configuration.api.core.ConfigFileGroup; import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; @@ -122,8 +123,11 @@ private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher } private void registerPolarisConfigGroupChangeListener(PolarisPropertySource polarisPropertySource) { - configFileService.getConfigFileGroup(polarisPropertySource.getNamespace(), polarisPropertySource.getGroup()). - addChangeListener(event -> { + ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(polarisPropertySource.getNamespace(), polarisPropertySource.getGroup()); + if (configFileGroup == null) { + return; + } + configFileGroup.addChangeListener(event -> { try { LOGGER.debug("ConfigFileGroup receive onChange event:{}", event); List oldConfigFileMetadataList = event.getOldConfigFileMetadataList(); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index 975a0e6c76..1a5a994ecc 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -96,6 +96,8 @@ public class PolarisConfigProperties { */ private boolean internalEnabled = true; + private boolean checkAddress = true; + public boolean isEnabled() { return enabled; } @@ -192,6 +194,14 @@ public void setInternalEnabled(boolean internalEnabled) { this.internalEnabled = internalEnabled; } + public boolean isCheckAddress() { + return checkAddress; + } + + public void setCheckAddress(boolean checkAddress) { + this.checkAddress = checkAddress; + } + @Override public String toString() { return "PolarisConfigProperties{" + @@ -207,6 +217,7 @@ public String toString() { ", dataSource='" + dataSource + '\'' + ", localFileRootPath='" + localFileRootPath + '\'' + ", internalEnabled=" + internalEnabled + + ", checkAddress=" + checkAddress + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java index 5d25b2067d..85c762c2f6 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.config.adapter; +import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; @@ -68,6 +69,7 @@ public void setUp() { @Test public void testLoadApplicationPropertiesFile() { + clearCompositePropertySourceCache(); PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, configFileService, environment); @@ -105,6 +107,7 @@ public void testLoadApplicationPropertiesFile() { @Test public void testActiveProfileFilesPriorityBiggerThanDefault() { + clearCompositePropertySourceCache(); PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, configFileService, environment); @@ -154,6 +157,7 @@ public void testActiveProfileFilesPriorityBiggerThanDefault() { @Test public void testGetCustomFiles() { + clearCompositePropertySourceCache(); PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, configFileService, environment); @@ -208,6 +212,7 @@ public void testGetCustomFiles() { @Test public void testGetCustomGroupFiles() { + clearCompositePropertySourceCache(); PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, configFileService, environment); @@ -260,4 +265,16 @@ public void testGetCustomGroupFiles() { assertThat(propertySource.getProperty("k2")).isEqualTo("v2"); assertThat(propertySource.getProperty("k3")).isEqualTo("v3"); } + + private void clearCompositePropertySourceCache() { + try { + Class clazz = PolarisConfigFileLocator.class; + Field field = clazz.getDeclaredField("compositePropertySourceCache"); + field.setAccessible(true); + field.set(null, null); + } + catch (Exception e) { + // ignore + } + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/ConfigChangeListenerTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/ConfigChangeListenerTest.java index 465af43531..0c5e2eb43b 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/ConfigChangeListenerTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/ConfigChangeListenerTest.java @@ -18,13 +18,16 @@ package com.tencent.cloud.polaris.config.listener; +import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import com.google.common.collect.Sets; +import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -35,6 +38,7 @@ import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.CompositePropertySource; import org.springframework.stereotype.Component; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -47,7 +51,9 @@ */ @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = DEFINED_PORT, classes = ConfigChangeListenerTest.TestApplication.class, - properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml"}) + properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml", + "spring.cloud.polaris.config.connect-remote-server=true", "spring.cloud.polaris.config.check-address=false" +}) public class ConfigChangeListenerTest { private static final CountDownLatch hits = new CountDownLatch(2); @@ -58,6 +64,19 @@ public class ConfigChangeListenerTest { @Autowired private TestApplication.TestConfig testConfig; + @BeforeAll + public static void setUp() { + try { + Class clazz = PolarisConfigFileLocator.class; + Field field = clazz.getDeclaredField("compositePropertySourceCache"); + field.setAccessible(true); + field.set(null, new CompositePropertySource("mock")); + } + catch (Exception e) { + // ignore + } + } + @Test public void test() throws InterruptedException { //before change diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java deleted file mode 100644 index 5ee33ef433..0000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - */ - -package com.tencent.cloud.polaris.config.listener; - -import java.util.HashMap; -import java.util.Map; - -import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; -import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; -import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; -import com.tencent.cloud.polaris.config.enums.RefreshType; -import com.tencent.polaris.configuration.api.core.ChangeType; -import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; -import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.context.refresh.ContextRefresher; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; - -/** - * test for {@link PolarisConfigRefreshOptimizationListener}. - */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerNotTriggeredTest.TestApplication.class, - properties = { - "server.port=48081", - "spring.cloud.polaris.address=grpc://127.0.0.1:10081", - "spring.cloud.polaris.config.refresh-type=reflect", - "spring.config.location = classpath:application-test.yml" - }) -public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest { - - private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher"; - - private static final String TEST_NAMESPACE = "testNamespace"; - - private static final String TEST_SERVICE_NAME = "testServiceName"; - - private static final String TEST_FILE_NAME = "application.properties"; - - @Autowired - private ConfigurableApplicationContext context; - - @BeforeAll - static void beforeAll() { - PolarisPropertySourceManager.clearPropertySources(); - } - - @Test - public void testNotSwitchConfigRefreshType() { - RefreshType actualRefreshType = context.getEnvironment() - .getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class); - assertThat(actualRefreshType).isEqualTo(RefreshType.REFLECT); - PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class); - assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFLECT); - assertThat(context.containsBean(REFLECT_REFRESHER_BEAN_NAME)).isTrue(); - PolarisRefreshAffectedContextRefresher refresher = context - .getBean(REFLECT_REFRESHER_BEAN_NAME, PolarisRefreshAffectedContextRefresher.class); - assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue(); - } - - @Test - public void testConfigFileChanged() { - Map content = new HashMap<>(); - content.put("k1", "v1"); - content.put("k2", "v2"); - content.put("k3", "v3"); - MockedConfigKVFile file = new MockedConfigKVFile(content); - - PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, - file, content); - PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - - PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class); - PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher); - - refresher.setRegistered(false); - spyRefresher.onApplicationEvent(null); - - ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); - ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED); - ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED); - Map changeInfos = new HashMap<>(); - changeInfos.put("k1", changeInfo); - changeInfos.put("k2", changeInfo3); - changeInfos.put("k4", changeInfo2); - ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos); - file.fireChangeListener(event); - - ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class); - when(mockContextRefresher.refresh()).thenReturn(event.changedKeys()); - - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k1"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k2"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k4"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshConfigurationProperties(event.changedKeys()); - } - - @SpringBootApplication - protected static class TestApplication { - - @Primary - @Bean - public ContextRefresher contextRefresher() { - return mock(ContextRefresher.class); - } - - @Component - protected static class TestBeanWithoutRefreshScope { - - } - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java deleted file mode 100644 index e352685576..0000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - */ - -package com.tencent.cloud.polaris.config.listener; - -import java.util.HashMap; -import java.util.Map; - -import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; -import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; -import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; -import com.tencent.cloud.polaris.config.enums.RefreshType; -import com.tencent.polaris.configuration.api.core.ChangeType; -import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; -import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.cloud.context.refresh.ContextRefresher; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; - -/** - * test for {@link PolarisConfigRefreshOptimizationListener}. - */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerTriggeredTest.TestApplication.class, - properties = { - "server.port=48081", - "spring.cloud.polaris.address=grpc://127.0.0.1:10081", - "spring.cloud.polaris.config.refresh-type=reflect", - "spring.config.location = classpath:application-test.yml" - }) -public class PolarisConfigRefreshOptimizationListenerTriggeredTest { - - private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher"; - - private static final String TEST_NAMESPACE = "testNamespace"; - - private static final String TEST_SERVICE_NAME = "testServiceName"; - - private static final String TEST_FILE_NAME = "application.properties"; - - @Autowired - private ConfigurableApplicationContext context; - - @BeforeEach - public void setUp() { - PolarisPropertySourceManager.clearPropertySources(); - } - - @Test - public void testSwitchConfigRefreshType() { - RefreshType actualRefreshType = context.getEnvironment() - .getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class); - // not change when using @RefreshScope - assertThat(actualRefreshType).isEqualTo(RefreshType.REFLECT); - PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class); - assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFLECT); - assertThat(context.containsBean(REFLECT_REFRESHER_BEAN_NAME)).isTrue(); - PolarisRefreshAffectedContextRefresher refresher = context - .getBean(REFLECT_REFRESHER_BEAN_NAME, PolarisRefreshAffectedContextRefresher.class); - assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue(); - } - - @Test - public void testConfigFileChanged() { - Map content = new HashMap<>(); - content.put("k1", "v1"); - content.put("k2", "v2"); - content.put("k3", "v3"); - MockedConfigKVFile file = new MockedConfigKVFile(content); - - PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, - file, content); - PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - - PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class); - PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher); - - refresher.setRegistered(false); - spyRefresher.onApplicationEvent(null); - - ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); - ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED); - ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED); - Map changeInfos = new HashMap<>(); - changeInfos.put("k1", changeInfo); - changeInfos.put("k2", changeInfo3); - changeInfos.put("k4", changeInfo2); - ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos); - file.fireChangeListener(event); - - ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class); - when(mockContextRefresher.refresh()).thenReturn(event.changedKeys()); - - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k1"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k2"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshSpringValue("k4"); - Mockito.verify(spyRefresher, Mockito.times(1)) - .refreshConfigurationProperties(event.changedKeys()); - } - - @SpringBootApplication - protected static class TestApplication { - - @Primary - @Bean - public ContextRefresher contextRefresher() { - return mock(ContextRefresher.class); - } - - @Component - @RefreshScope - protected static class TestBeanWithRefreshScope { - - } - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.yml b/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.yml index 0ee9909535..c90d1340b2 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.yml +++ b/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.yml @@ -5,3 +5,5 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + config: + connect-remote-server: false