Skip to content

Commit 6fae85d

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

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tests/test_client_middleware_digest_auth.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,17 @@ 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+
for _ in range(3):
1339+
start = time.perf_counter()
1340+
matches = _HEADER_PAIRS_PATTERN.findall(value)
1341+
elapsed = time.perf_counter() - start
1342+
if elapsed < best_time:
1343+
best_time = elapsed
13391344

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

0 commit comments

Comments
 (0)