From 234b67f5fd7d67b1a12713419b1e614d009589f4 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Mon, 2 Mar 2026 02:59:41 -0800 Subject: [PATCH] fix: mock time in retry exhaustion tests to prevent backoff sleep The TestRetryExhaustion tests from PR #223 didn't mock time.sleep/time.time, causing the retry backoff loops (275s+ total) to run in real time. Tests would time out instead of running quickly. Added _make_fast_time_mock() helper that creates a mock time module where time.time() advances 500s per call (so sleep_end is always in the past) and time.sleep() is a no-op. Both tests now complete in <1s. --- tests/test_run_agent.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_run_agent.py b/tests/test_run_agent.py index 92ab23cb..1005f40d 100644 --- a/tests/test_run_agent.py +++ b/tests/test_run_agent.py @@ -775,6 +775,21 @@ class TestRetryExhaustion: agent.compression_enabled = False agent.save_trajectories = False + @staticmethod + def _make_fast_time_mock(): + """Return a mock time module where sleep loops exit instantly.""" + mock_time = MagicMock() + _t = [1000.0] + + def _advancing_time(): + _t[0] += 500.0 # jump 500s per call so sleep_end is always in the past + return _t[0] + + mock_time.time.side_effect = _advancing_time + mock_time.sleep = MagicMock() # no-op + mock_time.monotonic.return_value = 12345.0 + return mock_time + def test_invalid_response_returns_error_not_crash(self, agent): """Exhausted retries on invalid (empty choices) response must not IndexError.""" self._setup_agent(agent) @@ -789,6 +804,7 @@ class TestRetryExhaustion: patch.object(agent, "_persist_session"), patch.object(agent, "_save_trajectory"), patch.object(agent, "_cleanup_task_resources"), + patch("run_agent.time", self._make_fast_time_mock()), ): result = agent.run_conversation("hello") assert result.get("failed") is True or result.get("completed") is False @@ -801,6 +817,7 @@ class TestRetryExhaustion: patch.object(agent, "_persist_session"), patch.object(agent, "_save_trajectory"), patch.object(agent, "_cleanup_task_resources"), + patch("run_agent.time", self._make_fast_time_mock()), ): with pytest.raises(RuntimeError, match="rate limited"): agent.run_conversation("hello")