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

hyunkun 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 9275b9d  Change main method under dubbo-remoting-api to standard unit 
test (#5026)
9275b9d is described below

commit 9275b9d5154b75c3a52f7fbb14f9290ef24bde2f
Author: xiaomoran <54576528+xiaomo...@users.noreply.github.com>
AuthorDate: Tue Sep 10 10:46:29 2019 +0800

    Change main method under dubbo-remoting-api to standard unit test (#5026)
    
    * change main method under dubbo-remoting-api to standard test
    
    * add timeout
    
    * remove unnecessary loops
---
 .../transport/mina/ClientToServerTest.java         |   3 +-
 .../transport/netty/ClientToServerTest.java        |   3 +-
 .../transport/netty4/ClientReconnectTest.java      |   2 +-
 .../transport/netty4/ClientToServerTest.java       |   3 +-
 .../remoting/transport/netty4}/DemoService.java    |   2 +-
 .../transport/netty4}/DemoServiceImpl.java         |   2 +-
 .../remoting/transport/netty4}/MockResult.java     |   2 +-
 .../transport/netty4/ReplierDispatcherTest.java}   | 117 ++++++++++++---------
 .../remoting/transport/netty4}/RpcMessage.java     |   2 +-
 .../transport/netty4}/RpcMessageHandler.java       |   3 +-
 10 files changed, 81 insertions(+), 58 deletions(-)

diff --git 
a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java
 
b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java
index 6414434..25d8618 100644
--- 
a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java
+++ 
b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.remoting.transport.mina;
 
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeChannel;
 import org.apache.dubbo.remoting.exchange.ExchangeServer;
