diff --git a/dubbo-remoting/dubbo-remoting-netty4/pom.xml b/dubbo-remoting/dubbo-remoting-netty4/pom.xml index ef74d0ceb48..566db4377a4 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/pom.xml +++ b/dubbo-remoting/dubbo-remoting-netty4/pom.xml @@ -44,5 +44,11 @@ io.netty netty-all + + com.alibaba + dubbo-serialization-hessian2 + ${project.parent.version} + test + 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 00000000000..59597bb1620 --- /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 00000000000..d780e5bd324 --- /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 00000000000..c14bca24986 --- /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 00000000000..fe2d31026bc --- /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 00000000000..2c7ada22eae --- /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 00000000000..5895f9babbd --- /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 00000000000..c0b2fcdb0ec --- /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 { + + public Class 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