Search in sources :

Example 26 with Request

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

the class FailfastHaStrategyTest method testCallError.

@Test(expected = MotanServiceException.class)
@SuppressWarnings("unchecked")
public void testCallError() {
    final LoadBalance<IWorld> loadBalance = mockery.mock(LoadBalance.class);
    final Referer<IWorld> referer = mockery.mock(Referer.class);
    final Request request = mockery.mock(Request.class);
    mockery.checking(new Expectations() {

        {
            one(loadBalance).select(request);
            will(returnValue(referer));
            one(referer).call(request);
            will(returnValue(null));
        }
    });
    assertEquals(null, failfastHaStrategy.call(request, loadBalance));
}
Also used : Expectations(org.jmock.Expectations) IWorld(com.weibo.api.motan.protocol.example.IWorld) Request(com.weibo.api.motan.rpc.Request) Test(org.junit.Test)

Example 27 with Request

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

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

the class RandomLoadBalanceTest method testSelect.

public void testSelect() {
    final Request request = mockery.mock(Request.class);
    mockery.checking(new Expectations() {

        {
            atLeast(1).of(request).getArguments();
            will(returnValue(new Object[] { 1, 2, 3 }));
            atLeast(0).of(request).getParamtersDesc();
            will(returnValue("void_"));
        }
    });
    for (int i = 0; i < 10; i++) {
        Referer<IHello> ref = randomLoadBalance.select(request);
        for (int j = 0; j < falseCount; j++) {
            assertNotSame(ref, referers.get(j));
        }
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 29 with Request

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

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

the class OpenTracingFilterTest method setUp.

@Before
public void setUp() throws Exception {
    OTFilter = new OpenTracingFilter();
    tracer = new MockTracer();
    OpenTracingContext.tracerFactory = new TracerFactory() {

        @Override
        public Tracer getTracer() {
            return tracer;
        }
    };
    URL url = new URL("motan", "localhost", 8002, "HelloService");
    request = new DefaultRequest();
    request.setInterfaceName("HelloService");
    request.setAttachment(URLParamType.group.name(), "test");
    request.setMethodName("sayHello");
    request.setParamtersDesc("java.lang.String");
    response = new DefaultResponse();
    refer = new AbstractReferer<HelloService>(HelloService.class, url) {

        @Override
        public void destroy() {
        }

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        protected Response doCall(Request request) {
            return response;
        }

        @Override
        protected boolean doInit() {
            return true;
        }
    };
    provider = new DefaultProvider<HelloService>(new HelloServiceImpl(), url, HelloService.class) {

        @Override
        public Response call(Request request) {
            return response;
        }
    };
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Tracer(io.opentracing.Tracer) MockTracer(io.opentracing.mock.MockTracer) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) MockTracer(io.opentracing.mock.MockTracer) URL(com.weibo.api.motan.rpc.URL) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) Before(org.junit.Before)

Aggregations

Request (com.weibo.api.motan.rpc.Request)31 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)12 Expectations (org.jmock.Expectations)12 Response (com.weibo.api.motan.rpc.Response)10 IHello (com.weibo.api.motan.protocol.example.IHello)9 URL (com.weibo.api.motan.rpc.URL)9 Test (org.junit.Test)9 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)6 Referer (com.weibo.api.motan.rpc.Referer)5 HashMap (java.util.HashMap)5 RegistryService (com.weibo.api.motan.registry.RegistryService)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)3 Channel (com.weibo.api.motan.transport.Channel)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 YarRequest (com.weibo.yar.YarRequest)3 ArrayList (java.util.ArrayList)3 Serialization (com.weibo.api.motan.codec.Serialization)2 IOException (java.io.IOException)2