I usually prefer not to make actual calls out to the internet in my tests: the remote service could be down or you might need to run your tests offline. Most importantly you want to run your tests FAST, and the network call can slow them down significantly.
I also want to make sure that the retry logic actually makes the retries I expect, and that it can actually succeeds eventually. And do not like the idea of using log messages as a proxy for that.
I could not find a proper solution online, so I'll leave my solution here for posterity, trying to save someone else the hours I've spent trying:
import urllib3
from http.client import HTTPMessage
from unittest.mock import ANY, Mock, patch, call
import requests
def request_with_retry(*args, **kwargs):
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=urllib3.Retry(
raise_on_status=False,
total=kwargs.pop("max_retries", 3),
status_forcelist=[429, 500, 502, 503, 504], # The HTTP response codes to retry on
allowed_methods=["HEAD", "GET", "PUT", "DELETE", "OPTIONS"], # The HTTP methods to retry on
))
session.mount("https://", adapter)
session.mount("http://", adapter)
return session.request(*args, **kwargs)
@patch("urllib3.connectionpool.HTTPConnectionPool._get_conn")
def test_retry_request(getconn_mock):
getconn_mock.return_value.getresponse.side_effect = [
Mock(status=500, msg=HTTPMessage()),
Mock(status=429, msg=HTTPMessage()),
Mock(status=200, msg=HTTPMessage()),
]
r = request_with_retry("GET", "http://any.url/testme", max_retries=2)
r.raise_for_status()
assert getconn_mock.return_value.request.mock_calls == [
call("GET", "/testme", body=None, headers=ANY),
call("GET", "/testme", body=None, headers=ANY),
call("GET", "/testme", body=None, headers=ANY),
]
(Note: if you are making several calls to this method, then you might want to initialize the session object only once, not every time you make a request!)