"""A completed fixture releases its sessions, kernels and still-running jobs.""" import json import unittest import urllib.error from harness import Albedo, Provider, alive, operation_id, python, text, wait_until class HarnessLifecycleTests(unittest.TestCase): def test_cleanup_stops_owned_roots_and_their_jobs(self): code = """ from pathlib import Path import os, sys Path('kernel.pid').write_text(str(os.getpid())) job = run(sys.executable, '-u', '-c', "import os,time; from pathlib import Path; Path('job.pid').write_text(str(os.getpid())); print('ready', flush=True); time.sleep(60)") await job.wait_for('ready') print('started') """ provider = Provider( lambda request: ( text("done") if request["messages"][-1].get("role") == "tool" else python(code) ) ) self.addCleanup(provider.close) with Albedo(provider) as app: root = app.session() direct = operation_id() with app.api( f"/sessions/{direct}", {"kind": "new", "workspace": str(app.workspace)}, method="PUT", headers={"If-None-Match": "*"}, ) as response: self.assertEqual(json.load(response)["id"], direct) app.prompt(root, "start a job").close() app.idle(root) kernel = int((app.workspace / "kernel.pid").read_text()) child = int((app.workspace / "job.pid").read_text()) self.assertTrue(alive(kernel)) self.assertTrue(alive(child)) app.cleanup() wait_until( lambda: not alive(kernel) and not alive(child), 10, "fixture processes survived cleanup", ) self.assertFalse(app.workspace.exists()) for identity in (root, direct): with self.assertRaises(urllib.error.HTTPError) as caught: app.api(f"/sessions/{identity}?tail=0") self.assertIn(caught.exception.code, (404, 410))