Merge "Rewrite rally.utils.sync_execute() to wait_for()"
This commit is contained in:
commit
69ec96ca71
@ -126,32 +126,28 @@ def import_modules_from_package(package):
|
|||||||
try_append_module(module_name, sys.modules)
|
try_append_module(module_name, sys.modules)
|
||||||
|
|
||||||
|
|
||||||
def sync_execute(func, args, kwargs, is_ready, update_result=None,
|
def wait_for(resource, is_ready, update_resource=None, timeout=60, sleep=1):
|
||||||
timeout=60, sleep=1):
|
"""Waits for the given resource to come into the desired state.
|
||||||
"""Wraps an asynchronous function call into a synchronous one. Assumes that
|
|
||||||
the called function immediately returns an object for which it takes some
|
|
||||||
time to get to the 'ready for use' state.
|
|
||||||
|
|
||||||
:param func: Asynchronous function to be called
|
Uses the readiness check function passed as a parameter and (optionally)
|
||||||
:param args: List of args for the function to be called with
|
a function that updates the resource being waited for.
|
||||||
:param kwargs: Dict of named args for the function to be called with
|
|
||||||
:param is_ready: A predicate that should take the func(**kwarg) execution
|
:param is_ready: A predicate that should take the resource object and
|
||||||
result and return True iff it is ready to be returned
|
return True iff it is ready to be returned
|
||||||
:param update_result: Function that should take the func(**kwarg) execution
|
:param update_resource: Function that should take the resource object
|
||||||
result and return an 'updated' result. If set to
|
and return an 'updated' resource. If set to
|
||||||
None, no result updating is performed
|
None, no result updating is performed
|
||||||
:param timeout: Timeout in seconds after which a TimeoutException will be
|
:param timeout: Timeout in seconds after which a TimeoutException will be
|
||||||
raised
|
raised
|
||||||
:param sleep: Pause in seconds between the two consecutive readiness checks
|
:param sleep: Pause in seconds between the two consecutive readiness checks
|
||||||
|
|
||||||
:returns: The 'ready for use' result of func(*args, **kwargs) function call
|
:returns: The "ready" resource object
|
||||||
"""
|
"""
|
||||||
start = time.time()
|
start = time.time()
|
||||||
result = func(*args, **kwargs)
|
while not is_ready(resource):
|
||||||
while not is_ready(result):
|
|
||||||
time.sleep(sleep)
|
time.sleep(sleep)
|
||||||
if time.time() - start > timeout:
|
if time.time() - start > timeout:
|
||||||
raise exceptions.TimeoutException()
|
raise exceptions.TimeoutException()
|
||||||
if update_result:
|
if update_resource:
|
||||||
result = update_result(result)
|
resource = update_resource(resource)
|
||||||
return result
|
return resource
|
||||||
|
@ -145,24 +145,26 @@ class ImportModulesTestCase(test.NoDBTestCase):
|
|||||||
self.assertTrue('tests.fixtures.import.package.b' in sys.modules)
|
self.assertTrue('tests.fixtures.import.package.b' in sys.modules)
|
||||||
|
|
||||||
|
|
||||||
class SyncExecuteTestCase(test.NoDBTestCase):
|
class WaitForTestCase(test.NoDBTestCase):
|
||||||
|
|
||||||
def test_sync_execute(self):
|
def test_wait_for(self):
|
||||||
|
|
||||||
def fake_factory():
|
def get_fake_checker_delayed(**delay):
|
||||||
return object()
|
deadline = datetime.datetime.now() + datetime.timedelta(**delay)
|
||||||
|
return lambda obj: datetime.datetime.now() > deadline
|
||||||
|
|
||||||
def fake_checker_based_on_time(obj):
|
def fake_checker_false(obj):
|
||||||
return datetime.datetime.now().microsecond > 500000
|
|
||||||
|
|
||||||
def fake_checker_always_false(obj):
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def fake_updater(obj):
|
def fake_updater(obj):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
utils.sync_execute(fake_factory, [], {}, fake_checker_based_on_time,
|
resource = object()
|
||||||
|
fake_checker_delayed = get_fake_checker_delayed(seconds=0.5)
|
||||||
|
|
||||||
|
loaded_resource = utils.wait_for(resource, fake_checker_delayed,
|
||||||
fake_updater, 1, 0.2)
|
fake_updater, 1, 0.2)
|
||||||
self.assertRaises(exceptions.TimeoutException, utils.sync_execute,
|
self.assertEqual(loaded_resource, resource)
|
||||||
fake_factory, [], {}, fake_checker_always_false,
|
|
||||||
fake_updater, 0.3, 0.1)
|
self.assertRaises(exceptions.TimeoutException, utils.wait_for,
|
||||||
|
object(), fake_checker_false, fake_updater, 0.3, 0.1)
|
||||||
|
Loading…
Reference in New Issue
Block a user