summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-05-03 17:41:40 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-05-03 17:41:40 +0200
commit336d20709f5a0524e673e1a72f4f5e282fd259b4 (patch)
treee9319bc4bd7dc8f328780f423f44069b5e654942
parent14486e98fa412d5ed522a25d32108612f3010aa4 (diff)
downloadpsutil-336d20709f5a0524e673e1a72f4f5e282fd259b4.tar.gz
add a process_namespace.iter() utility method to avoid doing functools.partial() every time
-rw-r--r--psutil/tests/__init__.py27
-rwxr-xr-xpsutil/tests/test_process.py7
2 files changed, 25 insertions, 9 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 104edbe8..986df572 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -1008,8 +1008,13 @@ class process_namespace:
Utilities such as parent(), children() and as_dict() are excluded.
Used by those tests who wish to call all Process methods in one shot
(and e.g. make sure they all raise NoSuchProcess).
- """
+ Usage:
+
+ >>> ns = process_namespace(proc)
+ >>> for fun, name in ns.iter(*ns.getters):
+ ... fun()
+ """
getters = []
for _name in psutil._as_dict_attrnames:
if _name == 'rlimit':
@@ -1066,10 +1071,24 @@ class process_namespace:
all = getters + setters + killers
del _name
- @staticmethod
- def clear_cache(proc):
+ def __init__(self, proc):
+ self._proc = proc
+
+ def iter(self, *tuples):
+ """Given a list of tuples yields a set of (fun, fun_name) tuples
+ in random order.
+ """
+ ls = list(tuples)
+ random.shuffle(ls)
+ for fun_name, args, kwds in ls:
+ self.clear_cache()
+ fun = getattr(self._proc, fun_name)
+ fun = functools.partial(fun, *args, **kwds)
+ yield (fun, fun_name)
+
+ def clear_cache(self):
"""Clear the cache of a Process instance."""
- proc._init(proc.pid, _ignore_nsp=True)
+ self._proc._init(self._proc.pid, _ignore_nsp=True)
@classmethod
def _test_this(cls):
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 361f6365..3f146b1c 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -8,7 +8,6 @@
import collections
import errno
-import functools
import getpass
import itertools
import os
@@ -1259,10 +1258,8 @@ class TestProcess(PsutilTestCase):
call_until(psutil.pids, "%s not in ret" % p.pid)
self.assertProcessGone(p)
- for name, args, kwds in process_namespace.all:
- process_namespace.clear_cache(p)
- fun = getattr(p, name)
- fun = functools.partial(fun, *args, **kwds)
+ ns = process_namespace(p)
+ for fun, name in ns.iter(*ns.all):
assert_raises_nsp(fun, name)
# NtQuerySystemInformation succeeds even if process is gone.