mirror of
https://ghfast.top/https://github.com/aeroxw/easy-tdx.git
synced 2026-09-12 23:54:17 +08:00
- Package directory: src/xmtdx/ -> src/easy_tdx/ - Import path: from easy_tdx import ... - pip install easy-tdx - Add LICENSE (MIT) with upstream attribution (pytdx, xmtdx) - Add NOTICE with detailed attribution - Update all examples, tests, scripts, docs - Bump version to 1.0.0 BREAKING CHANGE: import path changed from `xmtdx` to `easy_tdx` Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""同步 transport 回归测试。"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
from easy_tdx.exceptions import TdxConnectionError
|
|
from easy_tdx.transport.sync import TdxConnection
|
|
|
|
|
|
class _FakeSocket:
|
|
def __init__(self) -> None:
|
|
self.timeout: float | None = None
|
|
self.connected_to: tuple[str, int] | None = None
|
|
self.closed = False
|
|
|
|
def settimeout(self, timeout: float) -> None:
|
|
self.timeout = timeout
|
|
|
|
def connect(self, address: tuple[str, int]) -> None:
|
|
self.connected_to = address
|
|
|
|
def close(self) -> None:
|
|
self.closed = True
|
|
|
|
|
|
def test_sync_connection_closes_socket_when_setup_fails() -> None:
|
|
sock = _FakeSocket()
|
|
conn = TdxConnection("127.0.0.1", port=7709, timeout=0.2)
|
|
|
|
with patch("easy_tdx.transport.sync.socket.socket", return_value=sock), patch.object(
|
|
TdxConnection,
|
|
"_send_setup",
|
|
side_effect=TdxConnectionError("setup failed"),
|
|
):
|
|
try:
|
|
conn.connect()
|
|
except TdxConnectionError as exc:
|
|
assert "setup failed" in str(exc)
|
|
else: # pragma: no cover - 防御性断言
|
|
raise AssertionError("expected setup failure")
|
|
|
|
assert sock.timeout == 0.2
|
|
assert sock.connected_to == ("127.0.0.1", 7709)
|
|
assert sock.closed is True
|
|
assert conn._sock is None
|