Skip to content

Commit d8f0ea7

Browse files
committed
Fix session service validator coroutine handling
1 parent 7357848 commit d8f0ea7

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/core/testing/interfaces.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ def validate_session_service(service: ISessionService) -> None:
6868
"properly configured MagicMock instead."
6969
)
7070

71-
if inspect.iscoroutinefunction(method):
72-
raise TypeError(
73-
f"Session service {type(service).__name__}.get_session is a coroutine "
74-
"function but should be synchronous. This will cause coroutine warnings."
75-
)
71+
is_coroutine = inspect.iscoroutinefunction(method)
7672

7773
try:
7874
result = method("test_session_id")
@@ -87,11 +83,25 @@ def validate_session_service(service: ISessionService) -> None:
8783
if inspect.isawaitable(result):
8884
close = getattr(result, "close", None)
8985
if callable(close):
90-
close()
86+
try:
87+
close()
88+
except Exception:
89+
if logger.isEnabledFor(logging.DEBUG):
90+
logger.debug(
91+
"Failed to close coroutine produced during validation",
92+
exc_info=True,
93+
)
94+
95+
if not is_coroutine:
96+
raise TypeError(
97+
f"Session service {type(service).__name__}.get_session() returns an "
98+
"awaitable but the method is not async. This will cause coroutine "
99+
"warnings."
100+
)
101+
elif is_coroutine:
91102
raise TypeError(
92-
f"Session service {type(service).__name__}.get_session() returns an "
93-
"awaitable but the method is not async. This will cause coroutine "
94-
"warnings."
103+
f"Session service {type(service).__name__}.get_session is async but "
104+
"returned a non-awaitable result when called."
95105
)
96106

97107
except (TypeError, AttributeError) as e:

tests/unit/core/testing/test_interfaces.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,16 @@ def test_validate_session_service_with_async_mock(self) -> None:
7272
TestServiceValidator.validate_session_service(mock_service)
7373

7474
def test_validate_session_service_with_coroutine(self) -> None:
75-
"""Test validation with a service that returns coroutine function (should raise TypeError)."""
75+
"""Test validation with a proper coroutine implementation."""
7676
mock_service = MagicMock(spec=ISessionService)
7777

78-
async def bad_get_session(session_id: str) -> Session:
78+
async def good_get_session(session_id: str) -> Session:
7979
return Session(session_id)
8080

81-
mock_service.get_session = bad_get_session
81+
mock_service.get_session = good_get_session
8282

83-
# Should raise exception - coroutine functions cause coroutine warnings
84-
with pytest.raises(
85-
TypeError, match="is a coroutine function but should be synchronous"
86-
):
87-
TestServiceValidator.validate_session_service(mock_service)
83+
# Should not raise any exception for well-behaved coroutine implementations
84+
TestServiceValidator.validate_session_service(mock_service)
8885

8986
def test_validate_sync_method_with_async_mock(self) -> None:
9087
"""Test validation of sync method that is AsyncMock."""

0 commit comments

Comments
 (0)