Search in sources :

Example 31 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class MockClusterInvoker method doMockInvoke.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Result doMockInvoke(Invocation invocation, RpcException e) {
    Result result = null;
    Invoker<T> minvoker;
    List<Invoker<T>> mockInvokers = selectMockInvoker(invocation);
    if (mockInvokers == null || mockInvokers.isEmpty()) {
        minvoker = (Invoker<T>) new MockInvoker(directory.getUrl());
    } else {
        minvoker = mockInvokers.get(0);
    }
    try {
        result = minvoker.invoke(invocation);
    } catch (RpcException me) {
        if (me.isBiz()) {
            result = new RpcResult(me.getCause());
        } else {
            throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause());
        }
    } catch (Throwable me) {
        throw new RpcException(getMockExceptionMessage(e, me), me.getCause());
    }
    return result;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) RpcException(com.alibaba.dubbo.rpc.RpcException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Example 32 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class LoadBalanceTest method testLeastActiveLoadBalance_select.

@Test
public void testLeastActiveLoadBalance_select() {
    int runs = 10000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
    for (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
        // System.out.println(count);
        Assert.assertTrue("abs diff shoud < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(com.alibaba.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 33 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class LoadBalanceTest method testRoundRobinLoadBalance_select.

@Test
public void testRoundRobinLoadBalance_select() {
    int runs = 10000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RoundRobinLoadBalance.NAME);
    for (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
        Assert.assertTrue("abs diff shoud < 1", Math.abs(count - runs / (0f + invokers.size())) < 1f);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(com.alibaba.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 34 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class LoadBalanceTest method testRandomLoadBalance_select.

@Test
public void testRandomLoadBalance_select() {
    int runs = 1000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RandomLoadBalance.NAME);
    for (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
        // System.out.println(count);
        Assert.assertTrue("abs diff shoud < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
    }
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            RpcStatus.beginCount(invokers.get(i).getUrl(), invocation.getMethodName());
        }
    }
    counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
    for (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
    }
    Assert.assertEquals(runs, counter.get(invoker1).intValue());
    Assert.assertEquals(0, counter.get(invoker2).intValue());
    Assert.assertEquals(0, counter.get(invoker3).intValue());
    Assert.assertEquals(0, counter.get(invoker4).intValue());
    Assert.assertEquals(0, counter.get(invoker5).intValue());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(com.alibaba.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 35 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_False_HostFilter.

@Test
public void testRoute_False_HostFilter() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("true => " + " host = " + NetUtils.getLocalHost()));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
    Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
    Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
    Assert.assertEquals(2, fileredInvokers.size());
    Assert.assertEquals(invoker2, fileredInvokers.get(0));
    Assert.assertEquals(invoker3, fileredInvokers.get(1));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)67 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)39 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)38 URL (com.alibaba.dubbo.common.URL)34 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)19 Router (com.alibaba.dubbo.rpc.cluster.Router)14 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)13 Result (com.alibaba.dubbo.rpc.Result)11 Invocation (com.alibaba.dubbo.rpc.Invocation)10 RpcException (com.alibaba.dubbo.rpc.RpcException)9 RpcResult (com.alibaba.dubbo.rpc.RpcResult)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)8 DemoService (com.alibaba.dubbo.rpc.support.DemoService)7 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)5 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)5 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4