test: fix async mock in backpressure tests for coroutine compatibility

Co-authored-by: aider (openrouter/openai/o3-mini-high) <aider@aider.chat>
This commit is contained in:
Stanislav Hejny
2025-07-17 10:24:43 +01:00
parent 7f9c02e458
commit ef5a0363cd
+47 -22
View File
@@ -22,8 +22,9 @@ class TestAMQService(unittest.IsolatedAsyncioTestCase):
# Create the AMQService instance
self.service = AMQService(self.config, self.service_adapter)
# Mock the backpressure handler
# Mock the backpressure handler with AsyncMock for the coroutine method
self.service.backpressure_handler = MagicMock()
self.service.backpressure_handler.update_backpressure_value = AsyncMock()
self.service.backpressure_handler.loop = asyncio.get_event_loop()
async def test_backpressure_with_maximum(self):
@@ -31,33 +32,57 @@ class TestAMQService(unittest.IsolatedAsyncioTestCase):
# Call the backpressure method with a maximum value
self.service.backpressure(current_availability=50, maximum=200)
# Check that update_backpressure_value was called with the correct values
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
# Get the coroutine that was passed to run_coroutine_threadsafe
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
self.assertEqual(coro, (50, 200))
# Check that run_coroutine_threadsafe was called with the correct arguments
# We need to patch asyncio.run_coroutine_threadsafe to capture its arguments
with patch('asyncio.run_coroutine_threadsafe') as mock_run:
self.service.backpressure(current_availability=50, maximum=200)
mock_run.assert_called_once()
# The first argument should be the coroutine
coro_arg = mock_run.call_args[0][0]
# The second argument should be the loop
loop_arg = mock_run.call_args[0][1]
self.assertEqual(loop_arg, self.service.backpressure_handler.loop)
# For the coroutine, we can check its __qualname__ to verify it's the right method
self.assertEqual(
coro_arg.__qualname__,
'BackpressureHandler.update_backpressure_value'
)
# We can also verify the arguments passed to update_backpressure_value
self.service.backpressure_handler.update_backpressure_value.assert_called_with(
50, 200
)
async def test_backpressure_without_maximum(self):
"""Test backpressure method without a maximum value."""
# Call the backpressure method without a maximum value
self.service.backpressure(current_availability=50)
# Check that update_backpressure_value was called with the correct values
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
# Get the coroutine that was passed to run_coroutine_threadsafe
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
self.assertEqual(coro, (50, 100)) # Should use threshold_load from config
# Set a maximum_availability value
self.service.maximum_availability = 100
with patch('asyncio.run_coroutine_threadsafe') as mock_run:
# Call the backpressure method without a maximum value
self.service.backpressure(current_availability=50)
# Verify the handler was called with the right values
self.service.backpressure_handler.update_backpressure_value.assert_called_with(
50, 100
)
async def test_backpressure_with_negative_maximum(self):
"""Test backpressure method with a negative maximum value."""
# Call the backpressure method with a negative maximum value
self.service.backpressure(current_availability=50, maximum=-1)
# Check that update_backpressure_value was called with the correct values
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
# Get the coroutine that was passed to run_coroutine_threadsafe
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
self.assertEqual(coro, (50, 100)) # Should use threshold_load from config
# Set a maximum_availability value
self.service.maximum_availability = 100
with patch('asyncio.run_coroutine_threadsafe') as mock_run:
# Call the backpressure method with a negative maximum value
self.service.backpressure(current_availability=50, maximum=-1)
# Verify the handler was called with the right values
self.service.backpressure_handler.update_backpressure_value.assert_called_with(
50, 100
)
async def test_backpressure_no_handler(self):
"""Test backpressure method when no handler is available."""