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): avoid retrying Redis during test collection (#14326)
maopishouzzz12ec5d70e8e1
diff --git a/autogpt_platform/backend/backend/api/conn_manager_integration_test.py b/autogpt_platform/backend/backend/api/conn_manager_integration_test.pyindex c7b7c7534f..cb037019df 100644--- a/autogpt_platform/backend/backend/api/conn_manager_integration_test.py+++ b/autogpt_platform/backend/backend/api/conn_manager_integration_test.py@@ -27,18 +27,11 @@ from backend.data.execution import (exec_channel,graph_all_channel,)+from backend.util.testing import is_tcp_port_reachabledef _has_live_cluster() -> bool:- try:- c = redis_client.connect()- except Exception: # noqa: BLE001 — any connect failure → skip- return False- try:- c.close()- except Exception:- pass- return True+ return is_tcp_port_reachable(redis_client.HOST, redis_client.PORT)pytestmark = pytest.mark.skipif(diff --git a/autogpt_platform/backend/backend/data/e2e_redis_rabbit_test.py b/autogpt_platform/backend/backend/data/e2e_redis_rabbit_test.pyindex f9e2d86d2c..a273e90da7 100644--- a/autogpt_platform/backend/backend/data/e2e_redis_rabbit_test.py+++ b/autogpt_platform/backend/backend/data/e2e_redis_rabbit_test.py@@ -31,18 +31,11 @@ from backend.executor.utils import (GRAPH_EXECUTION_QUEUE_NAME,create_execution_queue_config,)+from backend.util.testing import is_tcp_port_reachabledef _has_live_cluster() -> bool:- try:- c = redis_client.connect()- except Exception: # noqa: BLE001 — any connect failure → skip- return False- try:- c.close()- except Exception:- pass- return True+ return is_tcp_port_reachable(redis_client.HOST, redis_client.PORT)def _has_live_rabbit() -> bool:diff --git a/autogpt_platform/backend/backend/data/event_bus_test.py b/autogpt_platform/backend/backend/data/event_bus_test.pyindex 3318ec2164..a188830cec 100644--- a/autogpt_platform/backend/backend/data/event_bus_test.py+++ b/autogpt_platform/backend/backend/data/event_bus_test.py@@ -11,6 +11,7 @@ from backend.data.event_bus import (RedisEventBus,_assert_no_wildcard,)+from backend.util.testing import is_tcp_port_reachableclass SampleEvent(BaseModel):@@ -84,15 +85,7 @@ def test_assert_no_wildcard_guard():def _has_live_cluster() -> bool:from backend.data import redis_client- try:- c = redis_client.connect()- except Exception: # noqa: BLE001 - any connect failure → skip the test- return False- try:- c.close()- except Exception:- pass- return True+ return is_tcp_port_reachable(redis_client.HOST, redis_client.PORT)@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/data/redis_client_test.py b/autogpt_platform/backend/backend/data/redis_client_test.pyindex 69d5d3ffb6..c8ca35aca5 100644--- a/autogpt_platform/backend/backend/data/redis_client_test.py+++ b/autogpt_platform/backend/backend/data/redis_client_test.py@@ -16,6 +16,7 @@ from redis.exceptions import TimeoutError as RedisTimeoutErrorfrom redis.retry import Retryimport backend.data.redis_client as redis_client+from backend.util.testing import is_tcp_port_reachable@pytest.fixture(autouse=True)@@ -243,15 +244,7 @@ async def test_disconnect_async_no_cached_client_is_noop() -> None:def _has_live_cluster() -> bool:- try:- c = redis_client.connect()- except Exception: # noqa: BLE001 — any connect failure → skip the test- return False- try:- c.close()- except Exception:- pass- return True+ return is_tcp_port_reachable(redis_client.HOST, redis_client.PORT)@pytest.mark.skipif(diff --git a/autogpt_platform/backend/backend/util/redis_test_collection_test.py b/autogpt_platform/backend/backend/util/redis_test_collection_test.pynew file mode 100644index 0000000000..4163487bd4--- /dev/null+++ b/autogpt_platform/backend/backend/util/redis_test_collection_test.py@@ -0,0 +1,28 @@+import runpy+from pathlib import Path+from unittest.mock import patch++import pytest++from backend.data import redis_client+++ "test_module",+ [+ "api/conn_manager_integration_test.py",+ "data/e2e_redis_rabbit_test.py",+ "data/event_bus_test.py",+ "data/redis_client_test.py",+ ],+)+def test_collection_does_not_call_retrying_redis_client(test_module: str) -> None:+ with (+ patch.object(+ redis_client, "connect", side_effect=ConnectionError("Redis unavailable")+ ) as connect,+ patch("socket.create_connection", side_effect=OSError("port closed")),+ ):+ runpy.run_path(str(Path(__file__).parents[1] / test_module))++ connect.assert_not_called()diff --git a/autogpt_platform/backend/backend/util/testing.py b/autogpt_platform/backend/backend/util/testing.pynew file mode 100644index 0000000000..c2e64063b7--- /dev/null+++ b/autogpt_platform/backend/backend/util/testing.py@@ -0,0 +1,26 @@+"""Lightweight helpers for tests that must inspect local infrastructure."""++import socket+from functools import cache+++@cache+def is_tcp_port_reachable(+ host: str,+ port: int,+ *,+ timeout: float = 1.0,+) -> bool:+ """Return whether a TCP endpoint accepts a connection within ``timeout``.++ This deliberately avoids service clients with retry policies so it is safe+ to call from module-level pytest skip markers during collection. Results+ are cached so modules that share an endpoint only wait for one probe.+ """+ try:+ connection = socket.create_connection((host, port), timeout=timeout)+ except OSError:+ return False++ connection.close()+ return Truediff --git a/autogpt_platform/backend/backend/util/testing_test.py b/autogpt_platform/backend/backend/util/testing_test.pynew file mode 100644index 0000000000..71381a26bf--- /dev/null+++ b/autogpt_platform/backend/backend/util/testing_test.py@@ -0,0 +1,56 @@+from collections.abc import Iterator+from unittest.mock import MagicMock, patch++import pytest++from backend.util.testing import is_tcp_port_reachable++[email protected](autouse=True)+def _clear_port_probe_cache() -> Iterator[None]:+ is_tcp_port_reachable.cache_clear()+ yield+ is_tcp_port_reachable.cache_clear()+++def test_is_tcp_port_reachable_closes_probe_socket() -> None:+ connection = MagicMock()++ with patch(+ "backend.util.testing.socket.create_connection",+ return_value=connection,+ ) as create_connection:+ assert is_tcp_port_reachable("redis.example", 6379)++ create_connection.assert_called_once_with(("redis.example", 6379), timeout=1.0)+ connection.close.assert_called_once_with()+++def test_is_tcp_port_reachable_caches_repeated_probe() -> None:+ connection = MagicMock()++ with patch(+ "backend.util.testing.socket.create_connection",+ return_value=connection,+ ) as create_connection:+ assert is_tcp_port_reachable("redis.example", 6379)+ assert is_tcp_port_reachable("redis.example", 6379)++ create_connection.assert_called_once_with(("redis.example", 6379), timeout=1.0)+++def test_is_tcp_port_reachable_returns_false_for_os_error() -> None:+ with patch(+ "backend.util.testing.socket.create_connection",+ side_effect=TimeoutError,+ ):+ assert not is_tcp_port_reachable("redis.example", 6379)+++def test_is_tcp_port_reachable_propagates_unexpected_errors() -> None:+ with patch(+ "backend.util.testing.socket.create_connection",+ side_effect=ValueError("invalid endpoint"),+ ):+ with pytest.raises(ValueError, match="invalid endpoint"):+ is_tcp_port_reachable("redis.example", 6379)