Search in sources :

Example 21 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestServiceCombLoadBalancerStats method testServiceExpire.

@Test
public void testServiceExpire(@Injectable Transport transport, @Mocked SPIServiceUtils utils, @Injectable MicroserviceInstancePing ping) {
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("instance1");
    new Expectations() {

        {
            SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);
            result = ping;
            ping.ping(instance);
            result = false;
        }
    };
    ServiceCombLoadBalancerStats serviceCombLoadBalancerStats = new ServiceCombLoadBalancerStats();
    serviceCombLoadBalancerStats.setServerExpireInSeconds(2);
    serviceCombLoadBalancerStats.setTimerIntervalInMillis(500);
    serviceCombLoadBalancerStats.init();
    ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
    serviceCombLoadBalancerStats.markSuccess(serviceCombServer);
    ServiceCombServerStats stats = serviceCombLoadBalancerStats.getServiceCombServerStats(serviceCombServer);
    Assert.assertEquals(serviceCombLoadBalancerStats.getPingView().size(), 1);
    await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCombLoadBalancerStats.getPingView().size() <= 0);
    Assert.assertEquals(serviceCombLoadBalancerStats.getPingView().size(), 0);
    System.out.print(stats.getFailedRequests());
    Assert.assertTrue(stats.getFailedRequests() >= 1);
}
Also used : Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 22 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestServiceCombLoadBalancerStats method testSimpleThread.

@Test
public void testSimpleThread(@Injectable Transport transport) {
    long time = System.currentTimeMillis();
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("instance1");
    ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getContinuousFailureCount(), 2);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getContinuousFailureCount(), 0);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getTotalRequests(), 4);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate(), 50);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRate(), 50);
    Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() <= System.currentTimeMillis() && ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() >= time);
}
Also used : CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 23 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestSessionSticknessRule method testLastServerNotExist.

@Test
public void testLastServerNotExist() {
    SessionStickinessRule rule = new SessionStickinessRule();
    Transport transport = mock(Transport.class);
    Invocation invocation = mock(Invocation.class);
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    ServiceCombServer mockedServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
    mockedServer.setAlive(true);
    mockedServer.setReadyToServe(true);
    mockedServer.setId("mockedServer");
    List<ServiceCombServer> allServers = Arrays.asList(mockedServer);
    LoadBalancer lb = new LoadBalancer(rule, "mockedServer");
    when(invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST)).thenReturn(allServers);
    rule.setLoadBalancer(lb);
    ServiceCombServer server = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
    Deencapsulation.setField(rule, "lastServer", server);
    new MockUp<SessionStickinessRule>(rule) {

        @Mock
        private boolean isTimeOut() {
            return false;
        }

        @Mock
        private boolean isErrorThresholdMet() {
            return false;
        }
    };
    Server s = rule.choose(allServers, invocation);
    Assert.assertEquals(mockedServer, s);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) MockUp(mockit.MockUp) Transport(org.apache.servicecomb.core.Transport) Test(org.junit.Test)

Example 24 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestSessionSticknessRule method testRuleFullOperation.

@Test
public void testRuleFullOperation() {
    SessionStickinessRule rule = new SessionStickinessRule();
    LoadBalancer mockedLb = mock(LoadBalancer.class);
    Transport transport = mock(Transport.class);
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    ServiceCombServer mockedServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8889", instance1));
    Invocation invocation = mock(Invocation.class);
    LoadBalancerStats stats = mock(LoadBalancerStats.class);
    Mockito.when(mockedLb.getLoadBalancerStats()).thenReturn(stats);
    Deencapsulation.invoke(rule, "chooseServerWhenTimeout", Arrays.asList(mockedServer), invocation);
    mockedServer.setAlive(true);
    mockedServer.setReadyToServe(true);
    List<ServiceCombServer> allServers = Arrays.asList(mockedServer);
    rule.setLoadBalancer(mockedLb);
    Server s = rule.choose(allServers, invocation);
    Assert.assertEquals(s, mockedServer);
    s = rule.choose(allServers, invocation);
    Assert.assertEquals(s, mockedServer);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Test(org.junit.Test)

Example 25 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class IsolationDiscoveryFilterTest method before.

@Before
public void before() {
    discoveryContext = new DiscoveryContext();
    discoveryContext.setInputParameters(invocation);
    discoveryTreeNode = new DiscoveryTreeNode();
    Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
    data = new HashMap<>();
    for (int i = 0; i < 3; ++i) {
        MicroserviceInstance instance = new MicroserviceInstance();
        instance.setInstanceId("i" + i);
        String endpoint = "rest://127.0.0.1:" + i;
        instance.setEndpoints(Collections.singletonList(endpoint));
        data.put(instance.getInstanceId(), instance);
        ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
        ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
    }
    discoveryTreeNode.data(data);
    filter = new IsolationDiscoveryFilter();
    TestServiceCombServerStats.releaseTryingChance();
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Before(org.junit.Before)

Aggregations

CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)28 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)24 Test (org.junit.Test)20 Invocation (org.apache.servicecomb.core.Invocation)12 Transport (org.apache.servicecomb.core.Transport)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Expectations (mockit.Expectations)10 Before (org.junit.Before)10 LoadBalancerStats (com.netflix.loadbalancer.LoadBalancerStats)8 MockUp (mockit.MockUp)8 SocketException (java.net.SocketException)6 Collections (java.util.Collections)6 Map (java.util.Map)6 ExecutionException (java.util.concurrent.ExecutionException)6 ExecutorService (java.util.concurrent.ExecutorService)6 Status (javax.ws.rs.core.Response.Status)6 Deencapsulation (mockit.Deencapsulation)6 Injectable (mockit.Injectable)6 Mock (mockit.Mock)6