From c623429e1a99353cd1ea90f95677bc156c70896f Mon Sep 17 00:00:00 2001 From: Huang YunKun Date: Thu, 5 Apr 2018 11:51:44 +0800 Subject: [PATCH] add unit test for netty4 remoting (#1509) --- dependencies-bom/pom.xml | 7 ++ .../grizzly/GrizzlyTransporterTest.java | 40 +++++++++++ .../alibaba/dubbo/remoting/http/TestUtil.java | 13 ---- .../http/jetty/JettyHttpBinderTest.java | 20 +++++- .../http/tomcat/TomcatHttpBinderTest.java | 20 +++++- .../src/test/java/com/alibaba/AppTest.java | 54 -------------- .../netty4/NettyTransporterTest.java | 70 +++++++++++++++++++ .../dubbo-remoting-zookeeper/pom.xml | 5 ++ .../curator/CuratorZookeeperClientTest.java | 48 ++++++------- 9 files changed, 178 insertions(+), 99 deletions(-) create mode 100644 dubbo-remoting/dubbo-remoting-grizzly/src/main/test/com/alibaba/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java delete mode 100644 dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/TestUtil.java delete mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/AppTest.java create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyTransporterTest.java 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 @@ commons-lang3 ${commons_lang3_version} + + + org.apache.curator + curator-test + ${curator_version} + test + 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 @@ import static org.hamcrest.core.Is.is; 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 @@ import static org.hamcrest.core.Is.is; 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 @@ org.apache.curator curator-framework + + org.apache.curator + curator-test + test + \ 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; + + @Before + public void setUp() throws Exception { + zkServerPort = NetUtils.getAvailablePort(); + zkServer = new TestingServer(this.zkServerPort, true); + } @Test public void testCheckExists() { - CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService")); + 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.assertTrue(curatorClient.checkExists(path)); - Assert.assertFalse(curatorClient.checkExists(path + "/noneexits")); + Assert.assertThat(curatorClient.checkExists(path), is(true)); + Assert.assertThat(curatorClient.checkExists(path + "/noneexits"), is(false)); } - /** - * 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")); - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - curatorClient.create(path, 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(); } }