Files
trex-core/tests/application/test_intent_concurrency.py
T
genichinandClaude Opus 4.8 aee3512383 feat(intents): dcy-011 buy intent portfolio_id 필수 계약 + ledger skip 가시화 (BAT-015)
dcy-011 전 주기 delivery. issue #6(buy 식별자 정정) + issue #11(ledger silent skip) 흡수.
거버넌스: 기존 Implemented REQ는 change-req, 신규 기능만 신규 REQ(OQ-3).

- REQ-007 (change-req): buy admission portfolio_id 필수화 + SnapshotStore symbol
  resolve + unknown_portfolio 거부 (hard cutover, OQ-1/OQ-2)
- REQ-021 (change-req): buy 메시지 payload symbol 제거
- REQ-018 (change-req): _record_ledger skip 가시화 (WARN + ledger_skipped 카운터)
- REQ-055 (신규): closed↔ledger_outbox anti-join 운영 점검 find_ledger_gap_slots

코드: intents.py(BuyIntent portfolio_id 필수 + resolve_symbol 주입 + _admit_buy
reject 분기), messaging_handlers.py, runtime/daemon.py(_resolve_symbol 배선),
execution.py(skip WARN 로그), adapters/persistence/ops_checks.py(신규 anti-join).

검증: 단위 282 passed/32 skipped, 인프라 게이트(PG+RabbitMQ) 314 passed, ruff clean.
BAT-015(standard) release-candidate. srs/index Next ID → REQ-056.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:57:51 +09:00

75 lines
2.8 KiB
Python

"""Concurrency / single-writer test for IntentService (REQ-007 NFR-2/NFR-3).
Many threads submit buy intents competing for a limited number of idle slots;
exactly that many must be admitted and no slot may be double-used.
"""
from __future__ import annotations
import threading
from datetime import date
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
from trex_core.application.intents import BuyIntent, IntentService
from trex_core.domain.slot import SlotService, SlotState
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
DAY = date(2026, 6, 3)
def test_concurrent_buys_admit_exactly_idle_count():
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
svc.preallocate_daily_slots(DAY, 5) # only 5 idle slots
results: list[str] = []
results_lock = threading.Lock()
def worker(i: int) -> None:
d = intents.submit_buy(BuyIntent(intent_id=f"buy-{i}", trading_date=DAY, symbol="005930", qty=1, portfolio_id=f"PF-{i}"))
with results_lock:
results.append(d.status)
threads = [threading.Thread(target=worker, args=(i,)) for i in range(20)]
for t in threads:
t.start()
for t in threads:
t.join()
admitted = [s for s in results if s == "admitted"]
rejected = [s for s in results if s == "rejected"]
assert len(admitted) == 5 # exactly the idle slots
assert len(rejected) == 15
# every non-idle slot is pending_buy exactly once (no double-use)
pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY]
idle = [s for s in repo.list_slots(DAY) if s.status is SlotState.IDLE]
assert len(pending) == 5 and len(idle) == 0
def test_duplicate_intent_id_admits_once_under_concurrency():
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
svc.preallocate_daily_slots(DAY, 5)
statuses: list[str] = []
lock = threading.Lock()
def worker() -> None:
d = intents.submit_buy(BuyIntent(intent_id="same-id", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
with lock:
statuses.append(d.status)
threads = [threading.Thread(target=worker) for _ in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
# same intent_id submitted 10x concurrently -> exactly ONE slot transitioned
# (the core idempotency guarantee). Racing duplicates observe the winner's
# decision, which may still be 'processing' before finalize; never 'rejected'.
pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY]
assert len(pending) == 1
assert all(s in ("admitted", "processing") for s in statuses)