Skip to content

Commit 301bc34

Browse files
htynknchickenlj
authored andcommitted
Merge pull request #1868, add test for rpc modules.
fixes #1697
1 parent e506367 commit 301bc34

File tree

9 files changed

+446
-0
lines changed

9 files changed

+446
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.rpc.protocol.memcached;
18+
19+
public class MemcachedProtocolTest {
20+
21+
}

dubbo-rpc/dubbo-rpc-redis/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,16 @@
3939
<groupId>redis.clients</groupId>
4040
<artifactId>jedis</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.github.kstyrc</groupId>
44+
<artifactId>embedded-redis</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.alibaba</groupId>
49+
<artifactId>dubbo-serialization-jdk</artifactId>
50+
<version>${project.parent.version}</version>
51+
<scope>test</scope>
52+
</dependency>
4253
</dependencies>
4354
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.rpc.protocol.redis;
18+
19+
public interface IDemoService {
20+
void set(String key, String value);
21+
22+
String get(String key);
23+
24+
void delete(String key);
25+
26+
String unsupported(String wrong);
27+
28+
String set(String key, String value, String extraArg);
29+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.rpc.protocol.redis;
18+
19+
import com.alibaba.dubbo.common.URL;
20+
import com.alibaba.dubbo.common.extension.ExtensionLoader;
21+
import com.alibaba.dubbo.common.utils.NetUtils;
22+
import com.alibaba.dubbo.rpc.Invoker;
23+
import com.alibaba.dubbo.rpc.Protocol;
24+
import com.alibaba.dubbo.rpc.ProxyFactory;
25+
import com.alibaba.dubbo.rpc.RpcException;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import redis.embedded.RedisServer;
30+
31+
import static org.hamcrest.CoreMatchers.is;
32+
import static org.hamcrest.CoreMatchers.nullValue;
33+
import static org.junit.Assert.assertThat;
34+
35+
public class RedisProtocolTest {
36+
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
37+
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
38+
private RedisServer redisServer;
39+
private URL registryUrl;
40+
41+
@Before
42+
public void setUp() throws Exception {
43+
int redisPort = NetUtils.getAvailablePort();
44+
this.redisServer = new RedisServer(redisPort);
45+
this.redisServer.start();
46+
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
47+
}
48+
49+
@After
50+
public void tearDown() {
51+
this.redisServer.stop();
52+
}
53+
54+
@Test
55+
public void testReferClass() {
56+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl);
57+
58+
Class<IDemoService> serviceClass = refer.getInterface();
59+
assertThat(serviceClass.getName(), is("com.alibaba.dubbo.rpc.protocol.redis.IDemoService"));
60+
}
61+
62+
@Test
63+
public void testInvocation() {
64+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
65+
registryUrl
66+
.addParameter("max.idle", 10)
67+
.addParameter("max.active", 20));
68+
IDemoService demoService = this.proxy.getProxy(refer);
69+
70+
String value = demoService.get("key");
71+
assertThat(value, is(nullValue()));
72+
73+
demoService.set("key", "newValue");
74+
value = demoService.get("key");
75+
assertThat(value, is("newValue"));
76+
77+
demoService.delete("key");
78+
value = demoService.get("key");
79+
assertThat(value, is(nullValue()));
80+
81+
refer.destroy();
82+
}
83+
84+
@Test(expected = RpcException.class)
85+
public void testUnsupportedMethod() {
86+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl);
87+
IDemoService demoService = this.proxy.getProxy(refer);
88+
89+
demoService.unsupported(null);
90+
}
91+
92+
@Test(expected = RpcException.class)
93+
public void testWrongParameters() {
94+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl);
95+
IDemoService demoService = this.proxy.getProxy(refer);
96+
97+
demoService.set("key", "value", "wrongValue");
98+
}
99+
100+
@Test(expected = RpcException.class)
101+
public void testWrongRedis() {
102+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, URL.valueOf("redis://localhost:1"));
103+
IDemoService demoService = this.proxy.getProxy(refer);
104+
105+
demoService.get("key");
106+
}
107+
108+
@Test(expected = UnsupportedOperationException.class)
109+
public void testExport() {
110+
protocol.export(protocol.refer(IDemoService.class, registryUrl));
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java=com.alibaba.dubbo.common.serialize.java.JavaSerialization
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.rpc.protocol.rest;
18+
19+
20+
public class DemoService implements IDemoService {
21+
@Override
22+
public Integer hello(Integer a, Integer b) {
23+
return a + b;
24+
}
25+
26+
@Override
27+
public String error() {
28+
throw new RuntimeException();
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.rpc.protocol.rest;
18+
19+
import javax.ws.rs.GET;
20+
import javax.ws.rs.Path;
21+
import javax.ws.rs.QueryParam;
22+
23+
@Path("/demoService")
24+
public interface IDemoService {
25+
@GET
26+
@Path("/hello")
27+
Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b);
28+
29+
@GET
30+
@Path("/error")
31+
String error();
32+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.rpc.protocol.rest;
18+
19+
import com.alibaba.dubbo.common.Constants;
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.common.extension.ExtensionLoader;
22+
import com.alibaba.dubbo.common.utils.NetUtils;
23+
import com.alibaba.dubbo.rpc.Exporter;
24+
import com.alibaba.dubbo.rpc.Protocol;
25+
import com.alibaba.dubbo.rpc.ProxyFactory;
26+
import com.alibaba.dubbo.rpc.Result;
27+
import com.alibaba.dubbo.rpc.RpcContext;
28+
import com.alibaba.dubbo.rpc.RpcException;
29+
import com.alibaba.dubbo.rpc.RpcInvocation;
30+
import com.alibaba.dubbo.rpc.ServiceClassHolder;
31+
import org.hamcrest.CoreMatchers;
32+
import org.junit.After;
33+
import org.junit.Test;
34+
35+
import static org.hamcrest.CoreMatchers.is;
36+
import static org.junit.Assert.assertThat;
37+
38+
public class RestProtocolTest {
39+
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
40+
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
41+
private final int availablePort = NetUtils.getAvailablePort();
42+
private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest");
43+
44+
@After
45+
public void tearDown() {
46+
protocol.destroy();
47+
}
48+
49+
@Test
50+
public void testExport() {
51+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
52+
53+
54+
RpcContext.getContext().setAttachment("timeout", "200");
55+
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
56+
57+
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl));
58+
59+
Integer echoString = demoService.hello(1, 2);
60+
assertThat(echoString, is(3));
61+
62+
exporter.unexport();
63+
}
64+
65+
@Test
66+
public void testNettyServer() {
67+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
68+
69+
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
70+
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
71+
72+
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
73+
74+
Integer echoString = demoService.hello(10, 10);
75+
assertThat(echoString, is(20));
76+
77+
exporter.unexport();
78+
}
79+
80+
@Test(expected = RpcException.class)
81+
public void testServletWithoutWebConfig() {
82+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
83+
84+
URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");
85+
86+
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, servletUrl));
87+
}
88+
89+
@Test(expected = RpcException.class)
90+
public void testErrorHandler() {
91+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
92+
93+
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
94+
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
95+
96+
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
97+
98+
demoService.error();
99+
}
100+
101+
@Test
102+
public void testInvoke() {
103+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
104+
105+
106+
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
107+
108+
RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});
109+
110+
Result result = exporter.getInvoker().invoke(rpcInvocation);
111+
assertThat(result.getValue(), CoreMatchers.<Object>is(5));
112+
}
113+
114+
@Test
115+
public void testFilter() {
116+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
117+
118+
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
119+
.addParameter(Constants.EXTENSION_KEY, "com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter");
120+
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
121+
122+
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
123+
124+
Integer result = demoService.hello(1, 2);
125+
126+
assertThat(result, is(3));
127+
128+
exporter.unexport();
129+
}
130+
131+
@Test(expected = RuntimeException.class)
132+
public void testRegFail() {
133+
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
134+
135+
URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
136+
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
137+
}
138+
139+
@Test
140+
public void testDefaultPort() {
141+
assertThat(protocol.getDefaultPort(), is(80));
142+
}
143+
}

0 commit comments

Comments
 (0)