use of com.weibo.api.motan.rpc.Response in project motan by weibocom.
the class SwitcherFilterTest method testOnSwitcher.
public void testOnSwitcher() {
MotanSwitcherUtil.setSwitcherValue("mock_class_name", true);
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
atLeast(2).of(caller).getUrl();
will(returnValue(url));
atLeast(2).of(request).getMethodName();
will(returnValue("mock_mothod_name"));
atLeast(2).of(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
atLeast(2).of(request).getInterfaceName();
will(returnValue("mock_class_name"));
atLeast(1).of(request).getRequestId();
will(returnValue(1L));
allowing(request).getAttachments();
will(returnValue(attachments));
}
});
Response resultOnSwitcher = filter.filter(caller, request);
assertNotNull(resultOnSwitcher);
assertTrue(resultOnSwitcher.getException().getMessage().contains("Request false for switcher is on"));
MotanSwitcherUtil.setSwitcherValue("mock_class_name", false);
Response resultOffSwitcher = filter.filter(caller, request);
assertEquals(response, resultOffSwitcher);
}
use of com.weibo.api.motan.rpc.Response in project motan by weibocom.
the class SwitcherFilterTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
request = mockery.mock(Request.class);
response = mockery.mock(Response.class);
caller = mockery.mock(Caller.class);
url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
attachments = new HashMap<String, String>();
attachments.put(URLParamType.host.getName(), URLParamType.host.getValue());
attachments.put(URLParamType.application.getName(), URLParamType.application.getValue());
attachments.put(URLParamType.module.getName(), URLParamType.module.getValue());
}
use of com.weibo.api.motan.rpc.Response in project motan by weibocom.
the class CompressRpcCodecTest method testCodecResponse.
public void testCodecResponse(Response respose) throws Exception {
respose.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
byte[] bytes = rpcCodec.encode(channel, respose);
assertTrue(isCompressVersion(bytes));
Response result = (Response) rpcCodec.decode(channel, "", bytes);
Assert.assertTrue(result.getValue().toString().equals(respose.getValue().toString()));
}
use of com.weibo.api.motan.rpc.Response in project motan by weibocom.
the class RefererInvocationHandler method invoke.
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isLocalMethod(method)) {
if ("toString".equals(method.getName())) {
return clustersToString();
}
throw new MotanServiceException("can not invoke local method:" + method.getName());
}
DefaultRequest request = new DefaultRequest();
request.setRequestId(RequestIdGenerator.getRequestId());
request.setArguments(args);
String methodName = method.getName();
boolean async = false;
if (methodName.endsWith(MotanConstants.ASYNC_SUFFIX) && method.getReturnType().equals(ResponseFuture.class)) {
methodName = MotanFrameworkUtil.removeAsyncSuffix(methodName);
async = true;
}
RpcContext.getContext().putAttribute(MotanConstants.ASYNC_SUFFIX, async);
request.setMethodName(methodName);
request.setParamtersDesc(ReflectUtil.getMethodParamDesc(method));
request.setInterfaceName(interfaceName);
request.setAttachment(URLParamType.requestIdFromClient.getName(), String.valueOf(RequestIdGenerator.getRequestIdFromClient()));
// 那么正常情况下只会使用A,如果A被开关降级,那么就会使用B,B也被降级,那么会使用C
for (Cluster<T> cluster : clusters) {
String protocolSwitcher = MotanConstants.PROTOCOL_SWITCHER_PREFIX + cluster.getUrl().getProtocol();
Switcher switcher = switcherService.getSwitcher(protocolSwitcher);
if (switcher != null && !switcher.isOn()) {
continue;
}
request.setAttachment(URLParamType.version.getName(), cluster.getUrl().getVersion());
request.setAttachment(URLParamType.clientGroup.getName(), cluster.getUrl().getGroup());
// 带上client的application和module
request.setAttachment(URLParamType.application.getName(), ApplicationInfo.getApplication(cluster.getUrl()).getApplication());
request.setAttachment(URLParamType.module.getName(), ApplicationInfo.getApplication(cluster.getUrl()).getModule());
Response response = null;
boolean throwException = Boolean.parseBoolean(cluster.getUrl().getParameter(URLParamType.throwException.getName(), URLParamType.throwException.getValue()));
try {
response = cluster.call(request);
if (async && response instanceof ResponseFuture) {
return response;
} else {
return response.getValue();
}
} catch (RuntimeException e) {
if (ExceptionUtil.isBizException(e)) {
Throwable t = e.getCause();
// 只抛出Exception,防止抛出远程的Error
if (t != null && t instanceof Exception) {
throw t;
} else {
String msg = t == null ? "biz exception cause is null" : ("biz exception cause is throwable error:" + t.getClass() + ", errmsg:" + t.getMessage());
throw new MotanServiceException(msg, MotanErrorMsgConstant.SERVICE_DEFAULT_ERROR);
}
} else if (!throwException) {
LoggerUtil.warn("RefererInvocationHandler invoke false, so return default value: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
return getDefaultReturnValue(method.getReturnType());
} else {
LoggerUtil.error("RefererInvocationHandler invoke Error: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
throw e;
}
}
}
throw new MotanServiceException("Referer call Error: cluster not exist, interface=" + clz.getName() + " " + MotanFrameworkUtil.toString(request), MotanErrorMsgConstant.SERVICE_UNFOUND);
}
use of com.weibo.api.motan.rpc.Response in project motan by weibocom.
the class FailoverHaStrategyTest method testCall.
public void testCall() {
final DefaultRequest request = new DefaultRequest();
request.setMethodName(IWorld.class.getMethods()[0].getName());
request.setArguments(new Object[] {});
request.setInterfaceName(IHello.class.getSimpleName());
request.setParamtersDesc("void");
final Response response = mockery.mock(Response.class);
final URL url = URL.valueOf("motan%3A%2F%2F10.209.128.244%3A8000%2Fcom.weibo.api.motan.protocol.example.IWorld%3Fprotocol%3Dmotan%26export%3Dmotan%3A8000%26application%3Dapi%26module%3Dtest%26check%3Dtrue%26refreshTimestamp%3D1373275099717%26methodconfig.world%28void%29.retries%3D1%26id%3Dmotan%26methodconfig.world%28java.lang.String%29.retries%3D1%26methodconfig.world%28java.lang.String%2Cboolean%29.retries%3D1%26nodeType%3Dservice%26group%3Dwangzhe-test-yf%26shareChannel%3Dtrue%26&");
mockery.checking(new Expectations() {
{
// failoverHaStrategy.referersHolder.get());
for (Referer<IWorld> ref : referers) {
atLeast(0).of(ref).call(request);
will(returnValue(response));
atLeast(0).of(ref).isAvailable();
will(returnValue(true));
atLeast(0).of(ref).getUrl();
will(returnValue(url));
atLeast(1).of(ref).destroy();
}
one(referers.get(0)).call(request);
will(returnValue(response));
}
});
failoverHaStrategy.call(request, loadBalance);
}
Aggregations