Merge in RR/image-prediction from image_representation_metadata to master
Squashed commit of the following:
commit bfe92b24a2959a72c0e913ef051476c01c285ad0
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu May 12 11:24:12 2022 +0200
updated comment
commit f5721560f3fda05a8ad45d0b5e406434204c1177
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu May 12 11:16:02 2022 +0200
unskip server predict test
commit 41d94199ede7d58427b9e9541605a94f962c3dc4
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu May 12 11:15:48 2022 +0200
added hash image encoder that produces representations by hashing
commit 84a8b0a290081616240c3876f8db8a1ae8592096
Merge: 1624ee4 6030f40
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu May 12 10:18:56 2022 +0200
Merge branch 'master' of ssh://git.iqser.com:2222/rr/image-prediction
commit 1624ee40376b84a4519025343f913120c464407a
Author: Matthias Bisping <Matthias.Bisping@iqser.com>
Date: Mon Apr 25 16:51:13 2022 +0200
Pull request #11: fixed assignment
Merge in RR/image-prediction from image_prediction_service_overhaul_xref_and_empty_result_fix_fix to master
Squashed commit of the following:
commit 7312e57d1127b081bfdc6e96311e8348d3f8110d
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Apr 25 16:45:12 2022 +0200
logging setup changed
commit 955e353d74f414ee2d57b234bdf84d32817d14bf
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Apr 25 16:37:52 2022 +0200
fixed assignment
103 lines
2.4 KiB
Python
103 lines
2.4 KiB
Python
import socket
|
|
from multiprocessing import Process
|
|
|
|
import pytest
|
|
import requests
|
|
from funcy import retry
|
|
from waitress import serve
|
|
|
|
from image_prediction.flask import make_prediction_server
|
|
from image_prediction.pipeline import load_pipeline
|
|
|
|
|
|
@pytest.fixture
|
|
def host():
|
|
return "0.0.0.0"
|
|
|
|
|
|
def get_free_port(host):
|
|
sock = socket.socket()
|
|
sock.bind((host, 0))
|
|
return sock.getsockname()[1]
|
|
|
|
|
|
@pytest.fixture
|
|
def port(host):
|
|
return get_free_port(host)
|
|
|
|
|
|
@pytest.fixture
|
|
def url(host, port):
|
|
return f"http://{host}:{port}"
|
|
|
|
|
|
@pytest.fixture(params=["dummy", "actual"])
|
|
def server_type(request):
|
|
return request.param
|
|
|
|
|
|
@pytest.fixture
|
|
def server(server_type):
|
|
if server_type == "dummy":
|
|
return make_prediction_server(lambda x: int(x.decode()) // 2)
|
|
|
|
elif server_type == "actual":
|
|
return make_prediction_server(lambda x: list(load_pipeline(verbose=False)(x)))
|
|
|
|
else:
|
|
raise ValueError(f"Unknown server type {server_type}.")
|
|
|
|
|
|
@pytest.fixture
|
|
def host_and_port(host, port):
|
|
return {"host": host, "port": 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=serve, kwargs={"app": server, **host_and_port})
|
|
|
|
server = get_server_process()
|
|
server.start()
|
|
|
|
if server_ready(url):
|
|
yield
|
|
|
|
server.kill()
|
|
server.join()
|
|
server.close()
|
|
|
|
|
|
@pytest.mark.parametrize("server_type", ["actual"])
|
|
def test_server_predict(url, real_pdf, real_expected_service_response):
|
|
response = requests.post(f"{url}/predict", data=real_pdf)
|
|
response.raise_for_status()
|
|
assert response.json() == real_expected_service_response
|
|
|
|
|
|
@pytest.mark.parametrize("server_type", ["dummy"])
|
|
def test_server_dummy_operation(url):
|
|
response = requests.post(f"{url}/predict", data=b"42")
|
|
response.raise_for_status()
|
|
assert response.json() == 21
|
|
|
|
|
|
@pytest.mark.parametrize("server_type", ["dummy"])
|
|
def test_server_health_check(url):
|
|
response = requests.get(f"{url}/health")
|
|
response.raise_for_status()
|
|
assert response.status_code == 200
|
|
|
|
|
|
@pytest.mark.parametrize("server_type", ["dummy"])
|
|
def test_server_ready_check(url):
|
|
assert server_ready(url)
|