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): ...@@ -1833,24 +1833,34 @@ def ready(ch):
# procwait waits for a process (subprocess.Popen) to terminate. # procwait waits for a process (subprocess.Popen) to terminate.
def procwait(ctx, proc): 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) wg = sync.WorkGroup(ctx)
def _(ctx): def _(ctx):
while 1: while 1:
if ready(ctx.done()): if ready(ctx.done()):
raise ctx.err() raise ctx.err()
ret = proc.poll() if condf():
if ret is not None:
return return
tdelay() tdelay()
wg.go(_) wg.go(_)
wg.wait() 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. # it returns bool whether target condition was reached or not - e.g. due to
def procwait_(ctx, proc): # -> ok # context being canceled.
def waitfor_(ctx, proc): # -> ok
try: try:
procwait(ctx, proc) waitfor(ctx, proc)
except Exception as e: except Exception as e:
if errors.Is(e, context.canceled) or errors.Is(e, context.deadlineExceeded): if errors.Is(e, context.canceled) or errors.Is(e, context.deadlineExceeded):
return False 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