From a6b434ee0576a3eadd507cdc1205a82d27796eac Mon Sep 17 00:00:00 2001 From: Christine Wang Date: Thu, 23 Oct 2014 21:52:12 -0400 Subject: [PATCH] cinderclient does not retry with TimeoutException Added retry support when encounter requests.exceptions.Timeout during HTTP request. Closes-Bug: #1379505 Change-Id: I6253a109c3a76dd2f15c96a349da68936f9bfff4 --- cinderclient/client.py | 4 ++++ cinderclient/tests/test_http.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cinderclient/client.py b/cinderclient/client.py index 358b2a25e..644765ad2 100644 --- a/cinderclient/client.py +++ b/cinderclient/client.py @@ -298,6 +298,10 @@ class HTTPClient(object): if attempts > self.retries: msg = 'Unable to establish connection: %s' % e raise exceptions.ConnectionError(msg) + except requests.exceptions.Timeout as e: + self._logger.debug("Timeout error: %s" % e) + if attempts > self.retries: + raise self._logger.debug( "Failed attempt(%s of %s), retrying in %s seconds" % (attempts, self.retries, backoff)) diff --git a/cinderclient/tests/test_http.py b/cinderclient/tests/test_http.py index f0121ea94..d692597ee 100644 --- a/cinderclient/tests/test_http.py +++ b/cinderclient/tests/test_http.py @@ -54,6 +54,9 @@ bad_500_request = mock.Mock(return_value=(bad_500_response)) connection_error_request = mock.Mock( side_effect=requests.exceptions.ConnectionError) +timeout_error_request = mock.Mock( + side_effect=requests.exceptions.Timeout) + def get_client(retries=0): cl = client.HTTPClient("username", "password", @@ -241,3 +244,20 @@ class ClientTest(utils.TestCase): self.assertRaises(NotImplementedError, cl.authenticate) test_auth_call() + + def test_get_retry_timeout_error(self): + cl = get_authed_client(retries=1) + + self.requests = [timeout_error_request, mock_request] + + def request(*args, **kwargs): + next_request = self.requests.pop(0) + return next_request(*args, **kwargs) + + @mock.patch.object(requests, "request", request) + @mock.patch('time.time', mock.Mock(return_value=1234)) + def test_get_call(): + resp, body = cl.get("/hi") + + test_get_call() + self.assertEqual(self.requests, [])