use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class CompressRpcCodecTest method testGzip.
// 测试gz压缩
@Test
public void testGzip() throws IOException {
DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
byte[] bytes = rpcCodec.encode(channel, request);
assertFalse(isGzip(bytes));
// 小于阈值
int bodyLength = ByteUtil.bytes2int(bytes, 12);
URL url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
Map<String, String> params = url.getParameters();
params.put(URLParamType.usegz.name(), "true");
params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength - 1));
Channel tempChannel = new MockChannel(url);
bytes = rpcCodec.encode(tempChannel, request);
assertTrue(isGzip(bytes));
// 等于、大于阈值.url内部对数字类型参数有缓存,且无法动态更新,需要重新生产url及channel
url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
params = url.getParameters();
params.put(URLParamType.usegz.name(), "true");
params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength));
tempChannel = new MockChannel(url);
bytes = rpcCodec.encode(tempChannel, request);
assertFalse(isGzip(bytes));
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class CompressRpcCodecTest method testSwitcher.
// 测试开关有效性
@Test
public void testSwitcher() throws IOException {
DefaultRequest request = getRequest("void", null);
byte[] bytes = rpcCodec.encode(channel, request);
assertTrue(isCompressVersion(bytes));
// 整体开关测试
MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, true);
bytes = rpcCodec.encode(channel, request);
assertTrue(isV1Version(bytes));
// 分组开关测试
MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, false);
MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.GROUP_CODEC_VERSION_SWITCHER + URLParamType.group.getValue(), true);
bytes = rpcCodec.encode(channel, request);
assertTrue(isV1Version(bytes));
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class CompressRpcCodecTest method getRequest.
protected DefaultRequest getRequest(String paramtersDesc, Object[] params) {
DefaultRequest request = new DefaultRequest();
request.setInterfaceName(basicInterface);
request.setMethodName(basicMethod);
request.setParamtersDesc(paramtersDesc);
if (params != null) {
request.setArguments(params);
}
Map<String, String> attachmentsMap = new HashMap<String, String>();
attachmentsMap.put(URLParamType.group.name(), URLParamType.group.getValue());
attachmentsMap.put(URLParamType.version.name(), URLParamType.version.getValue());
attachmentsMap.put(URLParamType.module.name(), URLParamType.module.getValue());
attachmentsMap.put(URLParamType.application.name(), URLParamType.application.getValue());
request.setAttachments(attachmentsMap);
addMethodSign(request, (CompressRpcCodec) rpcCodec);
return request;
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class C method testCall.
@Test
public void testCall() {
ProviderMessageRouter providerMessageRouter = new ProviderMessageRouter();
Provider<ProviderA> providerA = new DefaultProvider<ProviderA>(new A(), new URL("injvm", "localhost", 0, ProviderA.class.getName()), ProviderA.class);
Provider<ProviderB> providerB = new DefaultProvider<ProviderB>(new B(), new URL("injvm", "localhost", 0, ProviderB.class.getName()), ProviderB.class);
providerMessageRouter.addProvider(providerA);
providerMessageRouter.addProvider(providerB);
Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_ALL);
DefaultRequest requestA = new DefaultRequest();
requestA.setInterfaceName(com.weibo.api.motan.transport.ProviderA.class.getName());
requestA.setMethodName("providerA");
requestA.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
Response response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
Assert.assertEquals("A", response.getValue());
DefaultRequest requestB = new DefaultRequest();
requestB.setInterfaceName(com.weibo.api.motan.transport.ProviderB.class.getName());
requestB.setMethodName("providerB");
requestB.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestB);
Assert.assertEquals("B", response.getValue());
providerMessageRouter.removeProvider(providerA);
Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_B);
try {
Response result = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
result.getValue();
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class DefaultRpcCodecTest method getRequest.
// 获取基础request,不包括请求方法和参数描述,使用默认接口类和分组
protected DefaultRequest getRequest(String paramtersDesc, Object[] params) {
DefaultRequest request = new DefaultRequest();
request.setInterfaceName(basicInterface);
request.setMethodName(basicMethod);
request.setParamtersDesc(paramtersDesc);
if (params != null) {
request.setArguments(params);
}
return request;
}
Aggregations