Search in sources :

Example 1 with IWorld

use of com.weibo.api.motan.protocol.example.IWorld in project motan by weibocom.

the class FailoverHaStrategyTest method testCall.

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

        {
            // failoverHaStrategy.referersHolder.get());
            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(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) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL)

Example 2 with IWorld

use of com.weibo.api.motan.protocol.example.IWorld in project motan by weibocom.

the class FailoverHaStrategyTest method setUp.

@Before
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    super.setUp();
    loadBalance = mockery.mock(LoadBalance.class);
    final Referer<IWorld> referer1 = mockery.mock(Referer.class, "ref1");
    final Referer<IWorld> referer2 = mockery.mock(Referer.class, "ref2");
    referers = new ArrayList<Referer<IWorld>>();
    referers.add(referer1);
    referers.add(referer2);
    failoverHaStrategy = new FailoverHaStrategy<IWorld>() {

        @Override
        protected List<Referer<IWorld>> selectReferers(Request request, LoadBalance<IWorld> loadBalance) {
            return referers;
        }
    };
    URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.LOCALHOST, 0, IWorld.class.getName());
    url.addParameter(URLParamType.retries.getName(), String.valueOf(retries));
    failoverHaStrategy.setUrl(url);
}
Also used : IWorld(com.weibo.api.motan.protocol.example.IWorld) LoadBalance(com.weibo.api.motan.cluster.LoadBalance) Referer(com.weibo.api.motan.rpc.Referer) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ArrayList(java.util.ArrayList) List(java.util.List) URL(com.weibo.api.motan.rpc.URL) Before(org.junit.Before)

Example 3 with IWorld

use of com.weibo.api.motan.protocol.example.IWorld 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 4 with IWorld

use of com.weibo.api.motan.protocol.example.IWorld in project motan by weibocom.

the class RefererConfigTest method testException.

@Test
public void testException() {
    IWorld ref = null;
    // protocol empty
    List<ProtocolConfig> protocols = new ArrayList<ProtocolConfig>();
    refererConfig.setProtocols(protocols);
    try {
        ref = refererConfig.getRef();
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("protocol not set correctly"));
    }
    // protocol not exists
    protocols.add(mockProtocolConfig("notExist"));
    try {
        ref = refererConfig.getRef();
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Protocol is null"));
    }
    protocols.add(mockProtocolConfig("notExist"));
    // method config wrong
    MethodConfig mConfig = new MethodConfig();
    mConfig.setName("notExist");
    refererConfig.setMethods(mConfig);
    try {
        ref = refererConfig.getRef();
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("not found method"));
    }
}
Also used : IWorld(com.weibo.api.motan.protocol.example.IWorld) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with IWorld

use of com.weibo.api.motan.protocol.example.IWorld in project motan by weibocom.

the class RefererConfigTest method testMultiProtocol.

@Test
public void testMultiProtocol() {
    List<ProtocolConfig> protocols = getMultiProtocols(MotanConstants.PROTOCOL_INJVM, MotanConstants.PROTOCOL_MOTAN);
    refererConfig.setProtocols(protocols);
    IWorld ref = refererConfig.getRef();
    assertNotNull(ref);
    assertEquals(protocols.size(), refererConfig.getClusterSupports().size());
}
Also used : IWorld(com.weibo.api.motan.protocol.example.IWorld) Test(org.junit.Test)

Aggregations

IWorld (com.weibo.api.motan.protocol.example.IWorld)10 Expectations (org.jmock.Expectations)5 Test (org.junit.Test)5 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)4 Referer (com.weibo.api.motan.rpc.Referer)4 Response (com.weibo.api.motan.rpc.Response)4 URL (com.weibo.api.motan.rpc.URL)4 IHello (com.weibo.api.motan.protocol.example.IHello)3 Request (com.weibo.api.motan.rpc.Request)3 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)2 MockWorld (com.weibo.api.motan.protocol.example.MockWorld)2 ArrayList (java.util.ArrayList)2 LoadBalance (com.weibo.api.motan.cluster.LoadBalance)1 ServiceConfig (com.weibo.api.motan.config.ServiceConfig)1 List (java.util.List)1 Before (org.junit.Before)1