#!/usr/bin/env python3 """Concurrent repeats through LB and the full idempotency scope.""" import concurrent.futures as futures import json from pathlib import Path import sys import uuid sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) from client import Client,arguments,check def main(): args=arguments('Student idempotency tests') c=Client(args.base_url) alice,bob,eve,chat=c.bootstrap() key=uuid.uuid4().hex def send(_): return Client(c.base).message(alice,chat,'Concurrent one',key=key) with futures.ThreadPoolExecutor(max_workers=12) as pool:results=list(pool.map(send,range(24))) check(len({r['json']['id'] for r in results})==1,'duplicate committed message') instances=sorted({r['headers']['X-Instance-Id'] for r in results}) check(len(instances)>=2,'must observe two API processes') path='/api/v1/chats/'+chat['id']+'/messages' c.call('POST',path,409,actor=alice,body={'text':'changed'},headers={'Idempotency-Key':key}) for headers in [{},{'Idempotency-Key':'x'*129},{'Idempotency-Key':'bad key'}]:c.call('POST',path,400,actor=alice,body={'text':'x'},headers=headers) different=c.message(bob,chat,'Bob independent',key=key)['json'] check(different['id']!=results[0]['json']['id'],'user scope collided') other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'other','member_ids':[alice['id']]},schema='Chat')['json'] c.message(alice,other,'Other scope',key=key) page=c.call('GET',path,actor=alice,schema='MessagePage')['json'] check(len(page['items'])==2,'history has a duplicate') c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice) c.call('POST',path,401,actor=alice,body={'text':'Concurrent one'},headers={'Idempotency-Key':key}) result={'parallel_requests':24,'created_messages':1,'observed_instances':instances,'user_chat_scope':True,'revalidation_after_logout':True} (Path(__file__).resolve().parents[1]/'evidence/integration.json').write_text(json.dumps(result,indent=2)+'\n') print('PASS:',json.dumps(result)) if __name__=='__main__':main()