Search in sources :

Example 51 with Invocation

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());
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Test(org.junit.Test)

Example 52 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 53 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 54 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) ArrayList(java.util.ArrayList) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 55 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MockUp(mockit.MockUp) Test(org.junit.Test)

Aggregations

Invocation (org.apache.servicecomb.core.Invocation)204 Test (org.junit.Test)125 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)52 ArrayList (java.util.ArrayList)39 HashMap (java.util.HashMap)37 Response (org.apache.servicecomb.swagger.invocation.Response)37 Before (org.junit.Before)29 MockUp (mockit.MockUp)26 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)23 Expectations (mockit.Expectations)20 InvocationFinishEvent (org.apache.servicecomb.core.event.InvocationFinishEvent)20 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)19 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)18 Transport (org.apache.servicecomb.core.Transport)18 RoutingContext (io.vertx.ext.web.RoutingContext)17 Map (java.util.Map)17 ServerAccessLogEvent (org.apache.servicecomb.core.event.ServerAccessLogEvent)17 List (java.util.List)16 Endpoint (org.apache.servicecomb.core.Endpoint)16 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)15