Fix replica auth and idempotency races

This commit is contained in:
Tmribob
2026-11-06 16:13:24 +03:00
parent 57c81ea9d5
commit 057877d299
22 changed files with 682 additions and 60 deletions
+52 -4
View File
@@ -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], "Устойчивость")