aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLeonid Emar-Kar <46078689+Emar-Kar@users.noreply.github.com>2019-08-22 21:14:23 +0300
committerBu Sun Kim <8822365+busunkim96@users.noreply.github.com>2019-08-22 11:14:23 -0700
commit3d8cc46841bc967017fd4b1fc177fe08b0f62b64 (patch)
treeba04a4d8d746b3cb2e449b73ada387b4ddcc0f0c /tests
parent92840f1a1588afed74acb4d47938024152f91de0 (diff)
downloadpython-api-core-3d8cc46841bc967017fd4b1fc177fe08b0f62b64.tar.gz
API Core: Retry.__init__ add on_error (#8892)
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_retry.py63
1 files changed, 60 insertions, 3 deletions
diff --git a/tests/unit/test_retry.py b/tests/unit/test_retry.py
index 53c2396..5b5e59b 100644
--- a/tests/unit/test_retry.py
+++ b/tests/unit/test_retry.py
@@ -161,20 +161,25 @@ class TestRetry(object):
assert retry_._maximum == 60
assert retry_._multiplier == 2
assert retry_._deadline == 120
+ assert retry_._on_error is None
def test_constructor_options(self):
+ _some_function = mock.Mock()
+
retry_ = retry.Retry(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
deadline=4,
+ on_error=_some_function,
)
assert retry_._predicate == mock.sentinel.predicate
assert retry_._initial == 1
assert retry_._maximum == 2
assert retry_._multiplier == 3
assert retry_._deadline == 4
+ assert retry_._on_error is _some_function
def test_with_deadline(self):
retry_ = retry.Retry()
@@ -209,7 +214,8 @@ class TestRetry(object):
assert re.match(
(
r"<Retry predicate=<function.*?if_exception_type.*?>, "
- r"initial=1.0, maximum=60.0, multiplier=2.0, deadline=120.0>"
+ r"initial=1.0, maximum=60.0, multiplier=2.0, deadline=120.0, "
+ r"on_error=None>"
),
str(retry_),
)
@@ -230,8 +236,7 @@ class TestRetry(object):
target.assert_called_once_with("meep")
sleep.assert_not_called()
- # Make uniform return half of its maximum, which will be the calculated
- # sleep time.
+ # Make uniform return half of its maximum, which is the calculated sleep time.
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n / 2.0)
@mock.patch("time.sleep", autospec=True)
def test___call___and_execute_retry(self, sleep, uniform):
@@ -253,3 +258,55 @@ class TestRetry(object):
target.assert_has_calls([mock.call("meep"), mock.call("meep")])
sleep.assert_called_once_with(retry_._initial)
assert on_error.call_count == 1
+
+ @mock.patch("time.sleep", autospec=True)
+ def test___init___without_retry_executed(self, sleep):
+ _some_function = mock.Mock()
+
+ retry_ = retry.Retry(
+ predicate=retry.if_exception_type(ValueError), on_error=_some_function
+ )
+ # check the proper creation of the class
+ assert retry_._on_error is _some_function
+
+ target = mock.Mock(spec=["__call__"], side_effect=[42])
+ # __name__ is needed by functools.partial.
+ target.__name__ = "target"
+
+ wrapped = retry_(target)
+
+ result = wrapped("meep")
+
+ assert result == 42
+ target.assert_called_once_with("meep")
+ sleep.assert_not_called()
+ _some_function.assert_not_called()
+
+ # Make uniform return half of its maximum, which is the calculated sleep time.
+ @mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n / 2.0)
+ @mock.patch("time.sleep", autospec=True)
+ def test___init___when_retry_is_executed(self, sleep, uniform):
+ _some_function = mock.Mock()
+
+ retry_ = retry.Retry(
+ predicate=retry.if_exception_type(ValueError), on_error=_some_function
+ )
+ # check the proper creation of the class
+ assert retry_._on_error is _some_function
+
+ target = mock.Mock(
+ spec=["__call__"], side_effect=[ValueError(), ValueError(), 42]
+ )
+ # __name__ is needed by functools.partial.
+ target.__name__ = "target"
+
+ wrapped = retry_(target)
+ target.assert_not_called()
+
+ result = wrapped("meep")
+
+ assert result == 42
+ assert target.call_count == 3
+ assert _some_function.call_count == 2
+ target.assert_has_calls([mock.call("meep"), mock.call("meep")])
+ sleep.assert_any_call(retry_._initial)