test: add exponential backoff retry for help command test

This commit is contained in:
Paul Gauthier (aider) 2025-03-21 11:25:10 -07:00
parent f3a042dcdf
commit 0d75c4d0e3

View file

@ -1,5 +1,7 @@
import unittest import unittest
import time
from unittest.mock import MagicMock from unittest.mock import MagicMock
from requests.exceptions import ReadTimeout, ConnectionError
import aider import aider
from aider.coders import Coder from aider.coders import Coder
@ -10,6 +12,40 @@ from aider.models import Model
class TestHelp(unittest.TestCase): class TestHelp(unittest.TestCase):
@staticmethod
def retry_with_backoff(func, max_time=60, initial_delay=1, backoff_factor=2):
"""
Execute a function with exponential backoff retry logic.
Args:
func: Function to execute
max_time: Maximum time in seconds to keep retrying
initial_delay: Initial delay between retries in seconds
backoff_factor: Multiplier for delay after each retry
Returns:
The result of the function if successful
Raises:
The last exception encountered if all retries fail
"""
start_time = time.time()
delay = initial_delay
last_exception = None
while time.time() - start_time < max_time:
try:
return func()
except (ReadTimeout, ConnectionError) as e:
last_exception = e
time.sleep(delay)
delay = min(delay * backoff_factor, 15) # Cap max delay at 15 seconds
# If we've exhausted our retry time, raise the last exception
if last_exception:
raise last_exception
raise Exception("Retry timeout exceeded but no exception was caught")
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
io = InputOutput(pretty=False, yes=True) io = InputOutput(pretty=False, yes=True)
@ -22,14 +58,18 @@ class TestHelp(unittest.TestCase):
help_coder_run = MagicMock(return_value="") help_coder_run = MagicMock(return_value="")
aider.coders.HelpCoder.run = help_coder_run aider.coders.HelpCoder.run = help_coder_run
try: def run_help_command():
commands.cmd_help("hi") try:
except aider.commands.SwitchCoder: commands.cmd_help("hi")
pass except aider.commands.SwitchCoder:
else: pass
# If no exception was raised, fail the test else:
assert False, "SwitchCoder exception was not raised" # If no exception was raised, fail the test
assert False, "SwitchCoder exception was not raised"
# Use retry with backoff for the help command that loads models
cls.retry_with_backoff(run_help_command)
help_coder_run.assert_called_once() help_coder_run.assert_called_once()
def test_init(self): def test_init(self):