Skip to content

Commit

Permalink
test: shorten wait times for readiness probe pytest
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <niladri.halder26@gmail.com>
  • Loading branch information
niladrih committed Dec 7, 2024
1 parent f6e9145 commit abe3e25
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
8 changes: 4 additions & 4 deletions tests/bdd/features/health_probes/readiness_probe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Feature: Readiness Probe

Background:
Given a running agent-core service
And a running REST service with "--core-health-freq" set to "4s"
And a running REST service with "--core-health-freq" set to "800ms"

Scenario: The REST API /ready service should not update its readiness status more than once in 4s
Scenario: The REST API /ready service should not update its readiness status more than once in 800ms
Given agent-core service is available
And the REST service returns a 200 status code for an HTTP GET request to the /ready endpoint
When the agent-core service is brought down forcefully
Then the REST service returns 200 for /ready endpoint for 2 more second
And after a delay of 4s the REST service returns 503 for /ready endpoint for the following 5s
Then the REST service returns 200 for /ready endpoint for 400 more millisecond
And after a delay of 800ms the REST service returns 503 for /ready endpoint for the following 1s
63 changes: 42 additions & 21 deletions tests/bdd/features/health_probes/test_readiness_probe.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
"""Readiness Probe feature tests."""

import logging
import time

import pytest
import requests
from common.deployer import Deployer
from common.docker import Docker
from pytest_bdd import given, scenario, then, when
from requests import get as http_get
from retrying import retry

logger = logging.getLogger(__name__)

READINESS_API_ENDPOINT = "http://localhost:8081/ready"


def ready_http_get(context_msg_prefix: str):
try:
response = requests.get(READINESS_API_ENDPOINT, timeout=(0.003, 0.010))
logging.info(
f"{context_msg_prefix}: response.status_code: {response.status_code}"
)
return response
except requests.exceptions.Timeout:
logging.error(f"{context_msg_prefix}: the request timed out")
except requests.exceptions.RequestException as e:
logging.error(f"{context_msg_prefix}: an error occurred: {e}")


@pytest.fixture(scope="module")
def setup():
Deployer.start(io_engines=1, rest_core_health_freq="4s")
Deployer.start(io_engines=1, rest_core_health_freq="800ms", request_timeout="50ms")
yield
Deployer.stop()


@scenario(
"readiness_probe.feature",
"The REST API /ready service should not update its readiness status more than once in 4s",
"The REST API /ready service should not update its readiness status more than once in 800ms",
)
def test_the_rest_api_ready_service_should_not_update_its_readiness_status_more_than_once_in_4s(
def test_the_rest_api_ready_service_should_not_update_its_readiness_status_more_than_once_in_800ms(
setup,
):
"""The REST API /ready service should not update its readiness status more than once in 4s."""
"""The REST API /ready service should not update its readiness status more than once in 800ms."""


@given('a running REST service with "--core-health-freq" set to "4s"')
@given('a running REST service with "--core-health-freq" set to "800ms"')
def a_running_rest_service(setup):
"""a running REST service with "--core-health-freq" set to "4s"."""
"""a running REST service with "--core-health-freq" set to "800ms"."""


@given("a running agent-core service")
Expand All @@ -51,14 +67,15 @@ def the_rest_service_returns_a_200_status_code_for_an_http_get_request_to_the_re
setup,
):
"""the REST service returns a 200 status code for an HTTP GET request to the /ready endpoint."""
logging.info(f"Initial request: expected status: 200")

# 5 minute retry.
@retry(
stop_max_attempt_number=1500,
wait_fixed=200,
)
def rest_is_ready():
response = http_get(READINESS_API_ENDPOINT)
response = ready_http_get(context_msg_prefix="Initial request")
assert response.status_code == 200

rest_is_ready()
Expand All @@ -70,31 +87,35 @@ def the_agent_core_service_is_brought_down_forcefully(setup):
Docker.kill_container("core")


@then("the REST service returns 200 for /ready endpoint for 2 more second")
def the_rest_service_returns_200_for_ready_endpoint_for_2_more_second(setup):
"""the REST service returns 200 for /ready endpoint for 2 more second."""
@then("the REST service returns 200 for /ready endpoint for 400 more millisecond")
def the_rest_service_returns_200_for_ready_endpoint_for_400_more_millisecond(setup):
"""the REST service returns 200 for /ready endpoint for 400 more millisecond."""
logging.info(f"Request after killing core: expected status: 200")

start_time = time.time()
while time.time() - start_time < 2:
response = http_get(READINESS_API_ENDPOINT)
while time.time() - start_time < 0.4:
response = ready_http_get(context_msg_prefix="Request after killing core")
if response.status_code != 200:
raise ValueError(
"Expected Readiness probe to return 200 for this duration of 2s"
"Expected Readiness probe to return 200 for this duration of 400ms"
)


@then(
"after a delay of 4s the REST service returns 503 for /ready endpoint for the following 5s"
"after a delay of 800ms the REST service returns 503 for /ready endpoint for the following 1s"
)
def after_a_delay_of_4s_the_rest_service_returns_503_for_ready_endpoint_for_the_following_5s(
def after_a_delay_of_800ms_the_rest_service_returns_503_for_ready_endpoint_for_the_following_1s(
setup,
):
"""after a delay of 4s the REST service returns 503 for /ready endpoint for the following 5s."""
time.sleep(4)
"""after a delay of 800ms the REST service returns 503 for /ready endpoint for the following 1s."""
time.sleep(0.8)

logging.info(f"Request after cache refresh: expected status: 503")

start_time = time.time()
while time.time() - start_time < 5:
response = http_get(READINESS_API_ENDPOINT)
while time.time() - start_time < 1:
response = ready_http_get(context_msg_prefix="Request after cache refresh")
if response.status_code != 503:
raise ValueError(
"Expected Readiness probe to return 503 for this duration of 5s"
"Expected Readiness probe to return 503 for this duration of 1s"
)

0 comments on commit abe3e25

Please sign in to comment.