Skip to content

Commit 026317a

Browse files
committed
use tenacity
1 parent 52ff049 commit 026317a

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

packages/service-library/tests/test_logging_utils.py

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# pylint:disable=redefined-outer-name
22
# pylint:disable=unused-argument
33

4-
import asyncio
54
import logging
65
from collections.abc import Iterable
76
from contextlib import suppress
@@ -21,6 +20,12 @@
2120
set_parent_module_log_level,
2221
setup_async_loggers,
2322
)
23+
from tenacity import (
24+
AsyncRetrying,
25+
retry_if_exception_type,
26+
stop_after_delay,
27+
wait_fixed,
28+
)
2429

2530
_logger = logging.getLogger(__name__)
2631
_ALL_LOGGING_LEVELS = [
@@ -432,8 +437,15 @@ async def test_setup_async_loggers_basic(
432437
test_logger = logging.getLogger("test_async_logger")
433438
test_logger.info("Test async log message")
434439

435-
# Give some time for async logging to process
436-
await asyncio.sleep(0.1)
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
437449

438450
# Check that the log message was captured
439451
assert "Test async log message" in caplog.text
@@ -466,8 +478,17 @@ async def test_setup_async_loggers_with_filters(
466478
test_logger.info("This is an unfiltered message")
467479
unfiltered_logger.info("This is from unfiltered logger")
468480

469-
# Give some time for async logging to process
470-
await asyncio.sleep(0.1)
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
471492

472493
# Check that filtered message was not captured
473494
assert "This is a filtered_message" not in caplog.text
@@ -494,8 +515,15 @@ async def test_setup_async_loggers_with_tracing_settings(
494515
test_logger = logging.getLogger("test_tracing_logger")
495516
test_logger.info("Test message with tracing settings")
496517

497-
# Give some time for async logging to process
498-
await asyncio.sleep(0.1)
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
499527

500528
assert "Test message with tracing settings" in caplog.text
501529

@@ -517,8 +545,15 @@ async def test_setup_async_loggers_context_manager_cleanup(
517545
# During the context, handlers should be replaced
518546
test_logger.info("Message during context")
519547

520-
# Give some time for async logging to process
521-
await asyncio.sleep(0.1)
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
522557

523558

524559
async def test_setup_async_loggers_exception_handling(
@@ -542,8 +577,15 @@ def _raise_test_exception():
542577
test_logger = logging.getLogger("test_exception_logger")
543578
test_logger.info("Message before exception")
544579

545-
# Give some time for async logging to process
546-
await asyncio.sleep(0.1)
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
547589

548590
# Raise an exception to test cleanup
549591
_raise_test_exception()

0 commit comments

Comments
 (0)