Files
mtusi2026lab5/student-tests/outbox_recovery.py
T

45 lines
1.7 KiB
Python

"""Show that a committed job survives while the worker/broker publisher is stopped."""
import subprocess
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
def compose(*args):
subprocess.run(['docker', 'compose', *args], check=True, stdout=subprocess.DEVNULL)
def outbox_is_pending(attachment_id):
code = """import os,sys,psycopg
with psycopg.connect(os.environ['DATABASE_URL']) as c:
print(c.execute("SELECT count(*) FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=%s AND j.published_at IS NULL", (sys.argv[1],)).fetchone()[0])"""
result = subprocess.run(
['docker', 'compose', 'exec', '-T', 'api-1', 'python', '-c', code, str(attachment_id)],
check=True, text=True, capture_output=True,
)
return result.stdout.strip().endswith('1')
def main():
client = Client('http://localhost:8080')
alice, bob, _, chat = client.bootstrap()
data = (Path(__file__).parents[1] / 'tests/fixtures/sample.png').read_bytes()
compose('stop', 'worker')
try:
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=outbox.png',
202, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
check(outbox_is_pending(attachment['id']), 'outbox intent was not durable')
finally:
compose('up', '-d', 'worker')
ready = client.wait_ready(bob, attachment)
check(ready['status'] == 'ready', 'outbox job was not published after worker restart')
print('PASS: committed outbox intent survived worker stop and reached ready')
if __name__ == '__main__':
main()