92 lines
5.1 KiB
Python
92 lines
5.1 KiB
Python
import unittest
|
|
from unittest.mock import patch
|
|
from pathlib import Path
|
|
import tempfile
|
|
import numpy as np
|
|
from s3b_v04_gates import compare_fields,query_count,projection_terms
|
|
from run_s3b_v04_gates import run
|
|
from s2_recovery_supervisor import save
|
|
from s3b_runtime import read
|
|
|
|
|
|
class GateTests(unittest.TestCase):
|
|
def field(self,values):
|
|
return dict(raw_ids=np.arange(4),arrival=np.array(values,float),remaining=np.zeros(4),mass=np.array([1.,2.,3.,4.]))
|
|
|
|
def test_tied_order_scale_invariant_strict_mass_CDF(self):
|
|
r=compare_fields(self.field([0,1,1,2]),self.field([0,7,7,14]))
|
|
self.assertEqual(r['status'],'PASS');self.assertAlmostEqual(r['tau_b'],1)
|
|
self.assertEqual(r['max_strict_volume_CDF_delta'],0)
|
|
|
|
def test_actual_order_failure_and_undefined_tau_fail(self):
|
|
r=compare_fields(self.field([0,1,2,3]),self.field([0,2,1,3]))
|
|
self.assertEqual(r['status'],'FAIL');self.assertAlmostEqual(r['max_strict_volume_CDF_delta'],.3)
|
|
r=compare_fields(self.field([0,0,0,0]),self.field([0,0,0,0]))
|
|
self.assertEqual(r['status'],'FAIL');self.assertIsNone(r['tau_b'])
|
|
|
|
def test_partial_or_changed_mass_never_comparable(self):
|
|
for key,value in [('arrival',np.array([0,1,np.nan,3.])),('remaining',np.array([0,0,0,1.])),('mass',np.ones(4)),('raw_ids',np.array([1,2,3,4]))]:
|
|
b=self.field([0,1,2,3]);b[key]=value
|
|
with self.assertRaises(ValueError):compare_fields(self.field([0,1,2,3]),b)
|
|
|
|
def test_count_only_nonwall_double_zero_edges(self):
|
|
edges=np.array([[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]])
|
|
faces=np.array([[0,1,2]])
|
|
self.assertEqual(query_count(edges,faces,np.zeros(4)),3)
|
|
self.assertEqual(query_count(edges,faces,np.array([0.,0,0,1.])),0)
|
|
with self.assertRaises(ValueError):query_count(edges,faces,np.ones(4))
|
|
|
|
def test_max_rates_margin_and_zero_denominator(self):
|
|
rows=[dict(status='COMPLETE',nodes=100,exact_midpoint_queries=2,
|
|
costs=dict(events_CPU_s=20.,midpoint_CPU_s=3.),guard_CPU_s=30.) for _ in range(10)]
|
|
r=projection_terms(rows,1000,100,617)
|
|
self.assertEqual(r['variable_projection_CPU_s'],437.5)
|
|
self.assertEqual(r['fixed_projection_CPU_s'],7*617)
|
|
for row in rows:row['exact_midpoint_queries']=0
|
|
with self.assertRaises(ValueError):projection_terms(rows,1000,1,617)
|
|
r=projection_terms(rows,1000,0,617)
|
|
self.assertEqual(r['max_midpoint_CPU_per_query'],0)
|
|
self.assertEqual(r['max_fixed_overhead_CPU_per_part'],10)
|
|
with self.assertRaises(ValueError):projection_terms(rows[:9],1000,0,617)
|
|
|
|
def chain(self,reference_status='PASS',projection=100.):
|
|
calls=[]
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root=Path(temp);ten=root/'ten';ten.mkdir();guard=root/'guard';guard.mkdir();gate=root/'gates';gate.mkdir()
|
|
save(ten/'complete.json',dict(status='TEN_PASS_AWAITING_REFERENCE_AND_COST',cumulative_worker_CPU_s=5000.))
|
|
save(guard/'complete.json',dict(status='OBSERVED_QUEUE_TERMINAL'))
|
|
save(gate/'code-freeze.json',{'git_commit':'synthetic'})
|
|
c=dict(ten_root=str(ten),stage_guard_root=str(guard),additional_synthetic_CPU_s=2.)
|
|
def fake(command,dest,name,limits,**kw):
|
|
kind=command[-1];calls.append(kind);(dest/'output').mkdir()
|
|
data=dict(status=reference_status) if kind=='reference' else dict(status='COMPLETE',
|
|
projection_terms=dict(variable_projection_CPU_s=projection,fixed_projection_CPU_s=10.))
|
|
save(dest/'output/result.json',data)
|
|
return dict(status='PASS',monitor_errors=0,cpu_seconds=3.)
|
|
with patch('run_s3b_v04_gates.verify_gate_config',return_value=c),patch('run_s3b_v04_gates.supervise',side_effect=fake),patch('run_s3b_v04_gates.os.sched_setaffinity'),patch('run_s3b_v04_gates.signal.signal'):
|
|
code=run(gate)
|
|
result=read(gate/'complete.json')
|
|
cost=read(gate/'cost-gate.json') if (gate/'cost-gate.json').exists() else None
|
|
return code,calls,result,cost
|
|
|
|
def test_reference_failure_never_runs_census(self):
|
|
code,calls,result,cost=self.chain(reference_status='FAIL')
|
|
self.assertEqual(code,1);self.assertEqual(calls,['reference']);self.assertIsNone(cost)
|
|
self.assertEqual(result['reason'],'EXACT_REFERENCE_GATE_FAILED')
|
|
self.assertEqual(result['cumulative_worker_CPU_s'],5005.)
|
|
|
|
def test_cost_uses_final_guard_CPU_and_reserve(self):
|
|
code,calls,result,cost=self.chain()
|
|
self.assertEqual(code,0);self.assertEqual(calls,['reference','census'])
|
|
self.assertEqual(cost['projected_total_worker_CPU_s'],5000+2+6+100+10+7200)
|
|
self.assertFalse(result['full617_launched'])
|
|
|
|
def test_cost_failure_stops_without_retrial(self):
|
|
code,calls,result,cost=self.chain(projection=70000.)
|
|
self.assertEqual(code,1);self.assertEqual(calls,['reference','census'])
|
|
self.assertEqual(result['reason'],'COST_PROJECTION_GATE_FAILED')
|
|
self.assertEqual(cost['status'],'FAIL')
|
|
|
|
|
|
if __name__=='__main__':unittest.main(verbosity=2)
|