You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/08/15 02:24:14 UTC

[incubator-dubbo] branch master updated: [Dubbo-#2177] Improve and Add Unit Test for org.apache.dubbo.registry.support.AbstractRegistry #2177 Team2 (#2284)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 049591f  [Dubbo-#2177] Improve and Add Unit Test for org.apache.dubbo.registry.support.AbstractRegistry #2177 Team2 (#2284)
049591f is described below

commit 049591f82683229834ebdcd9540b1393157a74ea
Author: binss <i...@binss.me>
AuthorDate: Wed Aug 15 10:24:08 2018 +0800

    [Dubbo-#2177] Improve and Add Unit Test for org.apache.dubbo.registry.support.AbstractRegistry #2177 Team2 (#2284)
    
    * Fix format problem of AbstractRegistryTest
    
    * Improve existing tests and add multi thread tests
---
 .../registry/support/AbstractRegistryTest.java     | 291 +++++++++++++++++++--
 1 file changed, 264 insertions(+), 27 deletions(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java
index e3bdd8c..2167512 100644
--- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java
@@ -21,10 +21,12 @@ import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.registry.NotifyListener;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.After;
 import org.junit.Test;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 
 /**
  * AbstractRegistryTest
@@ -35,28 +37,41 @@ public class AbstractRegistryTest {
     private NotifyListener listener;
     private AbstractRegistry abstractRegistry;
     private boolean notifySuccess;
+    private int threadNumber;
 
     @Before
     public void init() {
         URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":2233");
         testUrl = URL.valueOf("http://1.2.3.4:9090/registry?check=false&file=N/A&interface=com.test");
+        threadNumber = 100;
 
-        //init the object
+        // init the object
         abstractRegistry = new AbstractRegistry(url) {
             @Override
             public boolean isAvailable() {
                 return false;
             }
         };
-        //init notify listener
+        // init notify listener
         listener = urls -> notifySuccess = true;
-        //notify flag
+        // notify flag
         notifySuccess = false;
     }
 
+    @After
+    public void after() {
+        abstractRegistry.destroy();
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#register(URL)}.
+     *
+     * @throws Exception
+     */
     @Test
-    public void registerTest() {
-        //check parameters
+    public void testRegister() {
+        // check parameters
         try {
             abstractRegistry.register(null);
             Assert.fail();
@@ -67,47 +82,142 @@ public class AbstractRegistryTest {
         int beginSize = abstractRegistry.getRegistered().size();
         abstractRegistry.register(testUrl);
         Assert.assertEquals(beginSize + 1, abstractRegistry.getRegistered().size());
-        //check register when the url is the same
+        // check register when the url is the same
         abstractRegistry.register(testUrl);
         Assert.assertEquals(beginSize + 1, abstractRegistry.getRegistered().size());
     }
 
+    /**
+     * Multi thread test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#register(URL)}.
+     *
+     * @throws Exception
+     */
     @Test
-    public void unregisterTest() {
-        //check parameters
+    public void testRegisterMultiThread() {
+        int threadBeginSize = abstractRegistry.getRegistered().size();
+        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
+        Thread[] threads = new Thread[threadNumber];
+        for (int i = 0; i < threadNumber; i++){
+            URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + i);
+            threads[i] = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    abstractRegistry.register(url);
+                    countDownLatch.countDown();
+                }
+            });
+        }
+        for (int i = 0; i < threadNumber; i++){
+            threads[i].run();
+        }
+
+        try {
+            // wait for all thread done
+            countDownLatch.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+
+        Assert.assertEquals(threadBeginSize + threadNumber, abstractRegistry.getRegistered().size());
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#unregister(URL)}.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testUnregister() {
+        // check parameters
         try {
             abstractRegistry.unregister(null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        // check if unregister url successfully
-        abstractRegistry.register(testUrl);
+
         int beginSize = abstractRegistry.getRegistered().size();
+        // test unregister before register
+        abstractRegistry.unregister(testUrl);
+        Assert.assertEquals(beginSize, abstractRegistry.getRegistered().size());
+
+        // test register then unregister
+        abstractRegistry.register(testUrl);
+        Assert.assertEquals(beginSize + 1, abstractRegistry.getRegistered().size());
         abstractRegistry.unregister(testUrl);
-        Assert.assertEquals(beginSize - 1, abstractRegistry.getRegistered().size());
-        // check if unregister a not exist url successfully
+        Assert.assertEquals(beginSize, abstractRegistry.getRegistered().size());
+        // test double unregister after register
         abstractRegistry.unregister(testUrl);
-        Assert.assertEquals(beginSize - 1, abstractRegistry.getRegistered().size());
+        Assert.assertEquals(beginSize, abstractRegistry.getRegistered().size());
     }
 
+    /**
+     * Multi thread test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#unregister(URL)}.
+     *
+     * @throws Exception
+     */
     @Test
-    public void subscribeTest() {
-        //check parameters
+    public void testUnregisterMultiThread() {
+        int threadBeginSize = abstractRegistry.getRegistered().size();
+        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
+        Thread[] threads = new Thread[threadNumber];
+
+        for(int i = 0; i < threadNumber; i++){
+            URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + i);
+            abstractRegistry.register(url);
+        }
+        Assert.assertEquals(threadBeginSize + threadNumber, abstractRegistry.getRegistered().size());
+
+        // unregister by multi thread
+        for (int i = 0; i < threadNumber; i++){
+            URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + i);
+            threads[i] = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    abstractRegistry.unregister(url);
+                    countDownLatch.countDown();
+                }
+            });
+        }
+        for(int i = 0; i < threadNumber; i++){
+            threads[i].run();
+        }
+
+        try {
+            // wait for all thread done
+            countDownLatch.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        Assert.assertEquals(threadBeginSize, abstractRegistry.getRegistered().size());
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#subscribe(URL, NotifyListener)}.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testSubscribe() {
+        // check parameters
         try {
             abstractRegistry.subscribe(null, listener);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        //check parameters
+        // check parameters
         try {
             abstractRegistry.subscribe(testUrl, null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        //check parameters
+        // check parameters
         try {
             abstractRegistry.subscribe(null, null);
             Assert.fail();
@@ -115,42 +225,139 @@ public class AbstractRegistryTest {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
         // check if subscribe successfully
+        Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl));
         abstractRegistry.subscribe(testUrl, listener);
         Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl));
         Assert.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener));
     }
 
+    /**
+     * Multi thread test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#subscribe(URL, NotifyListener)}.
+     *
+     * @throws Exception
+     */
     @Test
-    public void unsubscribeTest() {
-        //check parameters
+    public void testSubscribeMultiThread() {
+        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
+        Thread[] threads = new Thread[threadNumber];
+        NotifyListener[] listeners = new NotifyListener[threadNumber];
+        for (int i = 0; i<threadNumber; i++) {
+            listeners[i] = urls->notifySuccess = true;
+        }
+        for (int i = 0; i<threadNumber; i++) {
+            NotifyListener notifyListener = listeners[i];
+            threads[i] = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    abstractRegistry.subscribe(testUrl, notifyListener);
+                    countDownLatch.countDown();
+                }
+            });
+            threads[i].run();
+        }
+
+        try {
+            // wait for all thread done
+            countDownLatch.await();
+            for (int i = 0; i < threadNumber; i++) {
+                Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl));
+                Assert.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listeners[i]));
+            }
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#unsubscribe(URL, NotifyListener)}.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testUnsubscribe() {
+        // check parameters
         try {
             abstractRegistry.unsubscribe(null, listener);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        //check parameters
+        // check parameters
         try {
             abstractRegistry.unsubscribe(testUrl, null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        //check parameters
+        // check parameters
         try {
             abstractRegistry.unsubscribe(null, null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
+
+        Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl));
         // check if unsubscribe successfully
         abstractRegistry.subscribe(testUrl, listener);
         abstractRegistry.unsubscribe(testUrl, listener);
+        // Since we have subscribe testUrl, here should return a empty set instead of null
+        Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl));
         Assert.assertFalse(abstractRegistry.getSubscribed().get(testUrl).contains(listener));
     }
 
