2828)
2929
3030_logger = logging .getLogger (__name__ )
31+
32+
33+ async def _assert_check_log_message (
34+ caplog : pytest .LogCaptureFixture , expected_message : str
35+ ) -> None :
36+ """Helper to reliably check if a log message appears in caplog using tenacity."""
37+ async for attempt in AsyncRetrying (
38+ wait = wait_fixed (0.01 ),
39+ stop = stop_after_delay (2.0 ),
40+ reraise = True ,
41+ retry = retry_if_exception_type (AssertionError ),
42+ ):
43+ with attempt :
44+ assert expected_message in caplog .text
45+
46+
3147_ALL_LOGGING_LEVELS = [
3248 logging .CRITICAL ,
3349 logging .ERROR ,
@@ -437,18 +453,7 @@ async def test_setup_async_loggers_basic(
437453 test_logger = logging .getLogger ("test_async_logger" )
438454 test_logger .info ("Test async log message" )
439455
440- # Wait for log message to appear in caplog using tenacity
441- async for attempt in AsyncRetrying (
442- wait = wait_fixed (0.01 ),
443- stop = stop_after_delay (2.0 ),
444- reraise = True ,
445- retry = retry_if_exception_type (AssertionError ),
446- ):
447- with attempt :
448- assert "Test async log message" in caplog .text
449-
450- # Check that the log message was captured
451- assert "Test async log message" in caplog .text
456+ await _assert_check_log_message (caplog , "Test async log message" )
452457
453458
454459async def test_setup_async_loggers_with_filters (
@@ -478,17 +483,8 @@ async def test_setup_async_loggers_with_filters(
478483 test_logger .info ("This is an unfiltered message" )
479484 unfiltered_logger .info ("This is from unfiltered logger" )
480485
481- # Wait for log messages to appear in caplog using tenacity
482- async for attempt in AsyncRetrying (
483- wait = wait_fixed (0.01 ),
484- stop = stop_after_delay (2.0 ),
485- reraise = True ,
486- retry = retry_if_exception_type (AssertionError ),
487- ):
488- with attempt :
489- # Check that unfiltered messages were captured
490- assert "This is an unfiltered message" in caplog .text
491- assert "This is from unfiltered logger" in caplog .text
486+ await _assert_check_log_message (caplog , "This is an unfiltered message" )
487+ await _assert_check_log_message (caplog , "This is from unfiltered logger" )
492488
493489 # Check that filtered message was not captured
494490 assert "This is a filtered_message" not in caplog .text
@@ -515,17 +511,7 @@ async def test_setup_async_loggers_with_tracing_settings(
515511 test_logger = logging .getLogger ("test_tracing_logger" )
516512 test_logger .info ("Test message with tracing settings" )
517513
518- # Wait for log message to appear in caplog using tenacity
519- async for attempt in AsyncRetrying (
520- wait = wait_fixed (0.01 ),
521- stop = stop_after_delay (2.0 ),
522- reraise = True ,
523- retry = retry_if_exception_type (AssertionError ),
524- ):
525- with attempt :
526- assert "Test message with tracing settings" in caplog .text
527-
528- assert "Test message with tracing settings" in caplog .text
514+ await _assert_check_log_message (caplog , "Test message with tracing settings" )
529515
530516
531517async def test_setup_async_loggers_context_manager_cleanup (
@@ -545,15 +531,7 @@ async def test_setup_async_loggers_context_manager_cleanup(
545531 # During the context, handlers should be replaced
546532 test_logger .info ("Message during context" )
547533
548- # Wait for log message to appear in caplog using tenacity
549- async for attempt in AsyncRetrying (
550- wait = wait_fixed (0.01 ),
551- stop = stop_after_delay (2.0 ),
552- reraise = True ,
553- retry = retry_if_exception_type (AssertionError ),
554- ):
555- with attempt :
556- assert "Message during context" in caplog .text
534+ await _assert_check_log_message (caplog , "Message during context" )
557535
558536
559537async def test_setup_async_loggers_exception_handling (
@@ -577,15 +555,7 @@ def _raise_test_exception():
577555 test_logger = logging .getLogger ("test_exception_logger" )
578556 test_logger .info ("Message before exception" )
579557
580- # Wait for log message to appear in caplog using tenacity
581- async for attempt in AsyncRetrying (
582- wait = wait_fixed (0.01 ),
583- stop = stop_after_delay (2.0 ),
584- reraise = True ,
585- retry = retry_if_exception_type (AssertionError ),
586- ):
587- with attempt :
588- assert "Message before exception" in caplog .text
558+ await _assert_check_log_message (caplog , "Message before exception" )
589559
590560 # Raise an exception to test cleanup
591561 _raise_test_exception ()
0 commit comments