"""Exercise Redis cache, safe fallback and real post-insert transactional rollback.""" import json,subprocess,sys,time,uuid from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) from client import Client,check,run ROOT=Path(__file__).resolve().parents[1] def cmd(*args): return subprocess.check_output(args,cwd=ROOT,text=True).strip() def sql(statement): return cmd('docker','compose','exec','-T','db','psql','-v','ON_ERROR_STOP=1','-U','messenger','-d','messenger','-Atc',statement) def redis(*args): return cmd('docker','compose','exec','-T','cache','redis-cli','--raw',*args) def main(): c=Client(timeout=10); actor=c.user('cache') key='profile:v1:'+actor['id'] c.call('GET','/api/v1/users/me',actor=actor,schema='User') cached=json.loads(redis('GET',key)) check(cached==actor['user'],'profile not actually cached') remaining=int(redis('TTL',key)); check(0=before+3,'API did not read Redis') print('PASS: actual cached public profile, TTL <=60 seconds, at least 3 Redis cache hits',flush=True) subprocess.run(['docker','compose','stop','cache'],cwd=ROOT,check=True) try: start=time.monotonic() result=c.call('GET','/api/v1/users/me',actor=actor,schema='User')['json'] elapsed=time.monotonic()-start print('Cache outage diagnostic: profile_matches=%s elapsed_seconds=%.3f'%(result==actor['user'],elapsed),flush=True) check(result==actor['user'] and elapsed<3,'cache fallback incorrect or unbounded') c.call('GET','/api/v1/users/me',401) c.call('GET','/api/v1/users/me',401,headers={'Authorization':'Bearer random-invalid-token'}) c.call('GET','/health/ready',schema='Health') c.call('DELETE','/api/v1/auth/sessions/current',204,actor=actor) c.call('GET','/api/v1/users/me',401,actor=actor) print('PASS: Redis down: PostgreSQL fallback %.3fs, ready=200, invalid/absent/revoked sessions=401'%elapsed,flush=True) finally: subprocess.run(['docker','compose','start','cache'],cwd=ROOT,check=True) for _ in range(30): try: if redis('PING')=='PONG': break except subprocess.CalledProcessError: pass time.sleep(1) check(redis('EXISTS',key)=='0','cache unexpectedly persisted test key') actor=c.user('cache_recovery') for _ in range(5): c.call('GET','/api/v1/users/me',actor=actor,schema='User') if redis('EXISTS','profile:v1:'+actor['id'])=='1': break time.sleep(1) check(redis('EXISTS','profile:v1:'+actor['id'])=='1','cache did not recover') print('PASS: ephemeral Redis restart: cache is rebuilt; no session source of truth lost',flush=True) # Inject a scoped storage failure on member INSERT, after the chat INSERT. title='rollback_'+uuid.uuid4().hex function='lab2_test_'+uuid.uuid4().hex trigger=function+'_trigger' sql("CREATE FUNCTION "+function+"() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF EXISTS (SELECT 1 FROM chats WHERE id=NEW.chat_id AND title='"+title+"') THEN RAISE EXCEPTION 'synthetic member insert failure' USING ERRCODE='23514'; END IF; RETURN NEW; END $$; CREATE TRIGGER "+trigger+" BEFORE INSERT ON members FOR EACH ROW EXECUTE FUNCTION "+function+"();") try: c.call('POST','/api/v1/chats',503,actor=actor,body={'title':title,'member_ids':[actor['id']]}) check(sql("SELECT count(*) FROM chats WHERE title='"+title+"'")=='0','partial chat survived failed member INSERT') check(sql('SELECT count(*) FROM members m LEFT JOIN chats c ON c.id=m.chat_id WHERE c.id IS NULL')=='0','orphan membership') print('PASS: injected failure AFTER chat INSERT: HTTP 503, transaction rolled back chat and members',flush=True) finally: sql('DROP TRIGGER '+trigger+' ON members; DROP FUNCTION '+function+'();') c.call('POST','/api/v1/chats',201,actor=actor,body={'title':'after rollback','member_ids':[actor['id']]},schema='Chat') print('PASS: same API remains usable after rollback',flush=True) if __name__=='__main__': run(main)