|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify that replay capture cleanup reaps responsive and stalled children.""" |
| 3 | +import importlib.util |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import signal |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | + |
| 12 | +spec = importlib.util.spec_from_file_location("capture", Path(__file__).with_name("capture-replay.py")) |
| 13 | +capture = importlib.util.module_from_spec(spec) |
| 14 | +spec.loader.exec_module(capture) |
| 15 | + |
| 16 | + |
| 17 | +@unittest.skipUnless(os.name == "posix", "Process cleanup tests require POSIX signals") |
| 18 | +class CaptureCleanupTests(unittest.TestCase): |
| 19 | + def check_child(self, ignore_termination, expected_signal): |
| 20 | + script = ( |
| 21 | + "import signal\n" |
| 22 | + + ("signal.signal(signal.SIGTERM, signal.SIG_IGN)\n" if ignore_termination else "") |
| 23 | + + "print('ready', flush=True)\nsignal.pause()\n" |
| 24 | + ) |
| 25 | + process = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE, text=True) |
| 26 | + try: |
| 27 | + self.assertEqual(process.stdout.readline(), "ready\n") |
| 28 | + capture.stop_process(process, timeout=0.1) |
| 29 | + self.assertEqual(process.returncode, -expected_signal) |
| 30 | + finally: |
| 31 | + if process.poll() is None: |
| 32 | + process.kill() |
| 33 | + process.wait(timeout=5) |
| 34 | + process.stdout.close() |
| 35 | + |
| 36 | + def test_responsive_child_exits_on_termination(self): |
| 37 | + self.check_child(False, signal.SIGTERM) |
| 38 | + |
| 39 | + def test_stalled_child_is_killed_and_reaped(self): |
| 40 | + self.check_child(True, signal.SIGKILL) |
| 41 | + |
| 42 | + def test_completed_child_keeps_its_exit_status(self): |
| 43 | + process = subprocess.Popen([sys.executable, "-c", "pass"]) |
| 44 | + process.wait(timeout=5) |
| 45 | + capture.stop_process(process, timeout=0.1) |
| 46 | + self.assertEqual(process.returncode, 0) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + unittest.main() |
0 commit comments