From bb350a3b8e7c48a16ca91b0e756a70ce2ee38fa1 Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Wed, 29 Mar 2017 02:13:24 +0200 Subject: [PATCH] autotests: Avoid periodic polling in the wait methods Modify AsyncOpAbstract._wait_for_async_op and IWD.wait_for_object_condition to call context.iteration() in the blocking mode instead of calling context.pending() every 0.01s. This gets rid of busy-waiting and also ensures that the condition is checked after every single dbus (or other) event. This way a condition that potentially occurs for less than 0.01s can be reliably waited for. --- autotests/util/iwd.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/autotests/util/iwd.py b/autotests/util/iwd.py index 6ff8cbd3..b3160305 100755 --- a/autotests/util/iwd.py +++ b/autotests/util/iwd.py @@ -94,10 +94,7 @@ class AsyncOpAbstract(object): def _wait_for_async_op(self): context = mainloop.get_context() while not self._is_completed: - if context.pending(): - context.iteration() - else: - time.sleep(0.01) + context.iteration(may_block=True) self._is_completed = False if self._exception is not None: @@ -578,18 +575,20 @@ class IWD(AsyncOpAbstract): return _known_network_manager_if def wait_for_object_condition(self, obj, condition_str, max_wait = 15): - start = time.time() + self._wait_timed_out = False + def wait_timeout_cb(): + self._wait_timed_out = True + return False + + timeout = GLib.timeout_add_seconds(max_wait, wait_timeout_cb) context = mainloop.get_context() while not eval(condition_str): - if context.pending(): - context.iteration() - else: - now = time.time() - if (now - start) > max_wait: - raise TimeoutError('[' + condition_str + ']'\ - ' condition was not met in '\ - + str(max_wait) + ' sec') - time.sleep(0.01) + context.iteration(may_block=True) + if self._wait_timed_out: + raise TimeoutError('[' + condition_str + ']'\ + ' condition was not met in '\ + + str(max_wait) + ' sec') + GLib.source_remove(timeout) @staticmethod def clear_storage():