use of com.weibo.api.motan.transport.Channel 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.transport.Channel in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testHandle.
@Test
public void testHandle() throws Exception {
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
AttachmentRequest request = (AttachmentRequest) message;
verifyAttachments(request.getAttachments());
return yarResponse;
}
});
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertNotNull(httpResponse.content());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(yarResponse, retYarResponse);
}
use of com.weibo.api.motan.transport.Channel in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testAbnormal.
@Test
public void testAbnormal() throws Exception {
final String errmsg = "rpc process error";
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
throw new RuntimeException(errmsg);
}
});
// yar协议无法解析
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertNotNull(retYarResponse.getError());
// yar协议可以正常解析,但后续处理异常
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(errmsg, retYarResponse.getError());
}
use of com.weibo.api.motan.transport.Channel in project motan by weibocom.
the class NettyClient method request.
/**
* 请求remote service
*
* <pre>
* 1) get connection from pool
* 2) async requset
* 3) return connection to pool
* 4) check if async return response, true: return ResponseFuture; false: return result
* </pre>
*
* @param request
* @param async
* @return
* @throws TransportException
*/
private Response request(Request request, boolean async) throws TransportException {
Channel channel = null;
Response response = null;
try {
// return channel or throw exception(timeout or connection_fail)
channel = borrowObject();
if (channel == null) {
LoggerUtil.error("NettyClient borrowObject null: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request));
return null;
}
// async request
response = channel.request(request);
// return channel to pool
returnObject(channel);
} catch (Exception e) {
LoggerUtil.error("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
//TODO 对特定的异常回收channel
invalidateObject(channel);
if (e instanceof MotanAbstractException) {
throw (MotanAbstractException) e;
} else {
throw new MotanServiceException("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
}
}
// aysnc or sync result
response = asyncResponse(response, async);
return response;
}
use of com.weibo.api.motan.transport.Channel in project motan by weibocom.
the class UnSerializableClass method testNettyRequestDecodeException.
/**
* @throws Exception
*/
@Test
public void testNettyRequestDecodeException() throws Exception {
NettyServer nettyServer;
nettyServer = new NettyServer(url, new MessageHandler() {
@Override
public Object handle(Channel channel, Object message) {
Request request = (Request) message;
DefaultResponse response = new DefaultResponse();
response.setRequestId(request.getRequestId());
response.setValue("success");
return response;
}
});
nettyServer.open();
NettyClient nettyClient = new NettyClient(url);
nettyClient.open();
DefaultRequest request = new DefaultRequest();
request.setRequestId(RequestIdGenerator.getRequestId());
request.setInterfaceName(url.getPath());
request.setMethodName("hello");
request.setParamtersDesc("void");
try {
nettyClient.request(request);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("framework decode error"));
} finally {
nettyClient.close();
nettyServer.close();
}
}
Aggregations