diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/src/test/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-apollo/src/test/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfigurationTest.java index b14f172bb91..11ae86a8de1 100644 --- a/dubbo-configcenter/dubbo-configcenter-apollo/src/test/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfigurationTest.java +++ b/dubbo-configcenter/dubbo-configcenter-apollo/src/test/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfigurationTest.java @@ -24,7 +24,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; import java.io.FileOutputStream; import java.io.IOException; @@ -41,6 +44,7 @@ * Notice: EmbeddedApollo(apollo mock server) only support < junit5, please not upgrade the junit version in this UT, * the junit version in this UT is junit4, and the dependency comes from apollo-mockserver. */ +@TestMethodOrder(OrderAnnotation.class) public class ApolloDynamicConfigurationTest { private static final String SESSION_TIMEOUT_KEY = "session"; private static final String DEFAULT_NAMESPACE = "dubbo"; @@ -84,6 +88,7 @@ public void setUp() { * Test get rule. */ @Test + @Order(1) public void testGetRule() { String mockKey = "mockKey1"; String mockValue = String.valueOf(new Random().nextInt()); @@ -101,6 +106,7 @@ public void testGetRule() { * @throws InterruptedException the interrupted exception */ @Test + @Order(2) public void testGetInternalProperty() throws InterruptedException { String mockKey = "mockKey2"; String mockValue = String.valueOf(new Random().nextInt()); @@ -123,6 +129,7 @@ public void testGetInternalProperty() throws InterruptedException { * @throws Exception the exception */ @Test + @Order(3) public void testAddListener() throws Exception { String mockKey = "mockKey3"; String mockValue = String.valueOf(new Random().nextInt()); @@ -145,6 +152,44 @@ public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEve assertEquals(ConfigChangeType.MODIFIED, result.getChangeType()); } + /** + * Test remove listener. + * + * @throws Exception the exception + */ + @Test + @Order(4) + public void testRemoveListener() throws Exception { + String mockKey = "mockKey4"; + String mockValue = String.valueOf(new Random().nextInt()); + + final SettableFuture future = SettableFuture.create(); + + apolloDynamicConfiguration = new ApolloDynamicConfiguration(url); + apolloDynamicConfiguration.addListener(mockKey, DEFAULT_NAMESPACE, new ConfigurationListener() { + @Override + public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent event) { + future.set(event); + } + }); + + putData(mockKey, mockValue); + future.get(3000, TimeUnit.MILLISECONDS); + + apolloDynamicConfiguration.removeListener(mockKey, DEFAULT_NAMESPACE, new ConfigurationListener() { + @Override + public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent event) { + future.set(event); + } + }); + + deleteData(mockKey); + org.apache.dubbo.common.config.configcenter.ConfigChangedEvent result = future.get(3000, TimeUnit.MILLISECONDS); + assertEquals(mockValue, result.getContent()); + assertEquals(mockKey, result.getKey()); + assertEquals(ConfigChangeType.MODIFIED, result.getChangeType()); + } + private static void putData(String namespace, String key, String value) { embeddedApollo.addOrModifyProperty(namespace, key, value); } @@ -153,6 +198,9 @@ private static void putData(String key, String value) { embeddedApollo.addOrModifyProperty(DEFAULT_NAMESPACE, key, value); } + private static void deleteData(String key) { + embeddedApollo.deleteProperty(DEFAULT_NAMESPACE, key); + } private static void putMockRuleData(String key, String value, String group) { String fileName = ApolloDynamicConfigurationTest.class.getResource("/").getPath() + "mockdata-" + group + ".properties"; putMockData(key, value, fileName);