From 7d4e358ce50577e11474f03a56f63f6c2651e8f2 Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Sat, 12 Jul 2025 23:02:19 +0100 Subject: [PATCH] test: add AsyncMock to test backpressure handler conditions Co-authored-by: aider (openrouter/openai/o3-mini-high) --- tests/adapter/test_backpressure_handler.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/adapter/test_backpressure_handler.py b/tests/adapter/test_backpressure_handler.py index 49762b5..ea977a7 100644 --- a/tests/adapter/test_backpressure_handler.py +++ b/tests/adapter/test_backpressure_handler.py @@ -351,9 +351,18 @@ class TestBackpressureHandler: handler.current_parallel_executions = 1 handler.parallel_workers = 5 handler.last_backpressure_event = ScalingRequestAlert.UPDATE - with patch.object(handler, "handle_backpressure_overload_event") as mock_handle_overload: - await handler.check_overload_condition() - mock_handle_overload.assert_not_called() + handler.publish_backpressure_request = AsyncMock() + with patch("time.time") as mock_time: + mock_time.return_value = 12345 + with patch.object(handler, "handle_backpressure_overload_event") as mock_handle_overload: + await handler.check_overload_condition() + mock_handle_overload.assert_not_called() + if 12345 - handler.last_backpressure_event_time > mock_config.backpressure.time_window: + handler.publish_backpressure_request.assert_called_once() + assert handler.last_backpressure_event == ScalingRequestAlert.UPDATE + assert handler.last_backpressure_event_time == 12345 + else: + handler.publish_backpressure_request.assert_not_called() @pytest.mark.asyncio async def test_check_overload_condition_overload(self, mock_channel, mock_loop, mock_config): @@ -363,7 +372,9 @@ class TestBackpressureHandler: handler = BackpressureHandler(mock_channel, mock_loop, mock_config) handler.current_parallel_executions = 9 # Assuming parallel_workers is 10 handler.last_backpressure_event = ScalingRequestAlert.UPDATE + handler.publish_backpressure_request = AsyncMock() with patch.object(handler, "handle_backpressure_overload_event") as mock_handle_overload: + mock_handle_overload.return_value = None with patch("time.time") as mock_time: mock_time.return_value = 12345 await handler.check_overload_condition() @@ -381,6 +392,7 @@ class TestBackpressureHandler: handler = BackpressureHandler(mock_channel, mock_loop, mock_config) handler.current_parallel_executions = 10 handler.last_backpressure_event = ScalingRequestAlert.OVERLOAD + handler.publish_backpressure_request = AsyncMock() with patch.object(handler, "handle_backpressure_overload_event") as mock_handle_overload: await handler.check_overload_condition() mock_handle_overload.assert_not_called()