Skip to content

Commit 6b0bb83

Browse files
author
Pedro Crespo
committed
Tests diagnostics in webserver
1 parent 15ebbad commit 6b0bb83

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

services/web/server/tests/unit/test_rest.py

+28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from pytest_simcore.helpers.utils_assert import assert_status
1414
from servicelib.application import create_safe_application
1515
from servicelib.application_keys import APP_CONFIG_KEY
16+
from simcore_service_webserver.diagnostics import (
17+
INCIDENTS_REGISTRY_KEY,
18+
setup_diagnostics,
19+
)
1620
from simcore_service_webserver.resources import resources
1721
from simcore_service_webserver.rest import setup_rest
1822
from simcore_service_webserver.security import setup_security
@@ -31,6 +35,14 @@ def spec_dict(openapi_path):
3135
def client(loop, aiohttp_unused_port, aiohttp_client, api_version_prefix):
3236
app = create_safe_application()
3337

38+
MAX_DELAY_SECS_ALLOWED = 1 # secs
39+
40+
async def slow_handler(request: web.Request):
41+
import time
42+
43+
time.sleep(MAX_DELAY_SECS_ALLOWED * 1.1)
44+
raise web.HTTPOk()
45+
3446
server_kwargs = {"port": aiohttp_unused_port(), "host": "localhost"}
3547
# fake config
3648
app[APP_CONFIG_KEY] = {
@@ -40,6 +52,9 @@ def client(loop, aiohttp_unused_port, aiohttp_client, api_version_prefix):
4052
# activates only security+restAPI sub-modules
4153
setup_security(app)
4254
setup_rest(app)
55+
setup_diagnostics(app, max_delay_allowed=MAX_DELAY_SECS_ALLOWED)
56+
57+
app.router.add_get("/slow", slow_handler)
4358

4459
cli = loop.run_until_complete(aiohttp_client(app, server_kwargs=server_kwargs))
4560
return cli
@@ -59,6 +74,19 @@ async def test_check_health(client, api_version_prefix):
5974
assert data["status"] == "SERVICE_RUNNING"
6075

6176

77+
async def test_unhealthy_app(client, api_version_prefix):
78+
resp = await client.get(f"/{api_version_prefix}/")
79+
await assert_status(resp, web.HTTPOk)
80+
81+
resp = await client.get("/slow")
82+
await assert_status(resp, web.HTTPOk)
83+
84+
assert len(client.app[INCIDENTS_REGISTRY_KEY].slow_callbaks) >= 1
85+
86+
resp = await client.get(f"/{api_version_prefix}/")
87+
await assert_status(resp, web.HTTPServiceUnavailable)
88+
89+
6290
FAKE = {
6391
"path_value": "one",
6492
"query_value": "two",

0 commit comments

Comments
 (0)