Skip to content

Commit c623429

Browse files
htynknlovepoem
authored andcommitted
add unit test for netty4 remoting (#1509)
1 parent ab2af69 commit c623429

File tree

9 files changed

+178
-99
lines changed

9 files changed

+178
-99
lines changed

dependencies-bom/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@
320320
<artifactId>commons-lang3</artifactId>
321321
<version>${commons_lang3_version}</version>
322322
</dependency>
323+
<!-- Test lib -->
324+
<dependency>
325+
<groupId>org.apache.curator</groupId>
326+
<artifactId>curator-test</artifactId>
327+
<version>${curator_version}</version>
328+
<scope>test</scope>
329+
</dependency>
323330
</dependencies>
324331
</dependencyManagement>
325332

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.remoting.transport.grizzly;
18+
19+
import com.alibaba.dubbo.common.Constants;
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
22+
import com.alibaba.dubbo.remoting.Server;
23+
import com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter;
24+
import org.junit.Test;
25+
26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.junit.Assert.assertThat;
28+
29+
public class GrizzlyTransporterTest {
30+
@Test
31+
public void shouldAbleToBindGrizzly() throws Exception {
32+
int port = NetUtils.getAvailablePort();
33+
URL url = new URL("http", "localhost", port,
34+
new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
35+
36+
Server server = new GrizzlyTransporter().bind(url, new ChannelHandlerAdapter());
37+
38+
assertThat(server.isBound(), is(true));
39+
}
40+
}

dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/TestUtil.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpBinderTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
117
package com.alibaba.dubbo.remoting.http.jetty;
218

319
import com.alibaba.dubbo.common.Constants;
420
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
522
import com.alibaba.dubbo.remoting.http.HttpHandler;
623
import com.alibaba.dubbo.remoting.http.HttpServer;
7-
import com.alibaba.dubbo.remoting.http.TestUtil;
824
import org.apache.http.client.fluent.Request;
925
import org.junit.Test;
1026

@@ -18,7 +34,7 @@
1834
public class JettyHttpBinderTest {
1935
@Test
2036
public void shouldAbleHandleRequestForJettyBinder() throws Exception {
21-
int port = TestUtil.getFreePort();
37+
int port = NetUtils.getAvailablePort();
2238
URL url = new URL("http", "localhost", port,
2339
new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
2440
HttpServer httpServer = new JettyHttpServer(url, new HttpHandler() {

dubbo-remoting/dubbo-remoting-http/src/test/java/com/alibaba/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
117
package com.alibaba.dubbo.remoting.http.tomcat;
218

319
import com.alibaba.dubbo.common.Constants;
420
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
522
import com.alibaba.dubbo.remoting.http.HttpHandler;
623
import com.alibaba.dubbo.remoting.http.HttpServer;
7-
import com.alibaba.dubbo.remoting.http.TestUtil;
824
import org.apache.http.client.fluent.Request;
925
import org.junit.Test;
1026

@@ -18,7 +34,7 @@
1834
public class TomcatHttpBinderTest {
1935
@Test
2036
public void shouldAbleHandleRequestForTomcatBinder() throws Exception {
21-
int port = TestUtil.getFreePort();
37+
int port = NetUtils.getAvailablePort();
2238
URL url = new URL("http", "localhost", port,
2339
new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
2440

dubbo-remoting/dubbo-remoting-netty4/src/test/java/com/alibaba/AppTest.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.remoting.transport.netty4;
18+
19+
import com.alibaba.dubbo.common.Constants;
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
22+
import com.alibaba.dubbo.remoting.Channel;
23+
import com.alibaba.dubbo.remoting.RemotingException;
24+
import com.alibaba.dubbo.remoting.Server;
25+
import com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter;
26+
import org.junit.Test;
27+
28+
import java.util.concurrent.CountDownLatch;
29+
30+
import static org.hamcrest.CoreMatchers.is;
31+
import static org.junit.Assert.assertThat;
32+
33+
public class NettyTransporterTest {
34+
@Test
35+
public void shouldAbleToBindNetty4() throws Exception {
36+
int port = NetUtils.getAvailablePort();
37+
URL url = new URL("http", "localhost", port,
38+
new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
39+
40+
Server server = new NettyTransporter().bind(url, new ChannelHandlerAdapter());
41+
42+
assertThat(server.isBound(), is(true));
43+
}
44+
45+
@Test
46+
public void shouldConnectToNetty4Server() throws Exception {
47+
final CountDownLatch lock = new CountDownLatch(1);
48+
49+
int port = NetUtils.getAvailablePort();
50+
URL url = new URL("http", "localhost", port,
51+
new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
52+
53+
new NettyTransporter().bind(url, new ChannelHandlerAdapter() {
54+
55+
@Override
56+
public void connected(Channel channel) throws RemotingException {
57+
lock.countDown();
58+
}
59+
});
60+
new NettyTransporter().connect(url, new ChannelHandlerAdapter() {
61+
@Override
62+
public void sent(Channel channel, Object message) throws RemotingException {
63+
channel.send(message);
64+
channel.close();
65+
}
66+
});
67+
68+
lock.await();
69+
}
70+
}

dubbo-remoting/dubbo-remoting-zookeeper/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,10 @@
4646
<groupId>org.apache.curator</groupId>
4747
<artifactId>curator-framework</artifactId>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.apache.curator</groupId>
51+
<artifactId>curator-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
4954
</dependencies>
5055
</project>

dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,39 @@
1717
package com.alibaba.dubbo.remoting.zookeeper.curator;
1818

1919
import com.alibaba.dubbo.common.URL;
20-
20+
import com.alibaba.dubbo.common.utils.NetUtils;
21+
import org.apache.curator.test.TestingServer;
22+
import org.junit.After;
2123
import org.junit.Assert;
24+
import org.junit.Before;
2225
import org.junit.Ignore;
2326
import org.junit.Test;
2427

25-
/**
26-
* @date 2017/10/16
27-
*/
28+
import static org.hamcrest.core.Is.is;
29+
2830
@Ignore
2931
public class CuratorZookeeperClientTest {
32+
private TestingServer zkServer;
33+
private int zkServerPort;
3034

31-
@Test
32-
public void testCheckExists() {
33-
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
34-
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
35-
curatorClient.create(path, false);
36-
Assert.assertTrue(curatorClient.checkExists(path));
37-
Assert.assertFalse(curatorClient.checkExists(path + "/noneexits"));
35+
@Before
36+
public void setUp() throws Exception {
37+
zkServerPort = NetUtils.getAvailablePort();
38+
zkServer = new TestingServer(this.zkServerPort, true);
3839
}
3940

40-
/**
41-
* create checkExists performance test
42-
*/
4341
@Test
44-
public void testCreate() {
45-
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
42+
public void testCheckExists() {
43+
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + this.zkServerPort + "/com.alibaba.dubbo.registry.RegistryService"));
4644
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
4745
curatorClient.create(path, false);
46+
Assert.assertThat(curatorClient.checkExists(path), is(true));
47+
Assert.assertThat(curatorClient.checkExists(path + "/noneexits"), is(false));
48+
}
4849

49-
// Repeated execution of create 100 times
50-
long startTime = System.nanoTime();
51-
for (int i = 0; i < 100; i++) {
52-
curatorClient.create(path, true);
53-
}
54-
System.out.println("create cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
5550

56-
//The time of the 100 judgment
57-
startTime = System.nanoTime();
58-
for (int i = 0; i < 100; i++) {
59-
curatorClient.checkExists(path);
60-
}
61-
System.out.println("judge cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
51+
@After
52+
public void tearDown() throws Exception {
53+
zkServer.stop();
6254
}
6355
}

0 commit comments

Comments
 (0)