use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class AccessStatisticFilterTest method testFilter.
@SuppressWarnings("unchecked")
public void testFilter() {
final Request request = mockery.mock(Request.class);
final Response response = mockery.mock(Response.class);
final Caller<IHello> caller = mockery.mock(Caller.class);
final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
final Map<String, String> 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());
// TODO 优化重复代码
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
atLeast(2).of(caller).getUrl();
will(returnValue(url));
oneOf(response).getException();
will(returnValue(null));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
atLeast(1).of(request).getAttachments();
will(returnValue(attachments));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(null));
oneOf(caller).getUrl();
will(returnValue(url));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertNull(accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
oneOf(caller).getUrl();
will(returnValue(url));
atMost(2).of(response).getException();
will(returnValue(new MotanBizException()));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
oneOf(caller).getUrl();
will(returnValue(url));
atMost(2).of(response).getException();
will(returnValue(new MotanServiceException()));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class ActiveLimitFilterTest method testFilter.
@SuppressWarnings("unchecked")
public void testFilter() {
final Request request = mockery.mock(Request.class);
final Response response = mockery.mock(Response.class);
final Caller<IHello> caller = mockery.mock(Caller.class);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(URLParamType.actives.getName(), "" + 3);
final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName(), parameters);
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
atLeast(1).of(caller).getUrl();
will(returnValue(url));
atLeast(1).of(request).getMethodName();
will(returnValue("mock_mothod_name"));
atLeast(1).of(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
}
});
activeLimitFilter.filter(caller, request);
for (int i = 0; i < 4; i++) {
RpcStats.beforeCall(url, request);
}
try {
activeLimitFilter.filter(caller, request);
assertFalse(true);
} catch (MotanServiceException e) {
assertEquals(MotanErrorMsgConstant.SERVICE_REJECT, e.getMotanErrorMsg());
} catch (Exception e) {
assertFalse(true);
}
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class FailoverHaStrategyTest method testCallWithOneError.
public void testCallWithOneError() {
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() {
{
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(throwException(new MotanServiceException("mock throw exception when call")));
one(referers.get(1)).call(request);
will(returnValue(response));
}
});
failoverHaStrategy.call(request, loadBalance);
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class DefaultRpcCodecTest method testException.
@Test
public void testException() throws Exception {
DefaultResponse response = new DefaultResponse();
response.setException(new MotanServiceException("process thread pool is full, reject", MotanErrorMsgConstant.SERVICE_REJECT));
byte[] bytes = rpcCodec.encode(channel, response);
Response result = (Response) rpcCodec.decode(channel, "", bytes);
Assert.assertTrue(result.getException().getMessage().equals(response.getException().getMessage()));
Assert.assertTrue(result.getException().getClass().equals(response.getException().getClass()));
}
Aggregations