Skip to content

Commit

Permalink
refact serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 15, 2024
1 parent e434d02 commit bc18030
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import psutil
import psutil.tests
from psutil import LINUX
from psutil import POSIX
from psutil import WINDOWS
from psutil._common import bcat
Expand All @@ -34,7 +33,6 @@
from psutil._compat import PY3
from psutil._compat import FileNotFoundError
from psutil._compat import redirect_stderr
from psutil.tests import APPVEYOR
from psutil.tests import CI_TESTING
from psutil.tests import HAS_BATTERY
from psutil.tests import HAS_MEMORY_MAPS
Expand All @@ -47,8 +45,10 @@
from psutil.tests import SCRIPTS_DIR
from psutil.tests import PsutilTestCase
from psutil.tests import mock
from psutil.tests import process_namespace
from psutil.tests import reload_module
from psutil.tests import sh
from psutil.tests import system_namespace


# ===================================================================
Expand Down Expand Up @@ -259,53 +259,66 @@ def test_process_as_dict_no_new_names(self):

def test_serialization(self):
def check(ret):
if json is not None:
json.loads(json.dumps(ret))
json.loads(json.dumps(ret))

a = pickle.dumps(ret)
b = pickle.loads(a)
self.assertEqual(ret, b)

# --- process APIs

proc = psutil.Process()
check(psutil.Process().as_dict())
check(psutil.virtual_memory())
check(psutil.swap_memory())
check(psutil.cpu_times())
check(psutil.cpu_times_percent(interval=0))
check(psutil.net_io_counters())
if LINUX and not os.path.exists('/proc/diskstats'):
pass
else:
if not APPVEYOR:
check(psutil.disk_io_counters())
check(psutil.disk_partitions())
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())

ns = process_namespace(proc)
for fun, name in ns.iter(ns.getters, clear_cache=True):
with self.subTest(proc=proc, name=name):
try:
ret = fun()
except psutil.Error:
pass
else:
check(ret)

# --- system APIs

ns = system_namespace()
for fun, name in ns.iter(ns.getters):
with self.subTest(name=name):
try:
ret = fun()
except psutil.AccessDenied:
pass
else:
check(ret)

# --- exception classes

b = pickle.loads(
pickle.dumps(
psutil.NoSuchProcess(
pid=4567, name='name test', msg='msg test'
)
psutil.NoSuchProcess(pid=4567, name='name', msg='msg')
)
)
self.assertIsInstance(b, psutil.NoSuchProcess)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.name, 'name test')
self.assertEqual(b.msg, 'msg test')
self.assertEqual(b.name, 'name')
self.assertEqual(b.msg, 'msg')

b = pickle.loads(
pickle.dumps(
psutil.ZombieProcess(
pid=4567, name='name test', ppid=42, msg='msg test'
)
psutil.ZombieProcess(pid=4567, name='name', ppid=42, msg='msg')
)
)
self.assertIsInstance(b, psutil.ZombieProcess)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.ppid, 42)
self.assertEqual(b.name, 'name test')
self.assertEqual(b.msg, 'msg test')
self.assertEqual(b.name, 'name')
self.assertEqual(b.msg, 'msg')

b = pickle.loads(
pickle.dumps(psutil.AccessDenied(pid=123, name='name', msg='msg'))
)
self.assertIsInstance(b, psutil.AccessDenied)
self.assertEqual(b.pid, 123)
self.assertEqual(b.name, 'name')
self.assertEqual(b.msg, 'msg')
Expand All @@ -315,6 +328,7 @@ def check(ret):
psutil.TimeoutExpired(seconds=33, pid=4567, name='name')
)
)
self.assertIsInstance(b, psutil.TimeoutExpired)
self.assertEqual(b.seconds, 33)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.name, 'name')
Expand Down

0 comments on commit bc18030

Please sign in to comment.