Make message retries idempotent across replicas

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 11:36:23 +03:00
parent f295b716fe
commit 44fcce080f
5 changed files with 102 additions and 6 deletions
+3 -3
View File
@@ -30,7 +30,7 @@ class DatabaseTests(unittest.TestCase):
def test_migration_and_unique_username_race(self):
with psycopg.connect(URL) as conn:
versions = conn.execute("SELECT version FROM schema_migrations").fetchall()
self.assertEqual(versions, [(1,), (2,)])
self.assertEqual(versions, [(1,), (2,), (3,)])
name = "u_" + uuid4().hex[:16]
def register(_):
@@ -67,8 +67,8 @@ class DatabaseTests(unittest.TestCase):
chat_page = self.store.list_chats(alice["id"], 1, None)
self.assertEqual(chat_page["items"], [chat])
self.assertEqual(self.store.list_chats(alice["id"], 1, chat_page["next_cursor"]), {"items": [chat2], "next_cursor": None})
first = self.store.create_message(alice["id"], chat["id"], "первое")
second = self.store.create_message(bob["id"], chat["id"], "второе")
first = self.store.create_message(alice["id"], chat["id"], "первое", uuid4().hex)
second = self.store.create_message(bob["id"], chat["id"], "второе", uuid4().hex)
page = self.store.list_messages(alice["id"], chat["id"], 1, None)
self.assertEqual(page["items"], [first])
self.assertEqual(self.store.list_messages(bob["id"], chat["id"], 1, page["next_cursor"]), {"items": [second], "next_cursor": None})
+53
View File
@@ -0,0 +1,53 @@
"""Concurrent PostgreSQL-backed message retries create one row."""
import os
import subprocess
import sys
import unittest
from concurrent.futures import ThreadPoolExecutor
from uuid import uuid4
import psycopg
from src.store import Store
URL = os.environ.get("DATABASE_URL")
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
class IdempotencyTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
cls.store = Store()
def test_parallel_replay_conflict_and_scope(self):
alice = self.store.register("u_" + uuid4().hex[:16], "Алиса", "Test-password-123")
bob = self.store.register("u_" + uuid4().hex[:16], "Боб", "Test-password-123")
chat = self.store.create_chat(alice["id"], "Test", [alice["id"], bob["id"]])
other_chat = self.store.create_chat(alice["id"], "Other", [alice["id"]])
key = uuid4().hex
def send(_):
return Store().create_message(alice["id"], chat["id"], "same body", key)
with ThreadPoolExecutor(max_workers=2) as pool:
first, replay = list(pool.map(send, range(2)))
self.assertEqual(first, replay)
with psycopg.connect(URL) as conn:
count = conn.execute(
"SELECT count(*) FROM messages WHERE chat_id=%s AND sender_id=%s AND text=%s",
(chat["id"], alice["id"], "same body"),
).fetchone()[0]
self.assertEqual(count, 1)
with self.assertRaises(Exception) as conflict:
self.store.create_message(alice["id"], chat["id"], "different body", key)
self.assertEqual(conflict.exception.status_code, 409)
self.assertNotEqual(self.store.create_message(bob["id"], chat["id"], "same body", key)["id"], first["id"])
self.assertNotEqual(self.store.create_message(alice["id"], other_chat["id"], "same body", key)["id"], first["id"])
if __name__ == "__main__":
unittest.main()