Search in sources :

Example 6 with HeartbeatResponse

use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.

the class ServiceRegistryClientImpl method heartbeat.

@Override
public HeartbeatResponse heartbeat(String microserviceId, String microserviceInstanceId) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.INSTANCES_PATH).append("/").append(microserviceInstanceId).append(Const.HEARTBEAT_PATH);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.put(ipPort, url.toString(), new RequestParam(), syncHandler(countDownLatch, HttpClientResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            HeartbeatResponse response = new HeartbeatResponse();
            response.setMessage(holder.value.statusMessage());
            if (holder.value.statusCode() == Status.OK.getStatusCode()) {
                response.setOk(true);
                return response;
            }
            LOGGER.warn(holder.value.statusMessage());
            return response;
        }
    } catch (Exception e) {
        LOGGER.error("update microservice instance {}/{} heartbeat failed", microserviceId, microserviceInstanceId, e);
    }
    return null;
}
Also used : HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 7 with HeartbeatResponse

use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.

the class TestRegistry method testRegistryUtilsWithStubHeartbeat.

@Test
public void testRegistryUtilsWithStubHeartbeat(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
    HeartbeatResponse response = new HeartbeatResponse();
    response.setOk(true);
    response.setMessage("OK");
    new Expectations() {

        {
            oMockServiceRegistryClient.init();
            oMockServiceRegistryClient.registerMicroservice((Microservice) any);
            result = "sampleServiceID";
            oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
            result = "sampleInstanceID";
            oMockServiceRegistryClient.heartbeat(anyString, anyString);
            result = response;
            oMockServiceRegistryClient.unregisterMicroserviceInstance(anyString, anyString);
            result = false;
        }
    };
    RegistryUtils.setSrClient(oMockServiceRegistryClient);
    RegistryUtils.init();
    Assert.assertEquals(true, RegistryUtils.heartbeat().isOk());
    Assert.assertEquals(false, RegistryUtils.unregsiterInstance());
}
Also used : Expectations(mockit.Expectations) HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Test(org.junit.Test)

Example 8 with HeartbeatResponse

use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.

the class TestRegistry method testRegistryUtilsWithStubHeartbeatFailureException.

@Test
public void testRegistryUtilsWithStubHeartbeatFailureException(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
    HeartbeatResponse response = new HeartbeatResponse();
    response.setOk(true);
    response.setMessage("OK");
    try {
        new Expectations() {

            {
                oMockServiceRegistryClient.init();
                oMockServiceRegistryClient.registerMicroservice((Microservice) any);
                result = "sampleServiceID";
                oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
                result = "sampleInstanceID";
                oMockServiceRegistryClient.heartbeat(anyString, anyString);
                result = null;
            }
        };
        RegistryUtils.setSrClient(oMockServiceRegistryClient);
        RegistryUtils.init();
        new MockUp<Timer>() {

            @Mock
            public void sleep() throws TimerException {
                throw new TimerException();
            }
        };
        boolean validAssert = RegistryUtils.heartbeat().isOk();
        Assert.assertTrue(validAssert);
    } catch (Exception e) {
        Assert.assertEquals("java.lang.NullPointerException", e.getClass().getName());
    }
}
Also used : Expectations(mockit.Expectations) HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) TimerException(io.servicecomb.serviceregistry.utils.TimerException) MockUp(mockit.MockUp) TimerException(io.servicecomb.serviceregistry.utils.TimerException) Test(org.junit.Test)

Aggregations

HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)8 Expectations (mockit.Expectations)4 Test (org.junit.Test)4 TimerException (io.servicecomb.serviceregistry.utils.TimerException)3 ClientException (io.servicecomb.serviceregistry.client.ClientException)2 IpPort (io.servicecomb.foundation.common.net.IpPort)1 HealthCheck (io.servicecomb.serviceregistry.api.registry.HealthCheck)1 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)1 ServiceRegistryClientImpl (io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl)1 Timer (io.servicecomb.serviceregistry.utils.Timer)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Holder (javax.xml.ws.Holder)1 MockUp (mockit.MockUp)1