Improve tenacity retry sleep mocking

When switching from the retrying module to tenacity, we had to do some
hacking to get the internal sleep calls for tenacity to not actually
sleep during our unit tests. This required a time critical aspect where
we had to mock sleep at the time tenacity was first loaded.

This worked fine for local changes, but when consuming library code that
also uses tenacity, like the new os-brick release, the library would end
up loading tenacity before our test code had a chance to do this
mocking.

Turns out that the tenacity devs actually did add a mechanism for
handling this after all. This simplifies all that mocking to use the
sleep kwarg into the tenacity initiator that we can then use to mock out
the sleep call in a more sane way.

Change-Id: Ida0192b2dc5b782bc2ee33746cc63da536c0a0e0
Signed-off-by: Sean McGinnis <sean.mcginnis@gmail.com>
This commit is contained in:
Sean McGinnis
2020-06-03 16:39:28 -05:00
parent 307fccea4c
commit ce5709d0e2
3 changed files with 9 additions and 27 deletions

View File

@@ -146,7 +146,7 @@ class TestCase(testtools.TestCase):
# Protect against any case where someone doesn't directly patch a retry
# decorated call.
self.patch('cinder.utils._time_sleep')
self.patch('tenacity.nap.sleep')
if self.MOCK_WORKER:
# Mock worker creation for all tests that don't care about it

View File

@@ -938,7 +938,7 @@ class TestRetryDecorator(test.TestCase):
def test_no_retry_required(self):
self.counter = 0
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException,
interval=2,
retries=3,
@@ -955,7 +955,7 @@ class TestRetryDecorator(test.TestCase):
def test_no_retry_required_random(self):
self.counter = 0
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException,
interval=2,
retries=3,
@@ -976,7 +976,7 @@ class TestRetryDecorator(test.TestCase):
backoff_rate = 2
retries = 3
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException,
interval,
retries,
@@ -1000,7 +1000,7 @@ class TestRetryDecorator(test.TestCase):
backoff_rate = 2
retries = 3
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException,
interval,
retries,
@@ -1025,7 +1025,7 @@ class TestRetryDecorator(test.TestCase):
interval = 2
backoff_rate = 4
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException,
interval,
retries,
@@ -1049,7 +1049,7 @@ class TestRetryDecorator(test.TestCase):
def test_wrong_exception_no_retry(self):
with mock.patch('cinder.utils._time_sleep') as mock_sleep:
with mock.patch('tenacity.nap.sleep') as mock_sleep:
@utils.retry(exception.VolumeBackendAPIException)
def raise_unexpected_error():
raise WrongException("wrong exception")