Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): isolate copilot dispatch sleep mocks (#14372)

Toran Bruce Richards0ddb93fdc047
diff --git a/autogpt_platform/backend/backend/copilot/executor/manager_test.py b/autogpt_platform/backend/backend/copilot/executor/manager_test.py
index ebdd225994..d61a788b47 100644
--- a/autogpt_platform/backend/backend/copilot/executor/manager_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/manager_test.py
@@ -7,8 +7,11 @@ pin its retry/give-up contract.
"""
import threading
+import time
+from concurrent.futures import ThreadPoolExecutor
+from time import sleep as original_sleep
from types import SimpleNamespace
-from unittest.mock import AsyncMock, MagicMock, patch
+from unittest.mock import AsyncMock, MagicMock, call, patch
import pytest
@@ -76,29 +79,51 @@ def test_dispatch_preserves_codex_transport_for_engine_switch(
assert kwargs["llm_credential_id"] == "cred-codex"
-def test_dispatch_retries_until_success():
+def mock_dispatch_sleep():
+ mock_sleep = MagicMock()
+ with patch(
+ "backend.copilot.executor.manager.time", SimpleNamespace(sleep=mock_sleep)
+ ):
+ yield mock_sleep
+
+
+def test_dispatch_sleep_mock_does_not_affect_other_threads(mock_dispatch_sleep):
+ def sleep_in_other_thread():
+ sleeper = time.sleep
+ sleeper(0)
+ return sleeper
+
+ with ThreadPoolExecutor(max_workers=1) as executor:
+ sleeper = executor.submit(sleep_in_other_thread).result(timeout=5)
+
+ assert sleeper is original_sleep
+ mock_dispatch_sleep.assert_not_called()
+
+
+def test_dispatch_retries_until_success(mock_dispatch_sleep):
with (
patch(
"backend.copilot.executor.manager.schedule_turn",
new_callable=AsyncMock,
side_effect=[RuntimeError("rmq down"), RuntimeError("rmq down"), None],
) as mock_schedule,
- patch("backend.copilot.executor.manager.time.sleep") as mock_sleep,
):
_dispatch_engine_switch_continuation("sess-1", _SWITCH)
assert mock_schedule.await_count == 3
- assert mock_sleep.call_count == 2
+ assert mock_dispatch_sleep.call_args_list == [call(1), call(2)]
-def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker():
+def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker(
+ mock_dispatch_sleep,
+):
with (
patch(
"backend.copilot.executor.manager.schedule_turn",
new_callable=AsyncMock,
side_effect=RuntimeError("rmq down"),
) as mock_schedule,
- patch("backend.copilot.executor.manager.time.sleep"),
patch(
"backend.copilot.executor.manager._persist_switch_failure_marker"
) as mock_marker,
@@ -106,6 +131,7 @@ def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker():
_dispatch_engine_switch_continuation("sess-1", _SWITCH)
assert mock_schedule.await_count == _SWITCH_DISPATCH_ATTEMPTS
+ assert mock_dispatch_sleep.call_args_list == [call(1), call(2)]
mock_marker.assert_called_once_with("sess-1")