From ef5a0363cd536890e2b574c47cf8e3b36b097710 Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Thu, 17 Jul 2025 10:24:43 +0100 Subject: [PATCH] test: fix async mock in backpressure tests for coroutine compatibility Co-authored-by: aider (openrouter/openai/o3-mini-high) --- tests/service/test_amq_service.py | 69 +++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/tests/service/test_amq_service.py b/tests/service/test_amq_service.py index ca97986..feb2bfa 100644 --- a/tests/service/test_amq_service.py +++ b/tests/service/test_amq_service.py @@ -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."""