You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/04/21 06:10:09 UTC

[GitHub] ralf0131 closed pull request #1584: add more unit test for registry modules

ralf0131 closed pull request #1584: add more unit test for registry modules
URL: https://github.com/apache/incubator-dubbo/pull/1584
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/dependencies-bom/pom.xml b/dependencies-bom/pom.xml
index c0c7f8aadc..bed70bd430 100644
--- a/dependencies-bom/pom.xml
+++ b/dependencies-bom/pom.xml
@@ -106,6 +106,7 @@
         <log4j_version>1.2.16</log4j_version>
         <logback_version>1.2.2</logback_version>
         <commons_lang3_version>3.4</commons_lang3_version>
+        <embedded_redis_version>0.6</embedded_redis_version>
     </properties>
 
     <dependencyManagement>
@@ -327,6 +328,12 @@
                 <version>${curator_version}</version>
                 <scope>test</scope>
             </dependency>
+            <dependency>
+                <groupId>com.github.kstyrc</groupId>
+                <artifactId>embedded-redis</artifactId>
+                <version>${embedded_redis_version}</version>
+                <scope>test</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryFactoryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryFactoryTest.java
new file mode 100644
index 0000000000..bc8833b9af
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryFactoryTest.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.dubbo.registry.multicast;
+
+import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.registry.Registry;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+public class MulticastRegistryFactoryTest {
+    @Test
+    public void shouldCreateRegistry() {
+        Registry registry = new MulticastRegistryFactory().createRegistry(URL.valueOf("multicast://239.255.255.255/"));
+        assertThat(registry, not(nullValue()));
+        assertThat(registry.isAvailable(), is(true));
+    }
+}
\ No newline at end of file
diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
index bfae2018dd..5ddffdd4a3 100644
--- a/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
+++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/com/alibaba/dubbo/registry/multicast/MulticastRegistryTest.java
@@ -19,10 +19,8 @@
 import com.alibaba.dubbo.common.URL;
 import com.alibaba.dubbo.common.utils.NetUtils;
 import com.alibaba.dubbo.registry.NotifyListener;
-
-import junit.framework.Assert;
+import org.junit.Assert;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 import java.net.MulticastSocket;
@@ -31,28 +29,20 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 
-import static org.junit.Assert.assertEquals;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
 
-/**
- * MulticastRegistryTest
- *
- */
 public class MulticastRegistryTest {
 
-    String service = "com.alibaba.dubbo.test.injvmServie";
-    URL registryUrl = URL.valueOf("multicast://239.255.255.255/");
-    URL serviceUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + service
+    private String service = "com.alibaba.dubbo.test.injvmServie";
+    private URL registryUrl = URL.valueOf("multicast://239.255.255.255/");
+    private URL serviceUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + service
             + "?methods=test1,test2");
-    URL consumerUrl = URL.valueOf("subscribe://" + NetUtils.getLocalHost() + "/" + service + "?arg1=1&arg2=2");
-    MulticastRegistry registry = new MulticastRegistry(registryUrl);
-
-    /**
-     * @throws java.lang.Exception
-     */
-    @BeforeClass
-    public static void setUpBeforeClass() throws Exception {
-    }
+    private URL adminUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/*");
+    private URL consumerUrl = URL.valueOf("subscribe://" + NetUtils.getLocalHost() + "/" + service + "?arg1=1&arg2=2");
+    private MulticastRegistry registry = new MulticastRegistry(registryUrl);
 
     /**
      * @throws java.lang.Exception
@@ -63,11 +53,24 @@ public void setUp() throws Exception {
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void testUrlerror() {
+    public void testUrlError() {
         URL errorUrl = URL.valueOf("multicast://mullticast/");
         new MulticastRegistry(errorUrl);
     }
 
+    @Test(expected = IllegalStateException.class)
+    public void testAnyHost() {
+        URL errorUrl = URL.valueOf("multicast://0.0.0.0/");
+        new MulticastRegistry(errorUrl);
+    }
+
+    @Test
+    public void testGetCustomPort() {
+        URL customPortUrl = URL.valueOf("multicast://239.255.255.255:4321/");
+        MulticastRegistry multicastRegistry = new MulticastRegistry(customPortUrl);
+        assertThat(multicastRegistry.getUrl().getPort(), is(4321));
+    }
+
     /**
      * Test method for {@link com.alibaba.dubbo.registry.multicast.MulticastRegistry#getRegistered()}.
      */
