Search in sources :

Example 66 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelectBalance.

/**
 * Test balance.
 */
@Test
public void testSelectBalance() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
    for (Invoker invoker : invokers) {
        counter.put(invoker, new AtomicLong(0));
    }
    int runs = 1000;
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        counter.get(sinvoker).incrementAndGet();
    }
    for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
        Long count = entry.getValue().get();
        // System.out.println(count);
        if (entry.getKey().isAvailable())
            Assertions.assertTrue(count > runs / invokers.size(), "count should > avg");
    }
    Assertions.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 67 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testDonotSelectAgainAndNoCheckAvailable.

@Test
public void testDonotSelectAgainAndNoCheckAvailable() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertSame(invoker1, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertSame(invoker2, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertSame(invoker3, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertSame(invoker5, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertTrue(invokers.contains(sinvoker));
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) Test(org.junit.jupiter.api.Test)

Example 68 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelect_Invokersize2AndselectNotNull.

@Test
public void testSelect_Invokersize2AndselectNotNull() throws Exception {
    invokers.clear();
    invokers.add(invoker2);
    invokers.add(invoker4);
    LoadBalance l = cluster.initLoadBalance(invokers, invocation);
    Assertions.assertNotNull(l, "cluster.initLoadBalance returns null!");
    {
        selectedInvokers.clear();
        selectedInvokers.add(invoker4);
        Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers);
        Assertions.assertEquals(invoker2, invoker);
    }
    {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers);
        Assertions.assertEquals(invoker4, invoker);
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) Test(org.junit.jupiter.api.Test)

Example 69 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelect_Invokersize0.

@Test
public void testSelect_Invokersize0() throws Exception {
    LoadBalance l = cluster.initLoadBalance(invokers, invocation);
    Assertions.assertNotNull(l, "cluster.initLoadBalance returns null!");
    {
        Invoker invoker = cluster.select(l, null, null, null);
        Assertions.assertNull(invoker);
    }
    {
        invokers.clear();
        selectedInvokers.clear();
        Invoker invoker = cluster.select(l, null, invokers, null);
        Assertions.assertNull(invoker);
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) Test(org.junit.jupiter.api.Test)

Example 70 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractLoadBalanceTest method testGetWeight.

@Test
public void testGetWeight() {
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("say");
    Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url1 = new URL("", "", 0, "DemoService", new HashMap<>());
    url1 = url1.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - Integer.MAX_VALUE - 1);
    given(invoker1.getUrl()).willReturn(url1);
    Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url2 = new URL("", "", 0, "DemoService", new HashMap<>());
    url2 = url2.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - 10 * 60 * 1000L - 1);
    given(invoker2.getUrl()).willReturn(url2);
    Assertions.assertEquals(balance.getWeight(invoker1, invocation), balance.getWeight(invoker2, invocation));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8