-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_asgi_wsgi.py
More file actions
430 lines (355 loc) · 14.8 KB
/
test_asgi_wsgi.py
File metadata and controls
430 lines (355 loc) · 14.8 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import os
import subprocess
import time
from importlib.metadata import version
from typing import Callable
import httpx
import psutil
import pytest
import redis
HYPERCORN_VERSION = tuple(map(int, version("hypercorn").split(".")))
def wait_for_server(url: str, timeout: int = 30, github_actions: bool = False) -> bool:
"""Wait for server to be ready with HTTP health check.
Args:
url: Server URL to check
timeout: Base timeout in seconds
github_actions: Whether running in GitHub Actions (uses longer timeout)
Returns:
True if server is ready, False if timeout
"""
max_timeout = timeout * 2 if github_actions else timeout
start_time = time.time()
backoff = 0.1
while time.time() - start_time < max_timeout:
try:
with httpx.Client(timeout=2.0) as client:
response = client.get(url)
if response.status_code in (200, 404): # Server responding
return True
except (httpx.ConnectError, httpx.TimeoutException, httpx.RequestError):
pass
time.sleep(backoff)
backoff = min(backoff * 1.5, 2.0) # Exponential backoff, max 2s
return False
def retry_request(func: Callable, max_retries: int = 3, backoff: float = 0.5) -> Callable:
"""Retry HTTP requests with exponential backoff.
Args:
func: Function to retry
max_retries: Maximum number of retry attempts
backoff: Initial backoff time in seconds
Returns:
Wrapped function with retry logic
"""
def wrapper(*args, **kwargs):
last_exception = None
current_backoff = backoff
for attempt in range(max_retries + 1):
try:
return func(*args, **kwargs)
except (
httpx.ConnectError,
httpx.TimeoutException,
httpx.RequestError,
) as e:
last_exception = e
if attempt < max_retries:
time.sleep(current_backoff)
current_backoff *= 2 # Exponential backoff
else:
raise last_exception from None
return None
return wrapper
def terminate_process(proc: subprocess.Popen, timeout: float = 5.0) -> None:
"""Safely terminate a subprocess with timeout and cleanup child processes.
First tries terminate(), then kill() if process doesn't exit within timeout.
Also attempts to kill any child processes to prevent orphaned processes.
This prevents hanging tests in Python 3.12+ where subprocess.wait() can hang.
:param proc: The subprocess to terminate.
:param timeout: Maximum time to wait for process to terminate (default: 5 seconds).
"""
if proc.poll() is not None:
return # Process already terminated
# First, try to kill child processes (uvicorn/gunicorn workers)
try:
parent = psutil.Process(proc.pid)
children = parent.children(recursive=True)
for child in children:
try:
child.terminate()
except psutil.NoSuchProcess:
pass
# Wait a bit for children to terminate
psutil.wait_procs(children, timeout=2)
# Kill any remaining children
for child in children:
try:
if child.is_running():
child.kill()
except psutil.NoSuchProcess:
pass
except (psutil.NoSuchProcess, psutil.AccessDenied):
# Process already gone or access denied, continue with basic termination
pass
proc.terminate()
try:
proc.wait(timeout=timeout)
except subprocess.TimeoutExpired:
# Force kill if terminate didn't work
proc.kill()
try:
proc.wait(timeout=timeout) # Wait for kill to complete with timeout
except subprocess.TimeoutExpired:
# If even kill didn't work, give up to prevent hanging
pass
class TestASGIUvicorn:
@pytest.fixture(scope="function")
def uvicorn_server(self):
# Clear Redis gate before starting test
try:
r = redis.Redis(host="localhost", port=6379, db=15, decode_responses=True)
# Clear the shared gate used by ASGI app
keys_to_delete = list(r.scan_iter(match="*asgi_shared_gate*"))
if keys_to_delete:
r.delete(*keys_to_delete)
except Exception:
pass # Ignore Redis cleanup errors
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
workers = "2" if github_actions else "4" # Reduce workers in GitHub Actions
proc = subprocess.Popen(
[
"uvicorn",
"tests.asgi_wsgi.asgi_app:app",
"--host",
"0.0.0.0",
"--port",
"8000",
"--workers",
workers,
]
)
# Wait for server to be ready with HTTP health check
server_url = "http://0.0.0.0:8000/"
if not wait_for_server(server_url, timeout=15, github_actions=github_actions):
terminate_process(proc)
pytest.fail("Uvicorn server failed to start within timeout")
yield
terminate_process(proc)
# Additional cleanup: kill any remaining uvicorn processes
try:
subprocess.run(
["pkill", "-f", "uvicorn.*tests.asgi_wsgi.asgi_app"], check=False, capture_output=True, timeout=5
)
except Exception:
pass # Ignore cleanup errors
@pytest.mark.parametrize(
("num_requests", "positive_case"),
[
# Positive case: number of requests within the limit - all responses should be 200
(4, True),
# Negative case: number of requests exceeds the limit - at least one 429 response is expected
(20, False),
],
)
def test_asgi_web_server_rate_limit(self, uvicorn_server, num_requests, positive_case):
responses = []
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
timeout = 10.0 if github_actions else 5.0
with httpx.Client(timeout=timeout) as client:
def make_request():
return client.get("http://0.0.0.0:8000/")
make_request_with_retry = retry_request(make_request, max_retries=3 if github_actions else 1, backoff=0.5)
for _ in range(num_requests):
response = make_request_with_retry()
responses.append(response.status_code)
time.sleep(0.1) # small delay between requests
if positive_case:
assert all(code == 200 for code in responses)
else:
assert any(code == 429 for code in responses)
class TestASGIHypercorn:
@pytest.mark.parametrize(
("use_no_daemon", "expected_to_fail"),
[
# Test daemon mode (should fail with daemon error)
(False, True),
# Test non-daemon mode (should work without daemon error)
pytest.param(
True,
False,
marks=pytest.mark.xfail(
HYPERCORN_VERSION >= (0, 18, 0), reason="daemon=false config added in Hypercorn 0.18.0+"
),
id="no_daemon_mode",
),
],
ids=["daemon_mode", None], # None because no_daemon_mode has its own id
)
def test_hypercorn_server_daemon_behavior(self, use_no_daemon, expected_to_fail):
"""Test Hypercorn daemon behavior with and without daemon=false config.
- daemon_mode: Should fail with daemon process error
- no_daemon_mode: Should work without daemon process error (if supported)
"""
# Apply conditional xfail based on version and parameters
if use_no_daemon and HYPERCORN_VERSION >= (0, 18, 0):
pytest.xfail("--no-daemon behavior may be unstable in Hypercorn 0.18.0+")
cmd = [
"hypercorn",
"tests.asgi_wsgi.asgi_app:app",
"--bind",
"0.0.0.0:8000",
"--workers",
"4",
]
if use_no_daemon:
# daemon=false config only available in Hypercorn 0.18.0+
if HYPERCORN_VERSION < (0, 18, 0):
pytest.skip("daemon config not available in Hypercorn < 0.18.0")
cmd.extend(["--config", "/dev/stdin"])
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
proc.stdin.write("daemon = false\n")
proc.stdin.close()
else:
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
time.sleep(2) # give the server time to start
stderr_output = proc.stderr.read()
terminate_process(proc)
daemon_error_present = "AssertionError: daemonic processes are not allowed to have children" in stderr_output
if expected_to_fail:
assert daemon_error_present, "Expected daemon process error but didn't find it"
else:
assert not daemon_error_present, f"Unexpected daemon process error: {stderr_output}"
@pytest.fixture(scope="function")
def hypercorn_server_no_daemon(self):
"""Hypercorn server fixture without daemon mode (default behavior in Hypercorn >=0.18.0)."""
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
workers = "2" if github_actions else "4" # Reduce workers in GitHub Actions
proc = subprocess.Popen(
[
"hypercorn",
"tests.asgi_wsgi.asgi_app:app",
"--bind",
"0.0.0.0:8001",
"--workers",
workers,
"--config",
"/dev/stdin",
],
stdin=subprocess.PIPE,
text=True,
)
proc.stdin.write("daemon = false\n")
proc.stdin.close()
# Wait for server to be ready with HTTP health check
server_url = "http://0.0.0.0:8001/"
if not wait_for_server(server_url, timeout=20, github_actions=github_actions):
terminate_process(proc)
pytest.fail("Hypercorn server failed to start within timeout")
yield
terminate_process(proc)
@pytest.mark.parametrize(
("num_requests", "positive_case"),
[
# Positive case: number of requests within the limit - all responses should be 200
(4, True),
# Negative case: number of requests exceeds the limit - at least one 429 response is expected
(20, False),
],
)
@pytest.mark.skipif(
HYPERCORN_VERSION < (0, 18, 0), reason="Hypercorn before 0.18.0 has no option to switch off daemon mode"
)
def test_hypercorn_no_daemon_rate_limit(self, hypercorn_server_no_daemon, num_requests, positive_case):
"""Test rate limiting with Hypercorn server using --no-daemon flag."""
responses = []
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
timeout = 10.0 if github_actions else 5.0
with httpx.Client(timeout=timeout) as client:
def make_request():
return client.get("http://0.0.0.0:8001/")
make_request_with_retry = retry_request(make_request, max_retries=3 if github_actions else 1, backoff=0.5)
for _ in range(num_requests):
response = make_request_with_retry()
responses.append(response.status_code)
time.sleep(0.1) # small delay between requests
if positive_case:
assert all(code == 200 for code in responses)
else:
assert any(code == 429 for code in responses)
class TestWSGI:
@pytest.fixture(scope="function")
def gunicorn_server(self):
# Clear Redis gate before starting test
try:
r = redis.Redis(host="localhost", port=6379, db=15, decode_responses=True)
# Clear the shared gate used by WSGI app
keys_to_delete = list(r.scan_iter(match="*wsgi_shared_gate*"))
if keys_to_delete:
r.delete(*keys_to_delete)
except Exception:
pass # Ignore Redis cleanup errors
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
workers = "2" if github_actions else "4" # Reduce workers in GitHub Actions
proc = subprocess.Popen(
[
"gunicorn",
"tests.asgi_wsgi.wsgi_app:app",
"--bind",
"0.0.0.0:8100",
"--workers",
workers,
]
)
# Wait for server to be ready with HTTP health check
server_url = "http://0.0.0.0:8100/"
if not wait_for_server(server_url, timeout=15, github_actions=github_actions):
terminate_process(proc)
pytest.fail("Gunicorn server failed to start within timeout")
# Additional delay to let workers fully initialize and synchronize
time.sleep(1.0)
yield
terminate_process(proc)
# Additional cleanup: kill any remaining gunicorn processes
try:
subprocess.run(
["pkill", "-f", "gunicorn.*tests.asgi_wsgi.wsgi_app"], check=False, capture_output=True, timeout=5
)
except Exception:
pass # Ignore cleanup errors
@pytest.mark.parametrize(
("num_requests", "positive_case"),
[
# Positive case: number of requests within the limit - all responses should be 200
(2, True), # Reduced to 2 for reliable positive case
# Negative case: number of requests exceeds the limit - at least one 429 response is expected
(15, False), # Should definitely trigger rate limits
],
)
def test_wsgi_web_server_rate_limit(self, gunicorn_server, num_requests, positive_case):
responses = []
github_actions = os.getenv("GITHUB_ACTIONS") == "true"
timeout = 10.0 if github_actions else 5.0
with httpx.Client(timeout=timeout) as client:
def make_request():
return client.get("http://0.0.0.0:8100/")
make_request_with_retry = retry_request(make_request, max_retries=3 if github_actions else 1, backoff=0.5)
for _ in range(num_requests):
response = make_request_with_retry()
responses.append(response.status_code)
time.sleep(0.1) # small delay between requests
if positive_case:
assert all(code == 200 for code in responses)
else:
assert any(code == 429 for code in responses)
if __name__ == "__main__":
pytest.main()