44"""
55
66import unittest
7+ import retrying
78from mock import patch , Mock
89
10+ retrying_mock = Mock ()
11+
912import 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
1229class 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+
3071if __name__ == '__main__' :
3172 unittest .main ()
3273
0 commit comments