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;
}
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());
}
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());
}
}
Aggregations