A more robust logic to wait for the app to start in the tests.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Fabio Manganiello 2024-01-04 23:24:25 +01:00
parent 91a8fd3b56
commit 1e45aa5de9
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 27 additions and 6 deletions

View File

@ -4,12 +4,13 @@ import time
from threading import Thread
import pytest
import requests
from platypush import Application, Config
from .utils import config_file, set_base_url
app_start_timeout = 5
app_start_timeout = 15
def clear_loggers():
@ -25,6 +26,30 @@ def clear_loggers():
logger.removeHandler(handler)
def _wait_for_app(app: Application, timeout: int = app_start_timeout):
logging.info('Waiting for the app to start')
start_time = time.time()
http = None
success = False
while not http and time.time() - start_time < timeout:
http = (app.backends or {}).get('http')
time.sleep(1)
assert http, f'HTTP backend not started after {timeout} seconds'
while not success and time.time() - start_time < timeout:
try:
response = requests.get(f'http://localhost:{http.port}/')
response.raise_for_status()
success = True
except Exception as e:
logging.debug('App not ready yet: %s', e)
time.sleep(1)
assert success, f'App not ready after {timeout} seconds'
@pytest.fixture(scope='session', autouse=True)
def app():
logging.info('Starting Platypush test service')
@ -37,11 +62,7 @@ def app():
redis_port=16379,
)
Thread(target=_app.run).start()
logging.info(
'Sleeping %d seconds while waiting for the daemon to start up',
app_start_timeout,
)
time.sleep(app_start_timeout)
_wait_for_app(_app)
yield _app
logging.info('Stopping Platypush test service')