Files
mtusi2026lab5/student-tests/worker_recovery.py
T

45 lines
1.5 KiB
Python

"""Worker stop/start scenario for the durable staging and broker path."""
import subprocess
import sys
import time
import uuid
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 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=stopped.png',
202, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
client.message(alice, chat, '', attachments=[attachment['id']])
client.call(
'GET', '/api/v1/attachments/' + attachment['id'] + '/content',
409, actor=bob,
)
finally:
compose('up', '-d', 'worker')
ready = client.wait_ready(bob, attachment)
check(ready['status'] == 'ready', 'worker did not finish queued job')
content = client.call(
'GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=thumbnail',
200, actor=bob,
)
check(content['headers'].get_content_type() == 'image/jpeg', 'thumbnail is not JPEG')
print('PASS: upload survived stopped worker and reached ready after restart')
if __name__ == '__main__':
main()