"""单连接流水线(execute_pipeline)单元测试:不连真实网络。 关键防线:流水线把多条请求塞进同一条 TCP 流,一旦响应与请求错配就是 「价格张冠李戴」级别的灾难,所以这里逐项锁死顺序、长度与失败语义。 """ from __future__ import annotations import struct import unittest.mock as mock import pytest from pytdxdata.pool.connection import TdxConnection from pytdxdata.pool.pool import PooledConnection from pytdxdata.protocol.commands.base import BaseCommand class EchoCmd(BaseCommand[bytes]): """请求体即 tag,响应体原样带回,便于断言顺序。""" def __init__(self, tag: bytes) -> None: self.tag = tag def build_request(self) -> bytes: return b"REQ:" + self.tag def parse_response(self, body: bytes) -> bytes: return body class FakeQuote: """最小报价替身:stamp() 会写 symbol,需要 market/code 是可 int/str 的真值。""" def __init__(self) -> None: self.market = 0 self.code = "000001" self.symbol = "" def frame(body: bytes) -> bytes: """构造一帧未压缩响应(zipsize == unzipsize)。""" return struct.pack(" None: self.data = data self.pos = 0 async def read(self, n: int) -> bytes: chunk = self.data[self.pos:self.pos + n] self.pos += len(chunk) return chunk class FakeWriter: def __init__(self) -> None: self.written = b"" self.closed = False self.transport = FakeQuote() def write(self, data: bytes) -> None: self.written += data async def drain(self) -> None: pass def is_closing(self) -> bool: return self.closed def close(self) -> None: self.closed = True async def wait_closed(self) -> None: pass def make_conn(payload: bytes, host: str = "1.2.3.4") -> TdxConnection: conn = TdxConnection(host, 7709, 5.0) conn._reader = FakeReader(payload) # type: ignore[assignment] conn._writer = FakeWriter() # type: ignore[assignment] return conn # ---------- 基本语义 ---------- @pytest.mark.asyncio async def test_empty_cmds_returns_empty(): conn = make_conn(b"") assert await conn.execute_pipeline([]) == [] @pytest.mark.asyncio async def test_responses_paired_in_order(): bodies = [b"AAA", b"BBB", b"CCC"] conn = make_conn(b"".join(frame(b) for b in bodies)) cmds = [EchoCmd(t) for t in (b"1", b"2", b"3")] got = await conn.execute_pipeline(cmds) assert got == bodies # 顺序必须与 cmds 严格一致 @pytest.mark.asyncio async def test_all_requests_sent_at_once(): conn = make_conn(b"".join(frame(b"X") for _ in range(3))) cmds = [EchoCmd(t) for t in (b"a", b"b", b"c")] await conn.execute_pipeline(cmds) assert conn._writer.written == b"REQ:aREQ:bREQ:c" @pytest.mark.asyncio async def test_closed_connection_raises(): conn = make_conn(b"") conn._writer = None with pytest.raises(ConnectionError): await conn.execute_pipeline([EchoCmd(b"1")]) # ---------- 失败语义:响应流错配后必须废连接 ---------- @pytest.mark.asyncio async def test_truncated_stream_force_closes(): # 只给第一帧完整的、第二帧只有半个头 → 读不满,抛错且连接作废 conn = make_conn(frame(b"AAA") + b"\x00" * 5) with pytest.raises(ConnectionError): await conn.execute_pipeline([EchoCmd(b"1"), EchoCmd(b"2")]) assert conn._writer is None # _force_close 已清空 writer @pytest.mark.asyncio async def test_body_length_mismatch_propagates(): bad = struct.pack("