dubbo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] lovepoem closed pull request #1509: add unit test for remoting module
Date Thu, 05 Apr 2018 03:51:45 GMT
lovepoem closed pull request #1509: add unit test for remoting module
URL: https://github.com/apache/incubator-dubbo/pull/1509
 
 
   

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 14cd30df04..c0c7f8aadc 100644
--- a/dependencies-bom/pom.xml
+++ b/dependencies-bom/pom.xml
@@ -320,6 +320,13 @@
                 <artifactId>commons-lang3</artifactId>
                 <version>${commons_lang3_version}</version>
             </dependency>
+            <!-- Test lib -->
+            <dependency>
+                <groupId>org.apache.curator</groupId>
+                <artifactId>curator-test</artifactId>
+                <version>${curator_version}</version>
+                <scope>test</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
diff --git a/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/com/alibaba/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java
b/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/com/alibaba/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java
new file mode 100644
index 0000000000..9bb0693a63
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/com/alibaba/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.remoting.transport.grizzly;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.remoting.Server;
+import com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class GrizzlyTransporterTest {
+    @Test
+    public void shouldAbleToBindGrizzly() throws Exception {
+        int port = NetUtils.getAvailablePort();
+        URL url = new URL("http", "localhost", port,
+                new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
+
+        Server server = new GrizzlyTransporter().bind(url, new ChannelHandlerAdapter());
+
+        assertThat(server.isBound(), is(true));
+    }
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/TestUtil.java
b/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/TestUtil.java
deleted file mode 100644
index 8f31020e46..0000000000
--- a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/TestUtil.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.alibaba.dubbo.remoting.http;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-
-public class TestUtil {
-    public static Integer getFreePort() throws IOException {
-        ServerSocket socket = new ServerSocket(0);
-        int port = socket.getLocalPort();
-        socket.close();
-        return port;
-    }
-}
diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpBinderTest.java
b/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpBinderTest.java
index 19b647a069..47135dd146 100644
--- a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpBinderTest.java
+++ b/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpBinderTest.java
@@ -1,10 +1,26 @@
+/*
+ * 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.remoting.http.jetty;
 
 import com.alibaba.dubbo.common.Constants;
 import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.common.utils.NetUtils;
 import com.alibaba.dubbo.remoting.http.HttpHandler;
 import com.alibaba.dubbo.remoting.http.HttpServer;
-import com.alibaba.dubbo.remoting.http.TestUtil;
 import org.apache.http.client.fluent.Request;
 import org.junit.Test;
 
@@ -18,7 +34,7 @@
 public class JettyHttpBinderTest {
     @Test
     public void shouldAbleHandleRequestForJettyBinder() throws Exception {
-        int port = TestUtil.getFreePort();
+        int port = NetUtils.getAvailablePort();
         URL url = new URL("http", "localhost", port,
                 new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
         HttpServer httpServer = new JettyHttpServer(url, new HttpHandler() {
diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
b/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
index 78f1c71fa3..37c4b72177 100644
--- a/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
+++ b/dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
@@ -1,10 +1,26 @@
+/*
+ * 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.remoting.http.tomcat;
 
 import com.alibaba.dubbo.common.Constants;
 import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.common.utils.NetUtils;
 import com.alibaba.dubbo.remoting.http.HttpHandler;
 import com.alibaba.dubbo.remoting.http.HttpServer;
-import com.alibaba.dubbo.remoting.http.TestUtil;
 import org.apache.http.client.fluent.Request;
 import org.junit.Test;
 
@@ -18,7 +34,7 @@
 public class TomcatHttpBinderTest {
     @Test
     public void shouldAbleHandleRequestForTomcatBinder() throws Exception {
-        int port = TestUtil.getFreePort();
+        int port = NetUtils.getAvailablePort();
         URL url = new URL("http", "localhost", port,
                 new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
 
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/AppTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/AppTest.java
deleted file mode 100644
index 436822407c..0000000000
--- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/AppTest.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest 
-    extends TestCase
-{
-    /**
-     * Create the test case
-     *
-     * @param testName name of the test case
-     */
-    public AppTest( String testName )
-    {
-        super( testName );
-    }
-
-    /**
-     * @return the suite of tests being tested
-     */
-    public static Test suite()
-    {
-        return new TestSuite( AppTest.class );
-    }
-
-    /**
-     * Rigourous Test :-)
-     */
-    public void testApp()
-    {
-        assertTrue( true );
-    }
-}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyTransporterTest.java
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyTransporterTest.java
new file mode 100644
index 0000000000..6e04212b93
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyTransporterTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.remoting.transport.netty4;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.remoting.Channel;
+import com.alibaba.dubbo.remoting.RemotingException;
+import com.alibaba.dubbo.remoting.Server;
+import com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class NettyTransporterTest {
+    @Test
+    public void shouldAbleToBindNetty4() throws Exception {
+        int port = NetUtils.getAvailablePort();
+        URL url = new URL("http", "localhost", port,
+                new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
+
+        Server server = new NettyTransporter().bind(url, new ChannelHandlerAdapter());
+
+        assertThat(server.isBound(), is(true));
+    }
+
+    @Test
+    public void shouldConnectToNetty4Server() throws Exception {
+        final CountDownLatch lock = new CountDownLatch(1);
+
+        int port = NetUtils.getAvailablePort();
+        URL url = new URL("http", "localhost", port,
+                new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
+
+        new NettyTransporter().bind(url, new ChannelHandlerAdapter() {
+
+            @Override
+            public void connected(Channel channel) throws RemotingException {
+                lock.countDown();
+            }
+        });
+        new NettyTransporter().connect(url, new ChannelHandlerAdapter() {
+            @Override
+            public void sent(Channel channel, Object message) throws RemotingException {
+                channel.send(message);
+                channel.close();
+            }
+        });
+
+        lock.await();
+    }
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml b/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml
index db4c91d02f..2ff33976d1 100644
--- a/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml
+++ b/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml
@@ -46,5 +46,10 @@
             <groupId>org.apache.curator</groupId>
             <artifactId>curator-framework</artifactId>
         </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-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
index d2c7b31bde..a3d8a169cc 100644
--- a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
+++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java
@@ -17,47 +17,39 @@
 package com.alibaba.dubbo.remoting.zookeeper.curator;
 
 import com.alibaba.dubbo.common.URL;
-
+import com.alibaba.dubbo.common.utils.NetUtils;
+import org.apache.curator.test.TestingServer;
+import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
-/**
- * @date 2017/10/16
- */
+import static org.hamcrest.core.Is.is;
+
 @Ignore
 public class CuratorZookeeperClientTest {
+    private TestingServer zkServer;
+    private int zkServerPort;
 
-    @Test
-    public void testCheckExists() {
-        CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
-        String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
-        curatorClient.create(path, false);
-        Assert.assertTrue(curatorClient.checkExists(path));
-        Assert.assertFalse(curatorClient.checkExists(path + "/noneexits"));
+    @Before
+    public void setUp() throws Exception {
+        zkServerPort = NetUtils.getAvailablePort();
+        zkServer = new TestingServer(this.zkServerPort, true);
     }
 
-    /**
-     * create checkExists performance test
-     */
     @Test
-    public void testCreate() {
-        CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
+    public void testCheckExists() {
+        CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:"
+ this.zkServerPort + "/com.alibaba.dubbo.registry.RegistryService"));
         String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
         curatorClient.create(path, false);
+        Assert.assertThat(curatorClient.checkExists(path), is(true));
+        Assert.assertThat(curatorClient.checkExists(path + "/noneexits"), is(false));
+    }
 
-        // Repeated execution of create 100 times
-        long startTime = System.nanoTime();
-        for (int i = 0; i < 100; i++) {
-            curatorClient.create(path, true);
-        }
-        System.out.println("create cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
 
-        //The time of the 100 judgment
-        startTime = System.nanoTime();
-        for (int i = 0; i < 100; i++) {
-            curatorClient.checkExists(path);
-        }
-        System.out.println("judge cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
+    @After
+    public void tearDown() throws Exception {
+        zkServer.stop();
     }
 }


 

----------------------------------------------------------------
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

Mime
View raw message