Skip to content

Commit 553f63e

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

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/test_client_middleware_digest_auth.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,23 @@ 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()
13391336

1340-
# If this is taking more than 10ms, there's probably a performance/ReDoS issue.
1341-
assert (end - start) < 0.01
1337+
best_time = float("inf")
1338+
best_matches: list[tuple[str, str]] = []
1339+
1340+
for _ in range(5):
1341+
start = time.perf_counter()
1342+
matches = _HEADER_PAIRS_PATTERN.findall(value)
1343+
elapsed = time.perf_counter() - start
1344+
1345+
if elapsed < best_time:
1346+
best_time = elapsed
1347+
best_matches = matches
1348+
1349+
# Relaxed for CI/platform variability (e.g., macOS runners ~40-50ms observed)
1350+
assert (
1351+
best_time < 0.1
1352+
), f"Regex took {best_time * 1000:.1f}ms, expected <100ms - potential ReDoS issue"
1353+
13421354
# This example probably shouldn't produce a match either.
1343-
assert not matches
1355+
assert not best_matches

0 commit comments

Comments
 (0)