You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by wa...@apache.org on 2018/05/31 02:23:17 UTC
[incubator-dubbo] branch master updated: [Dubbo-1693] Enhance the
test coverage part-14 (#1859)
This is an automated email from the ASF dual-hosted git repository.
wangxin 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 900ab0b [Dubbo-1693] Enhance the test coverage part-14 (#1859)
900ab0b is described below
commit 900ab0b28ccce6e2fed4c2f73679a936282a349f
Author: Jasper Zhong <zh...@qq.com>
AuthorDate: Thu May 31 10:23:11 2018 +0800
[Dubbo-1693] Enhance the test coverage part-14 (#1859)
* add testcase
* remove useless code
---
dubbo-remoting/dubbo-remoting-netty4/pom.xml | 6 ++
.../transport/netty4/ClientReconnectTest.java | 97 ++++++++++++++++++++++
.../transport/netty4/ClientToServerTest.java | 71 ++++++++++++++++
.../remoting/transport/netty4/ClientsTest.java | 54 ++++++++++++
.../dubbo/remoting/transport/netty4/Hello.java | 45 ++++++++++
.../transport/netty4/NettyClientToServerTest.java | 39 +++++++++
.../dubbo/remoting/transport/netty4/World.java | 45 ++++++++++
.../remoting/transport/netty4/WorldHandler.java | 36 ++++++++
8 files changed, 393 insertions(+)
diff --git a/dubbo-remoting/dubbo-remoting-netty4/pom.xml b/dubbo-remoting/dubbo-remoting-netty4/pom.xml
index ef74d0c..566db43 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/pom.xml
+++ b/dubbo-remoting/dubbo-remoting-netty4/pom.xml
@@ -44,5 +44,11 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.alibaba</groupId>
+ <artifactId>dubbo-serialization-hessian2</artifactId>
+ <version>${project.parent.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientReconnectTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientReconnectTest.java
new file mode 100644
index 0000000..59597bb
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientReconnectTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.utils.DubboAppender;
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.remoting.Channel;
+import com.alibaba.dubbo.remoting.Client;
+import com.alibaba.dubbo.remoting.RemotingException;
+import com.alibaba.dubbo.remoting.Server;
+import com.alibaba.dubbo.remoting.exchange.Exchangers;
+import com.alibaba.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Client reconnect test
+ */
+public class ClientReconnectTest {
+ public static void main(String[] args) {
+ System.out.println(3 % 1);
+ }
+
+ @Before
+ public void clear() {
+ DubboAppender.clear();
+ }
+
+ @Test
+ public void testReconnect() throws RemotingException, InterruptedException {
+ {
+ int port = NetUtils.getAvailablePort();
+ Client client = startClient(port, 200);
+ Assert.assertEquals(false, client.isConnected());
+ Server server = startServer(port);
+ for (int i = 0; i < 100 && !client.isConnected(); i++) {
+ Thread.sleep(10);
+ }
+ Assert.assertEquals(true, client.isConnected());
+ client.close(2000);
+ server.close(2000);
+ }
+ {
+ int port = NetUtils.getAvailablePort();
+ Client client = startClient(port, 20000);
+ Assert.assertEquals(false, client.isConnected());
+ Server server = startServer(port);
+ for (int i = 0; i < 5; i++) {
+ Thread.sleep(200);
+ }
+ Assert.assertEquals(false, client.isConnected());
+ client.close(2000);
+ server.close(2000);
+ }
+ }
+
+
+ public Client startClient(int port, int reconnectPeriod) throws RemotingException {
+ final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?client=netty4&check=false&" + Constants.RECONNECT_KEY + "=" + reconnectPeriod;
+ return Exchangers.connect(url);
+ }
+
+ public Server startServer(int port) throws RemotingException {
+ final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?server=netty4";
+ return Exchangers.bind(url, new HandlerAdapter());
+ }
+
+ static class HandlerAdapter extends ExchangeHandlerAdapter {
+ @Override
+ public void connected(Channel channel) throws RemotingException {
+ }
+
+ @Override
+ public void disconnected(Channel channel) throws RemotingException {
+ }
+
+ @Override
+ public void caught(Channel channel, Throwable exception) throws RemotingException {
+ }
+ }
+}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientToServerTest.java
new file mode 100644
index 0000000..d780e5b
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientToServerTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.remoting.RemotingException;
+import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
+import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
+import com.alibaba.dubbo.remoting.exchange.ResponseFuture;
+import com.alibaba.dubbo.remoting.exchange.support.Replier;
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * ClientToServer
+ */
+public abstract class ClientToServerTest extends TestCase {
+
+ protected static final String LOCALHOST = "127.0.0.1";
+
+ protected ExchangeServer server;
+
+ protected ExchangeChannel client;
+
+ protected WorldHandler handler = new WorldHandler();
+
+ protected abstract ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException;
+
+ protected abstract ExchangeChannel newClient(int port) throws RemotingException;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ int port = (int) (1000 * Math.random() + 10000);
+ server = newServer(port, handler);
+ client = newClient(port);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ try {
+ if (server != null)
+ server.close();
+ } finally {
+ if (client != null)
+ client.close();
+ }
+ }
+
+ @Test
+ public void testFuture() throws Exception {
+ ResponseFuture future = client.request(new World("world"));
+ Hello result = (Hello) future.get();
+ Assert.assertEquals("hello,world", result.getName());
+ }
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientsTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientsTest.java
new file mode 100644
index 0000000..c14bca2
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientsTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.extension.ExtensionLoader;
+import com.alibaba.dubbo.remoting.Transporter;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.containsString;
+
+public class ClientsTest {
+ @Test
+ public void testGetTransportEmpty() {
+ try {
+ ExtensionLoader.getExtensionLoader(Transporter.class).getExtension("");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertThat(expected.getMessage(), containsString("Extension name == null"));
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetTransportNull() {
+ String name = null;
+ ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name);
+ }
+
+ @Test
+ public void testGetTransport3() {
+ String name = "netty4";
+ assertEquals(NettyTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testGetTransportWrong() {
+ String name = "nety";
+ assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
+ }
+}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/Hello.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/Hello.java
new file mode 100644
index 0000000..fe2d310
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/Hello.java
@@ -0,0 +1,45 @@
+/*
+ * 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 java.io.Serializable;
+
+/**
+ * Result
+ */
+public class Hello implements Serializable {
+
+ private static final long serialVersionUID = 753429849957096150L;
+
+ private String name;
+
+ public Hello() {
+ }
+
+ public Hello(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyClientToServerTest.java
new file mode 100644
index 0000000..2c7ada2
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyClientToServerTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.URL;
+import com.alibaba.dubbo.remoting.RemotingException;
+import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
+import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
+import com.alibaba.dubbo.remoting.exchange.Exchangers;
+import com.alibaba.dubbo.remoting.exchange.support.Replier;
+
+/**
+ * Netty4ClientToServerTest
+ */
+public class NettyClientToServerTest extends ClientToServerTest {
+
+ protected ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException {
+ return Exchangers.bind(URL.valueOf("exchange://localhost:" + port + "?server=netty4"), receiver);
+ }
+
+ protected ExchangeChannel newClient(int port) throws RemotingException {
+ return Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty4"));
+ }
+
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/World.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/World.java
new file mode 100644
index 0000000..5895f9b
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/World.java
@@ -0,0 +1,45 @@
+/*
+ * 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 java.io.Serializable;
+
+/**
+ * Data
+ */
+public class World implements Serializable {
+
+ private static final long serialVersionUID = 8563900571013747774L;
+
+ private String name;
+
+ public World() {
+ }
+
+ public World(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/WorldHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/WorldHandler.java
new file mode 100644
index 0000000..c0b2fcd
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/WorldHandler.java
@@ -0,0 +1,36 @@
+/*
+ * 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.remoting.RemotingException;
+import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
+import com.alibaba.dubbo.remoting.exchange.support.Replier;
+
+/**
+ * DataHandler
+ */
+public class WorldHandler implements Replier<World> {
+
+ public Class<World> interest() {
+ return World.class;
+ }
+
+ public Object reply(ExchangeChannel channel, World msg) throws RemotingException {
+ return new Hello("hello," + msg.getName());
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
wangxin@apache.org.