55 lines
2.9 KiB
Python
55 lines
2.9 KiB
Python
"""Additional black-box checks; public tests and schemas are imported unchanged."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
import json
|
|
import sys
|
|
import threading
|
|
import uuid
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Concurrent cross-replica idempotency and isolation')
|
|
c = Client(args.base_url)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/'+chat['id']+'/messages'
|
|
key = uuid.uuid4().hex
|
|
barrier = threading.Barrier(16)
|
|
|
|
def send(i):
|
|
local = Client(args.base_url)
|
|
barrier.wait()
|
|
raw = ('{ "text" : "same \\u0000 \\ud83d\\ude00" }' if i % 2 else '{"text":"same \\u0000 😀"}').encode('utf-8')
|
|
return local.call('POST', path, 201, actor=alice, raw=raw,
|
|
headers={'Content-Type':'application/json', 'Idempotency-Key':key}, schema='Message')
|
|
|
|
with ThreadPoolExecutor(max_workers=16) as pool:
|
|
responses = list(pool.map(send, range(16)))
|
|
first = responses[0]['json']
|
|
check(all(r['json'] == first for r in responses), 'concurrent replay changed result')
|
|
instances = sorted({r['headers']['X-Instance-Id'] for r in responses})
|
|
check(len(instances) >= 2, 'concurrency must reach different real replicas')
|
|
c.call('POST', path, 409, actor=alice, body={'text':'different'}, headers={'Idempotency-Key':key})
|
|
other_user = c.message(bob, chat, 'other user', key=key)['json']
|
|
check(other_user['id'] != first['id'], 'key was not scoped to user')
|
|
other_chat = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
|
body={'title':'independent scope', 'member_ids':[alice['id']]}, schema='Chat')['json']
|
|
check(c.message(alice, other_chat, 'other chat', key=key)['json']['id'] != first['id'], 'key was not scoped to chat')
|
|
for bad in ['', 'bad key', 'é', 'a'*129, 'slash/key']:
|
|
c.call('POST', path, 400, actor=alice, body={'text':'bad'}, headers={'Idempotency-Key':bad})
|
|
c.call('POST', path, 400, actor=alice, body={'text':'missing key'})
|
|
c.call('POST', path, 404, actor=eve, body={'text':first['text']}, headers={'Idempotency-Key':key})
|
|
page = c.call('GET', path+'?limit=100', actor=bob, schema='MessagePage')['json']
|
|
check(sum(m['id']==first['id'] for m in page['items']) == 1, 'duplicate message')
|
|
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
|
c.call('POST', path, 401, actor=alice, body={'text':first['text']}, headers={'Idempotency-Key':key})
|
|
print(json.dumps({'concurrent_requests':16, 'instances':instances, 'identical_result':True,
|
|
'key_scopes':'user + POST + chat path', 'revocation_rechecked':True}))
|
|
print('PASS: concurrent replay, Unicode, conflict, key validation, isolation and logout')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|