forked from mtusi2026/mtusi2026lab3
145 lines
4.1 KiB
Python
145 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Own lab3 tests (grade 4): multi-instance, idempotency, failover helpers."""
|
|
import threading
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from urllib.parse import quote
|
|
|
|
from client import Client, Failure, arguments, check, run
|
|
|
|
|
|
def test_multiple_instance_ids(c: Client):
|
|
alice = c.user("inst")
|
|
seen = set()
|
|
for _ in range(40):
|
|
seen.add(
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")["headers"][
|
|
"X-Instance-Id"
|
|
]
|
|
)
|
|
check(len(seen) >= 2, "expected >=2 X-Instance-Id via LB, got %r" % (seen,))
|
|
print("PASS: observed instances %s" % sorted(seen))
|
|
|
|
|
|
def test_idempotency_replay(c: Client):
|
|
alice = c.user("idem")
|
|
chat = c.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "Idem", "member_ids": [alice["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
key = uuid.uuid4().hex
|
|
first = c.message(alice, chat, "same", key=key)["json"]
|
|
second = c.message(alice, chat, "same", key=key)["json"]
|
|
check(first == second, "idempotent replay must return same message")
|
|
c.call(
|
|
"POST",
|
|
"/api/v1/chats/" + chat["id"] + "/messages",
|
|
409,
|
|
actor=alice,
|
|
body={"text": "other"},
|
|
headers={"Idempotency-Key": key},
|
|
schema="Error",
|
|
)
|
|
|
|
|
|
def test_idempotency_concurrent(c: Client):
|
|
alice = c.user("raceidem")
|
|
chat = c.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "RaceIdem", "member_ids": [alice["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
key = uuid.uuid4().hex
|
|
text = "concurrent-" + key
|
|
barrier = threading.Barrier(8)
|
|
results = []
|
|
|
|
def worker():
|
|
local = Client(c.base, timeout=20)
|
|
barrier.wait()
|
|
try:
|
|
msg = local.message(alice, chat, text, key=key)["json"]
|
|
results.append(msg["id"])
|
|
except Failure as exc:
|
|
results.append(str(exc))
|
|
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
list(pool.map(lambda _: worker(), range(8)))
|
|
ids = [r for r in results if isinstance(r, str) and len(r) == 36]
|
|
check(len(ids) == 8, "all concurrent calls must succeed, got %r" % (results,))
|
|
check(len(set(ids)) == 1, "expected exactly one message id, got %r" % (set(ids),))
|
|
|
|
|
|
def test_missing_idempotency_key(c: Client):
|
|
alice = c.user("nokey")
|
|
chat = c.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "NoKey", "member_ids": [alice["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
c.call(
|
|
"POST",
|
|
"/api/v1/chats/" + chat["id"] + "/messages",
|
|
400,
|
|
actor=alice,
|
|
body={"text": "без ключа"},
|
|
schema="Error",
|
|
)
|
|
|
|
|
|
def test_pagination(c: Client):
|
|
alice = c.user("page")
|
|
chat = c.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "Pages", "member_ids": [alice["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
for i in range(5):
|
|
c.message(alice, chat, "m-%d" % i)
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
collected = []
|
|
cursor = None
|
|
while True:
|
|
q = path + "?limit=2"
|
|
if cursor:
|
|
q += "&cursor=" + quote(cursor, safe="")
|
|
page = c.call("GET", q, actor=alice, schema="MessagePage")["json"]
|
|
collected.extend(page["items"])
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
check(len(collected) == 5, "expected 5 messages")
|
|
|
|
|
|
def main():
|
|
args = arguments("Own lab3 tests")
|
|
c = Client(args.base_url, args.ca_file, timeout=20)
|
|
c.call("GET", "/health/ready", schema="Health")
|
|
test_multiple_instance_ids(c)
|
|
test_idempotency_replay(c)
|
|
print("PASS: idempotency replay/conflict")
|
|
test_idempotency_concurrent(c)
|
|
print("PASS: concurrent idempotency")
|
|
test_missing_idempotency_key(c)
|
|
print("PASS: missing Idempotency-Key -> 400")
|
|
test_pagination(c)
|
|
print("PASS: pagination")
|
|
print("PASS: all own lab3 tests")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|