Skip to content

Commit

Permalink
Refactored HealthService to use standard names for the liveness and r…
Browse files Browse the repository at this point in the history
…eadiness endpoints to livez and readyz

Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Aug 27, 2024
1 parent 75ce0b3 commit 06f10be
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions charts/hedera-block-node/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ blockNode:
PRIVATE_KEY: "fake_private_key"
health:
readiness:
endpoint: "/healthz/readiness"
endpoint: "/healthz/readyz"
liveness:
endpoint: "/healthz/liveness"
endpoint: "/healthz/livez"
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface HealthService extends HttpService {
* @param req the server request
* @param res the server response
*/
void handleLiveness(@NonNull final ServerRequest req, @NonNull final ServerResponse res);
void handleLivez(@NonNull final ServerRequest req, @NonNull final ServerResponse res);

/**
* Handles the request for readiness endpoint, that it most be defined on routing
Expand All @@ -46,5 +46,5 @@ public interface HealthService extends HttpService {
* @param req the server request
* @param res the server response
*/
void handleReadiness(@NonNull final ServerRequest req, @NonNull final ServerResponse res);
void handleReadyz(@NonNull final ServerRequest req, @NonNull final ServerResponse res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
@Singleton
public class HealthServiceImpl implements HealthService {

private static final String LIVENESS_PATH = "/liveness";
private static final String READINESS_PATH = "/readiness";
private static final String LIVEZ_PATH = "/livez";
private static final String READYZ_PATH = "/readyz";

private final ServiceStatus serviceStatus;

Expand All @@ -56,9 +56,7 @@ public String getHealthRootPath() {
*/
@Override
public void routing(@NonNull final HttpRules httpRules) {
httpRules
.get(LIVENESS_PATH, this::handleLiveness)
.get(READINESS_PATH, this::handleReadiness);
httpRules.get(LIVEZ_PATH, this::handleLivez).get(READYZ_PATH, this::handleReadyz);
}

/**
Expand All @@ -68,7 +66,7 @@ public void routing(@NonNull final HttpRules httpRules) {
* @param res the server response
*/
@Override
public final void handleLiveness(
public final void handleLivez(
@NonNull final ServerRequest req, @NonNull final ServerResponse res) {
if (serviceStatus.isRunning()) {
res.status(200).send("OK");
Expand All @@ -85,7 +83,7 @@ public final void handleLiveness(
* @param res the server response
*/
@Override
public final void handleReadiness(
public final void handleReadyz(
@NonNull final ServerRequest req, @NonNull final ServerResponse res) {
if (serviceStatus.isRunning()) {
res.status(200).send("OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
@ExtendWith(MockitoExtension.class)
class HealthServiceTest {

private static final String READINESS_PATH = "/readiness";
private static final String LIVENESS_PATH = "/liveness";
private static final String READINESS_PATH = "/readyz";
private static final String LIVENESS_PATH = "/livez";
private static final String HEALTH_PATH = "/healthz";

@Mock private ServiceStatus serviceStatus;
Expand All @@ -42,63 +42,63 @@ class HealthServiceTest {
@Mock ServerResponse serverResponse;

@Test
public void testHandleLiveness() {
public void testHandleLivez() {
// given
when(serviceStatus.isRunning()).thenReturn(true);
when(serverResponse.status(200)).thenReturn(serverResponse);
doNothing().when(serverResponse).send("OK");
HealthService healthService = new HealthServiceImpl(serviceStatus);

// when
healthService.handleLiveness(serverRequest, serverResponse);
healthService.handleLivez(serverRequest, serverResponse);

// then
verify(serverResponse, times(1)).status(200);
verify(serverResponse, times(1)).send("OK");
}

@Test
public void testHandleLiveness_notRunning() {
public void testHandleLivez_notRunning() {
// given
when(serviceStatus.isRunning()).thenReturn(false);
when(serverResponse.status(503)).thenReturn(serverResponse);
doNothing().when(serverResponse).send("Service is not running");
HealthService healthService = new HealthServiceImpl(serviceStatus);

// when
healthService.handleLiveness(serverRequest, serverResponse);
healthService.handleLivez(serverRequest, serverResponse);

// then
verify(serverResponse, times(1)).status(503);
verify(serverResponse, times(1)).send("Service is not running");
}

@Test
public void testHandleReadiness() {
public void testHandleReadyz() {
// given
when(serviceStatus.isRunning()).thenReturn(true);
when(serverResponse.status(200)).thenReturn(serverResponse);
doNothing().when(serverResponse).send("OK");
HealthService healthService = new HealthServiceImpl(serviceStatus);

// when
healthService.handleReadiness(serverRequest, serverResponse);
healthService.handleReadyz(serverRequest, serverResponse);

// then
verify(serverResponse, times(1)).status(200);
verify(serverResponse, times(1)).send("OK");
}

@Test
public void testHandleReadiness_notRunning() {
public void testHandleReadyz_notRunning() {
// given
when(serviceStatus.isRunning()).thenReturn(false);
when(serverResponse.status(503)).thenReturn(serverResponse);
doNothing().when(serverResponse).send("Service is not running");
HealthService healthService = new HealthServiceImpl(serviceStatus);

// when
healthService.handleReadiness(serverRequest, serverResponse);
healthService.handleReadyz(serverRequest, serverResponse);

// then
verify(serverResponse, times(1)).status(503);
Expand Down

0 comments on commit 06f10be

Please sign in to comment.