A more robust logic to wait for the app to start in the tests.

This commit is contained in:
Fabio Manganiello 2024-01-04 23:24:25 +01:00
parent 91a8fd3b56
commit 1e45aa5de9
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -4,12 +4,13 @@ import time
from threading import Thread from threading import Thread
import pytest import pytest
import requests
from platypush import Application, Config from platypush import Application, Config
from .utils import config_file, set_base_url from .utils import config_file, set_base_url
app_start_timeout = 5 app_start_timeout = 15
def clear_loggers(): def clear_loggers():
@ -25,6 +26,30 @@ def clear_loggers():
logger.removeHandler(handler) 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) @pytest.fixture(scope='session', autouse=True)
def app(): def app():
logging.info('Starting Platypush test service') logging.info('Starting Platypush test service')
@ -37,11 +62,7 @@ def app():
redis_port=16379, redis_port=16379,
) )
Thread(target=_app.run).start() Thread(target=_app.run).start()
logging.info( _wait_for_app(_app)
'Sleeping %d seconds while waiting for the daemon to start up',
app_start_timeout,
)
time.sleep(app_start_timeout)
yield _app yield _app
logging.info('Stopping Platypush test service') logging.info('Stopping Platypush test service')