"""Локальные проверки реальных subprocess без Docker и внешних библиотек. python -m unittest discover -s student-tests -p test_task_supervisor.py -v """ from pathlib import Path import subprocess import sys from tempfile import TemporaryDirectory import time import unittest sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from src import task_supervisor class TaskSupervisorTests(unittest.TestCase): def test_successful_work_outlives_initial_lease(self): lease = 0.5 expires = time.monotonic() + lease renewals = [] def renew(): nonlocal expires now = time.monotonic() self.assertLess(now, expires, 'renewal must happen before lease expires') expires = now + lease renewals.append(now) return True started = time.monotonic() code = task_supervisor.run([sys.executable, '-c', 'import time; time.sleep(1.1)'], renew, timeout=5, interval=0.1) self.assertEqual(code, 0) self.assertGreater(time.monotonic() - started, lease) self.assertGreaterEqual(len(renewals), 3) def test_heartbeat_does_not_bypass_total_timeout(self): renewals = [] def renew(): renewals.append(True) return True started = time.monotonic() with self.assertRaises(subprocess.TimeoutExpired): task_supervisor.run([sys.executable, '-c', 'import time; time.sleep(60)'], renew, timeout=0.5, interval=0.1) self.assertTrue(renewals) self.assertLess(time.monotonic() - started, 5) def test_lost_lease_stops_work(self): with self.assertRaises(task_supervisor.LeaseLost): task_supervisor.run([sys.executable, '-c', 'import time; time.sleep(60)'], lambda: False, timeout=10, interval=0.1) def test_renewal_error_stops_work(self): def renew(): raise ConnectionError('database unavailable') with self.assertRaises(ConnectionError): task_supervisor.run([sys.executable, '-c', 'import time; time.sleep(60)'], renew, timeout=10, interval=0.1) def test_child_failure_is_not_success(self): self.assertEqual(task_supervisor.run([sys.executable, '-c', 'raise SystemExit(86)'], lambda: True, timeout=5, interval=0.1), 86) def test_timeout_kills_descendant_too(self): with TemporaryDirectory() as directory: marker = Path(directory) / 'survived' started = Path(directory) / 'started' descendant = ('import time; from pathlib import Path; ' f'Path({str(started)!r}).write_text("started"); ' 'time.sleep(1.5); ' f'Path({str(marker)!r}).write_text("orphan")') parent = ('import subprocess,sys,time; ' f'subprocess.Popen([sys.executable,"-c",{descendant!r}]); ' 'time.sleep(60)') with self.assertRaises(subprocess.TimeoutExpired): task_supervisor.run([sys.executable, '-c', parent], lambda: True, timeout=0.8, interval=0.1) self.assertTrue(started.exists(), 'descendant must start before timeout') time.sleep(1.6) self.assertFalse(marker.exists(), 'descendant survived parent timeout') if __name__ == '__main__': unittest.main()