add unit test for netty4 remoting (#1509)

This commit is contained in:
Huang YunKun 2018-04-05 11:51:44 +08:00 committed by Xin Wang
parent ab2af6956c
commit c623429e1a
9 changed files with 178 additions and 99 deletions

View File

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

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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() {

View File

@ -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)});

View File

@ -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 );
}
}

View File

@ -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();
}
}

View File

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

View File

@ -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();
}
}