summaryrefslogtreecommitdiff
path: root/psutil/tests/test_testutils.py
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-04-28 12:06:38 -0700
committerGitHub <noreply@github.com>2020-04-28 21:06:38 +0200
commit0065a3921b1cf31ad32cca5bd3ca209fe1fbceed (patch)
treec614e4f4e34eb3b12d2ffaeac1d0bd3374df6ccb /psutil/tests/test_testutils.py
parent56db14e14797ac790094a7bd8865d63383fd93a7 (diff)
downloadpsutil-0065a3921b1cf31ad32cca5bd3ca209fe1fbceed.tar.gz
Test sub-processes cleanup and ProcessTestCase class (#1739)
Diffstat (limited to 'psutil/tests/test_testutils.py')
-rwxr-xr-xpsutil/tests/test_testutils.py55
1 files changed, 41 insertions, 14 deletions
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
index fd32e0b7..b2be93ff 100755
--- a/psutil/tests/test_testutils.py
+++ b/psutil/tests/test_testutils.py
@@ -16,6 +16,7 @@ import io
import os
import socket
import stat
+import subprocess
from psutil import FREEBSD
from psutil import NETBSD
@@ -29,15 +30,14 @@ from psutil.tests import bind_socket
from psutil.tests import bind_unix_socket
from psutil.tests import call_until
from psutil.tests import chdir
-from psutil.tests import create_proc_children_pair
from psutil.tests import create_sockets
-from psutil.tests import create_zombie_proc
from psutil.tests import get_free_port
-from psutil.tests import get_test_subprocess
from psutil.tests import get_testfn
from psutil.tests import HAS_CONNECTIONS_UNIX
from psutil.tests import is_namedtuple
from psutil.tests import mock
+from psutil.tests import ProcessTestCase
+from psutil.tests import PYTHON_EXE
from psutil.tests import reap_children
from psutil.tests import retry
from psutil.tests import retry_on_failure
@@ -209,10 +209,10 @@ class TestFSTestUtils(unittest.TestCase):
self.assertEqual(os.getcwd(), base)
-class TestProcessUtils(unittest.TestCase):
+class TestProcessUtils(ProcessTestCase):
def test_reap_children(self):
- subp = get_test_subprocess()
+ subp = self.get_test_subprocess()
p = psutil.Process(subp.pid)
assert p.is_running()
reap_children()
@@ -221,7 +221,7 @@ class TestProcessUtils(unittest.TestCase):
assert not psutil.tests._subprocesses_started
def test_create_proc_children_pair(self):
- p1, p2 = create_proc_children_pair()
+ p1, p2 = self.create_proc_children_pair()
self.assertNotEqual(p1.pid, p2.pid)
assert p1.is_running()
assert p2.is_running()
@@ -241,11 +241,38 @@ class TestProcessUtils(unittest.TestCase):
@unittest.skipIf(not POSIX, "POSIX only")
def test_create_zombie_proc(self):
- parent, zombie = create_zombie_proc()
- self.addCleanup(terminate, zombie)
- self.addCleanup(terminate, parent) # executed first
+ parent, zombie = self.create_zombie_proc()
self.assertEqual(zombie.status(), psutil.STATUS_ZOMBIE)
+ def test_terminate(self):
+ # by subprocess.Popen
+ p = self.get_test_subprocess()
+ terminate(p)
+ assert not psutil.pid_exists(p.pid)
+ terminate(p)
+ # by psutil.Process
+ p = psutil.Process(self.get_test_subprocess().pid)
+ terminate(p)
+ assert not psutil.pid_exists(p.pid)
+ terminate(p)
+ # by psutil.Popen
+ cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
+ p = psutil.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ terminate(p)
+ assert not psutil.pid_exists(p.pid)
+ terminate(p)
+ # by PID
+ pid = self.get_test_subprocess().pid
+ terminate(pid)
+ assert not psutil.pid_exists(pid)
+ terminate(pid)
+ # zombie
+ parent, zombie = self.create_zombie_proc()
+ terminate(parent)
+ terminate(zombie)
+ assert not psutil.pid_exists(parent.pid)
+ assert not psutil.pid_exists(zombie.pid)
+
class TestNetUtils(unittest.TestCase):
@@ -377,9 +404,9 @@ class TestMemLeakClass(TestMemoryLeak):
def test_execute_w_exc(self):
def fun():
1 / 0
- self.execute_w_exc(ZeroDivisionError, fun, times=2000, warmup_times=20,
- tolerance=4096, retry_for=3)
-
+ # XXX: use high tolerance, occasional false positive
+ self.execute_w_exc(ZeroDivisionError, fun, times=2000,
+ warmup_times=20, tolerance=200 * 1024, retry_for=3)
with self.assertRaises(ZeroDivisionError):
self.execute_w_exc(OSError, fun)
@@ -397,5 +424,5 @@ class TestOtherUtils(unittest.TestCase):
if __name__ == '__main__':
- from psutil.tests.runner import run
- run(__file__)
+ from psutil.tests.runner import run_from_name
+ run_from_name(__file__)