"""Actual lightweight child processes; no HTTP servers, models or GPU needed.""" import json import os from pathlib import Path import subprocess import sys import tempfile import time import unittest @unittest.skipUnless(sys.platform.startswith("linux"), "Linux service flock and process identities") class ServiceTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.workspace = Path(self.temp.name) self.root = self.workspace / "app" (self.root / "config").mkdir(parents=True) self.script = Path(__file__).resolve().parents[1] / "scripts" / "service.py" executable = self.workspace / "child-python" executable.write_text('''#!/usr/bin/env python3 import os,pathlib,signal,sys,time name="api" if "uvicorn" in sys.argv else "worker" root=pathlib.Path(os.environ["WORKSPACE_ROOT"]) def stop(sig,frame): with (root/"stop-order").open("a") as f:f.write(name+"\\n") raise SystemExit(0) signal.signal(signal.SIGTERM,stop) (root/(name+"-started")).write_text(os.environ.get("LLM_GPU_BUDGET_GIB","missing")) if name=="api" and os.environ.get("TEST_FAIL_API"):time.sleep(.4);raise SystemExit(7) while True:time.sleep(.02) ''', encoding="utf-8") executable.chmod(0o700) self.configuration = {"service_python": str(executable), "service_shutdown_seconds": 1, "runner_env": {"LLM_GPU_BUDGET_GIB": "68"}} self.environment = {**os.environ, "APP_ROOT": str(self.root), "WORKSPACE_ROOT": str(self.workspace)} def start(self): (self.root / "config" / "settings.json").write_text(json.dumps(self.configuration)) process = subprocess.Popen([sys.executable, str(self.script), "start"], env=self.environment, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) def cleanup(): if process.poll() is None: process.terminate() try: process.wait(timeout=4) except subprocess.TimeoutExpired: process.kill() process.wait(timeout=4) process.stdout.close() self.addCleanup(cleanup) return process def test_stop_verifies_supervisor_and_stops_worker_before_api(self): process = self.start() deadline = time.monotonic() + 5 while not all((self.workspace / (name + "-started")).exists() for name in ("api", "worker")): if time.monotonic() > deadline or process.poll() is not None: self.fail("service did not start its children") time.sleep(.02) self.assertEqual((self.workspace / "worker-started").read_text(), "68") duplicate = subprocess.run([sys.executable, str(self.script), "start"], env=self.environment, capture_output=True, text=True, timeout=5) self.assertNotEqual(duplicate.returncode, 0) stopped = subprocess.run([sys.executable, str(self.script), "stop"], env=self.environment, capture_output=True, text=True, timeout=6) self.assertEqual(stopped.returncode, 0, stopped.stderr) self.assertEqual(process.wait(timeout=5), 0) self.assertEqual((self.workspace / "stop-order").read_text().splitlines(), ["worker", "api"]) def test_child_exit_stops_peer_and_service_returns_nonzero(self): self.configuration["runner_env"]["TEST_FAIL_API"] = "1" process = self.start() self.assertNotEqual(process.wait(timeout=6), 0) self.assertEqual((self.workspace / "stop-order").read_text().splitlines(), ["worker"]) self.assertFalse((self.root / "state" / "service.json").exists()) if __name__ == "__main__": unittest.main()