auto-t: iwd.py: use non_block_wait

This commit is contained in:
James Prestwood 2021-08-13 09:24:38 -07:00 committed by Denis Kenzior
parent 80ee406e3e
commit 732871b601
1 changed files with 25 additions and 86 deletions

View File

@ -109,9 +109,7 @@ class AsyncOpAbstract(object):
self._exception = _convert_dbus_ex(ex)
def _wait_for_async_op(self):
context = ctx.mainloop.get_context()
while not self._is_completed:
context.iteration(may_block=True)
ctx.non_block_wait(lambda s: s._is_completed, 30, self, exception=None)
self._is_completed = False
if self._exception is not None:
@ -1019,16 +1017,8 @@ class IWD(AsyncOpAbstract):
self._iwd_proc = self.namespace.start_iwd(iwd_config_dir,
iwd_storage_dir)
tries = 0
while not self._bus.name_has_owner(IWD_SERVICE):
if not ctx.args.gdb:
if tries > 200:
if start_iwd_daemon:
self.namespace.stop_process(self._iwd_proc)
self._iwd_proc = None
raise TimeoutError('IWD has failed to start')
tries += 1
time.sleep(0.1)
ctx.non_block_wait(self._bus.name_has_owner, 20, IWD_SERVICE,
exception=TimeoutError('IWD has failed to start'))
self._devices = DeviceList(self)
@ -1074,27 +1064,13 @@ class IWD(AsyncOpAbstract):
@staticmethod
def _wait_for_object_condition(obj, condition_str, max_wait = 50):
class TimeoutData:
_wait_timed_out = False
def _eval_wrap(obj, condition_str):
return eval(condition_str)
data = TimeoutData()
def wait_timeout_cb(data):
data._wait_timed_out = True
return False
try:
timeout = GLib.timeout_add_seconds(max_wait, wait_timeout_cb, data)
context = ctx.mainloop.get_context()
while not eval(condition_str):
context.iteration(may_block=True)
if data._wait_timed_out and ctx.args.gdb == None:
raise TimeoutError('[' + condition_str + ']'\
' condition was not met in '\
+ str(max_wait) + ' sec')
finally:
if not data._wait_timed_out:
GLib.source_remove(timeout)
ctx.non_block_wait(_eval_wrap, max_wait, obj, condition_str,
exception=TimeoutError('[' + condition_str + ']'\
' condition was not met in '\
+ str(max_wait) + ' sec'))
def wait_for_object_condition(self, *args, **kwargs):
self._wait_for_object_condition(*args, **kwargs)
@ -1108,54 +1084,30 @@ class IWD(AsyncOpAbstract):
This allows an object to be checked for a state transition without any
intermediate state changes.
'''
self._wait_timed_out = False
def _eval_from_to(obj, from_str, to_str):
# If neither the initial or expected condition evaluate the
# object must be in another unexpected state.
if not eval(from_str) and not eval(to_str):
raise Exception('unexpected condition between [%s] and [%s]' %
(from_str, to_str))
# Initial condition does not evaluate but expected does, pass
if not eval(from_str) and eval(to_str):
return True
def wait_timeout_cb():
self._wait_timed_out = True
return False
# Does initial condition pass?
if not eval(from_str):
raise Exception("initial condition [%s] not met" % from_str)
try:
timeout = GLib.timeout_add_seconds(max_wait, wait_timeout_cb)
context = ctx.mainloop.get_context()
while True:
context.iteration(may_block=True)
# If neither the initial or expected condition evaluate the
# object must be in another unexpected state.
if not eval(from_str) and not eval(to_str):
raise Exception('unexpected condition between [%s] and [%s]' %
(from_str, to_str))
# Initial condition does not evaluate but expected does, pass
if not eval(from_str) and eval(to_str):
break
if self._wait_timed_out and ctx.args.gdb == None:
raise TimeoutError('[' + to_str + ']'\
ctx.non_block_wait(_eval_from_to, max_wait, obj, from_str, to_str,
exception=TimeoutError('[' + to_str + ']'\
' condition was not met in '\
+ str(max_wait) + ' sec')
finally:
if not self._wait_timed_out:
GLib.source_remove(timeout)
+ str(max_wait) + ' sec'))
def wait(self, time):
self._wait_timed_out = False
def wait_timeout_cb():
self._wait_timed_out = True
return False
try:
timeout = GLib.timeout_add(int(time * 1000), wait_timeout_cb)
context = ctx.mainloop.get_context()
while not self._wait_timed_out:
context.iteration(may_block=True)
finally:
if not self._wait_timed_out:
GLib.source_remove(timeout)
ctx.non_block_wait(lambda : False, time, exception=False)
@staticmethod
def clear_storage(storage_dir=IWD_STORAGE_DIR):
@ -1200,21 +1152,8 @@ class IWD(AsyncOpAbstract):
if not wait_to_appear:
return list(self._devices.values() if not p2p else self._devices.p2p_dict.values())
self._wait_timed_out = False
def wait_timeout_cb():
self._wait_timed_out = True
return False
try:
timeout = GLib.timeout_add_seconds(max_wait, wait_timeout_cb)
context = ctx.mainloop.get_context()
while len(self._devices) < wait_to_appear:
context.iteration(may_block=True)
if self._wait_timed_out:
raise TimeoutError('IWD has no associated devices')
finally:
if not self._wait_timed_out:
GLib.source_remove(timeout)
ctx.non_block_wait(lambda s, n: len(s._devices) >= n, max_wait, self, wait_to_appear,
exception=TimeoutError('IWD has no associated devices'))
return list(self._devices.values() if not p2p else self._devices.p2p_dict.values())[:wait_to_appear]