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());
}
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));
}
}
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);
}
}
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);
}
}
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));
}
Aggregations