@@ -1269,23 +1269,30 @@ async def test_lock_with_get_robots_txt_file_for_url(server_url: URL) -> None:
1269
1269
assert spy .call_count == 1
1270
1270
1271
1271
1272
- async def test_reduced_logs_from_timed_out_request_handler (
1273
- monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
1274
- ) -> None :
1272
+ async def test_reduced_logs_from_timed_out_request_handler (caplog : pytest .LogCaptureFixture ) -> None :
1275
1273
caplog .set_level (logging .INFO )
1276
- crawler = BasicCrawler (configure_logging = False , request_handler_timeout = timedelta (seconds = 1 ))
1274
+ crawler = BasicCrawler (
1275
+ configure_logging = False ,
1276
+ request_handler_timeout = timedelta (seconds = 1 ),
1277
+ )
1277
1278
1278
1279
@crawler .router .default_handler
1279
1280
async def handler (context : BasicCrawlingContext ) -> None :
1281
+ # Intentionally add a delay longer than the timeout to trigger the timeout mechanism
1280
1282
await asyncio .sleep (10 ) # INJECTED DELAY
1281
1283
1282
- await crawler .run ([Request .from_url ('http://a.com/' )])
1284
+ # Capture all logs from the 'crawlee' logger at INFO level or higher
1285
+ with caplog .at_level (logging .INFO , logger = 'crawlee' ):
1286
+ await crawler .run ([Request .from_url ('http://a.com/' )])
1283
1287
1288
+ # Check for the timeout message in any of the logs
1289
+ found_timeout_message = False
1284
1290
for record in caplog .records :
1285
- if record .funcName == '_handle_failed_request' :
1291
+ if record .message and 'timed out after 1.0 seconds' in record . message :
1286
1292
full_message = (record .message or '' ) + (record .exc_text or '' )
1287
1293
assert Counter (full_message )['\n ' ] < 10
1288
1294
assert '# INJECTED DELAY' in full_message
1295
+ found_timeout_message = True
1289
1296
break
1290
- else :
1291
- raise AssertionError ( 'Expected log message about request handler error was not found.' )
1297
+
1298
+ assert found_timeout_message , 'Expected log message about request handler error was not found.'
0 commit comments