"""The roster's inspect budget must exceed the inspect work. Both tests here assert a RELATION between constants rather than their values. The failure they exist for is not a wrong number — it is the budget and the work drifting apart, which is invisible in each one read on its own. """ from __future__ import annotations from backend.app.services import service_roster as sr def test_the_wrapper_budget_exceeds_the_work_it_waits_for(): """The bug the operator's first consolidated deploy surfaced (2026-09-23). `_inspect_celery_sync` makes INSPECT_ROUND_TRIPS broadcasts, and a broadcast with no `destination` cannot know how many replies to expect, so each waits out its full timeout rather than returning on the last reply. The sync call therefore costs round_trips x INSPECT_TIMEOUT_SECONDS. The wrapper allowed `INSPECT_TIMEOUT_SECONDS * 2`, which reads like a safety factor and is exactly that worst case with nothing left over. Live result: a TimeoutError traceback on every refresh while the two inspect calls were working perfectly, and a roster that stopped advancing. Asserted as a RELATION, not as a number. A third inspect call added to the sync function is the way this silently comes back, and the only thing that keeps the two honest is deriving one from the other. """ budget = ( sr.INSPECT_TIMEOUT_SECONDS * sr.INSPECT_ROUND_TRIPS + sr.INSPECT_SLACK_SECONDS ) work = sr.INSPECT_TIMEOUT_SECONDS * sr.INSPECT_ROUND_TRIPS assert budget > work, ( f"the wrapper allows {budget}s for {work}s of broadcasts — a budget " f"equal to the work fails under any load at all" ) assert sr.INSPECT_SLACK_SECONDS > 0 def test_the_round_trip_count_matches_the_calls_actually_made(): """INSPECT_ROUND_TRIPS is only true if someone keeps it true, so read the source rather than trusting the constant: the budget above is derived from it, and a call added without updating it puts the wrapper back under the work.""" import inspect as _inspect src = _inspect.getsource(sr._inspect_celery_sync) calls = src.count("insp.") assert calls == sr.INSPECT_ROUND_TRIPS, ( f"_inspect_celery_sync makes {calls} inspect calls but " f"INSPECT_ROUND_TRIPS says {sr.INSPECT_ROUND_TRIPS}" )