11
11
from hypercorn .app_wrappers import ASGIWrapper
12
12
from hypercorn .config import Config
13
13
from hypercorn .trio .lifespan import Lifespan
14
+ from hypercorn .typing import ASGIReceiveCallable , ASGISendCallable , Scope
14
15
from hypercorn .utils import LifespanFailureError , LifespanTimeoutError
15
- from ..helpers import lifespan_failure , SlowLifespanFramework
16
+ from ..helpers import SlowLifespanFramework
16
17
17
18
18
19
@pytest .mark .trio
@@ -26,19 +27,23 @@ async def test_startup_timeout_error(nursery: trio._core._run.Nursery) -> None:
26
27
assert str (exc_info .value ).startswith ("Timeout whilst awaiting startup" )
27
28
28
29
30
+ async def _lifespan_failure (
31
+ scope : Scope , receive : ASGIReceiveCallable , send : ASGISendCallable
32
+ ) -> None :
33
+ async with trio .open_nursery ():
34
+ while True :
35
+ message = await receive ()
36
+ if message ["type" ] == "lifespan.startup" :
37
+ await send ({"type" : "lifespan.startup.failed" , "message" : "Failure" })
38
+ break
39
+
40
+
29
41
@pytest .mark .trio
30
42
async def test_startup_failure () -> None :
31
- lifespan = Lifespan (ASGIWrapper (lifespan_failure ), Config (), {})
32
- with pytest .raises (LifespanFailureError ) as exc_info :
33
- try :
34
- async with trio .open_nursery () as lifespan_nursery :
35
- await lifespan_nursery .start (lifespan .handle_lifespan )
36
- await lifespan .wait_for_startup ()
37
- except ExceptionGroup as exception :
38
- target_exception = exception
39
- if len (exception .exceptions ) == 1 :
40
- target_exception = exception .exceptions [0 ]
41
-
42
- raise target_exception .with_traceback (target_exception .__traceback__ )
43
-
44
- assert str (exc_info .value ) == "Lifespan failure in startup. 'Failure'"
43
+ lifespan = Lifespan (ASGIWrapper (_lifespan_failure ), Config (), {})
44
+ try :
45
+ async with trio .open_nursery () as lifespan_nursery :
46
+ await lifespan_nursery .start (lifespan .handle_lifespan )
47
+ await lifespan .wait_for_startup ()
48
+ except ExceptionGroup as error :
49
+ assert error .subgroup (LifespanFailureError ) is not None
0 commit comments