Search in sources :

Example 11 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method send_success.

@Test
public void send_success() {
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", null);
    CseServer server = new CseServer(restTransport, cacheEndpoint);
    new MockUp<System>() {

        @Mock
        long currentTimeMillis() {
            return 123;
        }
    };
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
        }
    };
    server.incrementContinuousFailureCount();
    sendResponse = Response.ok("success");
    Holder<String> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(123, server.getLastVisitTime());
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getActiveRequestsCount());
    Assert.assertEquals("success", result.value);
    Assert.assertEquals(0, server.getCountinuousFailureCount());
}
Also used : Expectations(mockit.Expectations) BeforeClass(org.junit.BeforeClass) Transport(org.apache.servicecomb.core.Transport) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) Expectations(mockit.Expectations) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) SimpleTransactionControlFilter(org.apache.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) ArrayList(java.util.ArrayList) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) Status(javax.ws.rs.core.Response.Status) InstanceCacheManager(org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MockUp(mockit.MockUp) Server(com.netflix.loadbalancer.Server) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Mockito(org.mockito.Mockito) List(java.util.List) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Rule(org.junit.Rule) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) IRule(com.netflix.loadbalancer.IRule) Holder(javax.xml.ws.Holder) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) CseContext(org.apache.servicecomb.core.CseContext) Assert(org.junit.Assert) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 12 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method testSetIsolationFilter.

@Test
public void testSetIsolationFilter() {
    Invocation invocation = Mockito.mock(Invocation.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("test");
    LoadbalanceHandler lbHandler = new LoadbalanceHandler();
    LoadBalancer myLB = new LoadBalancer("loadBalancerName", rule, "test");
    lbHandler.setIsolationFilter(myLB, "abc");
    Assert.assertEquals(1, myLB.getFilterSize());
    Mockito.when(invocation.getMicroserviceName()).thenReturn("abc");
    myLB = new LoadBalancer("loadBalancerName", rule, "test");
    lbHandler.setIsolationFilter(myLB, "abc");
    myLB.setInvocation(invocation);
    Assert.assertEquals(1, myLB.getFilterSize());
    Map<String, ServerListFilterExt> filters = Deencapsulation.getField(myLB, "filters");
    List<Server> servers = new ArrayList<>();
    servers.add(new Server(null));
    Assert.assertEquals(servers.size(), filters.get("org.apache.servicecomb.loadbalance.filter.IsolationServerListFilter").getFilteredListOfServers(servers).size());
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method send_failed.

@Test
public void send_failed() {
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", null);
    CseServer server = new CseServer(restTransport, cacheEndpoint);
    new MockUp<System>() {

        @Mock
        long currentTimeMillis() {
            return 123;
        }
    };
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
        }
    };
    int continuousFailureCount = server.getCountinuousFailureCount();
    sendResponse = Response.create(Status.BAD_REQUEST, "send failed");
    Holder<Throwable> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = (Throwable) resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(123, server.getLastVisitTime());
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
    Assert.assertEquals("InvocationException: code=400;msg=send failed", result.value.getMessage());
    Assert.assertEquals(continuousFailureCount + 1, server.getCountinuousFailureCount());
}
Also used : Expectations(mockit.Expectations) BeforeClass(org.junit.BeforeClass) Transport(org.apache.servicecomb.core.Transport) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) Expectations(mockit.Expectations) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) SimpleTransactionControlFilter(org.apache.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) ArrayList(java.util.ArrayList) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) Status(javax.ws.rs.core.Response.Status) InstanceCacheManager(org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MockUp(mockit.MockUp) Server(com.netflix.loadbalancer.Server) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Mockito(org.mockito.Mockito) List(java.util.List) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Rule(org.junit.Rule) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) IRule(com.netflix.loadbalancer.IRule) Holder(javax.xml.ws.Holder) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) CseContext(org.apache.servicecomb.core.CseContext) Assert(org.junit.Assert) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Test(org.junit.Test)

Example 14 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestIsolationServerListFilter method testGetFilteredListOfServers.

@Test
public void testGetFilteredListOfServers() {
    Invocation invocation = Mockito.mock(Invocation.class);
    CseServer testServer = Mockito.mock(CseServer.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("microserviceName");
    Mockito.when(testServer.getLastVisitTime()).thenReturn(System.currentTimeMillis());
    List<Server> serverList = new ArrayList<>();
    serverList.add(testServer);
    IsolationServerListFilter.setLoadBalancerStats(loadBalancerStats);
    IsolationServerListFilter.setInvocation(invocation);
    List<Server> returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(returnedServerList.size(), 1);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementSuccessiveConnectionFailureCount(testServer);
    returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(returnedServerList.size(), 0);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) CseServer(org.apache.servicecomb.loadbalance.CseServer) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestIsolationServerListFilter method testGetFilteredListOfServersOnContinuousFailureIsBelowThreshold.

@Test
public void testGetFilteredListOfServersOnContinuousFailureIsBelowThreshold() {
    ((AbstractConfiguration) DynamicPropertyFactory.getBackingConfigurationSource()).addProperty("cse.loadbalance.isolation.continuousFailureThreshold", "3");
    Invocation invocation = Mockito.mock(Invocation.class);
    CseServer testServer = Mockito.mock(CseServer.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("microserviceName");
    Mockito.when(testServer.getCountinuousFailureCount()).thenReturn(2);
    Mockito.when(testServer.getLastVisitTime()).thenReturn(System.currentTimeMillis());
    for (int i = 0; i < 3; ++i) {
        loadBalancerStats.incrementNumRequests(testServer);
    }
    List<Server> serverList = new ArrayList<>();
    serverList.add(testServer);
    IsolationServerListFilter.setLoadBalancerStats(loadBalancerStats);
    IsolationServerListFilter.setInvocation(invocation);
    List<Server> returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(1, returnedServerList.size());
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Invocation(org.apache.servicecomb.core.Invocation) CseServer(org.apache.servicecomb.loadbalance.CseServer) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Invocation (org.apache.servicecomb.core.Invocation)66 Test (org.junit.Test)50 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)23 Response (org.apache.servicecomb.swagger.invocation.Response)19 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)16 MockUp (mockit.MockUp)11 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)9 ArrayList (java.util.ArrayList)9 Server (com.netflix.loadbalancer.Server)8 HashMap (java.util.HashMap)8 Holder (javax.xml.ws.Holder)8 Expectations (mockit.Expectations)7 Mock (mockit.Mock)7 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)5 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)5 Map (java.util.Map)4 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4 Endpoint (org.apache.servicecomb.core.Endpoint)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4