#!/usr/bin/env python3 """Hold worker, prove queue admission cap, then recover accepted work.""" import hashlib, json, os, subprocess, time from pathlib import Path from client import Client, arguments, check, run ROOT=Path(__file__).resolve().parents[1] def compose(*args,capacity=None): env=os.environ.copy() if capacity is not None: env['MAX_QUEUED_JOBS']=str(capacity) subprocess.run(['docker','compose',*args],cwd=ROOT,env=env,check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) def main(): a=arguments('Queue admission backpressure under worker outage') compose('up','-d','--force-recreate','--scale','api=2','api',capacity=1) compose('stop','worker') try: c=Client(a.base_url,a.ca_file); alice,bob,_,chat=c.bootstrap(); raw=(ROOT/'tests/fixtures/sample.png').read_bytes(); path='/api/v1/chats/'+chat['id']+'/attachments?filename=backpressure.png' first=c.call('POST',path,202,actor=alice,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment')['json'] started=time.monotonic() c.call('POST',path,503,actor=alice,raw=raw,headers={'Content-Type':'image/png'}) compose('up','-d','--force-recreate','worker') ready=c.wait_ready(bob,first,wait_seconds=90) compose('up','-d','--force-recreate','--scale','api=2','api') result={'queue_limit':1,'worker_stopped':True,'first_upload_status':202,'next_upload_status':503,'accepted_attachment_id':first['id'],'final_status':ready['status'],'original_sha256_matches':ready['sha256']==hashlib.sha256(raw).hexdigest(),'recovery_seconds':round(time.monotonic()-started,3),'note':'503 admission refusal leaves the already accepted job intact.'} dest=ROOT/'evidence/backpressure.json'; dest.write_text(json.dumps(result,indent=2)+'\n'); print(json.dumps(result,indent=2)) finally: compose('up','-d','--force-recreate','worker') compose('up','-d','--force-recreate','--scale','api=2','api') if __name__=='__main__': run(main)