Skip to content

Commit b4abed6

Browse files
committed
Reenable the retry module and test it
1 parent f3f2112 commit b4abed6

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

stackify/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def POST(self, url, json_object, use_gzip=False):
7070
internal_log.exception('Cannot decode JSON response')
7171
raise
7272

73-
#@retrying.retry(wait_exponential_multiplier=1000, stop_max_delay=10000)
73+
@retrying.retry(wait_exponential_multiplier=1000, stop_max_delay=10000)
7474
def identify_application(self):
7575
result = self.POST('/Metrics/IdentifyApp', self.environment_detail)
7676
self.app_name_id = result.get('AppNameID')

tests/test_http.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,49 @@
44
"""
55

66
import unittest
7+
import retrying
78
from mock import patch, Mock
89

10+
retrying_mock = Mock()
11+
912
import stackify.http
1013

14+
from stackify.application import ApiConfiguration
15+
16+
17+
old_retry = retrying.retry
18+
19+
FAKE_RETRIES = 3
20+
21+
def fake_retry(*args, **kwargs):
22+
kwargs['wait_exponential_max'] = 0 # no delay between retries
23+
kwargs['stop_max_attempt_number'] = FAKE_RETRIES # stop after 3 tries
24+
def inner(func):
25+
return old_retry(*args, **kwargs)(func)
26+
return inner
27+
1128

1229
class TestClient(unittest.TestCase):
1330
'''
1431
Test the HTTP Client and associated utilities
1532
'''
1633

17-
def setUp(self):
18-
pass
34+
@classmethod
35+
def setUpClass(cls):
36+
retrying.retry = fake_retry
37+
reload(stackify.http)
1938

20-
def tearDown(self):
21-
pass
39+
@classmethod
40+
def tearDownClass(cls):
41+
reload(retrying)
42+
reload(stackify.http)
43+
44+
def setUp(self):
45+
self.config = ApiConfiguration(
46+
application = 'test_appname',
47+
environment = 'test_environment',
48+
api_key = 'test_apikey',
49+
api_url = 'test_apiurl')
2250

2351
def test_logger_no_config(self):
2452
'''GZIP encoder works'''
@@ -27,6 +55,19 @@ def test_logger_no_config(self):
2755
gzipped[4:8] = ' ' # blank the mtime
2856
self.assertEqual(gzipped, correct)
2957

58+
def test_identify_retrying(self):
59+
'''HTTP identify should retry'''
60+
client = stackify.http.HTTPClient(self.config)
61+
62+
class CustomException(Exception): pass
63+
crash = Mock(side_effect=CustomException)
64+
65+
with patch.object(client, 'POST', crash):
66+
with self.assertRaises(CustomException):
67+
client.identify_application()
68+
self.assertEqual(crash.call_count, FAKE_RETRIES)
69+
70+
3071
if __name__=='__main__':
3172
unittest.main()
3273

0 commit comments

Comments
 (0)