From c372529ee59f39645e89c865ef4e22540da32277 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Fri, 1 Apr 2022 19:04:41 +0200 Subject: [PATCH] dynamic waiting for server to be ready in tests --- .coveragerc | 2 ++ test/integration_tests/server_test.py | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.coveragerc b/.coveragerc index c7e13f0..da550cf 100644 --- a/.coveragerc +++ b/.coveragerc @@ -2,6 +2,8 @@ [run] branch = True parallel = True +command_line = -m pytest +concurrency = multiprocessing omit = */site-packages/* */distutils/* diff --git a/test/integration_tests/server_test.py b/test/integration_tests/server_test.py index b049bd1..b0befaa 100644 --- a/test/integration_tests/server_test.py +++ b/test/integration_tests/server_test.py @@ -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)