Improve utils.wait_for()
1) Rename the 'sleep' parameter to 'check_interval' for more clarity; 2) Add another test for the case when update_resource=None (better coverage); 3) Reduce the running time of the method call in the tests. Change-Id: If2c6770fb18714fc8fbee0ac0493fb02bbdfea08
This commit is contained in:
@@ -126,7 +126,8 @@ def import_modules_from_package(package):
|
||||
try_append_module(module_name, sys.modules)
|
||||
|
||||
|
||||
def wait_for(resource, is_ready, update_resource=None, timeout=60, sleep=1):
|
||||
def wait_for(resource, is_ready, update_resource=None, timeout=60,
|
||||
check_interval=1):
|
||||
"""Waits for the given resource to come into the desired state.
|
||||
|
||||
Uses the readiness check function passed as a parameter and (optionally)
|
||||
@@ -139,13 +140,14 @@ def wait_for(resource, is_ready, update_resource=None, timeout=60, sleep=1):
|
||||
None, no result updating is performed
|
||||
:param timeout: Timeout in seconds after which a TimeoutException will be
|
||||
raised
|
||||
:param sleep: Pause in seconds between the two consecutive readiness checks
|
||||
:param check_interval: Interval in seconds between the two consecutive
|
||||
readiness checks
|
||||
|
||||
:returns: The "ready" resource object
|
||||
"""
|
||||
start = time.time()
|
||||
while not is_ready(resource):
|
||||
time.sleep(sleep)
|
||||
time.sleep(check_interval)
|
||||
if time.time() - start > timeout:
|
||||
raise exceptions.TimeoutException()
|
||||
if update_resource:
|
||||
|
@@ -160,11 +160,15 @@ class WaitForTestCase(test.NoDBTestCase):
|
||||
return obj
|
||||
|
||||
resource = object()
|
||||
fake_checker_delayed = get_fake_checker_delayed(seconds=0.5)
|
||||
fake_checker_delayed = get_fake_checker_delayed(seconds=0.3)
|
||||
|
||||
loaded_resource = utils.wait_for(resource, fake_checker_delayed,
|
||||
fake_updater, 1, 0.2)
|
||||
self.assertEqual(loaded_resource, resource)
|
||||
|
||||
loaded_resource = utils.wait_for(resource, fake_checker_delayed,
|
||||
None, 1, 0.2)
|
||||
self.assertEqual(loaded_resource, resource)
|
||||
|
||||
self.assertRaises(exceptions.TimeoutException, utils.wait_for,
|
||||
object(), fake_checker_false, fake_updater, 0.3, 0.1)
|
||||
|
Reference in New Issue
Block a user