2828
2929class ConnectionUtilsTest (unittest .TestCase ):
3030
31- def test_retry (self ):
31+ def test_retry_no_except (self ):
3232 dummy = unittest .mock .Mock (__name__ = "function" )
3333 wrapped = retry (2 , ValueError )(dummy )
3434 wrapped ("Foo" , 42 )
3535 dummy .assert_called_once_with ("Foo" , 42 )
3636
37+ def test_retry_except (self ):
3738 dummy = unittest .mock .Mock (__name__ = "function" , side_effect = [ValueError , True ])
3839 wrapped = retry (2 , ValueError )(dummy )
3940 wrapped ()
@@ -57,18 +58,23 @@ def test_retry(self):
5758 wrapped ()
5859 self .assertEqual (dummy .call_count , 3 )
5960
61+ def test_retry_parameters (self ):
6062 dummy = unittest .mock .Mock (__name__ = "function" )
6163 with self .assertRaises (ValueError ):
6264 retry (0 , RuntimeError )(dummy )
6365 retry (1 , RuntimeError )(dummy )
6466
67+ def test_retry_jitter (self ):
6568 dummy = unittest .mock .Mock (__name__ = "function" , side_effect = ValueError )
69+ # Long wait time is to minimize noise from CPU availability
6670 wrapped = retry (2 , ValueError , wait = 5.0 )(dummy )
6771 start = time .time ()
6872 with self .assertRaises (ExceptionGroup ):
6973 wrapped ()
7074 stop = time .time ()
7175 self .assertEqual (dummy .call_count , 2 )
7276 # Should have only waited after the first call
77+ # Expected wait is +/- 25%, or 3.75-6.25 s
78+ # If it waited twice, that's at least 7.5 s
7379 self .assertGreaterEqual (stop - start , 3.75 )
74- self .assertLessEqual (stop - start , 6.25 )
80+ self .assertLessEqual (stop - start , 6.35 ) # Fudge the high end for exception handling overhead
0 commit comments