Files
mtusi2026lab3/student-tests/durability.py

70 lines
3.9 KiB
Python

"""Transaction rollback, lost reply, expiry and restart evidence on synthetic data only."""
import http.client
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import uuid
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client
from resilience import history
def sql(statement):
return subprocess.check_output(['docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-v', 'ON_ERROR_STOP=1', '-Atc', statement], text=True).strip()
def main():
c = Client()
actor, reader, _, chat = c.bootstrap()
key = uuid.uuid4().hex
first = c.message(actor, chat, 'persist-key\x00👩', key=key)['json']
duration = float(sql("SELECT extract(epoch FROM expires_at - clock_timestamp()) FROM idempotency_keys WHERE 1=1 AND key = '" + key + "'"))
assert 86300 < duration <= 86400
subprocess.run([str(ROOT / 'scripts/restart.sh')], check=True, stdout=subprocess.DEVNULL)
subprocess.run([sys.executable, str(ROOT / 'scripts/wait_ready.py')], check=True)
assert c.message(actor, chat, 'persist-key\x00👩', key=key)['json'] == first
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=actor, body={'text': 'different'}, headers={'Idempotency-Key': key})
print('PASS: original session and same idempotency result survive full stack restart; retention >=24h.', flush=True)
failed_key = uuid.uuid4().hex
marker = 'rollback-' + uuid.uuid4().hex
# Inject only for this synthetic message; both the claim and write must roll back.
sql("CREATE FUNCTION synthetic_failure() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.text = convert_to('" + marker + "', 'UTF8') THEN RAISE EXCEPTION 'synthetic failure'; END IF; RETURN NEW; END $$; CREATE TRIGGER synthetic_failure BEFORE INSERT ON messages FOR EACH ROW EXECUTE FUNCTION synthetic_failure();")
try:
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 503, actor=actor, body={'text': marker}, headers={'Idempotency-Key': failed_key})
assert sql("SELECT count(*) FROM idempotency_keys WHERE 1=1 AND key = '" + failed_key + "'") == '0'
finally:
sql('DROP TRIGGER synthetic_failure ON messages; DROP FUNCTION synthetic_failure();')
c.message(actor, chat, marker, key=failed_key)
print('PASS: synthetic failure leaves no idempotency result; same key succeeds after recovery.', flush=True)
parsed = urlsplit(c.base)
lost_key = uuid.uuid4().hex
connection = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=2)
connection.request('POST', '/api/v1/chats/' + chat['id'] + '/messages', body=json.dumps({'text': 'lost-reply-' + lost_key}), headers={'Authorization': 'Bearer ' + actor['token'], 'Content-Type': 'application/json', 'Idempotency-Key': lost_key})
connection.close() # Deliberately do not read the server's response.
time.sleep(.2)
restored = c.message(actor, chat, 'lost-reply-' + lost_key, key=lost_key)['json']
assert c.message(actor, chat, 'lost-reply-' + lost_key, key=lost_key)['json'] == restored
found = history(c, reader, chat)
assert sum(v['text'] == restored['text'] for v in found) == 1
print('PASS: retry after intentionally unread HTTP reply creates no duplicate.', flush=True)
sql("UPDATE idempotency_keys SET expires_at = clock_timestamp() - INTERVAL '1 second' WHERE 1=1 AND key = '" + key + "'")
renewed = c.message(actor, chat, 'new operation after expiry', key=key)['json']
assert renewed['id'] != first['id']
assert c.message(actor, chat, 'new operation after expiry', key=key)['json'] == renewed
found = history(c, reader, chat)
assert first in found and renewed in found
print('PASS: simulated expired key can be reused; original message remains in history.', flush=True)
if __name__ == '__main__':
main()