Skip to content

Commit

Permalink
fix some attributes in the except for ConnectionError/Timeout and cre…
Browse files Browse the repository at this point in the history
…ate a test to be sure it works
  • Loading branch information
Kyria committed Apr 27, 2017
1 parent 7fbce32 commit a13c2af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 2 additions & 6 deletions esipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ def _retry_request(self, req_and_resp, _retry=0, **kwargs):
res = self._request(req_and_resp, **kwargs)
except (RequestsConnectionError, Timeout) as e:
req, res = req_and_resp
res.status = 500
res._Response__status = 500
# request__path is the same as response, and is always set
res._Response__path = req._Request__path

try:
res.data = e.message.message
except AttributeError:
res.data = ""
res._Response__data = str(e)

if 500 <= res.status <= 599:
_retry += 1
Expand Down
21 changes: 21 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from esipy.cache import DummyCache

from requests.adapters import HTTPAdapter
from requests.exceptions import ConnectionError

import httmock
import mock
Expand Down Expand Up @@ -223,3 +224,23 @@ def test_esipy_backoff(self):

# Check that backoff slept for a sum > 2 seconds
self.assertTrue(end_calls - start_calls > 2)

def test_esipy_timeout(self):
def send_function(*args, **kwargs):
""" manually create a ConnectionError to test the retry and be sure
no exception is thrown """
send_function.count += 1
raise ConnectionError
send_function.count = 0

self.client_no_auth._session.send = mock.MagicMock(
side_effect=send_function
)

operation = self.app.op['get_incursions']()
with httmock.HTTMock(public_incursion):
incursions = self.client_no_auth.request(operation)
# there shouldn't be any exceptions

self.assertEqual(incursions.status, 500)
self.assertEqual(send_function.count, 5)

0 comments on commit a13c2af

Please sign in to comment.