Skip to content

Commit c7a7aca

Browse files
committed
test: Fix test_timeout_in_handler flakiness
Closes: #1652
1 parent cb7e310 commit c7a7aca

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

tests/unit/crawlers/_basic/test_basic_crawler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,12 +1315,12 @@ async def test_timeout_in_handler(sleep_type: str) -> None:
13151315
13161316
Handler should be able to time out even if the code causing the timeout is blocking sync code.
13171317
Crawler should attempt to retry it.
1318-
This test creates situation where the request handler times out twice, on third retry it does not time out."""
1318+
This test creates situation where the request handler times out twice, on third attempt it does not time out."""
13191319
# Test is skipped in older Python versions.
13201320
from asyncio import timeout # type:ignore[attr-defined] # noqa: PLC0415
13211321

13221322
handler_timeout = timedelta(seconds=1)
1323-
max_request_retries = 3
1323+
max_request_retries = 2 # Allows 3 total attempts: 1 initial + 2 retries
13241324
double_handler_timeout_s = handler_timeout.total_seconds() * 2
13251325
handler_sleep = iter([double_handler_timeout_s, double_handler_timeout_s, 0])
13261326

@@ -1343,11 +1343,11 @@ async def handler(context: BasicCrawlingContext) -> None:
13431343

13441344
# Timeout in pytest, because previous implementation would run crawler until following:
13451345
# "The request queue seems to be stuck for 300.0s, resetting internal state."
1346-
async with timeout(max_request_retries * double_handler_timeout_s):
1346+
async with timeout((max_request_retries + 1) * double_handler_timeout_s + 2):
13471347
await crawler.run(['https://a.placeholder.com'])
13481348

13491349
assert crawler.statistics.state.requests_finished == 1
1350-
assert mocked_handler_before_sleep.call_count == max_request_retries
1350+
assert mocked_handler_before_sleep.call_count == max_request_retries + 1 # Initial attempt + retries
13511351
assert mocked_handler_after_sleep.call_count == 1
13521352

13531353

0 commit comments

Comments
 (0)