Skip to content

Commit

Permalink
Add dubbo-configcenter-apollo RemoveListener test (apache#7893)
Browse files Browse the repository at this point in the history
  • Loading branch information
luox2 authored May 28, 2021
1 parent 76c62ba commit 515699b
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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<org.apache.dubbo.common.config.configcenter.ConfigChangedEvent> 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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 515699b

Please sign in to comment.