Search in sources :

Example 66 with MicroserviceInstance

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

the class RefreshableMicroserviceCacheTest method refresh.

@Test
public void refresh() {
    ArrayList<MicroserviceInstance> instances = new ArrayList<>();
    findServiceInstancesOprHolder.value = params -> {
        Assert.assertEquals("consumerId", params[0]);
        Assert.assertEquals("app", params[1]);
        Assert.assertEquals("svc", params[2]);
        Assert.assertEquals("0.0.0.0+", params[3]);
        Assert.assertNull(params[4]);
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev0");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(instances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
    // at the beginning, no instances in cache
    List<MicroserviceInstance> cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(0, cachedInstances.size());
    Assert.assertNull(microserviceCache.getRevisionId());
    // find 1 instance from sc
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    instances.add(microserviceInstance);
    microserviceInstance.setInstanceId("instanceId00");
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(1, cachedInstances.size());
    MicroserviceInstance instance = cachedInstances.iterator().next();
    Assert.assertEquals("instanceId00", instance.getInstanceId());
    Assert.assertEquals("rev0", microserviceCache.getRevisionId());
    // 2nd time, find 2 instances, one of them is the old instance
    MicroserviceInstance microserviceInstance1 = new MicroserviceInstance();
    instances.add(microserviceInstance1);
    microserviceInstance1.setInstanceId("instanceId01");
    findServiceInstancesOprHolder.value = params -> {
        Assert.assertEquals("consumerId", params[0]);
        Assert.assertEquals("app", params[1]);
        Assert.assertEquals("svc", params[2]);
        Assert.assertEquals("0.0.0.0+", params[3]);
        Assert.assertEquals("rev0", params[4]);
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev1");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(instances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(2, cachedInstances.size());
    Assert.assertEquals("instanceId00", cachedInstances.get(0).getInstanceId());
    Assert.assertEquals("instanceId01", cachedInstances.get(1).getInstanceId());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) Test(org.junit.Test)

Example 67 with MicroserviceInstance

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

the class TestMicroserviceInstanceRegisterTask method setup.

@Before
public void setup() {
    ConfigUtil.installDynamicConfig();
    eventBus = new EventBus();
    taskList = new ArrayList<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onEvent(MicroserviceInstanceRegisterTask task) {
            taskList.add(task);
        }
    });
    microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceName("ms");
    microservice.setServiceId("serviceId");
    microservice.setInstance(new MicroserviceInstance());
    HealthCheck healthCheck = new HealthCheck();
    healthCheck.setMode(HealthCheckMode.HEARTBEAT);
    microservice.getInstance().setHealthCheck(healthCheck);
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) HealthCheck(org.apache.servicecomb.registry.api.registry.HealthCheck) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) EventBus(com.google.common.eventbus.EventBus) Subscribe(com.google.common.eventbus.Subscribe) Before(org.junit.Before)

Example 68 with MicroserviceInstance

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

the class TestMicroserviceInstanceRegisterTask method registerHostSuccess.

@Test
public void registerHostSuccess() {
    MicroserviceInstance instance = microservice.getInstance();
    new Expectations(RegistrationManager.class) {

        {
            RegistrationManager.getPublishHostName();
            result = "hostName";
        }
    };
    new Expectations(RegistryUtils.class) {

        {
            serviceRegistryConfig.isPreferIpAddress();
            result = false;
            serviceRegistryConfig.getHeartbeatInterval();
            result = 10;
            serviceRegistryConfig.getResendHeartBeatTimes();
            result = 20;
            srClient.registerMicroserviceInstance(instance);
            result = "instanceId";
        }
    };
    MicroserviceInstanceRegisterTask registerTask = new MicroserviceInstanceRegisterTask(eventBus, serviceRegistryConfig, srClient, microservice);
    registerTask.taskStatus = TaskStatus.READY;
    registerTask.run();
    Assert.assertEquals(true, registerTask.isRegistered());
    Assert.assertEquals("hostName", instance.getHostName());
    Assert.assertEquals("instanceId", instance.getInstanceId());
    Assert.assertEquals(10, instance.getHealthCheck().getInterval());
    Assert.assertEquals(20, instance.getHealthCheck().getTimes());
    Assert.assertEquals(1, taskList.size());
}
Also used : Expectations(mockit.Expectations) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 69 with MicroserviceInstance

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

the class TestMicroserviceRegisterTask method setup.

@Before
public void setup() {
    collector = new LogCollector();
    eventBus = new EventBus();
    taskList = new ArrayList<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onEvent(MicroserviceRegisterTask task) {
            taskList.add(task);
        }
    });
    microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceName("ms");
    microservice.setInstance(new MicroserviceInstance());
}
Also used : LogCollector(org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) EventBus(com.google.common.eventbus.EventBus) Subscribe(com.google.common.eventbus.Subscribe) Before(org.junit.Before)

Example 70 with MicroserviceInstance

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

the class TestLoadBalanceHandler2 method testConfigEndpoint.

@Test
public void testConfigEndpoint() {
    ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
    SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
    when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
    MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
    when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
    when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
    when(microserviceMeta.getAppId()).thenReturn("testApp");
    when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
    when(referenceConfig.getTransport()).thenReturn("rest");
    Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
    TransportManager transportManager = Mockito.mock(TransportManager.class);
    Transport transport = Mockito.mock(Transport.class);
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
    // set up data
    MicroserviceInstance myself = new MicroserviceInstance();
    MicroserviceInstance findInstance = new MicroserviceInstance();
    List<String> findEndpoint = new ArrayList<>();
    findEndpoint.add("rest://localhost:9092");
    findInstance.setEndpoints(findEndpoint);
    findInstance.setInstanceId("findInstance");
    Map<String, MicroserviceInstance> data = new HashMap<>();
    DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
    scbEngine.setTransportManager(transportManager);
    SCBEngine.getInstance().setTransportManager(transportManager);
    LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
    mockUpInstanceCacheManager(instanceCacheManager);
    when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
    when(transportManager.findTransport("rest")).thenReturn(transport);
    data.put("findInstance", findInstance);
    parent.cacheVersion(1);
    LoadbalanceHandler handler = new LoadbalanceHandler();
    try {
        handler.handle(invocation, asyncResp);
    } catch (Exception e) {
    }
    Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
    // reset
    invocation.setEndpoint(null);
    // success
    invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
    try {
        handler.handle(invocation, asyncResp);
    } catch (Exception e) {
    }
    Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
    // reset
    invocation.setEndpoint(null);
    // endpoint format is not correct
    invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
    try {
        handler.handle(invocation, asyncResp);
        Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Illegal character in scheme name"));
    }
    // reset
    invocation.setEndpoint(null);
    // transport is not find
    invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
    try {
        handler.handle(invocation, asyncResp);
        Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
    }
}
Also used : NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) InvocationRuntimeType(org.apache.servicecomb.core.definition.InvocationRuntimeType) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) TransportManager(org.apache.servicecomb.core.transport.TransportManager) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Transport(org.apache.servicecomb.core.Transport) 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