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.
This commit is contained in:
teknium1
2026-03-02 02:59:41 -08:00
parent e27e3a4f8a
commit 234b67f5fd

View File

@@ -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")