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]
branch = True
parallel = True
command_line = -m pytest
concurrency = multiprocessing
omit =
*/site-packages/*
*/distutils/*

View File

@ -1,8 +1,10 @@
import socket
from multiprocessing import Process
from time import sleep
import pytest
import requests
from funcy import retry, complement
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}
@pytest.fixture(autouse=True)
def server_process(server, host_and_port):
@retry(tries=5, timeout=1)
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():
return Process(target=run_prediction_server, kwargs={"app": server, **host_and_port})
server = get_server_process()
server.start()
yield
if server_ready(url):
yield
server.terminate()
@ -71,6 +83,4 @@ def test_server_health_check(url):
def test_server_ready_check(url):
response = requests.get(f"{url}/ready")
response.raise_for_status()
assert response.status_code == 200
assert server_ready(url)