forked from mtusi2026/mtusi2026lab2
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
BASE = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/')
|
|
|
|
|
|
def register(username, password):
|
|
body = json.dumps({
|
|
'username': username,
|
|
'display_name': 'Race ' + username,
|
|
'password': password,
|
|
}).encode()
|
|
req = urllib.request.Request(
|
|
BASE + '/api/v1/users',
|
|
data=body,
|
|
headers={'Content-Type': 'application/json', 'Accept': 'application/json'},
|
|
method='POST',
|
|
)
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=15)
|
|
except urllib.error.HTTPError as e:
|
|
resp = e
|
|
return resp.code
|
|
|
|
|
|
def main():
|
|
username = 'race_' + uuid.uuid4().hex[:12]
|
|
password = 'T3st-' + uuid.uuid4().hex
|
|
codes = []
|
|
with ThreadPoolExecutor(max_workers=20) as pool:
|
|
futs = [pool.submit(register, username, password) for _ in range(20)]
|
|
for f in as_completed(futs):
|
|
codes.append(f.result())
|
|
ok = codes.count(201)
|
|
conflict = codes.count(409)
|
|
print('codes=%s created=%d conflict=%d' % (sorted(codes), ok, conflict))
|
|
if ok != 1 or conflict != 19:
|
|
print('FAIL: expected exactly one 201 and nineteen 409')
|
|
sys.exit(1)
|
|
print('PASS: race register handled by unique username')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|