dynamic waiting for server to be ready in tests

This commit is contained in:
Matthias Bisping 2022-04-01 19:04:41 +02:00
parent 1a1ece1f95
commit c372529ee5
2 changed files with 18 additions and 6 deletions

View File

@ -2,6 +2,8 @@
[run] [run]
branch = True branch = True
parallel = True parallel = True
command_line = -m pytest
concurrency = multiprocessing
omit = omit =
*/site-packages/* */site-packages/*
*/distutils/* */distutils/*

View File

@ -1,8 +1,10 @@
import socket import socket
from multiprocessing import Process from multiprocessing import Process
from time import sleep
import pytest import pytest
import requests import requests
from funcy import retry, complement
from image_prediction.flask import make_prediction_server, run_prediction_server from image_prediction.flask import make_prediction_server, run_prediction_server
@ -47,14 +49,24 @@ def host_and_port(host, port, server):
return {"host": host, "port": port} return {"host": host, "port": port}
@pytest.fixture(autouse=True) @retry(tries=5, timeout=1)
def server_process(server, host_and_port): def server_ready(url):
response = requests.get(f"{url}/ready")
response.raise_for_status()
return response.status_code == 200
@pytest.fixture(autouse=True, scope="function")
def server_process(server, host_and_port, url):
def get_server_process(): def get_server_process():
return Process(target=run_prediction_server, kwargs={"app": server, **host_and_port}) return Process(target=run_prediction_server, kwargs={"app": server, **host_and_port})
server = get_server_process() server = get_server_process()
server.start() server.start()
yield
if server_ready(url):
yield
server.terminate() server.terminate()
@ -71,6 +83,4 @@ def test_server_health_check(url):
def test_server_ready_check(url): def test_server_ready_check(url):
response = requests.get(f"{url}/ready") assert server_ready(url)
response.raise_for_status()
assert response.status_code == 200