Search in sources :

Example 6 with Referer

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

the class ConfigurableWeightLoadBalanceTest method testDoSelectToHolder.

@Test
public void testDoSelectToHolder() {
    generate(3, new int[] { 2, 3, 5 }, new int[] { 3, 4, 5 });
    List<Referer<IHello>> list = new ArrayList<Referer<IHello>>();
    balance.doSelectToHolder(new DefaultRequest(), list);
    assertTrue(list.size() > 0 && list.size() <= ConfigurableWeightLoadBalance.MAX_REFERER_COUNT);
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ArrayList(java.util.ArrayList) Referer(com.weibo.api.motan.rpc.Referer) MockReferer(com.weibo.api.motan.mock.MockReferer) IHello(com.weibo.api.motan.protocol.example.IHello) Test(org.junit.Test)

Example 7 with Referer

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

the class ConsistentHashLoadBalanceTest method testSelect.

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

        {
            for (Referer<IHello> ref : referers) {
                atLeast(0).of(ref).isAvailable();
                will(returnValue(true));
            }
            atLeast(1).of(request).getArguments();
            will(returnValue(new Object[] { 1, 2, 3 }));
            atLeast(0).of(request).getParamtersDesc();
            will(returnValue("void_"));
        }
    });
    Referer<IHello> ref1 = consistentHashLoadBalance.select(request);
    for (int i = 0; i < 100; i++) {
        Referer<IHello> ref2 = consistentHashLoadBalance.select(request);
        assertEquals(ref1, ref2);
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) Referer(com.weibo.api.motan.rpc.Referer) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 8 with Referer

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

the class ConsistentHashLoadBalanceTest method testSelect2.

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

        {
            int i = 0;
            for (Referer<IHello> ref : referers) {
                boolean rs = i++ % 2 == 1;
                atLeast(0).of(ref).isAvailable();
                will(returnValue(rs));
            }
            atLeast(1).of(request).getArguments();
            will(returnValue(new Object[] { 1, 2, 3 }));
            atLeast(0).of(request).getParamtersDesc();
            will(returnValue("void_"));
        }
    });
    Referer<IHello> ref1 = consistentHashLoadBalance.select(request);
    for (int i = 0; i < 100; i++) {
        Referer<IHello> ref2 = consistentHashLoadBalance.select(request);
        assertEquals(ref1, ref2);
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) Referer(com.weibo.api.motan.rpc.Referer) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 9 with Referer

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

the class ClusterSpiTest method initCluster.

@SuppressWarnings("unchecked")
private void initCluster(boolean throwException) {
    referers = new ArrayList<Referer<IHello>>();
    for (int i = 0; i < 10; i++) {
        referers.add(mockery.mock(Referer.class, "ref_" + i));
    }
    clusterSpi.setHaStrategy(new FailoverHaStrategy<IHello>());
    clusterSpi.setLoadBalance(new RandomLoadBalance<IHello>());
    URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
    url.addParameter(URLParamType.throwException.getName(), String.valueOf(throwException));
    url.addParameter(URLParamType.retries.getName(), "2");
    clusterSpi.setUrl(url);
    clusterSpi.onRefresh(referers);
    clusterSpi.init();
}
Also used : Referer(com.weibo.api.motan.rpc.Referer) RegistryService(com.weibo.api.motan.registry.RegistryService) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL)

Example 10 with Referer

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

the class ClusterSpiTest method testSilentCall.

public void testSilentCall() {
    initCluster(false);
    final Request request = mockRequest();
    final URL url = mockURL();
    mockery.checking(new Expectations() {

        {
            for (Referer<IHello> ref : referers) {
                atLeast(0).of(ref).call(request);
                will(throwException(new IllegalStateException("Throw exception for test")));
                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();
}
Also used : 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) URL(com.weibo.api.motan.rpc.URL)

Aggregations

Referer (com.weibo.api.motan.rpc.Referer)24 URL (com.weibo.api.motan.rpc.URL)13 IHello (com.weibo.api.motan.protocol.example.IHello)12 Expectations (org.jmock.Expectations)12 ArrayList (java.util.ArrayList)11 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)8 MockReferer (com.weibo.api.motan.mock.MockReferer)7 Request (com.weibo.api.motan.rpc.Request)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 IWorld (com.weibo.api.motan.protocol.example.IWorld)4 Response (com.weibo.api.motan.rpc.Response)4 Test (org.junit.Test)4 RegistryService (com.weibo.api.motan.registry.RegistryService)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 LoadBalance (com.weibo.api.motan.cluster.LoadBalance)1 FailoverHaStrategy (com.weibo.api.motan.cluster.ha.FailoverHaStrategy)1 RandomLoadBalance (com.weibo.api.motan.cluster.loadbalance.RandomLoadBalance)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1