Skip to content

Commit

Permalink
refactor: Healthz endpoints refactor (#143)
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 authored Aug 28, 2024
1 parent 75ce0b3 commit 3c95691
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 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
6 changes: 3 additions & 3 deletions server/src/test/resources/smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ if ! ./get-block.sh 1 > get-block.log 2>&1; then
fi
echo "get-block.sh executed successfully."

# 5. Call the endpoints /health/liveness and /health/readiness
# 5. Call the endpoints /health/livez and /health/readyz
SERVER_URL="http://localhost:8080"
LIVENESS_ENDPOINT="/healthz/liveness"
READINESS_ENDPOINT="/healthz/readiness"
LIVENESS_ENDPOINT="/healthz/livez"
READINESS_ENDPOINT="/healthz/readyz"

if ! curl -f $SERVER_URL$LIVENESS_ENDPOINT; then
echo "$LIVENESS_ENDPOINT failed."
Expand Down

0 comments on commit 3c95691

Please sign in to comment.