You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/11/09 02:49:51 UTC
[dubbo] branch 3.0 updated: optimize of
ZookeeperDynamicConfigurationTest (#9215)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 3c62e94 optimize of ZookeeperDynamicConfigurationTest (#9215)
3c62e94 is described below
commit 3c62e94a32ef2e732b9d8a915b60a1c08ce7804f
Author: Wang Chengming <63...@qq.com>
AuthorDate: Tue Nov 9 10:49:39 2021 +0800
optimize of ZookeeperDynamicConfigurationTest (#9215)
---
.../ZookeeperDynamicConfigurationTest.java | 85 ++++++++++------------
1 file changed, 37 insertions(+), 48 deletions(-)
diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java
index ee0f753..52e6969 100644
--- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java
+++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java
@@ -34,6 +34,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
import java.util.HashMap;
import java.util.Map;
@@ -53,8 +54,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class ZookeeperDynamicConfigurationTest {
private static CuratorFramework client;
- private static URL configUrl;
- private static int zkServerPort = NetUtils.getAvailablePort();
+ private static final int zkServerPort = NetUtils.getAvailablePort();
private static TestingServer zkServer;
private static DynamicConfiguration configuration;
@@ -63,7 +63,7 @@ public class ZookeeperDynamicConfigurationTest {
zkServer = new TestingServer(zkServerPort, true);
client = CuratorFrameworkFactory.newClient("127.0.0.1:" + zkServerPort, 60 * 1000, 60 * 1000,
- new ExponentialBackoffRetry(1000, 3));
+ new ExponentialBackoffRetry(1000, 3));
client.start();
try {
@@ -76,11 +76,10 @@ public class ZookeeperDynamicConfigurationTest {
e.printStackTrace();
}
-
- configUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort);
+ URL configUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort);
configuration = ExtensionLoader.getExtensionLoader(DynamicConfigurationFactory.class).getExtension(configUrl.getProtocol())
- .getDynamicConfiguration(configUrl);
+ .getDynamicConfiguration(configUrl);
}
@AfterAll
@@ -95,18 +94,35 @@ public class ZookeeperDynamicConfigurationTest {
client.setData().forPath(path, data.getBytes());
}
+ private ConfigurationListener mockListener(CountDownLatch latch, String[] value, Map<String, Integer> countMap) {
+ ConfigurationListener listener = Mockito.mock(ConfigurationListener.class);
+ Mockito.doAnswer(invoke -> {
+ ConfigChangedEvent event = invoke.getArgument(0);
+ Integer count = countMap.computeIfAbsent(event.getKey(), k -> 0);
+ countMap.put(event.getKey(), ++count);
+ value[0] = event.getContent();
+ latch.countDown();
+ return null;
+ }).when(listener).process(Mockito.any());
+ return listener;
+ }
+
@Test
- public void testGetConfig() throws Exception {
+ public void testGetConfig() {
Assertions.assertEquals("The content from dubbo.properties", configuration.getConfig("dubbo.properties", "dubbo"));
}
@Test
public void testAddListener() throws Exception {
CountDownLatch latch = new CountDownLatch(4);
- TestListener listener1 = new TestListener(latch);
- TestListener listener2 = new TestListener(latch);
- TestListener listener3 = new TestListener(latch);
- TestListener listener4 = new TestListener(latch);
+
+ String[] value1 = new String[1], value2 = new String[1], value3 = new String[1], value4 = new String[1];
+ Map<String, Integer> countMap1 = new HashMap<>(), countMap2 = new HashMap<>(), countMap3 = new HashMap<>(), countMap4 = new HashMap<>();
+ ConfigurationListener listener1 = mockListener(latch, value1, countMap1);
+ ConfigurationListener listener2 = mockListener(latch, value2, countMap2);
+ ConfigurationListener listener3 = mockListener(latch, value3, countMap3);
+ ConfigurationListener listener4 = mockListener(latch, value4, countMap4);
+
configuration.addListener("service:version:group.configurators", listener1);
configuration.addListener("service:version:group.configurators", listener2);
configuration.addListener("appname.tag-router", listener3);
@@ -122,15 +138,16 @@ public class ZookeeperDynamicConfigurationTest {
Thread.sleep(5000);
latch.await();
- Assertions.assertEquals(2, listener1.getCount("service:version:group.configurators"));
- Assertions.assertEquals(2, listener2.getCount("service:version:group.configurators"));
- Assertions.assertEquals(2, listener3.getCount("appname.tag-router"));
- Assertions.assertEquals(2, listener4.getCount("appname.tag-router"));
-
- Assertions.assertEquals("new value1", listener1.getValue());
- Assertions.assertEquals("new value1", listener2.getValue());
- Assertions.assertEquals("new value2", listener3.getValue());
- Assertions.assertEquals("new value2", listener4.getValue());
+
+ Assertions.assertEquals(1, countMap1.get("service:version:group.configurators"));
+ Assertions.assertEquals(1, countMap2.get("service:version:group.configurators"));
+ Assertions.assertEquals(1, countMap3.get("appname.tag-router"));
+ Assertions.assertEquals(1, countMap4.get("appname.tag-router"));
+
+ Assertions.assertEquals("new value1", value1[0]);
+ Assertions.assertEquals("new value1", value2[0]);
+ Assertions.assertEquals("new value2", value3[0]);
+ Assertions.assertEquals("new value2", value4[0]);
}
@Test
@@ -174,32 +191,4 @@ public class ZookeeperDynamicConfigurationTest {
assertEquals(new TreeSet(asList(key, key2)), configKeys);
}
- private class TestListener implements ConfigurationListener {
- private CountDownLatch latch;
- private String value;
- private Map<String, Integer> countMap = new HashMap<>();
-
- public TestListener(CountDownLatch latch) {
- this.latch = latch;
- }
-
- @Override
- public void process(ConfigChangedEvent event) {
- System.out.println(this + ": " + event);
- Integer count = countMap.computeIfAbsent(event.getKey(), k -> new Integer(0));
- countMap.put(event.getKey(), ++count);
-
- value = event.getContent();
- latch.countDown();
- }
-
- public int getCount(String key) {
- return countMap.get(key);
- }
-
- public String getValue() {
- return value;
- }
- }
-
}