use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestServiceCombServerStats method testGlobalAllowIsolatedServerTryingFlag_apply_with_chance_occupied.
@Test
public void testGlobalAllowIsolatedServerTryingFlag_apply_with_chance_occupied() {
Invocation invocation = new Invocation();
Assert.assertTrue(ServiceCombServerStats.applyForTryingChance(invocation));
Assert.assertSame(invocation, ServiceCombServerStats.globalAllowIsolatedServerTryingFlag.get().getInvocation());
Invocation otherInvocation = new Invocation();
Assert.assertFalse(ServiceCombServerStats.applyForTryingChance(otherInvocation));
Assert.assertSame(invocation, ServiceCombServerStats.globalAllowIsolatedServerTryingFlag.get().getInvocation());
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestWeightedResponseTimeRuleExt method testWeighed.
@Test
public void testWeighed() throws InterruptedException {
WeightedResponseTimeRuleExt rule = new WeightedResponseTimeRuleExt();
LoadBalancer loadBalancer = new LoadBalancer(rule, "testService");
List<ServiceCombServer> servers = new ArrayList<>();
Invocation invocation = Mockito.mock(Invocation.class);
ServiceCombServer server1 = Mockito.mock(ServiceCombServer.class);
Mockito.when(server1.toString()).thenReturn("server " + 0);
servers.add(server1);
ServiceCombServer server2 = Mockito.mock(ServiceCombServer.class);
Mockito.when(server2.toString()).thenReturn("server " + 1);
servers.add(server2);
AtomicInteger serverCounter1 = new AtomicInteger(0);
AtomicInteger serverCounter2 = new AtomicInteger(0);
for (int i = 0; i < 2000; i++) {
loadBalancer.getLoadBalancerStats().noteResponseTime(server1, 20);
loadBalancer.getLoadBalancerStats().noteResponseTime(server2, 400);
Thread.sleep(1);
if (rule.choose(servers, invocation).toString().equals("server 0")) {
serverCounter1.incrementAndGet();
} else {
serverCounter2.incrementAndGet();
}
}
double percent = (double) serverCounter1.get() / (serverCounter2.get() + serverCounter1.get());
System.out.println("percent" + percent);
Assert.assertTrue(percent > 0.60d);
Assert.assertTrue(percent < 0.90d);
serverCounter1.set(0);
serverCounter2.set(0);
Thread.sleep(1000);
for (int i = 0; i < 2000; i++) {
loadBalancer.getLoadBalancerStats().noteResponseTime(server1, 20);
loadBalancer.getLoadBalancerStats().noteResponseTime(server2, 20);
Thread.sleep(1);
if (rule.choose(servers, invocation).toString().equals("server 0")) {
serverCounter1.incrementAndGet();
} else {
serverCounter2.incrementAndGet();
}
}
percent = (double) serverCounter1.get() / (serverCounter2.get() + serverCounter1.get());
System.out.println("percent" + percent);
Assert.assertEquals(0.50d, percent, 0.2);
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestWeightedResponseTimeRuleExt method testBenchmark.
@Test
public void testBenchmark() {
// 100 instances will taken less than 0.1ms. Because we use weighed rule when response time more than 10ms,
// This only taken 1/1000 time.
WeightedResponseTimeRuleExt rule = new WeightedResponseTimeRuleExt();
LoadBalancer loadBalancer = new LoadBalancer(rule, "testService");
List<ServiceCombServer> servers = new ArrayList<>();
Invocation invocation = Mockito.mock(Invocation.class);
for (int i = 0; i < 100; i++) {
ServiceCombServer server = Mockito.mock(ServiceCombServer.class);
Mockito.when(server.toString()).thenReturn("server " + i);
servers.add(server);
loadBalancer.getLoadBalancerStats().noteResponseTime(server, i);
}
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
rule.choose(servers, invocation);
}
long taken = System.currentTimeMillis() - begin;
System.out.println("taken " + taken);
// 5 * times make slow machine happy
Assert.assertEquals("actually taken: " + taken, taken < 1000 * 5, true);
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestSessionSticknessRule method testServerWithTimeout.
@Test
public void testServerWithTimeout() {
boolean status = true;
SessionStickinessRule ss = new SessionStickinessRule();
Invocation invocation = mock(Invocation.class);
ServiceCombServer server = mock(ServiceCombServer.class);
List<ServiceCombServer> servers = new ArrayList<>();
servers.add(server);
Deencapsulation.setField(ss, "lastServer", server);
new MockUp<SessionStickinessRule>() {
@Mock
private boolean isTimeOut() {
return true;
}
};
try {
ss.choose(servers, invocation);
} catch (Exception e) {
status = false;
}
Assert.assertTrue(status);
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestSessionSticknessRule method testServerWithoutTimeoutAndThreshold.
@Test
public void testServerWithoutTimeoutAndThreshold() {
boolean status = true;
SessionStickinessRule ss = new SessionStickinessRule();
Invocation invocation = mock(Invocation.class);
ServiceCombServer server = mock(ServiceCombServer.class);
List<ServiceCombServer> servers = new ArrayList<>();
servers.add(server);
Deencapsulation.setField(ss, "lastServer", server);
new MockUp<SessionStickinessRule>() {
@Mock
private boolean isTimeOut() {
return false;
}
};
new MockUp<SessionStickinessRule>() {
@Mock
private boolean isErrorThresholdMet() {
return false;
}
};
new MockUp<SessionStickinessRule>() {
@Mock
private boolean isLastServerExists(Server server) {
return true;
}
};
try {
ss.choose(servers, invocation);
} catch (Exception e) {
status = false;
}
Assert.assertTrue(status);
}
Aggregations