369 lines
20 KiB
Python
369 lines
20 KiB
Python
"""成交修正收件、幂等及执行隔离回归;仅SQLite内存库和内存业务桩。"""
|
||
import unit_test_guard # noqa: F401
|
||
import asyncio
|
||
import copy
|
||
import hashlib
|
||
import json
|
||
import sqlite3
|
||
import sys
|
||
import threading
|
||
import unittest
|
||
from contextlib import nullcontext, contextmanager
|
||
from datetime import datetime, timedelta
|
||
from pathlib import Path
|
||
from unittest.mock import patch
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
from config.settings import settings
|
||
from app.core import ws_codec as wsc
|
||
from app.repo import qmt_repo as q, execution_repo as er, rotation_repo as store
|
||
from app.services import execution_orders as eo, executor, dispatcher, execution_conflicts, daily_buy_review
|
||
from app.services import rotation_execution
|
||
from app.ws.runner import WsRunner
|
||
from test_wiring import install_fakes
|
||
|
||
REAL_PENDING, REAL_CORRECTIONS = er.pending_trades, er.pending_corrections
|
||
REAL_INBOX_PENDING = q.inbox_pending
|
||
NOW = datetime(2026, 11, 5, 10, 30)
|
||
CODE, IID, PARENT = '600000.SH', 'P_D01', 'P'
|
||
|
||
|
||
def correction(**extra):
|
||
return {'correction_id': 'C1', 'instruction_id': IID, 'broker_order_id': 'B1',
|
||
'trade_no': 'B1#1', 'ts_code': CODE, 'side': 'buy',
|
||
'before': {'qty': 100, 'price': 10, 'amount': 1000},
|
||
'after': {'qty': 80, 'price': 10, 'amount': 800}, **extra}
|
||
|
||
|
||
class TransportTests(unittest.TestCase):
|
||
def setUp(self):
|
||
self.conn = sqlite3.connect(':memory:', check_same_thread=False)
|
||
self.conn.row_factory = sqlite3.Row
|
||
self.conn.create_function('JSON_UNQUOTE', 1, lambda value: value)
|
||
self.conn.create_function('JSON_CONTAINS_PATH', 3,
|
||
lambda value, mode, path: int(path[2:] in json.loads(value)))
|
||
self.conn.create_function('JSON_CONTAINS', 3,
|
||
lambda value, candidate, path: int(json.loads(value).get(path[2:]) is False)
|
||
if candidate == 'false' else None)
|
||
self.conn.execute('CREATE TABLE pms_qmt_inbox (seq INTEGER PRIMARY KEY, msg_id TEXT, msg_type TEXT, '
|
||
'corr_id TEXT, dedup_key TEXT UNIQUE, payload_json TEXT, msg_ts INTEGER, received_at TEXT, '
|
||
'processed INTEGER, processed_at TEXT, process_note TEXT)')
|
||
patch.object(q, 'fetch_one', side_effect=self.one).start()
|
||
patch.object(q, 'fetch_all', side_effect=self.all).start()
|
||
patch.object(q, 'execute', side_effect=self.execute).start()
|
||
patch.object(er.db, 'fetch_one', side_effect=self.one).start()
|
||
patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False).start()
|
||
self.addCleanup(patch.stopall)
|
||
self.addCleanup(self.conn.close)
|
||
|
||
def one(self, sql, params=None):
|
||
row = self.conn.execute(sql, params or {}).fetchone()
|
||
return dict(row) if row else None
|
||
|
||
def all(self, sql, params=None):
|
||
return [dict(row) for row in self.conn.execute(sql, params or {}).fetchall()]
|
||
|
||
def execute(self, sql, params=None):
|
||
sql = sql.replace('ON DUPLICATE KEY UPDATE seq = seq', 'ON CONFLICT DO NOTHING')
|
||
params = {k: v.isoformat() if isinstance(v, datetime) else v for k, v in (params or {}).items()}
|
||
return self.conn.execute(sql, params).rowcount
|
||
|
||
def put(self, seq, payload=None, kind='trade_correction'):
|
||
payload = correction() if payload is None else payload
|
||
return q.inbox_put(seq=seq, msg_id=str(seq), msg_type=kind, payload=payload,
|
||
msg_ts=1, corr_id=payload.get('instruction_id'), dedup_key=wsc.dedup_key(kind, payload),
|
||
processed=0 if wsc.needs_ledger(kind, payload) else 2)
|
||
|
||
def row(self, seq):
|
||
return self.one('SELECT * FROM pms_qmt_inbox WHERE seq=:s', {'s': seq})
|
||
|
||
def test_message_classification_keeps_plain_false_ordinary_but_routes_malformed(self):
|
||
for payload in ({'amended': True}, {'amended': False, 'correction_id': 'C1'},
|
||
{'amended': None}, {'amended': 'false'}, {'amended': 0},
|
||
{'correction_id': None}, {'reason': 'trade_correction'}):
|
||
self.assertTrue(wsc.needs_ledger('order_update', payload))
|
||
self.assertFalse(wsc.needs_ledger('order_update', {'amended': False}))
|
||
self.assertFalse(wsc.needs_ledger('order_update', {'status': 'FILLED'}))
|
||
self.assertTrue(wsc.needs_ledger('trade', {}))
|
||
|
||
def test_keys_are_bounded_separate_and_not_original_trade_key(self):
|
||
body = correction(correction_id='任意编号'*200, amended=True)
|
||
a = wsc.dedup_key('trade_correction', body)
|
||
b = wsc.dedup_key('order_update', body)
|
||
self.assertLessEqual(len(a), 80)
|
||
self.assertLessEqual(len(b), 80)
|
||
self.assertNotEqual(a, b)
|
||
self.assertEqual(wsc.dedup_key('trade', body), 'trade:B1#1')
|
||
|
||
def test_same_id_same_content_archives_new_seq_without_second_work(self):
|
||
self.assertEqual(self.put(1), q.PUT_NEW)
|
||
self.assertEqual(self.put(2), q.PUT_DUP_KEY)
|
||
self.assertEqual(self.row(1)['processed'], 0)
|
||
self.assertEqual(self.row(2)['processed'], 2)
|
||
self.assertIsNone(self.row(2)['dedup_key'])
|
||
self.assertEqual(self.put(1), q.PUT_DUP_SEQ)
|
||
|
||
def test_same_id_different_content_is_held_and_original_stays_immutable(self):
|
||
self.put(1)
|
||
self.assertEqual(self.put(2, correction(after={'qty': 90, 'price': 10, 'amount': 900})), q.PUT_CONFLICT)
|
||
self.assertEqual(self.row(2)['processed'], 3)
|
||
self.assertIn('不同修正内容', self.row(2)['process_note'])
|
||
self.assertEqual(json.loads(self.row(1)['payload_json'])['after']['qty'], 80)
|
||
self.assertEqual(REAL_CORRECTIONS(), 2)
|
||
|
||
def test_missing_correction_id_and_identity_still_blocks_account_buys(self):
|
||
self.put(1, {'amended': True}, 'order_update')
|
||
self.assertEqual(self.row(1)['processed'], 3)
|
||
self.assertEqual(REAL_CORRECTIONS(), 1)
|
||
|
||
def test_amendment_and_correction_are_independent_pending_evidence_in_either_order(self):
|
||
self.put(1, correction(amended=True, reason='trade_correction'), 'order_update')
|
||
self.put(2)
|
||
self.assertEqual(REAL_PENDING([IID]), 2)
|
||
self.assertEqual(REAL_CORRECTIONS(ts_code=CODE), 2)
|
||
self.assertEqual(REAL_CORRECTIONS(ts_code='600001.SH'), 0)
|
||
self.assertEqual(len(q.inbox_for_order(IID)), 2)
|
||
self.assertEqual(q.inbox_by_key(wsc.dedup_key('trade_correction', correction()))['seq'], 2)
|
||
|
||
def test_correction_queue_cannot_starve_original_trades(self):
|
||
self.put(1)
|
||
self.put(2, correction(), 'trade')
|
||
rows = REAL_INBOX_PENDING(limit=1, msg_type='trade')
|
||
self.assertEqual([row['seq'] for row in rows], [2])
|
||
|
||
def test_ordinary_status_is_not_counted_as_pending_correction(self):
|
||
self.put(1, {'instruction_id': IID, 'ts_code': CODE, 'status': 'FILLED', 'amended': False}, 'order_update')
|
||
self.assertEqual(REAL_CORRECTIONS(), 0)
|
||
|
||
def test_sql_classification_matches_non_boolean_and_numbered_amendments(self):
|
||
for seq, payload in enumerate(({'amended': None}, {'amended': 0}, {'amended': 'false'},
|
||
{'amended': False, 'correction_id': 'C1'}, {'correction_id': None}), 1):
|
||
self.put(seq, {'instruction_id': IID, 'ts_code': CODE, **payload}, 'order_update')
|
||
self.assertEqual(REAL_CORRECTIONS(), seq)
|
||
|
||
def test_legacy_unknown_correction_is_not_mistaken_for_applied_or_duplicate(self):
|
||
self.put(1)
|
||
self.execute('UPDATE pms_qmt_inbox SET processed=2, process_note=:note WHERE seq=1',
|
||
{'note': 'trade_correction 由通道进程消化, 不入账'})
|
||
self.assertEqual(REAL_CORRECTIONS(), 1)
|
||
self.assertEqual(REAL_PENDING([IID]), 1)
|
||
self.execute('UPDATE pms_qmt_inbox SET process_note=:note WHERE seq=1', {'note': '重复修正占位'})
|
||
self.assertEqual(REAL_CORRECTIONS(), 0)
|
||
|
||
def test_ws_persistence_uses_payload_order_identity_and_pending_state(self):
|
||
runner = WsRunner()
|
||
with patch.object(runner, '_p', side_effect=lambda key, default=None: default):
|
||
asyncio.run(runner._persist_upstream({'seq': 1, 'type': 'order_update', 'msg_id': 'm1',
|
||
'ts': 1, 'corr_id': 'caller-other-id', 'payload': correction(amended=True)}))
|
||
self.assertEqual(self.row(1)['processed'], 0)
|
||
self.assertEqual(self.row(1)['corr_id'], IID)
|
||
|
||
def test_ws_never_applies_amended_or_correction_state_directly(self):
|
||
runner = WsRunner()
|
||
with patch.object(q, 'update_order', side_effect=AssertionError('修正不得直接改委托')), \
|
||
patch.object(q, 'get_order', side_effect=AssertionError('修正不得进入普通状态分支')):
|
||
for kind in ('trade_correction', 'order_update'):
|
||
asyncio.run(runner._apply_side_effects(kind, correction(amended=True), {'seq': 1}))
|
||
asyncio.run(runner._on_order_update(IID, correction(amended=False)))
|
||
|
||
|
||
class ExecutionBoundaryTests(unittest.TestCase):
|
||
def setUp(self):
|
||
self.fake = install_fakes(prices={CODE: 10}, params={'PMS_DISPATCH_MODE': 'ws', 'PMS_EXEC_REPRICE_ENABLED': False})
|
||
self.fake.qmt.set_channel()
|
||
self.fake.insert_instruction(instruction_id=PARENT, origin_type='strategy', origin_id='S',
|
||
ts_code=CODE, side='buy', action='OPEN', qty=300, status='DISPATCHED', progress={})
|
||
self.parent = self.fake.instructions[PARENT]
|
||
self.parent.update(book_id='original', exec_qty=80)
|
||
self.fake.qmt.enqueue_order(instruction_id=IID, parent_id=PARENT, ts_code=CODE,
|
||
side='buy', qty=100, limit_price=10, valid_until=1)
|
||
self.order = self.fake.qmt.orders[IID]
|
||
self.order.update(status='FILLED', cum_qty=80, leaves_qty=0, final_at=NOW-timedelta(minutes=10))
|
||
self.audit = {'instruction_id': IID, 'cum_qty': 80, 'correction_seq': 10, 'received_at': NOW.isoformat()}
|
||
patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True).start()
|
||
patch.object(store, 'get', side_effect=lambda key: (copy.deepcopy(self.audit), 1)
|
||
if key == 'order-correction:'+hashlib.sha256(IID.encode()).hexdigest() else (None, 0)).start()
|
||
patch.object(eo, 'account_guard', side_effect=nullcontext).start()
|
||
self.addCleanup(patch.stopall)
|
||
|
||
def pending(self, code=CODE, kind='trade_correction', processed=0):
|
||
self.fake.qmt.inbox[11] = {'seq': 11, 'msg_type': kind, 'processed': processed,
|
||
'payload': correction(ts_code=code, amended=True)}
|
||
|
||
def test_verified_quantity_reduction_has_one_consistent_effective_quantity(self):
|
||
self.assertEqual(eo.effective_filled(self.order), 80)
|
||
self.assertEqual(eo.replacement_state(PARENT)['remaining'], 220)
|
||
self.assertEqual(executor._parent_available(self.parent, []), 220)
|
||
self.assertEqual(daily_buy_review._orders(self.parent)[1], 80)
|
||
|
||
def test_unverified_or_mismatched_filled_reduction_is_rejected(self):
|
||
for audit in (None, {}, dict(self.audit, cum_qty=79), dict(self.audit, received_at='unknown')):
|
||
with patch.object(store, 'get', return_value=(audit, 1)):
|
||
with self.assertRaises(ValueError):
|
||
eo.effective_filled(self.order)
|
||
|
||
def test_quantity_hold_blocks_old_parent_both_sides_and_daily_reapproval(self):
|
||
self.parent['progress']['trade_correction_hold'] = {'correction_id': 'C1'}
|
||
for side in ('buy', 'sell'):
|
||
self.parent['side'] = side
|
||
self.assertTrue(eo.correction_blocker(self.parent))
|
||
self.assertEqual(executor._parent_available(self.parent, []), 0)
|
||
self.assertFalse(eo.should_replace(self.parent, {'action': 'FIRE', 'limit_price': 11}))
|
||
self.assertTrue(execution_conflicts.queued_buy_blocker(dict(self.order, side=side)))
|
||
result = daily_buy_review.process(now=NOW)
|
||
self.assertEqual(result['approved'], [])
|
||
|
||
def test_old_parent_hold_does_not_ban_new_manual_parent_on_same_code(self):
|
||
self.parent['progress']['trade_correction_hold'] = True
|
||
newer = dict(self.parent, instruction_id='NEW', progress={})
|
||
self.assertEqual(eo.correction_blocker(newer), '')
|
||
self.assertEqual(eo.code_settlement_blocker(CODE), '')
|
||
|
||
def test_quantity_hold_cannot_be_automatically_approved_by_daily_model(self):
|
||
from app.services import proposal_service
|
||
self.parent.update(status='WAIT_REVIEW', side='buy')
|
||
self.parent['progress'].update(trade_correction_hold=True,
|
||
daily_review={'ymd': 20261105, 'status': 'pending'})
|
||
with patch.object(daily_buy_review.param_store, 'get_bool', return_value=True), \
|
||
patch.object(proposal_service, 'scan_and_route', side_effect=AssertionError('修正不调用模型解锁')):
|
||
result = daily_buy_review.process(now=NOW)
|
||
self.assertEqual(result['approved'], [])
|
||
self.assertEqual(result['waiting'], [PARENT])
|
||
|
||
def test_direct_dispatch_hold_remains_effective_when_reprice_switch_off(self):
|
||
self.parent['progress']['trade_correction_hold'] = True
|
||
with patch.object(dispatcher, '_ws', side_effect=AssertionError('修正后不得再次下单')):
|
||
result = dispatcher.dispatch(instruction_id='P_D02', parent_id=PARENT, ts_code=CODE,
|
||
side='buy', qty=100, limit_price=10, valid_until=NOW)
|
||
self.assertFalse(result['ok'])
|
||
self.assertIn('成交数量已修正', result['error'])
|
||
|
||
def test_grid_parent_revision_must_match_current_strategy_after_correction(self):
|
||
self.parent['progress'].update(stype='GRID', strategy_correction_id=None)
|
||
strategy = {'type': 'GRID', 'status': 'ACTIVE', 'state': {}}
|
||
with patch.object(eo.pms_repo, 'get_strategy', return_value=strategy):
|
||
self.assertEqual(eo.correction_blocker(self.parent), '')
|
||
strategy['state']['trade_correction'] = {'correction_id': 'C1'}
|
||
self.assertIn('旧版本', eo.correction_blocker(self.parent))
|
||
self.parent['progress']['strategy_correction_id'] = 'C1'
|
||
self.assertEqual(eo.correction_blocker(self.parent), '')
|
||
strategy['state']['correction_hold'] = True
|
||
self.assertIn('正在核实', eo.correction_blocker(self.parent))
|
||
strategy['state']['correction_hold'] = False
|
||
strategy['status'] = 'PAUSED'
|
||
self.assertTrue(eo.correction_blocker(self.parent))
|
||
|
||
def test_missing_explicit_grid_strategy_fails_closed_without_changing_other_types(self):
|
||
self.parent['progress']['stype'] = 'GRID'
|
||
with patch.object(eo.pms_repo, 'get_strategy', return_value=None):
|
||
self.assertTrue(eo.correction_blocker(self.parent))
|
||
self.parent['progress']['stype'] = 'T0'
|
||
self.assertEqual(eo.correction_blocker(self.parent), '')
|
||
|
||
def test_pending_correction_blocks_all_new_buys_but_only_affected_sells(self):
|
||
self.pending(processed=3)
|
||
self.assertTrue(eo.correction_blocker({'side': 'buy', 'ts_code': '600001.SH'}))
|
||
self.assertTrue(eo.correction_blocker({'side': 'sell', 'ts_code': CODE}))
|
||
self.assertEqual(eo.correction_blocker({'side': 'sell', 'ts_code': '600001.SH'}), '')
|
||
self.assertFalse(eo.replacement_state(PARENT)['ready'])
|
||
|
||
def test_missing_payload_code_still_blocks_new_sell_parent_by_outbox_identity(self):
|
||
self.pending(code=None, processed=3)
|
||
self.assertTrue(eo.correction_blocker({'instruction_id': 'NEW', 'side': 'sell', 'ts_code': CODE}))
|
||
self.assertEqual(eo.correction_blocker({'instruction_id': 'OTHER', 'side': 'sell', 'ts_code': '600001.SH'}), '')
|
||
|
||
def test_final_send_mark_cannot_pass_pending_correction(self):
|
||
self.pending()
|
||
with patch.object(q, 'mark_send_attempt', side_effect=AssertionError('不能标记可发送')):
|
||
self.assertFalse(eo.prepare_send(IID)['marked'])
|
||
|
||
def test_ordinary_terminal_repeat_cannot_overwrite_corrected_quantity_or_price(self):
|
||
runner = WsRunner()
|
||
with patch.object(q, 'update_order', side_effect=AssertionError('旧终态不能覆盖修正')):
|
||
asyncio.run(runner._on_order_update(IID, {'status': 'FILLED', 'cum_qty': 100, 'cum_avg_price': 9}))
|
||
self.assertEqual(self.order['cum_qty'], 80)
|
||
|
||
def test_old_ordinary_update_cannot_overwrite_active_order_correction(self):
|
||
self.order.update(status='PARTIAL')
|
||
self.audit['amendment_seq'] = 12
|
||
runner = WsRunner()
|
||
with patch.object(q, 'update_order', side_effect=AssertionError('乱序旧回报不能覆盖修正')):
|
||
asyncio.run(runner._on_order_update(IID, {'status': 'PARTIAL', 'cum_qty': 90}, sequence=11))
|
||
with patch.object(q, 'update_order', return_value=1) as update:
|
||
asyncio.run(runner._on_order_update(IID, {'status': 'PARTIAL', 'cum_qty': 90}, sequence=13))
|
||
self.assertEqual(update.call_args.kwargs['cum_qty'], 90)
|
||
|
||
def test_legitimate_later_fills_do_not_make_old_correction_cutoff_unreadable(self):
|
||
self.order.update(status='FILLED', cum_qty=100)
|
||
self.assertEqual(eo.effective_filled(self.order), 100)
|
||
self.assertEqual(eo.settlement_cutoff(self.order).replace(tzinfo=None), NOW)
|
||
|
||
def test_normal_order_ack_and_reject_are_serialized_with_correction_commit(self):
|
||
for kind in ('order_update', 'reject', 'ack'):
|
||
with self.subTest(kind=kind):
|
||
lock, read, probe = threading.RLock(), threading.Event(), threading.Event()
|
||
acquired_without_wait, failures = [], []
|
||
self.order.update(status='PARTIAL', cum_qty=100, cancel_state='SENT')
|
||
self.audit = None
|
||
@contextmanager
|
||
def guarded():
|
||
with lock:
|
||
yield
|
||
def get_order(_):
|
||
old = copy.deepcopy(self.order)
|
||
read.set()
|
||
if not probe.wait(2):
|
||
raise AssertionError('交错测试没有进入竞争点')
|
||
return old
|
||
def amend():
|
||
try:
|
||
if not read.wait(2):
|
||
raise AssertionError('普通回报未进入读状态步骤')
|
||
acquired = lock.acquire(blocking=False)
|
||
acquired_without_wait.append(acquired)
|
||
if acquired:
|
||
lock.release()
|
||
probe.set()
|
||
with guarded():
|
||
self.order.update(status='FILLED', cum_qty=80)
|
||
self.audit = {'instruction_id': IID, 'cum_qty': 80,
|
||
'correction_seq': 20, 'received_at': NOW.isoformat()}
|
||
except BaseException as exc:
|
||
failures.append(exc)
|
||
probe.set()
|
||
def update(_, **fields):
|
||
self.assertEqual(acquired_without_wait, [False], '修正不能插入普通回报读写之间')
|
||
self.order.update(fields)
|
||
return 1
|
||
thread = threading.Thread(target=amend, daemon=True)
|
||
with patch.object(eo, 'account_guard', side_effect=guarded), \
|
||
patch.object(q, 'get_order', side_effect=get_order), \
|
||
patch.object(q, 'update_order', side_effect=update):
|
||
thread.start()
|
||
runner = WsRunner()
|
||
payload = {'instruction_id': IID, 'status': 'FILLED', 'cum_qty': 100,
|
||
'code': 'ALREADY_FINAL', 'final': True}
|
||
asyncio.run(runner._apply_side_effects(kind, payload, {'seq': 19}))
|
||
thread.join(2)
|
||
self.assertFalse(thread.is_alive())
|
||
self.assertEqual(failures, [])
|
||
self.assertEqual(acquired_without_wait, [False])
|
||
self.assertEqual(self.order['cum_qty'], 80)
|
||
|
||
def test_old_snapshot_does_not_release_corrected_reservation(self):
|
||
account = {'orders': {IID: 1005.}}
|
||
rotation_execution.release_settled_reservations(account, {'received_at': NOW-timedelta(seconds=1)})
|
||
self.assertEqual(account['orders'][IID], 1005.)
|
||
rotation_execution.release_settled_reservations(account, {'received_at': NOW+timedelta(seconds=1)})
|
||
self.assertEqual(account['orders'][IID], 805.)
|
||
|
||
def test_unpaired_correction_blocks_reservation_release_even_with_new_snapshot(self):
|
||
self.pending()
|
||
account = {'orders': {IID: 1005.}}
|
||
rotation_execution.release_settled_reservations(account, {'received_at': NOW+timedelta(minutes=2)})
|
||
self.assertEqual(account['orders'][IID], 1005.)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main(verbosity=2)
|