+    /**
+     * Multi thread test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#unsubscribe(URL, NotifyListener)}.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testUnsubscribeMultiThread() {
+        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
+        Thread[] threads = new Thread[threadNumber];
+        NotifyListener[] listeners = new NotifyListener[threadNumber];
+        for (int i = 0; i < threadNumber; i++) {
+            listeners[i] = urls->notifySuccess=true;
+            abstractRegistry.subscribe(testUrl, listeners[i]);
+        }
+        for (int i = 0; i < threadNumber; i++){
+            NotifyListener notifyListener = listeners[i];
+            threads[i] = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    abstractRegistry.unsubscribe(testUrl, notifyListener);
+                    countDownLatch.countDown();
+                }
+            });
+            threads[i].run();
+        }
+
+        try {
+            // wait for all thread done
+            countDownLatch.await();
+            for(int i = 0; i < threadNumber; i++) {
+                Assert.assertFalse(abstractRegistry.getSubscribed().get(testUrl).contains(listener));
+            }
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#recover()}.
+     */
     @Test
-    public void recoverTest() throws Exception {
+    public void testRecover() throws Exception {
+        // test recover nothing
+        abstractRegistry.recover();
+        Assert.assertFalse(abstractRegistry.getRegistered().contains(testUrl));
+        Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl));
+
+        // test recover
         abstractRegistry.register(testUrl);
         abstractRegistry.subscribe(testUrl, listener);
         abstractRegistry.recover();
