Skip to content

Commit

Permalink
Use backoff in wait scripts (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
a1d4r authored Apr 4, 2024
1 parent 3b1c093 commit e216f3a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
13 changes: 12 additions & 1 deletion async_api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions async_api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ coverage = "^7.4.4"
httpx = "^0.27.0"
polyfactory = "^2.15.0"
faker = "^24.4.0"
backoff = "^2.2.1"

[tool.black] # https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
target-version = ["py312"]
Expand Down
18 changes: 8 additions & 10 deletions async_api/tests/functional/utils/wait_for_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import time

import backoff
import httpx

from tests.functional.settings import settings
Expand All @@ -9,17 +9,15 @@
logger = logging.getLogger(__name__)


@backoff.on_exception(backoff.expo, httpx.HTTPError, max_time=60)
def check_api_health():
response = httpx.get(f"{settings.api_url}/health", timeout=1)
response.raise_for_status()


def wait_for_api():
logger.info("Waiting for API: %s", settings.api_url)
while True:
try:
response = httpx.get(f"{settings.api_url}/health", timeout=1)
except httpx.HTTPError:
time.sleep(1)
continue
if response.status_code == 200:
break
time.sleep(1)
check_api_health()
logger.info("API is ready")


Expand Down
20 changes: 13 additions & 7 deletions async_api/tests/functional/utils/wait_for_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import logging
import time

from elasticsearch import Elasticsearch
import backoff

from elastic_transport import TransportError
from elasticsearch import ApiError, Elasticsearch

from tests.functional.settings import settings
from tests.functional.utils.logger import setup_logger

logging.getLogger("elastic_transport.transport").setLevel(logging.CRITICAL)

logger = logging.getLogger(__name__)


def wait_for_elasticsearch():
@backoff.on_exception(backoff.expo, (ApiError, TransportError), max_time=60)
def check_elasticsearch_health():
es_client = Elasticsearch(settings.elasticsearch_host, request_timeout=1)
es_client.info()


def wait_for_elasticsearch():
logger.info("Waiting for elasticsearch: %s", settings.elasticsearch_host)
while True:
if es_client.ping():
break
time.sleep(1)
check_elasticsearch_health()
logger.info("Elasticsearch is ready")


Expand Down
15 changes: 9 additions & 6 deletions async_api/tests/functional/utils/wait_for_redis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import time

import backoff

from redis import Redis

Expand All @@ -9,13 +10,15 @@
logger = logging.getLogger(__name__)


def wait_for_redis():
@backoff.on_predicate(backoff.expo, max_time=60)
def check_redis_health():
redis = Redis.from_url(str(settings.redis_url), socket_timeout=1)
return redis.ping()


def wait_for_redis():
logger.info("Waiting for redis: %s", settings.redis_url)
while True:
if redis.ping():
break
time.sleep(1)
check_redis_health()
logger.info("Redis is ready")


Expand Down

0 comments on commit e216f3a

Please sign in to comment.