forked from mtusi2026/mtusi2026lab5
formatted project by python-black
This commit is contained in:
@@ -16,10 +16,15 @@ from helpers import bearer, create_chat, create_user, idempotency_header, send_m
|
||||
@pytest.fixture
|
||||
async def replicas(request, make_settings):
|
||||
overrides = getattr(request, "param", {})
|
||||
apps = [create_app(make_settings(instance_id=name, **overrides)) for name in ("api-a", "api-b")]
|
||||
apps = [
|
||||
create_app(make_settings(instance_id=name, **overrides))
|
||||
for name in ("api-a", "api-b")
|
||||
]
|
||||
try:
|
||||
async with AsyncExitStack() as stack:
|
||||
clients = [await stack.enter_async_context(make_client(app)) for app in apps]
|
||||
clients = [
|
||||
await stack.enter_async_context(make_client(app)) for app in apps
|
||||
]
|
||||
yield clients
|
||||
finally:
|
||||
for app in apps:
|
||||
@@ -54,7 +59,11 @@ async def test_replay_and_conflict_across_replicas(replicas, db_engine):
|
||||
original = await send_message(first, user, chat, "Привет", "same-key")
|
||||
repeated = await second.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers={**bearer(user), **idempotency_header("same-key"), "Content-Type": "application/json"},
|
||||
headers={
|
||||
**bearer(user),
|
||||
**idempotency_header("same-key"),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
content='{ "text" : "\\u041f\\u0440\\u0438\\u0432\\u0435\\u0442" }',
|
||||
)
|
||||
conflict = await send_message(second, user, chat, "другой текст", "same-key")
|
||||
@@ -63,23 +72,38 @@ async def test_replay_and_conflict_across_replicas(replicas, db_engine):
|
||||
assert original.headers["X-Instance-Id"] != repeated.headers["X-Instance-Id"]
|
||||
assert conflict.status_code == 409
|
||||
async with db_engine.connect() as connection:
|
||||
assert (await connection.execute(text("SELECT count(*) FROM messages"))).scalar_one() == 1
|
||||
assert (await connection.execute(text("SELECT value FROM counters WHERE name='messages'"))).scalar_one() == 1
|
||||
assert (
|
||||
await connection.execute(text("SELECT count(*) FROM messages"))
|
||||
).scalar_one() == 1
|
||||
assert (
|
||||
await connection.execute(
|
||||
text("SELECT value FROM counters WHERE name='messages'")
|
||||
)
|
||||
).scalar_one() == 1
|
||||
|
||||
|
||||
async def test_parallel_replays_create_one_message(replicas, db_engine):
|
||||
user = await create_user(replicas[0])
|
||||
chat = await create_chat(replicas[0], user)
|
||||
responses = await asyncio.gather(*(
|
||||
send_message(replicas[index % 2], user, chat, "parallel", "one-key")
|
||||
for index in range(24)
|
||||
))
|
||||
responses = await asyncio.gather(
|
||||
*(
|
||||
send_message(replicas[index % 2], user, chat, "parallel", "one-key")
|
||||
for index in range(24)
|
||||
)
|
||||
)
|
||||
assert all(response.status_code == 201 for response in responses)
|
||||
assert len({response.json()["id"] for response in responses}) == 1
|
||||
assert {response.headers["X-Instance-Id"] for response in responses} == {"api-a", "api-b"}
|
||||
assert {response.headers["X-Instance-Id"] for response in responses} == {
|
||||
"api-a",
|
||||
"api-b",
|
||||
}
|
||||
async with db_engine.connect() as connection:
|
||||
assert (await connection.execute(text("SELECT count(*) FROM messages"))).scalar_one() == 1
|
||||
assert (await connection.execute(text("SELECT count(*) FROM idempotency_keys"))).scalar_one() == 1
|
||||
assert (
|
||||
await connection.execute(text("SELECT count(*) FROM messages"))
|
||||
).scalar_one() == 1
|
||||
assert (
|
||||
await connection.execute(text("SELECT count(*) FROM idempotency_keys"))
|
||||
).scalar_one() == 1
|
||||
|
||||
|
||||
async def test_key_scope_separates_users_and_chats(client):
|
||||
@@ -101,9 +125,11 @@ async def test_expired_key_can_be_reused_without_removing_message(client, db_eng
|
||||
chat = await create_chat(client, user)
|
||||
first = await send_message(client, user, chat, "old", "expiring-key")
|
||||
async with db_engine.begin() as connection:
|
||||
await connection.execute(text(
|
||||
"UPDATE idempotency_keys SET created_at = clock_timestamp() - interval '25 hours'"
|
||||
))
|
||||
await connection.execute(
|
||||
text(
|
||||
"UPDATE idempotency_keys SET created_at = clock_timestamp() - interval '25 hours'"
|
||||
)
|
||||
)
|
||||
second = await send_message(client, user, chat, "new", "expiring-key")
|
||||
assert first.status_code == second.status_code == 201
|
||||
assert first.json()["id"] != second.json()["id"]
|
||||
@@ -127,7 +153,9 @@ async def test_failed_transaction_does_not_poison_key(client, db_engine, monkeyp
|
||||
assert failed.status_code == 503
|
||||
async with db_engine.connect() as connection:
|
||||
for table in ("messages", "idempotency_keys", "counters"):
|
||||
assert (await connection.execute(text(f"SELECT count(*) FROM {table}"))).scalar_one() == 0
|
||||
assert (
|
||||
await connection.execute(text(f"SELECT count(*) FROM {table}"))
|
||||
).scalar_one() == 0
|
||||
monkeypatch.setattr(key_storage, "save_result", original)
|
||||
recovered = await send_message(client, user, chat, "retry", "retry-key")
|
||||
assert recovered.status_code == 201
|
||||
@@ -148,9 +176,15 @@ async def test_replay_works_when_message_limit_is_full(replicas):
|
||||
async def test_replay_checks_session_and_membership(client, db_engine):
|
||||
user = await create_user(client)
|
||||
chat = await create_chat(client, user)
|
||||
assert (await send_message(client, user, chat, "private", "private-key")).status_code == 201
|
||||
assert (
|
||||
await send_message(client, user, chat, "private", "private-key")
|
||||
).status_code == 201
|
||||
async with db_engine.begin() as connection:
|
||||
await connection.execute(text("DELETE FROM chat_members"))
|
||||
assert (await send_message(client, user, chat, "private", "private-key")).status_code == 404
|
||||
assert (
|
||||
await send_message(client, user, chat, "private", "private-key")
|
||||
).status_code == 404
|
||||
await client.delete("/api/v1/auth/sessions/current", headers=bearer(user))
|
||||
assert (await send_message(client, user, chat, "private", "private-key")).status_code == 401
|
||||
assert (
|
||||
await send_message(client, user, chat, "private", "private-key")
|
||||
).status_code == 401
|
||||
|
||||
Reference in New Issue
Block a user