[CI] ensure test server ready by checking /healthz
# pylint: disable=redefined-outer-name
import time
+import urllib
+
import pytest
import bentoml
@@ -26,6 +28,21 @@ def image(tmpdir_factory):
client.images.remove(image.id)
+def _wait_until_ready(_host, timeout, check_interval=0.5):
+ start_time = time.time()
+ while time.time() - start_time < timeout:
+ try:
+ if (
+ urllib.request.urlopen(f'http://{_host}/healthz', timeout=0.1).status
+ == 200
+ ):
+ break
+ except Exception: # pylint:disable=broad-except
+ time.sleep(check_interval - 0.1)
+ else:
+ raise AssertionError(f"server didn't get ready in {timeout} seconds")
+
+
@pytest.fixture(autouse=True, scope='session')
def host(image, enable_microbatch):
import docker
@@ -38,6 +55,7 @@ def host(image, enable_microbatch):
command = "bentoml serve-gunicorn /bento --enable-microbatch --workers 1"
else:
command = "bentoml serve-gunicorn /bento --workers 1"
+ try:
container = client.containers.run(
command=command,
image=image.id,
@@ -46,7 +64,9 @@ def host(image, enable_microbatch):
ports={'5000/tcp': port},
detach=True,
)
- time.sleep(10)
- yield f"127.0.0.1:{port}"
+ _host = f"127.0.0.1:{port}"
+ _wait_until_ready(_host, 10)
+ yield _host
+ finally:
container.stop()
- time.sleep(1)
+ time.sleep(1) # make sure container stopped & deleted
