forked from mtusi2026/mtusi2026lab2
56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
"""Test: concurrent registration of the same username must produce exactly one 201 and one 409."""
|
|
import base64
|
|
import json
|
|
import threading
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
|
|
|
|
BASE = "http://localhost:8080"
|
|
|
|
|
|
def post_user(username: str, password: str):
|
|
body = json.dumps({
|
|
"username": username,
|
|
"display_name": "Race Test",
|
|
"password": password,
|
|
}).encode()
|
|
req = urllib.request.Request(
|
|
BASE + "/api/v1/users",
|
|
data=body,
|
|
headers={"Content-Type": "application/json"},
|
|
method="POST",
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
|
return resp.status, resp.read().decode()
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, e.read().decode()
|
|
|
|
|
|
def main():
|
|
username = "race_" + uuid.uuid4().hex[:8]
|
|
password = "RaceTestPassword123"
|
|
results = []
|
|
lock = threading.Lock()
|
|
|
|
def worker():
|
|
status, body = post_user(username, password)
|
|
with lock:
|
|
results.append((status, body))
|
|
|
|
threads = [threading.Thread(target=worker) for _ in range(2)]
|
|
for t in threads:
|
|
t.start()
|
|
for t in threads:
|
|
t.join()
|
|
|
|
statuses = [s for s, _ in results]
|
|
print("Statuses:", statuses)
|
|
assert sorted(statuses) == [201, 409], f"expected [201, 409], got {statuses}"
|
|
print("PASS: race registration — one 201, one 409")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |