Search in sources :

Example 46 with MicroserviceInstance

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

the class TestInstanceCacheCheckerMock method check_findInstances_cacheNotMatch.

@Test
public void check_findInstances_cacheNotMatch() {
    Holder<MicroserviceInstances> findHolder = createFindServiceInstancesResult();
    new MockUp<RegistryUtils>() {

        @Mock
        MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule) {
            return findHolder.value;
        }
    };
    registerMicroservice(appId, microserviceName);
    MicroserviceVersions microserviceVersions = DiscoveryManager.INSTANCE.getAppManager().getOrCreateMicroserviceVersions(appId, microserviceName);
    microserviceVersions.setRevision("first");
    microserviceVersions.getOrCreateMicroserviceVersionRule(DefinitionConst.VERSION_RULE_ALL);
    Holder<MicroserviceInstances> newFindHolder = createFindServiceInstancesResult();
    newFindHolder.value.getInstancesResponse().getInstances().add(new MicroserviceInstance());
    findHolder.value = newFindHolder.value;
    InstanceCacheSummary instanceCacheSummary = checker.check();
    InstanceCacheResult instanceCacheResult = new InstanceCacheResult();
    instanceCacheResult.setAppId(appId);
    instanceCacheResult.setMicroserviceName(microserviceName);
    instanceCacheResult.setStatus(Status.ABNORMAL);
    instanceCacheResult.setDetail("instance cache not match");
    instanceCacheResult.setPulledInstances(new ArrayList<>());
    expectedSummary.getProducers().add(instanceCacheResult);
    expectedSummary.setStatus(Status.ABNORMAL);
    Assert.assertEquals(Json.encode(expectedSummary), Json.encode(instanceCacheSummary));
    Assert.assertNull(microserviceVersions.getRevision());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) MockUp(mockit.MockUp) MicroserviceVersions(org.apache.servicecomb.registry.consumer.MicroserviceVersions) Test(org.junit.Test)

Example 47 with MicroserviceInstance

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

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 48 with MicroserviceInstance

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

the class RefreshableMicroserviceCacheTest method refresh_service_no_change.

@Test
public void refresh_service_no_change() {
    findServiceInstancesOprHolder.value = params -> {
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setMicroserviceNotExist(false);
        microserviceInstances.setNeedRefresh(false);
        return microserviceInstances;
    };
    List<MicroserviceInstance> oldInstanceList = microserviceCache.getInstances();
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.NO_CHANGE, microserviceCache.getStatus());
    Assert.assertSame(oldInstanceList, microserviceCache.getInstances());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 49 with MicroserviceInstance

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

the class RefreshableMicroserviceCacheTest method refresh_service_not_exist.

@Test
public void refresh_service_not_exist() {
    findServiceInstancesOprHolder.value = params -> {
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setMicroserviceNotExist(true);
        return microserviceInstances;
    };
    List<MicroserviceInstance> oldInstanceList = microserviceCache.getInstances();
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.SERVICE_NOT_FOUND, microserviceCache.getStatus());
    Assert.assertSame(oldInstanceList, microserviceCache.getInstances());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 50 with MicroserviceInstance

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

the class RefreshableMicroserviceCacheTest method refresh_empty_instance_protection_disabled.

@Test
public void refresh_empty_instance_protection_disabled() {
    microserviceCache.instances = new ArrayList<>();
    MicroserviceInstance instance0 = new MicroserviceInstance();
    instance0.setInstanceId("instanceId0");
    microserviceCache.instances.add(instance0);
    pulledInstances = new ArrayList<>();
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    Assert.assertEquals(0, microserviceCache.getInstances().size());
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) 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