Skip to content

Commit 2cda92f

Browse files
author
rodrigo.nogueira
committed
Fix flaky test_regex_performance timing test
1 parent 877749b commit 2cda92f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

tests/test_client_middleware_digest_auth.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,18 @@ async def handler(request: Request) -> Response:
13331333

13341334
def test_regex_performance() -> None:
13351335
value = "0" * 54773 + "\\0=a"
1336-
start = time.perf_counter()
1337-
matches = _HEADER_PAIRS_PATTERN.findall(value)
1338-
end = time.perf_counter()
1336+
1337+
best_time = float("inf")
1338+
matches: list[tuple[str, str]] = []
1339+
for _ in range(3):
1340+
start = time.perf_counter()
1341+
matches = _HEADER_PAIRS_PATTERN.findall(value)
1342+
elapsed = time.perf_counter() - start
1343+
if elapsed < best_time:
1344+
best_time = elapsed
13391345

13401346
# If this is taking more than 10ms, there's probably a performance/ReDoS issue.
1341-
assert (end - start) < 0.01
1347+
assert best_time < 0.1, f"Regex took {best_time*1000:.1f}ms, expected <100ms"
1348+
13421349
# This example probably shouldn't produce a match either.
13431350
assert not matches

0 commit comments

Comments
 (0)