tests: two questions at once with one credit, with real threads

The claim that the user-row lock plus SQLite IMMEDIATE stops one credit
being spent twice was untested. Two threads on separate connections now
ask at the same time: one answer, one credit spent; the same
Idempotency-Key arriving twice at once also costs one credit. Removing
transaction_mode IMMEDIATE turns both red (500, database is locked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 13:07:08 +03:30
parent fe658c697a
commit 67cbe05609
3 changed files with 77 additions and 4 deletions

View File

@@ -3,8 +3,7 @@
راه‌های شکستن که امتحان می‌شوند: اعتبارِ دوباره برای تأییدِ تکراریِ پرداخت، عوض شدنِ اشتراکِ فعالِ
کسی که بسته می‌خرد، خرج شدنِ اعتبار برای سوالی که به API نرسید یا جوابش ساخته نشد، و قلبِ هدیه که
در سرور اثری نداشته باشد. هم‌زمانیِ دو سوال با یک اعتبار این‌جا آزموده نشده؛ جلوگیری‌اش به قفلِ
ردیفِ کاربر و IMMEDIATEِ SQLite سپرده شده (ai_tutor.views، فازِ ۱).
در سرور اثری نداشته باشد. هم‌زمانیِ دو سوال با یک اعتبار در ai_tutor/tests_credit_concurrency.py است.
"""
from datetime import timedelta
from unittest.mock import patch