Search in sources :

Example 6 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testCloseAvailablecheck.

@Test
public void testCloseAvailablecheck() {
    LoadBalance lb = EasyMock.createMock(LoadBalance.class);
    EasyMock.expect(lb.select(invokers, url, invocation)).andReturn(invoker1);
    EasyMock.replay(lb);
    initlistsize5();
    Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
    Assert.assertEquals(false, sinvoker.isAvailable());
    Assert.assertEquals(invoker1, sinvoker);
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) Test(org.junit.Test)

Example 7 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelect_multiInvokers.

public void testSelect_multiInvokers(String lbname) throws Exception {
    int min = 1000, max = 5000;
    Double d = (Math.random() * (max - min + 1) + min);
    int runs = d.intValue();
    Assert.assertTrue(runs > min);
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(lbname);
    initlistsize5();
    for (int i = 0; i < runs; i++) {
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker4);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance)

Example 8 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelectAgainAndCheckAvailable.

@Test
public void testSelectAgainAndCheckAvailable() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertTrue(sinvoker == invoker4);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            selectedInvokers.add(invoker1);
            selectedInvokers.add(invoker3);
            selectedInvokers.add(invoker5);
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            selectedInvokers.add(invoker1);
            selectedInvokers.add(invoker3);
            selectedInvokers.add(invoker2);
            selectedInvokers.add(invoker4);
            selectedInvokers.add(invoker5);
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) Test(org.junit.Test)

Example 9 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance 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 (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
        // System.out.println(count);
        if (minvoker.isAvailable())
            Assert.assertTrue("count should > avg", count > runs / invokers.size());
    }
    Assert.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get());
    ;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)9 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)6 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)6 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)6 Test (org.junit.Test)4 URL (com.alibaba.dubbo.common.URL)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 RpcException (com.alibaba.dubbo.rpc.RpcException)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ProxyFactory (com.alibaba.dubbo.rpc.ProxyFactory)1 Result (com.alibaba.dubbo.rpc.Result)1 StaticDirectory (com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory)1 AbstractClusterInvoker (com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker)1 Before (org.junit.Before)1