Search in sources :

Example 21 with Response

use of com.weibo.api.motan.rpc.Response in project motan by weibocom.

the class DefaultRpcCodecTest method testCodecResponse.

@Test
public void testCodecResponse(Response respose) throws Exception {
    byte[] bytes = rpcCodec.encode(channel, respose);
    Response result = (Response) rpcCodec.decode(channel, "", bytes);
    Assert.assertTrue(result.getValue().toString().equals(respose.getValue().toString()));
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) Test(org.junit.Test)

Example 22 with Response

use of com.weibo.api.motan.rpc.Response 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 23 with Response

use of com.weibo.api.motan.rpc.Response 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 24 with Response

use of com.weibo.api.motan.rpc.Response 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 25 with Response

use of com.weibo.api.motan.rpc.Response 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

Response (com.weibo.api.motan.rpc.Response)31 URL (com.weibo.api.motan.rpc.URL)13 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)11 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)11 Request (com.weibo.api.motan.rpc.Request)11 Expectations (org.jmock.Expectations)10 Test (org.junit.Test)9 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)7 IHello (com.weibo.api.motan.protocol.example.IHello)6 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)4 IWorld (com.weibo.api.motan.protocol.example.IWorld)4 Referer (com.weibo.api.motan.rpc.Referer)4 RegistryService (com.weibo.api.motan.registry.RegistryService)3 HashMap (java.util.HashMap)3 MotanBizException (com.weibo.api.motan.exception.MotanBizException)2 AccessLogFilter (com.weibo.api.motan.filter.AccessLogFilter)2 Filter (com.weibo.api.motan.filter.Filter)2 Channel (com.weibo.api.motan.transport.Channel)2 YarResponse (com.weibo.yar.YarResponse)2 MockTracer (io.opentracing.mock.MockTracer)2