Replace retrying with tenacity

We are replacing all usages of the retrying package with
tenacity with an end goal of removing the retrying package
from our requirements.

This patch updates all usages of retrying with tenacity.
Unit tests are added where applicable.

Note: This change is not newton critical so projects are welcome
to hold off on committing until post-newton. Ideally this change
will merge by the first part of Ocata so dependant functionality
can land and have time to solidify for Ocata.

Change-Id: Ia3760b9082beb4001b41e81caec2cbf73a1f5212
This commit is contained in:
Boden R 2016-09-01 09:52:29 -06:00
parent d7729f3836
commit 38d3aa469d
6 changed files with 42 additions and 21 deletions

View File

@ -0,0 +1,3 @@
# NOTE(boden): patch upfront to ensure all imports get patched modules
import eventlet
eventlet.monkey_patch()

View File

@ -15,6 +15,7 @@
import mock
import novaclient
import tenacity
import time
from oslo_config import cfg
@ -241,7 +242,7 @@ class TestDSE(base.TestCase):
'pub', 'p', [[3, 3]], 3, is_snapshot=True)
# check that out-of-sequence update not applied
self.assertRaises(
helper.TestFailureException,
tenacity.RetryError,
helper.retry_check_function_return_value,
lambda: sub.last_msg['data'], set([(3, 3)]))
# check that resub takes place, setting data to initial state
@ -523,7 +524,7 @@ class TestDSE(base.TestCase):
pe1.rpc('dsd', 'request_execute',
{'action': 'fake_act', 'action_args': {'name': 'testnode1'}})
self.assertRaises(
helper.TestFailureException,
tenacity.RetryError,
helper.retry_check_function_return_value,
lambda: len(dsd.exec_history), 3)

View File

@ -19,12 +19,13 @@ from __future__ import absolute_import
import json
import os
import tenacity
import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_messaging import conffixture
import retrying
from congress.datalog import compile
from congress.datalog import unify
@ -251,19 +252,22 @@ def form2str(formula):
return str(formula)
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_for_last_message(obj):
if not hasattr(obj, "last_msg"):
raise AttributeError("Missing 'last_msg' attribute")
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_for_message_to_arrive(obj):
if not hasattr(obj.msg, "body"):
raise AttributeError("Missing 'body' attribute")
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_for_message_data(obj, data):
if not hasattr(obj.msg, "body"):
raise AttributeError("Missing 'body' attribute")
@ -271,7 +275,8 @@ def retry_check_for_message_data(obj, data):
raise TestFailureException("Missing expected data in msg")
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_nonempty_last_policy_change(obj):
if not hasattr(obj, "last_policy_change"):
raise AttributeError("Missing 'last_policy_change' attribute")
@ -281,7 +286,8 @@ def retry_check_nonempty_last_policy_change(obj):
raise TestFailureException("last_policy_change == 0")
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_empty_last_policy_change(obj):
if not hasattr(obj, "last_policy_change"):
raise AttributeError("Missing 'last_policy_change' attribute")
@ -289,7 +295,8 @@ def retry_check_empty_last_policy_change(obj):
raise TestFailureException("last_policy_change != 0")
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_db_equal(policy, query, correct, target=None):
if not hasattr(policy, "select"):
raise AttributeError("Missing 'select' attribute")
@ -303,7 +310,8 @@ def retry_check_db_equal(policy, query, correct, target=None):
str(query), str(actual), str(correct)))
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_number_of_updates(deepsix, value):
if not hasattr(deepsix, "number_of_updates"):
raise AttributeError("Missing 'number_of_updates' attribute")
@ -312,7 +320,8 @@ def retry_check_number_of_updates(deepsix, value):
deepsix.number_of_updates, value))
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_subscriptions(deepsix, subscription_list):
if not check_subscriptions(deepsix, subscription_list):
raise TestFailureException(
@ -336,7 +345,8 @@ def check_subscriptions(deepsix, subscription_list):
return not missing
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_subscribers(deepsix, subscriber_list):
if not check_subscribers(deepsix, subscriber_list):
raise TestFailureException(
@ -344,7 +354,8 @@ def retry_check_subscribers(deepsix, subscriber_list):
deepsix.name, str(subscriber_list)))
@retrying.retry(stop_max_attempt_number=1000, wait_fixed=100)
@tenacity.retry(stop=tenacity.stop_after_attempt(1000),
wait=tenacity.wait_fixed(0.1))
def retry_check_no_subscribers(deepsix, subscriber_list):
"""Check that deepsix has none of the subscribers in subscriber_list"""
if check_subscribers(deepsix, subscriber_list, any_=True):
@ -374,7 +385,8 @@ def check_subscribers(deepsix, subscriber_list, any_=False):
return not missing
@retrying.retry(stop_max_attempt_number=20, wait_fixed=1000)
@tenacity.retry(stop=tenacity.stop_after_attempt(20),
wait=tenacity.wait_fixed(1))
def retry_check_function_return_value(f, expected_value):
"""Check if function f returns expected key."""
result = f()
@ -384,7 +396,8 @@ def retry_check_function_return_value(f, expected_value):
"Got %s instead." % (expected_value, result))
@retrying.retry(stop_max_attempt_number=10, wait_fixed=500)
@tenacity.retry(stop=tenacity.stop_after_attempt(10),
wait=tenacity.wait_fixed(0.5))
def retry_check_function_return_value_not_eq(f, value):
"""Check if function f does not return expected value."""
result = f()
@ -394,7 +407,8 @@ def retry_check_function_return_value_not_eq(f, value):
"from '%s'" % (result, value))
@retrying.retry(stop_max_attempt_number=10, wait_fixed=500)
@tenacity.retry(stop=tenacity.stop_after_attempt(10),
wait=tenacity.wait_fixed(0.5))
def retry_til_exception(expected_exception, f):
"""Check if function f does not return expected value."""
try:

View File

@ -17,9 +17,10 @@ from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import tenacity
from oslo_config import cfg
from oslo_log import log as logging
import retrying
from congress.datalog import base
from congress.datalog import compile
@ -207,7 +208,7 @@ class TestDsePerformance(testbase.SqlTestCase):
'schema': self.cage.service_object('api-schema')}
self.engine = self.cage.service_object('engine')
@retrying.retry(wait_fixed=100)
@tenacity.retry(wait=tenacity.wait_fixed(0.1))
def wait_til_query_nonempty(self, query, policy):
if len(self.engine.select(query, target=policy)) == 0:
raise Exception("Query %s is not empty" % query)

View File

@ -14,10 +14,12 @@
#
import os
import retrying
import tenacity
@retrying.retry(stop_max_attempt_number=20, wait_fixed=1000)
@tenacity.retry(stop=tenacity.stop_after_attempt(20),
wait=tenacity.wait_fixed(1))
def retry_check_function_return_value(f, expected_value, error_msg=None):
"""Check if function f returns expected value."""
if not error_msg:

View File

@ -12,7 +12,7 @@ python-subunit>=0.0.18 # Apache-2.0/BSD
testrepository>=0.0.18 # Apache-2.0/BSD
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=1.4.0 # MIT
retrying!=1.3.0,>=1.2.3 # Apache-2.0
tenacity>=3.0.0 # Apache-2.0
# Doc requirements
sphinx!=1.3b1,<1.3,>=1.2.1 # BSD