#!/usr/bin/env python3 """Verify accepted work survives stopped worker and API/broker restart with volumes.""" import hashlib from pathlib import Path import subprocess import sys import time ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, Failure, check from image_probe import jpeg_size def compose(*args): return subprocess.check_output(['docker', 'compose', *args], cwd=ROOT, text=True, stderr=subprocess.STDOUT) def ready(client): deadline = time.monotonic() + 90 while True: try: client.call('GET', '/health/ready') return except Failure: check(time.monotonic() < deadline, 'API did not recover') time.sleep(0.5) def main(): client = Client(timeout=15) alice, bob, eve, chat = client.bootstrap() compose('stop', 'worker') try: attachment, original = client.attachment(alice, chat) path = '/api/v1/attachments/' + attachment['id'] client.message(alice, chat, '', attachments=[attachment['id']]) client.call('GET', path + '/content', 409, actor=bob) check(client.call('GET', path, actor=bob, schema='Attachment')['json']['status'] == 'queued', 'worker was not stopped') print(f"PASS: worker stopped; upload 202 queued; message accepted; content 409; attachment_id={attachment['id']}", flush=True) ids = compose('ps', '-q', 'api', 'broker').split() before = {cid: subprocess.check_output(['docker', 'inspect', '-f', '{{.State.StartedAt}}', cid], text=True).strip() for cid in ids} compose('restart', 'api', 'broker') ready(client) after = {cid: subprocess.check_output(['docker', 'inspect', '-f', '{{.State.StartedAt}}', cid], text=True).strip() for cid in ids} check(all(before[cid] != after[cid] for cid in ids), 'API/broker restart was incomplete') check(client.call('GET', path, actor=bob, schema='Attachment')['json']['status'] == 'queued', 'queued metadata lost') print(f"PASS: both APIs and broker restarted with existing volumes; same attachment remains queued; attachment_id={attachment['id']}", flush=True) compose('start', 'worker') attachment = client.wait_ready(bob, attachment) downloaded = client.call('GET', path + '/content', actor=bob)['bytes'] thumbnail = client.call('GET', path + '/content?variant=thumbnail', actor=bob)['bytes'] check(downloaded == original, 'original changed') check(jpeg_size(thumbnail) == (256, 160), 'incorrect thumbnail dimensions') variant = next(item for item in attachment['variants'] if item['name'] == 'thumbnail') check(variant['sha256'] == hashlib.sha256(thumbnail).hexdigest(), 'thumbnail checksum mismatch') client.call('GET', path, 404, actor=eve) print(f"PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id={attachment['id']}", flush=True) finally: compose('start', 'worker') if __name__ == '__main__': main()