Search in sources :

Example 21 with MotanServiceException

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));
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) Request(com.weibo.api.motan.rpc.Request) RegistryService(com.weibo.api.motan.registry.RegistryService) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 22 with MotanServiceException

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);
    }
}
Also used : Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) Request(com.weibo.api.motan.rpc.Request) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) Response(com.weibo.api.motan.rpc.Response) RegistryService(com.weibo.api.motan.registry.RegistryService) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 23 with MotanServiceException

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);
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) IWorld(com.weibo.api.motan.protocol.example.IWorld) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL)

Example 24 with MotanServiceException

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()));
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) Test(org.junit.Test)

Aggregations

MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)24 Response (com.weibo.api.motan.rpc.Response)11 URL (com.weibo.api.motan.rpc.URL)8 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 Request (com.weibo.api.motan.rpc.Request)7 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)6 Expectations (org.jmock.Expectations)6 IHello (com.weibo.api.motan.protocol.example.IHello)4 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)4 Referer (com.weibo.api.motan.rpc.Referer)4 MotanBizException (com.weibo.api.motan.exception.MotanBizException)3 TransportException (com.weibo.api.motan.transport.TransportException)3 Method (java.lang.reflect.Method)3 HashMap (java.util.HashMap)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)2 RegistryService (com.weibo.api.motan.registry.RegistryService)2 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 Test (org.junit.Test)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 MotanAbstractException (com.weibo.api.motan.exception.MotanAbstractException)1