summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-10-01 20:17:41 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-10-01 20:17:41 +0200
commit1b921b69c79539f7dfa06c7189ab8be9b928d068 (patch)
treef31a0e60aa06a3602a5281cad4f42abfdfee5533
parentfe02332040d61951951945300c754aaaa7359026 (diff)
downloadpsutil-1b921b69c79539f7dfa06c7189ab8be9b928d068.tar.gz
refactoring
-rw-r--r--psutil/tests/test_linux.py77
-rw-r--r--psutil/tests/test_testutils.py35
2 files changed, 65 insertions, 47 deletions
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 7ee92597..ab617f2c 100644
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -1007,47 +1007,42 @@ class TestProcess(unittest.TestCase):
# For all those cases we check that the value found in
# /proc/pid/stat (by psutil) matches the one found in
# /proc/pid/status.
- for p in psutil.process_iter():
- try:
- f = psutil._psplatform.open_text('/proc/%s/status' % p.pid)
- except IOError:
- pass
- else:
- with f:
- for line in f:
- line = line.strip()
- if line.startswith('Name:'):
- name = line.split()[1]
- # Name is truncated to 15 chars
- self.assertEqual(p.name()[:15], name[:15])
- elif line.startswith('State:'):
- status = line[line.find('(') + 1:line.rfind(')')]
- status = status.replace(' ', '-')
- self.assertEqual(p.status(), status)
- elif line.startswith('PPid:'):
- ppid = int(line.split()[1])
- self.assertEqual(p.ppid(), ppid)
- # The ones below internally are determined by reading
- # 'status' file but we use a re to extract the info
- # so it makes sense to check them.
- elif line.startswith('Threads:'):
- num_threads = int(line.split()[1])
- self.assertEqual(p.num_threads(), num_threads)
- elif line.startswith('Uid:'):
- uids = tuple(map(int, line.split()[1:4]))
- self.assertEqual(tuple(p.uids()), uids)
- elif line.startswith('Gid:'):
- gids = tuple(map(int, line.split()[1:4]))
- self.assertEqual(tuple(p.gids()), gids)
- elif line.startswith('voluntary_ctxt_switches:'):
- vol = int(line.split()[1])
- self.assertAlmostEqual(
- p.num_ctx_switches().voluntary, vol, delta=2)
- elif line.startswith('nonvoluntary_ctxt_switches:'):
- invol = int(line.split()[1])
- self.assertAlmostEqual(
- p.num_ctx_switches().involuntary, invol,
- delta=2)
+ p = psutil.Process()
+ with psutil._psplatform.open_text('/proc/%s/status' % p.pid) as f:
+ for line in f:
+ line = line.strip()
+ if line.startswith('Name:'):
+ name = line.split()[1]
+ # Name is truncated to 15 chars
+ self.assertEqual(p.name()[:15], name[:15])
+ elif line.startswith('State:'):
+ status = line[line.find('(') + 1:line.rfind(')')]
+ status = status.replace(' ', '-')
+ self.assertEqual(p.status(), status)
+ elif line.startswith('PPid:'):
+ ppid = int(line.split()[1])
+ self.assertEqual(p.ppid(), ppid)
+ # The ones below internally are determined by reading
+ # 'status' file but we use a re to extract the info
+ # so it makes sense to check them.
+ elif line.startswith('Threads:'):
+ num_threads = int(line.split()[1])
+ self.assertEqual(p.num_threads(), num_threads)
+ elif line.startswith('Uid:'):
+ uids = tuple(map(int, line.split()[1:4]))
+ self.assertEqual(tuple(p.uids()), uids)
+ elif line.startswith('Gid:'):
+ gids = tuple(map(int, line.split()[1:4]))
+ self.assertEqual(tuple(p.gids()), gids)
+ elif line.startswith('voluntary_ctxt_switches:'):
+ vol = int(line.split()[1])
+ self.assertAlmostEqual(
+ p.num_ctx_switches().voluntary, vol, delta=2)
+ elif line.startswith('nonvoluntary_ctxt_switches:'):
+ invol = int(line.split()[1])
+ self.assertAlmostEqual(
+ p.num_ctx_switches().involuntary, invol,
+ delta=2)
def test_memory_full_info(self):
src = textwrap.dedent("""
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
index eccb7226..3ba5e164 100644
--- a/psutil/tests/test_testutils.py
+++ b/psutil/tests/test_testutils.py
@@ -20,7 +20,7 @@ class TestRetryDecorator(unittest.TestCase):
def test_retry_success(self, sleep):
# Fail 3 times out of 5; make sure the decorated fun returns.
- @retry(retries=5, logfun=None)
+ @retry(retries=5, interval=1, logfun=None)
def foo():
while queue:
queue.pop()
@@ -29,12 +29,13 @@ class TestRetryDecorator(unittest.TestCase):
queue = list(range(3))
self.assertEqual(foo(), 1)
+ self.assertEqual(sleep.call_count, 3)
@mock.patch('time.sleep')
def test_retry_failure(self, sleep):
# Fail 6 times out of 5; th function is supposed to raise exc.
- @retry(retries=5, logfun=None)
+ @retry(retries=5, interval=1, logfun=None)
def foo():
while queue:
queue.pop()
@@ -43,18 +44,40 @@ class TestRetryDecorator(unittest.TestCase):
queue = list(range(6))
self.assertRaises(ZeroDivisionError, foo)
+ self.assertEqual(sleep.call_count, 5)
@mock.patch('time.sleep')
- def test_exception(self, sleep):
-
- @retry(exception=ValueError)
+ def test_exception_arg(self, sleep):
+ @retry(exception=ValueError, interval=1)
def foo():
raise TypeError
self.assertRaises(TypeError, foo)
+ self.assertEqual(sleep.call_count, 0)
+
+ @mock.patch('time.sleep')
+ def test_no_interval_arg(self, sleep):
+ # if interval is not specified sleep is not supposed to be called
+
+ @retry(retries=5, interval=None, logfun=None)
+ def foo():
+ 1 / 0
+
+ self.assertRaises(ZeroDivisionError, foo)
+ self.assertEqual(sleep.call_count, 0)
+
+ @mock.patch('time.sleep')
+ def test_retries_arg(self, sleep):
+
+ @retry(retries=5, interval=1, logfun=None)
+ def foo():
+ 1 / 0
+
+ self.assertRaises(ZeroDivisionError, foo)
+ self.assertEqual(sleep.call_count, 5)
@mock.patch('time.sleep')
- def test_retries_and_timeout(self, sleep):
+ def test_retries_and_timeout_args(self, sleep):
self.assertRaises(ValueError, retry, retries=5, timeout=1)