Search in sources :

Example 11 with CacheEndpoint

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

the class TestIpPortManager method testGetAvailableAddress.

@Test
public void testGetAvailableAddress(@Injectable ServiceRegistryConfig config, @Injectable InstanceCacheManager cacheManager, @Injectable InstanceCache cache, @Injectable ClassificationAddress classificationAddress) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
        }
    };
    IpPortManager manager = new IpPortManager(config);
    manager.instanceCacheManager = cacheManager;
    IpPort address1 = manager.getAvailableAddress();
    // test initial
    Assert.assertEquals("127.0.0.1", address1.getHostOrIp());
    Assert.assertTrue(address1.getPort() == 9980 || address1.getPort() == 9981);
    // test getAvailableAddress()
    IpPort address2 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address2.getHostOrIp());
    if (address1.getPort() == 9980) {
        Assert.assertEquals(9981, address2.getPort());
    } else {
        Assert.assertEquals(9980, address2.getPort());
    }
    // test getAvailableAddress() when reaching the end
    IpPort address3 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address3.getHostOrIp());
    Assert.assertEquals(address1.getPort(), address3.getPort());
    // mock endpoint list
    Map<String, List<CacheEndpoint>> addresses = new HashMap<>();
    List<CacheEndpoint> instances = new ArrayList<>();
    instances.add(new CacheEndpoint("http://127.0.0.1:9982", null));
    addresses.put("rest", instances);
    ClassificationAddress classificationAddres = new ClassificationAddress(config, cacheManager);
    manager.classificationAddress = classificationAddres;
    new Expectations() {

        {
            cacheManager.getOrCreate("default", "SERVICECENTER", "latest");
            result = cache;
            cache.getOrCreateTransportMap();
            result = addresses;
        }
    };
    // test getAvailableAddress() when auto discovery is disabled
    IpPort address4 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
    if (address1.getPort() == 9980) {
        address4 = manager.getAvailableAddress();
    }
    Assert.assertEquals(9980, address4.getPort());
    IpPort address5 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
    Assert.assertEquals(9981, address5.getPort());
    // mock RegistrationManager.INSTANCE
    String instanceId = "e8a04b54cf2711e7b701286ed488fc20";
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setInstanceId(instanceId);
    Map<String, String> properties = new HashMap<>();
    microserviceInstance.setProperties(properties);
    new Expectations(RegistrationManager.INSTANCE) {

        {
            RegistrationManager.INSTANCE.getMicroserviceInstance();
            result = microserviceInstance;
        }
    };
    // test getAvailable address when auto discovery is enabled
    manager.initAutoDiscovery();
    manager.setAutoDiscoveryInited(true);
    IpPort address6 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address6.getHostOrIp());
    Assert.assertEquals(9982, address6.getPort());
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassificationAddress(org.apache.servicecomb.serviceregistry.refresh.ClassificationAddress) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 12 with CacheEndpoint

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

the class TestLoadbalanceHandler method send_failed.

@Test
public void send_failed(@Injectable LoadBalancer loadBalancer) {
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
    ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
    LoadBalancerStats stats = new LoadBalancerStats("test");
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
            loadBalancer.getLoadBalancerStats();
            result = stats;
        }
    };
    sendResponse = Response.consumerFailResp(new SocketException());
    Holder<Throwable> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = (Throwable) resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
    Assert.assertEquals("InvocationException: code=490;msg=CommonExceptionData [message=Unexpected consumer error, please check logs for details]", result.value.getMessage());
}
Also used : Expectations(mockit.Expectations) SCBEngine(org.apache.servicecomb.core.SCBEngine) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) MockUp(mockit.MockUp) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) Invocation(org.apache.servicecomb.core.Invocation) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Rule(org.junit.Rule) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) Assert(org.junit.Assert) Collections(java.util.Collections) Mocked(mockit.Mocked) SocketException(java.net.SocketException) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Holder(org.apache.servicecomb.foundation.common.Holder) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 13 with CacheEndpoint

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

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 14 with CacheEndpoint

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

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 15 with CacheEndpoint

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

the class TestServiceCombServer method testEqualsMethod.

@Test
public void testEqualsMethod() {
    Assert.assertFalse(cs.equals((Object) "abcd"));
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    ServiceCombServer other = new ServiceCombServer(null, transport, new CacheEndpoint("1234", instance1));
    Assert.assertFalse(cs.equals(other));
    MicroserviceInstance instance2 = new MicroserviceInstance();
    instance2.setInstanceId("123456");
    other = new ServiceCombServer(null, transport, new CacheEndpoint("abcd", instance2));
    Assert.assertTrue(cs.equals(other));
}
Also used : CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

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