Fix replica auth and idempotency races
This commit is contained in:
@@ -23,7 +23,7 @@ import common
|
||||
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
PROJECT = os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab2")
|
||||
PROJECT = os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab3")
|
||||
ENV_FILE = os.environ.get("COMPOSE_ENV_FILE", ".env")
|
||||
ENABLED = os.environ.get("STUDENT_DOCKER_TESTS") == "1"
|
||||
|
||||
@@ -47,9 +47,13 @@ def wait_ready(expected: int, seconds: float) -> float:
|
||||
deadline = started + seconds
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
last = common.request("GET", "/health/ready", timeout=5)
|
||||
if last.status == expected:
|
||||
return time.monotonic() - started
|
||||
try:
|
||||
last = common.request("GET", "/health/ready", timeout=5)
|
||||
if last.status == expected:
|
||||
return time.monotonic() - started
|
||||
except OSError:
|
||||
# A replica can close the listener while Compose restarts it.
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
raise AssertionError(
|
||||
"readiness did not reach %s within %.0fs (last=%s %r)"
|
||||
@@ -59,6 +63,50 @@ def wait_ready(expected: int, seconds: float) -> float:
|
||||
|
||||
@unittest.skipUnless(ENABLED, "set STUDENT_DOCKER_TESTS=1 to stop real containers")
|
||||
class DependencyOutages(unittest.TestCase):
|
||||
def test_logout_remains_revoked_across_replicas_after_cache_outage(self) -> None:
|
||||
"""A stale shared-cache hit must not bypass PostgreSQL revocation."""
|
||||
alice = common.actor("revoked")
|
||||
chat = common.create_chat(alice.token, [alice.id], "Durable logout")
|
||||
self.assertEqual(common.request("GET", "/api/v1/users/me", token=alice.token).status, 200)
|
||||
try:
|
||||
compose("stop", "valkey")
|
||||
logout = common.request("DELETE", "/api/v1/auth/sessions/current", token=alice.token)
|
||||
self.assertEqual(logout.status, 204, logout.json)
|
||||
|
||||
# Remove both processes' local fallback state and restore Valkey's
|
||||
# old data. Requests through nginx can land on either replica.
|
||||
compose("restart", "api-1", "api-2")
|
||||
compose("start", "valkey")
|
||||
wait_ready(200, 90)
|
||||
|
||||
common.expect_error(
|
||||
self, common.request("GET", "/api/v1/users/me", token=alice.token), 401, "unauthorized"
|
||||
)
|
||||
common.expect_error(
|
||||
self,
|
||||
common.request("GET", "/api/v1/chats/" + chat["id"], token=alice.token),
|
||||
401,
|
||||
"unauthorized",
|
||||
)
|
||||
common.expect_error(
|
||||
self,
|
||||
common.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
body={"text": "must not be accepted"},
|
||||
token=alice.token,
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
),
|
||||
401,
|
||||
"unauthorized",
|
||||
)
|
||||
finally:
|
||||
compose("start", "valkey", "postgres", "api-1", "api-2")
|
||||
try:
|
||||
wait_ready(200, 90)
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
def test_valkey_loss_degrades_then_postgres_loss_is_bounded(self) -> None:
|
||||
alice = common.actor("degrade")
|
||||
chat = common.create_chat(alice.token, [alice.id], "Устойчивость")
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Idempotency scopes and concurrent replay checks for laboratory 3."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
import common
|
||||
|
||||
|
||||
class IdempotencyScope(unittest.TestCase):
|
||||
def test_same_key_is_independent_for_different_users_and_chats(self) -> None:
|
||||
alice = common.actor("idemalice")
|
||||
eve = common.actor("idemeve")
|
||||
alice_chat = common.create_chat(alice.token, [alice.id], "Alice scope")
|
||||
eve_chat = common.create_chat(eve.token, [eve.id], "Eve scope")
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
first = common.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + alice_chat["id"] + "/messages",
|
||||
body={"text": "same body"},
|
||||
token=alice.token,
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
second = common.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + eve_chat["id"] + "/messages",
|
||||
body={"text": "same body"},
|
||||
token=eve.token,
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
self.assertEqual(first.status, 201, first.json)
|
||||
self.assertEqual(second.status, 201, second.json)
|
||||
self.assertNotEqual(first.json["id"], second.json["id"])
|
||||
self.assertEqual(second.json["chat_id"], eve_chat["id"])
|
||||
self.assertEqual(second.json["sender_id"], eve.id)
|
||||
|
||||
# A different payload in another scope is also a new independent write.
|
||||
other_scope = common.create_chat(eve.token, [eve.id], "Eve second scope")
|
||||
third = common.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + other_scope["id"] + "/messages",
|
||||
body={"text": "different body"},
|
||||
token=eve.token,
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
self.assertEqual(third.status, 201, third.json)
|
||||
self.assertEqual(third.json["chat_id"], other_scope["id"])
|
||||
|
||||
def test_concurrent_identical_replays_return_one_message_without_500(self) -> None:
|
||||
alice = common.actor("idemrace")
|
||||
chat = common.create_chat(alice.token, [alice.id], "Concurrent replay")
|
||||
key = uuid.uuid4().hex
|
||||
workers = 32
|
||||
barrier = threading.Barrier(workers)
|
||||
responses: list[common.Response] = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def post() -> None:
|
||||
barrier.wait()
|
||||
response = common.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
body={"text": "exactly once"},
|
||||
token=alice.token,
|
||||
headers={"Idempotency-Key": key},
|
||||
timeout=30,
|
||||
)
|
||||
with lock:
|
||||
responses.append(response)
|
||||
|
||||
threads = [threading.Thread(target=post) for _ in range(workers)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join(timeout=60)
|
||||
|
||||
self.assertEqual(len(responses), workers)
|
||||
self.assertTrue(all(response.status == 201 for response in responses), responses)
|
||||
message_ids = {response.json["id"] for response in responses}
|
||||
self.assertEqual(len(message_ids), 1)
|
||||
messages = common.history(alice.token, chat["id"])
|
||||
self.assertEqual([message["text"] for message in messages], ["exactly once"])
|
||||
Reference in New Issue
Block a user