Files
mtusi2026lab7/tests/test_pipeline.py
T

47 lines
2.0 KiB
Python

"""Prove durable handoff, broker restart, outbox recovery and worker redelivery."""
import json
from pathlib import Path
import subprocess
import time
from client import Client, arguments, check, run
ROOT = Path(__file__).resolve().parents[1]
def compose(*args):
subprocess.run(['docker','compose',*args],cwd=ROOT,check=True)
def main():
args=arguments('Durable attachment pipeline')
c=Client(args.base_url,args.ca_file,timeout=20)
alice,bob,_,chat=c.bootstrap()
compose('stop','worker','dispatcher')
try:
attachment,original=c.attachment(alice,chat)
check(attachment['status']=='queued','Upload was not queued')
c.message(alice,chat,'',attachments=[attachment['id']])
# Commit succeeded but dispatcher is deliberately absent.
compose('restart','api','broker')
compose('start','dispatcher')
compose('start','worker')
ready=c.wait_ready(bob,attachment,wait_seconds=180)
check({v['name'] for v in ready['variants']}=={'thumbnail','crop','optimized'},'Missing variants')
for variant in ready['variants']:
check(variant['size_bytes']>0,'Empty variant')
check(c.call('GET',f"/api/v1/attachments/{attachment['id']}/content",actor=bob)['bytes']==original,'Original changed')
print('PASS: committed job recovered after API/broker restart and delayed dispatcher',flush=True)
compose('exec','-T','worker','touch','/tmp/lab-crash-once')
crash,_=c.attachment(alice,chat)
ready2=c.wait_ready(bob,crash,wait_seconds=240)
check(len(ready2['variants'])==3,'Duplicate or missing variants after crash')
print('PASS: worker crash after S3 write recovered with exactly three variants',flush=True)
(ROOT/'evidence/pipeline.json').write_text(json.dumps({'durable_handoff':'pass','after_s3_crash':'pass','variant_names':[v['name'] for v in ready2['variants']]},indent=2)+'\n',encoding='utf-8')
finally:
compose('start','dispatcher','worker')
if __name__=='__main__':
run(main)