SIENTIAPDE-1975: Sonar fixes in the NATS consumer tests
- One call per pytest.raises block (S5778): build the guarded start() before the block and nest the patch outside it. - The NATS callbacks stay async on purpose: nats-py rejects callbacks that are not coroutine functions. Comment it and assert it in the callback test, so nobody drops the async keyword to please S7503. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -94,6 +94,8 @@ class NatsWriteConsumer:
|
|||||||
f'{traceback.format_exc()}'
|
f'{traceback.format_exc()}'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# These stay async with nothing to await: nats-py rejects callbacks that are not
|
||||||
|
# coroutine functions (InvalidCallbackTypeError on connect).
|
||||||
async def _on_error(self, e: Exception) -> None:
|
async def _on_error(self, e: Exception) -> None:
|
||||||
self.logger.warning(f'NATS error: {type(e).__name__} {e}')
|
self.logger.warning(f'NATS error: {type(e).__name__} {e}')
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import inspect
|
||||||
import json
|
import json
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
@@ -76,9 +77,12 @@ async def test_start_gives_up_after_the_connect_timeout():
|
|||||||
"""start() runs on the main loop: an unreachable NATS must not block it."""
|
"""start() runs on the main loop: an unreachable NATS must not block it."""
|
||||||
consumer = make_consumer(connect_timeout_s=0.1)
|
consumer = make_consumer(connect_timeout_s=0.1)
|
||||||
nc = _fake_nc(connect=AsyncMock(side_effect=_hang))
|
nc = _fake_nc(connect=AsyncMock(side_effect=_hang))
|
||||||
|
# Outer guard: fails the test instead of hanging it if start() ignores its timeout.
|
||||||
|
start = asyncio.wait_for(consumer.start(), timeout=5)
|
||||||
|
|
||||||
with patch(CLIENT, return_value=nc), pytest.raises(TimeoutError):
|
with patch(CLIENT, return_value=nc):
|
||||||
await asyncio.wait_for(consumer.start(), timeout=5)
|
with pytest.raises(TimeoutError):
|
||||||
|
await start
|
||||||
|
|
||||||
nc.close.assert_awaited_once()
|
nc.close.assert_awaited_once()
|
||||||
assert consumer.closed is True
|
assert consumer.closed is True
|
||||||
@@ -115,6 +119,9 @@ async def test_connection_events_are_logged_in_one_line():
|
|||||||
with patch(CLIENT, return_value=nc):
|
with patch(CLIENT, return_value=nc):
|
||||||
await consumer.start()
|
await consumer.start()
|
||||||
callbacks = nc.connect.await_args.kwargs
|
callbacks = nc.connect.await_args.kwargs
|
||||||
|
# nats-py raises InvalidCallbackTypeError on connect for a non-coroutine callback.
|
||||||
|
for name in ('error_cb', 'disconnected_cb', 'reconnected_cb'):
|
||||||
|
assert inspect.iscoroutinefunction(callbacks[name]), name
|
||||||
|
|
||||||
await callbacks['error_cb'](OSError('connection refused'))
|
await callbacks['error_cb'](OSError('connection refused'))
|
||||||
await callbacks['disconnected_cb']()
|
await callbacks['disconnected_cb']()
|
||||||
|
|||||||
Reference in New Issue
Block a user