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:
@@ -22,8 +22,9 @@ class TestAMQService(unittest.IsolatedAsyncioTestCase):
|
|||||||
# Create the AMQService instance
|
# Create the AMQService instance
|
||||||
self.service = AMQService(self.config, self.service_adapter)
|
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 = MagicMock()
|
||||||
|
self.service.backpressure_handler.update_backpressure_value = AsyncMock()
|
||||||
self.service.backpressure_handler.loop = asyncio.get_event_loop()
|
self.service.backpressure_handler.loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
async def test_backpressure_with_maximum(self):
|
async def test_backpressure_with_maximum(self):
|
||||||
@@ -31,33 +32,57 @@ class TestAMQService(unittest.IsolatedAsyncioTestCase):
|
|||||||
# Call the backpressure method with a maximum value
|
# Call the backpressure method with a maximum value
|
||||||
self.service.backpressure(current_availability=50, maximum=200)
|
self.service.backpressure(current_availability=50, maximum=200)
|
||||||
|
|
||||||
# Check that update_backpressure_value was called with the correct values
|
# Check that run_coroutine_threadsafe was called with the correct arguments
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
|
# We need to patch asyncio.run_coroutine_threadsafe to capture its arguments
|
||||||
# Get the coroutine that was passed to run_coroutine_threadsafe
|
with patch('asyncio.run_coroutine_threadsafe') as mock_run:
|
||||||
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
|
self.service.backpressure(current_availability=50, maximum=200)
|
||||||
self.assertEqual(coro, (50, 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):
|
async def test_backpressure_without_maximum(self):
|
||||||
"""Test backpressure method without a maximum value."""
|
"""Test backpressure method without a maximum value."""
|
||||||
|
# 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
|
# Call the backpressure method without a maximum value
|
||||||
self.service.backpressure(current_availability=50)
|
self.service.backpressure(current_availability=50)
|
||||||
|
|
||||||
# Check that update_backpressure_value was called with the correct values
|
# Verify the handler was called with the right values
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
|
self.service.backpressure_handler.update_backpressure_value.assert_called_with(
|
||||||
# Get the coroutine that was passed to run_coroutine_threadsafe
|
50, 100
|
||||||
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
|
)
|
||||||
self.assertEqual(coro, (50, 100)) # Should use threshold_load from config
|
|
||||||
|
|
||||||
async def test_backpressure_with_negative_maximum(self):
|
async def test_backpressure_with_negative_maximum(self):
|
||||||
"""Test backpressure method with a negative maximum value."""
|
"""Test backpressure method with a negative maximum value."""
|
||||||
|
# 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
|
# Call the backpressure method with a negative maximum value
|
||||||
self.service.backpressure(current_availability=50, maximum=-1)
|
self.service.backpressure(current_availability=50, maximum=-1)
|
||||||
|
|
||||||
# Check that update_backpressure_value was called with the correct values
|
# Verify the handler was called with the right values
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_once()
|
self.service.backpressure_handler.update_backpressure_value.assert_called_with(
|
||||||
# Get the coroutine that was passed to run_coroutine_threadsafe
|
50, 100
|
||||||
coro = self.service.backpressure_handler.update_backpressure_value.call_args[0]
|
)
|
||||||
self.assertEqual(coro, (50, 100)) # Should use threshold_load from config
|
|
||||||
|
|
||||||
async def test_backpressure_no_handler(self):
|
async def test_backpressure_no_handler(self):
|
||||||
"""Test backpressure method when no handler is available."""
|
"""Test backpressure method when no handler is available."""
|
||||||
|
|||||||
Reference in New Issue
Block a user