Skip to content

Commit 900ab0b

Browse files
DeadLionlovepoem
authored andcommitted
[Dubbo-1693] Enhance the test coverage part-14 (#1859)
* add testcase * remove useless code
1 parent 61727e1 commit 900ab0b

File tree

8 files changed

+393
-0
lines changed

8 files changed

+393
-0
lines changed

dubbo-remoting/dubbo-remoting-netty4/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@
4444
<groupId>io.netty</groupId>
4545
<artifactId>netty-all</artifactId>
4646
</dependency>
47+
<dependency>
48+
<groupId>com.alibaba</groupId>
49+
<artifactId>dubbo-serialization-hessian2</artifactId>
50+
<version>${project.parent.version}</version>
51+
<scope>test</scope>
52+
</dependency>
4753
</dependencies>
4854
</project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.utils.DubboAppender;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
22+
import com.alibaba.dubbo.remoting.Channel;
23+
import com.alibaba.dubbo.remoting.Client;
24+
import com.alibaba.dubbo.remoting.RemotingException;
25+
import com.alibaba.dubbo.remoting.Server;
26+
import com.alibaba.dubbo.remoting.exchange.Exchangers;
27+
import com.alibaba.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
/**
33+
* Client reconnect test
34+
*/
35+
public class ClientReconnectTest {
36+
public static void main(String[] args) {
37+
System.out.println(3 % 1);
38+
}
39+
40+
@Before
41+
public void clear() {
42+
DubboAppender.clear();
43+
}
44+
45+
@Test
46+
public void testReconnect() throws RemotingException, InterruptedException {
47+
{
48+
int port = NetUtils.getAvailablePort();
49+
Client client = startClient(port, 200);
50+
Assert.assertEquals(false, client.isConnected());
51+
Server server = startServer(port);
52+
for (int i = 0; i < 100 && !client.isConnected(); i++) {
53+
Thread.sleep(10);
54+
}
55+
Assert.assertEquals(true, client.isConnected());
56+
client.close(2000);
57+
server.close(2000);
58+
}
59+
{
60+
int port = NetUtils.getAvailablePort();
61+
Client client = startClient(port, 20000);
62+
Assert.assertEquals(false, client.isConnected());
63+
Server server = startServer(port);
64+
for (int i = 0; i < 5; i++) {
65+
Thread.sleep(200);
66+
}
67+
Assert.assertEquals(false, client.isConnected());
68+
client.close(2000);
69+
server.close(2000);
70+
}
71+
}
72+
73+
74+
public Client startClient(int port, int reconnectPeriod) throws RemotingException {
75+
final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?client=netty4&check=false&" + Constants.RECONNECT_KEY + "=" + reconnectPeriod;
76+
return Exchangers.connect(url);
77+
}
78+
79+
public Server startServer(int port) throws RemotingException {
80+
final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?server=netty4";
81+
return Exchangers.bind(url, new HandlerAdapter());
82+
}
83+
84+
static class HandlerAdapter extends ExchangeHandlerAdapter {
85+
@Override
86+
public void connected(Channel channel) throws RemotingException {
87+
}
88+
89+
@Override
90+
public void disconnected(Channel channel) throws RemotingException {
91+
}
92+
93+
@Override
94+
public void caught(Channel channel, Throwable exception) throws RemotingException {
95+
}
96+
}
97+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.remoting.RemotingException;
20+
import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
21+
import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
22+
import com.alibaba.dubbo.remoting.exchange.ResponseFuture;
23+
import com.alibaba.dubbo.remoting.exchange.support.Replier;
24+
import junit.framework.TestCase;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
/**
29+
* ClientToServer
30+
*/
31+
public abstract class ClientToServerTest extends TestCase {
32+
33+
protected static final String LOCALHOST = "127.0.0.1";
34+
35+
protected ExchangeServer server;
36+
37+
protected ExchangeChannel client;
38+
39+
protected WorldHandler handler = new WorldHandler();
40+
41+
protected abstract ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException;
42+
43+
protected abstract ExchangeChannel newClient(int port) throws RemotingException;
44+
45+
@Override
46+
protected void setUp() throws Exception {
47+
super.setUp();
48+
int port = (int) (1000 * Math.random() + 10000);
49+
server = newServer(port, handler);
50+
client = newClient(port);
51+
}
52+
53+
@Override
54+
protected void tearDown() throws Exception {
55+
super.tearDown();
56+
try {
57+
if (server != null)
58+
server.close();
59+
} finally {
60+
if (client != null)
61+
client.close();
62+
}
63+
}
64+
65+
@Test
66+
public void testFuture() throws Exception {
67+
ResponseFuture future = client.request(new World("world"));
68+
Hello result = (Hello) future.get();
69+
Assert.assertEquals("hello,world", result.getName());
70+
}
71+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.extension.ExtensionLoader;
20+
import com.alibaba.dubbo.remoting.Transporter;
21+
import org.junit.Test;
22+
23+
import static org.junit.Assert.*;
24+
import static org.junit.matchers.JUnitMatchers.containsString;
25+
26+
public class ClientsTest {
27+
@Test
28+
public void testGetTransportEmpty() {
29+
try {
30+
ExtensionLoader.getExtensionLoader(Transporter.class).getExtension("");
31+
fail();
32+
} catch (IllegalArgumentException expected) {
33+
assertThat(expected.getMessage(), containsString("Extension name == null"));
34+
}
35+
}
36+
37+
@Test(expected = IllegalArgumentException.class)
38+
public void testGetTransportNull() {
39+
String name = null;
40+
ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name);
41+
}
42+
43+
@Test
44+
public void testGetTransport3() {
45+
String name = "netty4";
46+
assertEquals(NettyTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
47+
}
48+
49+
@Test(expected = IllegalStateException.class)
50+
public void testGetTransportWrong() {
51+
String name = "nety";
52+
assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 java.io.Serializable;
20+
21+
/**
22+
* Result
23+
*/
24+
public class Hello implements Serializable {
25+
26+
private static final long serialVersionUID = 753429849957096150L;
27+
28+
private String name;
29+
30+
public Hello() {
31+
}
32+
33+
public Hello(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.URL;
20+
import com.alibaba.dubbo.remoting.RemotingException;
21+
import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
22+
import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
23+
import com.alibaba.dubbo.remoting.exchange.Exchangers;
24+
import com.alibaba.dubbo.remoting.exchange.support.Replier;
25+
26+
/**
27+
* Netty4ClientToServerTest
28+
*/
29+
public class NettyClientToServerTest extends ClientToServerTest {
30+
31+
protected ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException {
32+
return Exchangers.bind(URL.valueOf("exchange://localhost:" + port + "?server=netty4"), receiver);
33+
}
34+
35+
protected ExchangeChannel newClient(int port) throws RemotingException {
36+
return Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty4"));
37+
}
38+
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 java.io.Serializable;
20+
21+
/**
22+
* Data
23+
*/
24+
public class World implements Serializable {
25+
26+
private static final long serialVersionUID = 8563900571013747774L;
27+
28+
private String name;
29+
30+
public World() {
31+
}
32+
33+
public World(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)