This commit was merged in pull request #181.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Tests for agent retry logic on transient errors."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
@@ -42,11 +42,16 @@ async def test_run_retries_on_transient_error(sub_agent):
|
||||
sub_agent.agent.run = mock_run
|
||||
|
||||
# Act
|
||||
result = await sub_agent.run("test message")
|
||||
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
|
||||
result = await sub_agent.run("test message")
|
||||
|
||||
# Assert
|
||||
assert result == "Success after retries"
|
||||
assert call_count == 3 # 2 failures + 1 success
|
||||
# Verify exponential backoff: attempt 1 = 1s, attempt 2 = 2s
|
||||
assert mock_sleep.call_count == 2
|
||||
mock_sleep.assert_any_call(1)
|
||||
mock_sleep.assert_any_call(2)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -56,11 +61,16 @@ async def test_run_exhausts_retries(sub_agent):
|
||||
sub_agent.agent.run.side_effect = Exception("Ollama 500 error: XML parse error")
|
||||
|
||||
# Act & Assert
|
||||
with pytest.raises(Exception, match="Ollama 500 error: XML parse error"):
|
||||
await sub_agent.run("test message")
|
||||
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
|
||||
with pytest.raises(Exception, match="Ollama 500 error: XML parse error"):
|
||||
await sub_agent.run("test message")
|
||||
|
||||
# Should have been called 3 times (max retries)
|
||||
assert sub_agent.agent.run.call_count == 3
|
||||
# Verify exponential backoff: attempt 1 = 1s, attempt 2 = 2s
|
||||
assert mock_sleep.call_count == 2
|
||||
mock_sleep.assert_any_call(1)
|
||||
mock_sleep.assert_any_call(2)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -114,10 +124,13 @@ async def test_run_logs_retry_attempts(sub_agent, caplog):
|
||||
sub_agent.agent.run = mock_run
|
||||
|
||||
# Act
|
||||
result = await sub_agent.run("test message")
|
||||
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
|
||||
result = await sub_agent.run("test message")
|
||||
|
||||
# Assert
|
||||
assert result == "Success"
|
||||
assert call_count == 2 # 1 failure + 1 success
|
||||
assert "Agent run failed on attempt 1/3" in caplog.text
|
||||
assert "Retrying..." in caplog.text
|
||||
# Verify sleep was called with 1s for first attempt
|
||||
mock_sleep.assert_called_once_with(1)
|
||||
|
||||
Reference in New Issue
Block a user