114 lines
6.0 KiB
Python
114 lines
6.0 KiB
Python
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from engine.config import QualityConfig
|
|
from engine.isolated import recover_candidate, run_isolated
|
|
from engine.pipeline import PipelineError
|
|
|
|
|
|
def crash_worker(channel, action, base_dir, values, config):
|
|
if values.get('job_id'): channel.put({'type':'job','job_id':values['job_id']})
|
|
channel.close(); channel.join_thread()
|
|
os._exit(139)
|
|
|
|
|
|
def healthy_worker(channel, action, base_dir, values, config):
|
|
channel.put({'type':'result','report':{'ok':True,'worker_pid':os.getpid()}})
|
|
|
|
|
|
def input_error_worker(channel, action, base_dir, values, config):
|
|
channel.put({'type':'error','error':'invalid input','input_error':True})
|
|
|
|
|
|
def error_worker(channel, action, base_dir, values, config):
|
|
channel.put({'type':'error','error':'render backend failure','job_id':values['job_id'],'input_error':False})
|
|
|
|
|
|
def waiting_worker(channel, action, base_dir, values, config):
|
|
import time
|
|
channel.put({'type':'job','job_id':values['job_id']})
|
|
time.sleep(15)
|
|
|
|
|
|
class IsolatedTests(unittest.TestCase):
|
|
def saved_job(self, base):
|
|
folder=Path(base)/'output'/'quality_saved'; candidate=folder/'candidate_001'; candidate.mkdir(parents=True)
|
|
(folder/'request.json').write_text(json.dumps({'text':'box','config':QualityConfig().to_dict()}))
|
|
(folder/'job.json').write_text(json.dumps({'status':'building','calls':2,'elapsed_s':1,'usage':{}}))
|
|
(folder/'events.jsonl').write_text(json.dumps({'event':'tool_start','elapsed_s':2,'stage':'features','name':'op_fillet_edge'})+'\n')
|
|
(folder/'requirements.json').write_text(json.dumps({'name':'box','assumptions':[],'unresolved':[]}))
|
|
(candidate/'model.json').write_text(json.dumps({'output':'body','operations':[],'claims':[]}))
|
|
(candidate/'geometry.json').write_text(json.dumps({'valid':True,'solid_count':1,'volume_mm3':8,'bbox_min':[0,0,0],'bbox_max':[2,2,2]}))
|
|
# Deliberately not CAD data: recovery must copy an existing artifact without
|
|
# invoking the native kernel again inside the parent/server process.
|
|
(candidate/'model.step').write_bytes(b'already exported STEP')
|
|
(candidate/'model.stl').write_bytes(b'already exported STL')
|
|
return folder
|
|
|
|
def test_submitted_whole_part_outranks_newer_scratch_component(self):
|
|
import shutil
|
|
with tempfile.TemporaryDirectory() as base:
|
|
folder=self.saved_job(base)
|
|
scratch=folder/'checkpoint_000010'
|
|
shutil.copytree(folder/'candidate_001',scratch)
|
|
(scratch/'model.json').write_text(json.dumps({'output':'scratch_thread','operations':[],'claims':[]}))
|
|
(scratch/'model.step').write_bytes(b'scratch component only')
|
|
os.utime(scratch,(2000000000,2000000000))
|
|
result=recover_candidate(base,'quality_saved','native exit')
|
|
self.assertEqual(result['output_id'],'body')
|
|
self.assertEqual(result['recovered_candidate'],'candidate_001')
|
|
self.assertEqual((folder/'model.step').read_bytes(),b'already exported STEP')
|
|
|
|
def test_regular_worker_error_also_recovers_exported_geometry(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
folder=self.saved_job(base)
|
|
result=run_isolated('generate',base,_target=error_worker,job_id='quality_saved')
|
|
self.assertEqual(result['quality_status'],'partial')
|
|
self.assertIn('render backend failure',result['quality_reason'])
|
|
self.assertEqual((folder/'model.step').read_bytes(),b'already exported STEP')
|
|
|
|
def test_memory_limit_recovers_checkpoint_and_leaves_next_task_usable(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
folder=self.saved_job(base)
|
|
(folder/'candidate_001').rename(folder/'checkpoint_000010')
|
|
announced=[]
|
|
with patch('engine.worker_resources.memory_exceeded',side_effect=lambda *_: 'CADMemoryLimit: test threshold' if announced else None):
|
|
result=run_isolated('generate',base,_target=waiting_worker,job_id='quality_saved',job_callback=announced.append)
|
|
self.assertEqual(result['quality_status'],'partial')
|
|
self.assertEqual(result['recovered_candidate'],'checkpoint_000010')
|
|
self.assertIn('CADMemoryLimit',result['quality_reason'])
|
|
self.assertTrue(run_isolated('generate',base,_target=healthy_worker)['ok'])
|
|
|
|
def test_native_exit_is_contained_and_next_worker_succeeds(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
folder=self.saved_job(base)
|
|
result=run_isolated('generate',base,_target=crash_worker,job_id='quality_saved')
|
|
self.assertEqual(result['quality_status'],'partial')
|
|
self.assertIn('139',result['quality_reason'])
|
|
self.assertEqual((folder/'model.step').read_bytes(),b'already exported STEP')
|
|
self.assertEqual(json.loads((folder/'job.json').read_text())['status'],'partial')
|
|
healthy=run_isolated('generate',base,_target=healthy_worker)
|
|
self.assertTrue(healthy['ok'])
|
|
self.assertNotEqual(healthy['worker_pid'],os.getpid())
|
|
|
|
def test_exit_without_candidate_is_error_not_fake_success(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
with self.assertRaisesRegex(PipelineError,'139'):
|
|
run_isolated('generate',base,_target=crash_worker)
|
|
|
|
def test_input_validation_preserves_error_type(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
with self.assertRaisesRegex(ValueError,'invalid input'):
|
|
run_isolated('generate',base,_target=input_error_worker)
|
|
|
|
def test_invalid_candidate_is_not_recovered(self):
|
|
with tempfile.TemporaryDirectory() as base:
|
|
folder=self.saved_job(base)
|
|
(folder/'candidate_001'/'geometry.json').write_text(json.dumps({'valid':False,'solid_count':1}))
|
|
self.assertIsNone(recover_candidate(base,'quality_saved','native exit'))
|
|
self.assertFalse((folder/'model.step').exists())
|