Files
violin/tests/guard/test_psutil_execution.py

34 lines
881 B
Python

"""Unit tests for psutil process tree termination in execution.py."""
import subprocess
import sys
import time
import psutil
from plugins.violin_guard import execution
def test_terminate_process_tree_with_psutil():
# Spawn a python process that spawns a child process
cmd = [
sys.executable,
"-c",
"import subprocess, time; subprocess.Popen(['python', '-c', 'import time; time.sleep(60)']); time.sleep(60)",
]
proc = subprocess.Popen(cmd)
time.sleep(0.5)
assert proc.poll() is None
parent_ps = psutil.Process(proc.pid)
children = parent_ps.children(recursive=True)
assert len(children) >= 1
# Terminate process tree
execution._terminate_process(proc)
# Verify both parent and child processes are dead
assert proc.poll() is not None
for child in children:
assert not child.is_running()