fix: harden skipped ledger resolution paths
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
b88b21b772
commit
48dfc3e76f
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from datetime import UTC, date, datetime
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -87,3 +88,76 @@ def test_ledger_skip_store_filters_and_resolves(pg_clean):
|
||||
assert len(resolved) == 1
|
||||
assert resolved[0].status == "resolved"
|
||||
assert resolved[0].resolution_reason == "manual_ledger_backfill"
|
||||
|
||||
|
||||
def test_ledger_skip_store_resolves_null_execution_id_on_backfill(pg_clean):
|
||||
store = PostgreSQLLedgerSkipStore(pg_clean)
|
||||
slot_id = uuid4()
|
||||
record, _ = store.record_skip(
|
||||
instance_key="inst-1",
|
||||
slot_id=slot_id,
|
||||
portfolio_id="PF-1",
|
||||
symbol="005930",
|
||||
order_id="O-3",
|
||||
execution_id=None,
|
||||
side="buy",
|
||||
qty=1.0,
|
||||
price=1010.0,
|
||||
executed_at=datetime(2026, 8, 24, 3, 0, tzinfo=UTC),
|
||||
order_venue="SOR",
|
||||
execution_venue="unknown",
|
||||
skip_reason="missing_execution_id",
|
||||
)
|
||||
|
||||
resolved = store.resolve_for_fill(
|
||||
instance_key="inst-1",
|
||||
slot_id=slot_id,
|
||||
side="buy",
|
||||
order_id="O-3",
|
||||
execution_id="E-3",
|
||||
resolution_reason="manual_ledger_backfill",
|
||||
)
|
||||
|
||||
assert [row.skip_key for row in resolved] == [record.skip_key]
|
||||
assert resolved[0].execution_id == "E-3"
|
||||
assert resolved[0].status == "resolved"
|
||||
|
||||
|
||||
def test_ledger_skip_store_retries_unique_violation_during_concurrent_dedupe(pg_clean):
|
||||
slot_id = uuid4()
|
||||
start = threading.Barrier(2)
|
||||
results = []
|
||||
|
||||
def worker():
|
||||
store = PostgreSQLLedgerSkipStore(pg_clean)
|
||||
start.wait()
|
||||
results.append(
|
||||
store.record_skip(
|
||||
instance_key="inst-1",
|
||||
slot_id=slot_id,
|
||||
portfolio_id="PF-1",
|
||||
symbol="005930",
|
||||
order_id="O-4",
|
||||
execution_id=None,
|
||||
side="buy",
|
||||
qty=1.0,
|
||||
price=1111.0,
|
||||
executed_at=datetime(2026, 8, 24, 4, 0, tzinfo=UTC),
|
||||
order_venue="SOR",
|
||||
execution_venue="unknown",
|
||||
skip_reason="missing_execution_id",
|
||||
)
|
||||
)
|
||||
|
||||
t1 = threading.Thread(target=worker)
|
||||
t2 = threading.Thread(target=worker)
|
||||
t1.start()
|
||||
t2.start()
|
||||
t1.join()
|
||||
t2.join()
|
||||
|
||||
assert len(results) == 2
|
||||
assert len({record.skip_key for record, _notify in results}) == 1
|
||||
assert sorted(notify for _record, notify in results) == [False, True]
|
||||
listed = PostgreSQLLedgerSkipStore(pg_clean).list_records(instance_key="inst-1", status="unresolved")
|
||||
assert len(listed) == 1
|
||||
|
||||
Reference in New Issue
Block a user