From b15a5a311f406001251a74cc103739cae3a25a01 Mon Sep 17 00:00:00 2001 From: Mikhail Dubov Date: Thu, 10 Oct 2013 01:31:55 +0400 Subject: [PATCH] Rewrite rally.utils.sync_execute() to wait_for() The patch simplifies the logic of rally.utils.sync_execute(), removing the function call from this method. It is assumed that the function call to create the desired resource will be performed by the developers before calling wait_for(), which would result in a bit of code duplicate but would significantly improve the overall code readability. Blueprint benchmark-scenarios Change-Id: Ieeb365d394b6dad6016047c3b4e6eb0e2543a613 --- rally/utils.py | 32 ++++++++++++++------------------ tests/test_utils.py | 28 +++++++++++++++------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/rally/utils.py b/rally/utils.py index f5fa83a449..4a60e02a0f 100644 --- a/rally/utils.py +++ b/rally/utils.py @@ -126,32 +126,28 @@ def import_modules_from_package(package): try_append_module(module_name, sys.modules) -def sync_execute(func, args, kwargs, is_ready, update_result=None, - timeout=60, sleep=1): - """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. +def wait_for(resource, is_ready, update_resource=None, timeout=60, sleep=1): + """Waits for the given resource to come into the desired state. - :param func: Asynchronous function to be called - :param args: List of args for the function to be called with - :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 - result and return True iff it is ready to be returned - :param update_result: Function that should take the func(**kwarg) execution - result and return an 'updated' result. If set to + Uses the readiness check function passed as a parameter and (optionally) + a function that updates the resource being waited for. + + :param is_ready: A predicate that should take the resource object and + return True iff it is ready to be returned + :param update_resource: Function that should take the resource object + and return an 'updated' resource. If set to 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 - :returns: The 'ready for use' result of func(*args, **kwargs) function call + :returns: The "ready" resource object """ start = time.time() - result = func(*args, **kwargs) - while not is_ready(result): + while not is_ready(resource): time.sleep(sleep) if time.time() - start > timeout: raise exceptions.TimeoutException() - if update_result: - result = update_result(result) - return result + if update_resource: + resource = update_resource(resource) + return resource diff --git a/tests/test_utils.py b/tests/test_utils.py index b547f78442..541172fb17 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -145,24 +145,26 @@ class ImportModulesTestCase(test.NoDBTestCase): 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(): - return object() + def get_fake_checker_delayed(**delay): + deadline = datetime.datetime.now() + datetime.timedelta(**delay) + return lambda obj: datetime.datetime.now() > deadline - def fake_checker_based_on_time(obj): - return datetime.datetime.now().microsecond > 500000 - - def fake_checker_always_false(obj): + def fake_checker_false(obj): return False def fake_updater(obj): return obj - utils.sync_execute(fake_factory, [], {}, fake_checker_based_on_time, - fake_updater, 1, 0.2) - self.assertRaises(exceptions.TimeoutException, utils.sync_execute, - fake_factory, [], {}, fake_checker_always_false, - fake_updater, 0.3, 0.1) + 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) + self.assertEqual(loaded_resource, resource) + + self.assertRaises(exceptions.TimeoutException, utils.wait_for, + object(), fake_checker_false, fake_updater, 0.3, 0.1)