Search in sources :

Example 6 with Response

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

the class FailoverHaStrategyTest method testCallWithFalse.

public void testCallWithFalse() {
    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() {

        {
            one(loadBalance).selectToHolder(request, failoverHaStrategy.referersHolder.get());
            for (Referer<IWorld> ref : referers) {
                atLeast(0).of(ref).isAvailable();
                will(returnValue(true));
                atLeast(0).of(ref).getUrl();
                will(returnValue(url));
                atLeast(0).of(ref).destroy();
            }
            atLeast(2).of(referers.get(0)).call(request);
            will(throwException(new MotanServiceException("mock throw exception when 1th call")));
            oneOf(referers.get(1)).call(request);
            will(throwException(new MotanServiceException("mock throw exception when 2th call")));
        }
    });
    try {
        failoverHaStrategy.call(request, loadBalance);
        fail("Should throw exception before!");
        // should not run to here
        Assert.assertTrue(false);
    } catch (Exception e) {
    }
}
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) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 7 with Response

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

the class ClusterSpiTest method testCall.

public void testCall() {
    initCluster(true);
    final Request request = mockRequest();
    final Response response = mockery.mock(Response.class);
    final URL url = mockURL();
    mockery.checking(new Expectations() {

        {
            for (Referer<IHello> 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();
            }
        }
    });
    clusterSpi.call(request);
    clusterSpi.destroy();
    try {
        clusterSpi.call(request);
        fail("Should not run to here!");
    } catch (Exception e) {
        assertTrue(e instanceof MotanServiceException);
    }
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 8 with Response

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

the class ClusterTest method testCall.

@Test
public void testCall() {
    final Request request = mockery.mock(Request.class);
    final Response rs = mockery.mock(Response.class);
    mockery.checking(new Expectations() {

        {
            allowing(any(Referer.class)).method("getUrl").withNoArguments();
            will(returnValue(new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 18080, Object.class.getName())));
            allowing(any(Referer.class)).method("isAvailable").withNoArguments();
            will(returnValue(true));
            allowing(any(Referer.class)).method("call").with(same(request));
            will(returnValue(rs));
            atLeast(0).of(request).setRetries(0);
            will(returnValue(null));
            atLeast(0).of(request).getRetries();
            will(returnValue(0));
            atLeast(0).of(request).getMethodName();
            will(returnValue("get"));
            atLeast(0).of(request).getParamtersDesc();
            will(returnValue("void"));
        }
    });
    Response callRs = cluster.call(request);
    Assert.assertEquals(rs, callRs);
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 9 with Response

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

the class AccessLogFilterTest method testCall.

@SuppressWarnings("unchecked")
public void testCall() {
    final Request request = mockery.mock(Request.class);
    final Response response = mockery.mock(Response.class);
    final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
    url.addParameter(URLParamType.accessLog.getName(), String.valueOf(true));
    final Caller<IHello> caller = mockery.mock(Caller.class);
    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());
    mockery.checking(new Expectations() {

        {
            atLeast(1).of(caller).getUrl();
            will(returnValue(url));
            exactly(1).of(caller).call(request);
            will(returnValue(response));
            exactly(1).of(request).getInterfaceName();
            will(returnValue(IHello.class.getName()));
            exactly(1).of(request).getMethodName();
            will(returnValue("get"));
            exactly(1).of(request).getParamtersDesc();
            will(returnValue("param_desc"));
            exactly(1).of(request).getRequestId();
            will(returnValue(100L));
            atLeast(1).of(request).getAttachments();
            will(returnValue(attachments));
        }
    });
    accessLogFilter.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) URL(com.weibo.api.motan.rpc.URL) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 10 with Response

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

the class C method testCall.

@Test
public void testCall() {
    ProviderMessageRouter providerMessageRouter = new ProviderMessageRouter();
    Provider<ProviderA> providerA = new DefaultProvider<ProviderA>(new A(), new URL("injvm", "localhost", 0, ProviderA.class.getName()), ProviderA.class);
    Provider<ProviderB> providerB = new DefaultProvider<ProviderB>(new B(), new URL("injvm", "localhost", 0, ProviderB.class.getName()), ProviderB.class);
    providerMessageRouter.addProvider(providerA);
    providerMessageRouter.addProvider(providerB);
    Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_ALL);
    DefaultRequest requestA = new DefaultRequest();
    requestA.setInterfaceName(com.weibo.api.motan.transport.ProviderA.class.getName());
    requestA.setMethodName("providerA");
    requestA.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
    Response response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
    Assert.assertEquals("A", response.getValue());
    DefaultRequest requestB = new DefaultRequest();
    requestB.setInterfaceName(com.weibo.api.motan.transport.ProviderB.class.getName());
    requestB.setMethodName("providerB");
    requestB.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
    response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestB);
    Assert.assertEquals("B", response.getValue());
    providerMessageRouter.removeProvider(providerA);
    Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_B);
    try {
        Response result = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
        result.getValue();
        Assert.assertTrue(false);
    } catch (Exception e) {
        Assert.assertTrue(true);
    }
}
Also used : MockChannel(com.weibo.api.motan.mock.MockChannel) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) URL(com.weibo.api.motan.rpc.URL) Response(com.weibo.api.motan.rpc.Response) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) 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