@@ -47,7 +48,7 @@ public abstract class ClientToServerTest {
 
     @BeforeEach
     protected void setUp() throws Exception {
-        int port = (int) (1000 * Math.random() + 10000);
+        int port = NetUtils.getAvailablePort();
         server = newServer(port, handler);
         client = newClient(port);
     }
diff --git 
a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java
 
b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java
index 267f569..d89227f 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.remoting.transport.netty;
 
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeChannel;
 import org.apache.dubbo.remoting.exchange.ExchangeServer;
@@ -46,7 +47,7 @@ public abstract class ClientToServerTest  {
 
     @BeforeEach
     protected void setUp() throws Exception {
-        int port = (int) (1000 * Math.random() + 10000);
+        int port = NetUtils.getAvailablePort();
         server = newServer(port, handler);
         client = newClient(port);
     }
diff --git 
a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java
index 9a2065a..844950c 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java
@@ -50,7 +50,7 @@ public class ClientReconnectTest {
             Assertions.assertFalse(client.isConnected());
             Server server = startServer(port);
             for (int i = 0; i < 100 && !client.isConnected(); i++) {
-                Thread.sleep(10);
+                Thread.sleep(20);
             }
             Assertions.assertTrue(client.isConnected());
             client.close(2000);
diff --git 
a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java
index 9b8db00..1015a53 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.remoting.transport.netty4;
 
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeChannel;
 import org.apache.dubbo.remoting.exchange.ExchangeServer;
@@ -47,7 +48,7 @@ public abstract class ClientToServerTest {
 
     @BeforeEach
     protected void setUp() throws Exception {
-        int port = (int) (1000 * Math.random() + 10000);
+        int port = NetUtils.getAvailablePort();
         server = newServer(port, handler);
         client = newClient(port);
     }
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoService.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoService.java
similarity index 92%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoService.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoService.java
index 9b9a650..2734ed8 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoService.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoService.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 /**
  * <code>TestService</code>
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoServiceImpl.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoServiceImpl.java
similarity index 95%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoServiceImpl.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoServiceImpl.java
index 9d55bef..02b67f9 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/DemoServiceImpl.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/DemoServiceImpl.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 /**
  * <code>TestServiceImpl</code>
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/MockResult.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/MockResult.java
similarity index 95%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/MockResult.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/MockResult.java
index c353621..db1a814 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/MockResult.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/MockResult.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 import java.io.Serializable;
 
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ReplierDispatcherTest.java
similarity index 53%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ReplierDispatcherTest.java
index 4478f86..3ed2aca 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ReplierDispatcherTest.java
@@ -14,93 +14,112 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeChannel;
+import org.apache.dubbo.remoting.exchange.ExchangeServer;
 import org.apache.dubbo.remoting.exchange.Exchangers;
-import org.apache.dubbo.remoting.exchange.support.Replier;
 import org.apache.dubbo.remoting.exchange.support.ReplierDispatcher;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 import java.io.Serializable;
 import java.util.Random;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Executors;
+import java.util.concurrent.*;
+
+import static org.junit.jupiter.api.Assertions.fail;
+
 
 /**
- * Main
+ * ReplierDispatcherTest
  */
 
-public class Main {
-    public static void main(String[] args) throws Exception {
-        startServer(9010);
-        mutliThreadTest(10, 9010);
-        dataPackageTest(9010);
-    }
+public class ReplierDispatcherTest {
+
+    private ExchangeServer exchangeServer;
+
+    private ConcurrentHashMap<String, ExchangeChannel> clients = new 
ConcurrentHashMap<>();
 
-    private static void startServer(int port) throws Exception {
+    private int port;
+
+    @BeforeEach
+    public void startServer() throws RemotingException {
+        port = NetUtils.getAvailablePort();
         ReplierDispatcher dispatcher = new ReplierDispatcher();
         dispatcher.addReplier(RpcMessage.class, new RpcMessageHandler());
-        dispatcher.addReplier(Object.class, new Replier<Object>() {
-            public Object reply(ExchangeChannel channel, Object msg) {
-                for (int i = 0; i < 10000; i++)
-                    System.currentTimeMillis();
-                System.out.println("handle:" + msg + ";thread:" + 
Thread.currentThread().getName());
-                return new StringMessage("hello world");
-            }
-        });
-        Exchangers.bind(URL.valueOf("dubbo://localhost:" + port), dispatcher);
+        dispatcher.addReplier(Data.class, (channel, msg) -> new 
StringMessage("hello world"));
+        exchangeServer = Exchangers.bind(URL.valueOf("dubbo://localhost:" + 
port), dispatcher);
     }
 
-    static void dataPackageTest(int port) throws Exception {
+
+    @Test
+    public void testDataPackage() throws Exception {
         ExchangeChannel client = 
Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
         Random random = new Random();
         for (int i = 5; i < 100; i++) {
             StringBuilder sb = new StringBuilder();
             for (int j = 0; j < i * 100; j++)
-                sb.append("(" + random.nextLong() + ")");
-            Main.Data d = new Main.Data();
+                sb.append("(").append(random.nextLong()).append(")");
+            Data d = new Data();
             d.setData(sb.toString());
-            client.request(d).get();
+            Assertions.assertEquals(client.request(d).get().toString(), "hello 
world");
         }
-        System.out.println("send finished.");
+        clients.put(Thread.currentThread().getName(), client);
     }
 
-    static void mutliThreadTest(int tc, final int port) throws Exception {
-        Executor exec = Executors.newFixedThreadPool(tc);
+
+    @Test
+    public void testMultiThread() throws Exception {
+        int tc = 10;
+        ExecutorService exec = Executors.newFixedThreadPool(tc);
         for (int i = 0; i < tc; i++)
-            exec.execute(new Runnable() {
-                public void run() {
-                    try {
-                        test(port);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
+            exec.execute(() -> {
+                try {
+                    clientExchangeInfo(port);
+                } catch (Exception e) {
+                    fail();
                 }
             });
+        exec.shutdown();
+        exec.awaitTermination(10, TimeUnit.SECONDS);
     }
 
-    private static void test(int port) throws Exception {
-        ExchangeChannel client = 
Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
+    void clientExchangeInfo(int port) throws Exception {
+        ExchangeChannel client = 
Exchangers.connect(URL.valueOf("dubbo://localhost:" + port + "?" + 
CommonConstants.TIMEOUT_KEY + "=5000"));
+        clients.put(Thread.currentThread().getName(), client);
         MockResult result = (MockResult) client.request(new 
RpcMessage(DemoService.class.getName(), "plus", new Class<?>[]{int.class, 
int.class}, new Object[]{55, 25})).get();
-        System.out.println("55+25=" + result.getResult());
-
-        for (int i = 0; i < 100; i++)
+        Assertions.assertEquals(result.getResult(), 80);
+        for (int i = 0; i < 100; i++) {
             client.request(new RpcMessage(DemoService.class.getName(), 
"sayHello", new Class<?>[]{String.class}, new Object[]{"qianlei" + i}));
+        }
+        for (int i = 0; i < 100; i++) {
+            CompletableFuture<Object> future = client.request(new Data());
+            Assertions.assertEquals(future.get().toString(), "hello world");
+        }
+    }
 
-        for (int i = 0; i < 100; i++)
-            client.request(new Main.Data());
 
-        System.out.println("=====test invoke=====");
-        for (int i = 0; i < 100; i++) {
-            CompletableFuture<Object> future = client.request(new Main.Data());
-            System.out.println("invoke and get");
-            System.out.println("invoke result:" + future.get());
+    @AfterEach
+    public void tearDown() {
+        try {
+            if (exchangeServer != null)
+                exchangeServer.close();
+        } finally {
+            if (clients.size() != 0)
+                clients.forEach((key, value) -> {
+                    value.close();
+                    clients.remove(key);
+                });
         }
-        System.out.println("=====the end=====");
     }
 
+
     static class Data implements Serializable {
         private static final long serialVersionUID = -4666580993978548778L;
 
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessage.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessage.java
similarity index 93%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessage.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessage.java
index 6cb642e..4f5f413 100755
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessage.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessage.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 import java.io.Serializable;
 
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessageHandler.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java
similarity index 93%
rename from 
dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessageHandler.java
rename to 
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java
index 5211558..120d454 100755
--- 
a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/RpcMessageHandler.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java
@@ -14,10 +14,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.remoting;
+package org.apache.dubbo.remoting.transport.netty4;
 
 import org.apache.dubbo.common.bytecode.NoSuchMethodException;
 import org.apache.dubbo.common.bytecode.Wrapper;
+import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeChannel;
 import org.apache.dubbo.remoting.exchange.support.Replier;
 

Reply via email to