69 lines
3.2 KiB
Python
69 lines
3.2 KiB
Python
import json, pathlib, sys, tempfile, threading, time, unittest
|
|
from unittest.mock import patch
|
|
sys.path.insert(0,str(pathlib.Path(__file__).parent))
|
|
import executor as m
|
|
import runtime
|
|
from test_contract import lease
|
|
|
|
class Cancellation(unittest.TestCase):
|
|
def test_wall_deadline_kills_descendant(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
marker=pathlib.Path(root,'late')
|
|
child=f'import pathlib,time; time.sleep(.7); pathlib.Path({str(marker)!r}).touch()'
|
|
code=f'import subprocess,sys,time; subprocess.Popen([sys.executable,"-c",{child!r}]); time.sleep(5)'
|
|
with patch.object(runtime,'MAX_WALL_SECONDS',5.2):
|
|
with self.assertRaises(TimeoutError): m.execute(lease(),root,m.ProcessInference([sys.executable,'-c',code]),lambda _: None)
|
|
time.sleep(.8)
|
|
self.assertFalse(marker.exists())
|
|
|
|
def test_stalled_heartbeat_cancels_child(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
marker=pathlib.Path(root,'late'); release=threading.Event(); calls=[]
|
|
def heartbeat(_):
|
|
calls.append(time.monotonic())
|
|
if len(calls)>1: release.wait(3)
|
|
code=f'import time,pathlib; time.sleep(.7); pathlib.Path({str(marker)!r}).touch()'
|
|
try:
|
|
with patch.object(runtime,'HEARTBEAT_INTERVAL',.05),patch.object(runtime,'HEARTBEAT_TIMEOUT',.1):
|
|
with self.assertRaises(TimeoutError): m.execute(lease(),root,m.ProcessInference([sys.executable,'-c',code]),heartbeat)
|
|
finally: release.set()
|
|
time.sleep(.8); self.assertFalse(marker.exists())
|
|
|
|
def test_fixture_artifact_qa_and_cost_reservation(self):
|
|
# Explicit deterministic PROCESS fixture, never reported as real model inference.
|
|
source='def clamp_quantity(quantity):\n return min(99, max(0, quantity))\n'
|
|
response=json.dumps(dict(source=source,model='Free',tools=0))
|
|
beats=[]; original=lease(); snapshot=dict(original)
|
|
def heartbeat(binding):
|
|
beats.append(time.monotonic()); binding['leaseUntil']='untrusted mutation'
|
|
with tempfile.TemporaryDirectory() as root:
|
|
infer=m.ProcessInference([sys.executable,'-c',f'import time; time.sleep(.25); print({response!r})'])
|
|
with patch.object(runtime,'HEARTBEAT_INTERVAL',.05):
|
|
_,report=m.execute(original,root,infer,heartbeat)
|
|
self.assertEqual(original,snapshot)
|
|
self.assertGreater(len(beats),2)
|
|
self.assertIsNone(report['costCents'])
|
|
self.assertEqual(report['costStatus'],'UNKNOWN_RETAIN_FULL_20_CENT_RESERVATION')
|
|
with self.assertRaises(RuntimeError):
|
|
with runtime.LeaseGuard(lease(),lambda _:None) as guard: infer.run(guard)
|
|
|
|
def test_concurrent_reuse_rejected(self):
|
|
infer=m.ProcessInference([sys.executable,"-c","import time; time.sleep(.3); print(1)"])
|
|
errors=[]
|
|
with runtime.LeaseGuard(lease(),lambda _:None) as guard:
|
|
t=threading.Thread(target=lambda:infer.run(guard)); t.start()
|
|
time.sleep(.05)
|
|
with self.assertRaises(RuntimeError): infer.run(guard)
|
|
t.join()
|
|
|
|
def test_explicit_false_heartbeat_denied(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
with self.assertRaises(PermissionError):
|
|
m.execute(lease(),root,m.ProcessInference([sys.executable,"-c","print(1)"]),lambda _:False)
|
|
|
|
def test_callable_not_cancellable_rejected(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
with self.assertRaises(TypeError): m.execute(lease(),root,lambda:'',lambda _:None)
|
|
|
|
if __name__=='__main__': unittest.main()
|