forked from mtusi2026/mtusi2026lab2
27 lines
764 B
Python
27 lines
764 B
Python
from concurrent.futures import ThreadPoolExecutor
|
|
from threading import Barrier
|
|
from uuid import uuid4
|
|
|
|
from tests.client import Client
|
|
|
|
|
|
def test_only_one_concurrent_registration_succeeds():
|
|
username = 'race_' + uuid4().hex[:16]
|
|
body = {
|
|
'username': username,
|
|
'display_name': 'Race user',
|
|
'password': 'Race-password-' + uuid4().hex,
|
|
}
|
|
barrier = Barrier(8)
|
|
|
|
def register_once(_: int) -> int:
|
|
client = Client()
|
|
barrier.wait()
|
|
return client.call('POST', '/api/v1/users', expected=[201, 409], body=body)['status']
|
|
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
statuses = list(pool.map(register_once, range(8)))
|
|
|
|
assert statuses.count(201) == 1
|
|
assert statuses.count(409) == 7
|