use of org.apache.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);
Assertions.assertSame(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);
Assertions.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);
Assertions.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);
Assertions.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);
Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
}
}
}
use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.
the class AbstractClusterInvokerTest method testSelect_Invokersize1.
@Test
public void testSelect_Invokersize1() throws Exception {
invokers.clear();
invokers.add(invoker1);
LoadBalance l = cluster.initLoadBalance(invokers, invocation);
Assertions.assertNotNull(l, "cluster.initLoadBalance returns null!");
Invoker invoker = cluster.select(l, null, invokers, null);
Assertions.assertEquals(invoker1, invoker);
}
use of org.apache.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();
Assertions.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);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker2);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker4);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
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);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
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);
Assertions.assertTrue(sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
}
use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.
the class LoadBalanceBaseTest method getInvokeCounter.
public Map<Invoker, AtomicLong> getInvokeCounter(int runs, String loadbalanceName) {
Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
LoadBalance lb = getLoadBalance(loadbalanceName);
for (Invoker invoker : invokers) {
counter.put(invoker, new AtomicLong(0));
}
URL url = invokers.get(0).getUrl();
for (int i = 0; i < runs; i++) {
Invoker sinvoker = lb.select(invokers, url, invocation);
counter.get(sinvoker).incrementAndGet();
}
return counter;
}
use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.
the class AbstractClusterInvokerTest method setUp.
@SuppressWarnings({ "unchecked" })
@BeforeEach
public void setUp() throws Exception {
invocation.setMethodName("sayHello");
invoker1 = mock(Invoker.class);
invoker2 = mock(Invoker.class);
invoker3 = mock(Invoker.class);
invoker4 = mock(Invoker.class);
invoker5 = mock(Invoker.class);
mockedInvoker1 = mock(Invoker.class);
URL turl = URL.valueOf("test://test:11/test");
given(invoker1.isAvailable()).willReturn(false);
given(invoker1.getInterface()).willReturn(IHelloService.class);
given(invoker1.getUrl()).willReturn(turl.setPort(1).addParameter("name", "invoker1"));
given(invoker2.isAvailable()).willReturn(true);
given(invoker2.getInterface()).willReturn(IHelloService.class);
given(invoker2.getUrl()).willReturn(turl.setPort(2).addParameter("name", "invoker2"));
given(invoker3.isAvailable()).willReturn(false);
given(invoker3.getInterface()).willReturn(IHelloService.class);
given(invoker3.getUrl()).willReturn(turl.setPort(3).addParameter("name", "invoker3"));
given(invoker4.isAvailable()).willReturn(true);
given(invoker4.getInterface()).willReturn(IHelloService.class);
given(invoker4.getUrl()).willReturn(turl.setPort(4).addParameter("name", "invoker4"));
given(invoker5.isAvailable()).willReturn(false);
given(invoker5.getInterface()).willReturn(IHelloService.class);
given(invoker5.getUrl()).willReturn(turl.setPort(5).addParameter("name", "invoker5"));
given(mockedInvoker1.isAvailable()).willReturn(false);
given(mockedInvoker1.getInterface()).willReturn(IHelloService.class);
given(mockedInvoker1.getUrl()).willReturn(turl.setPort(999).setProtocol("mock"));
invokers.add(invoker1);
dic = new StaticDirectory<>(url, invokers, null);
dic.setConsumerUrl(consumerUrl);
cluster = new AbstractClusterInvoker(dic) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
return null;
}
};
cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
return null;
}
};
}
Aggregations