@@ -31,6 +31,9 @@ def test_validation(self):
31
31
@patch ('railib.rest.urlopen' )
32
32
class TestURLOpenWithRetry (unittest .TestCase ):
33
33
34
+ WARNING_LOG_PREFIX = "URL/Connection error occured"
35
+ ERROR_LOG_PREFIX = "Failed to connect to"
36
+
34
37
def test_successful_response (self , mock_urlopen ):
35
38
# Set up the mock urlopen to return a successful response
36
39
mock_response = MagicMock ()
@@ -55,7 +58,7 @@ def test_negative_retries(self, _):
55
58
56
59
self .assertIn ("Retries must be a non-negative integer" , str (e .exception ))
57
60
58
- def test_timeout_retry (self , mock_urlopen ):
61
+ def test_url_error_retry (self , mock_urlopen ):
59
62
# Set up the mock urlopen to raise a socket timeout error
60
63
mock_urlopen .side_effect = URLError (socket .timeout ())
61
64
@@ -66,26 +69,27 @@ def test_timeout_retry(self, mock_urlopen):
66
69
67
70
self .assertEqual (mock_urlopen .call_count , 3 ) # Expect 1 original call and 2 calls for retries
68
71
self .assertEqual (len (log .output ), 4 ) # Expect 3 log messages for retries and 1 for failure to connect
69
- self .assertIn ('Timeout occurred' , log .output [0 ])
70
- self .assertIn ('Timeout occurred' , log .output [1 ])
71
- self .assertIn ('Timeout occurred' , log .output [2 ])
72
- self .assertIn ('Failed to connect to' , log .output [3 ])
72
+ self .assertIn (TestURLOpenWithRetry . WARNING_LOG_PREFIX , log .output [0 ])
73
+ self .assertIn (TestURLOpenWithRetry . WARNING_LOG_PREFIX , log .output [1 ])
74
+ self .assertIn (TestURLOpenWithRetry . WARNING_LOG_PREFIX , log .output [2 ])
75
+ self .assertIn (TestURLOpenWithRetry . ERROR_LOG_PREFIX , log .output [3 ])
73
76
74
- def test_other_error_retry (self , mock_urlopen ):
75
- # Set up the mock urlopen to raise a non-timeout URLError
76
- mock_urlopen .side_effect = URLError ( 'Some other error' )
77
+ def test_connection_error_retry (self , mock_urlopen ):
78
+ # Set up the mock urlopen to raise a error that is subclass of ConnectonError
79
+ mock_urlopen .side_effect = ConnectionResetError ( "connection reset by peer" )
77
80
78
81
req = Request ('https://example.com' )
79
82
with self .assertLogs () as log :
80
83
with self .assertRaises (Exception ):
81
- _urlopen_with_retry (req , retries = 2 )
84
+ _urlopen_with_retry (req , 2 )
82
85
83
- self .assertEqual (mock_urlopen .call_count , 3 ) # Expect 3 calls for retries
86
+ self .assertEqual (mock_urlopen .call_count , 3 ) # Expect 1 original call and 2 calls for retries
84
87
self .assertEqual (len (log .output ), 4 ) # Expect 3 log messages for retries and 1 for failure to connect
85
- self .assertIn ('URLError occurred' , log .output [0 ])
86
- self .assertIn ('URLError occurred' , log .output [1 ])
87
- self .assertIn ('URLError occurred' , log .output [2 ])
88
- self .assertIn ('Failed to connect to' , log .output [3 ])
88
+ self .assertIn (TestURLOpenWithRetry .WARNING_LOG_PREFIX , log .output [0 ])
89
+ self .assertIn (TestURLOpenWithRetry .WARNING_LOG_PREFIX , log .output [1 ])
90
+ self .assertIn (TestURLOpenWithRetry .WARNING_LOG_PREFIX , log .output [2 ])
91
+ self .assertIn (TestURLOpenWithRetry .ERROR_LOG_PREFIX , log .output [3 ])
92
+
89
93
90
94
91
95
if __name__ == '__main__' :
0 commit comments