13
13
from pytest_simcore .helpers .utils_assert import assert_status
14
14
from servicelib .application import create_safe_application
15
15
from servicelib .application_keys import APP_CONFIG_KEY
16
+ from simcore_service_webserver .diagnostics import (
17
+ INCIDENTS_REGISTRY_KEY ,
18
+ setup_diagnostics ,
19
+ )
16
20
from simcore_service_webserver .resources import resources
17
21
from simcore_service_webserver .rest import setup_rest
18
22
from simcore_service_webserver .security import setup_security
@@ -31,6 +35,14 @@ def spec_dict(openapi_path):
31
35
def client (loop , aiohttp_unused_port , aiohttp_client , api_version_prefix ):
32
36
app = create_safe_application ()
33
37
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
+
34
46
server_kwargs = {"port" : aiohttp_unused_port (), "host" : "localhost" }
35
47
# fake config
36
48
app [APP_CONFIG_KEY ] = {
@@ -40,6 +52,9 @@ def client(loop, aiohttp_unused_port, aiohttp_client, api_version_prefix):
40
52
# activates only security+restAPI sub-modules
41
53
setup_security (app )
42
54
setup_rest (app )
55
+ setup_diagnostics (app , max_delay_allowed = MAX_DELAY_SECS_ALLOWED )
56
+
57
+ app .router .add_get ("/slow" , slow_handler )
43
58
44
59
cli = loop .run_until_complete (aiohttp_client (app , server_kwargs = server_kwargs ))
45
60
return cli
@@ -59,6 +74,19 @@ async def test_check_health(client, api_version_prefix):
59
74
assert data ["status" ] == "SERVICE_RUNNING"
60
75
61
76
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
+
62
90
FAKE = {
63
91
"path_value" : "one" ,
64
92
"query_value" : "two" ,
0 commit comments