summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-05-18 21:30:20 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-05-18 21:30:20 +0200
commitcab78f649d4a3a0a7c0bef526ff78a8924e90159 (patch)
treed292c0245f6974efcfc6857316255c3082e32047
parent96f9a5236b16a312d6fb8a751001605e59a4be0b (diff)
downloadpsutil-cab78f649d4a3a0a7c0bef526ff78a8924e90159.tar.gz
refactor some tests
-rw-r--r--psutil/tests/__init__.py4
-rwxr-xr-xpsutil/tests/test_contracts.py5
-rwxr-xr-xpsutil/tests/test_process.py77
3 files changed, 48 insertions, 38 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index df94d737..d3d02d11 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -935,7 +935,7 @@ class TestMemoryLeak(PsutilTestCase):
times = 200
warmup_times = 10
tolerance = 0 # memory
- retries = 5
+ retries = 10 if CI_TESTING else 5
verbose = True
_thisproc = psutil.Process()
@@ -998,7 +998,7 @@ class TestMemoryLeak(PsutilTestCase):
msg = "Run #%s: extra-mem=%s, per-call=%s, calls=%s" % (
idx, bytes2human(mem), bytes2human(mem / times), times)
messages.append(msg)
- success = mem <= tolerance or mem < prev_mem
+ success = mem <= tolerance or mem <= prev_mem
if success:
if idx > 1:
self._log(msg)
diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py
index a57e19e9..57d11b43 100755
--- a/psutil/tests/test_contracts.py
+++ b/psutil/tests/test_contracts.py
@@ -14,6 +14,7 @@ import multiprocessing
import os
import signal
import stat
+import sys
import time
import traceback
@@ -672,6 +673,10 @@ class TestFetchAllProcesses(PsutilTestCase):
priorities = [getattr(psutil, x) for x in dir(psutil)
if x.endswith('_PRIORITY_CLASS')]
self.assertIn(ret, priorities)
+ if sys.version_info > (3, 4):
+ self.assertIsInstance(ret, enum.IntEnum)
+ else:
+ self.assertIsInstance(ret, int)
def num_ctx_switches(self, ret, info):
assert is_namedtuple(ret)
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 4837ac8c..4a21cae5 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -41,7 +41,6 @@ from psutil.tests import CI_TESTING
from psutil.tests import CIRRUS
from psutil.tests import copyload_shared_lib
from psutil.tests import create_exe
-from psutil.tests import enum
from psutil.tests import GITHUB_WHEELS
from psutil.tests import GLOBAL_TIMEOUT
from psutil.tests import HAS_CPU_AFFINITY
@@ -370,7 +369,10 @@ class TestProcess(PsutilTestCase):
self.assertEqual(tuple(p.ionice()), (psutil.IOPRIO_CLASS_BE, 7))
with self.assertRaises(ValueError):
p.ionice(psutil.IOPRIO_CLASS_BE, value=8)
- p.ionice(psutil.IOPRIO_CLASS_RT, value=7)
+ try:
+ p.ionice(psutil.IOPRIO_CLASS_RT, value=7)
+ except psutil.AccessDenied:
+ pass
# errs
self.assertRaisesRegex(
ValueError, "ioclass accepts no value",
@@ -792,43 +794,46 @@ class TestProcess(PsutilTestCase):
def test_nice(self):
p = psutil.Process()
self.assertRaises(TypeError, p.nice, "str")
- if WINDOWS:
- try:
- init = p.nice()
- if sys.version_info > (3, 4):
- self.assertIsInstance(init, enum.IntEnum)
- else:
- self.assertIsInstance(init, int)
- self.assertEqual(init, psutil.NORMAL_PRIORITY_CLASS)
- p.nice(psutil.HIGH_PRIORITY_CLASS)
- self.assertEqual(p.nice(), psutil.HIGH_PRIORITY_CLASS)
- p.nice(psutil.NORMAL_PRIORITY_CLASS)
- self.assertEqual(p.nice(), psutil.NORMAL_PRIORITY_CLASS)
- finally:
- p.nice(psutil.NORMAL_PRIORITY_CLASS)
- else:
- first_nice = p.nice()
- try:
- if hasattr(os, "getpriority"):
- self.assertEqual(
- os.getpriority(os.PRIO_PROCESS, os.getpid()), p.nice())
- p.nice(1)
- self.assertEqual(p.nice(), 1)
- if hasattr(os, "getpriority"):
- self.assertEqual(
- os.getpriority(os.PRIO_PROCESS, os.getpid()), p.nice())
- # XXX - going back to previous nice value raises
- # AccessDenied on MACOS
- if not MACOS:
- p.nice(0)
- self.assertEqual(p.nice(), 0)
- except psutil.AccessDenied:
- pass
- finally:
+ init = p.nice()
+ try:
+ if WINDOWS:
+ for prio in [psutil.NORMAL_PRIORITY_CLASS,
+ psutil.IDLE_PRIORITY_CLASS,
+ psutil.BELOW_NORMAL_PRIORITY_CLASS,
+ psutil.REALTIME_PRIORITY_CLASS,
+ psutil.HIGH_PRIORITY_CLASS,
+ psutil.ABOVE_NORMAL_PRIORITY_CLASS]:
+ with self.subTest(prio=prio):
+ try:
+ p.nice(prio)
+ except psutil.AccessDenied:
+ pass
+ else:
+ self.assertEqual(p.nice(), prio)
+ else:
try:
- p.nice(first_nice)
+ if hasattr(os, "getpriority"):
+ self.assertEqual(
+ os.getpriority(os.PRIO_PROCESS, os.getpid()),
+ p.nice())
+ p.nice(1)
+ self.assertEqual(p.nice(), 1)
+ if hasattr(os, "getpriority"):
+ self.assertEqual(
+ os.getpriority(os.PRIO_PROCESS, os.getpid()),
+ p.nice())
+ # XXX - going back to previous nice value raises
+ # AccessDenied on MACOS
+ if not MACOS:
+ p.nice(0)
+ self.assertEqual(p.nice(), 0)
except psutil.AccessDenied:
pass
+ finally:
+ try:
+ p.nice(init)
+ except psutil.AccessDenied:
+ pass
def test_status(self):
p = psutil.Process()