Skip to content

Commit

Permalink
change call_until() signature so that it can be used with lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 5, 2024
1 parent 809dd5a commit 4e85bee
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
10 changes: 4 additions & 6 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def spawn_zombie():
zpid = int(conn.recv(1024))
_pids_started.add(zpid)
zombie = psutil.Process(zpid)
call_until(zombie.status, "ret == psutil.STATUS_ZOMBIE")
call_until(lambda: zombie.status() == psutil.STATUS_ZOMBIE)
return (parent, zombie)
finally:
conn.close()
Expand Down Expand Up @@ -792,12 +792,10 @@ def wait_for_file(fname, delete=True, empty=False):
timeout=GLOBAL_TIMEOUT,
interval=0.001,
)
def call_until(fun, expr):
"""Keep calling function for timeout secs and exit if eval()
expression is True.
"""
def call_until(fun):
"""Keep calling function until it evaluates to True."""
ret = fun()
assert eval(expr) # noqa
assert ret
return ret


Expand Down
6 changes: 3 additions & 3 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ def test_open_files_file_gone(self):
files = p.open_files()
with open(self.get_testfn(), 'w'):
# give the kernel some time to see the new file
call_until(p.open_files, "len(ret) != %i" % len(files))
call_until(lambda: len(p.open_files()) != len(files))
with mock.patch(
'psutil._pslinux.os.readlink',
side_effect=OSError(errno.ENOENT, ""),
Expand All @@ -1977,7 +1977,7 @@ def test_open_files_fd_gone(self):
files = p.open_files()
with open(self.get_testfn(), 'w'):
# give the kernel some time to see the new file
call_until(p.open_files, "len(ret) != %i" % len(files))
call_until(lambda: len(p.open_files()) != len(files))
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
with mock.patch(
patch_point, side_effect=IOError(errno.ENOENT, "")
Expand All @@ -1993,7 +1993,7 @@ def test_open_files_enametoolong(self):
files = p.open_files()
with open(self.get_testfn(), 'w'):
# give the kernel some time to see the new file
call_until(p.open_files, "len(ret) != %i" % len(files))
call_until(lambda: len(p.open_files()) != len(files))
patch_point = 'psutil._pslinux.os.readlink'
with mock.patch(
patch_point, side_effect=OSError(errno.ENAMETOOLONG, "")
Expand Down
7 changes: 4 additions & 3 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def test_cwd_2(self):
),
]
p = self.spawn_psproc(cmd)
call_until(p.cwd, "ret == os.path.dirname(os.getcwd())")
call_until(lambda: p.cwd() == os.path.dirname(os.getcwd()))

@unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
def test_cpu_affinity(self):
Expand Down Expand Up @@ -1075,7 +1075,8 @@ def test_open_files(self):
f.write(b'x' * 1024)
f.flush()
# give the kernel some time to see the new file
files = call_until(p.open_files, "len(ret) != %i" % len(files))
call_until(lambda: len(p.open_files()) != len(files))
files = p.open_files()
filenames = [os.path.normcase(x.path) for x in files]
assert os.path.normcase(testfn) in filenames
if LINUX:
Expand Down Expand Up @@ -1399,7 +1400,7 @@ def assert_raises_nsp(fun, fun_name):
p.terminate()
p.wait()
if WINDOWS: # XXX
call_until(psutil.pids, "%s not in ret" % p.pid)
call_until(lambda: p.pid not in psutil.pids())
self.assertProcessGone(p)

ns = process_namespace(p)
Expand Down
3 changes: 1 addition & 2 deletions psutil/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ def test_wait_for_file_no_delete(self):
assert os.path.exists(testfn)

def test_call_until(self):
ret = call_until(lambda: 1, "ret == 1")
assert ret == 1
call_until(lambda: 1)


class TestFSTestUtils(PsutilTestCase):
Expand Down

0 comments on commit 4e85bee

Please sign in to comment.