我的重试课程是
class RetryRequest:
"""
Module for Retry logic on requests API requests
"""
def __init__(self):
# Retry Logic
retry_server_errors = requests.adapters.Retry(
total=20,
connect=10,
status_forcelist=list(range(500, 512)),
status=10,
backoff_factor=0.1,
allowed_methods=['GET', 'PATCH', 'DELETE'],
)
adapter = requests.adapters.HTTPAdapter(max_retries=retry_server_errors)
s = requests.Session()
s.mount("https://", adapter)
s.mount("http://", adapter)
self.session = s
我的测试是
@patch('requests.adapters.HTTPAdapter.send', side_effect=ConnectionError)
def test_retry_on_connection_error(mock_send):
"""
Test to verify that the RetryRequest retries when a connection error occurs.
"""
retry_request = RetryRequest()
request_session = retry_request.session
# Make the request and expect a ConnectionError
with pytest.raises(ConnectionError):
request_session.get('https://example.com/test')
# Ensure that the request was retried 10 times
assert mock_send.call_count == 10, "Expected 10 retries on connection error"
但它只被调用一次,所以在
connect
上重试 10 次不起作用
我无法让它触发
connect
重试,所以不确定那是什么。但我已经决定只使用total
。无论如何,我需要在 urllib3 内部提出。
@patch(
'urllib3.connectionpool.HTTPSConnectionPool._get_conn',
side_effect=urllib3.exceptions.ConnectionError,
)
def test_retry_on_connection_error(m_get_conn):
"""Test to verify that the RetryRequest retries when a connection error occurs."""
retry_request = RetryRequest(**{'backoff_factor': 0, 'total': 9})
request_session = retry_request.session
# Make the request and expect a ConnectionError
with pytest.raises(requests.exceptions.ConnectionError):
request_session.get('https://example.com/test')
# Ensure that the request was retried 9 times
assert m_get_conn.call_count == 10, 'Expected 9 retries on connection error'