Search in sources :

Example 41 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestIpPortManager method testCreateServiceRegistryCacheWithInstanceCache.

@Test
public void testCreateServiceRegistryCacheWithInstanceCache() {
    List<MicroserviceInstance> list = new ArrayList<>();
    MicroserviceInstance e1 = new MicroserviceInstance();
    list.add(e1);
    new MockUp<RegistryUtils>() {

        @Mock
        public List<MicroserviceInstance> findServiceInstance(String appId, String serviceName, String versionRule) {
            return list;
        }
    };
}
Also used : ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 42 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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 43 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestPropertiesLoader method testInstancePropertiesLoader.

@Test
public void testInstancePropertiesLoader() {
    Microservice microservice = LocalServiceRegistryFactory.createLocal().getMicroservice();
    MicroserviceInstance instance = microservice.getInstance();
    Map<String, String> expectedMap = new HashMap<>();
    expectedMap.put("key0", "value0");
    expectedMap.put("ek0", "ev0");
    Assert.assertEquals(expectedMap, instance.getProperties());
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 44 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestSimpleMicroserviceInstancePing method testPing.

@Test
public void testPing() throws IOException {
    SimpleMicroserviceInstancePing ping = new SimpleMicroserviceInstancePing();
    Assert.assertEquals(ping.getOrder(), 100);
    MicroserviceInstance instance = new MicroserviceInstance();
    List<String> endpoints = new ArrayList<>();
    ServerSocket ss = new ServerSocket(35677);
    endpoints.add("http://localhost:35677");
    instance.setEndpoints(endpoints);
    Assert.assertTrue(ping.ping(instance));
    MicroserviceInstance instance2 = new MicroserviceInstance();
    Assert.assertFalse(ping.ping(instance2));
    ss.close();
    Assert.assertFalse(ping.ping(instance));
}
Also used : ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServerSocket(java.net.ServerSocket) SimpleMicroserviceInstancePing(org.apache.servicecomb.registry.consumer.SimpleMicroserviceInstancePing) Test(org.junit.Test)

Example 45 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestSimpleMicroserviceInstancePing method testPing_more_endpoin.

@Test
public void testPing_more_endpoin() throws IOException {
    SimpleMicroserviceInstancePing ping = new SimpleMicroserviceInstancePing();
    MicroserviceInstance instance = new MicroserviceInstance();
    List<String> endpoints = new ArrayList<>();
    ServerSocket ss = new ServerSocket(35677);
    endpoints.add("http://localhost:35676");
    endpoints.add("http://localhost:35677");
    instance.setEndpoints(endpoints);
    Assert.assertTrue(ping.ping(instance));
    ss.close();
    Assert.assertFalse(ping.ping(instance));
}
Also used : ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServerSocket(java.net.ServerSocket) SimpleMicroserviceInstancePing(org.apache.servicecomb.registry.consumer.SimpleMicroserviceInstancePing) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12