Files
mtusi2026lab5/tests/failed_replay.py
T

59 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Prove permanent failure, DLQ routing and guarded operator replay."""
import os
from pathlib import Path
import subprocess
import time
from client import Client, arguments, check, run
def executable(value):
path = Path(value).resolve()
check(path.is_file() and os.access(path, os.X_OK), str(path) + ': not executable')
return path
def main():
def configure(parser):
parser.add_argument('--stop-worker-script', required=True)
parser.add_argument('--crash-worker-script', required=True)
parser.add_argument('--replay-script', required=True)
args = arguments('Failed registry, DLQ and guarded replay', configure)
stop = executable(args.stop_worker_script)
crash = executable(args.crash_worker_script)
replay = executable(args.replay_script)
client = Client(args.base_url, args.ca_file)
alice, bob, _eve, chat = client.bootstrap()
subprocess.run([str(stop)], check=True)
attachment, original = client.attachment(alice, chat)
subprocess.run([str(crash), 'permanent'], check=True)
deadline = time.monotonic() + 20
while True:
failed = client.call(
'GET', f'/api/v1/attachments/{attachment["id"]}', actor=bob,
schema='Attachment',
)['json']
if failed['status'] == 'failed':
break
check(time.monotonic() < deadline, 'attachment did not reach failed')
time.sleep(0.2)
check(failed['error_code'] == 'fault_injected', 'unsafe or unexpected error code')
subprocess.run([str(replay), attachment['id']], check=True)
ready = client.wait_ready(bob, attachment)
check(len(ready['variants']) == 3, 'replay produced incomplete variants')
content = client.call(
'GET', f'/api/v1/attachments/{attachment["id"]}/content', actor=bob
)['bytes']
check(content == original, 'replay changed original')
duplicate = subprocess.run([str(replay), attachment['id']], check=False)
check(duplicate.returncode != 0, 'ready job was replayed again')
print('PASS: permanent failure reached failed/DLQ and guarded replay reached ready')
if __name__ == '__main__':
run(main)