diff --git a/dubbo-registry/dubbo-registry-redis/pom.xml b/dubbo-registry/dubbo-registry-redis/pom.xml
index 0be39a3170..9257094ab9 100644
--- a/dubbo-registry/dubbo-registry-redis/pom.xml
+++ b/dubbo-registry/dubbo-registry-redis/pom.xml
@@ -39,5 +39,10 @@
             <groupId>redis.clients</groupId>
             <artifactId>jedis</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.github.kstyrc</groupId>
+            <artifactId>embedded-redis</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/dubbo-registry/dubbo-registry-redis/src/test/java/com/alibaba/dubbo/registry/redis/RedisRegistryTest.java b/dubbo-registry/dubbo-registry-redis/src/test/java/com/alibaba/dubbo/registry/redis/RedisRegistryTest.java
index 54dc3851ed..28c69180fe 100644
--- a/dubbo-registry/dubbo-registry-redis/src/test/java/com/alibaba/dubbo/registry/redis/RedisRegistryTest.java
+++ b/dubbo-registry/dubbo-registry-redis/src/test/java/com/alibaba/dubbo/registry/redis/RedisRegistryTest.java
@@ -17,86 +17,104 @@
 package com.alibaba.dubbo.registry.redis;
 
 import com.alibaba.dubbo.common.URL;
-
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.registry.NotifyListener;
+import com.alibaba.dubbo.registry.Registry;
+import org.junit.After;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
+import redis.embedded.RedisServer;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static com.alibaba.dubbo.common.Constants.BACKUP_KEY;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
 
