fix: fix ConfigChangeListener and unit test

pull/1657/head
Fishtail 2 months ago committed by fishtailfu
parent 5506b769bf
commit e9495c6afe

@ -3,3 +3,4 @@
- [fix:fix PolarisContextProperties instantiated twice causing NPE.](https://github.com/Tencent/spring-cloud-tencent/pull/1638) - [fix:fix PolarisContextProperties instantiated twice causing NPE.](https://github.com/Tencent/spring-cloud-tencent/pull/1638)
- [feat: support tsf 2024.](https://github.com/Tencent/spring-cloud-tencent/pull/1635) - [feat: support tsf 2024.](https://github.com/Tencent/spring-cloud-tencent/pull/1635)
- [fix: fix ConfigChangeListener and unit test](https://github.com/Tencent/spring-cloud-tencent/pull/1657)

@ -18,6 +18,6 @@ package com.tencent.cloud.polaris.config.listener;
public interface SyncConfigChangeListener extends ConfigChangeListener { public interface SyncConfigChangeListener extends ConfigChangeListener {
default boolean isAsync() { default boolean isAsync() {
return true; return false;
} }
} }

@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener; import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
@ -52,7 +53,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = ConfigChangeListenerTest.TestApplication.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" "spring.cloud.polaris.config.connect-remote-server=true", "spring.cloud.polaris.config.check-address=false", "spring.cloud.polaris.config.internal-enabled=false"
}) })
public class ConfigChangeListenerTest { public class ConfigChangeListenerTest {
@ -81,21 +82,20 @@ public class ConfigChangeListenerTest {
public void test() throws InterruptedException { public void test() throws InterruptedException {
//before change //before change
Assertions.assertThat(testConfig.getTimeout()).isEqualTo(1000); Assertions.assertThat(testConfig.getTimeout()).isEqualTo(1000);
//submit change event
System.setProperty("timeout", "2000");
Set<String> ketSet = new HashSet<>(); Set<String> ketSet = new HashSet<>();
ketSet.add("timeout"); ketSet.add("timeout");
EnvironmentChangeEvent event = new EnvironmentChangeEvent(applicationContext, ketSet); for (int i = 2; i <= 1000; i++) {
//submit change event
applicationEventPublisher.publishEvent(event); System.setProperty("timeout", String.valueOf(i * 1000));
EnvironmentChangeEvent event = new EnvironmentChangeEvent(applicationContext, ketSet);
//after change applicationEventPublisher.publishEvent(event);
boolean ret = hits.await(2, TimeUnit.SECONDS); //after change
Assertions.assertThat(ret).isEqualTo(true); boolean ret = hits.await(2, TimeUnit.SECONDS);
Assertions.assertThat(ret).isEqualTo(true);
Assertions.assertThat(testConfig.getChangeCnt()).isEqualTo(2); Assertions.assertThat(testConfig.getChangeCnt()).isEqualTo(2 * i - 2);
Assertions.assertThat(testConfig.getTimeout()).isEqualTo(2000); Assertions.assertThat(testConfig.getSyncChangeCnt()).isEqualTo(2 * i - 2);
Assertions.assertThat(testConfig.getTimeout()).isEqualTo(i * 1000);
}
} }
@SpringBootApplication @SpringBootApplication
@ -107,7 +107,9 @@ public class ConfigChangeListenerTest {
@Value("${timeout:1000}") @Value("${timeout:1000}")
private int timeout; private int timeout;
private int changeCnt; private AtomicInteger changeCnt = new AtomicInteger(0);
private int syncChangeCnt;
public int getTimeout() { public int getTimeout() {
return timeout; return timeout;
@ -118,14 +120,18 @@ public class ConfigChangeListenerTest {
} }
public int getChangeCnt() { public int getChangeCnt() {
return changeCnt; return changeCnt.get();
}
public int getSyncChangeCnt() {
return syncChangeCnt;
} }
@PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"}) @PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"})
public void configChangedListener(ConfigChangeEvent event) { public void configChangedListener(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout"); ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt(changeInfo.getNewValue().toString()); timeout = Integer.parseInt(changeInfo.getNewValue().toString());
changeCnt++; changeCnt.incrementAndGet();
hits.countDown(); hits.countDown();
} }
@ -133,9 +139,23 @@ public class ConfigChangeListenerTest {
public void configChangedListener2(ConfigChangeEvent event) { public void configChangedListener2(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout"); ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt(changeInfo.getNewValue().toString()); timeout = Integer.parseInt(changeInfo.getNewValue().toString());
changeCnt++; changeCnt.incrementAndGet();
hits.countDown(); hits.countDown();
} }
@PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"}, async = false)
public void syncConfigChangedListener(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt(changeInfo.getNewValue().toString());
syncChangeCnt++;
}
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = {"timeout"}, async = false)
public void syncConfigChangedListener2(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt(changeInfo.getNewValue().toString());
syncChangeCnt++;
}
} }
@Component @Component

Loading…
Cancel
Save