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