Files
mtusi2026lab2/student-tests/grade5/integration.py
T
gamayunov2004 799f56c513
Student application / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 2: исследовать и ограничить cache stampede — на 5
2026-10-25 15:42:14 +03:00

79 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""Additional real HTTP/PG/Valkey checks; synthetic own keys, no global flush."""
import base64, json, os, sys, time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from threading import Barrier
from urllib.parse import urlencode
import psycopg, redis
sys.path.insert(0,str(Path(__file__).resolve().parents[2]/'tests'))
from client import Client,check,timestamp
def main():
c=Client('http://api:8080'); peer=Client('http://api-peer:8080'); short=Client('http://api-expiry:8080')
url=Path(os.environ['DATABASE_URL_FILE']).read_text().strip()
r=redis.Redis.from_url(Path(os.environ['CACHE_URL_FILE']).read_text().strip(),decode_responses=True)
a,b,eve,chat=c.bootstrap();key='profile:'+a['id']
def me(actor=a,client=c,expected=200):return client.call('GET','/api/v1/users/me',expected,actor=actor,schema='User' if expected==200 else None)
# Profiles contain no secret/session fields; malformed optional state never leaks into HTTP.
corrupt=['','null','[]','{}','not-json',json.dumps(dict(a['user'],id=b['id'])),json.dumps(dict(a['user'],display_name=[])),json.dumps(dict(a['user'],created_at='nonsense')),json.dumps(dict(a['user'],username=4)),json.dumps(dict(a['user'],password='DO_NOT_RETURN'))]
for value in corrupt:
r.set(key,value,ex=60);check(me()['json']==a['user'],'corrupt cache escaped');check(json.loads(r.get(key))==a['user'],'corrupt cache not repaired')
r.delete(key);check(me()['json']==a['user'],'cold load mismatch')
ttl=r.ttl(key);check(0<ttl<=60,'cold TTL');check(me()['json']==a['user'],'warm mismatch');check(r.ttl(key)<=ttl,'sliding TTL')
r.pexpire(key,30);time.sleep(.05);check(not r.exists(key),'expiry');check(me()['json']==a['user'],'expiry reload')
print('PASS: cold/warm/real TTL expiry; 10 empty/malformed/wrong-id/wrong-type/extra-field values repaired from PG')
# Semantic normalization inherited from lab1, adapted to PG and Bearer.
upper=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'upper','member_ids':[a['id'].upper(),b['id']]},schema='Chat')['json']
check(a['id'] in upper['member_ids'],'UUID normalization failed')
check(c.call('GET','/api/v1/chats/'+chat['id'].upper(),actor=a,schema='Chat')['json']['id']==chat['id'],'path UUID normalization')
c.call('POST','/api/v1/chats',400,actor=a,body={'title':'duplicate alias','member_ids':[a['id'],a['id'].upper()]})
c.call('GET','/api/v1/unknown_route',404)
c.call('GET','/api/v1/chats/'+chat['id']+'/messages?cursor=bad',404,actor=eve)
first=c.message(a,chat,'cursor-a')['json'];second=c.message(a,chat,'cursor-b')['json']
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages?limit=1',actor=a,schema='MessagePage')['json']
payload=json.loads(base64.urlsafe_b64decode(page['next_cursor']+'='*(-len(page['next_cursor'])%4)))
for identity in ['urn:uuid:'+first['id'],first['id'].upper(),first['id'].replace('-','')]:
payload['id']=identity;cursor=base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip('=')
continuation=c.call('GET','/api/v1/chats/'+chat['id']+'/messages?'+urlencode({'cursor':cursor}),actor=a,schema='MessagePage')['json']
check(continuation['items']==[second],'UUID cursor alias not normalized')
for stamp in ['not-a-timestamp','2026-01-01T00:00:00','2026-02-30T00:00:00Z']:
payload['created_at']=stamp;cursor=base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip('=')
c.call('GET','/api/v1/chats/'+chat['id']+'/messages?'+urlencode({'cursor':cursor}),400,actor=a)
print('PASS: member/path/cursor UUID semantics; duplicate aliases 400; malformed timestamps 400; outsider+bad cursor 404; unknown route envelope')
# Sequential read-after-write through a separate process, then the reverse.
for writer,reader in [(c,peer),(peer,c)]:
created=writer.call('POST','/api/v1/chats',201,actor=a,body={'title':'RAW cross-process','member_ids':[a['id'],b['id']]},schema='Chat')['json']
check(reader.call('GET','/api/v1/chats/'+created['id'],actor=b,schema='Chat')['json']['id']==created['id'],'chat RAW')
check(created['id'] in {x['id'] for x in reader.call('GET','/api/v1/chats',actor=b,schema='ChatPage')['json']['items']},'chat list RAW')
msg=writer.message(a,created,'synthetic RAW')['json']
check(reader.call('GET','/api/v1/chats/'+created['id']+'/messages',actor=b,schema='MessagePage')['json']['items']==[msg],'message RAW')
print('PASS: chats/chat lists/messages read-after-write immediately across two API processes in both directions')
# Revocation and expiry remain authoritative for every optional cache state.
for state in ['warm','cold','corrupt']:
actor=c.user('g5logout');own='profile:'+actor['id'];me(actor)
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=actor)
if state=='cold':r.delete(own)
if state=='corrupt':r.set(own,'[]',ex=60)
me(actor,expected=401);me(actor,client=peer,expected=401)
actor=short.user('g5expiry');own='profile:'+actor['id'];me(actor,client=short)
if state=='cold':r.delete(own)
if state=='corrupt':r.set(own,'[]',ex=60)
time.sleep(2.2);me(actor,client=short,expected=401);me(actor,client=c,expected=401)
print('PASS: revoked/expired sessions 401 in warm/cold/corrupt cache and in another API process')
# Real DB counter demonstrates local scope and bounded fallback under slow leader.
with psycopg.connect(url,user='g5observer',autocommit=True) as obs,psycopg.connect(url,user='g5observer',autocommit=True) as lock:
def profile_count():return obs.execute("SELECT coalesce(sum(calls),0) FROM pg_stat_statements WHERE userid=(SELECT oid FROM pg_roles WHERE rolname='messenger') AND query LIKE 'SELECT id,username,display_name,created_at FROM users WHERE id=%'").fetchone()[0]
for delay,replicas in [(.2,2),(1.2,1)]:
r.delete(key);before=profile_count();lock.execute('BEGIN');lock.execute('LOCK TABLE users IN ACCESS EXCLUSIVE MODE')
barrier=Barrier(9)
def request(i):barrier.wait();return me(client=peer if replicas==2 and i%2 else c)
with ThreadPoolExecutor(8) as pool:
futures=[pool.submit(request,i) for i in range(8)];start=time.monotonic();barrier.wait();time.sleep(delay);lock.execute('COMMIT');results=[f.result() for f in futures]
count=int(profile_count()-before);check(all(x['json']==a['user'] for x in results),'concurrent fallback mismatch')
if replicas==2:check(1<=count<=2,'local scope unexpected SQL')
else:check(count>=2,'1s timeout did not use real PG fallback')
print('PASS: HTTP C8 lock=%.1fs processes=%d real profile SELECT=%d elapsed=%.3fs max_http=%.3fs'%(delay,replicas,count,time.monotonic()-start,max(x['elapsed'] for x in results)))
print('PASS: grade5 additional integration checks completed')
if __name__=='__main__':main()