Skip to content

Commit

Permalink
test subprocesses: sleep() with an interval of 0.1 to make the test p…
Browse files Browse the repository at this point in the history
…rocess more 'killable'
  • Loading branch information
giampaolo committed Jun 11, 2024
1 parent 5f80c12 commit 1d092e7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions psutil/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 43 additions & 9 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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())")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion psutil/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions psutil/tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -208,15 +212,23 @@ 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)
if self.expect_exact_path_match():
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()
Expand Down

0 comments on commit 1d092e7

Please sign in to comment.