forked from encode/uvicorn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_lifespan.py
247 lines (195 loc) · 7.21 KB
/
test_lifespan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import asyncio
import pytest
from uvicorn.config import Config
from uvicorn.lifespan.off import LifespanOff
from uvicorn.lifespan.on import LifespanOn
def test_lifespan_on():
startup_complete = False
shutdown_complete = False
async def app(scope, receive, send):
nonlocal startup_complete, shutdown_complete
message = await receive()
assert message["type"] == "lifespan.startup"
startup_complete = True
await send({"type": "lifespan.startup.complete"})
message = await receive()
assert message["type"] == "lifespan.shutdown"
shutdown_complete = True
await send({"type": "lifespan.shutdown.complete"})
async def test():
config = Config(app=app, lifespan="on")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
def test_lifespan_off():
async def app(scope, receive, send):
pass # pragma: no cover
async def test():
config = Config(app=app, lifespan="off")
lifespan = LifespanOff(config)
await lifespan.startup()
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
def test_lifespan_auto():
startup_complete = False
shutdown_complete = False
async def app(scope, receive, send):
nonlocal startup_complete, shutdown_complete
message = await receive()
assert message["type"] == "lifespan.startup"
startup_complete = True
await send({"type": "lifespan.startup.complete"})
message = await receive()
assert message["type"] == "lifespan.shutdown"
shutdown_complete = True
await send({"type": "lifespan.shutdown.complete"})
async def test():
config = Config(app=app, lifespan="auto")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
def test_lifespan_auto_with_error():
async def app(scope, receive, send):
assert scope["type"] == "http"
async def test():
config = Config(app=app, lifespan="auto")
lifespan = LifespanOn(config)
await lifespan.startup()
assert lifespan.error_occured
assert not lifespan.should_exit
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
def test_lifespan_on_with_error():
async def app(scope, receive, send):
if scope["type"] != "http":
raise RuntimeError()
async def test():
config = Config(app=app, lifespan="on")
lifespan = LifespanOn(config)
await lifespan.startup()
assert lifespan.error_occured
assert lifespan.should_exit
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
@pytest.mark.parametrize("mode", ("auto", "on"))
@pytest.mark.parametrize("raise_exception", (True, False))
def test_lifespan_with_failed_startup(mode, raise_exception, caplog):
async def app(scope, receive, send):
message = await receive()
assert message["type"] == "lifespan.startup"
await send(
{"type": "lifespan.startup.failed", "message": "the lifespan event failed"}
)
if raise_exception:
# App should be able to re-raise an exception if startup failed.
raise RuntimeError()
async def test():
config = Config(app=app, lifespan=mode)
lifespan = LifespanOn(config)
await lifespan.startup()
assert lifespan.startup_failed
assert lifespan.error_occured is raise_exception
assert lifespan.should_exit
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
error_messages = [
record.message
for record in caplog.records
if record.name == "uvicorn.error" and record.levelname == "ERROR"
]
assert "the lifespan event failed" in error_messages.pop(0)
assert "Application startup failed. Exiting." in error_messages.pop(0)
def test_lifespan_scope_asgi3app():
async def asgi3app(scope, receive, send):
assert scope == {
"type": "lifespan",
"asgi": {"version": "3.0", "spec_version": "2.0"},
}
async def test():
config = Config(app=asgi3app, lifespan="on")
lifespan = LifespanOn(config)
await lifespan.startup()
assert not lifespan.startup_failed
assert not lifespan.error_occured
assert not lifespan.should_exit
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
def test_lifespan_scope_asgi2app():
def asgi2app(scope):
assert scope == {
"type": "lifespan",
"asgi": {"version": "2.0", "spec_version": "2.0"},
}
async def asgi(receive, send):
pass
return asgi
async def test():
config = Config(app=asgi2app, lifespan="on")
lifespan = LifespanOn(config)
await lifespan.startup()
await lifespan.shutdown()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
loop.close()
@pytest.mark.parametrize("mode", ("auto", "on"))
@pytest.mark.parametrize("raise_exception", (True, False))
def test_lifespan_with_failed_shutdown(mode, raise_exception, caplog):
async def app(scope, receive, send):
message = await receive()
assert message["type"] == "lifespan.startup"
await send({"type": "lifespan.startup.complete"})
message = await receive()
assert message["type"] == "lifespan.shutdown"
await send(
{"type": "lifespan.shutdown.failed", "message": "the lifespan event failed"}
)
if raise_exception:
# App should be able to re-raise an exception if startup failed.
raise RuntimeError()
async def test():
config = Config(app=app, lifespan=mode)
lifespan = LifespanOn(config)
await lifespan.startup()
assert not lifespan.startup_failed
await lifespan.shutdown()
assert lifespan.shutdown_failed
assert lifespan.error_occured is raise_exception
assert lifespan.should_exit
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
error_messages = [
record.message
for record in caplog.records
if record.name == "uvicorn.error" and record.levelname == "ERROR"
]
assert "the lifespan event failed" in error_messages.pop(0)
assert "Application shutdown failed. Exiting." in error_messages.pop(0)
loop.close()