test_retry_logic.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. 测试修复后的重试逻辑
  3. """
  4. import asyncio
  5. import sys
  6. import os
  7. # 添加路径
  8. CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
  9. sys.path.insert(0, CURRENT_DIR)
  10. import config
  11. def test_error_classification():
  12. """测试错误分类逻辑"""
  13. print("🧪 测试错误分类逻辑")
  14. # 测试用例
  15. test_cases = [
  16. ("Request timed out.", True, "应该识别为网络错误"),
  17. ("APITimeoutError: timeout", True, "应该识别为网络错误"),
  18. ("Connection error occurred", True, "应该识别为网络错误"),
  19. ("ReadTimeout exception", True, "应该识别为网络错误"),
  20. ("ValueError: invalid input", False, "应该识别为非网络错误"),
  21. ("KeyError: missing key", False, "应该识别为非网络错误"),
  22. ]
  23. # 网络错误关键词(与agent.py中一致)
  24. network_keywords = [
  25. "Connection error", "APIConnectionError", "ConnectError",
  26. "timeout", "timed out", "TimeoutError", "APITimeoutError",
  27. "ReadTimeout", "ConnectTimeout", "远程主机强迫关闭", "网络连接"
  28. ]
  29. for error_msg, expected, description in test_cases:
  30. is_network_error = any(keyword in error_msg for keyword in network_keywords)
  31. status = "✅" if is_network_error == expected else "❌"
  32. print(f" {status} {description}")
  33. print(f" 错误信息: '{error_msg}'")
  34. print(f" 预期: {'网络错误' if expected else '非网络错误'}")
  35. print(f" 实际: {'网络错误' if is_network_error else '非网络错误'}")
  36. print()
  37. def test_retry_intervals():
  38. """测试重试间隔计算"""
  39. print("⏱️ 测试重试间隔计算")
  40. base_delay = config.RETRY_BASE_DELAY # 2秒
  41. max_retries = config.MAX_RETRIES # 5次
  42. print(f" 基础延迟: {base_delay}秒")
  43. print(f" 最大重试: {max_retries}次")
  44. print()
  45. total_wait_time = 0
  46. for attempt in range(max_retries - 1): # 不包括最后一次(不会重试)
  47. # 新的计算公式:wait_time = base_delay * (2 ** attempt) + attempt
  48. wait_time = base_delay * (2 ** attempt) + attempt
  49. total_wait_time += wait_time
  50. print(f" 第{attempt + 1}次失败后等待: {wait_time}秒")
  51. print(f"\n 总等待时间: {total_wait_time}秒")
  52. print(f" 加上LLM超时({config.NETWORK_TIMEOUT}秒 x {max_retries}次): {config.NETWORK_TIMEOUT * max_retries}秒")
  53. print(f" 最大总耗时: {total_wait_time + config.NETWORK_TIMEOUT * max_retries}秒")
  54. if __name__ == "__main__":
  55. print("🔧 测试修复后的重试机制\n")
  56. test_error_classification()
  57. print("=" * 50)
  58. test_retry_intervals()
  59. print("\n✅ 测试完成")