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/05/23 06:39:39 UTC

[dubbo] branch master updated: Add tests for ZookeeperDynamicConfiguration (#7825)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new a9b31b1  Add tests for ZookeeperDynamicConfiguration (#7825)
a9b31b1 is described below

commit a9b31b15ef2c084c67a49017c891aec2e6d15dc7
Author: passer <wh...@gmail.com>
AuthorDate: Sun May 23 14:39:27 2021 +0800

    Add tests for ZookeeperDynamicConfiguration (#7825)
    
    * Add tests for ZookeeperDynamicConfiguration (#7823)
    
    * Optimize ZookeeperDynamicConfigurationTest (#7823)
---
 .../ZookeeperDynamicConfigurationTest.java         | 82 +++++++++++++++++-----
 1 file changed, 63 insertions(+), 19 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 ddac80a..a605055 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
@@ -28,9 +28,9 @@ import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.retry.ExponentialBackoffRetry;
 import org.apache.curator.test.TestingServer;
-import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.util.HashMap;
@@ -47,15 +47,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  * TODO refactor using mockito
  */
 public class ZookeeperDynamicConfigurationTest {
-    private static CuratorFramework client;
+    private CuratorFramework client;
 
-    private static URL configUrl;
-    private static int zkServerPort = NetUtils.getAvailablePort();
-    private static TestingServer zkServer;
-    private static DynamicConfiguration configuration;
+    private URL configUrl;
+    private int zkServerPort = NetUtils.getAvailablePort();
+    private TestingServer zkServer;
+    private DynamicConfiguration configuration;
 
-    @BeforeAll
-    public static void setUp() throws Exception {
+    @BeforeEach
+    public void setUp() throws Exception {
         zkServer = new TestingServer(zkServerPort, true);
 
         client = CuratorFrameworkFactory.newClient("127.0.0.1:" + zkServerPort, 60 * 1000, 60 * 1000,
@@ -64,9 +64,7 @@ public class ZookeeperDynamicConfigurationTest {
 
         try {
             setData("/dubbo/config/dubbo/dubbo.properties", "The content from dubbo.properties");
-            setData("/dubbo/config/dubbo/service:version:group.configurators", "The content from configurators");
             setData("/dubbo/config/appname", "The content from higher level node");
-            setData("/dubbo/config/dubbo/appname.tag-router", "The content from appname tagrouters");
             setData("/dubbo/config/dubbo/never.change.DemoService.configurators", "Never change value from configurators");
         } catch (Exception e) {
             e.printStackTrace();
@@ -74,16 +72,16 @@ public class ZookeeperDynamicConfigurationTest {
 
 
         configUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort);
-
         configuration = ExtensionLoader.getExtensionLoader(DynamicConfigurationFactory.class).getExtension(configUrl.getProtocol()).getDynamicConfiguration(configUrl);
     }
 
-    @AfterAll
-    public static void tearDown() throws Exception {
+    @AfterEach
+    public void tearDown() throws Exception {
+        configuration.close();
         zkServer.stop();
     }
 
-    private static void setData(String path, String data) throws Exception {
+    private void setData(String path, String data) throws Exception {
         if (client.checkExists().forPath(path) == null) {
             client.create().creatingParentsIfNeeded().forPath(path);
         }
@@ -117,10 +115,10 @@ 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(1, listener1.getCount("service:version:group.configurators"));
+        Assertions.assertEquals(1, listener2.getCount("service:version:group.configurators"));
+        Assertions.assertEquals(1, listener3.getCount("appname.tag-router"));
+        Assertions.assertEquals(1, listener4.getCount("appname.tag-router"));
 
         Assertions.assertEquals("new value1", listener1.getValue());
         Assertions.assertEquals("new value1", listener2.getValue());
@@ -129,6 +127,52 @@ public class ZookeeperDynamicConfigurationTest {
     }
 
     @Test
+    public void tesRemoveListener() throws Exception {
+        String key1 = "service:version:group.configurators.remove";
+        String path1 = "/dubbo/config/dubbo/" + key1;
+        String key2 = "appname.tag-router.remove";
+        String path2 = "/dubbo/config/dubbo/" + key2;
+
+        CountDownLatch latch = new CountDownLatch(2);
+        TestListener listener1 = new TestListener(latch);
+        TestListener listener2 = new TestListener(latch);
+        configuration.addListener(key1, listener1);
+        configuration.addListener(key2, listener2);
+
+        Thread.sleep(100);
+        setData(path1, "new value1");
+        Thread.sleep(100);
+        setData(path2, "new value2");
+        Thread.sleep(100);
+        latch.await();
+
+        Assertions.assertEquals(1, listener1.getCount(key1));
+        Assertions.assertEquals(1, listener2.getCount(key2));
+
+        configuration.removeListener(key1, listener1);
+        setData(path1, "new value11");
+        Thread.sleep(100);
+        Assertions.assertEquals(1, listener1.getCount(key1));
+
+        setData(path2, "new value22");
+        Thread.sleep(100);
+        Assertions.assertEquals(2, listener2.getCount(key2));
+    }
+
+    @Test
+    public void testRemoveConfig() {
+        String key = "remove-config";
+        String group = "test";
+        String content = "test remove config";
+        configuration.publishConfig(key, group, content);
+
+        assertEquals(content, configuration.getConfig(key, group));
+
+        configuration.removeConfig(key, group);
+        assertEquals(null, configuration.getConfig(key, group));
+    }
+
+    @Test
     public void testPublishConfig() {
         String key = "user-service";
         String group = "org.apache.dubbo.service.UserService";