@@ -160,10 +367,23 @@ public class AbstractRegistryTest {
         Assert.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener));
     }
 
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#notify(List)}.
+     */
     @Test
-    public void notifyTest() {
+    public void testNotify() {
         abstractRegistry.subscribe(testUrl, listener);
+        // check parameters
+        Assert.assertFalse(notifySuccess);
+        abstractRegistry.notify(null);
+        Assert.assertFalse(notifySuccess);
+
         List<URL> urls = new ArrayList<>();
+        Assert.assertFalse(notifySuccess);
+        abstractRegistry.notify(urls);
+        Assert.assertFalse(notifySuccess);
+
         urls.add(testUrl);
         // check if notify successfully
         Assert.assertFalse(notifySuccess);
@@ -171,22 +391,39 @@ public class AbstractRegistryTest {
         Assert.assertTrue(notifySuccess);
     }
 
+    /**
+     * Test method for
+     * {@link org.apache.dubbo.registry.support.AbstractRegistry#notify(URL, NotifyListener, List)}.
+     *
+     * @throws Exception
+     */
     @Test
-    public void notify2Test() {
-        //check parameters
+    public void testNotifyThreeArgs() {
+        // check parameters
         try {
             abstractRegistry.notify(null, null, null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
-        //check parameters
+        // check parameters
         try {
             abstractRegistry.notify(testUrl, null, null);
             Assert.fail();
         } catch (Exception e) {
             Assert.assertTrue(e instanceof IllegalArgumentException);
         }
+        // check parameters
+        try {
+            abstractRegistry.notify(null, listener, null);
+            Assert.fail();
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IllegalArgumentException);
+        }
+
+        Assert.assertFalse(notifySuccess);
+        abstractRegistry.notify(testUrl, listener, null);
+        Assert.assertFalse(notifySuccess);
 
         List<URL> urls = new ArrayList<>();
         urls.add(testUrl);
@@ -195,4 +432,4 @@ public class AbstractRegistryTest {
         abstractRegistry.notify(testUrl, listener, urls);
         Assert.assertTrue(notifySuccess);
     }
-}
\ No newline at end of file
+}