-/**
- * RedisRegistryTest
- *
- */
 public class RedisRegistryTest {
 
-    String service = "com.alibaba.dubbo.test.injvmServie";
-    URL registryUrl = URL.valueOf("redis://239.255.255.255/");
-    URL registryUrlWithPasswd = URL.valueOf("zookeeper://239.255.255.255?password=123456");
-    URL serviceUrl = URL.valueOf("redis://redis/" + service
-            + "?notify=false&methods=test1,test2");
-    URL consumerUrl = URL.valueOf("redis://consumer/" + service + "?notify=false&methods=test1,test2");
-    // RedisRegistry registry    = new RedisRegistry(registryUrl);
-    // RedisRegistry registryWithPasswd  = new RedisRegistry(registryUrlWithPasswd);
-
-    /**
-     * @throws java.lang.Exception
-     */
-    @BeforeClass
-    public static void setUpBeforeClass() throws Exception {
-    }
+    private String service = "com.alibaba.dubbo.test.injvmServie";
+    private URL serviceUrl = URL.valueOf("redis://redis/" + service + "?notify=false&methods=test1,test2");
+    private RedisServer redisServer;
+    private RedisRegistry redisRegistry;
+    private URL registryUrl;
 
-    /**
-     * @throws java.lang.Exception
-     */
     @Before
     public void setUp() throws Exception {
-        //registry.register(serviceUrl);
+        int redisPort = NetUtils.getAvailablePort();
+        this.redisServer = new RedisServer(redisPort);
+        this.redisServer.start();
+        this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
+
+        redisRegistry = (RedisRegistry) new RedisRegistryFactory().createRegistry(registryUrl);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        this.redisServer.stop();
     }
 
-    /**
-     * Test method for {@link com.alibaba.dubbo.registry.redis.RedisRegistry#getRegistered()}.
-     */
     @Test
     public void testRegister() {
-        /*List<URL> registered = null;
-        // clear first
-        registered = registry.getRegistered(service);
+        Set<URL> registered = null;
 
         for (int i = 0; i < 2; i++) {
-            registry.register(service, serviceUrl);
-            registered = registry.getRegistered(service);
-            assertTrue(registered.contains(serviceUrl));
+            redisRegistry.register(serviceUrl);
+            registered = redisRegistry.getRegistered();
+            assertThat(registered.contains(serviceUrl), is(true));
         }
-        // confirm only 1 regist success;
-        registered = registry.getRegistered(service);
-        assertEquals(1, registered.size());*/
+
+        registered = redisRegistry.getRegistered();
+        assertThat(registered.size(), is(1));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testAnyHost() {
+        URL errorUrl = URL.valueOf("multicast://0.0.0.0/");
+        new RedisRegistryFactory().createRegistry(errorUrl);
     }
 
-    /**
-     * Test method for
-     * {@link com.alibaba.dubbo.registry.redis.RedisRegistry#subscribe(URL, com.alibaba.dubbo.registry.NotifyListener)}
-     * .
-     */
-    @Test
-    public void testSubscribe() {
-        /*final String subscribearg = "arg1=1&arg2=2";
-        // verify lisener.
-        final AtomicReference<Map<String, String>> args = new AtomicReference<Map<String, String>>();
-        registry.subscribe(service, new URL("dubbo", NetUtils.getLocalHost(), 0, StringUtils.parseQueryString(subscribearg)), new NotifyListener() {
 
+    @Test
+    public void testSubscribeAndUnsubscribe() {
+        NotifyListener listener = new NotifyListener() {
+            @Override
             public void notify(List<URL> urls) {
-                // FIXME assertEquals(RedisRegistry.this.service, service);
-                args.set(urls.get(0).getParameters());
+
             }
-        });
-        assertEquals(serviceUrl.toParameterString(), StringUtils.toQueryString(args.get()));
-        Map<String, String> arg = registry.getSubscribed(service);
-        assertEquals(subscribearg, StringUtils.toQueryString(arg));*/
+        };
+        redisRegistry.subscribe(serviceUrl, listener);
+
+        Map<URL, Set<NotifyListener>> subscribed = redisRegistry.getSubscribed();
+        assertThat(subscribed.size(), is(1));
+        assertThat(subscribed.get(serviceUrl).size(), is(1));
 
+        redisRegistry.unsubscribe(serviceUrl, listener);
+        subscribed = redisRegistry.getSubscribed();
+        assertThat(subscribed.get(serviceUrl).size(), is(0));
     }
 
     @Test
-    public void testRedisPasswd() {
-        // Assert.assertNotNull(registryWithPasswd);
+    public void testAvailable() {
+        redisRegistry.register(serviceUrl);
+        assertThat(redisRegistry.isAvailable(), is(true));
+
+        redisRegistry.destroy();
+        assertThat(redisRegistry.isAvailable(), is(false));
     }
 
+    @Test
+    public void testAvailableWithBackup() {
+        URL url = URL.valueOf("redis://redisOne:8880").addParameter(BACKUP_KEY, "redisTwo:8881");
+        Registry registry = new RedisRegistryFactory().createRegistry(url);
+
+        assertThat(registry.isAvailable(), is(false));
+
+        url = URL.valueOf(this.registryUrl.toFullString()).addParameter(BACKUP_KEY, "redisTwo:8881");
+        registry = new RedisRegistryFactory().createRegistry(url);
+
+        assertThat(registry.isAvailable(), is(true));
+    }
 }
\ No newline at end of file
diff --git a/dubbo-registry/dubbo-registry-zookeeper/pom.xml b/dubbo-registry/dubbo-registry-zookeeper/pom.xml
index 66ba656cf2..b1ba554622 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/pom.xml
+++ b/dubbo-registry/dubbo-registry-zookeeper/pom.xml
@@ -40,5 +40,10 @@
             <artifactId>dubbo-remoting-zookeeper</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-test</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
index 6b37e97965..7148e59705 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
@@ -17,45 +17,51 @@
 package com.alibaba.dubbo.registry.zookeeper;
 
 import com.alibaba.dubbo.common.URL;
-
-import junit.framework.Assert;
+import com.alibaba.dubbo.common.status.Status;
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.registry.NotifyListener;
+import com.alibaba.dubbo.registry.Registry;
+import com.alibaba.dubbo.registry.status.RegistryStatusChecker;
+import com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter;
+import org.apache.curator.test.TestingServer;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
-/**
- * ZookeeperRegistryTest
- *
- */
-public class ZookeeperRegistryTest {
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
 
-    String service = "com.alibaba.dubbo.test.injvmServie";
-    URL registryUrl = URL.valueOf("zookeeper://239.255.255.255/");
-    URL serviceUrl = URL.valueOf("zookeeper://zookeeper/" + service
-            + "?notify=false&methods=test1,test2");
-    URL consumerUrl = URL.valueOf("zookeeper://consumer/" + service + "?notify=false&methods=test1,test2");
-    // ZookeeperRegistry registry    = new ZookeeperRegistry(registryUrl);
-
-    /**
-     * @throws java.lang.Exception
-     */
-    @BeforeClass
-    public static void setUpBeforeClass() throws Exception {
-    }
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+public class ZookeeperRegistryTest {
+    private TestingServer zkServer;
+    private ZookeeperRegistry zookeeperRegistry;
+    private String service = "com.alibaba.dubbo.test.injvmServie";
+    private URL serviceUrl = URL.valueOf("zookeeper://zookeeper/" + service + "?notify=false&methods=test1,test2");
+    private URL anyUrl = URL.valueOf("zookeeper://zookeeper/*");
+    private URL registryUrl;
+    private ZookeeperRegistryFactory zookeeperRegistryFactory;
 
-    /**
-     * @throws java.lang.Exception
-     */
     @Before
     public void setUp() throws Exception {
-        //registry.register(service, serviceUrl);
+        int zkServerPort = NetUtils.getAvailablePort();
+        this.zkServer = new TestingServer(zkServerPort, true);
+        this.registryUrl = URL.valueOf("zookeeper://localhost:" + zkServerPort);
+
+        zookeeperRegistryFactory = new ZookeeperRegistryFactory();
+        zookeeperRegistryFactory.setZookeeperTransporter(new CuratorZookeeperTransporter());
+        this.zookeeperRegistry = (ZookeeperRegistry) zookeeperRegistryFactory.createRegistry(registryUrl);
     }
 
-    /*@Test(expected = IllegalStateException.class)
-    public void testUrlerror() {
-        URL errorUrl = URL.valueOf("zookeeper://zookeeper/");
-        new ZookeeperRegistry(errorUrl);
-    }*/
+    @After
+    public void tearDown() throws Exception {
+        zkServer.stop();
+    }
 
     @Test
     public void testDefaultPort() {
@@ -63,46 +69,89 @@ public void testDefaultPort() {
         Assert.assertEquals("10.20.153.10:2181", ZookeeperRegistry.appendDefaultPort("10.20.153.10"));
     }
 
-    /**
-     * Test method for {@link com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#getRegistered()}.
-     */
+    @Test(expected = IllegalStateException.class)
+    public void testAnyHost() {
+        URL errorUrl = URL.valueOf("multicast://0.0.0.0/");
+        new ZookeeperRegistryFactory().createRegistry(errorUrl);
+    }
+
     @Test
     public void testRegister() {
-        /*List<URL> registered = null;
-        // clear first
-        registered = registry.getRegistered(service);
+        Set<URL> registered;
 
         for (int i = 0; i < 2; i++) {
-            registry.register(service, serviceUrl);
-            registered = registry.getRegistered(service);
-            assertTrue(registered.contains(serviceUrl));
+            zookeeperRegistry.register(serviceUrl);
+            registered = zookeeperRegistry.getRegistered();
+            assertThat(registered.contains(serviceUrl), is(true));
         }
-        // confirm only 1 regist success;
-        registered = registry.getRegistered(service);
-        assertEquals(1, registered.size());*/
+
+        registered = zookeeperRegistry.getRegistered();
+        assertThat(registered.size(), is(1));
     }
 
-    /**
-     * Test method for
-     * {@link com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#subscribe(URL, com.alibaba.dubbo.registry.NotifyListener)}
-     * .
-     */
     @Test
     public void testSubscribe() {
-        /*final String subscribearg = "arg1=1&arg2=2";
-        // verify lisener.
-        final AtomicReference<Map<String, String>> args = new AtomicReference<Map<String, String>>();
-        registry.subscribe(service, new URL("dubbo", NetUtils.getLocalHost(), 0, StringUtils.parseQueryString(subscribearg)), new NotifyListener() {
+        NotifyListener listener = EasyMock.mock(NotifyListener.class);
+        zookeeperRegistry.subscribe(serviceUrl, listener);
+
+        Map<URL, Set<NotifyListener>> subscribed = zookeeperRegistry.getSubscribed();
+        assertThat(subscribed.size(), is(1));
+        assertThat(subscribed.get(serviceUrl).size(), is(1));
+
+        zookeeperRegistry.unsubscribe(serviceUrl, listener);
+        subscribed = zookeeperRegistry.getSubscribed();
+        assertThat(subscribed.size(), is(1));
+        assertThat(subscribed.get(serviceUrl).size(), is(0));
+    }
+
+    @Test
+    public void testAvailable() {
+        zookeeperRegistry.register(serviceUrl);
+        assertThat(zookeeperRegistry.isAvailable(), is(true));
 
+        zookeeperRegistry.destroy();
+        assertThat(zookeeperRegistry.isAvailable(), is(false));
+    }
+
+    @Test
+    public void testLookup() {
+        List<URL> lookup = zookeeperRegistry.lookup(serviceUrl);
+        assertThat(lookup.size(), is(0));
+
+        zookeeperRegistry.register(serviceUrl);
+        lookup = zookeeperRegistry.lookup(serviceUrl);
+        assertThat(lookup.size(), is(1));
+    }
+
+
+    @Test
+    public void testStatusChecker() {
+        RegistryStatusChecker registryStatusChecker = new RegistryStatusChecker();
+        Status status = registryStatusChecker.check();
+        assertThat(status.getLevel(), is(Status.Level.UNKNOWN));
+
+        Registry registry = zookeeperRegistryFactory.getRegistry(registryUrl);
+        assertThat(registry, not(nullValue()));
+
+        status = registryStatusChecker.check();
+        assertThat(status.getLevel(), is(Status.Level.ERROR));
+
+        registry.register(serviceUrl);
+        status = registryStatusChecker.check();
+        assertThat(status.getLevel(), is(Status.Level.OK));
+    }
+
+    @Test
+    public void testSubscribeAnyValue() throws InterruptedException {
+        final CountDownLatch latch = new CountDownLatch(1);
+        zookeeperRegistry.register(serviceUrl);
+        zookeeperRegistry.subscribe(anyUrl, new NotifyListener() {
+            @Override
             public void notify(List<URL> urls) {
-                // FIXME assertEquals(ZookeeperRegistry.this.service, service);
-                args.set(urls.get(0).getParameters());
+                latch.countDown();
             }
         });
-        assertEquals(serviceUrl.toParameterString(), StringUtils.toQueryString(args.get()));
-        Map<String, String> arg = registry.getSubscribed(service);
-        assertEquals(subscribearg, StringUtils.toQueryString(arg));*/
-
+        zookeeperRegistry.register(serviceUrl);
+        latch.await();
     }
-
 }
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services