Commit c2c35851 authored by Kirill Smelkov's avatar Kirill Smelkov

X wcfs: tests: Factor-out waiting for a general condition to become true into waitfor

Currently in wcfs_test.py there is only waiting for a proc
(subprocess.Popen instance) to become ready. However in the next patch
we'll need to wait via polling for another condition.

-> Generalize the pollwait code into waitfor* variants, and make
procwait* use waitfor* internally.
parent 17f98edc
......@@ -1833,24 +1833,34 @@ def ready(ch):
# procwait waits for a process (subprocess.Popen) to terminate.
def procwait(ctx, proc):
waitfor(ctx, lambda: proc.poll() is not None)
# procwait_, similarly to procwait, waits for a process (subprocess.Popen) to terminate.
#
# it returns bool whether process terminated or not - e.g. due to context being canceled.
def procwait_(ctx, proc): # -> ok
return waitfor_(ctx, lambda: proc.poll() is not None)
# waitfor waits for condf() to become true.
def waitfor(ctx, condf):
wg = sync.WorkGroup(ctx)
def _(ctx):
while 1:
if ready(ctx.done()):
raise ctx.err()
ret = proc.poll()
if ret is not None:
if condf():
return
tdelay()
wg.go(_)
wg.wait()
# procwait_, similarly to procwait, waits for a process (subprocess.Popen) to terminate.
# waitfor_, similarly to waitfor, waits for condf() to become true.
#
# it returns bool whether process terminated or not - e.g. due to context being canceled.
def procwait_(ctx, proc): # -> ok
# it returns bool whether target condition was reached or not - e.g. due to
# context being canceled.
def waitfor_(ctx, proc): # -> ok
try:
procwait(ctx, proc)
waitfor(ctx, proc)
except Exception as e:
if errors.Is(e, context.canceled) or errors.Is(e, context.deadlineExceeded):
return False
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment