From 1d092e728abddb628738d2f0a110b74152969771 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 12 Jun 2024 00:15:21 +0200 Subject: [PATCH] test subprocesses: sleep() with an interval of 0.1 to make the test process more 'killable' --- psutil/tests/__init__.py | 2 +- psutil/tests/test_connections.py | 6 ++-- psutil/tests/test_process.py | 52 ++++++++++++++++++++++++++------ psutil/tests/test_testutils.py | 6 +++- psutil/tests/test_unicode.py | 18 +++++++++-- 5 files changed, 67 insertions(+), 17 deletions(-) diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index b18b74239..684c68ae2 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -408,7 +408,7 @@ def spawn_children_pair(): s += "f = open('%s', 'w');" s += "f.write(str(os.getpid()));" s += "f.close();" - s += "time.sleep(60);" + s += "[time.sleep(0.1) for x in range(100 * 6)];" p = subprocess.Popen([r'%s', '-c', s]) p.wait() """ % (os.path.basename(testfn), PYTHON_EXE)) diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index de3ae59df..4a0674d62 100755 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -328,7 +328,7 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): s.listen(5) with open('{testfn}', 'w') as f: f.write(str(s.getsockname()[:2])) - time.sleep(60) + [time.sleep(0.1) for x in range(100)] """) udp_template = textwrap.dedent(""" @@ -337,7 +337,7 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): s.bind(('{addr}', 0)) with open('{testfn}', 'w') as f: f.write(str(s.getsockname()[:2])) - time.sleep(60) + [time.sleep(0.1) for x in range(100)] """) # must be relative on Windows @@ -530,7 +530,7 @@ def test_multi_sockets_procs(self): with create_sockets(): with open(r'%s', 'w') as f: f.write("hello") - time.sleep(60) + [time.sleep(0.1) for x in range(100)] """ % fname) sproc = self.pyrun(src) pids.append(sproc.pid) diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 4f80277b7..1ee5393c2 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -255,7 +255,8 @@ def test_cpu_percent_numcpus_none(self): def test_cpu_times(self): times = psutil.Process().cpu_times() - assert (times.user > 0.0) or (times.system > 0.0), times + assert times.user >= 0.0, times + assert times.system >= 0.0, times assert times.children_user >= 0.0, times assert times.children_system >= 0.0, times if LINUX: @@ -727,8 +728,17 @@ def test_exe(self): self.assertEqual(out, 'hey') def test_cmdline(self): - cmdline = [PYTHON_EXE, "-c", "import time; time.sleep(60)"] + cmdline = [ + PYTHON_EXE, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] p = self.spawn_psproc(cmdline) + + if NETBSD and p.cmdline() == []: + # https://github.com/giampaolo/psutil/issues/2250 + raise unittest.SkipTest("OPENBSD: returned EBUSY") + # XXX - most of the times the underlying sysctl() call on Net # and Open BSD returns a truncated string. # Also /proc/pid/cmdline behaves the same so it looks @@ -750,7 +760,9 @@ def test_cmdline(self): def test_long_cmdline(self): cmdline = [PYTHON_EXE] cmdline.extend(["-v"] * 50) - cmdline.extend(["-c", "import time; time.sleep(10)"]) + cmdline.extend( + ["-c", "import time; [time.sleep(0.1) for x in range(100)]"] + ) p = self.spawn_psproc(cmdline) if OPENBSD: # XXX: for some reason the test process may turn into a @@ -776,7 +788,11 @@ def test_name(self): @unittest.skipIf(PYPY, "unreliable on PYPY") def test_long_name(self): pyexe = create_py_exe(self.get_testfn(suffix="0123456789" * 2)) - cmdline = [pyexe, "-c", "import time; time.sleep(10)"] + cmdline = [ + pyexe, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] p = self.spawn_psproc(cmdline) if OPENBSD: # XXX: for some reason the test process may turn into a @@ -805,7 +821,11 @@ def test_prog_w_funky_name(self): # with funky chars such as spaces and ")", see: # https://github.com/giampaolo/psutil/issues/628 pyexe = create_py_exe(self.get_testfn(suffix='foo bar )')) - cmdline = [pyexe, "-c", "import time; time.sleep(10)"] + cmdline = [ + pyexe, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] p = self.spawn_psproc(cmdline) self.assertEqual(p.cmdline(), cmdline) self.assertEqual(p.name(), os.path.basename(pyexe)) @@ -931,7 +951,10 @@ def test_cwd_2(self): cmd = [ PYTHON_EXE, "-c", - "import os, time; os.chdir('..'); time.sleep(60)", + ( + "import os, time; os.chdir('..'); [time.sleep(0.1) for x in" + " range(100)]" + ), ] p = self.spawn_psproc(cmd) call_until(p.cwd, "ret == os.path.dirname(os.getcwd())") @@ -1031,7 +1054,10 @@ def test_open_files(self): assert os.path.isfile(file.path), file # another process - cmdline = "import time; f = open(r'%s', 'r'); time.sleep(60);" % testfn + cmdline = ( + "import time; f = open(r'%s', 'r'); [time.sleep(0.1) for x in" + " range(100)];" % testfn + ) p = self.spawn_psproc([PYTHON_EXE, "-c", cmdline]) for x in range(100): @@ -1599,7 +1625,11 @@ def test_misc(self): # XXX this test causes a ResourceWarning on Python 3 because # psutil.__subproc instance doesn't get properly freed. # Not sure what to do though. - cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"] + cmd = [ + PYTHON_EXE, + "-c", + "import time; [time.sleep(0.1) for x in range(100)];", + ] with psutil.Popen( cmd, stdout=subprocess.PIPE, @@ -1635,7 +1665,11 @@ def test_kill_terminate(self): # subprocess.Popen()'s terminate(), kill() and send_signal() do # not raise exception after the process is gone. psutil.Popen # diverges from that. - cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"] + cmd = [ + PYTHON_EXE, + "-c", + "import time; [time.sleep(0.1) for x in range(100)];", + ] with psutil.Popen( cmd, stdout=subprocess.PIPE, diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py index 1a18b65a8..ef98a3abe 100755 --- a/psutil/tests/test_testutils.py +++ b/psutil/tests/test_testutils.py @@ -255,7 +255,11 @@ def test_terminate(self): self.assertPidGone(p.pid) terminate(p) # by psutil.Popen - cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"] + cmd = [ + PYTHON_EXE, + "-c", + "import time; [time.sleep(0.1) for x in range(100)];", + ] p = psutil.Popen( cmd, stdout=subprocess.PIPE, diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py index 7fb2ef199..45aeb4e92 100755 --- a/psutil/tests/test_unicode.py +++ b/psutil/tests/test_unicode.py @@ -197,7 +197,11 @@ def expect_exact_path_match(self): # --- def test_proc_exe(self): - cmd = [self.funky_name, "-c", "import time; time.sleep(10)"] + cmd = [ + self.funky_name, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] subp = self.spawn_testproc(cmd) p = psutil.Process(subp.pid) exe = p.exe() @@ -208,7 +212,11 @@ def test_proc_exe(self): ) def test_proc_name(self): - cmd = [self.funky_name, "-c", "import time; time.sleep(10)"] + cmd = [ + self.funky_name, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] subp = self.spawn_testproc(cmd) name = psutil.Process(subp.pid).name() self.assertIsInstance(name, str) @@ -216,7 +224,11 @@ def test_proc_name(self): self.assertEqual(name, os.path.basename(self.funky_name)) def test_proc_cmdline(self): - cmd = [self.funky_name, "-c", "import time; time.sleep(10)"] + cmd = [ + self.funky_name, + "-c", + "import time; [time.sleep(0.1) for x in range(100)]", + ] subp = self.spawn_testproc(cmd) p = psutil.Process(subp.pid) cmdline = p.cmdline()