-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1868, add test for rpc modules.
fixes #1697
- Loading branch information
Showing
9 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...mcached/src/test/java/com/alibaba/dubbo/rpc/protocol/memcached/MemcachedProtocolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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.rpc.protocol.memcached; | ||
|
||
public class MemcachedProtocolTest { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...-rpc/dubbo-rpc-redis/src/test/java/com/alibaba/dubbo/rpc/protocol/redis/IDemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.rpc.protocol.redis; | ||
|
||
public interface IDemoService { | ||
void set(String key, String value); | ||
|
||
String get(String key); | ||
|
||
void delete(String key); | ||
|
||
String unsupported(String wrong); | ||
|
||
String set(String key, String value, String extraArg); | ||
} |
112 changes: 112 additions & 0 deletions
112
...dubbo-rpc-redis/src/test/java/com/alibaba/dubbo/rpc/protocol/redis/RedisProtocolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* 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.rpc.protocol.redis; | ||
|
||
import com.alibaba.dubbo.common.URL; | ||
import com.alibaba.dubbo.common.extension.ExtensionLoader; | ||
import com.alibaba.dubbo.common.utils.NetUtils; | ||
import com.alibaba.dubbo.rpc.Invoker; | ||
import com.alibaba.dubbo.rpc.Protocol; | ||
import com.alibaba.dubbo.rpc.ProxyFactory; | ||
import com.alibaba.dubbo.rpc.RpcException; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import redis.embedded.RedisServer; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class RedisProtocolTest { | ||
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); | ||
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); | ||
private RedisServer redisServer; | ||
private URL registryUrl; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
int redisPort = NetUtils.getAvailablePort(); | ||
this.redisServer = new RedisServer(redisPort); | ||
this.redisServer.start(); | ||
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
this.redisServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testReferClass() { | ||
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl); | ||
|
||
Class<IDemoService> serviceClass = refer.getInterface(); | ||
assertThat(serviceClass.getName(), is("com.alibaba.dubbo.rpc.protocol.redis.IDemoService")); | ||
} | ||
|
||
@Test | ||
public void testInvocation() { | ||
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, | ||
registryUrl | ||
.addParameter("max.idle", 10) | ||
.addParameter("max.active", 20)); | ||
IDemoService demoService = this.proxy.getProxy(refer); | ||
|
||
String value = demoService.get("key"); | ||
assertThat(value, is(nullValue())); | ||
|
||
demoService.set("key", "newValue"); | ||
value = demoService.get("key"); | ||
assertThat(value, is("newValue")); | ||
|
||
demoService.delete("key"); | ||
value = demoService.get("key"); | ||
assertThat(value, is(nullValue())); | ||
|
||
refer.destroy(); | ||
} | ||
|
||
@Test(expected = RpcException.class) | ||
public void testUnsupportedMethod() { | ||
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl); | ||
IDemoService demoService = this.proxy.getProxy(refer); | ||
|
||
demoService.unsupported(null); | ||
} | ||
|
||
@Test(expected = RpcException.class) | ||
public void testWrongParameters() { | ||
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, registryUrl); | ||
IDemoService demoService = this.proxy.getProxy(refer); | ||
|
||
demoService.set("key", "value", "wrongValue"); | ||
} | ||
|
||
@Test(expected = RpcException.class) | ||
public void testWrongRedis() { | ||
Invoker<IDemoService> refer = protocol.refer(IDemoService.class, URL.valueOf("redis://localhost:1")); | ||
IDemoService demoService = this.proxy.getProxy(refer); | ||
|
||
demoService.get("key"); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void testExport() { | ||
protocol.export(protocol.refer(IDemoService.class, registryUrl)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...c/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
java=com.alibaba.dubbo.common.serialize.java.JavaSerialization |
30 changes: 30 additions & 0 deletions
30
dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/DemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.rpc.protocol.rest; | ||
|
||
|
||
public class DemoService implements IDemoService { | ||
@Override | ||
public Integer hello(Integer a, Integer b) { | ||
return a + b; | ||
} | ||
|
||
@Override | ||
public String error() { | ||
throw new RuntimeException(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/IDemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.rpc.protocol.rest; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
|
||
@Path("/demoService") | ||
public interface IDemoService { | ||
@GET | ||
@Path("/hello") | ||
Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b); | ||
|
||
@GET | ||
@Path("/error") | ||
String error(); | ||
} |
143 changes: 143 additions & 0 deletions
143
...pc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* 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.rpc.protocol.rest; | ||
|
||
import com.alibaba.dubbo.common.Constants; | ||
import com.alibaba.dubbo.common.URL; | ||
import com.alibaba.dubbo.common.extension.ExtensionLoader; | ||
import com.alibaba.dubbo.common.utils.NetUtils; | ||
import com.alibaba.dubbo.rpc.Exporter; | ||
import com.alibaba.dubbo.rpc.Protocol; | ||
import com.alibaba.dubbo.rpc.ProxyFactory; | ||
import com.alibaba.dubbo.rpc.Result; | ||
import com.alibaba.dubbo.rpc.RpcContext; | ||
import com.alibaba.dubbo.rpc.RpcException; | ||
import com.alibaba.dubbo.rpc.RpcInvocation; | ||
import com.alibaba.dubbo.rpc.ServiceClassHolder; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class RestProtocolTest { | ||
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest"); | ||
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); | ||
private final int availablePort = NetUtils.getAvailablePort(); | ||
private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest"); | ||
|
||
@After | ||
public void tearDown() { | ||
protocol.destroy(); | ||
} | ||
|
||
@Test | ||
public void testExport() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
|
||
RpcContext.getContext().setAttachment("timeout", "200"); | ||
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl)); | ||
|
||
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl)); | ||
|
||
Integer echoString = demoService.hello(1, 2); | ||
assertThat(echoString, is(3)); | ||
|
||
exporter.unexport(); | ||
} | ||
|
||
@Test | ||
public void testNettyServer() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty"); | ||
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); | ||
|
||
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); | ||
|
||
Integer echoString = demoService.hello(10, 10); | ||
assertThat(echoString, is(20)); | ||
|
||
exporter.unexport(); | ||
} | ||
|
||
@Test(expected = RpcException.class) | ||
public void testServletWithoutWebConfig() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet"); | ||
|
||
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, servletUrl)); | ||
} | ||
|
||
@Test(expected = RpcException.class) | ||
public void testErrorHandler() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty"); | ||
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); | ||
|
||
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); | ||
|
||
demoService.error(); | ||
} | ||
|
||
@Test | ||
public void testInvoke() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
|
||
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl)); | ||
|
||
RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3}); | ||
|
||
Result result = exporter.getInvoker().invoke(rpcInvocation); | ||
assertThat(result.getValue(), CoreMatchers.<Object>is(5)); | ||
} | ||
|
||
@Test | ||
public void testFilter() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty") | ||
.addParameter(Constants.EXTENSION_KEY, "com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"); | ||
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); | ||
|
||
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); | ||
|
||
Integer result = demoService.hello(1, 2); | ||
|
||
assertThat(result, is(3)); | ||
|
||
exporter.unexport(); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void testRegFail() { | ||
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); | ||
|
||
URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter"); | ||
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); | ||
} | ||
|
||
@Test | ||
public void testDefaultPort() { | ||
assertThat(protocol.getDefaultPort(), is(80)); | ||
} | ||
} |
Oops, something went wrong.