Files
mtusi2026lab5/student-tests/worker_ack_recovery.py
T

56 lines
2.0 KiB
Python

"""Inject a pause after DB ready and before broker ack, then restart the worker."""
import subprocess
import sys
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
TEST_CONTAINER = 'mtusi2026lab5-ack-test'
def run(*args, capture=False):
return subprocess.run(['docker', 'compose', *args], check=True, text=True,
capture_output=capture)
def cleanup_test_container():
subprocess.run(['docker', 'rm', '-f', TEST_CONTAINER], check=False,
stdout=subprocess.DEVNULL, stderr=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()
run('stop', 'worker')
cleanup_test_container()
run('run', '--no-deps', '-d', '--name', TEST_CONTAINER, '-e', 'ACK_DELAY_SECONDS=60', 'worker')
try:
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=ack-window.png',
202, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
deadline = time.monotonic() + 60
while time.monotonic() < deadline:
state = client.call('GET', '/api/v1/attachments/' + attachment['id'], actor=bob, schema='Attachment')['json']
if state['status'] == 'ready':
break
time.sleep(0.2)
else:
raise AssertionError('attachment did not reach ready during ack pause')
time.sleep(2)
subprocess.run(['docker', 'stop', TEST_CONTAINER], check=True, stdout=subprocess.DEVNULL)
finally:
cleanup_test_container()
run('up', '-d', 'worker')
ready = client.wait_ready(bob, attachment)
check(len(ready['variants']) == 3, 'redelivery changed the variant set')
print('PASS: redelivery after ready-before-ack kept one result set')
if __name__ == '__main__':
main()