from pathlib import Path import subprocess import sys import unittest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'app')) import process_identity @unittest.skipUnless(sys.platform.startswith('linux'), 'requires Linux pidfds') class LinuxProcessIdentityTests(unittest.TestCase): def test_pidfd_is_bound_before_exact_identity_and_drives_termination(self): process = subprocess.Popen([ sys.executable, '-I', '-S', '-B', '-c', 'import time; time.sleep(60)', ]) try: retained = process_identity.open_process(process.pid, terminate=True) self.assertIsNotNone(retained._pidfd) self.assertEqual(retained.identity.pid, process.pid) retained.terminate() self.assertTrue(retained.wait(5)) retained.close() process.wait(timeout=5) self.assertIsNotNone(process.returncode) finally: if process.poll() is None: process.kill() process.wait(timeout=5) if __name__ == '__main__': unittest.main()