Search in sources :

Example 26 with MicroserviceInstances

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

the class TestServiceRegistryClientImpl method findServiceInstances_microserviceNotExist.

@Test
public void findServiceInstances_microserviceNotExist() {
    HttpClientResponse response = new MockUp<HttpClientResponse>() {

        @Mock
        int statusCode() {
            return 400;
        }

        @Mock
        HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) {
            Buffer bodyBuffer = Buffer.buffer("{\"errorCode\":\"400012\"}");
            bodyHandler.handle(bodyBuffer);
            return null;
        }
    }.getMockInstance();
    RestResponse restResponse = new RestResponse(null, response);
    new MockUp<RestClientUtil>() {

        @Mock
        void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
            Assert.assertEquals("appId=appId&global=true&serviceName=serviceName&version=0.0.0.0%2B", requestContext.getParams().getQueryParams());
            restResponse.setRequestContext(requestContext);
            responseHandler.handle(restResponse);
        }
    };
    MicroserviceInstances microserviceInstances = oClient.findServiceInstances("consumerId", "appId", "serviceName", DefinitionConst.VERSION_RULE_ALL, null);
    Assert.assertTrue(microserviceInstances.isMicroserviceNotExist());
    Assert.assertFalse(microserviceInstances.isNeedRefresh());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientResponse(io.vertx.core.http.HttpClientResponse) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) Handler(io.vertx.core.Handler) MockUp(mockit.MockUp) Mock(mockit.Mock) Test(org.junit.Test)

Example 27 with MicroserviceInstances

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

the class LocalServiceRegistryClientImplTest method testLoadRegistryFile.

@Test
public void testLoadRegistryFile() {
    Assert.assertNotNull(registryClient);
    Assert.assertThat(registryClient.getAllMicroservices().size(), Is.is(2));
    List<MicroserviceInstance> m = registryClient.findServiceInstance("", "default", "ms2", DefinitionConst.VERSION_RULE_ALL);
    Assert.assertEquals(1, m.size());
    MicroserviceInstances microserviceInstances = registryClient.findServiceInstances("", "default", "ms2", DefinitionConst.VERSION_RULE_ALL, null);
    List<MicroserviceInstance> mi = microserviceInstances.getInstancesResponse().getInstances();
    Assert.assertEquals(1, mi.size());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 28 with MicroserviceInstances

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

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 29 with MicroserviceInstances

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

the class TestInstanceCacheCheckerMock method createFindServiceInstancesResult.

private Holder<MicroserviceInstances> createFindServiceInstancesResult() {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    microserviceInstances.setNeedRefresh(true);
    microserviceInstances.setRevision("first");
    FindInstancesResponse findInstancesResponse = new FindInstancesResponse();
    findInstancesResponse.setInstances(new ArrayList<>());
    microserviceInstances.setInstancesResponse(findInstancesResponse);
    Holder<MicroserviceInstances> findHolder = new Holder<>();
    findHolder.value = microserviceInstances;
    return findHolder;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) Holder(org.apache.servicecomb.foundation.common.Holder) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse)

Example 30 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances 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)

Aggregations

MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)52 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)34 Test (org.junit.Test)22 FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)18 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)12 ArrayList (java.util.ArrayList)8 MockUp (mockit.MockUp)8 Before (org.junit.Before)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Enumeration (java.util.Enumeration)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2