forked from mtusi2026/mtusi2026lab3
Lab 3: solution for grade 3 - BPI2304 Vardanyan
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""Additional lab3 end-to-end test; requires ORIGINAL skeleton's tests/client.py.
|
||||
|
||||
Creates one chat and posts an identical message 12 times through the load balancer.
|
||||
"""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
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, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Concurrent Idempotency-Key test for two replicas")
|
||||
initial = Client(args.base_url, args.ca_file)
|
||||
alice, bob, _, chat = initial.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = "Один текст от параллельных реплик " + key
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
|
||||
def send_one(_index):
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
response = client.call("POST", path, 201, actor=alice, body={"text": text},
|
||||
headers={"Idempotency-Key": key}, schema="Message")
|
||||
return response["json"], response["headers"]["X-Instance-Id"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as executor:
|
||||
replies = list(executor.map(send_one, range(12)))
|
||||
ids = {row["id"] for row, _ in replies}
|
||||
check(len(ids) == 1, "different message IDs returned for one key")
|
||||
check(all(row == replies[0][0] for row, _ in replies), "different responses for one key")
|
||||
messages = initial.call("GET", path, actor=bob, schema="MessagePage")["json"]["items"]
|
||||
check(len([m for m in messages if m["text"] == text]) == 1,
|
||||
"duplicate in PostgreSQL message history")
|
||||
instances = {inst for _, inst in replies}
|
||||
print("PASS: 12 concurrent same-key POSTs => one durable message; observed instances:",
|
||||
", ".join(sorted(instances)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user